codex
Loading...
Searching...
No Matches
bsearch.hpp
Go to the documentation of this file.
1#include <algorithm>
2#include <cassert>
3#include <cstddef>
4#include <iterator>
5#include <ranges>
6
7namespace codex {
8
9template<std::random_access_iterator I, std::sentinel_for<I> S>
10I lower_bound(I b, S e, const auto &x) {
11 assert(std::is_sorted(b, e));
12 [[maybe_unused]] const I ib = b;
13 [[maybe_unused]] const S ie = e;
14 while(b != e) {
15 assert(ib <= b && b <= e && e <= ie);
16 assert(std::find(ib, b, x) == b);
17 assert(b == ib || b[-1] < x);
18 assert(e == ie || !(*e < x));
19 const I m = b + static_cast<std::size_t>(e - b) / 2;
20 assert(b <= m && m < e);
21 if(*m < x)
22 b = m + 1;
23 else
24 e = m;
25 }
26 assert(ib <= b && b <= ie);
27 assert(b == ie || !(*b < x));
28 assert(b == ib || b[-1] < x);
29 assert(b == ie || *b == x || std::find(ib, ie, x) == ie);
30 return b;
31}
32
33template<std::random_access_iterator I, std::sentinel_for<I> S>
34bool bsearch(I b, S e, const auto &x) {
35 b = lower_bound(b, e, x);
36 return b != e && !(x < *b);
37}
38
39}
static const struct board b
Definition chess0.c:57
#define x
Definition gcc14.c:1
Definition rotate.hpp:6
bool bsearch(I b, S e, const auto &x)
Definition bsearch.hpp:34
I lower_bound(I b, S e, const auto &x)
Definition bsearch.hpp:10
#define m(a)
Definition std2.c:8
Definition reflexpr.cpp:168
Definition mult_inh.c:26