codex
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#include <algorithm>
2#include <iterator>
3#include <random>
4
5static std::random_device rnd;
6
7constexpr auto less_than(auto y) { return [y](auto x) { return x < y; }; }
8
9template<std::random_access_iterator I, std::sentinel_for<I> S>
10constexpr bool check_range(I b, S e) {
11 return 0 <= std::distance(b, e);
12}
13
14template<std::input_iterator I, std::sentinel_for<I> S>
15constexpr bool check_range(I b, S e) {
16 if constexpr(std::forward_iterator<I>)
17 std::distance(b, e);
18 return true;
19}
20
21template<std::input_iterator I, std::sentinel_for<I> S>
22constexpr bool contains(I b, S e, I i) {
23 return check_range(b, i) && check_range(i, e);
24}
25
26template<std::forward_iterator I, std::sentinel_for<I> S>
27I rand_it(I b, S e) {
28 using D = std::iter_difference_t<I>;
29 const D n = std::distance(b, e);
30 const D i = std::uniform_int_distribution<D>{0, n - 1}(rnd);
31 return std::next(b, i);
32}
33
34template<std::input_iterator I, std::sentinel_for<I> S>
35constexpr bool is_min_element(I b, S e, const auto &x) {
36 return std::all_of(b, e, [&x](const auto &y) { return !(y < x); });
37}
38
39template<std::forward_iterator I, std::sentinel_for<I> S>
40constexpr I min_element(I b, S e) {
41 assert(check_range(b, e));
42 if(b == e)
43 return b;
44 auto ret = b;
45 for(auto i = std::next(ret); i != e; ++i) {
46 assert(is_min_element(b, i, *ret));
47 if(*i < *ret)
48 ret = i;
49 }
50 assert(contains(b, e, ret));
51 assert(is_min_element(b, e, *ret));
52 return ret;
53}
54
55template<std::forward_iterator I, std::sentinel_for<I> S>
56I sort_element(I b, I i, S e) {
57 std::iter_swap(b, i);
58 const auto ret = std::partition(std::next(b), e, less_than(*b));
59 std::iter_swap(b, std::prev(ret));
60 return ret;
61}
static const struct board b
Definition chess0.c:57
#define y
Definition gcc14.c:2
#define x
Definition gcc14.c:1
Definition reflexpr.cpp:168
Definition mult_inh.c:26
I rand_it(I b, S e)
Definition utils.hpp:27
constexpr auto less_than(auto y)
Definition utils.hpp:7
constexpr bool is_min_element(I b, S e, const auto &x)
Definition utils.hpp:35
I sort_element(I b, I i, S e)
Definition utils.hpp:56
constexpr bool check_range(I b, S e)
Definition utils.hpp:10
constexpr I min_element(I b, S e)
Definition utils.hpp:40
static std::random_device rnd
Definition utils.hpp:5
constexpr bool contains(I b, S e, I i)
Definition utils.hpp:22