modutimeq.c 6.98 KB
Newer Older
1
/*
2
 * This file is part of the MicroPython project, http://micropython.org/
3
4
5
6
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Damien P. George
7
 * Copyright (c) 2016 Paul Sokolovsky
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

28
29
#include <string.h>

30
31
32
33
34
35
#include "py/nlr.h"
#include "py/objlist.h"
#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/smallint.h"

36
#if MICROPY_PY_UTIMEQ
37
38
39

#define MODULO MICROPY_PY_UTIME_TICKS_PERIOD

40
41
#define DEBUG 0

42
43
// the algorithm here is modelled on CPython's heapq.py

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
struct qentry {
    mp_uint_t time;
    mp_obj_t callback;
    mp_obj_t args;
};

typedef struct _mp_obj_utimeq_t {
    mp_obj_base_t base;
    mp_uint_t alloc;
    mp_uint_t len;
    struct qentry items[];
} mp_obj_utimeq_t;


STATIC mp_obj_utimeq_t *get_heap(mp_obj_t heap_in) {
59
60
61
    return MP_OBJ_TO_PTR(heap_in);
}

62
63
64
STATIC bool time_less_than(struct qentry *item, struct qentry *parent) {
    mp_uint_t item_tm = item->time;
    mp_uint_t parent_tm = parent->time;
65
66
67
68
69
70
71
    mp_uint_t res = parent_tm - item_tm;
    if ((mp_int_t)res < 0) {
        res += MODULO;
    }
    return res < (MODULO / 2);
}

72
73
74
75
76
77
78
79
80
81
82
83
84
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) {
    mp_arg_check_num(n_args, n_kw, 1, 1, false);
    mp_uint_t alloc = mp_obj_get_int(args[0]);
    mp_obj_utimeq_t *o = m_new_obj_var(mp_obj_utimeq_t, struct qentry, alloc);
    o->base.type = type;
    memset(o->items, 0, sizeof(*o->items) * alloc);
    o->alloc = alloc;
    o->len = 0;
    return MP_OBJ_FROM_PTR(o);
}

STATIC void heap_siftdown(mp_obj_utimeq_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
    struct qentry item = heap->items[pos];
85
86
    while (pos > start_pos) {
        mp_uint_t parent_pos = (pos - 1) >> 1;
87
88
        struct qentry *parent = &heap->items[parent_pos];
        bool lessthan = time_less_than(&item, parent);
89
        if (lessthan) {
90
            heap->items[pos] = *parent;
91
92
93
94
95
96
97
98
            pos = parent_pos;
        } else {
            break;
        }
    }
    heap->items[pos] = item;
}

99
STATIC void heap_siftup(mp_obj_utimeq_t *heap, mp_uint_t pos) {
100
101
    mp_uint_t start_pos = pos;
    mp_uint_t end_pos = heap->len;
102
    struct qentry item = heap->items[pos];
103
104
105
    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
        if (child_pos + 1 < end_pos) {
106
            bool lessthan = time_less_than(&heap->items[child_pos], &heap->items[child_pos + 1]);
107
108
109
110
111
112
113
114
115
            if (!lessthan) {
                child_pos += 1;
            }
        }
        // bubble up the smaller child
        heap->items[pos] = heap->items[child_pos];
        pos = child_pos;
    }
    heap->items[pos] = item;
116
    heap_siftdown(heap, start_pos, pos);
117
118
119
}

STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) {
120
    (void)n_args;
121
    mp_obj_t heap_in = args[0];
122
123
124
125
126
127
128
129
130
131
    mp_obj_utimeq_t *heap = get_heap(heap_in);
    if (heap->len == heap->alloc) {
        mp_raise_msg(&mp_type_IndexError, "queue overflow");
    }
    mp_uint_t l = heap->len;
    heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]);
    heap->items[l].callback = args[2];
    heap->items[l].args = args[3];
    heap_siftdown(heap, 0, heap->len);
    heap->len++;
132
133
    return mp_const_none;
}
134
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_utimeq_heappush_obj, 4, 4, mod_utimeq_heappush);
135

136
137
STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
    mp_obj_utimeq_t *heap = get_heap(heap_in);
138
139
140
    if (heap->len == 0) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
    }
141
142
143
144
145
146
147
148
149
    mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref);
    if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) {
        mp_raise_TypeError("");
    }

    struct qentry *item = &heap->items[0];
    ret->items[0] = MP_OBJ_NEW_SMALL_INT(item->time);
    ret->items[1] = item->callback;
    ret->items[2] = item->args;
150
151
    heap->len -= 1;
    heap->items[0] = heap->items[heap->len];
152
153
    heap->items[heap->len].callback = MP_OBJ_NULL; // so we don't retain a pointer
    heap->items[heap->len].args = MP_OBJ_NULL;
154
    if (heap->len) {
155
        heap_siftup(heap, 0);
156
    }
157
    return mp_const_none;
158
}
159
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
160

161
162
163
164
#if DEBUG
STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
    mp_obj_utimeq_t *heap = get_heap(heap_in);
    for (int i = 0; i < heap->len; i++) {
165
        printf(UINT_FMT "\t%p\t%p\n", heap->items[i].time,
166
            MP_OBJ_TO_PTR(heap->items[i].callback), MP_OBJ_TO_PTR(heap->items[i].args));
167
168
169
    }
    return mp_const_none;
}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_dump_obj, mod_utimeq_dump);
#endif

STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) {
    mp_obj_utimeq_t *self = MP_OBJ_TO_PTR(self_in);
    switch (op) {
        case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
        case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
        default: return MP_OBJ_NULL; // op not supported
    }
}

STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = {
    { MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) },
    { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) },
    #if DEBUG
    { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) },
    #endif
};

STATIC MP_DEFINE_CONST_DICT(utimeq_locals_dict, utimeq_locals_dict_table);

STATIC const mp_obj_type_t utimeq_type = {
    { &mp_type_type },
    .name = MP_QSTR_utimeq,
    .make_new = utimeq_make_new,
    .unary_op = utimeq_unary_op,
    .locals_dict = (void*)&utimeq_locals_dict,
};
199
200
201

STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utimeq) },
202
    { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&utimeq_type) },
203
204
205
206
207
208
209
210
211
};

STATIC MP_DEFINE_CONST_DICT(mp_module_utimeq_globals, mp_module_utimeq_globals_table);

const mp_obj_module_t mp_module_utimeq = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t*)&mp_module_utimeq_globals,
};

212
#endif //MICROPY_PY_UTIMEQ