static int comparePositions(stIntTuple *position1, stIntTuple *position2) { if(stIntTuple_get(position1, 0) == INT64_MAX || stIntTuple_get(position2, 0) == INT64_MAX) { //Indicates we should ignore the first position and compare the second. assert(stIntTuple_get(position1, 1) != INT64_MAX); assert(stIntTuple_get(position2, 1) != INT64_MAX); return cmpFn(stIntTuple_get(position1, 1), stIntTuple_get(position2, 1)); } return cmpFn(stIntTuple_get(position1, 0), stIntTuple_get(position2, 0)); }
int stSortedSet_equals(stSortedSet *sortedSet1, stSortedSet *sortedSet2) { if(stSortedSet_size(sortedSet1) != stSortedSet_size(sortedSet2)) { return 0; } if(!stSortedSet_comparatorsEqual(sortedSet1, sortedSet2)) { return 0; } int (*cmpFn)(const void *, const void *) = stSortedSet_getComparator(sortedSet1)->compareFn; stSortedSetIterator *it1 = stSortedSet_getIterator(sortedSet1); stSortedSetIterator *it2 = stSortedSet_getIterator(sortedSet2); void *o1 = stSortedSet_getNext(it1), *o2 = stSortedSet_getNext(it2); while(o1 != NULL && o2 != NULL) { if(cmpFn(o1, o2) != 0) { stSortedSet_destructIterator(it1); stSortedSet_destructIterator(it2); return 0; } o1 = stSortedSet_getNext(it1); o2 = stSortedSet_getNext(it2); } stSortedSet_destructIterator(it1); stSortedSet_destructIterator(it2); return 1; }
Type FindMax(Vector<Type> &v, int (cmpFn)(Type, Type)) { Type max = v[0]; for (int i = 1; i < v.size(); i++) { if (cmpFn(max,v[i]) == -1) max = v[i]; } return max; }
bool Set<ElemType>::equals(Set & otherSet) { if (cmpFn != otherSet.cmpFn) { Error("Equals: sets have different comparison functions"); } Iterator thisItr = iterator(), otherItr = otherSet.iterator(); while (thisItr.hasNext() && otherItr.hasNext()) { if (cmpFn(thisItr.next(), otherItr.next()) != 0) return false; } return !thisItr.hasNext() && !otherItr.hasNext(); }
Bool VG_(lookupXA_UNSAFE) ( XArray* xao, const void* key, /*OUT*/Word* first, /*OUT*/Word* last, Int(*cmpFn)(const void*, const void*) ) { Word lo, mid, hi, cres; void* midv; struct _XArray* xa = (struct _XArray*)xao; vg_assert(xa); lo = 0; hi = xa->usedsizeE-1; while (True) { /* current unsearched space is from lo to hi, inclusive. */ if (lo > hi) return False; /* not found */ mid = (lo + hi) / 2; midv = VG_(indexXA)( xa, mid ); cres = cmpFn( key, midv ); if (cres < 0) { hi = mid-1; continue; } if (cres > 0) { lo = mid+1; continue; } /* Found it, at mid. See how far we can expand this. */ vg_assert(cmpFn( key, VG_(indexXA)(xa, lo) ) >= 0); vg_assert(cmpFn( key, VG_(indexXA)(xa, hi) ) <= 0); if (first) { *first = mid; while (*first > 0 && 0 == cmpFn( key, VG_(indexXA)(xa, (*first)-1))) { (*first)--; } } if (last) { *last = mid; while (*last < xa->usedsizeE-1 && 0 == cmpFn( key, VG_(indexXA)(xa, (*last)+1))) { (*last)++; } } return True; } }