From 705801c1c2ce97531be392e6bd553c9671fdcb1e Mon Sep 17 00:00:00 2001 From: David Champion Date: Sat, 28 Jan 2017 18:47:41 -0800 Subject: [PATCH] Add reentrant hash_walk() function for iterating down a hash table. --- hash.c | 27 +++++++++++++++++++++++++++ hash.h | 7 +++++++ 2 files changed, 34 insertions(+) diff --git a/hash.c b/hash.c index c8dbaeba..d87a3218 100644 --- a/hash.c +++ b/hash.c @@ -297,3 +297,30 @@ void hash_destroy (HASH **ptr, void (*destroy) (void *)) FREE (&pptr->table); FREE (ptr); /* __FREE_CHECKED__ */ } + +struct hash_elem *hash_walk(const HASH *table, struct hash_walk_state *state) +{ + if (state->last && state->last->next) + { + state->last = state->last->next; + return state->last; + } + + if (state->last) + state->index++; + + while (state->index < table->nelem) + { + if (table->table[state->index]) + { + state->last = table->table[state->index]; + return state->last; + } + state->index++; + } + + state->index = 0; + state->last = NULL; + return NULL; +} + diff --git a/hash.h b/hash.h index 3aa5b605..ce2a5d69 100644 --- a/hash.h +++ b/hash.h @@ -64,4 +64,11 @@ void int_hash_delete (HASH * table, unsigned int key, const void *data, void hash_destroy (HASH ** hash, void (*destroy) (void *)); +struct hash_walk_state { + int index; + struct hash_elem *last; +}; + +struct hash_elem *hash_walk(const HASH *table, struct hash_walk_state *state); + #endif -- 2.40.0