blob: d1c368c6aec3a2e9f69a411fb4b5d1abbd288624 [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"
8#include "map.h"
Damien660365e2013-12-17 18:27:24 +00009
10// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000011// prefixed with zero for the empty case.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020012STATIC 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 +000013
14int get_doubling_prime_greater_or_equal_to(int x) {
15 for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
16 if (doubling_primes[i] >= x) {
17 return doubling_primes[i];
18 }
19 }
20 // ran out of primes in the table!
21 // return something sensible, at least make it odd
22 return x | 1;
23}
24
Damiend99b0522013-12-21 18:17:45 +000025/******************************************************************************/
26/* map */
27
Damien George38a2da62014-01-08 17:33:12 +000028void mp_map_init(mp_map_t *map, int n) {
Damien George9a58d762014-02-08 18:47:46 +000029 if (n == 0) {
30 map->alloc = 0;
31 map->table = NULL;
32 } else {
33 map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
34 map->table = m_new0(mp_map_elem_t, map->alloc);
35 }
Damien George38a2da62014-01-08 17:33:12 +000036 map->used = 0;
37 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000038 map->table_is_fixed_array = 0;
39}
40
41void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
42 map->alloc = n;
43 map->used = n;
44 map->all_keys_are_qstrs = 1;
45 map->table_is_fixed_array = 1;
46 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000047}
48
Damien George38a2da62014-01-08 17:33:12 +000049mp_map_t *mp_map_new(int n) {
Damiend99b0522013-12-21 18:17:45 +000050 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000051 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000052 return map;
53}
54
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020055// Differentiate from mp_map_clear() - semantics is different
56void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000057 if (!map->table_is_fixed_array) {
58 m_del(mp_map_elem_t, map->table, map->alloc);
59 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020060 map->used = map->alloc = 0;
61}
62
63void mp_map_free(mp_map_t *map) {
64 mp_map_deinit(map);
65 m_del_obj(mp_map_t, map);
66}
67
John R. Lenton4ce6cea2014-01-06 17:38:47 +000068void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000069 if (!map->table_is_fixed_array) {
70 m_del(mp_map_elem_t, map->table, map->alloc);
71 }
72 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000073 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000074 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000075 map->table_is_fixed_array = 0;
76 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000077}
78
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020079STATIC void mp_map_rehash(mp_map_t *map) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000080 int old_alloc = map->alloc;
81 mp_map_elem_t *old_table = map->table;
82 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
83 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000084 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000085 map->table = m_new0(mp_map_elem_t, map->alloc);
86 for (int i = 0; i < old_alloc; i++) {
87 if (old_table[i].key != NULL) {
Damien George38a2da62014-01-08 17:33:12 +000088 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 +000089 }
90 }
91 m_del(mp_map_elem_t, old_table, old_alloc);
92}
93
Damien George38a2da62014-01-08 17:33:12 +000094mp_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 +000095 // if the map is a fixed array then we must do a brute force linear search
96 if (map->table_is_fixed_array) {
97 if (lookup_kind != MP_MAP_LOOKUP) {
98 return NULL;
99 }
100 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
101 if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
102 return elem;
103 }
104 }
105 return NULL;
106 }
107
108 // map is a hash table (not a fixed array), so do a hash lookup
109
Damien660365e2013-12-17 18:27:24 +0000110 machine_uint_t hash;
Damien George38a2da62014-01-08 17:33:12 +0000111 hash = mp_obj_hash(index);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000112 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000113 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000114 mp_map_rehash(map);
115 } else {
116 return NULL;
117 }
118 }
Damien George9a58d762014-02-08 18:47:46 +0000119
Damien660365e2013-12-17 18:27:24 +0000120 uint pos = hash % map->alloc;
121 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000122 mp_map_elem_t *elem = &map->table[pos];
Damien660365e2013-12-17 18:27:24 +0000123 if (elem->key == NULL) {
124 // not in table
Damien George9a58d762014-02-08 18:47:46 +0000125 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien660365e2013-12-17 18:27:24 +0000126 if (map->used + 1 >= map->alloc) {
127 // not enough room in table, rehash it
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000128 mp_map_rehash(map);
Damien660365e2013-12-17 18:27:24 +0000129 // restart the search for the new element
130 pos = hash % map->alloc;
131 } else {
132 map->used += 1;
133 elem->key = index;
Damien George38a2da62014-01-08 17:33:12 +0000134 if (!MP_OBJ_IS_QSTR(index)) {
135 map->all_keys_are_qstrs = 0;
136 }
Damien660365e2013-12-17 18:27:24 +0000137 return elem;
138 }
139 } else {
140 return NULL;
141 }
Damien George38a2da62014-01-08 17:33:12 +0000142 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
Damien660365e2013-12-17 18:27:24 +0000143 // found it
144 /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
145 if (add_if_not_found) {
146 elem->key = index;
147 }
148 */
Damien George9a58d762014-02-08 18:47:46 +0000149 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000150 map->used--;
Damien George38a2da62014-01-08 17:33:12 +0000151 // this leaks this memory (but see dict_get_helper)
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000152 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
153 retval->key = elem->key;
154 retval->value = elem->value;
155 elem->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000156 elem->value = NULL;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000157 return retval;
158 }
Damien660365e2013-12-17 18:27:24 +0000159 return elem;
160 } else {
161 // not yet found, keep searching in this table
162 pos = (pos + 1) % map->alloc;
163 }
164 }
165}
166
Damiend99b0522013-12-21 18:17:45 +0000167/******************************************************************************/
168/* set */
169
170void mp_set_init(mp_set_t *set, int n) {
171 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
172 set->used = 0;
173 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000174}
175
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200176STATIC void mp_set_rehash(mp_set_t *set) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000177 int old_alloc = set->alloc;
178 mp_obj_t *old_table = set->table;
179 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
180 set->used = 0;
181 set->table = m_new0(mp_obj_t, set->alloc);
182 for (int i = 0; i < old_alloc; i++) {
183 if (old_table[i] != NULL) {
184 mp_set_lookup(set, old_table[i], true);
185 }
186 }
187 m_del(mp_obj_t, old_table, old_alloc);
188}
189
John R. Lenton2a241722014-01-12 16:39:39 +0000190mp_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 +0000191 int hash;
192 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000193 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000194 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000195 mp_set_rehash(set);
196 } else {
197 return NULL;
198 }
199 }
John R. Lentonae00d332014-01-12 18:23:36 +0000200 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
201 hash = 0;
202 pos = 0;
203 } else {
204 hash = mp_obj_hash(index);;
205 pos = hash % set->alloc;
206 }
Damien660365e2013-12-17 18:27:24 +0000207 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000208 mp_obj_t elem = set->table[pos];
209 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000210 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000211 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000212 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000213 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000214 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000215 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000216 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000217 } else {
Damiend99b0522013-12-21 18:17:45 +0000218 set->used += 1;
219 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000220 return index;
221 }
John R. Lentonae00d332014-01-12 18:23:36 +0000222 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
223 pos++;
Damien660365e2013-12-17 18:27:24 +0000224 } else {
Damiend99b0522013-12-21 18:17:45 +0000225 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000226 }
Damien George9a58d762014-02-08 18:47:46 +0000227 } else if ((lookup_kind & MP_MAP_LOOKUP_FIRST) || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000228 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000229 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000230 set->used--;
231 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000232 }
Damien660365e2013-12-17 18:27:24 +0000233 return elem;
234 } else {
235 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000236 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000237 }
238 }
239}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000240
241void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000242 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000243 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000244 set->used = 0;
245 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000246}