]> granicus.if.org Git - mutt/commitdiff
Add reentrant hash_walk() function for iterating down a hash table.
authorDavid Champion <dgc@bikeshed.us>
Sun, 29 Jan 2017 02:47:41 +0000 (18:47 -0800)
committerDavid Champion <dgc@bikeshed.us>
Sun, 29 Jan 2017 02:47:41 +0000 (18:47 -0800)
hash.c
hash.h

diff --git a/hash.c b/hash.c
index c8dbaeba2a6bcd4d104e0b88c5acdb5fce653c17..d87a3218065d0424f242763c201d45c24ffc1e55 100644 (file)
--- 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 3aa5b60514feb454d7a0db5e9010d2f4afc85610..ce2a5d69c690dbbe89eed58ff530e4bb7f49ba29 100644 (file)
--- 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