Numworks Epsilon  1.4.1
Graphing Calculator Operating System
map.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 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 <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include "py/mpconfig.h"
33 #include "py/misc.h"
34 #include "py/runtime.h"
35 
36 // Fixed empty map. Useful when need to call kw-receiving functions
37 // without any keywords from C, etc.
39  .all_keys_are_qstrs = 0,
40  .is_fixed = 1,
41  .is_ordered = 1,
42  .used = 0,
43  .alloc = 0,
44  .table = NULL,
45 };
46 
47 // This table of sizes is used to control the growth of hash tables.
48 // The first set of sizes are chosen so the allocation fits exactly in a
49 // 4-word GC block, and it's not so important for these small values to be
50 // prime. The latter sizes are prime and increase at an increasing rate.
52  0, 2, 4, 6, 8, 10, 12, // +2
53  17, 23, 29, 37, 47, 59, 73, // *1.25
54  97, 127, 167, 223, 293, 389, 521, 691, 919, 1223, 1627, 2161, // *1.33
55  3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
56 };
57 
59  for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
60  if (hash_allocation_sizes[i] >= x) {
61  return hash_allocation_sizes[i];
62  }
63  }
64  // ran out of primes in the table!
65  // return something sensible, at least make it odd
66  return (x + x / 2) | 1;
67 }
68 
69 /******************************************************************************/
70 /* map */
71 
72 void mp_map_init(mp_map_t *map, size_t n) {
73  if (n == 0) {
74  map->alloc = 0;
75  map->table = NULL;
76  } else {
77  map->alloc = n;
78  map->table = m_new0(mp_map_elem_t, map->alloc);
79  }
80  map->used = 0;
81  map->all_keys_are_qstrs = 1;
82  map->is_fixed = 0;
83  map->is_ordered = 0;
84 }
85 
86 void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
87  map->alloc = n;
88  map->used = n;
89  map->all_keys_are_qstrs = 1;
90  map->is_fixed = 1;
91  map->is_ordered = 1;
92  map->table = (mp_map_elem_t*)table;
93 }
94 
95 // Differentiate from mp_map_clear() - semantics is different
96 void mp_map_deinit(mp_map_t *map) {
97  if (!map->is_fixed) {
98  m_del(mp_map_elem_t, map->table, map->alloc);
99  }
100  map->used = map->alloc = 0;
101 }
102 
103 void mp_map_clear(mp_map_t *map) {
104  if (!map->is_fixed) {
105  m_del(mp_map_elem_t, map->table, map->alloc);
106  }
107  map->alloc = 0;
108  map->used = 0;
109  map->all_keys_are_qstrs = 1;
110  map->is_fixed = 0;
111  map->table = NULL;
112 }
113 
115  size_t old_alloc = map->alloc;
116  size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
117  mp_map_elem_t *old_table = map->table;
118  mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
119  // If we reach this point, table resizing succeeded, now we can edit the old map.
120  map->alloc = new_alloc;
121  map->used = 0;
122  map->all_keys_are_qstrs = 1;
123  map->table = new_table;
124  for (size_t i = 0; i < old_alloc; i++) {
125  if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
126  mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
127  }
128  }
129  m_del(mp_map_elem_t, old_table, old_alloc);
130 }
131 
132 // MP_MAP_LOOKUP behaviour:
133 // - returns NULL if not found, else the slot it was found in with key,value non-null
134 // MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
135 // - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
136 // MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
137 // - returns NULL if not found, else the slot if was found in with key null and value non-null
139  // If the map is a fixed array then we must only be called for a lookup
140  assert(!map->is_fixed || lookup_kind == MP_MAP_LOOKUP);
141 
142  // Work out if we can compare just pointers
143  bool compare_only_ptrs = map->all_keys_are_qstrs;
144  if (compare_only_ptrs) {
145  if (MP_OBJ_IS_QSTR(index)) {
146  // Index is a qstr, so can just do ptr comparison.
147  } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
148  // Index is a non-interned string.
149  // We can either intern the string, or force a full equality comparison.
150  // We chose the latter, since interning costs time and potentially RAM,
151  // and it won't necessarily benefit subsequent calls because these calls
152  // most likely won't pass the newly-interned string.
153  compare_only_ptrs = false;
154  } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
155  // If we are not adding, then we can return straight away a failed
156  // lookup because we know that the index will never be found.
157  return NULL;
158  }
159  }
160 
161  // if the map is an ordered array then we must do a brute force linear search
162  if (map->is_ordered) {
163  for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
164  if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
165  if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
166  // remove the found element by moving the rest of the array down
167  mp_obj_t value = elem->value;
168  --map->used;
169  memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
170  // put the found element after the end so the caller can access it if needed
171  elem = &map->table[map->used];
172  elem->key = MP_OBJ_NULL;
173  elem->value = value;
174  }
175  return elem;
176  }
177  }
178  if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
179  return NULL;
180  }
181  if (map->used == map->alloc) {
182  // TODO: Alloc policy
183  map->alloc += 4;
184  map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
185  mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
186  }
187  mp_map_elem_t *elem = map->table + map->used++;
188  elem->key = index;
189  if (!MP_OBJ_IS_QSTR(index)) {
190  map->all_keys_are_qstrs = 0;
191  }
192  return elem;
193  }
194 
195  // map is a hash table (not an ordered array), so do a hash lookup
196 
197  if (map->alloc == 0) {
198  if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
199  mp_map_rehash(map);
200  } else {
201  return NULL;
202  }
203  }
204 
205  // get hash of index, with fast path for common case of qstr
206  mp_uint_t hash;
207  if (MP_OBJ_IS_QSTR(index)) {
208  hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
209  } else {
211  }
212 
213  size_t pos = hash % map->alloc;
214  size_t start_pos = pos;
215  mp_map_elem_t *avail_slot = NULL;
216  for (;;) {
217  mp_map_elem_t *slot = &map->table[pos];
218  if (slot->key == MP_OBJ_NULL) {
219  // found NULL slot, so index is not in table
220  if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
221  map->used += 1;
222  if (avail_slot == NULL) {
223  avail_slot = slot;
224  }
225  avail_slot->key = index;
226  avail_slot->value = MP_OBJ_NULL;
227  if (!MP_OBJ_IS_QSTR(index)) {
228  map->all_keys_are_qstrs = 0;
229  }
230  return avail_slot;
231  } else {
232  return NULL;
233  }
234  } else if (slot->key == MP_OBJ_SENTINEL) {
235  // found deleted slot, remember for later
236  if (avail_slot == NULL) {
237  avail_slot = slot;
238  }
239  } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
240  // found index
241  // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
242  if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
243  // delete element in this slot
244  map->used--;
245  if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
246  // optimisation if next slot is empty
247  slot->key = MP_OBJ_NULL;
248  } else {
249  slot->key = MP_OBJ_SENTINEL;
250  }
251  // keep slot->value so that caller can access it if needed
252  }
253  return slot;
254  }
255 
256  // not yet found, keep searching in this table
257  pos = (pos + 1) % map->alloc;
258 
259  if (pos == start_pos) {
260  // search got back to starting position, so index is not in table
261  if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
262  if (avail_slot != NULL) {
263  // there was an available slot, so use that
264  map->used++;
265  avail_slot->key = index;
266  avail_slot->value = MP_OBJ_NULL;
267  if (!MP_OBJ_IS_QSTR(index)) {
268  map->all_keys_are_qstrs = 0;
269  }
270  return avail_slot;
271  } else {
272  // not enough room in table, rehash it
273  mp_map_rehash(map);
274  // restart the search for the new element
275  start_pos = pos = hash % map->alloc;
276  }
277  } else {
278  return NULL;
279  }
280  }
281  }
282 }
283 
284 /******************************************************************************/
285 /* set */
286 
287 #if MICROPY_PY_BUILTINS_SET
288 
289 void mp_set_init(mp_set_t *set, size_t n) {
290  set->alloc = n;
291  set->used = 0;
292  set->table = m_new0(mp_obj_t, set->alloc);
293 }
294 
295 STATIC void mp_set_rehash(mp_set_t *set) {
296  size_t old_alloc = set->alloc;
297  mp_obj_t *old_table = set->table;
298  set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
299  set->used = 0;
300  set->table = m_new0(mp_obj_t, set->alloc);
301  for (size_t i = 0; i < old_alloc; i++) {
302  if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
303  mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
304  }
305  }
306  m_del(mp_obj_t, old_table, old_alloc);
307 }
308 
309 mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
310  // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
311  // is handled by using bitwise operations.
312 
313  if (set->alloc == 0) {
314  if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
315  mp_set_rehash(set);
316  } else {
317  return MP_OBJ_NULL;
318  }
319  }
321  size_t pos = hash % set->alloc;
322  size_t start_pos = pos;
323  mp_obj_t *avail_slot = NULL;
324  for (;;) {
325  mp_obj_t elem = set->table[pos];
326  if (elem == MP_OBJ_NULL) {
327  // found NULL slot, so index is not in table
328  if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
329  if (avail_slot == NULL) {
330  avail_slot = &set->table[pos];
331  }
332  set->used++;
333  *avail_slot = index;
334  return index;
335  } else {
336  return MP_OBJ_NULL;
337  }
338  } else if (elem == MP_OBJ_SENTINEL) {
339  // found deleted slot, remember for later
340  if (avail_slot == NULL) {
341  avail_slot = &set->table[pos];
342  }
343  } else if (mp_obj_equal(elem, index)) {
344  // found index
345  if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
346  // delete element
347  set->used--;
348  if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
349  // optimisation if next slot is empty
350  set->table[pos] = MP_OBJ_NULL;
351  } else {
352  set->table[pos] = MP_OBJ_SENTINEL;
353  }
354  }
355  return elem;
356  }
357 
358  // not yet found, keep searching in this table
359  pos = (pos + 1) % set->alloc;
360 
361  if (pos == start_pos) {
362  // search got back to starting position, so index is not in table
363  if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
364  if (avail_slot != NULL) {
365  // there was an available slot, so use that
366  set->used++;
367  *avail_slot = index;
368  return index;
369  } else {
370  // not enough room in table, rehash it
371  mp_set_rehash(set);
372  // restart the search for the new element
373  start_pos = pos = hash % set->alloc;
374  }
375  } else {
376  return MP_OBJ_NULL;
377  }
378  }
379  }
380 }
381 
383  for (size_t pos = 0; pos < set->alloc; pos++) {
384  if (MP_SET_SLOT_IS_FILLED(set, pos)) {
385  mp_obj_t elem = set->table[pos];
386  // delete element
387  set->used--;
388  if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
389  // optimisation if next slot is empty
390  set->table[pos] = MP_OBJ_NULL;
391  } else {
392  set->table[pos] = MP_OBJ_SENTINEL;
393  }
394  return elem;
395  }
396  }
397  return MP_OBJ_NULL;
398 }
399 
400 void mp_set_clear(mp_set_t *set) {
401  m_del(mp_obj_t, set->table, set->alloc);
402  set->alloc = 0;
403  set->used = 0;
404  set->table = NULL;
405 }
406 
407 #endif // MICROPY_PY_BUILTINS_SET
408 
409 #if defined(DEBUG_PRINT) && DEBUG_PRINT
410 void mp_map_dump(mp_map_t *map) {
411  for (size_t i = 0; i < map->alloc; i++) {
412  if (map->table[i].key != NULL) {
413  mp_obj_print(map->table[i].key, PRINT_REPR);
414  } else {
415  printf("(nil)");
416  }
417  printf(": %p\n", map->table[i].value);
418  }
419  printf("---\n");
420 }
421 #endif
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
STATIC void mp_map_rehash(mp_map_t *map)
Definition: map.c:114
#define mp_seq_clear(start, len, alloc_len, item_sz)
Definition: obj.h:856
#define assert(e)
Definition: assert.h:9
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
#define m_del(type, ptr, num)
Definition: misc.h:77
void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table)
Definition: map.c:86
void mp_map_init(mp_map_t *map, size_t n)
Definition: map.c:72
#define MP_OBJ_QSTR_VALUE(o)
Definition: obj.h:91
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:76
#define MP_OBJ_SENTINEL
Definition: obj.h:75
#define MP_UNLIKELY(x)
Definition: mpconfig.h:1293
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2)
Definition: obj.c:162
unsigned short uint16_t
Definition: stdint.h:5
STATIC const uint16_t hash_allocation_sizes[]
Definition: map.c:51
void mp_map_clear(mp_map_t *map)
Definition: map.c:103
#define MP_ARRAY_SIZE(a)
Definition: misc.h:106
mp_obj_t key
Definition: obj.h:342
size_t alloc
Definition: obj.h:361
size_t is_fixed
Definition: obj.h:358
const mp_map_t mp_const_empty_map
Definition: map.c:38
#define STATIC
Definition: mpconfig.h:1178
#define MP_OBJ_SMALL_INT_VALUE(o)
Definition: obj.h:86
mp_map_elem_t * mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind)
Definition: map.c:138
void mp_set_init(mp_set_t *set, size_t n)
#define MP_LIKELY(x)
Definition: mpconfig.h:1288
void mp_map_deinit(mp_map_t *map)
Definition: map.c:96
mp_uint_t qstr_hash(qstr q)
Definition: qstr.c:269
mp_obj_t mp_set_remove_first(mp_set_t *set)
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
void * memmove(void *dst, const void *src, size_t n)
Definition: memmove.c:3
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind)
const mp_obj_type_t mp_type_str
Definition: objstr.c:1950
mp_obj_t value
Definition: obj.h:343
Definition: obj.h:356
STATIC size_t get_hash_alloc_greater_or_equal_to(size_t x)
Definition: map.c:58
void mp_map_dump(mp_map_t *map)
#define m_new0(type, num)
Definition: misc.h:59
#define m_renew(type, ptr, old_num, new_num)
Definition: misc.h:75
enum _mp_map_lookup_kind_t mp_map_lookup_kind_t
void mp_set_clear(mp_set_t *set)
size_t used
Definition: obj.h:360
Definition: obj.h:388
size_t all_keys_are_qstrs
Definition: obj.h:357
uint64_t mp_obj_t
Definition: obj.h:39
mp_map_elem_t * table
Definition: obj.h:362
size_t is_ordered
Definition: obj.h:359