From 4ce6ceadcad294a62f6fb2b23da262dc5cad0793 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 6 Jan 2014 17:38:47 +0000 Subject: Added dict.clear. Added 0 to the list of primes. Funky primes, these. --- py/map.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'py/map.c') diff --git a/py/map.c b/py/map.c index 01209c9b74..dff0bdfea0 100644 --- a/py/map.c +++ b/py/map.c @@ -9,7 +9,8 @@ #include "map.h" // approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}] -static int doubling_primes[] = {7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607}; +// prefixed with zero for the empty case. +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}; int get_doubling_prime_greater_or_equal_to(int x) { for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) { @@ -38,6 +39,31 @@ mp_map_t *mp_map_new(mp_map_kind_t kind, int n) { return map; } +void mp_map_clear(mp_map_t *map) { + map->used = 0; + machine_uint_t a = map->alloc; + map->alloc = 0; + map->table = m_renew(mp_map_elem_t, map->table, a, map->alloc); + mp_map_elem_t nul = {NULL, NULL}; + for (uint i=0; ialloc; i++) { + map->table[i] = nul; + } +} + +static void mp_map_rehash (mp_map_t *map) { + 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; + map->table = m_new0(mp_map_elem_t, map->alloc); + for (int i = 0; i < old_alloc; i++) { + if (old_table[i].key != NULL) { + mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value; + } + } + m_del(mp_map_elem_t, old_table, old_alloc); +} + mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found) { bool is_map_mp_obj = (map->kind == MP_MAP_OBJ); machine_uint_t hash; @@ -46,6 +72,13 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n } else { hash = (machine_uint_t)index; } + if (map->alloc == 0) { + if (add_if_not_found) { + mp_map_rehash(map); + } else { + return NULL; + } + } uint pos = hash % map->alloc; for (;;) { mp_map_elem_t *elem = &map->table[pos]; @@ -54,17 +87,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n if (add_if_not_found) { if (map->used + 1 >= map->alloc) { // not enough room in table, rehash it - 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; - map->table = m_new0(mp_map_elem_t, map->alloc); - for (int i = 0; i < old_alloc; i++) { - if (old_table[i].key != NULL) { - mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value; - } - } - m_del(mp_map_elem_t, old_table, old_alloc); + mp_map_rehash(map); // restart the search for the new element pos = hash % map->alloc; } else { @@ -106,6 +129,7 @@ void mp_set_init(mp_set_t *set, int n) { mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) { int hash = mp_obj_hash(index); + assert(set->alloc); /* FIXME: if alloc is ever 0 when doing a lookup, this'll fail: */ int pos = hash % set->alloc; for (;;) { mp_obj_t elem = set->table[pos]; -- cgit v1.2.3 From 0fcbaa442f538c8ffca8a7387f7dc6d280172a5c Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 6 Jan 2014 19:48:34 +0000 Subject: implemented dict.pop --- py/map.c | 15 ++++++++++--- py/map.h | 2 +- py/objdict.c | 51 ++++++++++++++++++++++++++++++++++-------- tests/basics/tests/dict_pop.py | 12 ++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 tests/basics/tests/dict_pop.py (limited to 'py/map.c') diff --git a/py/map.c b/py/map.c index dff0bdfea0..8554150adf 100644 --- a/py/map.c +++ b/py/map.c @@ -58,13 +58,13 @@ static void mp_map_rehash (mp_map_t *map) { map->table = m_new0(mp_map_elem_t, map->alloc); for (int i = 0; i < old_alloc; i++) { if (old_table[i].key != NULL) { - mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value; + mp_map_lookup_helper(map, old_table[i].key, true, false)->value = old_table[i].value; } } m_del(mp_map_elem_t, old_table, old_alloc); } -mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found) { +mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found, bool remove_if_found) { bool is_map_mp_obj = (map->kind == MP_MAP_OBJ); machine_uint_t hash; if (is_map_mp_obj) { @@ -105,6 +105,15 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n elem->key = index; } */ + if (remove_if_found) { + map->used--; + /* this leaks this memory (but see dict_get_helper) */ + mp_map_elem_t *retval = m_new(mp_map_elem_t, 1); + retval->key = elem->key; + retval->value = elem->value; + elem->key = NULL; + return retval; + } return elem; } else { // not yet found, keep searching in this table @@ -115,7 +124,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found) { mp_obj_t o = (mp_obj_t)(machine_uint_t)index; - return mp_map_lookup_helper(map, o, add_if_not_found); + return mp_map_lookup_helper(map, o, add_if_not_found, false); } /******************************************************************************/ diff --git a/py/map.h b/py/map.h index b4592c270a..f0a57758c5 100644 --- a/py/map.h +++ b/py/map.h @@ -26,7 +26,7 @@ typedef struct _mp_set_t { int get_doubling_prime_greater_or_equal_to(int x); void mp_map_init(mp_map_t *map, mp_map_kind_t kind, int n); mp_map_t *mp_map_new(mp_map_kind_t kind, int n); -mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found); +mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found, bool remove_if_found); mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found); void mp_map_clear(mp_map_t *map); diff --git a/py/objdict.c b/py/objdict.c index fd5e37dfe0..935829ddb5 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -50,7 +50,7 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { case RT_BINARY_OP_SUBSCR: { // dict load - mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false); + mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false, false); if (elem == NULL) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "")); } else { @@ -140,20 +140,52 @@ static mp_obj_t dict_copy(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); +static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop) { + mp_map_elem_t *elem = mp_map_lookup_helper(self, key, false, pop); + if (elem == NULL) { + if (deflt == NULL) { + if (pop) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "")); + } else { + return mp_const_none; + } + } else { + return deflt; + } + } else { + mp_obj_t value = elem->value; + if (pop) { + /* catch the leak (from mp_map_lookup_helper) */ + m_free(elem, 2 * sizeof(mp_obj_t)); + } + return value; + } + +} + static mp_obj_t dict_get(int n_args, const mp_obj_t *args) { assert(2 <= n_args && n_args <= 3); assert(MP_OBJ_IS_TYPE(args[0], &dict_type)); - mp_map_elem_t *elem = mp_map_lookup_helper(&((mp_obj_dict_t *)args[0])->map, - args[1], false); - if (elem == NULL) { - return n_args >= 3 ? args[2] : mp_const_none; - } else { - return elem->value; - } + return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, + args[1], + n_args == 3 ? args[2] : NULL, + false); } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get); +static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) { + assert(2 <= n_args && n_args <= 3); + assert(MP_OBJ_IS_TYPE(args[0], &dict_type)); + + return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, + args[1], + n_args == 3 ? args[2] : NULL, + true); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop); + + /******************************************************************************/ /* dict constructors & etc */ @@ -168,6 +200,7 @@ const mp_obj_type_t dict_type = { { "clear", &dict_clear_obj }, { "copy", &dict_copy_obj }, { "get", &dict_get_obj }, + { "pop", &dict_pop_obj }, { NULL, NULL }, // end-of-list sentinel }, }; @@ -186,6 +219,6 @@ uint mp_obj_dict_len(mp_obj_t self_in) { mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) { assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); mp_obj_dict_t *self = self_in; - mp_map_lookup_helper(&self->map, key, true)->value = value; + mp_map_lookup_helper(&self->map, key, true, false)->value = value; return self_in; } diff --git a/tests/basics/tests/dict_pop.py b/tests/basics/tests/dict_pop.py new file mode 100644 index 0000000000..602560ce9d --- /dev/null +++ b/tests/basics/tests/dict_pop.py @@ -0,0 +1,12 @@ +d = {1: 2, 3: 4} +print(d.pop(3), d) +print(d) +print(d.pop(1, 42), d) +print(d.pop(1, 42), d) +print(d.pop(1, None), d) +try: + print(d.pop(1), "!!!",) +except KeyError: + print("Raised KeyError") +else: + print("Did not rise KeyError!") -- cgit v1.2.3 From be8fe5be2eb89cd8db741b16dcb50bf5966c33ae Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 6 Jan 2014 20:25:51 +0000 Subject: Added dict.setdefault --- py/map.c | 1 + py/objdict.c | 36 +++++++++++++++++++++++++---------- tests/basics/tests/dict_setdefault.py | 13 +++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 tests/basics/tests/dict_setdefault.py (limited to 'py/map.c') diff --git a/py/map.c b/py/map.c index 8554150adf..b88181989d 100644 --- a/py/map.c +++ b/py/map.c @@ -112,6 +112,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n retval->key = elem->key; retval->value = elem->value; elem->key = NULL; + elem->value = NULL; return retval; } return elem; diff --git a/py/objdict.c b/py/objdict.c index b4cf8f4aa8..753db7d808 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -140,27 +140,30 @@ static mp_obj_t dict_copy(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); -static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop) { - mp_map_elem_t *elem = mp_map_lookup_helper(self, key, false, pop); - if (elem == NULL) { +static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop, bool set) { + mp_map_elem_t *elem = mp_map_lookup_helper(self, key, set, pop); + mp_obj_t value; + if (elem == NULL || elem->value == NULL) { if (deflt == NULL) { if (pop) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "")); } else { - return mp_const_none; + value = mp_const_none; } } else { - return deflt; + value = deflt; } } else { - mp_obj_t value = elem->value; + value = elem->value; if (pop) { /* catch the leak (from mp_map_lookup_helper) */ m_free(elem, 2 * sizeof(mp_obj_t)); } - return value; } - + if (set) { + elem->value = value; + } + return value; } static mp_obj_t dict_get(int n_args, const mp_obj_t *args) { @@ -170,7 +173,7 @@ static mp_obj_t dict_get(int n_args, const mp_obj_t *args) { return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, args[1], n_args == 3 ? args[2] : NULL, - false); + false, false); } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get); @@ -181,11 +184,22 @@ static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) { return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, args[1], n_args == 3 ? args[2] : NULL, - true); + true, false); } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop); +static mp_obj_t dict_setdefault(int n_args, const mp_obj_t *args) { + assert(2 <= n_args && n_args <= 3); + assert(MP_OBJ_IS_TYPE(args[0], &dict_type)); + + return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, + args[1], + n_args == 3 ? args[2] : NULL, + false, true); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault); + static mp_obj_t dict_popitem(mp_obj_t self_in) { assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); @@ -199,6 +213,7 @@ static mp_obj_t dict_popitem(mp_obj_t self_in) { self->map.used--; mp_obj_t items[] = {next->key, next->value}; next->key = NULL; + next->value = NULL; mp_obj_t tuple = mp_obj_new_tuple(2, items); return tuple; @@ -222,6 +237,7 @@ const mp_obj_type_t dict_type = { { "get", &dict_get_obj }, { "pop", &dict_pop_obj }, { "popitem", &dict_popitem_obj }, + { "setdefault", &dict_setdefault_obj }, { NULL, NULL }, // end-of-list sentinel }, }; diff --git a/tests/basics/tests/dict_setdefault.py b/tests/basics/tests/dict_setdefault.py new file mode 100644 index 0000000000..57d0ba4518 --- /dev/null +++ b/tests/basics/tests/dict_setdefault.py @@ -0,0 +1,13 @@ +d = {} +print(d.setdefault(1)) +print(d.setdefault(1)) +print(d.setdefault(5, 42)) +print(d.setdefault(5, 1)) +print(d[1]) +print(d[5]) +d.pop(5) +print(d.setdefault(5, 1)) +print(d[1]) +print(d[5]) + + -- cgit v1.2.3