1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:07:34 +00:00

AK: Deal with unsigned integers in binary search.

This commit is contained in:
asynts 2021-01-01 21:32:59 +01:00 committed by Andreas Kling
parent febc8a5ac7
commit e77031ce67
3 changed files with 32 additions and 6 deletions

View file

@ -32,11 +32,21 @@
namespace AK {
struct IntegralComparator {
constexpr auto operator()(auto& lhs, auto& rhs) { return lhs - rhs; }
struct DefaultComparator {
template<typename T, typename S>
constexpr int operator()(T& lhs, S& rhs)
{
if (lhs > rhs)
return 1;
if (lhs < rhs)
return -1;
return 0;
}
};
template<typename Container, typename Needle, typename Comparator = IntegralComparator>
template<typename Container, typename Needle, typename Comparator = DefaultComparator>
constexpr auto binary_search(
Container&& haystack,
Needle&& needle,
@ -52,8 +62,10 @@ constexpr auto binary_search(
size_t low = 0;
size_t high = haystack.size() - 1;
while (low <= high) {
size_t middle = low + ((high - low) / 2);
auto comparison = comparator(needle, haystack[middle]);
size_t middle = low + (high - low) / 2;
int comparison = comparator(needle, haystack[middle]);
if (comparison < 0)
if (middle != 0)
high = middle - 1;