2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-08-30 08:23:35 -04:00
|
|
|
#include <stdint.h>
|
2013-12-17 13:27:24 -05:00
|
|
|
#include <stdlib.h>
|
2015-03-17 19:25:04 -04:00
|
|
|
#include <string.h>
|
2014-04-05 16:53:54 -04:00
|
|
|
#include <assert.h>
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/misc.h"
|
2015-05-11 08:25:19 -04:00
|
|
|
#include "py/runtime.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2017-12-09 10:30:55 -05:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_PRINT (0)
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2021-08-18 00:52:48 -04:00
|
|
|
#if MICROPY_OPT_MAP_LOOKUP_CACHE
|
|
|
|
// MP_STATE_VM(map_lookup_cache) provides a cache of index to the last known
|
|
|
|
// position of that index in any map. On a cache hit, this allows
|
|
|
|
// short-circuiting the full linear search in the case of an ordered map
|
|
|
|
// (i.e. all builtin modules and objects' locals dicts), and computation of
|
|
|
|
// the hash (and potentially some linear probing) in the case of a regular
|
|
|
|
// map. Note the same cache is shared across all maps.
|
|
|
|
|
|
|
|
// Gets the index into the cache for this index. Shift down by two to remove
|
|
|
|
// mp_obj_t tag bits.
|
|
|
|
#define MAP_CACHE_OFFSET(index) ((((uintptr_t)(index)) >> 2) % MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE)
|
|
|
|
// Gets the map cache entry for the corresponding index.
|
|
|
|
#define MAP_CACHE_ENTRY(index) (MP_STATE_VM(map_lookup_cache)[MAP_CACHE_OFFSET(index)])
|
|
|
|
// Retrieve the mp_obj_t at the location suggested by the cache.
|
|
|
|
#define MAP_CACHE_GET(map, index) (&(map)->table[MAP_CACHE_ENTRY(index) % (map)->alloc])
|
|
|
|
// Update the cache for this index.
|
|
|
|
#define MAP_CACHE_SET(index, pos) MAP_CACHE_ENTRY(index) = (pos) & 0xff;
|
|
|
|
#else
|
|
|
|
#define MAP_CACHE_SET(index, pos)
|
|
|
|
#endif
|
|
|
|
|
2016-04-15 11:24:46 -04:00
|
|
|
// This table of sizes is used to control the growth of hash tables.
|
|
|
|
// The first set of sizes are chosen so the allocation fits exactly in a
|
|
|
|
// 4-word GC block, and it's not so important for these small values to be
|
|
|
|
// prime. The latter sizes are prime and increase at an increasing rate.
|
2016-05-20 07:38:15 -04:00
|
|
|
STATIC const uint16_t hash_allocation_sizes[] = {
|
2016-04-15 11:24:46 -04:00
|
|
|
0, 2, 4, 6, 8, 10, 12, // +2
|
|
|
|
17, 23, 29, 37, 47, 59, 73, // *1.25
|
|
|
|
97, 127, 167, 223, 293, 389, 521, 691, 919, 1223, 1627, 2161, // *1.33
|
|
|
|
3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
|
|
|
|
};
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2017-02-07 19:00:15 -05:00
|
|
|
STATIC size_t get_hash_alloc_greater_or_equal_to(size_t x) {
|
2016-04-15 11:24:46 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
|
|
|
|
if (hash_allocation_sizes[i] >= x) {
|
|
|
|
return hash_allocation_sizes[i];
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// ran out of primes in the table!
|
|
|
|
// return something sensible, at least make it odd
|
2016-04-15 11:24:46 -04:00
|
|
|
return (x + x / 2) | 1;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* map */
|
|
|
|
|
2017-02-07 19:00:15 -05:00
|
|
|
void mp_map_init(mp_map_t *map, size_t n) {
|
2014-02-08 13:47:46 -05:00
|
|
|
if (n == 0) {
|
|
|
|
map->alloc = 0;
|
|
|
|
map->table = NULL;
|
|
|
|
} else {
|
2014-04-06 14:00:58 -04:00
|
|
|
map->alloc = n;
|
2014-02-08 13:47:46 -05:00
|
|
|
map->table = m_new0(mp_map_elem_t, map->alloc);
|
|
|
|
}
|
2014-01-08 12:33:12 -05:00
|
|
|
map->used = 0;
|
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 19:25:04 -04:00
|
|
|
map->is_fixed = 0;
|
|
|
|
map->is_ordered = 0;
|
2014-02-08 13:47:46 -05:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:00:15 -05:00
|
|
|
void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
|
2014-02-08 13:47:46 -05:00
|
|
|
map->alloc = n;
|
|
|
|
map->used = n;
|
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 19:25:04 -04:00
|
|
|
map->is_fixed = 1;
|
|
|
|
map->is_ordered = 1;
|
2020-02-26 23:36:53 -05:00
|
|
|
map->table = (mp_map_elem_t *)table;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:02:20 -05:00
|
|
|
// Differentiate from mp_map_clear() - semantics is different
|
|
|
|
void mp_map_deinit(mp_map_t *map) {
|
2015-03-17 19:25:04 -04:00
|
|
|
if (!map->is_fixed) {
|
2014-02-08 13:47:46 -05:00
|
|
|
m_del(mp_map_elem_t, map->table, map->alloc);
|
|
|
|
}
|
2014-01-24 17:02:20 -05:00
|
|
|
map->used = map->alloc = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-06 12:38:47 -05:00
|
|
|
void mp_map_clear(mp_map_t *map) {
|
2015-03-17 19:25:04 -04:00
|
|
|
if (!map->is_fixed) {
|
2014-02-08 13:47:46 -05:00
|
|
|
m_del(mp_map_elem_t, map->table, map->alloc);
|
|
|
|
}
|
|
|
|
map->alloc = 0;
|
2014-01-06 12:38:47 -05:00
|
|
|
map->used = 0;
|
2014-01-08 12:33:12 -05:00
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 19:25:04 -04:00
|
|
|
map->is_fixed = 0;
|
2014-02-08 13:47:46 -05:00
|
|
|
map->table = NULL;
|
2014-01-06 12:38:47 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void mp_map_rehash(mp_map_t *map) {
|
2017-02-07 19:00:15 -05:00
|
|
|
size_t old_alloc = map->alloc;
|
|
|
|
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
|
2017-12-09 10:30:55 -05:00
|
|
|
DEBUG_printf("mp_map_rehash(%p): " UINT_FMT " -> " UINT_FMT "\n", map, old_alloc, new_alloc);
|
2014-01-06 12:38:47 -05:00
|
|
|
mp_map_elem_t *old_table = map->table;
|
2016-04-01 05:51:40 -04:00
|
|
|
mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
|
|
|
|
// If we reach this point, table resizing succeeded, now we can edit the old map.
|
|
|
|
map->alloc = new_alloc;
|
2014-01-06 12:38:47 -05:00
|
|
|
map->used = 0;
|
2014-01-08 12:33:12 -05:00
|
|
|
map->all_keys_are_qstrs = 1;
|
2016-04-01 05:51:40 -04:00
|
|
|
map->table = new_table;
|
2017-02-07 19:00:15 -05:00
|
|
|
for (size_t i = 0; i < old_alloc; i++) {
|
2014-04-05 12:17:19 -04:00
|
|
|
if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
|
2014-01-08 12:33:12 -05:00
|
|
|
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
|
2014-01-06 12:38:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(mp_map_elem_t, old_table, old_alloc);
|
|
|
|
}
|
|
|
|
|
2014-04-05 18:33:12 -04:00
|
|
|
// MP_MAP_LOOKUP behaviour:
|
|
|
|
// - returns NULL if not found, else the slot it was found in with key,value non-null
|
|
|
|
// MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
|
|
|
|
// - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
|
|
|
|
// MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
|
|
|
|
// - returns NULL if not found, else the slot if was found in with key null and value non-null
|
2021-09-23 22:49:51 -04:00
|
|
|
mp_map_elem_t *MICROPY_WRAP_MP_MAP_LOOKUP(mp_map_lookup)(mp_map_t * map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
|
2017-08-31 02:45:02 -04:00
|
|
|
// If the map is a fixed array then we must only be called for a lookup
|
|
|
|
assert(!map->is_fixed || lookup_kind == MP_MAP_LOOKUP);
|
2015-12-30 19:19:28 -05:00
|
|
|
|
2021-08-18 00:52:48 -04:00
|
|
|
#if MICROPY_OPT_MAP_LOOKUP_CACHE
|
|
|
|
// Try the cache for lookup or add-if-not-found.
|
|
|
|
if (lookup_kind != MP_MAP_LOOKUP_REMOVE_IF_FOUND && map->alloc) {
|
|
|
|
mp_map_elem_t *slot = MAP_CACHE_GET(map, index);
|
|
|
|
// Note: Just comparing key for value equality will have false negatives, but
|
|
|
|
// these will be handled by the regular path below.
|
|
|
|
if (slot->key == index) {
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 07:11:57 -04:00
|
|
|
// Work out if we can compare just pointers
|
|
|
|
bool compare_only_ptrs = map->all_keys_are_qstrs;
|
|
|
|
if (compare_only_ptrs) {
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_qstr(index)) {
|
2014-04-28 07:11:57 -04:00
|
|
|
// Index is a qstr, so can just do ptr comparison.
|
py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3a464862c introduced a regression by breaking existing
users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated
helpers like mp_obj_is_int()) have some specific nuances, and mistakes like
this one can happen again.
This commit adds mp_obj_is_exact_type() which behaves like the the old
mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it
attempts to statically assert that it's not called with types which should
be checked using mp_obj_is_type(). If called with any of these types: int,
str, bool, NoneType - it will cause a compilation error. Additional
checked types (e.g function types) can be added in the future.
Existing users of mp_obj_is_type() with the now "invalid" types, were
translated to use mp_obj_is_exact_type().
The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other
compilers) can't statically check conditions that are only known during
link-time (like variables' addresses comparison). However, in this case,
GCC is able to statically detect these conditions, probably because it's
the exact same object - `&mp_type_int == &mp_type_int` is detected.
Misuses of this function with runtime-chosen types (e.g:
`mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC
is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC().
Compiling with this commit and without the fix for d96cfd13e3a464862c shows
that it detects the problem.
Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2020-01-22 07:34:19 -05:00
|
|
|
} else if (mp_obj_is_exact_type(index, &mp_type_str)) {
|
2014-04-28 07:11:57 -04:00
|
|
|
// Index is a non-interned string.
|
|
|
|
// We can either intern the string, or force a full equality comparison.
|
|
|
|
// We chose the latter, since interning costs time and potentially RAM,
|
|
|
|
// and it won't necessarily benefit subsequent calls because these calls
|
|
|
|
// most likely won't pass the newly-interned string.
|
|
|
|
compare_only_ptrs = false;
|
2015-03-20 13:41:37 -04:00
|
|
|
} else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-28 07:11:57 -04:00
|
|
|
// If we are not adding, then we can return straight away a failed
|
|
|
|
// lookup because we know that the index will never be found.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 19:25:04 -04:00
|
|
|
// if the map is an ordered array then we must do a brute force linear search
|
|
|
|
if (map->is_ordered) {
|
2014-02-08 13:47:46 -05:00
|
|
|
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
|
2014-04-28 07:11:57 -04:00
|
|
|
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
|
2017-12-18 21:37:15 -05:00
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
2015-03-17 19:25:04 -04:00
|
|
|
if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
|
2017-03-02 19:21:19 -05:00
|
|
|
// remove the found element by moving the rest of the array down
|
|
|
|
mp_obj_t value = elem->value;
|
|
|
|
--map->used;
|
|
|
|
memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
|
|
|
|
// put the found element after the end so the caller can access it if needed
|
2020-04-22 11:10:30 -04:00
|
|
|
// note: caller must NULL the value so the GC can clean up (e.g. see dict_get_helper).
|
2017-03-02 19:21:19 -05:00
|
|
|
elem = &map->table[map->used];
|
|
|
|
elem->key = MP_OBJ_NULL;
|
|
|
|
elem->value = value;
|
2015-03-17 19:25:04 -04:00
|
|
|
}
|
2017-12-18 21:37:15 -05:00
|
|
|
#endif
|
2021-08-18 00:52:48 -04:00
|
|
|
MAP_CACHE_SET(index, elem - map->table);
|
2014-02-08 13:47:46 -05:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 21:37:15 -05:00
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
2015-03-17 19:25:04 -04:00
|
|
|
if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (map->used == map->alloc) {
|
|
|
|
// TODO: Alloc policy
|
|
|
|
map->alloc += 4;
|
|
|
|
map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
|
|
|
|
mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
|
|
|
|
}
|
|
|
|
mp_map_elem_t *elem = map->table + map->used++;
|
|
|
|
elem->key = index;
|
2020-06-08 22:34:58 -04:00
|
|
|
elem->value = MP_OBJ_NULL;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (!mp_obj_is_qstr(index)) {
|
2015-03-17 19:25:04 -04:00
|
|
|
map->all_keys_are_qstrs = 0;
|
|
|
|
}
|
|
|
|
return elem;
|
2017-12-18 21:37:15 -05:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2014-02-08 13:47:46 -05:00
|
|
|
}
|
|
|
|
|
2015-03-17 19:25:04 -04:00
|
|
|
// map is a hash table (not an ordered array), so do a hash lookup
|
2014-02-08 13:47:46 -05:00
|
|
|
|
2014-01-06 12:38:47 -05:00
|
|
|
if (map->alloc == 0) {
|
2015-03-20 13:41:37 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-01-06 12:38:47 -05:00
|
|
|
mp_map_rehash(map);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 13:47:46 -05:00
|
|
|
|
2015-12-26 16:15:47 -05:00
|
|
|
// get hash of index, with fast path for common case of qstr
|
|
|
|
mp_uint_t hash;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_qstr(index)) {
|
2015-12-26 16:15:47 -05:00
|
|
|
hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
|
|
|
|
} else {
|
|
|
|
hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:00:15 -05:00
|
|
|
size_t pos = hash % map->alloc;
|
|
|
|
size_t start_pos = pos;
|
2014-04-05 12:17:19 -04:00
|
|
|
mp_map_elem_t *avail_slot = NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
for (;;) {
|
2014-04-05 12:17:19 -04:00
|
|
|
mp_map_elem_t *slot = &map->table[pos];
|
|
|
|
if (slot->key == MP_OBJ_NULL) {
|
|
|
|
// found NULL slot, so index is not in table
|
2015-03-20 13:41:37 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 12:17:19 -04:00
|
|
|
map->used += 1;
|
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = slot;
|
|
|
|
}
|
2015-11-18 20:27:28 -05:00
|
|
|
avail_slot->key = index;
|
|
|
|
avail_slot->value = MP_OBJ_NULL;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (!mp_obj_is_qstr(index)) {
|
2014-04-05 12:17:19 -04:00
|
|
|
map->all_keys_are_qstrs = 0;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2015-11-18 20:27:28 -05:00
|
|
|
return avail_slot;
|
2014-04-05 12:17:19 -04:00
|
|
|
} else {
|
2014-04-05 18:33:12 -04:00
|
|
|
return NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-05 12:17:19 -04:00
|
|
|
} else if (slot->key == MP_OBJ_SENTINEL) {
|
|
|
|
// found deleted slot, remember for later
|
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = slot;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-28 07:11:57 -04:00
|
|
|
} else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
|
2014-04-05 12:17:19 -04:00
|
|
|
// found index
|
|
|
|
// Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
|
2015-03-20 13:41:37 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
2014-04-05 12:17:19 -04:00
|
|
|
// delete element in this slot
|
|
|
|
map->used--;
|
|
|
|
if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
|
|
|
|
// optimisation if next slot is empty
|
|
|
|
slot->key = MP_OBJ_NULL;
|
|
|
|
} else {
|
|
|
|
slot->key = MP_OBJ_SENTINEL;
|
|
|
|
}
|
2014-04-05 18:33:12 -04:00
|
|
|
// keep slot->value so that caller can access it if needed
|
2014-01-06 14:48:34 -05:00
|
|
|
}
|
2021-08-18 00:52:48 -04:00
|
|
|
MAP_CACHE_SET(index, pos);
|
2014-04-05 12:17:19 -04:00
|
|
|
return slot;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-04 21:17:17 -04:00
|
|
|
|
|
|
|
// not yet found, keep searching in this table
|
|
|
|
pos = (pos + 1) % map->alloc;
|
2014-04-05 12:17:19 -04:00
|
|
|
|
|
|
|
if (pos == start_pos) {
|
|
|
|
// search got back to starting position, so index is not in table
|
2015-03-20 13:41:37 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 12:17:19 -04:00
|
|
|
if (avail_slot != NULL) {
|
|
|
|
// there was an available slot, so use that
|
|
|
|
map->used++;
|
|
|
|
avail_slot->key = index;
|
|
|
|
avail_slot->value = MP_OBJ_NULL;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (!mp_obj_is_qstr(index)) {
|
2014-04-05 12:17:19 -04:00
|
|
|
map->all_keys_are_qstrs = 0;
|
|
|
|
}
|
|
|
|
return avail_slot;
|
|
|
|
} else {
|
|
|
|
// not enough room in table, rehash it
|
|
|
|
mp_map_rehash(map);
|
|
|
|
// restart the search for the new element
|
|
|
|
start_pos = pos = hash % map->alloc;
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-05 18:33:12 -04:00
|
|
|
return NULL;
|
2014-04-05 12:17:19 -04:00
|
|
|
}
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set */
|
|
|
|
|
2014-12-27 12:07:16 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_SET
|
|
|
|
|
2017-02-07 19:00:15 -05:00
|
|
|
void mp_set_init(mp_set_t *set, size_t n) {
|
2014-04-06 20:16:17 -04:00
|
|
|
set->alloc = n;
|
2013-12-21 13:17:45 -05:00
|
|
|
set->used = 0;
|
|
|
|
set->table = m_new0(mp_obj_t, set->alloc);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void mp_set_rehash(mp_set_t *set) {
|
2017-02-07 19:00:15 -05:00
|
|
|
size_t old_alloc = set->alloc;
|
2014-01-12 10:44:26 -05:00
|
|
|
mp_obj_t *old_table = set->table;
|
2016-04-15 11:24:46 -04:00
|
|
|
set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
|
2014-01-12 10:44:26 -05:00
|
|
|
set->used = 0;
|
|
|
|
set->table = m_new0(mp_obj_t, set->alloc);
|
2017-02-07 19:00:15 -05:00
|
|
|
for (size_t i = 0; i < old_alloc; i++) {
|
2014-04-05 12:17:19 -04:00
|
|
|
if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
|
|
|
|
mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2014-01-12 10:44:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(mp_obj_t, old_table, old_alloc);
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:39:39 -05:00
|
|
|
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
|
2015-03-20 13:41:37 -04:00
|
|
|
// Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
|
|
|
|
// is handled by using bitwise operations.
|
|
|
|
|
2014-01-12 10:44:26 -05:00
|
|
|
if (set->alloc == 0) {
|
2014-01-12 13:23:36 -05:00
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-01-12 10:44:26 -05:00
|
|
|
mp_set_rehash(set);
|
|
|
|
} else {
|
2015-11-20 09:09:20 -05:00
|
|
|
return MP_OBJ_NULL;
|
2014-01-12 10:44:26 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-11 08:25:19 -04:00
|
|
|
mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
|
2017-02-07 19:00:15 -05:00
|
|
|
size_t pos = hash % set->alloc;
|
|
|
|
size_t start_pos = pos;
|
2014-04-05 12:17:19 -04:00
|
|
|
mp_obj_t *avail_slot = NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
for (;;) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_t elem = set->table[pos];
|
|
|
|
if (elem == MP_OBJ_NULL) {
|
2014-04-05 12:17:19 -04:00
|
|
|
// found NULL slot, so index is not in table
|
2014-01-12 13:23:36 -05:00
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 12:17:19 -04:00
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = &set->table[pos];
|
|
|
|
}
|
|
|
|
set->used++;
|
|
|
|
*avail_slot = index;
|
|
|
|
return index;
|
|
|
|
} else {
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
} else if (elem == MP_OBJ_SENTINEL) {
|
|
|
|
// found deleted slot, remember for later
|
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = &set->table[pos];
|
|
|
|
}
|
|
|
|
} else if (mp_obj_equal(elem, index)) {
|
|
|
|
// found index
|
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
|
|
|
// delete element
|
|
|
|
set->used--;
|
|
|
|
if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
|
|
|
|
// optimisation if next slot is empty
|
|
|
|
set->table[pos] = MP_OBJ_NULL;
|
|
|
|
} else {
|
|
|
|
set->table[pos] = MP_OBJ_SENTINEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not yet found, keep searching in this table
|
|
|
|
pos = (pos + 1) % set->alloc;
|
|
|
|
|
|
|
|
if (pos == start_pos) {
|
|
|
|
// search got back to starting position, so index is not in table
|
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
|
|
|
if (avail_slot != NULL) {
|
|
|
|
// there was an available slot, so use that
|
|
|
|
set->used++;
|
|
|
|
*avail_slot = index;
|
|
|
|
return index;
|
|
|
|
} else {
|
2013-12-17 13:27:24 -05:00
|
|
|
// not enough room in table, rehash it
|
2014-01-12 10:44:26 -05:00
|
|
|
mp_set_rehash(set);
|
2013-12-17 13:27:24 -05:00
|
|
|
// restart the search for the new element
|
2014-04-05 12:17:19 -04:00
|
|
|
start_pos = pos = hash % set->alloc;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-21 13:17:45 -05:00
|
|
|
return MP_OBJ_NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-05 12:17:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_set_remove_first(mp_set_t *set) {
|
2017-02-07 19:00:15 -05:00
|
|
|
for (size_t pos = 0; pos < set->alloc; pos++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_set_slot_is_filled(set, pos)) {
|
2014-04-05 12:17:19 -04:00
|
|
|
mp_obj_t elem = set->table[pos];
|
|
|
|
// delete element
|
|
|
|
set->used--;
|
|
|
|
if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
|
|
|
|
// optimisation if next slot is empty
|
|
|
|
set->table[pos] = MP_OBJ_NULL;
|
|
|
|
} else {
|
|
|
|
set->table[pos] = MP_OBJ_SENTINEL;
|
2014-01-12 11:39:39 -05:00
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 12:17:19 -04:00
|
|
|
return MP_OBJ_NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-01-12 10:44:26 -05:00
|
|
|
|
|
|
|
void mp_set_clear(mp_set_t *set) {
|
2014-02-08 13:47:46 -05:00
|
|
|
m_del(mp_obj_t, set->table, set->alloc);
|
2014-01-12 10:44:26 -05:00
|
|
|
set->alloc = 0;
|
2014-02-08 13:47:46 -05:00
|
|
|
set->used = 0;
|
|
|
|
set->table = NULL;
|
2014-01-12 10:44:26 -05:00
|
|
|
}
|
2014-04-04 21:14:22 -04:00
|
|
|
|
2014-12-27 12:07:16 -05:00
|
|
|
#endif // MICROPY_PY_BUILTINS_SET
|
|
|
|
|
2014-11-05 16:16:41 -05:00
|
|
|
#if defined(DEBUG_PRINT) && DEBUG_PRINT
|
2014-04-04 21:14:22 -04:00
|
|
|
void mp_map_dump(mp_map_t *map) {
|
2017-02-07 19:00:15 -05:00
|
|
|
for (size_t i = 0; i < map->alloc; i++) {
|
2018-08-02 00:17:24 -04:00
|
|
|
if (map->table[i].key != MP_OBJ_NULL) {
|
2014-04-04 21:14:22 -04:00
|
|
|
mp_obj_print(map->table[i].key, PRINT_REPR);
|
|
|
|
} else {
|
2018-08-02 00:17:24 -04:00
|
|
|
DEBUG_printf("(nil)");
|
2014-04-04 21:14:22 -04:00
|
|
|
}
|
2018-08-02 00:17:24 -04:00
|
|
|
DEBUG_printf(": %p\n", map->table[i].value);
|
2014-04-04 21:14:22 -04:00
|
|
|
}
|
2018-08-02 00:17:24 -04:00
|
|
|
DEBUG_printf("---\n");
|
2014-04-04 21:14:22 -04:00
|
|
|
}
|
|
|
|
#endif
|