blob: 6411f8adeab1e683cdc1b882f34252f13094b8df [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdlib.h>
Damien660365e2013-12-17 18:27:24 +00002
3#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00004#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00005#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00006#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "runtime0.h"
Damien660365e2013-12-17 18:27:24 +00008
9// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000010// prefixed with zero for the empty case.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020011STATIC 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};
Damien660365e2013-12-17 18:27:24 +000012
Damien Georgedf6567e2014-03-30 13:54:02 +010013STATIC int get_doubling_prime_greater_or_equal_to(int x) {
Damien660365e2013-12-17 18:27:24 +000014 for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
15 if (doubling_primes[i] >= x) {
16 return doubling_primes[i];
17 }
18 }
19 // ran out of primes in the table!
20 // return something sensible, at least make it odd
21 return x | 1;
22}
23
Damiend99b0522013-12-21 18:17:45 +000024/******************************************************************************/
25/* map */
26
Damien George38a2da62014-01-08 17:33:12 +000027void mp_map_init(mp_map_t *map, int n) {
Damien George9a58d762014-02-08 18:47:46 +000028 if (n == 0) {
29 map->alloc = 0;
30 map->table = NULL;
31 } else {
32 map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
33 map->table = m_new0(mp_map_elem_t, map->alloc);
34 }
Damien George38a2da62014-01-08 17:33:12 +000035 map->used = 0;
36 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000037 map->table_is_fixed_array = 0;
38}
39
40void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
41 map->alloc = n;
42 map->used = n;
43 map->all_keys_are_qstrs = 1;
44 map->table_is_fixed_array = 1;
45 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000046}
47
Damien George38a2da62014-01-08 17:33:12 +000048mp_map_t *mp_map_new(int n) {
Damiend99b0522013-12-21 18:17:45 +000049 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000050 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000051 return map;
52}
53
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020054// Differentiate from mp_map_clear() - semantics is different
55void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000056 if (!map->table_is_fixed_array) {
57 m_del(mp_map_elem_t, map->table, map->alloc);
58 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020059 map->used = map->alloc = 0;
60}
61
62void mp_map_free(mp_map_t *map) {
63 mp_map_deinit(map);
64 m_del_obj(mp_map_t, map);
65}
66
John R. Lenton4ce6cea2014-01-06 17:38:47 +000067void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000068 if (!map->table_is_fixed_array) {
69 m_del(mp_map_elem_t, map->table, map->alloc);
70 }
71 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000072 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000073 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000074 map->table_is_fixed_array = 0;
75 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000076}
77
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020078STATIC void mp_map_rehash(mp_map_t *map) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000079 int old_alloc = map->alloc;
80 mp_map_elem_t *old_table = map->table;
81 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
82 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000083 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000084 map->table = m_new0(mp_map_elem_t, map->alloc);
85 for (int i = 0; i < old_alloc; i++) {
86 if (old_table[i].key != NULL) {
Damien George38a2da62014-01-08 17:33:12 +000087 mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000088 }
89 }
90 m_del(mp_map_elem_t, old_table, old_alloc);
91}
92
Damien George38a2da62014-01-08 17:33:12 +000093mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
Damien George9a58d762014-02-08 18:47:46 +000094 // if the map is a fixed array then we must do a brute force linear search
95 if (map->table_is_fixed_array) {
96 if (lookup_kind != MP_MAP_LOOKUP) {
97 return NULL;
98 }
99 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
100 if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
101 return elem;
102 }
103 }
104 return NULL;
105 }
106
107 // map is a hash table (not a fixed array), so do a hash lookup
108
Damien660365e2013-12-17 18:27:24 +0000109 machine_uint_t hash;
Damien George38a2da62014-01-08 17:33:12 +0000110 hash = mp_obj_hash(index);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000111 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000112 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000113 mp_map_rehash(map);
114 } else {
115 return NULL;
116 }
117 }
Damien George9a58d762014-02-08 18:47:46 +0000118
Damien660365e2013-12-17 18:27:24 +0000119 uint pos = hash % map->alloc;
120 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000121 mp_map_elem_t *elem = &map->table[pos];
Damien660365e2013-12-17 18:27:24 +0000122 if (elem->key == NULL) {
123 // not in table
Damien George9a58d762014-02-08 18:47:46 +0000124 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien660365e2013-12-17 18:27:24 +0000125 if (map->used + 1 >= map->alloc) {
126 // not enough room in table, rehash it
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000127 mp_map_rehash(map);
Damien660365e2013-12-17 18:27:24 +0000128 // restart the search for the new element
129 pos = hash % map->alloc;
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300130 continue;
Damien660365e2013-12-17 18:27:24 +0000131 } else {
132 map->used += 1;
133 elem->key = index;
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300134 elem->value = NULL;
Damien George38a2da62014-01-08 17:33:12 +0000135 if (!MP_OBJ_IS_QSTR(index)) {
136 map->all_keys_are_qstrs = 0;
137 }
Damien660365e2013-12-17 18:27:24 +0000138 return elem;
139 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300140 } else if (elem->value == NULL) {
Damien660365e2013-12-17 18:27:24 +0000141 return NULL;
142 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300143 // Otherwise it's just entry marked as deleted, so continue with next one
Damien George38a2da62014-01-08 17:33:12 +0000144 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
Damien660365e2013-12-17 18:27:24 +0000145 // found it
146 /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
147 if (add_if_not_found) {
148 elem->key = index;
149 }
150 */
Damien George9a58d762014-02-08 18:47:46 +0000151 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000152 map->used--;
Damien George38a2da62014-01-08 17:33:12 +0000153 // this leaks this memory (but see dict_get_helper)
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000154 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
155 retval->key = elem->key;
156 retval->value = elem->value;
157 elem->key = NULL;
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300158 // elem->key = NULL && elem->value != NULL means "marked deleted"
159 // assume value indeed never NULL
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000160 return retval;
161 }
Damien660365e2013-12-17 18:27:24 +0000162 return elem;
Damien660365e2013-12-17 18:27:24 +0000163 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300164
165 // not yet found, keep searching in this table
166 pos = (pos + 1) % map->alloc;
Damien660365e2013-12-17 18:27:24 +0000167 }
168}
169
Damiend99b0522013-12-21 18:17:45 +0000170/******************************************************************************/
171/* set */
172
173void mp_set_init(mp_set_t *set, int n) {
174 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
175 set->used = 0;
176 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000177}
178
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200179STATIC void mp_set_rehash(mp_set_t *set) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000180 int old_alloc = set->alloc;
181 mp_obj_t *old_table = set->table;
182 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
183 set->used = 0;
184 set->table = m_new0(mp_obj_t, set->alloc);
185 for (int i = 0; i < old_alloc; i++) {
186 if (old_table[i] != NULL) {
187 mp_set_lookup(set, old_table[i], true);
188 }
189 }
190 m_del(mp_obj_t, old_table, old_alloc);
191}
192
John R. Lenton2a241722014-01-12 16:39:39 +0000193mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
John R. Lentonae00d332014-01-12 18:23:36 +0000194 int hash;
195 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000196 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000197 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000198 mp_set_rehash(set);
199 } else {
200 return NULL;
201 }
202 }
John R. Lentonae00d332014-01-12 18:23:36 +0000203 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
204 hash = 0;
205 pos = 0;
206 } else {
207 hash = mp_obj_hash(index);;
208 pos = hash % set->alloc;
209 }
Damien660365e2013-12-17 18:27:24 +0000210 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000211 mp_obj_t elem = set->table[pos];
212 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000213 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000214 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000215 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000216 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000217 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000218 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000219 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000220 } else {
Damiend99b0522013-12-21 18:17:45 +0000221 set->used += 1;
222 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000223 return index;
224 }
John R. Lentonae00d332014-01-12 18:23:36 +0000225 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
226 pos++;
Damien660365e2013-12-17 18:27:24 +0000227 } else {
Damiend99b0522013-12-21 18:17:45 +0000228 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000229 }
Damien George9a58d762014-02-08 18:47:46 +0000230 } else if ((lookup_kind & MP_MAP_LOOKUP_FIRST) || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000231 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000232 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000233 set->used--;
234 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000235 }
Damien660365e2013-12-17 18:27:24 +0000236 return elem;
237 } else {
238 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000239 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000240 }
241 }
242}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000243
244void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000245 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000246 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000247 set->used = 0;
248 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000249}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300250
251#if DEBUG_PRINT
252void mp_map_dump(mp_map_t *map) {
253 for (int i = 0; i < map->alloc; i++) {
254 if (map->table[i].key != NULL) {
255 mp_obj_print(map->table[i].key, PRINT_REPR);
256 } else {
257 printf("(nil)");
258 }
259 printf(": %p\n", map->table[i].value);
260 }
261 printf("---\n");
262}
263#endif