forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmap.c
372 lines (343 loc) · 12.9 KB
/
map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* 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.
*/
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
// prefixed with zero for the empty case.
STATIC int doubling_primes[] = {0, 7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607};
STATIC int get_doubling_prime_greater_or_equal_to(int x) {
for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
if (doubling_primes[i] >= x) {
return doubling_primes[i];
}
}
// ran out of primes in the table!
// return something sensible, at least make it odd
return x | 1;
}
/******************************************************************************/
/* map */
void mp_map_init(mp_map_t *map, int n) {
if (n == 0) {
map->alloc = 0;
map->table = NULL;
} else {
map->alloc = n;
map->table = m_new0(mp_map_elem_t, map->alloc);
}
map->used = 0;
map->all_keys_are_qstrs = 1;
map->table_is_fixed_array = 0;
}
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
map->alloc = n;
map->used = n;
map->all_keys_are_qstrs = 1;
map->table_is_fixed_array = 1;
map->table = (mp_map_elem_t*)table;
}
mp_map_t *mp_map_new(int n) {
mp_map_t *map = m_new(mp_map_t, 1);
mp_map_init(map, n);
return map;
}
// Differentiate from mp_map_clear() - semantics is different
void mp_map_deinit(mp_map_t *map) {
if (!map->table_is_fixed_array) {
m_del(mp_map_elem_t, map->table, map->alloc);
}
map->used = map->alloc = 0;
}
void mp_map_free(mp_map_t *map) {
mp_map_deinit(map);
m_del_obj(mp_map_t, map);
}
void mp_map_clear(mp_map_t *map) {
if (!map->table_is_fixed_array) {
m_del(mp_map_elem_t, map->table, map->alloc);
}
map->alloc = 0;
map->used = 0;
map->all_keys_are_qstrs = 1;
map->table_is_fixed_array = 0;
map->table = NULL;
}
STATIC void mp_map_rehash(mp_map_t *map) {
int old_alloc = map->alloc;
mp_map_elem_t *old_table = map->table;
map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
map->used = 0;
map->all_keys_are_qstrs = 1;
map->table = m_new0(mp_map_elem_t, map->alloc);
for (int i = 0; i < old_alloc; i++) {
if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
}
}
m_del(mp_map_elem_t, old_table, old_alloc);
}
// MP_MAP_LOOKUP behaviour:
// - returns NULL if not found, else the slot it was found in with key,value non-null
// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
// - returns NULL if not found, else the slot if was found in with key null and value non-null
mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
// Work out if we can compare just pointers
bool compare_only_ptrs = map->all_keys_are_qstrs;
if (compare_only_ptrs) {
if (MP_OBJ_IS_QSTR(index)) {
// Index is a qstr, so can just do ptr comparison.
} else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
// Index is a non-interned string.
// We can either intern the string, or force a full equality comparison.
// We chose the latter, since interning costs time and potentially RAM,
// and it won't necessarily benefit subsequent calls because these calls
// most likely won't pass the newly-interned string.
compare_only_ptrs = false;
} else if (!(lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
// If we are not adding, then we can return straight away a failed
// lookup because we know that the index will never be found.
return NULL;
}
}
// if the map is a fixed array then we must do a brute force linear search
if (map->table_is_fixed_array) {
if (lookup_kind != MP_MAP_LOOKUP) {
return NULL;
}
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
return elem;
}
}
return NULL;
}
// map is a hash table (not a fixed array), so do a hash lookup
if (map->alloc == 0) {
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
mp_map_rehash(map);
} else {
return NULL;
}
}
machine_uint_t hash = mp_obj_hash(index);
uint pos = hash % map->alloc;
uint start_pos = pos;
mp_map_elem_t *avail_slot = NULL;
for (;;) {
mp_map_elem_t *slot = &map->table[pos];
if (slot->key == MP_OBJ_NULL) {
// found NULL slot, so index is not in table
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
map->used += 1;
if (avail_slot == NULL) {
avail_slot = slot;
}
slot->key = index;
slot->value = MP_OBJ_NULL;
if (!MP_OBJ_IS_QSTR(index)) {
map->all_keys_are_qstrs = 0;
}
return slot;
} else {
return NULL;
}
} else if (slot->key == MP_OBJ_SENTINEL) {
// found deleted slot, remember for later
if (avail_slot == NULL) {
avail_slot = slot;
}
} else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
// found index
// Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
// delete element in this slot
map->used--;
if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
// optimisation if next slot is empty
slot->key = MP_OBJ_NULL;
} else {
slot->key = MP_OBJ_SENTINEL;
}
// keep slot->value so that caller can access it if needed
}
return slot;
}
// not yet found, keep searching in this table
pos = (pos + 1) % map->alloc;
if (pos == start_pos) {
// search got back to starting position, so index is not in table
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (avail_slot != NULL) {
// there was an available slot, so use that
map->used++;
avail_slot->key = index;
avail_slot->value = MP_OBJ_NULL;
if (!MP_OBJ_IS_QSTR(index)) {
map->all_keys_are_qstrs = 0;
}
return avail_slot;
} else {
// not enough room in table, rehash it
mp_map_rehash(map);
// restart the search for the new element
start_pos = pos = hash % map->alloc;
}
} else {
return NULL;
}
}
}
}
/******************************************************************************/
/* set */
void mp_set_init(mp_set_t *set, int n) {
set->alloc = n;
set->used = 0;
set->table = m_new0(mp_obj_t, set->alloc);
}
STATIC void mp_set_rehash(mp_set_t *set) {
int old_alloc = set->alloc;
mp_obj_t *old_table = set->table;
set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
set->used = 0;
set->table = m_new0(mp_obj_t, set->alloc);
for (int i = 0; i < old_alloc; i++) {
if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
}
m_del(mp_obj_t, old_table, old_alloc);
}
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
if (set->alloc == 0) {
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
mp_set_rehash(set);
} else {
return NULL;
}
}
machine_uint_t hash = mp_obj_hash(index);
uint pos = hash % set->alloc;
uint start_pos = pos;
mp_obj_t *avail_slot = NULL;
for (;;) {
mp_obj_t elem = set->table[pos];
if (elem == MP_OBJ_NULL) {
// found NULL slot, so index is not in table
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (avail_slot == NULL) {
avail_slot = &set->table[pos];
}
set->used++;
*avail_slot = index;
return index;
} else {
return MP_OBJ_NULL;
}
} else if (elem == MP_OBJ_SENTINEL) {
// found deleted slot, remember for later
if (avail_slot == NULL) {
avail_slot = &set->table[pos];
}
} else if (mp_obj_equal(elem, index)) {
// found index
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
// delete element
set->used--;
if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
// optimisation if next slot is empty
set->table[pos] = MP_OBJ_NULL;
} else {
set->table[pos] = MP_OBJ_SENTINEL;
}
}
return elem;
}
// not yet found, keep searching in this table
pos = (pos + 1) % set->alloc;
if (pos == start_pos) {
// search got back to starting position, so index is not in table
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (avail_slot != NULL) {
// there was an available slot, so use that
set->used++;
*avail_slot = index;
return index;
} else {
// not enough room in table, rehash it
mp_set_rehash(set);
// restart the search for the new element
start_pos = pos = hash % set->alloc;
}
} else {
return MP_OBJ_NULL;
}
}
}
}
mp_obj_t mp_set_remove_first(mp_set_t *set) {
for (uint pos = 0; pos < set->alloc; pos++) {
if (MP_SET_SLOT_IS_FILLED(set, pos)) {
mp_obj_t elem = set->table[pos];
// delete element
set->used--;
if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
// optimisation if next slot is empty
set->table[pos] = MP_OBJ_NULL;
} else {
set->table[pos] = MP_OBJ_SENTINEL;
}
return elem;
}
}
return MP_OBJ_NULL;
}
void mp_set_clear(mp_set_t *set) {
m_del(mp_obj_t, set->table, set->alloc);
set->alloc = 0;
set->used = 0;
set->table = NULL;
}
#if DEBUG_PRINT
void mp_map_dump(mp_map_t *map) {
for (int i = 0; i < map->alloc; i++) {
if (map->table[i].key != NULL) {
mp_obj_print(map->table[i].key, PRINT_REPR);
} else {
printf("(nil)");
}
printf(": %p\n", map->table[i].value);
}
printf("---\n");
}
#endif