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

BinarySearch: constexpr support

Problem:
- It is not possible to perform a binary search at compile-time
  because `binary_search` is not `constexpr`-aware.

Solution:
- Add `constexpr` support.
This commit is contained in:
Lenny Maiorani 2020-10-13 18:30:35 -04:00 committed by Andreas Kling
parent 0658051996
commit a274a8e5a0
2 changed files with 17 additions and 2 deletions

View file

@ -33,13 +33,13 @@
namespace AK {
template<typename T>
int integral_compare(const typename RemoveConst<T>::Type& a, const typename RemoveConst<T>::Type& b)
constexpr int integral_compare(const typename RemoveConst<T>::Type& a, const typename RemoveConst<T>::Type& b)
{
return a - b;
}
template<typename T, typename Compare>
T* binary_search(Span<T> haystack, const typename RemoveConst<T>::Type& needle, Compare compare = integral_compare, size_t* nearby_index = nullptr)
constexpr T* binary_search(Span<T> haystack, const typename RemoveConst<T>::Type& needle, Compare compare = integral_compare, size_t* nearby_index = nullptr)
{
if (haystack.size() == 0) {
if (nearby_index)