blob: 7f3c90059085e0aa86101325969568e49364475d [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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>
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020029#include <string.h>
Damien George8b0535e2014-04-05 21:53:54 +010030#include <assert.h>
Damien660365e2013-12-17 18:27:24 +000031
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/mpconfig.h"
33#include "py/misc.h"
Damien Georgec2a4e4e2015-05-11 12:25:19 +000034#include "py/runtime0.h"
35#include "py/runtime.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,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020041 .is_fixed = 1,
42 .is_ordered = 1,
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +020043 .used = 0,
44 .alloc = 0,
45 .table = NULL,
46};
47
Damien George00137b82016-04-15 16:24:46 +010048// This table of sizes is used to control the growth of hash tables.
49// The first set of sizes are chosen so the allocation fits exactly in a
50// 4-word GC block, and it's not so important for these small values to be
51// prime. The latter sizes are prime and increase at an increasing rate.
Damien George3ff16ff2016-05-20 12:38:15 +010052STATIC const uint16_t hash_allocation_sizes[] = {
Damien George00137b82016-04-15 16:24:46 +010053 0, 2, 4, 6, 8, 10, 12, // +2
54 17, 23, 29, 37, 47, 59, 73, // *1.25
55 97, 127, 167, 223, 293, 389, 521, 691, 919, 1223, 1627, 2161, // *1.33
56 3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
57};
Damien660365e2013-12-17 18:27:24 +000058
Damien Georgeaf622eb2017-02-08 11:00:15 +110059STATIC size_t get_hash_alloc_greater_or_equal_to(size_t x) {
Damien George00137b82016-04-15 16:24:46 +010060 for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
61 if (hash_allocation_sizes[i] >= x) {
62 return hash_allocation_sizes[i];
Damien660365e2013-12-17 18:27:24 +000063 }
64 }
65 // ran out of primes in the table!
66 // return something sensible, at least make it odd
Damien George00137b82016-04-15 16:24:46 +010067 return (x + x / 2) | 1;
Damien660365e2013-12-17 18:27:24 +000068}
69
Damiend99b0522013-12-21 18:17:45 +000070/******************************************************************************/
71/* map */
72
Damien Georgeaf622eb2017-02-08 11:00:15 +110073void mp_map_init(mp_map_t *map, size_t n) {
Damien George9a58d762014-02-08 18:47:46 +000074 if (n == 0) {
75 map->alloc = 0;
76 map->table = NULL;
77 } else {
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +030078 map->alloc = n;
Damien George9a58d762014-02-08 18:47:46 +000079 map->table = m_new0(mp_map_elem_t, map->alloc);
80 }
Damien George38a2da62014-01-08 17:33:12 +000081 map->used = 0;
82 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020083 map->is_fixed = 0;
84 map->is_ordered = 0;
Damien George9a58d762014-02-08 18:47:46 +000085}
86
Damien Georgeaf622eb2017-02-08 11:00:15 +110087void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
Damien George9a58d762014-02-08 18:47:46 +000088 map->alloc = n;
89 map->used = n;
90 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020091 map->is_fixed = 1;
92 map->is_ordered = 1;
Damien George9a58d762014-02-08 18:47:46 +000093 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000094}
95
Damien Georgeaf622eb2017-02-08 11:00:15 +110096mp_map_t *mp_map_new(size_t n) {
Damiend99b0522013-12-21 18:17:45 +000097 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000098 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000099 return map;
100}
101
Paul Sokolovsky9a24a042014-01-25 00:02:20 +0200102// Differentiate from mp_map_clear() - semantics is different
103void mp_map_deinit(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200104 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +0000105 m_del(mp_map_elem_t, map->table, map->alloc);
106 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +0200107 map->used = map->alloc = 0;
108}
109
110void mp_map_free(mp_map_t *map) {
111 mp_map_deinit(map);
112 m_del_obj(mp_map_t, map);
113}
114
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000115void mp_map_clear(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200116 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +0000117 m_del(mp_map_elem_t, map->table, map->alloc);
118 }
119 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000120 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000121 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200122 map->is_fixed = 0;
Damien George9a58d762014-02-08 18:47:46 +0000123 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000124}
125
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200126STATIC void mp_map_rehash(mp_map_t *map) {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100127 size_t old_alloc = map->alloc;
128 size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000129 mp_map_elem_t *old_table = map->table;
Stephen Kyleb4753272016-04-01 10:51:40 +0100130 mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
131 // If we reach this point, table resizing succeeded, now we can edit the old map.
132 map->alloc = new_alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000133 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000134 map->all_keys_are_qstrs = 1;
Stephen Kyleb4753272016-04-01 10:51:40 +0100135 map->table = new_table;
Damien Georgeaf622eb2017-02-08 11:00:15 +1100136 for (size_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100137 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000138 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 +0000139 }
140 }
141 m_del(mp_map_elem_t, old_table, old_alloc);
142}
143
Damien Georged0e82432014-04-05 23:33:12 +0100144// MP_MAP_LOOKUP behaviour:
145// - returns NULL if not found, else the slot it was found in with key,value non-null
146// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
147// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
148// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
149// - returns NULL if not found, else the slot if was found in with key null and value non-null
Damien George2801e6f2015-04-04 15:53:11 +0100150mp_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 +0100151
Damien George6dde0192015-12-31 00:19:28 +0000152 if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
153 // can't add/remove from a fixed array
154 return NULL;
155 }
156
Damien George186e4632014-04-28 12:11:57 +0100157 // Work out if we can compare just pointers
158 bool compare_only_ptrs = map->all_keys_are_qstrs;
159 if (compare_only_ptrs) {
160 if (MP_OBJ_IS_QSTR(index)) {
161 // Index is a qstr, so can just do ptr comparison.
162 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
163 // Index is a non-interned string.
164 // We can either intern the string, or force a full equality comparison.
165 // We chose the latter, since interning costs time and potentially RAM,
166 // and it won't necessarily benefit subsequent calls because these calls
167 // most likely won't pass the newly-interned string.
168 compare_only_ptrs = false;
Damien Georged1cee022015-03-20 17:41:37 +0000169 } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George186e4632014-04-28 12:11:57 +0100170 // If we are not adding, then we can return straight away a failed
171 // lookup because we know that the index will never be found.
172 return NULL;
173 }
174 }
175
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200176 // if the map is an ordered array then we must do a brute force linear search
177 if (map->is_ordered) {
Damien George9a58d762014-02-08 18:47:46 +0000178 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100179 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200180 if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
Damien George9275c182017-03-03 11:21:19 +1100181 // remove the found element by moving the rest of the array down
182 mp_obj_t value = elem->value;
183 --map->used;
184 memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
185 // put the found element after the end so the caller can access it if needed
186 elem = &map->table[map->used];
187 elem->key = MP_OBJ_NULL;
188 elem->value = value;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200189 }
Damien George9a58d762014-02-08 18:47:46 +0000190 return elem;
191 }
192 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200193 if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
194 return NULL;
195 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200196 if (map->used == map->alloc) {
197 // TODO: Alloc policy
198 map->alloc += 4;
199 map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
200 mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
201 }
202 mp_map_elem_t *elem = map->table + map->used++;
203 elem->key = index;
204 if (!MP_OBJ_IS_QSTR(index)) {
205 map->all_keys_are_qstrs = 0;
206 }
207 return elem;
Damien George9a58d762014-02-08 18:47:46 +0000208 }
209
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200210 // map is a hash table (not an ordered array), so do a hash lookup
Damien George9a58d762014-02-08 18:47:46 +0000211
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000212 if (map->alloc == 0) {
Damien Georged1cee022015-03-20 17:41:37 +0000213 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000214 mp_map_rehash(map);
215 } else {
216 return NULL;
217 }
218 }
Damien George9a58d762014-02-08 18:47:46 +0000219
Damien Georgebbe8d512015-12-26 21:15:47 +0000220 // get hash of index, with fast path for common case of qstr
221 mp_uint_t hash;
222 if (MP_OBJ_IS_QSTR(index)) {
223 hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
224 } else {
225 hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
226 }
227
Damien Georgeaf622eb2017-02-08 11:00:15 +1100228 size_t pos = hash % map->alloc;
229 size_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100230 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000231 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100232 mp_map_elem_t *slot = &map->table[pos];
233 if (slot->key == MP_OBJ_NULL) {
234 // found NULL slot, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000235 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100236 map->used += 1;
237 if (avail_slot == NULL) {
238 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000239 }
Damien George593faf12015-11-19 01:27:28 +0000240 avail_slot->key = index;
241 avail_slot->value = MP_OBJ_NULL;
Damien George95004e52014-04-05 17:17:19 +0100242 if (!MP_OBJ_IS_QSTR(index)) {
243 map->all_keys_are_qstrs = 0;
244 }
Damien George593faf12015-11-19 01:27:28 +0000245 return avail_slot;
Damien George95004e52014-04-05 17:17:19 +0100246 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100247 return NULL;
Damien660365e2013-12-17 18:27:24 +0000248 }
Damien George95004e52014-04-05 17:17:19 +0100249 } else if (slot->key == MP_OBJ_SENTINEL) {
250 // found deleted slot, remember for later
251 if (avail_slot == NULL) {
252 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000253 }
Damien George186e4632014-04-28 12:11:57 +0100254 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100255 // found index
256 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien Georged1cee022015-03-20 17:41:37 +0000257 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100258 // delete element in this slot
259 map->used--;
260 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
261 // optimisation if next slot is empty
262 slot->key = MP_OBJ_NULL;
263 } else {
264 slot->key = MP_OBJ_SENTINEL;
265 }
Damien Georged0e82432014-04-05 23:33:12 +0100266 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000267 }
Damien George95004e52014-04-05 17:17:19 +0100268 return slot;
Damien660365e2013-12-17 18:27:24 +0000269 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300270
271 // not yet found, keep searching in this table
272 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100273
274 if (pos == start_pos) {
275 // search got back to starting position, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000276 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100277 if (avail_slot != NULL) {
278 // there was an available slot, so use that
279 map->used++;
280 avail_slot->key = index;
281 avail_slot->value = MP_OBJ_NULL;
282 if (!MP_OBJ_IS_QSTR(index)) {
283 map->all_keys_are_qstrs = 0;
284 }
285 return avail_slot;
286 } else {
287 // not enough room in table, rehash it
288 mp_map_rehash(map);
289 // restart the search for the new element
290 start_pos = pos = hash % map->alloc;
291 }
292 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100293 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100294 }
295 }
Damien660365e2013-12-17 18:27:24 +0000296 }
297}
298
Damiend99b0522013-12-21 18:17:45 +0000299/******************************************************************************/
300/* set */
301
Damien Georgee37dcaa2014-12-27 17:07:16 +0000302#if MICROPY_PY_BUILTINS_SET
303
Damien Georgeaf622eb2017-02-08 11:00:15 +1100304void mp_set_init(mp_set_t *set, size_t n) {
Damien George2bfd2dc2014-04-07 01:16:17 +0100305 set->alloc = n;
Damiend99b0522013-12-21 18:17:45 +0000306 set->used = 0;
307 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000308}
309
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200310STATIC void mp_set_rehash(mp_set_t *set) {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100311 size_t old_alloc = set->alloc;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000312 mp_obj_t *old_table = set->table;
Damien George00137b82016-04-15 16:24:46 +0100313 set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000314 set->used = 0;
315 set->table = m_new0(mp_obj_t, set->alloc);
Damien Georgeaf622eb2017-02-08 11:00:15 +1100316 for (size_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100317 if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
318 mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000319 }
320 }
321 m_del(mp_obj_t, old_table, old_alloc);
322}
323
John R. Lenton2a241722014-01-12 16:39:39 +0000324mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
Damien Georged1cee022015-03-20 17:41:37 +0000325 // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
326 // is handled by using bitwise operations.
327
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000328 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000329 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000330 mp_set_rehash(set);
331 } else {
Damien George83229d32015-11-20 14:09:20 +0000332 return MP_OBJ_NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000333 }
334 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000335 mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
Damien Georgeaf622eb2017-02-08 11:00:15 +1100336 size_t pos = hash % set->alloc;
337 size_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100338 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000339 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000340 mp_obj_t elem = set->table[pos];
341 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100342 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000343 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100344 if (avail_slot == NULL) {
345 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000346 }
Damien George95004e52014-04-05 17:17:19 +0100347 set->used++;
348 *avail_slot = index;
349 return index;
Damien660365e2013-12-17 18:27:24 +0000350 } else {
Damiend99b0522013-12-21 18:17:45 +0000351 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000352 }
Damien George95004e52014-04-05 17:17:19 +0100353 } else if (elem == MP_OBJ_SENTINEL) {
354 // found deleted slot, remember for later
355 if (avail_slot == NULL) {
356 avail_slot = &set->table[pos];
357 }
358 } else if (mp_obj_equal(elem, index)) {
359 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000360 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100361 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000362 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100363 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
364 // optimisation if next slot is empty
365 set->table[pos] = MP_OBJ_NULL;
366 } else {
367 set->table[pos] = MP_OBJ_SENTINEL;
368 }
John R. Lenton2a241722014-01-12 16:39:39 +0000369 }
Damien660365e2013-12-17 18:27:24 +0000370 return elem;
Damien George95004e52014-04-05 17:17:19 +0100371 }
372
373 // not yet found, keep searching in this table
374 pos = (pos + 1) % set->alloc;
375
376 if (pos == start_pos) {
377 // search got back to starting position, so index is not in table
378 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
379 if (avail_slot != NULL) {
380 // there was an available slot, so use that
381 set->used++;
382 *avail_slot = index;
383 return index;
384 } else {
385 // not enough room in table, rehash it
386 mp_set_rehash(set);
387 // restart the search for the new element
388 start_pos = pos = hash % set->alloc;
389 }
390 } else {
391 return MP_OBJ_NULL;
392 }
Damien660365e2013-12-17 18:27:24 +0000393 }
394 }
395}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000396
Damien George95004e52014-04-05 17:17:19 +0100397mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100398 for (size_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100399 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100400 mp_obj_t elem = set->table[pos];
401 // delete element
402 set->used--;
403 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
404 // optimisation if next slot is empty
405 set->table[pos] = MP_OBJ_NULL;
406 } else {
407 set->table[pos] = MP_OBJ_SENTINEL;
408 }
409 return elem;
410 }
411 }
412 return MP_OBJ_NULL;
413}
414
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000415void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000416 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000417 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000418 set->used = 0;
419 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000420}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300421
Damien Georgee37dcaa2014-12-27 17:07:16 +0000422#endif // MICROPY_PY_BUILTINS_SET
423
Damien George7860c2a2014-11-05 21:16:41 +0000424#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300425void mp_map_dump(mp_map_t *map) {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100426 for (size_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300427 if (map->table[i].key != NULL) {
428 mp_obj_print(map->table[i].key, PRINT_REPR);
429 } else {
430 printf("(nil)");
431 }
432 printf(": %p\n", map->table[i].value);
433 }
434 printf("---\n");
435}
436#endif