map.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "py/mpconfig.h"
  31. #include "py/misc.h"
  32. #include "py/runtime.h"
  33. // Fixed empty map. Useful when need to call kw-receiving functions
  34. // without any keywords from C, etc.
  35. const mp_map_t mp_const_empty_map = {
  36. .all_keys_are_qstrs = 0,
  37. .is_fixed = 1,
  38. .is_ordered = 1,
  39. .used = 0,
  40. .alloc = 0,
  41. .table = NULL,
  42. };
  43. // This table of sizes is used to control the growth of hash tables.
  44. // The first set of sizes are chosen so the allocation fits exactly in a
  45. // 4-word GC block, and it's not so important for these small values to be
  46. // prime. The latter sizes are prime and increase at an increasing rate.
  47. STATIC const uint16_t hash_allocation_sizes[] = {
  48. 0, 2, 4, 6, 8, 10, 12, // +2
  49. 17, 23, 29, 37, 47, 59, 73, // *1.25
  50. 97, 127, 167, 223, 293, 389, 521, 691, 919, 1223, 1627, 2161, // *1.33
  51. 3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
  52. };
  53. STATIC size_t get_hash_alloc_greater_or_equal_to(size_t x) {
  54. for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
  55. if (hash_allocation_sizes[i] >= x) {
  56. return hash_allocation_sizes[i];
  57. }
  58. }
  59. // ran out of primes in the table!
  60. // return something sensible, at least make it odd
  61. return (x + x / 2) | 1;
  62. }
  63. /******************************************************************************/
  64. /* map */
  65. void mp_map_init(mp_map_t *map, size_t n) {
  66. if (n == 0) {
  67. map->alloc = 0;
  68. map->table = NULL;
  69. } else {
  70. map->alloc = n;
  71. map->table = m_new0(mp_map_elem_t, map->alloc);
  72. }
  73. map->used = 0;
  74. map->all_keys_are_qstrs = 1;
  75. map->is_fixed = 0;
  76. map->is_ordered = 0;
  77. }
  78. void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
  79. map->alloc = n;
  80. map->used = n;
  81. map->all_keys_are_qstrs = 1;
  82. map->is_fixed = 1;
  83. map->is_ordered = 1;
  84. map->table = (mp_map_elem_t*)table;
  85. }
  86. // Differentiate from mp_map_clear() - semantics is different
  87. void mp_map_deinit(mp_map_t *map) {
  88. if (!map->is_fixed) {
  89. m_del(mp_map_elem_t, map->table, map->alloc);
  90. }
  91. map->used = map->alloc = 0;
  92. }
  93. void mp_map_clear(mp_map_t *map) {
  94. if (!map->is_fixed) {
  95. m_del(mp_map_elem_t, map->table, map->alloc);
  96. }
  97. map->alloc = 0;
  98. map->used = 0;
  99. map->all_keys_are_qstrs = 1;
  100. map->is_fixed = 0;
  101. map->table = NULL;
  102. }
  103. STATIC void mp_map_rehash(mp_map_t *map) {
  104. size_t old_alloc = map->alloc;
  105. size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
  106. mp_map_elem_t *old_table = map->table;
  107. mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
  108. // If we reach this point, table resizing succeeded, now we can edit the old map.
  109. map->alloc = new_alloc;
  110. map->used = 0;
  111. map->all_keys_are_qstrs = 1;
  112. map->table = new_table;
  113. for (size_t i = 0; i < old_alloc; i++) {
  114. if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
  115. mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
  116. }
  117. }
  118. m_del(mp_map_elem_t, old_table, old_alloc);
  119. }
  120. // MP_MAP_LOOKUP behaviour:
  121. // - returns NULL if not found, else the slot it was found in with key,value non-null
  122. // MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
  123. // - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
  124. // MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
  125. // - returns NULL if not found, else the slot if was found in with key null and value non-null
  126. mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
  127. // If the map is a fixed array then we must only be called for a lookup
  128. assert(!map->is_fixed || lookup_kind == MP_MAP_LOOKUP);
  129. // Work out if we can compare just pointers
  130. bool compare_only_ptrs = map->all_keys_are_qstrs;
  131. if (compare_only_ptrs) {
  132. if (MP_OBJ_IS_QSTR(index)) {
  133. // Index is a qstr, so can just do ptr comparison.
  134. } else if (MP_OBJ_IS_TYPE(index, &mp_type_str)) {
  135. // Index is a non-interned string.
  136. // We can either intern the string, or force a full equality comparison.
  137. // We chose the latter, since interning costs time and potentially RAM,
  138. // and it won't necessarily benefit subsequent calls because these calls
  139. // most likely won't pass the newly-interned string.
  140. compare_only_ptrs = false;
  141. } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  142. // If we are not adding, then we can return straight away a failed
  143. // lookup because we know that the index will never be found.
  144. return NULL;
  145. }
  146. }
  147. // if the map is an ordered array then we must do a brute force linear search
  148. if (map->is_ordered) {
  149. for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
  150. if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
  151. if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
  152. // remove the found element by moving the rest of the array down
  153. mp_obj_t value = elem->value;
  154. --map->used;
  155. memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
  156. // put the found element after the end so the caller can access it if needed
  157. elem = &map->table[map->used];
  158. elem->key = MP_OBJ_NULL;
  159. elem->value = value;
  160. }
  161. return elem;
  162. }
  163. }
  164. if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
  165. return NULL;
  166. }
  167. if (map->used == map->alloc) {
  168. // TODO: Alloc policy
  169. map->alloc += 4;
  170. map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
  171. mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
  172. }
  173. mp_map_elem_t *elem = map->table + map->used++;
  174. elem->key = index;
  175. if (!MP_OBJ_IS_QSTR(index)) {
  176. map->all_keys_are_qstrs = 0;
  177. }
  178. return elem;
  179. }
  180. // map is a hash table (not an ordered array), so do a hash lookup
  181. if (map->alloc == 0) {
  182. if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  183. mp_map_rehash(map);
  184. } else {
  185. return NULL;
  186. }
  187. }
  188. // get hash of index, with fast path for common case of qstr
  189. mp_uint_t hash;
  190. if (MP_OBJ_IS_QSTR(index)) {
  191. hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
  192. } else {
  193. hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
  194. }
  195. size_t pos = hash % map->alloc;
  196. size_t start_pos = pos;
  197. mp_map_elem_t *avail_slot = NULL;
  198. for (;;) {
  199. mp_map_elem_t *slot = &map->table[pos];
  200. if (slot->key == MP_OBJ_NULL) {
  201. // found NULL slot, so index is not in table
  202. if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  203. map->used += 1;
  204. if (avail_slot == NULL) {
  205. avail_slot = slot;
  206. }
  207. avail_slot->key = index;
  208. avail_slot->value = MP_OBJ_NULL;
  209. if (!MP_OBJ_IS_QSTR(index)) {
  210. map->all_keys_are_qstrs = 0;
  211. }
  212. return avail_slot;
  213. } else {
  214. return NULL;
  215. }
  216. } else if (slot->key == MP_OBJ_SENTINEL) {
  217. // found deleted slot, remember for later
  218. if (avail_slot == NULL) {
  219. avail_slot = slot;
  220. }
  221. } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
  222. // found index
  223. // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
  224. if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
  225. // delete element in this slot
  226. map->used--;
  227. if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
  228. // optimisation if next slot is empty
  229. slot->key = MP_OBJ_NULL;
  230. } else {
  231. slot->key = MP_OBJ_SENTINEL;
  232. }
  233. // keep slot->value so that caller can access it if needed
  234. }
  235. return slot;
  236. }
  237. // not yet found, keep searching in this table
  238. pos = (pos + 1) % map->alloc;
  239. if (pos == start_pos) {
  240. // search got back to starting position, so index is not in table
  241. if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  242. if (avail_slot != NULL) {
  243. // there was an available slot, so use that
  244. map->used++;
  245. avail_slot->key = index;
  246. avail_slot->value = MP_OBJ_NULL;
  247. if (!MP_OBJ_IS_QSTR(index)) {
  248. map->all_keys_are_qstrs = 0;
  249. }
  250. return avail_slot;
  251. } else {
  252. // not enough room in table, rehash it
  253. mp_map_rehash(map);
  254. // restart the search for the new element
  255. start_pos = pos = hash % map->alloc;
  256. }
  257. } else {
  258. return NULL;
  259. }
  260. }
  261. }
  262. }
  263. /******************************************************************************/
  264. /* set */
  265. #if MICROPY_PY_BUILTINS_SET
  266. void mp_set_init(mp_set_t *set, size_t n) {
  267. set->alloc = n;
  268. set->used = 0;
  269. set->table = m_new0(mp_obj_t, set->alloc);
  270. }
  271. STATIC void mp_set_rehash(mp_set_t *set) {
  272. size_t old_alloc = set->alloc;
  273. mp_obj_t *old_table = set->table;
  274. set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
  275. set->used = 0;
  276. set->table = m_new0(mp_obj_t, set->alloc);
  277. for (size_t i = 0; i < old_alloc; i++) {
  278. if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
  279. mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  280. }
  281. }
  282. m_del(mp_obj_t, old_table, old_alloc);
  283. }
  284. mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
  285. // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
  286. // is handled by using bitwise operations.
  287. if (set->alloc == 0) {
  288. if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  289. mp_set_rehash(set);
  290. } else {
  291. return MP_OBJ_NULL;
  292. }
  293. }
  294. mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
  295. size_t pos = hash % set->alloc;
  296. size_t start_pos = pos;
  297. mp_obj_t *avail_slot = NULL;
  298. for (;;) {
  299. mp_obj_t elem = set->table[pos];
  300. if (elem == MP_OBJ_NULL) {
  301. // found NULL slot, so index is not in table
  302. if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  303. if (avail_slot == NULL) {
  304. avail_slot = &set->table[pos];
  305. }
  306. set->used++;
  307. *avail_slot = index;
  308. return index;
  309. } else {
  310. return MP_OBJ_NULL;
  311. }
  312. } else if (elem == MP_OBJ_SENTINEL) {
  313. // found deleted slot, remember for later
  314. if (avail_slot == NULL) {
  315. avail_slot = &set->table[pos];
  316. }
  317. } else if (mp_obj_equal(elem, index)) {
  318. // found index
  319. if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
  320. // delete element
  321. set->used--;
  322. if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
  323. // optimisation if next slot is empty
  324. set->table[pos] = MP_OBJ_NULL;
  325. } else {
  326. set->table[pos] = MP_OBJ_SENTINEL;
  327. }
  328. }
  329. return elem;
  330. }
  331. // not yet found, keep searching in this table
  332. pos = (pos + 1) % set->alloc;
  333. if (pos == start_pos) {
  334. // search got back to starting position, so index is not in table
  335. if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
  336. if (avail_slot != NULL) {
  337. // there was an available slot, so use that
  338. set->used++;
  339. *avail_slot = index;
  340. return index;
  341. } else {
  342. // not enough room in table, rehash it
  343. mp_set_rehash(set);
  344. // restart the search for the new element
  345. start_pos = pos = hash % set->alloc;
  346. }
  347. } else {
  348. return MP_OBJ_NULL;
  349. }
  350. }
  351. }
  352. }
  353. mp_obj_t mp_set_remove_first(mp_set_t *set) {
  354. for (size_t pos = 0; pos < set->alloc; pos++) {
  355. if (MP_SET_SLOT_IS_FILLED(set, pos)) {
  356. mp_obj_t elem = set->table[pos];
  357. // delete element
  358. set->used--;
  359. if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
  360. // optimisation if next slot is empty
  361. set->table[pos] = MP_OBJ_NULL;
  362. } else {
  363. set->table[pos] = MP_OBJ_SENTINEL;
  364. }
  365. return elem;
  366. }
  367. }
  368. return MP_OBJ_NULL;
  369. }
  370. void mp_set_clear(mp_set_t *set) {
  371. m_del(mp_obj_t, set->table, set->alloc);
  372. set->alloc = 0;
  373. set->used = 0;
  374. set->table = NULL;
  375. }
  376. #endif // MICROPY_PY_BUILTINS_SET
  377. #if defined(DEBUG_PRINT) && DEBUG_PRINT
  378. void mp_map_dump(mp_map_t *map) {
  379. for (size_t i = 0; i < map->alloc; i++) {
  380. if (map->table[i].key != NULL) {
  381. mp_obj_print(map->table[i].key, PRINT_REPR);
  382. } else {
  383. printf("(nil)");
  384. }
  385. printf(": %p\n", map->table[i].value);
  386. }
  387. printf("---\n");
  388. }
  389. #endif