Differential D3094 Diff 14018 ps/trunk/libraries/source/spidermonkey/include-win32-debug/mozilla/BinarySearch.h
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/libraries/source/spidermonkey/include-win32-debug/mozilla/BinarySearch.h
Show All 25 Lines | |||||
* | * | ||||
* Vector<int> sortedInts = ... | * Vector<int> sortedInts = ... | ||||
* | * | ||||
* size_t match; | * size_t match; | ||||
* if (BinarySearch(sortedInts, 0, sortedInts.length(), 13, &match)) { | * if (BinarySearch(sortedInts, 0, sortedInts.length(), 13, &match)) { | ||||
* printf("found 13 at %lu\n", match); | * printf("found 13 at %lu\n", match); | ||||
* } | * } | ||||
* | * | ||||
* The BinarySearchIf() version behaves similar, but takes |aComparator|, a | * The BinarySearchIf() version behaves similarly, but takes |aComparator|, a | ||||
* functor to compare the values with, instead of a value to find. | * functor to compare the values with, instead of a value to find. | ||||
* That functor should take one argument - the value to compare - and return an | * That functor should take one argument - the value to compare - and return an | ||||
* |int| with the comparison result: | * |int| with the comparison result: | ||||
* | * | ||||
* * 0, if the argument is equal to, | * * 0, if the argument is equal to, | ||||
* * less than 0, if the argument is greater than, | * * less than 0, if the argument is greater than, | ||||
* * greater than 0, if the argument is less than | * * greater than 0, if the argument is less than | ||||
* | * | ||||
* the value. | * the value. | ||||
* | * | ||||
* Example: | * Example: | ||||
* | * | ||||
* struct Comparator { | * struct Comparator { | ||||
* int operator()(int val) const { | * int operator()(int aVal) const { | ||||
* if (mTarget < val) return -1; | * if (mTarget < aVal) { return -1; } | ||||
* if (mValue > val) return 1; | * if (mTarget > aVal) { return 1; } | ||||
* return 0; | * return 0; | ||||
* } | * } | ||||
* Comparator(int target) : mTarget(target) {} | * explicit Comparator(int aTarget) : mTarget(aTarget) {} | ||||
const int mTarget; | * const int mTarget; | ||||
* }; | * }; | ||||
* | * | ||||
* Vector<int> sortedInts = ... | * Vector<int> sortedInts = ... | ||||
* | * | ||||
* size_t match; | * size_t match; | ||||
* if (BinarySearchIf(sortedInts, 0, sortedInts.length(), Comparator(13), &match)) { | * if (BinarySearchIf(sortedInts, 0, sortedInts.length(), Comparator(13), &match)) { | ||||
* printf("found 13 at %lu\n", match); | * printf("found 13 at %lu\n", match); | ||||
* } | * } | ||||
Show All 38 Lines | |||||
class BinarySearchDefaultComparator | class BinarySearchDefaultComparator | ||||
{ | { | ||||
public: | public: | ||||
explicit BinarySearchDefaultComparator(const T& aTarget) | explicit BinarySearchDefaultComparator(const T& aTarget) | ||||
: mTarget(aTarget) | : mTarget(aTarget) | ||||
{} | {} | ||||
template <class U> | template <class U> | ||||
int operator()(const U& val) const { | int operator()(const U& aVal) const { | ||||
if (mTarget == val) { | if (mTarget == aVal) { | ||||
return 0; | return 0; | ||||
} | } | ||||
if (mTarget < val) { | if (mTarget < aVal) { | ||||
return -1; | return -1; | ||||
} | } | ||||
return 1; | return 1; | ||||
} | } | ||||
private: | private: | ||||
const T& mTarget; | const T& mTarget; | ||||
Show All 17 Lines |
Wildfire Games · Phabricator