From: Nicholas Nethercote Date: Wed, 17 Aug 2005 21:06:07 +0000 (+0000) Subject: Added VG_(OSet_LookupWithCmp)(), which can be useful. X-Git-Tag: svn/VALGRIND_3_1_0~574 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=64974a1033885cd9f2d7288d704e1264be5d4c64;p=thirdparty%2Fvalgrind.git Added VG_(OSet_LookupWithCmp)(), which can be useful. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4444 --- diff --git a/coregrind/m_oset.c b/coregrind/m_oset.c index f3298d29b6..c3570ee036 100644 --- a/coregrind/m_oset.c +++ b/coregrind/m_oset.c @@ -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) { diff --git a/include/pub_tool_oset.h b/include/pub_tool_oset.h index ed8e2de1b9..435f679013 100644 --- a/include/pub_tool_oset.h +++ b/include/pub_tool_oset.h @@ -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 diff --git a/memcheck/tests/oset_test.c b/memcheck/tests/oset_test.c index aedd2aaf99..7868f2c218 100644 --- a/memcheck/tests/oset_test.c +++ b/memcheck/tests/oset_test.c @@ -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