LCOV - code coverage report
Current view: top level - py - map.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-325-gd11ca092f.info Lines: 196 197 99.5 %
Date: 2024-04-17 08:48:33 Functions: 12 12 100.0 %
Branches: 117 124 94.4 %

           Branch data     Line data    Source code
       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                 :            : 
      27                 :            : #include <stdint.h>
      28                 :            : #include <stdlib.h>
      29                 :            : #include <string.h>
      30                 :            : #include <assert.h>
      31                 :            : 
      32                 :            : #include "py/mpconfig.h"
      33                 :            : #include "py/misc.h"
      34                 :            : #include "py/runtime.h"
      35                 :            : 
      36                 :            : #if MICROPY_DEBUG_VERBOSE // print debugging info
      37                 :            : #define DEBUG_PRINT (1)
      38                 :            : #else // don't print debugging info
      39                 :            : #define DEBUG_PRINT (0)
      40                 :            : #define DEBUG_printf(...) (void)0
      41                 :            : #endif
      42                 :            : 
      43                 :            : #if MICROPY_OPT_MAP_LOOKUP_CACHE
      44                 :            : // MP_STATE_VM(map_lookup_cache) provides a cache of index to the last known
      45                 :            : // position of that index in any map. On a cache hit, this allows
      46                 :            : // short-circuiting the full linear search in the case of an ordered map
      47                 :            : // (i.e. all builtin modules and objects' locals dicts), and computation of
      48                 :            : // the hash (and potentially some linear probing) in the case of a regular
      49                 :            : // map. Note the same cache is shared across all maps.
      50                 :            : 
      51                 :            : // Gets the index into the cache for this index. Shift down by two to remove
      52                 :            : // mp_obj_t tag bits.
      53                 :            : #define MAP_CACHE_OFFSET(index) ((((uintptr_t)(index)) >> 2) % MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE)
      54                 :            : // Gets the map cache entry for the corresponding index.
      55                 :            : #define MAP_CACHE_ENTRY(index) (MP_STATE_VM(map_lookup_cache)[MAP_CACHE_OFFSET(index)])
      56                 :            : // Retrieve the mp_obj_t at the location suggested by the cache.
      57                 :            : #define MAP_CACHE_GET(map, index) (&(map)->table[MAP_CACHE_ENTRY(index) % (map)->alloc])
      58                 :            : // Update the cache for this index.
      59                 :            : #define MAP_CACHE_SET(index, pos) MAP_CACHE_ENTRY(index) = (pos) & 0xff;
      60                 :            : #else
      61                 :            : #define MAP_CACHE_SET(index, pos)
      62                 :            : #endif
      63                 :            : 
      64                 :            : // This table of sizes is used to control the growth of hash tables.
      65                 :            : // The first set of sizes are chosen so the allocation fits exactly in a
      66                 :            : // 4-word GC block, and it's not so important for these small values to be
      67                 :            : // prime.  The latter sizes are prime and increase at an increasing rate.
      68                 :            : static const uint16_t hash_allocation_sizes[] = {
      69                 :            :     0, 2, 4, 6, 8, 10, 12, // +2
      70                 :            :     17, 23, 29, 37, 47, 59, 73, // *1.25
      71                 :            :     97, 127, 167, 223, 293, 389, 521, 691, 919, 1223, 1627, 2161, // *1.33
      72                 :            :     3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
      73                 :            : };
      74                 :            : 
      75                 :      47570 : static size_t get_hash_alloc_greater_or_equal_to(size_t x) {
      76         [ +  - ]:     194741 :     for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
      77         [ +  + ]:     194741 :         if (hash_allocation_sizes[i] >= x) {
      78                 :            :             return hash_allocation_sizes[i];
      79                 :            :         }
      80                 :            :     }
      81                 :            :     // ran out of primes in the table!
      82                 :            :     // return something sensible, at least make it odd
      83                 :          0 :     return (x + x / 2) | 1;
      84                 :            : }
      85                 :            : 
      86                 :            : /******************************************************************************/
      87                 :            : /* map                                                                        */
      88                 :            : 
      89                 :      22632 : void mp_map_init(mp_map_t *map, size_t n) {
      90         [ +  + ]:      22632 :     if (n == 0) {
      91                 :       9550 :         map->alloc = 0;
      92                 :       9550 :         map->table = NULL;
      93                 :            :     } else {
      94                 :      13082 :         map->alloc = n;
      95                 :      13082 :         map->table = m_new0(mp_map_elem_t, map->alloc);
      96                 :            :     }
      97                 :      22632 :     map->used = 0;
      98                 :      22632 :     map->all_keys_are_qstrs = 1;
      99                 :      22632 :     map->is_fixed = 0;
     100                 :      22632 :     map->is_ordered = 0;
     101                 :      22632 : }
     102                 :            : 
     103                 :     549390 : void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
     104                 :     549390 :     map->alloc = n;
     105                 :     549390 :     map->used = n;
     106                 :     549390 :     map->all_keys_are_qstrs = 1;
     107                 :     549390 :     map->is_fixed = 1;
     108                 :     549390 :     map->is_ordered = 1;
     109                 :     549390 :     map->table = (mp_map_elem_t *)table;
     110                 :     549390 : }
     111                 :            : 
     112                 :            : // Differentiate from mp_map_clear() - semantics is different
     113                 :       3840 : void mp_map_deinit(mp_map_t *map) {
     114         [ +  - ]:       3840 :     if (!map->is_fixed) {
     115                 :       3840 :         m_del(mp_map_elem_t, map->table, map->alloc);
     116                 :            :     }
     117                 :       3840 :     map->used = map->alloc = 0;
     118                 :       3840 : }
     119                 :            : 
     120                 :          8 : void mp_map_clear(mp_map_t *map) {
     121         [ +  - ]:          8 :     if (!map->is_fixed) {
     122                 :          8 :         m_del(mp_map_elem_t, map->table, map->alloc);
     123                 :            :     }
     124                 :          8 :     map->alloc = 0;
     125                 :          8 :     map->used = 0;
     126                 :          8 :     map->all_keys_are_qstrs = 1;
     127                 :          8 :     map->is_fixed = 0;
     128                 :          8 :     map->table = NULL;
     129                 :          8 : }
     130                 :            : 
     131                 :      40580 : static void mp_map_rehash(mp_map_t *map) {
     132                 :      40580 :     size_t old_alloc = map->alloc;
     133                 :      40580 :     size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
     134                 :      40580 :     DEBUG_printf("mp_map_rehash(%p): " UINT_FMT " -> " UINT_FMT "\n", map, old_alloc, new_alloc);
     135                 :      40580 :     mp_map_elem_t *old_table = map->table;
     136                 :      40580 :     mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
     137                 :            :     // If we reach this point, table resizing succeeded, now we can edit the old map.
     138                 :      40576 :     map->alloc = new_alloc;
     139                 :      40576 :     map->used = 0;
     140                 :      40576 :     map->all_keys_are_qstrs = 1;
     141                 :      40576 :     map->table = new_table;
     142         [ +  + ]:     400483 :     for (size_t i = 0; i < old_alloc; i++) {
     143         [ +  - ]:     359907 :         if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
     144                 :     359907 :             mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
     145                 :            :         }
     146                 :            :     }
     147                 :      40576 :     m_del(mp_map_elem_t, old_table, old_alloc);
     148                 :      40576 : }
     149                 :            : 
     150                 :            : // MP_MAP_LOOKUP behaviour:
     151                 :            : //  - returns NULL if not found, else the slot it was found in with key,value non-null
     152                 :            : // MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
     153                 :            : //  - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
     154                 :            : // MP_MAP_LOOKUP_REMOVE_IF_FOUND behaviour:
     155                 :            : //  - returns NULL if not found, else the slot if was found in with key null and value non-null
     156                 :   25635023 : mp_map_elem_t *MICROPY_WRAP_MP_MAP_LOOKUP(mp_map_lookup)(mp_map_t * map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
     157                 :            :     // If the map is a fixed array then we must only be called for a lookup
     158   [ +  +  -  + ]:   25635023 :     assert(!map->is_fixed || lookup_kind == MP_MAP_LOOKUP);
     159                 :            : 
     160                 :            :     #if MICROPY_OPT_MAP_LOOKUP_CACHE
     161                 :            :     // Try the cache for lookup or add-if-not-found.
     162   [ +  +  +  + ]:   25635023 :     if (lookup_kind != MP_MAP_LOOKUP_REMOVE_IF_FOUND && map->alloc) {
     163                 :   23923199 :         mp_map_elem_t *slot = MAP_CACHE_GET(map, index);
     164                 :            :         // Note: Just comparing key for value equality will have false negatives, but
     165                 :            :         // these will be handled by the regular path below.
     166         [ +  + ]:   23923199 :         if (slot->key == index) {
     167                 :            :             return slot;
     168                 :            :         }
     169                 :            :     }
     170                 :            :     #endif
     171                 :            : 
     172                 :            :     // Work out if we can compare just pointers
     173                 :    4476624 :     bool compare_only_ptrs = map->all_keys_are_qstrs;
     174         [ +  + ]:    4476624 :     if (compare_only_ptrs) {
     175         [ +  + ]:    4169804 :         if (mp_obj_is_qstr(index)) {
     176                 :            :             // Index is a qstr, so can just do ptr comparison.
     177   [ +  +  +  + ]:       5717 :         } else if (mp_obj_is_exact_type(index, &mp_type_str)) {
     178                 :            :             // Index is a non-interned string.
     179                 :            :             // We can either intern the string, or force a full equality comparison.
     180                 :            :             // We chose the latter, since interning costs time and potentially RAM,
     181                 :            :             // and it won't necessarily benefit subsequent calls because these calls
     182                 :            :             // most likely won't pass the newly-interned string.
     183                 :            :             compare_only_ptrs = false;
     184         [ +  + ]:        841 :         } else if (lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     185                 :            :             // If we are not adding, then we can return straight away a failed
     186                 :            :             // lookup because we know that the index will never be found.
     187                 :            :             return NULL;
     188                 :            :         }
     189                 :            :     }
     190                 :            : 
     191                 :            :     // if the map is an ordered array then we must do a brute force linear search
     192         [ +  + ]:    4476592 :     if (map->is_ordered) {
     193         [ +  + ]:    4322107 :         for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
     194   [ +  +  +  +  :    3448925 :             if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
                   -  + ]
     195                 :            :                 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
     196         [ +  + ]:     229336 :                 if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {
     197                 :            :                     // remove the found element by moving the rest of the array down
     198                 :         24 :                     mp_obj_t value = elem->value;
     199                 :         24 :                     --map->used;
     200                 :         24 :                     memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
     201                 :            :                     // put the found element after the end so the caller can access it if needed
     202                 :            :                     // note: caller must NULL the value so the GC can clean up (e.g. see dict_get_helper).
     203                 :         24 :                     elem = &map->table[map->used];
     204                 :         24 :                     elem->key = MP_OBJ_NULL;
     205                 :         24 :                     elem->value = value;
     206                 :            :                 }
     207                 :            :                 #endif
     208                 :     229336 :                 MAP_CACHE_SET(index, elem - map->table);
     209                 :     229336 :                 return elem;
     210                 :            :             }
     211                 :            :         }
     212                 :            :         #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
     213         [ +  + ]:     873182 :         if (MP_LIKELY(lookup_kind != MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)) {
     214                 :            :             return NULL;
     215                 :            :         }
     216         [ +  + ]:         94 :         if (map->used == map->alloc) {
     217                 :            :             // TODO: Alloc policy
     218                 :         30 :             map->alloc += 4;
     219                 :         30 :             map->table = m_renew(mp_map_elem_t, map->table, map->used, map->alloc);
     220                 :         30 :             mp_seq_clear(map->table, map->used, map->alloc, sizeof(*map->table));
     221                 :            :         }
     222                 :         94 :         mp_map_elem_t *elem = map->table + map->used++;
     223                 :         94 :         elem->key = index;
     224                 :         94 :         elem->value = MP_OBJ_NULL;
     225         [ +  + ]:         94 :         if (!mp_obj_is_qstr(index)) {
     226                 :         12 :             map->all_keys_are_qstrs = 0;
     227                 :            :         }
     228                 :         94 :         return elem;
     229                 :            :         #else
     230                 :            :         return NULL;
     231                 :            :         #endif
     232                 :            :     }
     233                 :            : 
     234                 :            :     // map is a hash table (not an ordered array), so do a hash lookup
     235                 :            : 
     236         [ +  + ]:    3374074 :     if (map->alloc == 0) {
     237         [ +  + ]:     107645 :         if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     238                 :       3711 :             mp_map_rehash(map);
     239                 :            :         } else {
     240                 :            :             return NULL;
     241                 :            :         }
     242                 :            :     }
     243                 :            : 
     244                 :            :     // get hash of index, with fast path for common case of qstr
     245                 :    3270140 :     mp_uint_t hash;
     246         [ +  + ]:    3270140 :     if (mp_obj_is_qstr(index)) {
     247                 :    2971503 :         hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
     248                 :            :     } else {
     249                 :     298637 :         hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
     250                 :            :     }
     251                 :            : 
     252                 :    3270364 :     size_t pos = hash % map->alloc;
     253                 :    3270364 :     size_t start_pos = pos;
     254                 :    3270364 :     mp_map_elem_t *avail_slot = NULL;
     255                 :   21175273 :     for (;;) {
     256                 :   21175273 :         mp_map_elem_t *slot = &map->table[pos];
     257         [ +  + ]:   21175273 :         if (slot->key == MP_OBJ_NULL) {
     258                 :            :             // found NULL slot, so index is not in table
     259         [ +  + ]:    1546090 :             if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     260                 :     521481 :                 map->used += 1;
     261         [ +  + ]:     521481 :                 if (avail_slot == NULL) {
     262                 :     521294 :                     avail_slot = slot;
     263                 :            :                 }
     264                 :     521481 :                 avail_slot->key = index;
     265                 :     521481 :                 avail_slot->value = MP_OBJ_NULL;
     266         [ +  + ]:     521481 :                 if (!mp_obj_is_qstr(index)) {
     267                 :     278860 :                     map->all_keys_are_qstrs = 0;
     268                 :            :                 }
     269                 :     521481 :                 return avail_slot;
     270                 :            :             } else {
     271                 :            :                 return NULL;
     272                 :            :             }
     273         [ +  + ]:   19629183 :         } else if (slot->key == MP_OBJ_SENTINEL) {
     274                 :            :             // found deleted slot, remember for later
     275         [ +  + ]:       3648 :             if (avail_slot == NULL) {
     276                 :       3522 :                 avail_slot = slot;
     277                 :            :             }
     278   [ +  +  +  +  :   19625535 :         } else if (slot->key == index || (!compare_only_ptrs && mp_obj_equal(slot->key, index))) {
                   +  + ]
     279                 :            :             // found index
     280                 :            :             // Note: CPython does not replace the index; try x={True:'true'};x[1]='one';x
     281         [ +  + ]:     586228 :             if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
     282                 :            :                 // delete element in this slot
     283                 :       3386 :                 map->used--;
     284         [ +  + ]:       3386 :                 if (map->table[(pos + 1) % map->alloc].key == MP_OBJ_NULL) {
     285                 :            :                     // optimisation if next slot is empty
     286                 :        615 :                     slot->key = MP_OBJ_NULL;
     287                 :            :                 } else {
     288                 :       2771 :                     slot->key = MP_OBJ_SENTINEL;
     289                 :            :                 }
     290                 :            :                 // keep slot->value so that caller can access it if needed
     291                 :            :             }
     292                 :     586228 :             MAP_CACHE_SET(index, pos);
     293                 :     586228 :             return slot;
     294                 :            :         }
     295                 :            : 
     296                 :            :         // not yet found, keep searching in this table
     297                 :   19042955 :         pos = (pos + 1) % map->alloc;
     298                 :            : 
     299         [ +  + ]:   19042955 :         if (pos == start_pos) {
     300                 :            :             // search got back to starting position, so index is not in table
     301         [ +  + ]:    1174922 :             if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     302         [ +  + ]:      37030 :                 if (avail_slot != NULL) {
     303                 :            :                     // there was an available slot, so use that
     304                 :        161 :                     map->used++;
     305                 :        161 :                     avail_slot->key = index;
     306                 :        161 :                     avail_slot->value = MP_OBJ_NULL;
     307         [ +  + ]:        161 :                     if (!mp_obj_is_qstr(index)) {
     308                 :          4 :                         map->all_keys_are_qstrs = 0;
     309                 :            :                     }
     310                 :        161 :                     return avail_slot;
     311                 :            :                 } else {
     312                 :            :                     // not enough room in table, rehash it
     313                 :      36869 :                     mp_map_rehash(map);
     314                 :            :                     // restart the search for the new element
     315                 :      36876 :                     start_pos = pos = hash % map->alloc;
     316                 :            :                 }
     317                 :            :             } else {
     318                 :            :                 return NULL;
     319                 :            :             }
     320                 :            :         }
     321                 :            :     }
     322                 :            : }
     323                 :            : 
     324                 :            : /******************************************************************************/
     325                 :            : /* set                                                                        */
     326                 :            : 
     327                 :            : #if MICROPY_PY_BUILTINS_SET
     328                 :            : 
     329                 :       6845 : void mp_set_init(mp_set_t *set, size_t n) {
     330                 :       6845 :     set->alloc = n;
     331                 :       6845 :     set->used = 0;
     332                 :       6845 :     set->table = m_new0(mp_obj_t, set->alloc);
     333                 :       6845 : }
     334                 :            : 
     335                 :       6990 : static void mp_set_rehash(mp_set_t *set) {
     336                 :       6990 :     size_t old_alloc = set->alloc;
     337                 :       6990 :     mp_obj_t *old_table = set->table;
     338                 :       6990 :     set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
     339                 :       6990 :     set->used = 0;
     340                 :       6990 :     set->table = m_new0(mp_obj_t, set->alloc);
     341         [ +  + ]:      39964 :     for (size_t i = 0; i < old_alloc; i++) {
     342         [ +  - ]:      32974 :         if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
     343                 :      32974 :             mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
     344                 :            :         }
     345                 :            :     }
     346                 :       6990 :     m_del(mp_obj_t, old_table, old_alloc);
     347                 :       6990 : }
     348                 :            : 
     349                 :      65917 : mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
     350                 :            :     // Note: lookup_kind can be MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND which
     351                 :            :     // is handled by using bitwise operations.
     352                 :            : 
     353         [ +  + ]:      65917 :     if (set->alloc == 0) {
     354         [ +  + ]:       2344 :         if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     355                 :       1660 :             mp_set_rehash(set);
     356                 :            :         } else {
     357                 :            :             return MP_OBJ_NULL;
     358                 :            :         }
     359                 :            :     }
     360                 :      65233 :     mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
     361                 :      65233 :     size_t pos = hash % set->alloc;
     362                 :      65233 :     size_t start_pos = pos;
     363                 :      65233 :     mp_obj_t *avail_slot = NULL;
     364                 :     176995 :     for (;;) {
     365                 :     176995 :         mp_obj_t elem = set->table[pos];
     366         [ +  + ]:     176995 :         if (elem == MP_OBJ_NULL) {
     367                 :            :             // found NULL slot, so index is not in table
     368         [ +  + ]:      49317 :             if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     369         [ +  + ]:      47721 :                 if (avail_slot == NULL) {
     370                 :      47673 :                     avail_slot = &set->table[pos];
     371                 :            :                 }
     372                 :      47721 :                 set->used++;
     373                 :      47721 :                 *avail_slot = index;
     374                 :      47721 :                 return index;
     375                 :            :             } else {
     376                 :            :                 return MP_OBJ_NULL;
     377                 :            :             }
     378         [ +  + ]:     127678 :         } else if (elem == MP_OBJ_SENTINEL) {
     379                 :            :             // found deleted slot, remember for later
     380         [ +  + ]:       2496 :             if (avail_slot == NULL) {
     381                 :       2284 :                 avail_slot = &set->table[pos];
     382                 :            :             }
     383         [ +  + ]:     125182 :         } else if (mp_obj_equal(elem, index)) {
     384                 :            :             // found index
     385         [ +  + ]:      13584 :             if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
     386                 :            :                 // delete element
     387                 :       2092 :                 set->used--;
     388         [ +  + ]:       2092 :                 if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
     389                 :            :                     // optimisation if next slot is empty
     390                 :        248 :                     set->table[pos] = MP_OBJ_NULL;
     391                 :            :                 } else {
     392                 :       1844 :                     set->table[pos] = MP_OBJ_SENTINEL;
     393                 :            :                 }
     394                 :            :             }
     395                 :      13584 :             return elem;
     396                 :            :         }
     397                 :            : 
     398                 :            :         // not yet found, keep searching in this table
     399                 :     114094 :         pos = (pos + 1) % set->alloc;
     400                 :            : 
     401         [ +  + ]:     114094 :         if (pos == start_pos) {
     402                 :            :             // search got back to starting position, so index is not in table
     403         [ +  + ]:       7662 :             if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
     404         [ +  + ]:       5438 :                 if (avail_slot != NULL) {
     405                 :            :                     // there was an available slot, so use that
     406                 :        108 :                     set->used++;
     407                 :        108 :                     *avail_slot = index;
     408                 :        108 :                     return index;
     409                 :            :                 } else {
     410                 :            :                     // not enough room in table, rehash it
     411                 :       5330 :                     mp_set_rehash(set);
     412                 :            :                     // restart the search for the new element
     413                 :       5330 :                     start_pos = pos = hash % set->alloc;
     414                 :            :                 }
     415                 :            :             } else {
     416                 :            :                 return MP_OBJ_NULL;
     417                 :            :             }
     418                 :            :         }
     419                 :            :     }
     420                 :            : }
     421                 :            : 
     422                 :         52 : mp_obj_t mp_set_remove_first(mp_set_t *set) {
     423         [ +  + ]:        276 :     for (size_t pos = 0; pos < set->alloc; pos++) {
     424         [ +  + ]:        272 :         if (mp_set_slot_is_filled(set, pos)) {
     425                 :         48 :             mp_obj_t elem = set->table[pos];
     426                 :            :             // delete element
     427                 :         48 :             set->used--;
     428         [ +  + ]:         48 :             if (set->table[(pos + 1) % set->alloc] == MP_OBJ_NULL) {
     429                 :            :                 // optimisation if next slot is empty
     430                 :          4 :                 set->table[pos] = MP_OBJ_NULL;
     431                 :            :             } else {
     432                 :         44 :                 set->table[pos] = MP_OBJ_SENTINEL;
     433                 :            :             }
     434                 :         48 :             return elem;
     435                 :            :         }
     436                 :            :     }
     437                 :            :     return MP_OBJ_NULL;
     438                 :            : }
     439                 :            : 
     440                 :        128 : void mp_set_clear(mp_set_t *set) {
     441                 :        128 :     m_del(mp_obj_t, set->table, set->alloc);
     442                 :        128 :     set->alloc = 0;
     443                 :        128 :     set->used = 0;
     444                 :        128 :     set->table = NULL;
     445                 :        128 : }
     446                 :            : 
     447                 :            : #endif // MICROPY_PY_BUILTINS_SET
     448                 :            : 
     449                 :            : #if defined(DEBUG_PRINT) && DEBUG_PRINT
     450                 :            : void mp_map_dump(mp_map_t *map) {
     451                 :            :     for (size_t i = 0; i < map->alloc; i++) {
     452                 :            :         if (map->table[i].key != MP_OBJ_NULL) {
     453                 :            :             mp_obj_print(map->table[i].key, PRINT_REPR);
     454                 :            :         } else {
     455                 :            :             DEBUG_printf("(nil)");
     456                 :            :         }
     457                 :            :         DEBUG_printf(": %p\n", map->table[i].value);
     458                 :            :     }
     459                 :            :     DEBUG_printf("---\n");
     460                 :            : }
     461                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d