hash_table: Add an iterator for doing things like cleanup of the HT.
authorEric Anholt <eric@anholt.net>
Wed, 23 Mar 2011 19:05:56 +0000 (12:05 -0700)
committerEric Anholt <eric@anholt.net>
Tue, 26 Apr 2011 19:20:02 +0000 (12:20 -0700)
Without this, consumers often have to keep linked lists of the
entries, at additional malloc cost.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/mesa/program/hash_table.c
src/mesa/program/hash_table.h

index f7ef366..877a9e2 100644 (file)
@@ -160,6 +160,25 @@ hash_table_remove(struct hash_table *ht, const void *key)
     }
 }
 
+void
+hash_table_call_foreach(struct hash_table *ht,
+                       void (*callback)(const void *key,
+                                        void *data,
+                                        void *closure),
+                       void *closure)
+{
+   int bucket;
+
+   for (bucket = 0; bucket < ht->num_buckets; bucket++) {
+      struct node *node, *temp;
+      foreach_s(node, temp, &ht->buckets[bucket]) {
+        struct hash_node *hn = (struct hash_node *) node;
+
+        callback(hn->key, hn->data, closure);
+      }
+   }
+}
+
 unsigned
 hash_table_string_hash(const void *key)
 {
index f1c4fdc..e715bb1 100644 (file)
@@ -144,6 +144,13 @@ hash_table_pointer_hash(const void *key);
 int
 hash_table_pointer_compare(const void *key1, const void *key2);
 
+void
+hash_table_call_foreach(struct hash_table *ht,
+                       void (*callback)(const void *key,
+                                        void *data,
+                                        void *closure),
+                       void *closure);
+
 #ifdef __cplusplus
 }
 #endif