Added VG_(OSet_LookupWithCmp)(), which can be useful.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4444 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_oset.c b/coregrind/m_oset.c
index f3298d2..c3570ee 100644
--- a/coregrind/m_oset.c
+++ b/coregrind/m_oset.c
@@ -497,6 +497,21 @@
return ( n ? elem_of_node(n) : NULL );
}
+// Find the *element* in t matching k, or NULL if not found; use the given
+// comparison function rather than the standard one.
+void* VG_(OSet_LookupWithCmp)(AvlTree* t, void* k, OSetCmp_t cmp)
+{
+ // Save the normal one to the side, then restore once we're done.
+ void* e;
+ OSetCmp_t tmpcmp;
+ vg_assert(t);
+ tmpcmp = t->cmp;
+ t->cmp = cmp;
+ e = VG_(OSet_Lookup)(t, k);
+ t->cmp = tmpcmp;
+ return e;
+}
+
// Is there an element matching k?
Bool VG_(OSet_Contains)(AvlTree* t, void* k)
{
diff --git a/include/pub_tool_oset.h b/include/pub_tool_oset.h
index ed8e2de..435f679 100644
--- a/include/pub_tool_oset.h
+++ b/include/pub_tool_oset.h
@@ -117,6 +117,9 @@
// * Lookup: Returns a pointer to the element matching the key, if there is
// one, otherwise returns NULL.
//
+// * LookupWithCmp: Like Lookup, but you specify the comparison function,
+// which overrides the OSet's normal one.
+//
// * Insert: Inserts a new element into the list. Note that 'elem' must
// have been allocated using VG_(OSet_AllocNode)(), otherwise you will get
// assertion failures about "bad magic". Duplicates are forbidden, and
@@ -145,13 +148,14 @@
// they will return NULL if VG_(OSet_Next)() is called without an
// intervening call to VG_(OSet_ResetIter)().
-extern Int VG_(OSet_Size) ( OSet* os );
-extern void VG_(OSet_Insert) ( OSet* os, void* elem );
-extern Bool VG_(OSet_Contains) ( OSet* os, void* key );
-extern void* VG_(OSet_Lookup) ( OSet* os, void* key );
-extern void* VG_(OSet_Remove) ( OSet* os, void* key );
-extern void VG_(OSet_ResetIter) ( OSet* os );
-extern void* VG_(OSet_Next) ( OSet* os );
+extern Int VG_(OSet_Size) ( OSet* os );
+extern void VG_(OSet_Insert) ( OSet* os, void* elem );
+extern Bool VG_(OSet_Contains) ( OSet* os, void* key );
+extern void* VG_(OSet_Lookup) ( OSet* os, void* key );
+extern void* VG_(OSet_LookupWithCmp)( OSet* os, void* key, OSetCmp_t cmp );
+extern void* VG_(OSet_Remove) ( OSet* os, void* key );
+extern void VG_(OSet_ResetIter) ( OSet* os );
+extern void* VG_(OSet_Next) ( OSet* os );
#endif // __PUB_TOOL_OSET_H
diff --git a/memcheck/tests/oset_test.c b/memcheck/tests/oset_test.c
index aedd2aa..7868f2c 100644
--- a/memcheck/tests/oset_test.c
+++ b/memcheck/tests/oset_test.c
@@ -139,7 +139,7 @@
// Check we can find the remaining elements (with the right values).
for (i = 1; i < NN; i += 2) {
- assert( pv = VG_(OSet_Lookup)(oset1, vs[i]) );
+ assert( pv = VG_(OSet_LookupWithCmp)(oset1, vs[i], NULL) );
assert( pv == vs[i] );
}
@@ -286,6 +286,7 @@
a = vs[i]->first + 0; assert( vs[i] == VG_(OSet_Lookup)(oset2, &a) );
a = vs[i]->first + 1; assert( vs[i] == VG_(OSet_Lookup)(oset2, &a) );
a = vs[i]->first + 2; assert( vs[i] == VG_(OSet_Lookup)(oset2, &a) );
+ assert( vs[i] == VG_(OSet_LookupWithCmp)(oset2, &a, blockCmp) );
}
// Check that we can iterate over the OSet elements in sorted order, and