fix wrong slot and index numbers

Fri, 27 May 2022 14:02:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2022 14:02:27 +0200
changeset 560
2d6a3e2dc8ff
parent 559
8603709932b9
child 561
bb17790af41e

fix wrong slot and index numbers

src/hash_map.c file | annotate | diff | comparison | revisions
--- a/src/hash_map.c	Fri May 27 14:02:14 2022 +0200
+++ b/src/hash_map.c	Fri May 27 14:02:27 2022 +0200
@@ -295,20 +295,15 @@
     } else {
         // just advance
         elm = elm->next;
+        iter->index++;
     }
 
-    // do we leave the bucket?
-    if (elm == NULL) {
-        // search the next bucket
-        for (; elm == NULL && iter->slot < map->bucket_count; iter->slot++) {
-            elm = map->buckets[iter->slot];
-        }
+    // search the next bucket, if required
+    while (elm == NULL && ++iter->slot < map->bucket_count) {
+        elm = map->buckets[iter->slot];
     }
 
-    // advance the index in any case
-    iter->index++;
-
-    // fill the struct with the current element
+    // fill the struct with the next element
     iter->elem_handle = elm;
     if (elm == NULL) {
         iter->kv_data.key = NULL;
@@ -334,7 +329,7 @@
 
     if (map->size > 0) {
         struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
-        struct cx_hash_map_element_s *elm = NULL;
+        struct cx_hash_map_element_s *elm = hash_map->buckets[0];
         for (; elm == NULL; iter.slot++) {
             elm = hash_map->buckets[iter.slot];
         }

mercurial