Added an iterator to VgHashTable.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4407 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_hashtable.c b/coregrind/m_hashtable.c
index cbb8886..98d0413 100644
--- a/coregrind/m_hashtable.c
+++ b/coregrind/m_hashtable.c
@@ -41,7 +41,9 @@
struct _VgHashTable {
UInt n_chains; // should be prime
- VgHashNode* chains[0];
+ VgHashNode* iterNode; // current iterator node
+ UInt iterChain; // next chain to be traversed by the iterator
+ VgHashNode* chains[0]; // must be last field in the struct!
};
/*--------------------------------------------------------------------*/
@@ -203,6 +205,33 @@
}
}
+void VG_(HT_ResetIter)(VgHashTable table)
+{
+ vg_assert(table);
+ table->iterNode = NULL;
+ table->iterChain = 0;
+}
+
+void* VG_(HT_Next)(VgHashTable table)
+{
+ Int i;
+ vg_assert(table);
+
+ if (table->iterNode && table->iterNode->next) {
+ table->iterNode = table->iterNode->next;
+ return table->iterNode;
+ }
+
+ for (i = table->iterChain; i < table->n_chains; i++) {
+ if (table->chains[i]) {
+ table->iterNode = table->chains[i];
+ table->iterChain = i + 1; // Next chain to be traversed
+ return table->iterNode;
+ }
+ }
+ return NULL;
+}
+
void VG_(HT_destruct)(VgHashTable table)
{
UInt i;
diff --git a/include/pub_tool_hashtable.h b/include/pub_tool_hashtable.h
index 165fefd..e5e52d9 100644
--- a/include/pub_tool_hashtable.h
+++ b/include/pub_tool_hashtable.h
@@ -90,6 +90,14 @@
void (*f)(VgHashNode*, void*),
void* d );
+/* Reset the table's iterator to point to the first element. */
+extern void VG_(HT_ResetIter) ( VgHashTable table );
+
+/* Return the element pointed to by the iterator and move on to the next
+ one. Returns NULL if the last one has been passed, or if HT_ResetIter()
+ has not been called previously. */
+extern void* VG_(HT_Next) ( VgHashTable table );
+
/* Destroy a table. */
extern void VG_(HT_destruct) ( VgHashTable t );