blob: a0db4ab11e25da72778566c615d15b806cb52ed7 [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 George38a2da62014-01-08 17:33:12 +0000139mp_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;
153 } else if (!(lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
154 // 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 George9a58d762014-02-08 18:47:46 +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 George9a58d762014-02-08 18:47:46 +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 George9a58d762014-02-08 18:47:46 +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
252 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
253 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) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000301 if (set->alloc == 0) {
John R. Lentonae00d332014-01-12 18:23:36 +0000302 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000303 mp_set_rehash(set);
304 } else {
305 return NULL;
306 }
307 }
Damien George40f3c022014-07-03 13:25:24 +0100308 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100309 mp_uint_t pos = hash % set->alloc;
310 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100311 mp_obj_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000312 for (;;) {
Damiend99b0522013-12-21 18:17:45 +0000313 mp_obj_t elem = set->table[pos];
314 if (elem == MP_OBJ_NULL) {
Damien George95004e52014-04-05 17:17:19 +0100315 // found NULL slot, so index is not in table
John R. Lentonae00d332014-01-12 18:23:36 +0000316 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100317 if (avail_slot == NULL) {
318 avail_slot = &set->table[pos];
Damien660365e2013-12-17 18:27:24 +0000319 }
Damien George95004e52014-04-05 17:17:19 +0100320 set->used++;
321 *avail_slot = index;
322 return index;
Damien660365e2013-12-17 18:27:24 +0000323 } else {
Damiend99b0522013-12-21 18:17:45 +0000324 return MP_OBJ_NULL;
Damien660365e2013-12-17 18:27:24 +0000325 }
Damien George95004e52014-04-05 17:17:19 +0100326 } else if (elem == MP_OBJ_SENTINEL) {
327 // found deleted slot, remember for later
328 if (avail_slot == NULL) {
329 avail_slot = &set->table[pos];
330 }
331 } else if (mp_obj_equal(elem, index)) {
332 // found index
John R. Lentonae00d332014-01-12 18:23:36 +0000333 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100334 // delete element
John R. Lenton2a241722014-01-12 16:39:39 +0000335 set->used--;
Damien George95004e52014-04-05 17:17:19 +0100336 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
337 // optimisation if next slot is empty
338 set->table[pos] = MP_OBJ_NULL;
339 } else {
340 set->table[pos] = MP_OBJ_SENTINEL;
341 }
John R. Lenton2a241722014-01-12 16:39:39 +0000342 }
Damien660365e2013-12-17 18:27:24 +0000343 return elem;
Damien George95004e52014-04-05 17:17:19 +0100344 }
345
346 // not yet found, keep searching in this table
347 pos = (pos + 1) % set->alloc;
348
349 if (pos == start_pos) {
350 // search got back to starting position, so index is not in table
351 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
352 if (avail_slot != NULL) {
353 // there was an available slot, so use that
354 set->used++;
355 *avail_slot = index;
356 return index;
357 } else {
358 // not enough room in table, rehash it
359 mp_set_rehash(set);
360 // restart the search for the new element
361 start_pos = pos = hash % set->alloc;
362 }
363 } else {
364 return MP_OBJ_NULL;
365 }
Damien660365e2013-12-17 18:27:24 +0000366 }
367 }
368}
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000369
Damien George95004e52014-04-05 17:17:19 +0100370mp_obj_t mp_set_remove_first(mp_set_t *set) {
Damien George93965e72014-08-30 13:23:35 +0100371 for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
Damien George8b0535e2014-04-05 21:53:54 +0100372 if (MP_SET_SLOT_IS_FILLED(set, pos)) {
Damien George95004e52014-04-05 17:17:19 +0100373 mp_obj_t elem = set->table[pos];
374 // delete element
375 set->used--;
376 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
377 // optimisation if next slot is empty
378 set->table[pos] = MP_OBJ_NULL;
379 } else {
380 set->table[pos] = MP_OBJ_SENTINEL;
381 }
382 return elem;
383 }
384 }
385 return MP_OBJ_NULL;
386}
387
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000388void mp_set_clear(mp_set_t *set) {
Damien George9a58d762014-02-08 18:47:46 +0000389 m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000390 set->alloc = 0;
Damien George9a58d762014-02-08 18:47:46 +0000391 set->used = 0;
392 set->table = NULL;
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000393}
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300394
Damien Georgee37dcaa2014-12-27 17:07:16 +0000395#endif // MICROPY_PY_BUILTINS_SET
396
Damien George7860c2a2014-11-05 21:16:41 +0000397#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300398void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100399 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300400 if (map->table[i].key != NULL) {
401 mp_obj_print(map->table[i].key, PRINT_REPR);
402 } else {
403 printf("(nil)");
404 }
405 printf(": %p\n", map->table[i].value);
406 }
407 printf("---\n");
408}
409#endif