blob: fb0c5a0795bb588f2ca624d4d577dbf148ffe733 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George93965e72014-08-30 13:23:35 +010027#include <stdint.h>
Damien660365e2013-12-17 18:27:24 +000028#include <stdlib.h>
Damien George8b0535e2014-04-05 21:53:54 +010029#include <assert.h>
Damien660365e2013-12-17 18:27:24 +000030
Damiend99b0522013-12-21 18:17:45 +000031#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030032#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000034#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "runtime0.h"
Damien660365e2013-12-17 18:27:24 +000036
37// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000038// prefixed with zero for the empty case.
Damien George93965e72014-08-30 13:23:35 +010039STATIC uint32_t 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 +000040
Damien George93965e72014-08-30 13:23:35 +010041STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
42 for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
Damien660365e2013-12-17 18:27:24 +000043 if (doubling_primes[i] >= x) {
44 return doubling_primes[i];
45 }
46 }
47 // ran out of primes in the table!
48 // return something sensible, at least make it odd
49 return x | 1;
50}
51
Damiend99b0522013-12-21 18:17:45 +000052/******************************************************************************/
53/* map */
54
Damien George93965e72014-08-30 13:23:35 +010055void mp_map_init(mp_map_t *map, mp_uint_t n) {
Damien George9a58d762014-02-08 18:47:46 +000056 if (n == 0) {
57 map->alloc = 0;
58 map->table = NULL;
59 } else {
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +030060 map->alloc = n;
Damien George9a58d762014-02-08 18:47:46 +000061 map->table = m_new0(mp_map_elem_t, map->alloc);
62 }
Damien George38a2da62014-01-08 17:33:12 +000063 map->used = 0;
64 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000065 map->table_is_fixed_array = 0;
66}
67
Damien George93965e72014-08-30 13:23:35 +010068void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table) {
Damien George9a58d762014-02-08 18:47:46 +000069 map->alloc = n;
70 map->used = n;
71 map->all_keys_are_qstrs = 1;
72 map->table_is_fixed_array = 1;
73 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000074}
75
Damien George93965e72014-08-30 13:23:35 +010076mp_map_t *mp_map_new(mp_uint_t n) {
Damiend99b0522013-12-21 18:17:45 +000077 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000078 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000079 return map;
80}
81
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020082// Differentiate from mp_map_clear() - semantics is different
83void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000084 if (!map->table_is_fixed_array) {
85 m_del(mp_map_elem_t, map->table, map->alloc);
86 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020087 map->used = map->alloc = 0;
88}
89
90void mp_map_free(mp_map_t *map) {
91 mp_map_deinit(map);
92 m_del_obj(mp_map_t, map);
93}
94
John R. Lenton4ce6cea2014-01-06 17:38:47 +000095void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000096 if (!map->table_is_fixed_array) {
97 m_del(mp_map_elem_t, map->table, map->alloc);
98 }
99 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000100 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000101 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +0000102 map->table_is_fixed_array = 0;
103 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000104}
105
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200106STATIC void mp_map_rehash(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100107 mp_uint_t old_alloc = map->alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000108 mp_map_elem_t *old_table = map->table;
109 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
110 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000111 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000112 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien George93965e72014-08-30 13:23:35 +0100113 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100114 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000115 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 +0000116 }
117 }
118 m_del(mp_map_elem_t, old_table, old_alloc);
119}
120
Damien Georged0e82432014-04-05 23:33:12 +0100121// MP_MAP_LOOKUP behaviour:
122// - returns NULL if not found, else the slot it was found in with key,value non-null
123// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
124// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
125// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
126// - returns NULL if not found, else the slot if was found in with key null and value non-null
Damien George38a2da62014-01-08 17:33:12 +0000127mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
Damien George186e4632014-04-28 12:11:57 +0100128
129 // Work out if we can compare just pointers
130 bool compare_only_ptrs = map->all_keys_are_qstrs;
131 if (compare_only_ptrs) {
132 if (MP_OBJ_IS_QSTR(index)) {
133 // Index is a qstr, so can just do ptr comparison.
134 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
135 // Index is a non-interned string.
136 // We can either intern the string, or force a full equality comparison.
137 // We chose the latter, since interning costs time and potentially RAM,
138 // and it won't necessarily benefit subsequent calls because these calls
139 // most likely won't pass the newly-interned string.
140 compare_only_ptrs = false;
141 } else if (!(lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
142 // If we are not adding, then we can return straight away a failed
143 // lookup because we know that the index will never be found.
144 return NULL;
145 }
146 }
147
Damien George9a58d762014-02-08 18:47:46 +0000148 // if the map is a fixed array then we must do a brute force linear search
149 if (map->table_is_fixed_array) {
150 if (lookup_kind != MP_MAP_LOOKUP) {
151 return NULL;
152 }
153 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100154 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Damien George9a58d762014-02-08 18:47:46 +0000155 return elem;
156 }
157 }
158 return NULL;
159 }
160
161 // map is a hash table (not a fixed array), so do a hash lookup
162
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000163 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000164 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000165 mp_map_rehash(map);
166 } else {
167 return NULL;
168 }
169 }
Damien George9a58d762014-02-08 18:47:46 +0000170
Damien George40f3c022014-07-03 13:25:24 +0100171 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100172 mp_uint_t pos = hash % map->alloc;
173 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100174 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000175 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100176 mp_map_elem_t *slot = &map->table[pos];
177 if (slot->key == MP_OBJ_NULL) {
178 // found NULL slot, so index is not in table
Damien George9a58d762014-02-08 18:47:46 +0000179 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100180 map->used += 1;
181 if (avail_slot == NULL) {
182 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000183 }
Damien George95004e52014-04-05 17:17:19 +0100184 slot->key = index;
185 slot->value = MP_OBJ_NULL;
186 if (!MP_OBJ_IS_QSTR(index)) {
187 map->all_keys_are_qstrs = 0;
188 }
189 return slot;
190 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100191 return NULL;
Damien660365e2013-12-17 18:27:24 +0000192 }
Damien George95004e52014-04-05 17:17:19 +0100193 } else if (slot->key == MP_OBJ_SENTINEL) {
194 // found deleted slot, remember for later
195 if (avail_slot == NULL) {
196 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000197 }
Damien George186e4632014-04-28 12:11:57 +0100198 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100199 // found index
200 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien George9a58d762014-02-08 18:47:46 +0000201 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100202 // delete element in this slot
203 map->used--;
204 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
205 // optimisation if next slot is empty
206 slot->key = MP_OBJ_NULL;
207 } else {
208 slot->key = MP_OBJ_SENTINEL;
209 }
Damien Georged0e82432014-04-05 23:33:12 +0100210 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000211 }
Damien George95004e52014-04-05 17:17:19 +0100212 return slot;
Damien660365e2013-12-17 18:27:24 +0000213 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300214
215 // not yet found, keep searching in this table
216 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100217
218 if (pos == start_pos) {
219 // search got back to starting position, so index is not in table
220 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
221 if (avail_slot != NULL) {
222 // there was an available slot, so use that
223 map->used++;
224 avail_slot->key = index;
225 avail_slot->value = MP_OBJ_NULL;
226 if (!MP_OBJ_IS_QSTR(index)) {
227 map->all_keys_are_qstrs = 0;
228 }
229 return avail_slot;
230 } else {
231 // not enough room in table, rehash it
232 mp_map_rehash(map);
233 // restart the search for the new element
234 start_pos = pos = hash % map->alloc;
235 }
236 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100237 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100238 }
239 }
Damien660365e2013-12-17 18:27:24 +0000240 }
241}
242
Damiend99b0522013-12-21 18:17:45 +0000243/******************************************************************************/
244/* set */
245
Damien George93965e72014-08-30 13:23:35 +0100246void mp_set_init(mp_set_t *set, mp_uint_t n) {
Damien George2bfd2dc2014-04-07 01:16:17 +0100247 set->alloc = n;
Damiend99b0522013-12-21 18:17:45 +0000248 set->used = 0;
249 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000250}
251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200252STATIC void mp_set_rehash(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100253 mp_uint_t old_alloc = set->alloc;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000254 mp_obj_t *old_table = set->table;
255 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
256 set->used = 0;
257 set->table = m_new0(mp_obj_t, set->alloc);
Damien George93965e72014-08-30 13:23:35 +0100258 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100259 if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
260 mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000261 }
262 }
263 m_del(mp_obj_t, old_table, old_alloc);
264}
265
John R. Lenton2a241722014-01-12 16:39:39 +0000266mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000267 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000268 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000269 mp_set_rehash(set);
270 } else {
271 return NULL;
272 }
273 }
Damien George40f3c022014-07-03 13:25:24 +0100274 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100275 mp_uint_t pos = hash % set->alloc;
276 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100277 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000278 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000279 mp_obj_t elem = set->table[pos];
280 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100281 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000282 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100283 if (avail_slot == NULL) {
284 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000285 }
Damien George95004e52014-04-05 17:17:19 +0100286 set->used++;
287 *avail_slot = index;
288 return index;
Damien660365e2013-12-17 18:27:24 +0000289 } else {
Damiend99b0522013-12-21 18:17:45 +0000290 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000291 }
Damien George95004e52014-04-05 17:17:19 +0100292 } else if (elem == MP_OBJ_SENTINEL) {
293 // found deleted slot, remember for later
294 if (avail_slot == NULL) {
295 avail_slot = &set->table[pos];
296 }
297 } else if (mp_obj_equal(elem, index)) {
298 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000299 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100300 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000301 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100302 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
303 // optimisation if next slot is empty
304 set->table[pos] = MP_OBJ_NULL;
305 } else {
306 set->table[pos] = MP_OBJ_SENTINEL;
307 }
John R. Lenton2a241722014-01-12 16:39:39 +0000308 }
Damien660365e2013-12-17 18:27:24 +0000309 return elem;
Damien George95004e52014-04-05 17:17:19 +0100310 }
311
312 // not yet found, keep searching in this table
313 pos = (pos + 1) % set->alloc;
314
315 if (pos == start_pos) {
316 // search got back to starting position, so index is not in table
317 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
318 if (avail_slot != NULL) {
319 // there was an available slot, so use that
320 set->used++;
321 *avail_slot = index;
322 return index;
323 } else {
324 // not enough room in table, rehash it
325 mp_set_rehash(set);
326 // restart the search for the new element
327 start_pos = pos = hash % set->alloc;
328 }
329 } else {
330 return MP_OBJ_NULL;
331 }
Damien660365e2013-12-17 18:27:24 +0000332 }
333 }
334}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000335
Damien George95004e52014-04-05 17:17:19 +0100336mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100337 for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100338 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100339 mp_obj_t elem = set->table[pos];
340 // delete element
341 set->used--;
342 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
343 // optimisation if next slot is empty
344 set->table[pos] = MP_OBJ_NULL;
345 } else {
346 set->table[pos] = MP_OBJ_SENTINEL;
347 }
348 return elem;
349 }
350 }
351 return MP_OBJ_NULL;
352}
353
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000354void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000355 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000356 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000357 set->used = 0;
358 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000359}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300360
Damien George7860c2a2014-11-05 21:16:41 +0000361#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300362void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100363 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300364 if (map->table[i].key != NULL) {
365 mp_obj_print(map->table[i].key, PRINT_REPR);
366 } else {
367 printf("(nil)");
368 }
369 printf(": %p\n", map->table[i].value);
370 }
371 printf("---\n");
372}
373#endif