forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.hpp
243 lines (197 loc) · 6.09 KB
/
product.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
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
#ifndef ITER_PRODUCT_HPP_
#define ITER_PRODUCT_HPP_
#include "internal/iterator_wrapper.hpp"
#include "internal/iterbase.hpp"
#include <array>
#include <iterator>
#include <tuple>
#include <utility>
namespace iter {
namespace impl {
template <typename... Containers>
class Productor;
template <typename Container, typename... RestContainers>
class Productor<Container, RestContainers...>;
template <>
class Productor<>;
}
template <typename... Containers>
impl::Productor<Containers...> product(Containers&&...);
}
// specialization for at least 1 template argument
template <typename Container, typename... RestContainers>
class iter::impl::Productor<Container, RestContainers...> {
friend Productor iter::product<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename... RC>
friend class Productor;
template <typename T>
using ProdIterDeref =
std::tuple<iterator_deref<T>, iterator_deref<RestContainers>...>;
private:
Container container_;
Productor<RestContainers...> rest_products_;
Productor(Container&& container, RestContainers&&... rest)
: container_(std::forward<Container>(container)),
rest_products_{std::forward<RestContainers>(rest)...} {}
public:
Productor(Productor&&) = default;
private:
template <typename ContainerT, typename RestIter>
class IteratorTempl {
private:
template <typename, typename>
friend class IteratorTempl;
IteratorWrapper<ContainerT> sub_iter_;
IteratorWrapper<ContainerT> sub_begin_;
RestIter rest_iter_;
RestIter rest_end_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = ProdIterDeref<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
constexpr static const bool is_base_iter = false;
IteratorTempl(IteratorWrapper<ContainerT>&& sub_iter, RestIter&& rest_iter,
RestIter&& rest_end)
: sub_iter_{sub_iter},
sub_begin_{sub_iter},
rest_iter_{rest_iter},
rest_end_{rest_end} {}
void reset() {
sub_iter_ = sub_begin_;
}
IteratorTempl& operator++() {
++rest_iter_;
if (!(rest_iter_ != rest_end_)) {
rest_iter_.reset();
++sub_iter_;
}
return *this;
}
IteratorTempl operator++(int) {
auto ret = *this;
++*this;
return ret;
}
template <typename T, typename U>
bool operator!=(const IteratorTempl<T, U>& other) const {
return sub_iter_ != other.sub_iter_
&& (RestIter::is_base_iter || rest_iter_ != other.rest_iter_);
}
template <typename T, typename U>
bool operator==(const IteratorTempl<T, U>& other) const {
return !(*this != other);
}
ProdIterDeref<ContainerT> operator*() {
return std::tuple_cat(
std::tuple<iterator_deref<ContainerT>>{*sub_iter_}, *rest_iter_);
}
ArrowProxy<ProdIterDeref<ContainerT>> operator->() {
return {**this};
}
};
using RestIter = typename Productor<RestContainers...>::Iterator;
using RestConstIter = typename Productor<RestContainers...>::ConstIterator;
public:
using Iterator = IteratorTempl<Container, RestIter>;
using ConstIterator = IteratorTempl<AsConst<Container>, RestConstIter>;
Iterator begin() {
return {get_begin(container_), get_begin(rest_products_),
get_end(rest_products_)};
}
Iterator end() {
return {
get_end(container_), get_end(rest_products_), get_end(rest_products_)};
}
ConstIterator begin() const {
return {get_begin(std::as_const(container_)),
get_begin(std::as_const(rest_products_)),
get_end(std::as_const(rest_products_))};
}
ConstIterator end() const {
return {get_end(std::as_const(container_)),
get_end(std::as_const(rest_products_)),
get_end(std::as_const(rest_products_))};
}
};
template <>
class iter::impl::Productor<> {
public:
Productor(Productor&&) = default;
class Iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = std::tuple<>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
constexpr static const bool is_base_iter = true;
void reset() {}
Iterator& operator++() {
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
// see note in zip about base case operator!=
bool operator!=(const Iterator&) const {
return false;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
std::tuple<> operator*() const {
return {};
}
};
using ConstIterator = Iterator;
Iterator begin() {
return {};
}
Iterator end() {
return {};
}
ConstIterator begin() const {
return {};
}
ConstIterator end() const {
return {};
}
};
template <typename... Containers>
iter::impl::Productor<Containers...> iter::product(Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
namespace iter {
namespace impl {
// rvalue must be copied, lvalue and const lvalue references can be bound
template <std::size_t... Is, typename Container>
decltype(auto) product_repeat(
std::index_sequence<Is...>, Container&& container) {
return product(((void)Is, Container(container))...);
}
template <std::size_t... Is, typename Container>
decltype(auto) product_repeat(
std::index_sequence<Is...>, Container& container) {
return product(((void)Is, container)...);
}
template <std::size_t... Is, typename Container>
decltype(auto) product_repeat(
std::index_sequence<Is...>, const Container& container) {
return product(((void)Is, container)...);
}
}
template <std::size_t N, typename Container>
decltype(auto) product(Container&& container) {
return impl::product_repeat(
std::make_index_sequence<N>{}, std::forward<Container>(container));
}
constexpr std::array<std::tuple<>, 1> product() {
return {{}};
}
}
#endif