modutimeq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * Copyright (c) 2016-2017 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include "py/objlist.h"
  29. #include "py/runtime.h"
  30. #include "py/smallint.h"
  31. #if MICROPY_PY_UTIMEQ
  32. #define MODULO MICROPY_PY_UTIME_TICKS_PERIOD
  33. #define DEBUG 0
  34. // the algorithm here is modelled on CPython's heapq.py
  35. struct qentry {
  36. mp_uint_t time;
  37. mp_uint_t id;
  38. mp_obj_t callback;
  39. mp_obj_t args;
  40. };
  41. typedef struct _mp_obj_utimeq_t {
  42. mp_obj_base_t base;
  43. mp_uint_t alloc;
  44. mp_uint_t len;
  45. struct qentry items[];
  46. } mp_obj_utimeq_t;
  47. STATIC mp_uint_t utimeq_id;
  48. STATIC mp_obj_utimeq_t *get_heap(mp_obj_t heap_in) {
  49. return MP_OBJ_TO_PTR(heap_in);
  50. }
  51. STATIC bool time_less_than(struct qentry *item, struct qentry *parent) {
  52. mp_uint_t item_tm = item->time;
  53. mp_uint_t parent_tm = parent->time;
  54. mp_uint_t res = parent_tm - item_tm;
  55. if (res == 0) {
  56. // TODO: This actually should use the same "ring" logic
  57. // as for time, to avoid artifacts when id's overflow.
  58. return item->id < parent->id;
  59. }
  60. if ((mp_int_t)res < 0) {
  61. res += MODULO;
  62. }
  63. return res && res < (MODULO / 2);
  64. }
  65. STATIC mp_obj_t utimeq_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  66. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  67. mp_uint_t alloc = mp_obj_get_int(args[0]);
  68. mp_obj_utimeq_t *o = m_new_obj_var(mp_obj_utimeq_t, struct qentry, alloc);
  69. o->base.type = type;
  70. memset(o->items, 0, sizeof(*o->items) * alloc);
  71. o->alloc = alloc;
  72. o->len = 0;
  73. return MP_OBJ_FROM_PTR(o);
  74. }
  75. STATIC void heap_siftdown(mp_obj_utimeq_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
  76. struct qentry item = heap->items[pos];
  77. while (pos > start_pos) {
  78. mp_uint_t parent_pos = (pos - 1) >> 1;
  79. struct qentry *parent = &heap->items[parent_pos];
  80. bool lessthan = time_less_than(&item, parent);
  81. if (lessthan) {
  82. heap->items[pos] = *parent;
  83. pos = parent_pos;
  84. } else {
  85. break;
  86. }
  87. }
  88. heap->items[pos] = item;
  89. }
  90. STATIC void heap_siftup(mp_obj_utimeq_t *heap, mp_uint_t pos) {
  91. mp_uint_t start_pos = pos;
  92. mp_uint_t end_pos = heap->len;
  93. struct qentry item = heap->items[pos];
  94. for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
  95. // choose right child if it's <= left child
  96. if (child_pos + 1 < end_pos) {
  97. bool lessthan = time_less_than(&heap->items[child_pos], &heap->items[child_pos + 1]);
  98. if (!lessthan) {
  99. child_pos += 1;
  100. }
  101. }
  102. // bubble up the smaller child
  103. heap->items[pos] = heap->items[child_pos];
  104. pos = child_pos;
  105. }
  106. heap->items[pos] = item;
  107. heap_siftdown(heap, start_pos, pos);
  108. }
  109. STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) {
  110. (void)n_args;
  111. mp_obj_t heap_in = args[0];
  112. mp_obj_utimeq_t *heap = get_heap(heap_in);
  113. if (heap->len == heap->alloc) {
  114. mp_raise_msg(&mp_type_IndexError, "queue overflow");
  115. }
  116. mp_uint_t l = heap->len;
  117. heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]);
  118. heap->items[l].id = utimeq_id++;
  119. heap->items[l].callback = args[2];
  120. heap->items[l].args = args[3];
  121. heap_siftdown(heap, 0, heap->len);
  122. heap->len++;
  123. return mp_const_none;
  124. }
  125. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_utimeq_heappush_obj, 4, 4, mod_utimeq_heappush);
  126. STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
  127. mp_obj_utimeq_t *heap = get_heap(heap_in);
  128. if (heap->len == 0) {
  129. nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
  130. }
  131. mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref);
  132. if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) {
  133. mp_raise_TypeError(NULL);
  134. }
  135. struct qentry *item = &heap->items[0];
  136. ret->items[0] = MP_OBJ_NEW_SMALL_INT(item->time);
  137. ret->items[1] = item->callback;
  138. ret->items[2] = item->args;
  139. heap->len -= 1;
  140. heap->items[0] = heap->items[heap->len];
  141. heap->items[heap->len].callback = MP_OBJ_NULL; // so we don't retain a pointer
  142. heap->items[heap->len].args = MP_OBJ_NULL;
  143. if (heap->len) {
  144. heap_siftup(heap, 0);
  145. }
  146. return mp_const_none;
  147. }
  148. STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
  149. STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
  150. mp_obj_utimeq_t *heap = get_heap(heap_in);
  151. if (heap->len == 0) {
  152. nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
  153. }
  154. struct qentry *item = &heap->items[0];
  155. return MP_OBJ_NEW_SMALL_INT(item->time);
  156. }
  157. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime);
  158. #if DEBUG
  159. STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
  160. mp_obj_utimeq_t *heap = get_heap(heap_in);
  161. for (int i = 0; i < heap->len; i++) {
  162. printf(UINT_FMT "\t%p\t%p\n", heap->items[i].time,
  163. MP_OBJ_TO_PTR(heap->items[i].callback), MP_OBJ_TO_PTR(heap->items[i].args));
  164. }
  165. return mp_const_none;
  166. }
  167. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_dump_obj, mod_utimeq_dump);
  168. #endif
  169. STATIC mp_obj_t utimeq_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  170. mp_obj_utimeq_t *self = MP_OBJ_TO_PTR(self_in);
  171. switch (op) {
  172. case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
  173. case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
  174. default: return MP_OBJ_NULL; // op not supported
  175. }
  176. }
  177. STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = {
  178. { MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) },
  179. { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) },
  180. { MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) },
  181. #if DEBUG
  182. { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) },
  183. #endif
  184. };
  185. STATIC MP_DEFINE_CONST_DICT(utimeq_locals_dict, utimeq_locals_dict_table);
  186. STATIC const mp_obj_type_t utimeq_type = {
  187. { &mp_type_type },
  188. .name = MP_QSTR_utimeq,
  189. .make_new = utimeq_make_new,
  190. .unary_op = utimeq_unary_op,
  191. .locals_dict = (void*)&utimeq_locals_dict,
  192. };
  193. STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = {
  194. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utimeq) },
  195. { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&utimeq_type) },
  196. };
  197. STATIC MP_DEFINE_CONST_DICT(mp_module_utimeq_globals, mp_module_utimeq_globals_table);
  198. const mp_obj_module_t mp_module_utimeq = {
  199. .base = { &mp_type_module },
  200. .globals = (mp_obj_dict_t*)&mp_module_utimeq_globals,
  201. };
  202. #endif //MICROPY_PY_UTIMEQ