Skip to content

Commit f5d6979

Browse files
committed
extmod: Add uheapq module.
1 parent e72be1b commit f5d6979

9 files changed

Lines changed: 192 additions & 0 deletions

File tree

extmod/moduheapq.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <unistd.h>
28+
29+
#include "mpconfig.h"
30+
#include "misc.h"
31+
#include "nlr.h"
32+
#include "qstr.h"
33+
#include "obj.h"
34+
#include "objlist.h"
35+
#include "runtime0.h"
36+
#include "runtime.h"
37+
38+
#if MICROPY_PY_UHEAPQ
39+
40+
// the algorithm here is modelled on CPython's heapq.py
41+
42+
STATIC mp_obj_list_t *get_heap(mp_obj_t heap_in) {
43+
if (!MP_OBJ_IS_TYPE(heap_in, &mp_type_list)) {
44+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "heap must be a list"));
45+
}
46+
return heap_in;
47+
}
48+
49+
STATIC void heap_siftdown(mp_obj_list_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
50+
mp_obj_t item = heap->items[pos];
51+
while (pos > start_pos) {
52+
mp_uint_t parent_pos = (pos - 1) >> 1;
53+
mp_obj_t parent = heap->items[parent_pos];
54+
if (mp_binary_op(MP_BINARY_OP_LESS, item, parent) == mp_const_true) {
55+
heap->items[pos] = parent;
56+
pos = parent_pos;
57+
} else {
58+
break;
59+
}
60+
}
61+
heap->items[pos] = item;
62+
}
63+
64+
STATIC void heap_siftup(mp_obj_list_t *heap, mp_uint_t pos) {
65+
mp_uint_t start_pos = pos;
66+
mp_uint_t end_pos = heap->len;
67+
mp_obj_t item = heap->items[pos];
68+
for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
69+
// choose right child if it's <= left child
70+
if (child_pos + 1 < end_pos && mp_binary_op(MP_BINARY_OP_LESS, heap->items[child_pos], heap->items[child_pos + 1]) == mp_const_false) {
71+
child_pos += 1;
72+
}
73+
// bubble up the smaller child
74+
heap->items[pos] = heap->items[child_pos];
75+
pos = child_pos;
76+
}
77+
heap->items[pos] = item;
78+
heap_siftdown(heap, start_pos, pos);
79+
}
80+
81+
STATIC mp_obj_t mod_uheapq_heappush(mp_obj_t heap_in, mp_obj_t item) {
82+
mp_obj_list_t *heap = get_heap(heap_in);
83+
mp_obj_list_append(heap, item);
84+
heap_siftdown(heap, 0, heap->len - 1);
85+
return mp_const_none;
86+
}
87+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_uheapq_heappush_obj, mod_uheapq_heappush);
88+
89+
STATIC mp_obj_t mod_uheapq_heappop(mp_obj_t heap_in) {
90+
mp_obj_list_t *heap = get_heap(heap_in);
91+
if (heap->len == 0) {
92+
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
93+
}
94+
mp_obj_t item = heap->items[0];
95+
heap->len -= 1;
96+
heap->items[0] = heap->items[heap->len];
97+
heap->items[heap->len] = MP_OBJ_NULL; // so we don't retain a pointer
98+
if (heap->len) {
99+
heap_siftup(heap, 0);
100+
}
101+
return item;
102+
}
103+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heappop_obj, mod_uheapq_heappop);
104+
105+
STATIC mp_obj_t mod_uheapq_heapify(mp_obj_t heap_in) {
106+
mp_obj_list_t *heap = get_heap(heap_in);
107+
for (mp_uint_t i = heap->len / 2; i > 0;) {
108+
heap_siftup(heap, --i);
109+
}
110+
return mp_const_none;
111+
}
112+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heapify_obj, mod_uheapq_heapify);
113+
114+
STATIC const mp_map_elem_t mp_module_uheapq_globals_table[] = {
115+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uheapq) },
116+
{ MP_OBJ_NEW_QSTR(MP_QSTR_heappush), (mp_obj_t)&mod_uheapq_heappush_obj },
117+
{ MP_OBJ_NEW_QSTR(MP_QSTR_heappop), (mp_obj_t)&mod_uheapq_heappop_obj },
118+
{ MP_OBJ_NEW_QSTR(MP_QSTR_heapify), (mp_obj_t)&mod_uheapq_heapify_obj },
119+
};
120+
121+
STATIC const mp_obj_dict_t mp_module_uheapq_globals = {
122+
.base = {&mp_type_dict},
123+
.map = {
124+
.all_keys_are_qstrs = 1,
125+
.table_is_fixed_array = 1,
126+
.used = MP_ARRAY_SIZE(mp_module_uheapq_globals_table),
127+
.alloc = MP_ARRAY_SIZE(mp_module_uheapq_globals_table),
128+
.table = (mp_map_elem_t*)mp_module_uheapq_globals_table,
129+
},
130+
};
131+
132+
const mp_obj_module_t mp_module_uheapq = {
133+
.base = { &mp_type_module },
134+
.name = MP_QSTR_uheapq,
135+
.globals = (mp_obj_dict_t*)&mp_module_uheapq_globals,
136+
};
137+
138+
#endif //MICROPY_PY_UHEAPQ

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ extern const mp_obj_module_t mp_module_uctypes;
9191
extern const mp_obj_module_t mp_module_uzlib;
9292
extern const mp_obj_module_t mp_module_ujson;
9393
extern const mp_obj_module_t mp_module_ure;
94+
extern const mp_obj_module_t mp_module_uheapq;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
211211
#if MICROPY_PY_URE
212212
{ MP_OBJ_NEW_QSTR(MP_QSTR_ure), (mp_obj_t)&mp_module_ure },
213213
#endif
214+
#if MICROPY_PY_UHEAPQ
215+
{ MP_OBJ_NEW_QSTR(MP_QSTR_uheapq), (mp_obj_t)&mp_module_uheapq },
216+
#endif
214217

215218
// extra builtin modules as defined by a port
216219
MICROPY_PORT_BUILTIN_MODULES

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ typedef double mp_float_t;
403403
#define MICROPY_PY_URE (0)
404404
#endif
405405

406+
#ifndef MICROPY_PY_UHEAPQ
407+
#define MICROPY_PY_UHEAPQ (0)
408+
#endif
409+
406410
/*****************************************************************************/
407411
/* Hooks for a port to add builtins */
408412

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ PY_O_BASENAME = \
114114
../extmod/modujson.o \
115115
../extmod/modure.o \
116116
../extmod/moduzlib.o \
117+
../extmod/moduheapq.o \
117118

118119
# prepend the build destination prefix to the py object files
119120
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))

py/qstrdefs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,10 @@ Q(search)
485485
Q(group)
486486
Q(DEBUG)
487487
#endif
488+
489+
#if MICROPY_PY_UHEAPQ
490+
Q(uheapq)
491+
Q(heappush)
492+
Q(heappop)
493+
Q(heapify)
494+
#endif

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define MICROPY_PY_UZLIB (1)
6161
#define MICROPY_PY_UJSON (1)
6262
#define MICROPY_PY_URE (1)
63+
#define MICROPY_PY_UHEAPQ (1)
6364

6465
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
6566
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)

tests/extmod/uheapq1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
try:
2+
import uheapq as heapq
3+
except:
4+
import heapq
5+
6+
try:
7+
heapq.heappop([])
8+
except IndexError:
9+
print("IndexError")
10+
11+
try:
12+
heapq.heappush((), 1)
13+
except TypeError:
14+
print("TypeError")
15+
16+
def pop_and_print(h):
17+
l = []
18+
while h:
19+
l.append(str(heapq.heappop(h)))
20+
print(' '.join(l))
21+
22+
h = []
23+
heapq.heappush(h, 3)
24+
heapq.heappush(h, 1)
25+
heapq.heappush(h, 2)
26+
print(h)
27+
pop_and_print(h)
28+
29+
h = [4, 3, 8, 9, 10, 2, 7, 11, 5]
30+
heapq.heapify(h)
31+
print(h)
32+
heapq.heappush(h, 1)
33+
heapq.heappush(h, 6)
34+
heapq.heappush(h, 12)
35+
print(h)
36+
pop_and_print(h)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define MICROPY_PY_UZLIB (1)
5959
#define MICROPY_PY_UJSON (1)
6060
#define MICROPY_PY_URE (1)
61+
#define MICROPY_PY_UHEAPQ (1)
6162

6263
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
6364
// names in exception messages (may require more RAM).

0 commit comments

Comments
 (0)