blob: 2bae8b28d74bb157b9955835bdb6d7d7ea0f8c46 [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
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020043// Differentiate from mp_map_clear() - semantics is different
44void mp_map_deinit(mp_map_t *map) {
45 m_del(mp_map_elem_t, map->table, map->alloc);
46 map->used = map->alloc = 0;
47}
48
49void mp_map_free(mp_map_t *map) {
50 mp_map_deinit(map);
51 m_del_obj(mp_map_t, map);
52}
53
John R. Lenton4ce6cea2014-01-06 17:38:47 +000054void mp_map_clear(mp_map_t *map) {
55 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000056 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000057 machine_uint_t a = map->alloc;
58 map->alloc = 0;
59 map->table = m_renew(mp_map_elem_t, map->table, a, map->alloc);
60 mp_map_elem_t nul = {NULL, NULL};
61 for (uint i=0; i<map->alloc; i++) {
62 map->table[i] = nul;
63 }
64}
65
Damien George38a2da62014-01-08 17:33:12 +000066static void mp_map_rehash(mp_map_t *map) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000067 int old_alloc = map->alloc;
68 mp_map_elem_t *old_table = map->table;
69 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
70 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +000071 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +000072 map->table = m_new0(mp_map_elem_t, map->alloc);
73 for (int i = 0; i < old_alloc; i++) {
74 if (old_table[i].key != NULL) {
Damien George38a2da62014-01-08 17:33:12 +000075 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 +000076 }
77 }
78 m_del(mp_map_elem_t, old_table, old_alloc);
79}
80
Damien George38a2da62014-01-08 17:33:12 +000081mp_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 +000082 machine_uint_t hash;
Damien George38a2da62014-01-08 17:33:12 +000083 hash = mp_obj_hash(index);
John R. Lenton4ce6cea2014-01-06 17:38:47 +000084 if (map->alloc == 0) {
Damien George38a2da62014-01-08 17:33:12 +000085 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +000086 mp_map_rehash(map);
87 } else {
88 return NULL;
89 }
90 }
Damien660365e2013-12-17 18:27:24 +000091 uint pos = hash % map->alloc;
92 for (;;) {
Damiend99b0522013-12-21 18:17:45 +000093 mp_map_elem_t *elem = &map->table[pos];
Damien660365e2013-12-17 18:27:24 +000094 if (elem->key == NULL) {
95 // not in table
Damien George38a2da62014-01-08 17:33:12 +000096 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien660365e2013-12-17 18:27:24 +000097 if (map->used + 1 >= map->alloc) {
98 // not enough room in table, rehash it
John R. Lenton4ce6cea2014-01-06 17:38:47 +000099 mp_map_rehash(map);
Damien660365e2013-12-17 18:27:24 +0000100 // restart the search for the new element
101 pos = hash % map->alloc;
102 } else {
103 map->used += 1;
104 elem->key = index;
Damien George38a2da62014-01-08 17:33:12 +0000105 if (!MP_OBJ_IS_QSTR(index)) {
106 map->all_keys_are_qstrs = 0;
107 }
Damien660365e2013-12-17 18:27:24 +0000108 return elem;
109 }
110 } else {
111 return NULL;
112 }
Damien George38a2da62014-01-08 17:33:12 +0000113 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
Damien660365e2013-12-17 18:27:24 +0000114 // found it
115 /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
116 if (add_if_not_found) {
117 elem->key = index;
118 }
119 */
Damien George38a2da62014-01-08 17:33:12 +0000120 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000121 map->used--;
Damien George38a2da62014-01-08 17:33:12 +0000122 // this leaks this memory (but see dict_get_helper)
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000123 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
124 retval->key = elem->key;
125 retval->value = elem->value;
126 elem->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000127 elem->value = NULL;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000128 return retval;
129 }
Damien660365e2013-12-17 18:27:24 +0000130 return elem;
131 } else {
132 // not yet found, keep searching in this table
133 pos = (pos + 1) % map->alloc;
134 }
135 }
136}
137
Damiend99b0522013-12-21 18:17:45 +0000138/******************************************************************************/
139/* set */
140
141void mp_set_init(mp_set_t *set, int n) {
142 set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
143 set->used = 0;
144 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000145}
146
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000147static void mp_set_rehash(mp_set_t *set) {
148 int old_alloc = set->alloc;
149 mp_obj_t *old_table = set->table;
150 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
151 set->used = 0;
152 set->table = m_new0(mp_obj_t, set->alloc);
153 for (int i = 0; i < old_alloc; i++) {
154 if (old_table[i] != NULL) {
155 mp_set_lookup(set, old_table[i], true);
156 }
157 }
158 m_del(mp_obj_t, old_table, old_alloc);
159}
160
John R. Lenton2a241722014-01-12 16:39:39 +0000161mp_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 +0000162 int hash;
163 int pos;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000164 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000165 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000166 mp_set_rehash(set);
167 } else {
168 return NULL;
169 }
170 }
John R. Lentonae00d332014-01-12 18:23:36 +0000171 if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
172 hash = 0;
173 pos = 0;
174 } else {
175 hash = mp_obj_hash(index);;
176 pos = hash % set->alloc;
177 }
Damien660365e2013-12-17 18:27:24 +0000178 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000179 mp_obj_t elem = set->table[pos];
180 if (elem == MP_OBJ_NULL) {
Damien660365e2013-12-17 18:27:24 +0000181 // not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000182 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damiend99b0522013-12-21 18:17:45 +0000183 if (set->used + 1 >= set->alloc) {
Damien660365e2013-12-17 18:27:24 +0000184 // not enough room in table, rehash it
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000185 mp_set_rehash(set);
Damien660365e2013-12-17 18:27:24 +0000186 // restart the search for the new element
Damiend99b0522013-12-21 18:17:45 +0000187 pos = hash % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000188 } else {
Damiend99b0522013-12-21 18:17:45 +0000189 set->used += 1;
190 set->table[pos] = index;
Damien660365e2013-12-17 18:27:24 +0000191 return index;
192 }
John R. Lentonae00d332014-01-12 18:23:36 +0000193 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
194 pos++;
Damien660365e2013-12-17 18:27:24 +0000195 } else {
Damiend99b0522013-12-21 18:17:45 +0000196 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000197 }
John R. Lentonae00d332014-01-12 18:23:36 +0000198 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) {
Damien660365e2013-12-17 18:27:24 +0000199 // found it
John R. Lentonae00d332014-01-12 18:23:36 +0000200 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton2a241722014-01-12 16:39:39 +0000201 set->used--;
202 set->table[pos] = NULL;
John R. Lenton2a241722014-01-12 16:39:39 +0000203 }
Damien660365e2013-12-17 18:27:24 +0000204 return elem;
205 } else {
206 // not yet found, keep searching in this table
Damiend99b0522013-12-21 18:17:45 +0000207 pos = (pos + 1) % set->alloc;
Damien660365e2013-12-17 18:27:24 +0000208 }
209 }
210}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000211
212void mp_set_clear(mp_set_t *set) {
213 set->used = 0;
214 machine_uint_t a = set->alloc;
215 set->alloc = 0;
216 set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
217 for (uint i=0; i<set->alloc; i++) {
218 set->table[i] = NULL;
219 }
220}