2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* 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 13:23:35 +01:00
|
|
|
#include <stdint.h>
|
2013-12-17 18:27:24 +00:00
|
|
|
#include <stdlib.h>
|
2015-03-17 23:25:04 +00:00
|
|
|
#include <string.h>
|
2014-04-05 21:53:54 +01:00
|
|
|
#include <assert.h>
|
2013-12-17 18:27:24 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/misc.h"
|
2015-05-11 13:25:19 +01:00
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "py/runtime.h"
|
2013-12-17 18:27:24 +00:00
|
|
|
|
2014-11-26 19:17:16 +00:00
|
|
|
// Fixed empty map. Useful when need to call kw-receiving functions
|
|
|
|
// without any keywords from C, etc.
|
|
|
|
const mp_map_t mp_const_empty_map = {
|
|
|
|
.all_keys_are_qstrs = 0,
|
2015-03-17 23:25:04 +00:00
|
|
|
.is_fixed = 1,
|
|
|
|
.is_ordered = 1,
|
2014-11-26 19:17:16 +00:00
|
|
|
.used = 0,
|
|
|
|
.alloc = 0,
|
|
|
|
.table = NULL,
|
|
|
|
};
|
|
|
|
|
2013-12-17 18:27:24 +00:00
|
|
|
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
|
2014-01-06 17:38:47 +00:00
|
|
|
// prefixed with zero for the empty case.
|
2014-08-30 13:23:35 +01:00
|
|
|
STATIC 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};
|
2013-12-17 18:27:24 +00:00
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
|
2015-01-16 17:47:07 +00:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
|
2013-12-17 18:27:24 +00:00
|
|
|
if (doubling_primes[i] >= x) {
|
|
|
|
return doubling_primes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ran out of primes in the table!
|
|
|
|
// return something sensible, at least make it odd
|
|
|
|
return x | 1;
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* map */
|
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
void mp_map_init(mp_map_t *map, mp_uint_t n) {
|
2014-02-08 18:47:46 +00:00
|
|
|
if (n == 0) {
|
|
|
|
map->alloc = 0;
|
|
|
|
map->table = NULL;
|
|
|
|
} else {
|
2014-04-06 19:00:58 +01:00
|
|
|
map->alloc = n;
|
2014-02-08 18:47:46 +00:00
|
|
|
map->table = m_new0(mp_map_elem_t, map->alloc);
|
|
|
|
}
|
2014-01-08 17:33:12 +00:00
|
|
|
map->used = 0;
|
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 23:25:04 +00:00
|
|
|
map->is_fixed = 0;
|
|
|
|
map->is_ordered = 0;
|
2014-02-08 18:47:46 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table) {
|
2014-02-08 18:47:46 +00:00
|
|
|
map->alloc = n;
|
|
|
|
map->used = n;
|
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 23:25:04 +00:00
|
|
|
map->is_fixed = 1;
|
|
|
|
map->is_ordered = 1;
|
2014-02-08 18:47:46 +00:00
|
|
|
map->table = (mp_map_elem_t*)table;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
mp_map_t *mp_map_new(mp_uint_t n) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_map_t *map = m_new(mp_map_t, 1);
|
2014-01-08 17:33:12 +00:00
|
|
|
mp_map_init(map, n);
|
2013-12-17 18:27:24 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2014-01-24 22:02:20 +00:00
|
|
|
// Differentiate from mp_map_clear() - semantics is different
|
|
|
|
void mp_map_deinit(mp_map_t *map) {
|
2015-03-17 23:25:04 +00:00
|
|
|
if (!map->is_fixed) {
|
2014-02-08 18:47:46 +00:00
|
|
|
m_del(mp_map_elem_t, map->table, map->alloc);
|
|
|
|
}
|
2014-01-24 22:02:20 +00:00
|
|
|
map->used = map->alloc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_map_free(mp_map_t *map) {
|
|
|
|
mp_map_deinit(map);
|
|
|
|
m_del_obj(mp_map_t, map);
|
|
|
|
}
|
|
|
|
|
2014-01-06 17:38:47 +00:00
|
|
|
void mp_map_clear(mp_map_t *map) {
|
2015-03-17 23:25:04 +00:00
|
|
|
if (!map->is_fixed) {
|
2014-02-08 18:47:46 +00:00
|
|
|
m_del(mp_map_elem_t, map->table, map->alloc);
|
|
|
|
}
|
|
|
|
map->alloc = 0;
|
2014-01-06 17:38:47 +00:00
|
|
|
map->used = 0;
|
2014-01-08 17:33:12 +00:00
|
|
|
map->all_keys_are_qstrs = 1;
|
2015-03-17 23:25:04 +00:00
|
|
|
map->is_fixed = 0;
|
2014-02-08 18:47:46 +00:00
|
|
|
map->table = NULL;
|
2014-01-06 17:38:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void mp_map_rehash(mp_map_t *map) {
|
2014-08-30 13:23:35 +01:00
|
|
|
mp_uint_t old_alloc = map->alloc;
|
2014-01-06 17:38:47 +00:00
|
|
|
mp_map_elem_t *old_table = map->table;
|
|
|
|
map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
|
|
|
|
map->used = 0;
|
2014-01-08 17:33:12 +00:00
|
|
|
map->all_keys_are_qstrs = 1;
|
2014-01-06 17:38:47 +00:00
|
|
|
map->table = m_new0(mp_map_elem_t, map->alloc);
|
2014-08-30 13:23:35 +01:00
|
|
|
for (mp_uint_t i = 0; i < old_alloc; i++) {
|
2014-04-05 17:17:19 +01:00
|
|
|
if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
|
2014-01-08 17:33:12 +00:00
|
|
|
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
|
2014-01-06 17:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(mp_map_elem_t, old_table, old_alloc);
|
|
|
|
}
|
|
|
|
|
2014-04-05 23:33:12 +01: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
|
2015-04-04 15:53:11 +01:00
|
|
|
mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
|
2014-04-28 12:11:57 +01:00
|
|
|
|
2015-12-31 00:19:28 +00:00
|
|
|
if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
|
|
|
|
// can't add/remove from a fixed array
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-28 12:11:57 +01:00
|
|
|
// Work out if we can compare just pointers
|
|
|
|
bool compare_only_ptrs = map->all_keys_are_qstrs;
|
|
|
|
if (compare_only_ptrs) {
|
|
|
|
if (MP_OBJ_IS_QSTR(index)) {
|
|
|
|
// Index is a qstr, so can just do ptr comparison.
|
|
|
|
} else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
|
|
|
|
// 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 17:41:37 +00:00
|
|
|
} else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-28 12:11:57 +01: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 23:25:04 +00:00
|
|
|
// if the map is an ordered array then we must do a brute force linear search
|
|
|
|
if (map->is_ordered) {
|
2014-02-08 18:47:46 +00:00
|
|
|
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
|
2014-04-28 12:11:57 +01:00
|
|
|
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
|
2015-03-17 23:25:04 +00:00
|
|
|
if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
|
|
|
|
elem->key = MP_OBJ_SENTINEL;
|
|
|
|
// keep elem->value so that caller can access it if needed
|
|
|
|
}
|
2014-02-08 18:47:46 +00:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 23:25:04 +00:00
|
|
|
if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// TODO shrink array down over any previously-freed slots
|
|
|
|
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;
|
|
|
|
if (!MP_OBJ_IS_QSTR(index)) {
|
|
|
|
map->all_keys_are_qstrs = 0;
|
|
|
|
}
|
|
|
|
return elem;
|
2014-02-08 18:47:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:25:04 +00:00
|
|
|
// map is a hash table (not an ordered array), so do a hash lookup
|
2014-02-08 18:47:46 +00:00
|
|
|
|
2014-01-06 17:38:47 +00:00
|
|
|
if (map->alloc == 0) {
|
2015-03-20 17:41:37 +00:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-01-06 17:38:47 +00:00
|
|
|
mp_map_rehash(map);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 18:47:46 +00:00
|
|
|
|
2015-12-26 21:15:47 +00:00
|
|
|
// get hash of index, with fast path for common case of qstr
|
|
|
|
mp_uint_t hash;
|
|
|
|
if (MP_OBJ_IS_QSTR(index)) {
|
|
|
|
hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
|
|
|
|
} else {
|
|
|
|
hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
|
|
|
|
}
|
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
mp_uint_t pos = hash % map->alloc;
|
|
|
|
mp_uint_t start_pos = pos;
|
2014-04-05 17:17:19 +01:00
|
|
|
mp_map_elem_t *avail_slot = NULL;
|
2013-12-17 18:27:24 +00:00
|
|
|
for (;;) {
|
2014-04-05 17:17:19 +01: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 17:41:37 +00:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 17:17:19 +01:00
|
|
|
map->used += 1;
|
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = slot;
|
|
|
|
}
|
2015-11-19 01:27:28 +00:00
|
|
|
avail_slot->key = index;
|
|
|
|
avail_slot->value = MP_OBJ_NULL;
|
2014-04-05 17:17:19 +01:00
|
|
|
if (!MP_OBJ_IS_QSTR(index)) {
|
|
|
|
map->all_keys_are_qstrs = 0;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2015-11-19 01:27:28 +00:00
|
|
|
return avail_slot;
|
2014-04-05 17:17:19 +01:00
|
|
|
} else {
|
2014-04-05 23:33:12 +01:00
|
|
|
return NULL;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2014-04-05 17:17:19 +01:00
|
|
|
} else if (slot->key == MP_OBJ_SENTINEL) {
|
|
|
|
// found deleted slot, remember for later
|
|
|
|
if (avail_slot == NULL) {
|
|
|
|
avail_slot = slot;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2014-04-28 12:11:57 +01:00
|
|
|
} else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
|
2014-04-05 17:17:19 +01:00
|
|
|
// found index
|
|
|
|
// Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
|
2015-03-20 17:41:37 +00:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
2014-04-05 17:17:19 +01: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 23:33:12 +01:00
|
|
|
// keep slot->value so that caller can access it if needed
|
2014-01-06 19:48:34 +00:00
|
|
|
}
|
2014-04-05 17:17:19 +01:00
|
|
|
return slot;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2014-04-05 02:17:17 +01:00
|
|
|
|
|
|
|
// not yet found, keep searching in this table
|
|
|
|
pos = (pos + 1) % map->alloc;
|
2014-04-05 17:17:19 +01:00
|
|
|
|
|
|
|
if (pos == start_pos) {
|
|
|
|
// search got back to starting position, so index is not in table
|
2015-03-20 17:41:37 +00:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 17:17:19 +01: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;
|
|
|
|
if (!MP_OBJ_IS_QSTR(index)) {
|
|
|
|
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 23:33:12 +01:00
|
|
|
return NULL;
|
2014-04-05 17:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set */
|
|
|
|
|
2014-12-27 17:07:16 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_SET
|
|
|
|
|
2014-08-30 13:23:35 +01:00
|
|
|
void mp_set_init(mp_set_t *set, mp_uint_t n) {
|
2014-04-07 01:16:17 +01:00
|
|
|
set->alloc = n;
|
2013-12-21 18:17:45 +00:00
|
|
|
set->used = 0;
|
|
|
|
set->table = m_new0(mp_obj_t, set->alloc);
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void mp_set_rehash(mp_set_t *set) {
|
2014-08-30 13:23:35 +01:00
|
|
|
mp_uint_t old_alloc = set->alloc;
|
2014-01-12 15:44:26 +00:00
|
|
|
mp_obj_t *old_table = set->table;
|
|
|
|
set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
|
|
|
|
set->used = 0;
|
|
|
|
set->table = m_new0(mp_obj_t, set->alloc);
|
2014-08-30 13:23:35 +01:00
|
|
|
for (mp_uint_t i = 0; i < old_alloc; i++) {
|
2014-04-05 17:17:19 +01: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 15:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(mp_obj_t, old_table, old_alloc);
|
|
|
|
}
|
|
|
|
|
2014-01-12 16:39:39 +00: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 17:41:37 +00: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 15:44:26 +00:00
|
|
|
if (set->alloc == 0) {
|
2014-01-12 18:23:36 +00:00
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-01-12 15:44:26 +00:00
|
|
|
mp_set_rehash(set);
|
|
|
|
} else {
|
2015-11-20 14:09:20 +00:00
|
|
|
return MP_OBJ_NULL;
|
2014-01-12 15:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-11 13:25:19 +01:00
|
|
|
mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
|
2014-08-30 13:23:35 +01:00
|
|
|
mp_uint_t pos = hash % set->alloc;
|
|
|
|
mp_uint_t start_pos = pos;
|
2014-04-05 17:17:19 +01:00
|
|
|
mp_obj_t *avail_slot = NULL;
|
2013-12-17 18:27:24 +00:00
|
|
|
for (;;) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t elem = set->table[pos];
|
|
|
|
if (elem == MP_OBJ_NULL) {
|
2014-04-05 17:17:19 +01:00
|
|
|
// found NULL slot, so index is not in table
|
2014-01-12 18:23:36 +00:00
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
2014-04-05 17:17:19 +01: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 18:27:24 +00:00
|
|
|
// not enough room in table, rehash it
|
2014-01-12 15:44:26 +00:00
|
|
|
mp_set_rehash(set);
|
2013-12-17 18:27:24 +00:00
|
|
|
// restart the search for the new element
|
2014-04-05 17:17:19 +01:00
|
|
|
start_pos = pos = hash % set->alloc;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-21 18:17:45 +00:00
|
|
|
return MP_OBJ_NULL;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2014-04-05 17:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_set_remove_first(mp_set_t *set) {
|
2014-08-30 13:23:35 +01:00
|
|
|
for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
|
2014-04-05 21:53:54 +01:00
|
|
|
if (MP_SET_SLOT_IS_FILLED(set, pos)) {
|
2014-04-05 17:17:19 +01: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 16:39:39 +00:00
|
|
|
}
|
2013-12-17 18:27:24 +00:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 17:17:19 +01:00
|
|
|
return MP_OBJ_NULL;
|
2013-12-17 18:27:24 +00:00
|
|
|
}
|
2014-01-12 15:44:26 +00:00
|
|
|
|
|
|
|
void mp_set_clear(mp_set_t *set) {
|
2014-02-08 18:47:46 +00:00
|
|
|
m_del(mp_obj_t, set->table, set->alloc);
|
2014-01-12 15:44:26 +00:00
|
|
|
set->alloc = 0;
|
2014-02-08 18:47:46 +00:00
|
|
|
set->used = 0;
|
|
|
|
set->table = NULL;
|
2014-01-12 15:44:26 +00:00
|
|
|
}
|
2014-04-05 02:14:22 +01:00
|
|
|
|
2014-12-27 17:07:16 +00:00
|
|
|
#endif // MICROPY_PY_BUILTINS_SET
|
|
|
|
|
2014-11-05 21:16:41 +00:00
|
|
|
#if defined(DEBUG_PRINT) && DEBUG_PRINT
|
2014-04-05 02:14:22 +01:00
|
|
|
void mp_map_dump(mp_map_t *map) {
|
2014-08-30 13:23:35 +01:00
|
|
|
for (mp_uint_t i = 0; i < map->alloc; i++) {
|
2014-04-05 02:14:22 +01:00
|
|
|
if (map->table[i].key != NULL) {
|
|
|
|
mp_obj_print(map->table[i].key, PRINT_REPR);
|
|
|
|
} else {
|
|
|
|
printf("(nil)");
|
|
|
|
}
|
|
|
|
printf(": %p\n", map->table[i].value);
|
|
|
|
}
|
|
|
|
printf("---\n");
|
|
|
|
}
|
|
|
|
#endif
|