From 8bd664e467f760db6f689eb9d30c3d685abe6de5 Mon Sep 17 00:00:00 2001 From: Jacob Janzen Date: Mon, 12 Feb 2024 22:52:41 -0600 Subject: fix hash collision handling --- ht.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/ht.c b/ht.c index d23e141..e5e0f0f 100644 --- a/ht.c +++ b/ht.c @@ -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; } -- cgit v1.2.3