diff --git a/mfbt/BinarySearch.h b/mfbt/BinarySearch.h index d8782694e7f6..870be6e6af7c 100644 --- a/mfbt/BinarySearch.h +++ b/mfbt/BinarySearch.h @@ -6,6 +6,7 @@ #define mozilla_BinarySearch_h #include +#include #include #include "mozilla/Assertions.h" @@ -32,8 +33,8 @@ namespace mozilla { * * The BinarySearchIf() version behaves similarly, but takes |aComparator|, a * 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 - * |int| with the comparison result: + * That functor should take one argument - the value to compare - and return a + * signed integer with the comparison result: * * * 0, if the argument is equal to, * * less than 0, if the argument is greater than, @@ -57,7 +58,8 @@ namespace mozilla { * * size_t match; * if (BinarySearchIf(sortedInts, 0, sortedInts.length(), Comparator(13), - * &match)) { printf("found 13 at %lu\n", match); + * &match)) { + * printf("found 13 at %lu\n", match); * } * */ @@ -76,6 +78,8 @@ bool BinarySearchIf(const Container& aContainer, size_t aBegin, size_t aEnd, // Allow any intermediate type so long as it provides a suitable ordering // relation. const auto result = aCompare(aContainer[middle]); + static_assert(!std::is_unsigned_v, + "comparator result must allow negative values"); if (result == 0) { *aMatchOrInsertionPoint = middle; @@ -174,6 +178,8 @@ size_t LowerBound(const Container& aContainer, size_t aBegin, size_t aEnd, // Allow any intermediate type so long as it provides a suitable ordering // relation. const auto result = aCompare(aContainer[middle]); + static_assert(!std::is_unsigned_v, + "comparator result must allow negative values"); // The range returning from LowerBound does include elements // equivalent to the given value i.e. aCompare(element) == 0 @@ -200,6 +206,8 @@ size_t UpperBound(const Container& aContainer, size_t aBegin, size_t aEnd, // Allow any intermediate type so long as it provides a suitable ordering // relation. const auto result = aCompare(aContainer[middle]); + static_assert(!std::is_unsigned_v, + "comparator result must allow negative values"); // The range returning from UpperBound does NOT include elements // equivalent to the given value i.e. aCompare(element) == 0 @@ -226,6 +234,8 @@ std::pair EqualRange(const Container& aContainer, size_t aBegin, // Allow any intermediate type so long as it provides a suitable ordering // relation. const auto result = aCompare(aContainer[middle]); + static_assert(!std::is_unsigned_v, + "comparator result must allow negative values"); if (result < 0) { high = middle;