-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathiteratoriterator.hpp
More file actions
279 lines (219 loc) · 7.54 KB
/
iteratoriterator.hpp
File metadata and controls
279 lines (219 loc) · 7.54 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef ITERATOR_ITERATOR_HPP_
#define ITERATOR_ITERATOR_HPP_
#include <iterator>
#include <type_traits>
#include <utility>
#include "iterbase.hpp"
// IterIterWrapper and IteratorIterator provide a means to have a container
// of iterators act like a container of the pointed to objects. This is useful
// for combinatorics and similar itertools which need to keep track of
// more than one element at a time.
// an IterIterWrapper<some_collection_type<collection<T>::iterator>>
// behave like some_collection<T> when iterated over or indexed
namespace iter {
namespace impl {
template <typename T, typename = void>
struct HasConstDeref : std::false_type {};
template <typename T>
struct HasConstDeref<T, std::void_t<decltype(*std::declval<const T&>())>>
: std::true_type {};
template <typename TopIter>
class IteratorIterator {
template <typename>
friend class IteratorIterator;
using Diff = std::ptrdiff_t;
static_assert(
std::is_same<
typename std::iterator_traits<TopIter>::iterator_category,
std::random_access_iterator_tag>::value,
"IteratorIterator only works with random access iterators");
private:
TopIter sub_iter;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_cv_t<
std::remove_reference_t<decltype(**std::declval<TopIter>())>>;
using difference_type = std::ptrdiff_t;
using pointer =
std::remove_reference_t<decltype(**std::declval<TopIter>())>*;
using reference = std::add_lvalue_reference_t<
std::remove_reference_t<decltype(**std::declval<TopIter>())>>;
IteratorIterator() = default;
IteratorIterator(const TopIter& it) : sub_iter{it} {}
const TopIter& get() const {
return sub_iter;
}
template <typename T>
bool operator==(const IteratorIterator<T>& other) const {
return !(*this != other);
}
template <typename T>
bool operator!=(const IteratorIterator<T>& other) const {
return this->sub_iter != other.sub_iter;
}
IteratorIterator& operator++() {
++this->sub_iter;
return *this;
}
IteratorIterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
IteratorIterator& operator--() {
--this->sub_iter;
return *this;
}
IteratorIterator operator--(int) {
auto ret = *this;
--*this;
return ret;
}
auto operator*() const -> decltype(**sub_iter) {
return **this->sub_iter;
}
auto operator->() const -> decltype(*sub_iter) {
return *this->sub_iter;
}
IteratorIterator& operator+=(Diff n) {
this->sub_iter += n;
return *this;
}
IteratorIterator operator+(Diff n) const {
auto it = *this;
it += n;
return it;
}
friend IteratorIterator operator+(Diff n, IteratorIterator it) {
it += n;
return it;
}
IteratorIterator& operator-=(Diff n) {
this->sub_iter -= n;
return *this;
}
IteratorIterator operator-(Diff n) const {
auto it = *this;
it -= n;
return it;
}
Diff operator-(const IteratorIterator& rhs) const {
return this->sub_iter - rhs.sub_iter;
}
auto operator[](Diff idx) const -> decltype(*sub_iter[idx]) {
return *sub_iter[idx];
}
bool operator<(const IteratorIterator& rhs) const {
return this->sub_iter < rhs.sub_iter;
}
bool operator>(const IteratorIterator& rhs) const {
return this->sub_iter > rhs.sub_iter;
}
bool operator<=(const IteratorIterator& rhs) const {
return this->sub_iter <= rhs.sub_iter;
}
bool operator>=(const IteratorIterator& rhs) const {
return this->sub_iter >= rhs.sub_iter;
}
};
template <typename Container>
class IterIterWrapper {
private:
Container container;
using contained_iter = typename Container::value_type;
using size_type = typename Container::size_type;
using iterator = IteratorIterator<typename Container::iterator>;
using const_iterator =
IteratorIterator<typename Container::const_iterator>;
using reverse_iterator =
IteratorIterator<typename Container::reverse_iterator>;
using const_reverse_iterator =
IteratorIterator<typename Container::const_reverse_iterator>;
template <typename U = Container, typename = void>
struct ConstAtTypeOrVoid : type_is<void> {};
template <typename U>
struct ConstAtTypeOrVoid<U,
std::void_t<decltype(*std::declval<const U&>().at(0))>>
: type_is<decltype(*std::declval<const U&>().at(0))> {};
using const_at_type_or_void_t = typename ConstAtTypeOrVoid<>::type;
template <typename U = Container, typename = void>
struct ConstIndexTypeOrVoid : type_is<void> {};
template <typename U>
struct ConstIndexTypeOrVoid<U,
std::void_t<decltype(*std::declval<const U&>()[0])>>
: type_is<decltype(*std::declval<const U&>()[0])> {};
using const_index_type_or_void_t = typename ConstIndexTypeOrVoid<>::type;
public:
IterIterWrapper() = default;
explicit IterIterWrapper(size_type sz) : container(sz) {}
IterIterWrapper(size_type sz, const contained_iter& val)
: container(sz, val) {}
auto at(size_type pos) -> decltype(*container.at(pos)) {
return *container.at(pos);
}
auto at(size_type pos) const -> const_at_type_or_void_t {
return *container.at(pos);
}
auto operator[](size_type pos) noexcept(noexcept(*container[pos]))
-> decltype(*container[pos]) {
return *container[pos];
}
auto operator[](size_type pos) const noexcept(noexcept(*container[pos]))
-> const_index_type_or_void_t {
return *container[pos];
}
bool empty() const noexcept {
return container.empty();
}
size_type size() const noexcept {
return container.size();
}
iterator begin() noexcept {
return {container.begin()};
}
iterator end() noexcept {
return {container.end()};
}
const_iterator begin() const noexcept {
return {container.begin()};
}
const_iterator end() const noexcept {
return {container.end()};
}
const_iterator cbegin() const noexcept {
return {container.cbegin()};
}
const_iterator cend() const noexcept {
return {container.cend()};
}
reverse_iterator rbegin() noexcept {
return {container.rbegin()};
}
reverse_iterator rend() noexcept {
return {container.rend()};
}
const_reverse_iterator rbegin() const noexcept {
return {container.rbegin()};
}
const_reverse_iterator rend() const noexcept {
return {container.rend()};
}
const_reverse_iterator crbegin() const noexcept {
return {container.rbegin()};
}
const_reverse_iterator crend() const noexcept {
return {container.rend()};
}
// get() exposes the underlying container. this allows the
// itertools to manipulate the iterators in the container
// and should not be depended on anywhere else.
Container& get() noexcept {
return container;
}
const Container& get() const noexcept {
return container;
}
};
}
}
#endif