map.c 6.76 KB
Newer Older
 Damien committed Dec 17, 2013 1 2 3 4 5 #include #include #include #include "misc.h" Damien committed Dec 21, 2013 6 #include "mpconfig.h" Damien George committed Jan 21, 2014 7 #include "qstr.h" Damien committed Dec 17, 2013 8 #include "obj.h" Damien committed Dec 21, 2013 9 10 #include "runtime0.h" #include "map.h" Damien committed Dec 17, 2013 11 12 // approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}] John R. Lenton committed Jan 07, 2014 13 14 // 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}; Damien committed Dec 17, 2013 15 16 17 18 19 20 21 22 23 24 25 26 int get_doubling_prime_greater_or_equal_to(int x) { 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; } Damien committed Dec 21, 2013 27 28 29 /******************************************************************************/ /* map */ Damien George committed Jan 08, 2014 30 void mp_map_init(mp_map_t *map, int n) { Damien committed Dec 17, 2013 31 map->alloc = get_doubling_prime_greater_or_equal_to(n + 1); Damien George committed Jan 08, 2014 32 33 map->used = 0; map->all_keys_are_qstrs = 1; Damien committed Dec 21, 2013 34 map->table = m_new0(mp_map_elem_t, map->alloc); Damien committed Dec 17, 2013 35 36 } Damien George committed Jan 08, 2014 37 mp_map_t *mp_map_new(int n) { Damien committed Dec 21, 2013 38 mp_map_t *map = m_new(mp_map_t, 1); Damien George committed Jan 08, 2014 39 mp_map_init(map, n); Damien committed Dec 17, 2013 40 41 42 return map; } John R. Lenton committed Jan 07, 2014 43 44 void mp_map_clear(mp_map_t *map) { map->used = 0; Damien George committed Jan 08, 2014 45 map->all_keys_are_qstrs = 1; John R. Lenton committed Jan 07, 2014 46 47 48 49 50 51 52 53 54 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; } } Damien George committed Jan 08, 2014 55 static void mp_map_rehash(mp_map_t *map) { John R. Lenton committed Jan 07, 2014 56 57 58 59 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; Damien George committed Jan 08, 2014 60 map->all_keys_are_qstrs = 1; John R. Lenton committed Jan 07, 2014 61 62 63 map->table = m_new0(mp_map_elem_t, map->alloc); for (int i = 0; i < old_alloc; i++) { if (old_table[i].key != NULL) { Damien George committed Jan 08, 2014 64 mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value; John R. Lenton committed Jan 07, 2014 65 66 67 68 69 } } m_del(mp_map_elem_t, old_table, old_alloc); } Damien George committed Jan 08, 2014 70 mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) { Damien committed Dec 17, 2013 71 machine_uint_t hash; Damien George committed Jan 08, 2014 72 hash = mp_obj_hash(index); John R. Lenton committed Jan 07, 2014 73 if (map->alloc == 0) { Damien George committed Jan 08, 2014 74 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { John R. Lenton committed Jan 07, 2014 75 76 77 78 79 mp_map_rehash(map); } else { return NULL; } } Damien committed Dec 17, 2013 80 81 uint pos = hash % map->alloc; for (;;) { Damien committed Dec 21, 2013 82 mp_map_elem_t *elem = &map->table[pos]; Damien committed Dec 17, 2013 83 84 if (elem->key == NULL) { // not in table Damien George committed Jan 08, 2014 85 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { Damien committed Dec 17, 2013 86 87 if (map->used + 1 >= map->alloc) { // not enough room in table, rehash it John R. Lenton committed Jan 07, 2014 88 mp_map_rehash(map); Damien committed Dec 17, 2013 89 90 91 92 93 // restart the search for the new element pos = hash % map->alloc; } else { map->used += 1; elem->key = index; Damien George committed Jan 08, 2014 94 95 96 if (!MP_OBJ_IS_QSTR(index)) { map->all_keys_are_qstrs = 0; } Damien committed Dec 17, 2013 97 98 99 100 101 return elem; } } else { return NULL; } Damien George committed Jan 08, 2014 102 } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) { Damien committed Dec 17, 2013 103 104 105 106 107 108 // found it /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x if (add_if_not_found) { elem->key = index; } */ Damien George committed Jan 08, 2014 109 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) { John R. Lenton committed Jan 07, 2014 110 map->used--; Damien George committed Jan 08, 2014 111 // this leaks this memory (but see dict_get_helper) John R. Lenton committed Jan 07, 2014 112 113 114 115 mp_map_elem_t *retval = m_new(mp_map_elem_t, 1); retval->key = elem->key; retval->value = elem->value; elem->key = NULL; John R. Lenton committed Jan 07, 2014 116 elem->value = NULL; John R. Lenton committed Jan 07, 2014 117 118 return retval; } Damien committed Dec 17, 2013 119 120 121 122 123 124 125 126 return elem; } else { // not yet found, keep searching in this table pos = (pos + 1) % map->alloc; } } } Damien committed Dec 21, 2013 127 128 129 130 131 132 133 /******************************************************************************/ /* 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); Damien committed Dec 17, 2013 134 135 } John R. Lenton committed Jan 12, 2014 136 137 138 139 140 141 142 143 144 145 146 147 148 149 static void mp_set_rehash(mp_set_t *set) { 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++) { if (old_table[i] != NULL) { mp_set_lookup(set, old_table[i], true); } } m_del(mp_obj_t, old_table, old_alloc); } John R. Lenton committed Jan 12, 2014 150 mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) { John R. Lenton committed Jan 12, 2014 151 152 int hash; int pos; John R. Lenton committed Jan 12, 2014 153 if (set->alloc == 0) { John R. Lenton committed Jan 12, 2014 154 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { John R. Lenton committed Jan 12, 2014 155 156 157 158 159 mp_set_rehash(set); } else { return NULL; } } John R. Lenton committed Jan 12, 2014 160 161 162 163 164 165 166 if (lookup_kind & MP_MAP_LOOKUP_FIRST) { hash = 0; pos = 0; } else { hash = mp_obj_hash(index);; pos = hash % set->alloc; } Damien committed Dec 17, 2013 167 for (;;) { Damien committed Dec 21, 2013 168 169 mp_obj_t elem = set->table[pos]; if (elem == MP_OBJ_NULL) { Damien committed Dec 17, 2013 170 // not in table John R. Lenton committed Jan 12, 2014 171 if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { Damien committed Dec 21, 2013 172 if (set->used + 1 >= set->alloc) { Damien committed Dec 17, 2013 173 // not enough room in table, rehash it John R. Lenton committed Jan 12, 2014 174 mp_set_rehash(set); Damien committed Dec 17, 2013 175 // restart the search for the new element Damien committed Dec 21, 2013 176 pos = hash % set->alloc; Damien committed Dec 17, 2013 177 } else { Damien committed Dec 21, 2013 178 179 set->used += 1; set->table[pos] = index; Damien committed Dec 17, 2013 180 181 return index; } John R. Lenton committed Jan 12, 2014 182 183 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) { pos++; Damien committed Dec 17, 2013 184 } else { Damien committed Dec 21, 2013 185 return MP_OBJ_NULL; Damien committed Dec 17, 2013 186 } John R. Lenton committed Jan 12, 2014 187 } else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) { Damien committed Dec 17, 2013 188 // found it John R. Lenton committed Jan 12, 2014 189 if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) { John R. Lenton committed Jan 12, 2014 190 191 192 set->used--; set->table[pos] = NULL; } Damien committed Dec 17, 2013 193 194 195 return elem; } else { // not yet found, keep searching in this table Damien committed Dec 21, 2013 196 pos = (pos + 1) % set->alloc; Damien committed Dec 17, 2013 197 198 199 } } } John R. Lenton committed Jan 12, 2014 200 201 202 203 204 205 206 207 208 209 void mp_set_clear(mp_set_t *set) { set->used = 0; machine_uint_t a = set->alloc; set->alloc = 0; set->table = m_renew(mp_obj_t, set->table, a, set->alloc); for (uint i=0; ialloc; i++) { set->table[i] = NULL; } }