blob: f41152703ea28eb3f5ddb9524d3102c3951ed692 [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <assert.h>
4
5#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00006#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00008#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "runtime0.h"
10#include "map.h"
Damien660365e2013-12-17 18:27:24 +000011
12// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000013// prefixed with zero for the empty case.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020014STATIC 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 +000015
16int 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
Damiend99b0522013-12-21 18:17:45 +000027/******************************************************************************/
28/* map */
29
Damien George38a2da62014-01-08 17:33:12 +000030void mp_map_init(mp_map_t *map, int n) {
Damien George9a58d762014-02-08 18:47:46 +000031 if (n == 0) {
32 map->alloc = 0;
33 map->table = NULL;
34 } else {
35 map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
36 map->table = m_new0(mp_map_elem_t, map->alloc);
37 }
Damien George38a2da62014-01-08 17:33:12 +000038 map->used = 0;
39 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000040 map->table_is_fixed_array = 0;
41}
42
43void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
44 map->alloc = n;
45 map->used = n;
46 map->all_keys_are_qstrs = 1;
47 map->table_is_fixed_array = 1;
48 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000049}
50
Damien George38a2da62014-01-08 17:33:12 +000051mp_map_t *mp_map_new(int n) {
Damiend99b0522013-12-21 18:17:45 +000052 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000053 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000054 return map;
55}
56
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020057// Differentiate from mp_map_clear() - semantics is different
58void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000059 if (!map->table_is_fixed_array) {
60 m_del(mp_map_elem_t, map->table, map->alloc);
61 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020062 map->used = map->alloc = 0;
63}
64
65void mp_map_free(mp_map_t *map) {
66 mp_map_deinit(map);
67 m_del_obj(mp_map_t, map);
68}
69
John R. Lenton4ce6cea2014-01-06 17:38:47 +000070void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000071 if (!map->table_is_fixed_array) {
72 m_del(mp_map_elem_t, map->table, map->alloc);
73 }
74 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000075 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000076 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000077 map->table_is_fixed_array = 0;
78 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000079}
80
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020081STATIC void mp_map_rehash(mp_map_t *map) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000082 int old_alloc = map->alloc;
83 mp_map_elem_t *old_table = map->table;
84 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
85 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000086 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000087 map->table = m_new0(mp_map_elem_t, map->alloc);
88 for (int i = 0; i < old_alloc; i++) {
89 if (old_table[i].key != NULL) {
Damien George38a2da62014-01-08 17:33:12 +000090 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 +000091 }
92 }
93 m_del(mp_map_elem_t, old_table, old_alloc);
94}
95
Damien George38a2da62014-01-08 17:33:12 +000096mp_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 +000097 // if the map is a fixed array then we must do a brute force linear search
98 if (map->table_is_fixed_array) {
99 if (lookup_kind != MP_MAP_LOOKUP) {
100 return NULL;
101 }
102 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
103 if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
104 return elem;
105 }
106 }
107 return NULL;
108 }
109
110 // map is a hash table (not a fixed array), so do a hash lookup
111
Damien660365e2013-12-17 18:27:24 +0000112 machine_uint_t hash;
Damien George38a2da62014-01-08 17:33:12 +0000113 hash = mp_obj_hash(index);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000114 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000115 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000116 mp_map_rehash(map);
117 } else {
118 return NULL;
119 }
120 }
Damien George9a58d762014-02-08 18:47:46 +0000121
Damien660365e2013-12-17 18:27:24 +0000122 uint pos = hash % map->alloc;
123 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000124 mp_map_elem_t *elem = &map->table[pos];
Damien660365e2013-12-17 18:27:24 +0000125 if (elem->key == NULL) {
126 // not in table
Damien George9a58d762014-02-08 18:47:46 +0000127 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien660365e2013-12-17 18:27:24 +0000128 if (map->used + 1 >= map->alloc) {
129 // not enough room in table, rehash it
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000130 mp_map_rehash(map);
Damien660365e2013-12-17 18:27:24 +0000131 // restart the search for the new element
132 pos = hash % map->alloc;
133 } else {
134 map->used += 1;
135 elem->key = index;
Damien George38a2da62014-01-08 17:33:12 +0000136 if (!MP_OBJ_IS_QSTR(index)) {
137 map->all_keys_are_qstrs = 0;
138 }
Damien660365e2013-12-17 18:27:24 +0000139 return elem;
140 }
141 } else {
142 return NULL;
143 }
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;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000158 elem->value = NULL;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000159 return retval;
160 }
Damien660365e2013-12-17 18:27:24 +0000161 return elem;
162 } else {
163 // not yet found, keep searching in this table
164 pos = (pos + 1) % map->alloc;
165 }
166 }
167}
168
Damiend99b0522013-12-21 18:17:45 +0000169/******************************************************************************/
170/* set */
171
172void mp_set_init(mp_set_t *set, int n) {
173 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
174 set->used = 0;
175 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000176}
177
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200178STATIC void mp_set_rehash(mp_set_t *set) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000179 int old_alloc = set->alloc;
180 mp_obj_t *old_table = set->table;
181 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
182 set->used = 0;
183 set->table = m_new0(mp_obj_t, set->alloc);
184 for (int i = 0; i < old_alloc; i++) {
185 if (old_table[i] != NULL) {
186 mp_set_lookup(set, old_table[i], true);
187 }
188 }
189 m_del(mp_obj_t, old_table, old_alloc);
190}
191
John R. Lenton2a241722014-01-12 16:39:39 +0000192mp_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 +0000193 int hash;
194 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000195 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000196 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000197 mp_set_rehash(set);
198 } else {
199 return NULL;
200 }
201 }
John R. Lentonae00d332014-01-12 18:23:36 +0000202 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
203 hash = 0;
204 pos = 0;
205 } else {
206 hash = mp_obj_hash(index);;
207 pos = hash % set->alloc;
208 }
Damien660365e2013-12-17 18:27:24 +0000209 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000210 mp_obj_t elem = set->table[pos];
211 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000212 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000213 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000214 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000215 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000216 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000217 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000218 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000219 } else {
Damiend99b0522013-12-21 18:17:45 +0000220 set->used += 1;
221 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000222 return index;
223 }
John R. Lentonae00d332014-01-12 18:23:36 +0000224 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
225 pos++;
Damien660365e2013-12-17 18:27:24 +0000226 } else {
Damiend99b0522013-12-21 18:17:45 +0000227 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000228 }
Damien George9a58d762014-02-08 18:47:46 +0000229 } else if ((lookup_kind & MP_MAP_LOOKUP_FIRST) || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000230 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000231 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000232 set->used--;
233 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000234 }
Damien660365e2013-12-17 18:27:24 +0000235 return elem;
236 } else {
237 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000238 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000239 }
240 }
241}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000242
243void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000244 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000245 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000246 set->used = 0;
247 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000248}