blob: 9f919e06ae32a40eac123b7f6234087772e1d904 [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.
14static 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) {
Damien660365e2013-12-17 18:27:24 +000031 map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
Damien George38a2da62014-01-08 17:33:12 +000032 map->used = 0;
33 map->all_keys_are_qstrs = 1;
Damiend99b0522013-12-21 18:17:45 +000034 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien660365e2013-12-17 18:27:24 +000035}
36
Damien George38a2da62014-01-08 17:33:12 +000037mp_map_t *mp_map_new(int n) {
Damiend99b0522013-12-21 18:17:45 +000038 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000039 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000040 return map;
41}
42
John R. Lenton4ce6cea2014-01-06 17:38:47 +000043void mp_map_clear(mp_map_t *map) {
44 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000045 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000046 machine_uint_t a = map->alloc;
47 map->alloc = 0;
48 map->table = m_renew(mp_map_elem_t, map->table, a, map->alloc);
49 mp_map_elem_t nul = {NULL, NULL};
50 for (uint i=0; i<map->alloc; i++) {
51 map->table[i] = nul;
52 }
53}
54
Damien George38a2da62014-01-08 17:33:12 +000055static void mp_map_rehash(mp_map_t *map) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000056 int old_alloc = map->alloc;
57 mp_map_elem_t *old_table = map->table;
58 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
59 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000060 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000061 map->table = m_new0(mp_map_elem_t, map->alloc);
62 for (int i = 0; i < old_alloc; i++) {
63 if (old_table[i].key != NULL) {
Damien George38a2da62014-01-08 17:33:12 +000064 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 +000065 }
66 }
67 m_del(mp_map_elem_t, old_table, old_alloc);
68}
69
Damien George38a2da62014-01-08 17:33:12 +000070mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
Damien660365e2013-12-17 18:27:24 +000071 machine_uint_t hash;
Damien George38a2da62014-01-08 17:33:12 +000072 hash = mp_obj_hash(index);
John R. Lenton4ce6cea2014-01-06 17:38:47 +000073 if (map->alloc == 0) {
Damien George38a2da62014-01-08 17:33:12 +000074 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000075 mp_map_rehash(map);
76 } else {
77 return NULL;
78 }
79 }
Damien660365e2013-12-17 18:27:24 +000080 uint pos = hash % map->alloc;
81 for (;;) {
Damiend99b0522013-12-21 18:17:45 +000082 mp_map_elem_t *elem = &map->table[pos];
Damien660365e2013-12-17 18:27:24 +000083 if (elem->key == NULL) {
84 // not in table
Damien George38a2da62014-01-08 17:33:12 +000085 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien660365e2013-12-17 18:27:24 +000086 if (map->used + 1 >= map->alloc) {
87 // not enough room in table, rehash it
John R. Lenton4ce6cea2014-01-06 17:38:47 +000088 mp_map_rehash(map);
Damien660365e2013-12-17 18:27:24 +000089 // restart the search for the new element
90 pos = hash % map->alloc;
91 } else {
92 map->used += 1;
93 elem->key = index;
Damien George38a2da62014-01-08 17:33:12 +000094 if (!MP_OBJ_IS_QSTR(index)) {
95 map->all_keys_are_qstrs = 0;
96 }
Damien660365e2013-12-17 18:27:24 +000097 return elem;
98 }
99 } else {
100 return NULL;
101 }
Damien George38a2da62014-01-08 17:33:12 +0000102 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
Damien660365e2013-12-17 18:27:24 +0000103 // found it
104 /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
105 if (add_if_not_found) {
106 elem->key = index;
107 }
108 */
Damien George38a2da62014-01-08 17:33:12 +0000109 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000110 map->used--;
Damien George38a2da62014-01-08 17:33:12 +0000111 // this leaks this memory (but see dict_get_helper)
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000112 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
113 retval->key = elem->key;
114 retval->value = elem->value;
115 elem->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000116 elem->value = NULL;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000117 return retval;
118 }
Damien660365e2013-12-17 18:27:24 +0000119 return elem;
120 } else {
121 // not yet found, keep searching in this table
122 pos = (pos + 1) % map->alloc;
123 }
124 }
125}
126
Damiend99b0522013-12-21 18:17:45 +0000127/******************************************************************************/
128/* set */
129
130void mp_set_init(mp_set_t *set, int n) {
131 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
132 set->used = 0;
133 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000134}
135
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000136static void mp_set_rehash(mp_set_t *set) {
137 int old_alloc = set->alloc;
138 mp_obj_t *old_table = set->table;
139 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
140 set->used = 0;
141 set->table = m_new0(mp_obj_t, set->alloc);
142 for (int i = 0; i < old_alloc; i++) {
143 if (old_table[i] != NULL) {
144 mp_set_lookup(set, old_table[i], true);
145 }
146 }
147 m_del(mp_obj_t, old_table, old_alloc);
148}
149
John R. Lenton2a241722014-01-12 16:39:39 +0000150mp_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 +0000151 int hash;
152 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000153 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000154 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000155 mp_set_rehash(set);
156 } else {
157 return NULL;
158 }
159 }
John R. Lentonae00d332014-01-12 18:23:36 +0000160 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
161 hash = 0;
162 pos = 0;
163 } else {
164 hash = mp_obj_hash(index);;
165 pos = hash % set->alloc;
166 }
Damien660365e2013-12-17 18:27:24 +0000167 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000168 mp_obj_t elem = set->table[pos];
169 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000170 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000171 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000172 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000173 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000174 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000175 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000176 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000177 } else {
Damiend99b0522013-12-21 18:17:45 +0000178 set->used += 1;
179 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000180 return index;
181 }
John R. Lentonae00d332014-01-12 18:23:36 +0000182 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
183 pos++;
Damien660365e2013-12-17 18:27:24 +0000184 } else {
Damiend99b0522013-12-21 18:17:45 +0000185 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000186 }
John R. Lentonae00d332014-01-12 18:23:36 +0000187 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000188 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000189 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000190 set->used--;
191 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000192 }
Damien660365e2013-12-17 18:27:24 +0000193 return elem;
194 } else {
195 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000196 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000197 }
198 }
199}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000200
201void mp_set_clear(mp_set_t *set) {
202 set->used = 0;
203 machine_uint_t a = set->alloc;
204 set->alloc = 0;
205 set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
206 for (uint i=0; i<set->alloc; i++) {
207 set->table[i] = NULL;
208 }
209}