blob: 8cfc19b6980a7bb4d0fbbf18c3dc42ce3ee62ac3 [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"
34#include "py/obj.h"
Damien660365e2013-12-17 18:27:24 +000035
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +020036// Fixed empty map. Useful when need to call kw-receiving functions
37// without any keywords from C, etc.
38const mp_map_t mp_const_empty_map = {
39 .all_keys_are_qstrs = 0,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020040 .is_fixed = 1,
41 .is_ordered = 1,
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +020042 .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) {
Damien George963a5a32015-01-16 17:47:07 +000052 for (size_t 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;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020075 map->is_fixed = 0;
76 map->is_ordered = 0;
Damien George9a58d762014-02-08 18:47:46 +000077}
78
Damien George93965e72014-08-30 13:23:35 +010079void 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 +000080 map->alloc = n;
81 map->used = n;
82 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020083 map->is_fixed = 1;
84 map->is_ordered = 1;
Damien George9a58d762014-02-08 18:47:46 +000085 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000086}
87
Damien George93965e72014-08-30 13:23:35 +010088mp_map_t *mp_map_new(mp_uint_t n) {
Damiend99b0522013-12-21 18:17:45 +000089 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000090 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000091 return map;
92}
93
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020094// Differentiate from mp_map_clear() - semantics is different
95void mp_map_deinit(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020096 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +000097 m_del(mp_map_elem_t, map->table, map->alloc);
98 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020099 map->used = map->alloc = 0;
100}
101
102void mp_map_free(mp_map_t *map) {
103 mp_map_deinit(map);
104 m_del_obj(mp_map_t, map);
105}
106
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000107void mp_map_clear(mp_map_t *map) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200108 if (!map->is_fixed) {
Damien George9a58d762014-02-08 18:47:46 +0000109 m_del(mp_map_elem_t, map->table, map->alloc);
110 }
111 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000112 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000113 map->all_keys_are_qstrs = 1;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200114 map->is_fixed = 0;
Damien George9a58d762014-02-08 18:47:46 +0000115 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000116}
117
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200118STATIC void mp_map_rehash(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100119 mp_uint_t old_alloc = map->alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000120 mp_map_elem_t *old_table = map->table;
121 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
122 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000123 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000124 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien George93965e72014-08-30 13:23:35 +0100125 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100126 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000127 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 +0000128 }
129 }
130 m_del(mp_map_elem_t, old_table, old_alloc);
131}
132
Damien Georged0e82432014-04-05 23:33:12 +0100133// MP_MAP_LOOKUP behaviour:
134// - returns NULL if not found, else the slot it was found in with key,value non-null
135// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
136// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
137// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
138// - 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 +0100139mp_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 +0100140
141 // Work out if we can compare just pointers
142 bool compare_only_ptrs = map->all_keys_are_qstrs;
143 if (compare_only_ptrs) {
144 if (MP_OBJ_IS_QSTR(index)) {
145 // Index is a qstr, so can just do ptr comparison.
146 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
147 // Index is a non-interned string.
148 // We can either intern the string, or force a full equality comparison.
149 // We chose the latter, since interning costs time and potentially RAM,
150 // and it won't necessarily benefit subsequent calls because these calls
151 // most likely won't pass the newly-interned string.
152 compare_only_ptrs = false;
Damien Georged1cee022015-03-20 17:41:37 +0000153 } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George186e4632014-04-28 12:11:57 +0100154 // If we are not adding, then we can return straight away a failed
155 // lookup because we know that the index will never be found.
156 return NULL;
157 }
158 }
159
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200160 // if the map is an ordered array then we must do a brute force linear search
161 if (map->is_ordered) {
162 if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
163 // can't add/remove from a fixed array
Damien George9a58d762014-02-08 18:47:46 +0000164 return NULL;
165 }
166 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100167 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200168 if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
169 elem->key = MP_OBJ_SENTINEL;
170 // keep elem->value so that caller can access it if needed
171 }
Damien George9a58d762014-02-08 18:47:46 +0000172 return elem;
173 }
174 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200175 if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
176 return NULL;
177 }
178 // TODO shrink array down over any previously-freed slots
179 if (map->used == map->alloc) {
180 // TODO: Alloc policy
181 map->alloc += 4;
182 map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
183 mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
184 }
185 mp_map_elem_t *elem = map->table + map->used++;
186 elem->key = index;
187 if (!MP_OBJ_IS_QSTR(index)) {
188 map->all_keys_are_qstrs = 0;
189 }
190 return elem;
Damien George9a58d762014-02-08 18:47:46 +0000191 }
192
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200193 // map is a hash table (not an ordered array), so do a hash lookup
Damien George9a58d762014-02-08 18:47:46 +0000194
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000195 if (map->alloc == 0) {
Damien Georged1cee022015-03-20 17:41:37 +0000196 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000197 mp_map_rehash(map);
198 } else {
199 return NULL;
200 }
201 }
Damien George9a58d762014-02-08 18:47:46 +0000202
Damien George40f3c022014-07-03 13:25:24 +0100203 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100204 mp_uint_t pos = hash % map->alloc;
205 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100206 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000207 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100208 mp_map_elem_t *slot = &map->table[pos];
209 if (slot->key == MP_OBJ_NULL) {
210 // found NULL slot, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000211 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100212 map->used += 1;
213 if (avail_slot == NULL) {
214 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000215 }
Damien George95004e52014-04-05 17:17:19 +0100216 slot->key = index;
217 slot->value = MP_OBJ_NULL;
218 if (!MP_OBJ_IS_QSTR(index)) {
219 map->all_keys_are_qstrs = 0;
220 }
221 return slot;
222 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100223 return NULL;
Damien660365e2013-12-17 18:27:24 +0000224 }
Damien George95004e52014-04-05 17:17:19 +0100225 } else if (slot->key == MP_OBJ_SENTINEL) {
226 // found deleted slot, remember for later
227 if (avail_slot == NULL) {
228 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000229 }
Damien George186e4632014-04-28 12:11:57 +0100230 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100231 // found index
232 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien Georged1cee022015-03-20 17:41:37 +0000233 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100234 // delete element in this slot
235 map->used--;
236 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
237 // optimisation if next slot is empty
238 slot->key = MP_OBJ_NULL;
239 } else {
240 slot->key = MP_OBJ_SENTINEL;
241 }
Damien Georged0e82432014-04-05 23:33:12 +0100242 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000243 }
Damien George95004e52014-04-05 17:17:19 +0100244 return slot;
Damien660365e2013-12-17 18:27:24 +0000245 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300246
247 // not yet found, keep searching in this table
248 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100249
250 if (pos == start_pos) {
251 // search got back to starting position, so index is not in table
Damien Georged1cee022015-03-20 17:41:37 +0000252 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100253 if (avail_slot != NULL) {
254 // there was an available slot, so use that
255 map->used++;
256 avail_slot->key = index;
257 avail_slot->value = MP_OBJ_NULL;
258 if (!MP_OBJ_IS_QSTR(index)) {
259 map->all_keys_are_qstrs = 0;
260 }
261 return avail_slot;
262 } else {
263 // not enough room in table, rehash it
264 mp_map_rehash(map);
265 // restart the search for the new element
266 start_pos = pos = hash % map->alloc;
267 }
268 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100269 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100270 }
271 }
Damien660365e2013-12-17 18:27:24 +0000272 }
273}
274
Damiend99b0522013-12-21 18:17:45 +0000275/******************************************************************************/
276/* set */
277
Damien Georgee37dcaa2014-12-27 17:07:16 +0000278#if MICROPY_PY_BUILTINS_SET
279
Damien George93965e72014-08-30 13:23:35 +0100280void mp_set_init(mp_set_t *set, mp_uint_t n) {
Damien George2bfd2dc2014-04-07 01:16:17 +0100281 set->alloc = n;
Damiend99b0522013-12-21 18:17:45 +0000282 set->used = 0;
283 set->table = m_new0(mp_obj_t, set->alloc);
Damien660365e2013-12-17 18:27:24 +0000284}
285
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200286STATIC void mp_set_rehash(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100287 mp_uint_t old_alloc = set->alloc;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000288 mp_obj_t *old_table = set->table;
289 set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
290 set->used = 0;
291 set->table = m_new0(mp_obj_t, set->alloc);
Damien George93965e72014-08-30 13:23:35 +0100292 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100293 if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
294 mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000295 }
296 }
297 m_del(mp_obj_t, old_table, old_alloc);
298}
299
John R. Lenton2a241722014-01-12 16:39:39 +0000300mp_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 +0000301 // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
302 // is handled by using bitwise operations.
303
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000304 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000305 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000306 mp_set_rehash(set);
307 } else {
308 return NULL;
309 }
310 }
Damien George40f3c022014-07-03 13:25:24 +0100311 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100312 mp_uint_t pos = hash % set->alloc;
313 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100314 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000315 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000316 mp_obj_t elem = set->table[pos];
317 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100318 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000319 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100320 if (avail_slot == NULL) {
321 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000322 }
Damien George95004e52014-04-05 17:17:19 +0100323 set->used++;
324 *avail_slot = index;
325 return index;
Damien660365e2013-12-17 18:27:24 +0000326 } else {
Damiend99b0522013-12-21 18:17:45 +0000327 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000328 }
Damien George95004e52014-04-05 17:17:19 +0100329 } else if (elem == MP_OBJ_SENTINEL) {
330 // found deleted slot, remember for later
331 if (avail_slot == NULL) {
332 avail_slot = &set->table[pos];
333 }
334 } else if (mp_obj_equal(elem, index)) {
335 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000336 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100337 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000338 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100339 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
340 // optimisation if next slot is empty
341 set->table[pos] = MP_OBJ_NULL;
342 } else {
343 set->table[pos] = MP_OBJ_SENTINEL;
344 }
John R. Lenton2a241722014-01-12 16:39:39 +0000345 }
Damien660365e2013-12-17 18:27:24 +0000346 return elem;
Damien George95004e52014-04-05 17:17:19 +0100347 }
348
349 // not yet found, keep searching in this table
350 pos = (pos + 1) % set->alloc;
351
352 if (pos == start_pos) {
353 // search got back to starting position, so index is not in table
354 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
355 if (avail_slot != NULL) {
356 // there was an available slot, so use that
357 set->used++;
358 *avail_slot = index;
359 return index;
360 } else {
361 // not enough room in table, rehash it
362 mp_set_rehash(set);
363 // restart the search for the new element
364 start_pos = pos = hash % set->alloc;
365 }
366 } else {
367 return MP_OBJ_NULL;
368 }
Damien660365e2013-12-17 18:27:24 +0000369 }
370 }
371}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000372
Damien George95004e52014-04-05 17:17:19 +0100373mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100374 for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100375 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100376 mp_obj_t elem = set->table[pos];
377 // delete element
378 set->used--;
379 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
380 // optimisation if next slot is empty
381 set->table[pos] = MP_OBJ_NULL;
382 } else {
383 set->table[pos] = MP_OBJ_SENTINEL;
384 }
385 return elem;
386 }
387 }
388 return MP_OBJ_NULL;
389}
390
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000391void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000392 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000393 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000394 set->used = 0;
395 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000396}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300397
Damien Georgee37dcaa2014-12-27 17:07:16 +0000398#endif // MICROPY_PY_BUILTINS_SET
399
Damien George7860c2a2014-11-05 21:16:41 +0000400#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300401void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100402 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300403 if (map->table[i].key != NULL) {
404 mp_obj_print(map->table[i].key, PRINT_REPR);
405 } else {
406 printf("(nil)");
407 }
408 printf(": %p\n", map->table[i].value);
409 }
410 printf("---\n");
411}
412#endif