blob: 6864513c26a73dc64c10e24b15be4e76f6c5bdbf [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>
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
Damien660365e2013-12-17 18:27:24 +000048// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000049// prefixed with zero for the empty case.
Damien George93965e72014-08-30 13:23:35 +010050STATIC 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 +000051
Damien George93965e72014-08-30 13:23:35 +010052STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
Damien George963a5a32015-01-16 17:47:07 +000053 for (size_t i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
Damien660365e2013-12-17 18:27:24 +000054 if (doubling_primes[i] >= x) {
55 return doubling_primes[i];
56 }
57 }
58 // ran out of primes in the table!
59 // return something sensible, at least make it odd
60 return x | 1;
61}
62
Damiend99b0522013-12-21 18:17:45 +000063/******************************************************************************/
64/* map */
65
Damien George93965e72014-08-30 13:23:35 +010066void mp_map_init(mp_map_t *map, mp_uint_t n) {
Damien George9a58d762014-02-08 18:47:46 +000067 if (n == 0) {
68 map->alloc = 0;
69 map->table = NULL;
70 } else {
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +030071 map->alloc = n;
Damien George9a58d762014-02-08 18:47:46 +000072 map->table = m_new0(mp_map_elem_t, map->alloc);
73 }
Damien George38a2da62014-01-08 17:33:12 +000074 map->used = 0;
75 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020076 map->is_fixed = 0;
77 map->is_ordered = 0;
Damien George9a58d762014-02-08 18:47:46 +000078}
79
Damien George93965e72014-08-30 13:23:35 +010080void 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 +000081 map->alloc = n;
82 map->used = n;
83 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020084 map->is_fixed = 1;
85 map->is_ordered = 1;
Damien George9a58d762014-02-08 18:47:46 +000086 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000087}
88
Damien George93965e72014-08-30 13:23:35 +010089mp_map_t *mp_map_new(mp_uint_t n) {
Damiend99b0522013-12-21 18:17:45 +000090 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000091 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000092 return map;
93}
94
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020095// Differentiate from mp_map_clear() - semantics is different
96void mp_map_deinit(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020097 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +000098 m_del(mp_map_elem_t, map->table, map->alloc);
99 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +0200100 map->used = map->alloc = 0;
101}
102
103void mp_map_free(mp_map_t *map) {
104 mp_map_deinit(map);
105 m_del_obj(mp_map_t, map);
106}
107
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000108void mp_map_clear(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200109 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +0000110 m_del(mp_map_elem_t, map->table, map->alloc);
111 }
112 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000113 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000114 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200115 map->is_fixed = 0;
Damien George9a58d762014-02-08 18:47:46 +0000116 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000117}
118
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200119STATIC void mp_map_rehash(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100120 mp_uint_t old_alloc = map->alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000121 mp_map_elem_t *old_table = map->table;
122 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
123 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000124 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000125 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien George93965e72014-08-30 13:23:35 +0100126 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100127 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000128 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 +0000129 }
130 }
131 m_del(mp_map_elem_t, old_table, old_alloc);
132}
133
Damien Georged0e82432014-04-05 23:33:12 +0100134// MP_MAP_LOOKUP behaviour:
135// - returns NULL if not found, else the slot it was found in with key,value non-null
136// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
137// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
138// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
139// - 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 +0100140mp_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 +0100141
142 // Work out if we can compare just pointers
143 bool compare_only_ptrs = map->all_keys_are_qstrs;
144 if (compare_only_ptrs) {
145 if (MP_OBJ_IS_QSTR(index)) {
146 // Index is a qstr, so can just do ptr comparison.
147 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
148 // Index is a non-interned string.
149 // We can either intern the string, or force a full equality comparison.
150 // We chose the latter, since interning costs time and potentially RAM,
151 // and it won't necessarily benefit subsequent calls because these calls
152 // most likely won't pass the newly-interned string.
153 compare_only_ptrs = false;
Damien Georged1cee022015-03-20 17:41:37 +0000154 } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George186e4632014-04-28 12:11:57 +0100155 // If we are not adding, then we can return straight away a failed
156 // lookup because we know that the index will never be found.
157 return NULL;
158 }
159 }
160
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200161 // if the map is an ordered array then we must do a brute force linear search
162 if (map->is_ordered) {
163 if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
164 // can't add/remove from a fixed array
Damien George9a58d762014-02-08 18:47:46 +0000165 return NULL;
166 }
167 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100168 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200169 if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
170 elem->key = MP_OBJ_SENTINEL;
171 // keep elem->value so that caller can access it if needed
172 }
Damien George9a58d762014-02-08 18:47:46 +0000173 return elem;
174 }
175 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200176 if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
177 return NULL;
178 }
179 // TODO shrink array down over any previously-freed slots
180 if (map->used == map->alloc) {
181 // TODO: Alloc policy
182 map->alloc += 4;
183 map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
184 mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
185 }
186 mp_map_elem_t *elem = map->table + map->used++;
187 elem->key = index;
188 if (!MP_OBJ_IS_QSTR(index)) {
189 map->all_keys_are_qstrs = 0;
190 }
191 return elem;
Damien George9a58d762014-02-08 18:47:46 +0000192 }
193
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200194 // map is a hash table (not an ordered array), so do a hash lookup
Damien George9a58d762014-02-08 18:47:46 +0000195
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000196 if (map->alloc == 0) {
Damien Georged1cee022015-03-20 17:41:37 +0000197 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000198 mp_map_rehash(map);
199 } else {
200 return NULL;
201 }
202 }
Damien George9a58d762014-02-08 18:47:46 +0000203
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000204 mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
Damien George93965e72014-08-30 13:23:35 +0100205 mp_uint_t pos = hash % map->alloc;
206 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100207 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000208 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100209 mp_map_elem_t *slot = &map->table[pos];
210 if (slot->key == MP_OBJ_NULL) {
211 // found NULL slot, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000212 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100213 map->used += 1;
214 if (avail_slot == NULL) {
215 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000216 }
Damien George593faf12015-11-19 01:27:28 +0000217 avail_slot->key = index;
218 avail_slot->value = MP_OBJ_NULL;
Damien George95004e52014-04-05 17:17:19 +0100219 if (!MP_OBJ_IS_QSTR(index)) {
220 map->all_keys_are_qstrs = 0;
221 }
Damien George593faf12015-11-19 01:27:28 +0000222 return avail_slot;
Damien George95004e52014-04-05 17:17:19 +0100223 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100224 return NULL;
Damien660365e2013-12-17 18:27:24 +0000225 }
Damien George95004e52014-04-05 17:17:19 +0100226 } else if (slot->key == MP_OBJ_SENTINEL) {
227 // found deleted slot, remember for later
228 if (avail_slot == NULL) {
229 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000230 }
Damien George186e4632014-04-28 12:11:57 +0100231 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100232 // found index
233 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien Georged1cee022015-03-20 17:41:37 +0000234 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100235 // delete element in this slot
236 map->used--;
237 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
238 // optimisation if next slot is empty
239 slot->key = MP_OBJ_NULL;
240 } else {
241 slot->key = MP_OBJ_SENTINEL;
242 }
Damien Georged0e82432014-04-05 23:33:12 +0100243 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000244 }
Damien George95004e52014-04-05 17:17:19 +0100245 return slot;
Damien660365e2013-12-17 18:27:24 +0000246 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300247
248 // not yet found, keep searching in this table
249 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100250
251 if (pos == start_pos) {
252 // search got back to starting position, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000253 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100254 if (avail_slot != NULL) {
255 // there was an available slot, so use that
256 map->used++;
257 avail_slot->key = index;
258 avail_slot->value = MP_OBJ_NULL;
259 if (!MP_OBJ_IS_QSTR(index)) {
260 map->all_keys_are_qstrs = 0;
261 }
262 return avail_slot;
263 } else {
264 // not enough room in table, rehash it
265 mp_map_rehash(map);
266 // restart the search for the new element
267 start_pos = pos = hash % map->alloc;
268 }
269 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100270 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100271 }
272 }
Damien660365e2013-12-17 18:27:24 +0000273 }
274}
275
Damiend99b0522013-12-21 18:17:45 +0000276/******************************************************************************/
277/* set */
278
Damien Georgee37dcaa2014-12-27 17:07:16 +0000279#if MICROPY_PY_BUILTINS_SET
280
Damien George93965e72014-08-30 13:23:35 +0100281void mp_set_init(mp_set_t *set, mp_uint_t n) {
Damien George2bfd2dc2014-04-07 01:16:17 +0100282 set->alloc = n;
Damiend99b0522013-12-21 18:17:45 +0000283 set->used = 0;
284 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000285}
286
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200287STATIC void mp_set_rehash(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100288 mp_uint_t old_alloc = set->alloc;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000289 mp_obj_t *old_table = set->table;
290 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
291 set->used = 0;
292 set->table = m_new0(mp_obj_t, set->alloc);
Damien George93965e72014-08-30 13:23:35 +0100293 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100294 if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
295 mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000296 }
297 }
298 m_del(mp_obj_t, old_table, old_alloc);
299}
300
John R. Lenton2a241722014-01-12 16:39:39 +0000301mp_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 +0000302 // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
303 // is handled by using bitwise operations.
304
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000305 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000306 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000307 mp_set_rehash(set);
308 } else {
309 return NULL;
310 }
311 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000312 mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
Damien George93965e72014-08-30 13:23:35 +0100313 mp_uint_t pos = hash % set->alloc;
314 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100315 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000316 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000317 mp_obj_t elem = set->table[pos];
318 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100319 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000320 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100321 if (avail_slot == NULL) {
322 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000323 }
Damien George95004e52014-04-05 17:17:19 +0100324 set->used++;
325 *avail_slot = index;
326 return index;
Damien660365e2013-12-17 18:27:24 +0000327 } else {
Damiend99b0522013-12-21 18:17:45 +0000328 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000329 }
Damien George95004e52014-04-05 17:17:19 +0100330 } else if (elem == MP_OBJ_SENTINEL) {
331 // found deleted slot, remember for later
332 if (avail_slot == NULL) {
333 avail_slot = &set->table[pos];
334 }
335 } else if (mp_obj_equal(elem, index)) {
336 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000337 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100338 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000339 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100340 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
341 // optimisation if next slot is empty
342 set->table[pos] = MP_OBJ_NULL;
343 } else {
344 set->table[pos] = MP_OBJ_SENTINEL;
345 }
John R. Lenton2a241722014-01-12 16:39:39 +0000346 }
Damien660365e2013-12-17 18:27:24 +0000347 return elem;
Damien George95004e52014-04-05 17:17:19 +0100348 }
349
350 // not yet found, keep searching in this table
351 pos = (pos + 1) % set->alloc;
352
353 if (pos == start_pos) {
354 // search got back to starting position, so index is not in table
355 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
356 if (avail_slot != NULL) {
357 // there was an available slot, so use that
358 set->used++;
359 *avail_slot = index;
360 return index;
361 } else {
362 // not enough room in table, rehash it
363 mp_set_rehash(set);
364 // restart the search for the new element
365 start_pos = pos = hash % set->alloc;
366 }
367 } else {
368 return MP_OBJ_NULL;
369 }
Damien660365e2013-12-17 18:27:24 +0000370 }
371 }
372}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000373
Damien George95004e52014-04-05 17:17:19 +0100374mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100375 for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100376 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100377 mp_obj_t elem = set->table[pos];
378 // delete element
379 set->used--;
380 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
381 // optimisation if next slot is empty
382 set->table[pos] = MP_OBJ_NULL;
383 } else {
384 set->table[pos] = MP_OBJ_SENTINEL;
385 }
386 return elem;
387 }
388 }
389 return MP_OBJ_NULL;
390}
391
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000392void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000393 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000394 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000395 set->used = 0;
396 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000397}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300398
Damien Georgee37dcaa2014-12-27 17:07:16 +0000399#endif // MICROPY_PY_BUILTINS_SET
400
Damien George7860c2a2014-11-05 21:16:41 +0000401#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300402void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100403 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300404 if (map->table[i].key != NULL) {
405 mp_obj_print(map->table[i].key, PRINT_REPR);
406 } else {
407 printf("(nil)");
408 }
409 printf(": %p\n", map->table[i].value);
410 }
411 printf("---\n");
412}
413#endif