diff options
author | Jacob Janzen <jacob.a.s.janzen@gmail.com> | 2024-02-12 22:52:41 -0600 |
---|---|---|
committer | Jacob Janzen <jacob.a.s.janzen@gmail.com> | 2024-02-12 22:52:41 -0600 |
commit | 8bd664e467f760db6f689eb9d30c3d685abe6de5 (patch) | |
tree | 17239d137521ab5d84132e6e0a3a44b95e6c1ece | |
parent | 632fc043ba549e2bf5282ce2fe6b94bdc747044a (diff) |
fix hash collision handling
-rw-r--r-- | ht.c | 23 |
1 files changed, 8 insertions, 15 deletions
@@ -156,30 +156,23 @@ void ht_iter_init(ht_t *h) void *ht_iter_next(ht_t *h) { + void *out; + // return NULL if we've reached the end if (!h->curr_node && h->curr_index >= h->max_size) return NULL; + // look for next index with node if current node is NULL if (!h->curr_node) { - // look for next index with node if current node is NULL while (!h->curr_node) { h->curr_node = h->vals[h->curr_index++]; if (h->curr_index >= h->max_size) return NULL; } - return h->curr_node->val; - } else { - // move to the next node - h->curr_node = h->curr_node->next; - if (h->curr_node) - return h->curr_node->val; - - // look for next index with node if necessary - while (!h->curr_node) { - h->curr_node = h->vals[h->curr_index++]; - if (h->curr_index >= h->max_size) - return NULL; - } - return h->curr_node->val; } + + // get the value and move to the next node + out = h->curr_node->val; + h->curr_node = h->curr_node->next; + return out; } |