2013-12-17 13:27:24 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
#include "obj.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "runtime0.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
|
|
|
|
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
|
2014-01-06 12:38:47 -05:00
|
|
|
// prefixed with zero for the empty case.
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC int 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 13:27:24 -05:00
|
|
|
|
2014-03-30 08:54:02 -04:00
|
|
|
STATIC int get_doubling_prime_greater_or_equal_to(int x) {
|
2013-12-17 13:27:24 -05:00
|
|
|
for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
|
|
|
|
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 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* map */
|
|
|
|
|
2014-01-08 12:33:12 -05:00
|
|
|
void mp_map_init(mp_map_t *map, int n) {
|
2014-02-08 13:47:46 -05:00
|
|
|
if (n == 0) {
|
|
|
|
map->alloc = 0;
|
|
|
|
map->table = NULL;
|
|
|
|
} else {
|
|
|
|
map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
|
|
|
|
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;
|
2014-02-08 13:47:46 -05:00
|
|
|
map->table_is_fixed_array = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
|
|
|
|
map->alloc = n;
|
|
|
|
map->used = n;
|
|
|
|
map->all_keys_are_qstrs = 1;
|
|
|
|
map->table_is_fixed_array = 1;
|
|
|
|
map->table = (mp_map_elem_t*)table;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-01-08 12:33:12 -05:00
|
|
|
mp_map_t *mp_map_new(int n) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_map_t *map = m_new(mp_map_t, 1);
|
2014-01-08 12:33:12 -05:00
|
|
|
mp_map_init(map, n);
|
2013-12-17 13:27:24 -05:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:02:20 -05:00
|
|
|
// Differentiate from mp_map_clear() - semantics is different
|
|
|
|
void mp_map_deinit(mp_map_t *map) {
|
2014-02-08 13:47:46 -05:00
|
|
|
if (!map->table_is_fixed_array) {
|
|
|
|
m_del(mp_map_elem_t, map->table, map->alloc);
|
|
|
|
}
|
2014-01-24 17:02:20 -05: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 12:38:47 -05:00
|
|
|
void mp_map_clear(mp_map_t *map) {
|
2014-02-08 13:47:46 -05:00
|
|
|
if (!map->table_is_fixed_array) {
|
|
|
|
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;
|
2014-02-08 13:47:46 -05:00
|
|
|
map->table_is_fixed_array = 0;
|
|
|
|
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) {
|
2014-01-06 12:38:47 -05:00
|
|
|
int old_alloc = map->alloc;
|
|
|
|
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 12:33:12 -05:00
|
|
|
map->all_keys_are_qstrs = 1;
|
2014-01-06 12:38:47 -05:00
|
|
|
map->table = m_new0(mp_map_elem_t, map->alloc);
|
|
|
|
for (int 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-01-08 12:33:12 -05:00
|
|
|
mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
|
2014-02-08 13:47:46 -05:00
|
|
|
// if the map is a fixed array then we must do a brute force linear search
|
|
|
|
if (map->table_is_fixed_array) {
|
|
|
|
if (lookup_kind != MP_MAP_LOOKUP) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
|
|
|
|
if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// map is a hash table (not a fixed array), so do a hash lookup
|
|
|
|
|
2014-01-06 12:38:47 -05:00
|
|
|
if (map->alloc == 0) {
|
2014-02-08 13:47:46 -05: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
|
|
|
|
2014-04-05 12:17:19 -04:00
|
|
|
machine_uint_t hash = mp_obj_hash(index);
|
2013-12-17 13:27:24 -05:00
|
|
|
uint pos = hash % map->alloc;
|
2014-04-05 12:17:19 -04:00
|
|
|
uint start_pos = pos;
|
|
|
|
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
|
2014-02-08 13:47:46 -05: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;
|
|
|
|
}
|
|
|
|
slot->key = index;
|
|
|
|
slot->value = MP_OBJ_NULL;
|
|
|
|
if (!MP_OBJ_IS_QSTR(index)) {
|
|
|
|
map->all_keys_are_qstrs = 0;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-04-05 12:17:19 -04:00
|
|
|
return slot;
|
|
|
|
} else {
|
|
|
|
return MP_OBJ_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-05 12:17:19 -04:00
|
|
|
} else if (slot->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(slot->key, index))) {
|
|
|
|
// found index
|
|
|
|
// Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
|
2014-02-08 13:47:46 -05:00
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
2014-01-08 12:33:12 -05:00
|
|
|
// this leaks this memory (but see dict_get_helper)
|
2014-01-06 14:48:34 -05:00
|
|
|
mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
|
2014-04-05 12:17:19 -04:00
|
|
|
retval->key = slot->key;
|
|
|
|
retval->value = slot->value;
|
|
|
|
// 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-01-06 14:48:34 -05:00
|
|
|
return retval;
|
|
|
|
}
|
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
|
|
|
|
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
|
|
|
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 {
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set */
|
|
|
|
|
|
|
|
void mp_set_init(mp_set_t *set, int n) {
|
|
|
|
set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
|
|
|
|
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) {
|
2014-01-12 10:44:26 -05:00
|
|
|
int old_alloc = set->alloc;
|
|
|
|
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);
|
|
|
|
for (int 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) {
|
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 {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 12:17:19 -04:00
|
|
|
machine_uint_t hash = mp_obj_hash(index);
|
|
|
|
uint pos = hash % set->alloc;
|
|
|
|
uint start_pos = pos;
|
|
|
|
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) {
|
|
|
|
for (uint pos = 0; pos < set->alloc; pos++) {
|
|
|
|
if (set->table[pos] != MP_OBJ_NULL && set->table[pos] != MP_OBJ_SENTINEL) {
|
|
|
|
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
|
|
|
|
|
|
|
#if DEBUG_PRINT
|
|
|
|
void mp_map_dump(mp_map_t *map) {
|
|
|
|
for (int i = 0; i < map->alloc; i++) {
|
|
|
|
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
|