|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <assert.h> |
| 4 | + |
| 5 | +#include "misc.h" |
| 6 | +#include "mpyconfig.h" |
| 7 | +#include "runtime.h" |
| 8 | + |
| 9 | +#include "map.h" |
| 10 | +#include "obj.h" |
| 11 | +#include "objprivate.h" |
| 12 | + |
| 13 | +// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}] |
| 14 | +static int doubling_primes[] = {7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607}; |
| 15 | + |
| 16 | +int get_doubling_prime_greater_or_equal_to(int x) { |
| 17 | + for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) { |
| 18 | + if (doubling_primes[i] >= x) { |
| 19 | + return doubling_primes[i]; |
| 20 | + } |
| 21 | + } |
| 22 | + // ran out of primes in the table! |
| 23 | + // return something sensible, at least make it odd |
| 24 | + return x | 1; |
| 25 | +} |
| 26 | + |
| 27 | +void py_map_init(py_map_t *map, py_map_kind_t kind, int n) { |
| 28 | + map->kind = kind; |
| 29 | + map->used = 0; |
| 30 | + map->alloc = get_doubling_prime_greater_or_equal_to(n + 1); |
| 31 | + map->table = m_new0(py_map_elem_t, map->alloc); |
| 32 | +} |
| 33 | + |
| 34 | +py_map_t *py_map_new(py_map_kind_t kind, int n) { |
| 35 | + py_map_t *map = m_new(py_map_t, 1); |
| 36 | + py_map_init(map, kind, n); |
| 37 | + return map; |
| 38 | +} |
| 39 | + |
| 40 | +py_map_elem_t* py_map_lookup_helper(py_map_t *map, py_obj_t index, bool add_if_not_found) { |
| 41 | + bool is_map_py_obj = (map->kind == MAP_PY_OBJ); |
| 42 | + machine_uint_t hash; |
| 43 | + if (is_map_py_obj) { |
| 44 | + hash = py_obj_hash(index); |
| 45 | + } else { |
| 46 | + hash = (machine_uint_t)index; |
| 47 | + } |
| 48 | + uint pos = hash % map->alloc; |
| 49 | + for (;;) { |
| 50 | + py_map_elem_t *elem = &map->table[pos]; |
| 51 | + if (elem->key == NULL) { |
| 52 | + // not in table |
| 53 | + if (add_if_not_found) { |
| 54 | + if (map->used + 1 >= map->alloc) { |
| 55 | + // not enough room in table, rehash it |
| 56 | + int old_alloc = map->alloc; |
| 57 | + py_map_elem_t *old_table = map->table; |
| 58 | + map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1); |
| 59 | + map->used = 0; |
| 60 | + map->table = m_new0(py_map_elem_t, map->alloc); |
| 61 | + for (int i = 0; i < old_alloc; i++) { |
| 62 | + if (old_table[i].key != NULL) { |
| 63 | + py_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value; |
| 64 | + } |
| 65 | + } |
| 66 | + m_free(old_table); |
| 67 | + // restart the search for the new element |
| 68 | + pos = hash % map->alloc; |
| 69 | + } else { |
| 70 | + map->used += 1; |
| 71 | + elem->key = index; |
| 72 | + return elem; |
| 73 | + } |
| 74 | + } else { |
| 75 | + return NULL; |
| 76 | + } |
| 77 | + } else if (elem->key == index || (is_map_py_obj && py_obj_equal(elem->key, index))) { |
| 78 | + // found it |
| 79 | + /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x |
| 80 | + if (add_if_not_found) { |
| 81 | + elem->key = index; |
| 82 | + } |
| 83 | + */ |
| 84 | + return elem; |
| 85 | + } else { |
| 86 | + // not yet found, keep searching in this table |
| 87 | + pos = (pos + 1) % map->alloc; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +py_map_elem_t* py_qstr_map_lookup(py_map_t *map, qstr index, bool add_if_not_found) { |
| 93 | + py_obj_t o = (py_obj_t)(machine_uint_t)index; |
| 94 | + return py_map_lookup_helper(map, o, add_if_not_found); |
| 95 | +} |
| 96 | + |
| 97 | +py_map_elem_t* py_map_lookup(py_obj_t o, py_obj_t index, bool add_if_not_found) { |
| 98 | + assert(IS_O(o, O_MAP)); |
| 99 | + return py_map_lookup_helper(&((py_obj_base_t *)o)->u_map, index, add_if_not_found); |
| 100 | +} |
| 101 | + |
| 102 | +py_obj_t py_set_lookup(py_obj_t o_in, py_obj_t index, bool add_if_not_found) { |
| 103 | + assert(IS_O(o_in, O_SET)); |
| 104 | + py_obj_base_t *o = o_in; |
| 105 | + int hash = py_obj_hash(index); |
| 106 | + int pos = hash % o->u_set.alloc; |
| 107 | + for (;;) { |
| 108 | + py_obj_t elem = o->u_set.table[pos]; |
| 109 | + if (elem == NULL) { |
| 110 | + // not in table |
| 111 | + if (add_if_not_found) { |
| 112 | + if (o->u_set.used + 1 >= o->u_set.alloc) { |
| 113 | + // not enough room in table, rehash it |
| 114 | + int old_alloc = o->u_set.alloc; |
| 115 | + py_obj_t *old_table = o->u_set.table; |
| 116 | + o->u_set.alloc = get_doubling_prime_greater_or_equal_to(o->u_set.alloc + 1); |
| 117 | + o->u_set.used = 0; |
| 118 | + o->u_set.table = m_new(py_obj_t, o->u_set.alloc); |
| 119 | + for (int i = 0; i < old_alloc; i++) { |
| 120 | + if (old_table[i] != NULL) { |
| 121 | + py_set_lookup(o, old_table[i], true); |
| 122 | + } |
| 123 | + } |
| 124 | + m_free(old_table); |
| 125 | + // restart the search for the new element |
| 126 | + pos = hash % o->u_set.alloc; |
| 127 | + } else { |
| 128 | + o->u_set.used += 1; |
| 129 | + o->u_set.table[pos] = index; |
| 130 | + return index; |
| 131 | + } |
| 132 | + } else { |
| 133 | + return NULL; |
| 134 | + } |
| 135 | + } else if (py_obj_equal(elem, index)) { |
| 136 | + // found it |
| 137 | + return elem; |
| 138 | + } else { |
| 139 | + // not yet found, keep searching in this table |
| 140 | + pos = (pos + 1) % o->u_set.alloc; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments