moduheapq.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This file is part of the MicroPython 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. #include "py/objlist.h"
  27. #include "py/runtime.h"
  28. #if MICROPY_PY_UHEAPQ
  29. // the algorithm here is modelled on CPython's heapq.py
  30. STATIC mp_obj_list_t *get_heap(mp_obj_t heap_in) {
  31. if (!MP_OBJ_IS_TYPE(heap_in, &mp_type_list)) {
  32. mp_raise_TypeError("heap must be a list");
  33. }
  34. return MP_OBJ_TO_PTR(heap_in);
  35. }
  36. STATIC void heap_siftdown(mp_obj_list_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
  37. mp_obj_t item = heap->items[pos];
  38. while (pos > start_pos) {
  39. mp_uint_t parent_pos = (pos - 1) >> 1;
  40. mp_obj_t parent = heap->items[parent_pos];
  41. if (mp_binary_op(MP_BINARY_OP_LESS, item, parent) == mp_const_true) {
  42. heap->items[pos] = parent;
  43. pos = parent_pos;
  44. } else {
  45. break;
  46. }
  47. }
  48. heap->items[pos] = item;
  49. }
  50. STATIC void heap_siftup(mp_obj_list_t *heap, mp_uint_t pos) {
  51. mp_uint_t start_pos = pos;
  52. mp_uint_t end_pos = heap->len;
  53. mp_obj_t item = heap->items[pos];
  54. for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
  55. // choose right child if it's <= left child
  56. 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) {
  57. child_pos += 1;
  58. }
  59. // bubble up the smaller child
  60. heap->items[pos] = heap->items[child_pos];
  61. pos = child_pos;
  62. }
  63. heap->items[pos] = item;
  64. heap_siftdown(heap, start_pos, pos);
  65. }
  66. STATIC mp_obj_t mod_uheapq_heappush(mp_obj_t heap_in, mp_obj_t item) {
  67. mp_obj_list_t *heap = get_heap(heap_in);
  68. mp_obj_list_append(heap_in, item);
  69. heap_siftdown(heap, 0, heap->len - 1);
  70. return mp_const_none;
  71. }
  72. STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_uheapq_heappush_obj, mod_uheapq_heappush);
  73. STATIC mp_obj_t mod_uheapq_heappop(mp_obj_t heap_in) {
  74. mp_obj_list_t *heap = get_heap(heap_in);
  75. if (heap->len == 0) {
  76. nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
  77. }
  78. mp_obj_t item = heap->items[0];
  79. heap->len -= 1;
  80. heap->items[0] = heap->items[heap->len];
  81. heap->items[heap->len] = MP_OBJ_NULL; // so we don't retain a pointer
  82. if (heap->len) {
  83. heap_siftup(heap, 0);
  84. }
  85. return item;
  86. }
  87. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heappop_obj, mod_uheapq_heappop);
  88. STATIC mp_obj_t mod_uheapq_heapify(mp_obj_t heap_in) {
  89. mp_obj_list_t *heap = get_heap(heap_in);
  90. for (mp_uint_t i = heap->len / 2; i > 0;) {
  91. heap_siftup(heap, --i);
  92. }
  93. return mp_const_none;
  94. }
  95. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_uheapq_heapify_obj, mod_uheapq_heapify);
  96. STATIC const mp_rom_map_elem_t mp_module_uheapq_globals_table[] = {
  97. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uheapq) },
  98. { MP_ROM_QSTR(MP_QSTR_heappush), MP_ROM_PTR(&mod_uheapq_heappush_obj) },
  99. { MP_ROM_QSTR(MP_QSTR_heappop), MP_ROM_PTR(&mod_uheapq_heappop_obj) },
  100. { MP_ROM_QSTR(MP_QSTR_heapify), MP_ROM_PTR(&mod_uheapq_heapify_obj) },
  101. };
  102. STATIC MP_DEFINE_CONST_DICT(mp_module_uheapq_globals, mp_module_uheapq_globals_table);
  103. const mp_obj_module_t mp_module_uheapq = {
  104. .base = { &mp_type_module },
  105. .globals = (mp_obj_dict_t*)&mp_module_uheapq_globals,
  106. };
  107. #endif //MICROPY_PY_UHEAPQ