blob: 301ea51ae4de892586bb5c9075c4488416dfd690 [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;
130 } else {
131 map->used += 1;
132 elem->key = index;
Damien George38a2da62014-01-08 17:33:12 +0000133 if (!MP_OBJ_IS_QSTR(index)) {
134 map->all_keys_are_qstrs = 0;
135 }
Damien660365e2013-12-17 18:27:24 +0000136 return elem;
137 }
138 } else {
139 return NULL;
140 }
Damien George38a2da62014-01-08 17:33:12 +0000141 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
Damien660365e2013-12-17 18:27:24 +0000142 // found it
143 /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
144 if (add_if_not_found) {
145 elem->key = index;
146 }
147 */
Damien George9a58d762014-02-08 18:47:46 +0000148 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000149 map->used--;
Damien George38a2da62014-01-08 17:33:12 +0000150 // this leaks this memory (but see dict_get_helper)
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000151 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
152 retval->key = elem->key;
153 retval->value = elem->value;
154 elem->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000155 elem->value = NULL;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000156 return retval;
157 }
Damien660365e2013-12-17 18:27:24 +0000158 return elem;
159 } else {
160 // not yet found, keep searching in this table
161 pos = (pos + 1) % map->alloc;
162 }
163 }
164}
165
Damiend99b0522013-12-21 18:17:45 +0000166/******************************************************************************/
167/* set */
168
169void mp_set_init(mp_set_t *set, int n) {
170 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
171 set->used = 0;
172 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000173}
174
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200175STATIC void mp_set_rehash(mp_set_t *set) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000176 int old_alloc = set->alloc;
177 mp_obj_t *old_table = set->table;
178 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
179 set->used = 0;
180 set->table = m_new0(mp_obj_t, set->alloc);
181 for (int i = 0; i < old_alloc; i++) {
182 if (old_table[i] != NULL) {
183 mp_set_lookup(set, old_table[i], true);
184 }
185 }
186 m_del(mp_obj_t, old_table, old_alloc);
187}
188
John R. Lenton2a241722014-01-12 16:39:39 +0000189mp_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 +0000190 int hash;
191 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000192 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000193 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000194 mp_set_rehash(set);
195 } else {
196 return NULL;
197 }
198 }
John R. Lentonae00d332014-01-12 18:23:36 +0000199 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
200 hash = 0;
201 pos = 0;
202 } else {
203 hash = mp_obj_hash(index);;
204 pos = hash % set->alloc;
205 }
Damien660365e2013-12-17 18:27:24 +0000206 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000207 mp_obj_t elem = set->table[pos];
208 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000209 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000210 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000211 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000212 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000213 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000214 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000215 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000216 } else {
Damiend99b0522013-12-21 18:17:45 +0000217 set->used += 1;
218 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000219 return index;
220 }
John R. Lentonae00d332014-01-12 18:23:36 +0000221 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
222 pos++;
Damien660365e2013-12-17 18:27:24 +0000223 } else {
Damiend99b0522013-12-21 18:17:45 +0000224 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000225 }
Damien George9a58d762014-02-08 18:47:46 +0000226 } else if ((lookup_kind & MP_MAP_LOOKUP_FIRST) || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000227 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000228 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000229 set->used--;
230 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000231 }
Damien660365e2013-12-17 18:27:24 +0000232 return elem;
233 } else {
234 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000235 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000236 }
237 }
238}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000239
240void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000241 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000242 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000243 set->used = 0;
244 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000245}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300246
247#if DEBUG_PRINT
248void mp_map_dump(mp_map_t *map) {
249 for (int i = 0; i < map->alloc; i++) {
250 if (map->table[i].key != NULL) {
251 mp_obj_print(map->table[i].key, PRINT_REPR);
252 } else {
253 printf("(nil)");
254 }
255 printf(": %p\n", map->table[i].value);
256 }
257 printf("---\n");
258}
259#endif