blob: c88e6e56e3b8f5b5d2573196167cc2d957beff5d [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
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/mpconfig.h"
32#include "py/misc.h"
33#include "py/obj.h"
Damien660365e2013-12-17 18:27:24 +000034
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +020035// Fixed empty map. Useful when need to call kw-receiving functions
36// without any keywords from C, etc.
37const mp_map_t mp_const_empty_map = {
38 .all_keys_are_qstrs = 0,
39 .table_is_fixed_array = 1,
40 .used = 0,
41 .alloc = 0,
42 .table = NULL,
43};
44
Damien660365e2013-12-17 18:27:24 +000045// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton4ce6cea2014-01-06 17:38:47 +000046// prefixed with zero for the empty case.
Damien George93965e72014-08-30 13:23:35 +010047STATIC 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 +000048
Damien George93965e72014-08-30 13:23:35 +010049STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
50 for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
Damien660365e2013-12-17 18:27:24 +000051 if (doubling_primes[i] >= x) {
52 return doubling_primes[i];
53 }
54 }
55 // ran out of primes in the table!
56 // return something sensible, at least make it odd
57 return x | 1;
58}
59
Damiend99b0522013-12-21 18:17:45 +000060/******************************************************************************/
61/* map */
62
Damien George93965e72014-08-30 13:23:35 +010063void mp_map_init(mp_map_t *map, mp_uint_t n) {
Damien George9a58d762014-02-08 18:47:46 +000064 if (n == 0) {
65 map->alloc = 0;
66 map->table = NULL;
67 } else {
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +030068 map->alloc = n;
Damien George9a58d762014-02-08 18:47:46 +000069 map->table = m_new0(mp_map_elem_t, map->alloc);
70 }
Damien George38a2da62014-01-08 17:33:12 +000071 map->used = 0;
72 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +000073 map->table_is_fixed_array = 0;
74}
75
Damien George93965e72014-08-30 13:23:35 +010076void 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 +000077 map->alloc = n;
78 map->used = n;
79 map->all_keys_are_qstrs = 1;
80 map->table_is_fixed_array = 1;
81 map->table = (mp_map_elem_t*)table;
Damien660365e2013-12-17 18:27:24 +000082}
83
Damien George93965e72014-08-30 13:23:35 +010084mp_map_t *mp_map_new(mp_uint_t n) {
Damiend99b0522013-12-21 18:17:45 +000085 mp_map_t *map = m_new(mp_map_t, 1);
Damien George38a2da62014-01-08 17:33:12 +000086 mp_map_init(map, n);
Damien660365e2013-12-17 18:27:24 +000087 return map;
88}
89
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020090// Differentiate from mp_map_clear() - semantics is different
91void mp_map_deinit(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +000092 if (!map->table_is_fixed_array) {
93 m_del(mp_map_elem_t, map->table, map->alloc);
94 }
Paul Sokolovsky9a24a042014-01-25 00:02:20 +020095 map->used = map->alloc = 0;
96}
97
98void mp_map_free(mp_map_t *map) {
99 mp_map_deinit(map);
100 m_del_obj(mp_map_t, map);
101}
102
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000103void mp_map_clear(mp_map_t *map) {
Damien George9a58d762014-02-08 18:47:46 +0000104 if (!map->table_is_fixed_array) {
105 m_del(mp_map_elem_t, map->table, map->alloc);
106 }
107 map->alloc = 0;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000108 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000109 map->all_keys_are_qstrs = 1;
Damien George9a58d762014-02-08 18:47:46 +0000110 map->table_is_fixed_array = 0;
111 map->table = NULL;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000112}
113
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200114STATIC void mp_map_rehash(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100115 mp_uint_t old_alloc = map->alloc;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000116 mp_map_elem_t *old_table = map->table;
117 map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
118 map->used = 0;
Damien George38a2da62014-01-08 17:33:12 +0000119 map->all_keys_are_qstrs = 1;
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000120 map->table = m_new0(mp_map_elem_t, map->alloc);
Damien George93965e72014-08-30 13:23:35 +0100121 for (mp_uint_t i = 0; i < old_alloc; i++) {
Damien George95004e52014-04-05 17:17:19 +0100122 if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
Damien George38a2da62014-01-08 17:33:12 +0000123 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 +0000124 }
125 }
126 m_del(mp_map_elem_t, old_table, old_alloc);
127}
128
Damien Georged0e82432014-04-05 23:33:12 +0100129// MP_MAP_LOOKUP behaviour:
130// - returns NULL if not found, else the slot it was found in with key,value non-null
131// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
132// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
133// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
134// - 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 +0000135mp_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 +0100136
137 // Work out if we can compare just pointers
138 bool compare_only_ptrs = map->all_keys_are_qstrs;
139 if (compare_only_ptrs) {
140 if (MP_OBJ_IS_QSTR(index)) {
141 // Index is a qstr, so can just do ptr comparison.
142 } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
143 // Index is a non-interned string.
144 // We can either intern the string, or force a full equality comparison.
145 // We chose the latter, since interning costs time and potentially RAM,
146 // and it won't necessarily benefit subsequent calls because these calls
147 // most likely won't pass the newly-interned string.
148 compare_only_ptrs = false;
149 } else if (!(lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
150 // If we are not adding, then we can return straight away a failed
151 // lookup because we know that the index will never be found.
152 return NULL;
153 }
154 }
155
Damien George9a58d762014-02-08 18:47:46 +0000156 // if the map is a fixed array then we must do a brute force linear search
157 if (map->table_is_fixed_array) {
158 if (lookup_kind != MP_MAP_LOOKUP) {
159 return NULL;
160 }
161 for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Damien George186e4632014-04-28 12:11:57 +0100162 if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
Damien George9a58d762014-02-08 18:47:46 +0000163 return elem;
164 }
165 }
166 return NULL;
167 }
168
169 // map is a hash table (not a fixed array), so do a hash lookup
170
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000171 if (map->alloc == 0) {
Damien George9a58d762014-02-08 18:47:46 +0000172 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000173 mp_map_rehash(map);
174 } else {
175 return NULL;
176 }
177 }
Damien George9a58d762014-02-08 18:47:46 +0000178
Damien George40f3c022014-07-03 13:25:24 +0100179 mp_uint_t hash = mp_obj_hash(index);
Damien George93965e72014-08-30 13:23:35 +0100180 mp_uint_t pos = hash % map->alloc;
181 mp_uint_t start_pos = pos;
Damien George95004e52014-04-05 17:17:19 +0100182 mp_map_elem_t *avail_slot = NULL;
Damien660365e2013-12-17 18:27:24 +0000183 for (;;) {
Damien George95004e52014-04-05 17:17:19 +0100184 mp_map_elem_t *slot = &map->table[pos];
185 if (slot->key == MP_OBJ_NULL) {
186 // found NULL slot, so index is not in table
Damien George9a58d762014-02-08 18:47:46 +0000187 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100188 map->used += 1;
189 if (avail_slot == NULL) {
190 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000191 }
Damien George95004e52014-04-05 17:17:19 +0100192 slot->key = index;
193 slot->value = MP_OBJ_NULL;
194 if (!MP_OBJ_IS_QSTR(index)) {
195 map->all_keys_are_qstrs = 0;
196 }
197 return slot;
198 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100199 return NULL;
Damien660365e2013-12-17 18:27:24 +0000200 }
Damien George95004e52014-04-05 17:17:19 +0100201 } else if (slot->key == MP_OBJ_SENTINEL) {
202 // found deleted slot, remember for later
203 if (avail_slot == NULL) {
204 avail_slot = slot;
Damien660365e2013-12-17 18:27:24 +0000205 }
Damien George186e4632014-04-28 12:11:57 +0100206 } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
Damien George95004e52014-04-05 17:17:19 +0100207 // found index
208 // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
Damien George9a58d762014-02-08 18:47:46 +0000209 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George95004e52014-04-05 17:17:19 +0100210 // delete element in this slot
211 map->used--;
212 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
213 // optimisation if next slot is empty
214 slot->key = MP_OBJ_NULL;
215 } else {
216 slot->key = MP_OBJ_SENTINEL;
217 }
Damien Georged0e82432014-04-05 23:33:12 +0100218 // keep slot->value so that caller can access it if needed
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000219 }
Damien George95004e52014-04-05 17:17:19 +0100220 return slot;
Damien660365e2013-12-17 18:27:24 +0000221 }
Paul Sokolovsky4a088f42014-04-05 04:17:17 +0300222
223 // not yet found, keep searching in this table
224 pos = (pos + 1) % map->alloc;
Damien George95004e52014-04-05 17:17:19 +0100225
226 if (pos == start_pos) {
227 // search got back to starting position, so index is not in table
228 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
229 if (avail_slot != NULL) {
230 // there was an available slot, so use that
231 map->used++;
232 avail_slot->key = index;
233 avail_slot->value = MP_OBJ_NULL;
234 if (!MP_OBJ_IS_QSTR(index)) {
235 map->all_keys_are_qstrs = 0;
236 }
237 return avail_slot;
238 } else {
239 // not enough room in table, rehash it
240 mp_map_rehash(map);
241 // restart the search for the new element
242 start_pos = pos = hash % map->alloc;
243 }
244 } else {
Damien Georged0e82432014-04-05 23:33:12 +0100245 return NULL;
Damien George95004e52014-04-05 17:17:19 +0100246 }
247 }
Damien660365e2013-12-17 18:27:24 +0000248 }
249}
250
Damiend99b0522013-12-21 18:17:45 +0000251/******************************************************************************/
252/* set */
253
Damien Georgee37dcaa2014-12-27 17:07:16 +0000254#if MICROPY_PY_BUILTINS_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 Georgee37dcaa2014-12-27 17:07:16 +0000371#endif // MICROPY_PY_BUILTINS_SET
372
Damien George7860c2a2014-11-05 21:16:41 +0000373#if defined(DEBUG_PRINT) && DEBUG_PRINT
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300374void mp_map_dump(mp_map_t *map) {
Damien George93965e72014-08-30 13:23:35 +0100375 for (mp_uint_t i = 0; i < map->alloc; i++) {
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300376 if (map->table[i].key != NULL) {
377 mp_obj_print(map->table[i].key, PRINT_REPR);
378 } else {
379 printf("(nil)");
380 }
381 printf(": %p\n", map->table[i].value);
382 }
383 printf("---\n");
384}
385#endif