forked from PacktPublishing/Software-Architecture-with-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_niebloids.cpp
43 lines (38 loc) · 1.41 KB
/
05_niebloids.cpp
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
42
43
#include <algorithm>
#include <deque>
#include <functional>
#include <gsl/pointers>
#include <iostream>
#include <ostream>
#include <ranges>
namespace my_ranges {
namespace detail {
struct contains_fn final {
template <std::input_iterator It, std::sentinel_for<It> Sent, typename T,
typename Proj = std::identity>
requires std::indirect_binary_predicate<std::ranges::equal_to,
std::projected<It, Proj>, const T *>
constexpr bool operator()(It first, Sent last, const T &value,
Proj projection = {}) const {
while (first != last && std::invoke(projection, *first) != value) ++first;
return first != last;
}
template <std::ranges::input_range Range, typename T,
typename Proj = std::identity>
requires std::indirect_binary_predicate<
std::ranges::equal_to,
std::projected<std::ranges::iterator_t<Range>, Proj>, const T *>
constexpr bool operator()(Range &&range, const T &value,
Proj projection = {}) const {
return (*this)(std::ranges::begin(range), std::ranges::end(range), value,
std::move(projection));
}
};
} // namespace detail
inline constexpr detail::contains_fn contains{};
} // namespace my_ranges
int main() {
constexpr auto ints =
std::ranges::views::iota(0) | std::ranges::views::take(5);
return my_ranges::contains(ints, 42);
}