2020-06-03 18:40:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
|
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
2014-10-22 13:37:18 -04:00
|
|
|
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/objlist.h"
|
|
|
|
#include "py/runtime.h"
|
2014-10-22 13:37:18 -04:00
|
|
|
|
2018-08-16 16:34:12 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2014-10-22 13:37:18 -04:00
|
|
|
#if MICROPY_PY_UHEAPQ
|
|
|
|
|
|
|
|
// the algorithm here is modelled on CPython's heapq.py
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
STATIC mp_obj_list_t *uheapq_get_heap(mp_obj_t heap_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (!mp_obj_is_type(heap_in, &mp_type_list)) {
|
2018-08-16 16:34:12 -04:00
|
|
|
mp_raise_TypeError(translate("heap must be a list"));
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_TO_PTR(heap_in);
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
STATIC void uheapq_heap_siftdown(mp_obj_list_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
|
2014-10-22 13:37:18 -04:00
|
|
|
mp_obj_t item = heap->items[pos];
|
|
|
|
while (pos > start_pos) {
|
|
|
|
mp_uint_t parent_pos = (pos - 1) >> 1;
|
|
|
|
mp_obj_t parent = heap->items[parent_pos];
|
2016-12-21 22:27:05 -05:00
|
|
|
if (mp_binary_op(MP_BINARY_OP_LESS, item, parent) == mp_const_true) {
|
2014-10-22 13:37:18 -04:00
|
|
|
heap->items[pos] = parent;
|
|
|
|
pos = parent_pos;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
heap->items[pos] = item;
|
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
STATIC void uheapq_heap_siftup(mp_obj_list_t *heap, mp_uint_t pos) {
|
2014-10-22 13:37:18 -04:00
|
|
|
mp_uint_t start_pos = pos;
|
|
|
|
mp_uint_t end_pos = heap->len;
|
|
|
|
mp_obj_t item = heap->items[pos];
|
|
|
|
for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
|
|
|
|
// choose right child if it's <= left child
|
2016-12-21 22:27:05 -05:00
|
|
|
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) {
|
|
|
|
child_pos += 1;
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
// bubble up the smaller child
|
|
|
|
heap->items[pos] = heap->items[child_pos];
|
|
|
|
pos = child_pos;
|
|
|
|
}
|
|
|
|
heap->items[pos] = item;
|
2021-04-23 15:26:42 -04:00
|
|
|
uheapq_heap_siftdown(heap, start_pos, pos);
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
|
2016-12-21 22:27:05 -05:00
|
|
|
STATIC mp_obj_t mod_uheapq_heappush(mp_obj_t heap_in, mp_obj_t item) {
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_list_t *heap = uheapq_get_heap(heap_in);
|
2016-12-21 22:27:05 -05:00
|
|
|
mp_obj_list_append(heap_in, item);
|
2021-04-23 15:26:42 -04:00
|
|
|
uheapq_heap_siftdown(heap, 0, heap->len - 1);
|
2014-10-22 13:37:18 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2016-12-21 22:27:05 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_uheapq_heappush_obj, mod_uheapq_heappush);
|
2014-10-22 13:37:18 -04:00
|
|
|
|
2016-12-21 22:27:05 -05:00
|
|
|
STATIC mp_obj_t mod_uheapq_heappop(mp_obj_t heap_in) {
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_list_t *heap = uheapq_get_heap(heap_in);
|
2014-10-22 13:37:18 -04:00
|
|
|
if (heap->len == 0) {
|
2020-11-19 17:12:53 -05:00
|
|
|
mp_raise_IndexError(translate("empty heap"));
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
mp_obj_t item = heap->items[0];
|
|
|
|
heap->len -= 1;
|
|
|
|
heap->items[0] = heap->items[heap->len];
|
|
|
|
heap->items[heap->len] = MP_OBJ_NULL; // so we don't retain a pointer
|
|
|
|
if (heap->len) {
|
2021-04-23 15:26:42 -04:00
|
|
|
uheapq_heap_siftup(heap, 0);
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
2016-12-21 22:27:05 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heappop_obj, mod_uheapq_heappop);
|
2014-10-22 13:37:18 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t mod_uheapq_heapify(mp_obj_t heap_in) {
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_list_t *heap = uheapq_get_heap(heap_in);
|
2014-10-22 13:37:18 -04:00
|
|
|
for (mp_uint_t i = heap->len / 2; i > 0;) {
|
2021-04-23 15:26:42 -04:00
|
|
|
uheapq_heap_siftup(heap, --i);
|
2014-10-22 13:37:18 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heapify_obj, mod_uheapq_heapify);
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_uheapq_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uheapq) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_heappush), MP_ROM_PTR(&mod_uheapq_heappush_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_heappop), MP_ROM_PTR(&mod_uheapq_heappop_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_heapify), MP_ROM_PTR(&mod_uheapq_heapify_obj) },
|
2014-10-22 13:37:18 -04:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_uheapq_globals, mp_module_uheapq_globals_table);
|
2014-10-22 13:37:18 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_uheapq = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_uheapq_globals,
|
2014-10-22 13:37:18 -04:00
|
|
|
};
|
2021-04-23 15:26:42 -04:00
|
|
|
#endif
|
2014-10-22 13:37:18 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif // MICROPY_PY_UHEAPQ
|