1 |
|
|
#pragma once |
2 |
|
|
|
3 |
|
|
#include <concepts> |
4 |
|
|
#include <iterator> // for iterator_traits, begin, end |
5 |
|
|
#include <optional> |
6 |
|
|
#include <utility> // for forward, move |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
#include "meta.hpp" |
10 |
|
|
|
11 |
|
|
using namespace meta; |
12 |
|
|
|
13 |
|
|
namespace next { |
14 |
|
|
|
15 |
|
|
#pragma region next interface |
16 |
|
|
|
17 |
|
|
// This interface class is used in CRTP |
18 |
|
|
template <typename Range> struct next_interface : public crtp<Range> { |
19 |
|
|
|
20 |
|
|
struct iterator { |
21 |
|
|
using T = typename Range::value_type; |
22 |
|
|
|
23 |
|
|
Range ⦥ |
24 |
|
|
std::optional<T> current; |
25 |
|
|
|
26 |
|
80 |
iterator(Range &range, std::optional<T> value) |
27 |
|
80 |
: range(range), current(value) {} |
28 |
|
|
|
29 |
|
206 |
iterator &operator++() { |
30 |
|
206 |
current = range.next(); |
31 |
|
206 |
return *this; |
32 |
|
|
} |
33 |
|
|
|
34 |
|
246 |
bool operator==(iterator const &other) const { |
35 |
|
246 |
return current == other.current; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
246 |
bool operator!=(iterator const &other) const { return !(*this == other); } |
39 |
|
|
|
40 |
|
206 |
T const &operator*() const { return *current; } |
41 |
|
|
|
42 |
|
|
T const *operator->() const { return std::addressof(*current); } |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
40 |
constexpr iterator begin() { |
46 |
|
40 |
this->underlying().initialize(); |
47 |
|
40 |
return {this->underlying(), std::optional(this->underlying().next())}; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
40 |
constexpr iterator end() { return {this->underlying(), std::nullopt}; } |
51 |
|
|
}; |
52 |
|
|
|
53 |
|
|
template <typename T> |
54 |
|
|
concept InputRange = std::derived_from< |
55 |
|
|
std::remove_reference_t<T>, next_interface<std::remove_reference_t<T>>> && |
56 |
|
|
requires(T t) { |
57 |
|
|
t.initialize(); |
58 |
|
|
t.next(); |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
template <typename R> |
62 |
|
|
concept Iterable = requires(R r) { |
63 |
|
|
std::begin(r); |
64 |
|
|
std::end(r); |
65 |
|
|
}; |
66 |
|
|
|
67 |
|
|
template <InputRange Range> using value_t = typename Range::value_type; |
68 |
|
|
|
69 |
|
|
#pragma endregion |
70 |
|
|
|
71 |
|
|
} // namespace next |