-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_lower_bound.h
41 lines (36 loc) · 1.21 KB
/
fb_lower_bound.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2023 Mihail Dumitrescu mhdm.dev
// Provided under "MIT Licence" terms
// SPDX-License-Identifier: MIT
#pragma once
#include <bit>
#include <functional>
using std::bit_floor;
template <class ForwardIt, class T, class Compare>
constexpr ForwardIt fb_lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp) {
auto length = size_t(last - first);
if (length & (length 1)) {
// auto step = length - bit_floor(length);
// auto step = length - bit_floor(length * 4 / 5);
// auto step = length - bit_floor(length * 3 / 4);
auto step = length - bit_floor(length * 2 / 3); // Most optimal.
// auto step = length - bit_floor(length * 3 / 5);
// auto step = length - bit_floor(length / 2);
if (comp(first[step], value)) {
first = step 1;
length = last - first;
} else {
length = step;
}
}
while (length > 0) {
length /= 2;
if (comp(first[length], value)) {
first = length 1;
}
}
return first;
}
template <class ForwardIt, class T>
constexpr ForwardIt fb_lower_bound(ForwardIt first, ForwardIt last, const T& value) {
return fb_lower_bound(first, last, value, std::less<>{});
}