hash.c | 27 +++++++++++++++++++++++++++ hash.h | 7 +++++++ 2 files changed, 34 insertions(+), 0 deletions(-)
# HG changeset patch # User David Champion <[email protected]> # Date 1472605287 25200 # Tue Aug 30 18:01:27 2016 -0700 # Node ID beaac419ca1dd13d0d11fdf33bb0d128fa634045 # Parent d7cbf28a3dfd48aace63a1c915982b8596bc3ad6 Add reentrant hash_walk() function for iterating down a hash table. diff --git a/hash.c b/hash.c --- a/hash.c +++ b/hash.c @@ -176,3 +176,30 @@ 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 --- a/hash.h +++ b/hash.h @@ -46,4 +46,11 @@ void (*destroy) (void *)); 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
