aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornjn <njn@a5019735-40e9-0310-863c-91ae7b9d1cf9>2005-08-14 17:42:35 +0000
committernjn <njn@a5019735-40e9-0310-863c-91ae7b9d1cf9>2005-08-14 17:42:35 +0000
commit1af7972c4bfecc5254556915ce011002a26a69a5 (patch)
treeb277f2d5ce78e08c9b204de4f08ee6ba56269be6
parent4b70e8c73ee9d28ece852b781457f0b19359e513 (diff)
downloadvalgrind-1af7972c4bfecc5254556915ce011002a26a69a5.tar.gz
Added an iterator to VgHashTable.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4407 a5019735-40e9-0310-863c-91ae7b9d1cf9
-rw-r--r--coregrind/m_hashtable.c31
-rw-r--r--include/pub_tool_hashtable.h8
2 files changed, 38 insertions, 1 deletions
diff --git a/coregrind/m_hashtable.c b/coregrind/m_hashtable.c
index cbb888639..98d041355 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_apply_to_all_nodes)( VgHashTable table,
}
}
+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 165fefdf8..e5e52d98c 100644
--- a/include/pub_tool_hashtable.h
+++ b/include/pub_tool_hashtable.h
@@ -90,6 +90,14 @@ extern void VG_(HT_apply_to_all_nodes)( VgHashTable t,
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 );