-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstructiterator.hpp
200 lines (165 loc) · 3.85 KB
/
structiterator.hpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef GNR_STRUCTITERATOR_HPP
# define GNR_STRUCTITERATOR_HPP
# pragma once
#include <cstddef>
#include <iterator>
#include "boost/pfr.hpp"
namespace gnr
{
namespace detail::struct_iterator
{
template <class S>
constexpr auto all_same() noexcept
{
if constexpr (constexpr auto N(boost::pfr::tuple_size_v<S>); N > 1)
{
return [&]<auto ...I>(std::index_sequence<I...>) noexcept
{
return (
(
std::is_same_v<
boost::pfr::tuple_element_t<I, S>,
boost::pfr::tuple_element_t<I + 1, S>
>
) && ...
);
}(std::make_index_sequence<N - 1>());
}
else
{
return true;
}
}
template <class S>
static constexpr auto is_proper_v(std::is_class_v<S> && all_same<S>());
}
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
class struct_iterator
{
S& s_;
std::size_t i_;
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = decltype(&boost::pfr::get<0>(s_));
using reference = decltype(boost::pfr::get<0>(s_));
using value_type = std::remove_reference_t<reference>;
public:
constexpr explicit struct_iterator(S& s,
std::size_t const i = boost::pfr::tuple_size_v<S>) noexcept :
s_{s},
i_{i}
{
}
// increment, decrement
constexpr auto& operator++() noexcept { return ++i_, *this; }
constexpr auto& operator--() noexcept { return --i_, *this; }
constexpr auto operator++(int) const noexcept
{
return struct_iterator(s_, i_ + 1);
}
constexpr auto operator--(int) const noexcept
{
return struct_iterator(s_, i_ - 1);
}
// arithmetic
constexpr auto operator-(struct_iterator const other) const noexcept
{
return difference_type(i_ - other.i_);
}
constexpr auto operator+(std::size_t const N) const noexcept
{
return struct_iterator(s_, i_ + N);
}
constexpr auto operator-(std::size_t const N) const noexcept
{
return struct_iterator(s_, i_ - N);
}
// comparison
constexpr auto operator==(struct_iterator const other) const noexcept
{
return i_ == other.i_;
}
constexpr auto operator!=(struct_iterator const other) const noexcept
{
return !(*this == other);
}
constexpr auto operator<(struct_iterator const other) const noexcept
{
return i_ < other.i_;
}
// member access
constexpr auto& operator[](std::size_t const i) const noexcept
{
return *(*this + i);
}
constexpr auto& operator*() const noexcept
{
return [&]<auto ...I>(std::index_sequence<I...>) noexcept -> auto&
{
pointer r{};
(
(
r = I == i_ ? &boost::pfr::get<I>(s_) : r
),
...
);
return *r;
}(std::make_index_sequence<boost::pfr::tuple_size_v<S>>());
}
};
//
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
constexpr auto begin(S& s) noexcept
{
return struct_iterator{s, {}};
}
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
constexpr auto end(S& s) noexcept
{
return struct_iterator{s};
}
//
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
constexpr auto cbegin(S const& s) noexcept
{
return begin(s);
}
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
constexpr auto cend(S const& s) noexcept
{
return end(s);
}
//
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
constexpr auto size(S&) noexcept
{
return boost::pfr::tuple_size_v<S>;
}
//
template <typename S>
requires detail::struct_iterator::is_proper_v<S>
class range
{
S& s_;
public:
constexpr explicit range(S& s) noexcept : s_{s}
{
}
constexpr auto begin() const noexcept
{
return gnr::begin(s_);
}
constexpr auto end() const noexcept
{
return gnr::end(s_);
}
};
}
#endif // GNR_STRUCTITERATOR_HPP