<<< Date Index >>>     <<< Thread Index >>>

[PATCH 1 of 5] Add reentrant hash_walk() function for iterating down a hash table



# HG changeset patch
# User David Champion <dgc@xxxxxxxxxxxx>
# Date 1294082393 21600
# Branch HEAD
# Node ID c51fb78f6b17a8a30b8921e75c1dea8a69fb52a7
# Parent  4cfc804998e218bdbd13cb3ffeee9fd84e0e259e
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