]> granicus.if.org Git - neomutt/commitdiff
Add reentrant hash_walk() function for iterating down a hash table.
authorDavid Champion <dgc@bikeshed.us>
Fri, 8 Apr 2016 00:01:50 +0000 (01:01 +0100)
committerRichard Russon <rich@flatcap.org>
Tue, 17 May 2016 16:27:26 +0000 (17:27 +0100)
hash.c
hash.h

diff --git a/hash.c b/hash.c
index 08f717179b91d1dea2e280d2fd0e03378acc5e26..b20e5c50b2ae5f650e58a049af5ee7d0eb81478e 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -176,3 +176,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 fb77d0c14c5cc5d81668612f2ea44e8a2811e45a..2939beccd14ecd07455b2574e54c132fcdae37b2 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -46,4 +46,11 @@ void hash_delete_hash (HASH * table, int hash, const char *key, const void *data
                       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