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: neomutt-20170225~32^2~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6172eb1ecb9ef8002e6f55ffa04f6dd88274b013;p=neomutt Add reentrant hash_walk() function for iterating down a hash table. --- diff --git a/hash.c b/hash.c index c8dbaeba2..d87a32180 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 3aa5b6051..ce2a5d69c 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