From: David Champion Date: Sun, 29 Jan 2017 02:47:41 +0000 (-0800) Subject: Add reentrant hash_walk() function for iterating down a hash table. X-Git-Tag: mutt-1-8-rel~34 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=705801c1c2ce97531be392e6bd553c9671fdcb1e;p=mutt Add reentrant hash_walk() function for iterating down a hash table. --- 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