]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added VG_(OSet_LookupWithCmp)(), which can be useful.
authorNicholas Nethercote <njn@valgrind.org>
Wed, 17 Aug 2005 21:06:07 +0000 (21:06 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Wed, 17 Aug 2005 21:06:07 +0000 (21:06 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4444

coregrind/m_oset.c
include/pub_tool_oset.h
memcheck/tests/oset_test.c

index f3298d29b61e2a8a4c12e1c55899d83da569f696..c3570ee03633dfd953ad3c61862b17337b6bfa03 100644 (file)
@@ -497,6 +497,21 @@ void* VG_(OSet_Lookup)(AvlTree* t, void* k)
    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)
 {
index ed8e2de1b950ae4856a5ccf720085eabd411fc4a..435f67901365a428c46e5a1774d09ebeb8a9a57e 100644 (file)
@@ -117,6 +117,9 @@ extern void  VG_(OSet_FreeNode)  ( OSet* os, void* elem );
 // * 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 @@ extern void  VG_(OSet_FreeNode)  ( OSet* os, void* elem );
 //   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
 
index aedd2aaf9956f340ab66a2c63443348748b6fb04..7868f2c218a6a0f9b34cc6df195e90bfc3356cb4 100644 (file)
@@ -139,7 +139,7 @@ void example1(void)
 
    // 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 @@ void example2(void)
       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