blob: a06325862e73785ac786c8818ffdcf1880098487 [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
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +020037// Fixed empty map. Useful when need to call kw-receiving functions
38// without any keywords from C, etc.
39const mp_map_t mp_const_empty_map = {
40 .all_keys_are_qstrs = 0,
41 .table_is_fixed_array = 1,
42 .used = 0,
43 .alloc = 0,
44 .table = NULL,
45};
46
Damien660365e2013-12-17 18:27:24 +000047// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000048// prefixed with zero for the empty case.
Damien George93965e72014-08-30 13:23:35 +010049STATIC 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 +000050
Damien George93965e72014-08-30 13:23:35 +010051STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
52 for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
Damien660365e2013-12-17 18:27:24 +000053 if (doubling_primes[i] >= x) {
54 return doubling_primes[i];
55 }
56 }
57 // ran out of primes in the table!
58 // return something sensible, at least make it odd
59 return x | 1;
60}
61
Damiend99b0522013-12-21 18:17:45 +000062/******************************************************************************/
63/* map */
64
Damien George93965e72014-08-30 13:23:35 +010065void mp_map_init(mp_map_t *map, mp_uint_t n) {
Damien George9a58d762014-02-08 18:47:46 +000066 if (n == 0) {
67 map->alloc = 0;
68 map->table = NULL;
69 } else {
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +030070 map->alloc = n;
Damien George9a58d762014-02-08 18:47:46 +000071 map->table = m_new0(mp_map_elem_t, map->alloc);
72 }
Damien George38a2da62014-01-08 17:33:12 +000073 map->used = 0;
74 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000075 map->table_is_fixed_array = 0;
76}
77
Damien George93965e72014-08-30 13:23:35 +010078void 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 +000079 map->alloc = n;
80 map->used = n;
81 map->all_keys_are_qstrs = 1;
82 map->table_is_fixed_array = 1;
83 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000084}
85
Damien George93965e72014-08-30 13:23:35 +010086mp_map_t *mp_map_new(mp_uint_t n) {
Damiend99b0522013-12-21 18:17:45 +000087 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000088 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000089 return map;
90}
91
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020092// Differentiate from mp_map_clear() - semantics is different
93void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000094 if (!map->table_is_fixed_array) {
95 m_del(mp_map_elem_t, map->table, map->alloc);
96 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020097 map->used = map->alloc = 0;
98}
99
100void mp_map_free(mp_map_t *map) {
101 mp_map_deinit(map);
102 m_del_obj(mp_map_t, map);
103}
104
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000105void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +0000106 if (!map->table_is_fixed_array) {
107 m_del(mp_map_elem_t, map->table, map->alloc);
108 }
109 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000110 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000111 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +0000112 map->table_is_fixed_array = 0;
113 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000114}
115
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200116STATIC void mp_map_rehash(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100117 mp_uint_t old_alloc = map->alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000118 mp_map_elem_t *old_table = map->table;
119 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
120 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000121 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000122 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien George93965e72014-08-30 13:23:35 +0100123 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100124 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000125 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 +0000126 }
127 }
128 m_del(mp_map_elem_t, old_table, old_alloc);
129}
130
Damien Georged0e82432014-04-05 23:33:12 +0100131// MP_MAP_LOOKUP behaviour:
132// - returns NULL if not found, else the slot it was found in with key,value non-null
133// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
134// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
135// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
136// - 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 +0000137mp_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 +0100138
139 // Work out if we can compare just pointers
140 bool compare_only_ptrs = map->all_keys_are_qstrs;
141 if (compare_only_ptrs) {
142 if (MP_OBJ_IS_QSTR(index)) {
143 // Index is a qstr, so can just do ptr comparison.
144 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
145 // Index is a non-interned string.
146 // We can either intern the string, or force a full equality comparison.
147 // We chose the latter, since interning costs time and potentially RAM,
148 // and it won't necessarily benefit subsequent calls because these calls
149 // most likely won't pass the newly-interned string.
150 compare_only_ptrs = false;
151 } else if (!(lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
152 // If we are not adding, then we can return straight away a failed
153 // lookup because we know that the index will never be found.
154 return NULL;
155 }
156 }
157
Damien George9a58d762014-02-08 18:47:46 +0000158 // if the map is a fixed array then we must do a brute force linear search
159 if (map->table_is_fixed_array) {
160 if (lookup_kind != MP_MAP_LOOKUP) {
161 return NULL;
162 }
163 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100164 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Damien George9a58d762014-02-08 18:47:46 +0000165 return elem;
166 }
167 }
168 return NULL;
169 }
170
171 // map is a hash table (not a fixed array), so do a hash lookup
172
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000173 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000174 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000175 mp_map_rehash(map);
176 } else {
177 return NULL;
178 }
179 }
Damien George9a58d762014-02-08 18:47:46 +0000180
Damien George40f3c022014-07-03 13:25:24 +0100181 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100182 mp_uint_t pos = hash % map->alloc;
183 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100184 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000185 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100186 mp_map_elem_t *slot = &map->table[pos];
187 if (slot->key == MP_OBJ_NULL) {
188 // found NULL slot, so index is not in table
Damien George9a58d762014-02-08 18:47:46 +0000189 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100190 map->used += 1;
191 if (avail_slot == NULL) {
192 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000193 }
Damien George95004e52014-04-05 17:17:19 +0100194 slot->key = index;
195 slot->value = MP_OBJ_NULL;
196 if (!MP_OBJ_IS_QSTR(index)) {
197 map->all_keys_are_qstrs = 0;
198 }
199 return slot;
200 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100201 return NULL;
Damien660365e2013-12-17 18:27:24 +0000202 }
Damien George95004e52014-04-05 17:17:19 +0100203 } else if (slot->key == MP_OBJ_SENTINEL) {
204 // found deleted slot, remember for later
205 if (avail_slot == NULL) {
206 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000207 }
Damien George186e4632014-04-28 12:11:57 +0100208 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100209 // found index
210 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien George9a58d762014-02-08 18:47:46 +0000211 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100212 // delete element in this slot
213 map->used--;
214 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
215 // optimisation if next slot is empty
216 slot->key = MP_OBJ_NULL;
217 } else {
218 slot->key = MP_OBJ_SENTINEL;
219 }
Damien Georged0e82432014-04-05 23:33:12 +0100220 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000221 }
Damien George95004e52014-04-05 17:17:19 +0100222 return slot;
Damien660365e2013-12-17 18:27:24 +0000223 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300224
225 // not yet found, keep searching in this table
226 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100227
228 if (pos == start_pos) {
229 // search got back to starting position, so index is not in table
230 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
231 if (avail_slot != NULL) {
232 // there was an available slot, so use that
233 map->used++;
234 avail_slot->key = index;
235 avail_slot->value = MP_OBJ_NULL;
236 if (!MP_OBJ_IS_QSTR(index)) {
237 map->all_keys_are_qstrs = 0;
238 }
239 return avail_slot;
240 } else {
241 // not enough room in table, rehash it
242 mp_map_rehash(map);
243 // restart the search for the new element
244 start_pos = pos = hash % map->alloc;
245 }
246 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100247 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100248 }
249 }
Damien660365e2013-12-17 18:27:24 +0000250 }
251}
252
Damiend99b0522013-12-21 18:17:45 +0000253/******************************************************************************/
254/* set */
255
Damien George93965e72014-08-30 13:23:35 +0100256void mp_set_init(mp_set_t *set, mp_uint_t n) {
Damien George2bfd2dc2014-04-07 01:16:17 +0100257 set->alloc = n;
Damiend99b0522013-12-21 18:17:45 +0000258 set->used = 0;
259 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000260}
261
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200262STATIC void mp_set_rehash(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100263 mp_uint_t old_alloc = set->alloc;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000264 mp_obj_t *old_table = set->table;
265 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
266 set->used = 0;
267 set->table = m_new0(mp_obj_t, set->alloc);
Damien George93965e72014-08-30 13:23:35 +0100268 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100269 if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
270 mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000271 }
272 }
273 m_del(mp_obj_t, old_table, old_alloc);
274}
275
John R. Lenton2a241722014-01-12 16:39:39 +0000276mp_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 +0000277 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000278 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000279 mp_set_rehash(set);
280 } else {
281 return NULL;
282 }
283 }
Damien George40f3c022014-07-03 13:25:24 +0100284 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100285 mp_uint_t pos = hash % set->alloc;
286 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100287 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000288 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000289 mp_obj_t elem = set->table[pos];
290 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100291 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000292 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100293 if (avail_slot == NULL) {
294 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000295 }
Damien George95004e52014-04-05 17:17:19 +0100296 set->used++;
297 *avail_slot = index;
298 return index;
Damien660365e2013-12-17 18:27:24 +0000299 } else {
Damiend99b0522013-12-21 18:17:45 +0000300 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000301 }
Damien George95004e52014-04-05 17:17:19 +0100302 } else if (elem == MP_OBJ_SENTINEL) {
303 // found deleted slot, remember for later
304 if (avail_slot == NULL) {
305 avail_slot = &set->table[pos];
306 }
307 } else if (mp_obj_equal(elem, index)) {
308 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000309 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100310 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000311 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100312 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
313 // optimisation if next slot is empty
314 set->table[pos] = MP_OBJ_NULL;
315 } else {
316 set->table[pos] = MP_OBJ_SENTINEL;
317 }
John R. Lenton2a241722014-01-12 16:39:39 +0000318 }
Damien660365e2013-12-17 18:27:24 +0000319 return elem;
Damien George95004e52014-04-05 17:17:19 +0100320 }
321
322 // not yet found, keep searching in this table
323 pos = (pos + 1) % set->alloc;
324
325 if (pos == start_pos) {
326 // search got back to starting position, so index is not in table
327 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
328 if (avail_slot != NULL) {
329 // there was an available slot, so use that
330 set->used++;
331 *avail_slot = index;
332 return index;
333 } else {
334 // not enough room in table, rehash it
335 mp_set_rehash(set);
336 // restart the search for the new element
337 start_pos = pos = hash % set->alloc;
338 }
339 } else {
340 return MP_OBJ_NULL;
341 }
Damien660365e2013-12-17 18:27:24 +0000342 }
343 }
344}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000345
Damien George95004e52014-04-05 17:17:19 +0100346mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100347 for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100348 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100349 mp_obj_t elem = set->table[pos];
350 // delete element
351 set->used--;
352 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
353 // optimisation if next slot is empty
354 set->table[pos] = MP_OBJ_NULL;
355 } else {
356 set->table[pos] = MP_OBJ_SENTINEL;
357 }
358 return elem;
359 }
360 }
361 return MP_OBJ_NULL;
362}
363
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000364void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000365 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000366 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000367 set->used = 0;
368 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000369}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300370
Damien George7860c2a2014-11-05 21:16:41 +0000371#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300372void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100373 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300374 if (map->table[i].key != NULL) {
375 mp_obj_print(map->table[i].key, PRINT_REPR);
376 } else {
377 printf("(nil)");
378 }
379 printf(": %p\n", map->table[i].value);
380 }
381 printf("---\n");
382}
383#endif