|
| 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 |
0 commit comments