forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixed_product.hpp
285 lines (233 loc) · 6.83 KB
/
mixed_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
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
280
281
282
283
284
285
#ifndef ITER_MIXED_PRODUCT_HPP_
#define ITER_MIXED_PRODUCT_HPP_
#include "internal/iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
#include <array>
/* If the set's powers are co-prime, we can increment all iterators in
* the tuple given correct Cartesian project. The algorithm adds
* additional fake elements to the sets and iterates over them.
*
* Example:
* {1, 2}
* {1, 2, 3, 4}
* adding fake elements so the powers are co-prime:
* {1, 2}
* {1, 2, 3, 4, [5]}
*
* mixed product:
* (1, 1)
* (2, 2)
* (1, 3)
* (2, 4)
* (1, [5]) <- fake (skipping)
* (2, 1)
* (1, 2)
* (2, 3)
* (1, 4)
* */
namespace iter {
namespace impl {
template <typename... Containers>
class MixedProductor;
template <typename Container, typename... RestContainers>
class MixedProductor<Container, RestContainers...>;
template <>
class MixedProductor<>;
}
template <typename... Containers>
impl::MixedProductor<Containers...> mixed_product(Containers&&...);
}
// specialization for at least 1 template argument
template <typename Container, typename... RestContainers>
class iter::impl::MixedProductor<Container, RestContainers...> {
using bigint_t = typename MixedProductor<RestContainers...>::bigint_t;
friend MixedProductor iter::mixed_product<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename... RC>
friend class MixedProductor;
using ProdIterDeref =
std::tuple<iterator_deref<Container>, iterator_deref<RestContainers>...>;
private:
Container container;
MixedProductor<RestContainers...> rest_products;
MixedProductor(Container&& in_container, RestContainers&&... rest)
: container(std::forward<Container>(in_container)),
rest_products{std::forward<RestContainers>(rest)...} {}
// calculate fake size for current container and store total fake size
void set_size(bigint_t size) {
if (size == 0)
return;
this->size_ = size;
this->fakesize_ = size;
if (this->total_fakesize() != 0)
{
while (gcd(this->fakesize_, this->total_fakesize()) != 1)
++(this->fakesize_);
this->total_fakesize(this->fakesize_ * this->total_fakesize());
}
else
{
this->total_fakesize(this->fakesize_);
}
}
static bigint_t gcd(bigint_t a, bigint_t b) {
return (!b ? a : gcd(b, a%b));
}
bigint_t size_{0};
bigint_t fakesize_{0};
public:
MixedProductor(MixedProductor&&) = default;
bigint_t total_fakesize(bigint_t fsz = 0) {
return rest_products.total_fakesize(fsz);
}
class Iterator
: public std::iterator<std::input_iterator_tag, ProdIterDeref> {
private:
using RestIter = typename MixedProductor<RestContainers...>::Iterator;
iterator_type<Container> iter;
iterator_type<Container> begin;
iterator_type<Container> end;
RestIter rest_iter;
MixedProductor<Container, RestContainers...>* owner;
bigint_t counter;
bool is_fake() const {
if (counter < owner->size_ || owner->fakesize_ == 0)
return false;
return counter < owner->fakesize_ ? true : false;
}
public:
bigint_t global_counter() const {
return this->rest_iter.global_counter();
}
constexpr static const bool is_base_iter = false;
Iterator(const iterator_type<Container>& it, RestIter&& rest,
const iterator_type<Container>& end, MixedProductor<Container, RestContainers...>* owner)
: iter{it}, begin{it}, end{end}, rest_iter{rest}, owner{owner}, counter{0} {}
friend class MixedProductor<Container, RestContainers...>;
// returns true if there is any iterator in fake state
bool do_increment() {
if (!this->is_fake())
++this->iter;
++this->counter;
if (this->iter == this->end)
{
if (this->counter > 0 && this->owner->fakesize_ == 0)
this->owner->set_size(this->counter);
if (!this->is_fake())
{
this->iter = this->begin;
this->counter = 0;
return this->rest_iter.do_increment();
}
this->rest_iter.do_increment();
return true;
}
return this->rest_iter.do_increment();
}
Iterator& operator++() {
bool is_fake = false;
do {
is_fake = this->do_increment();
if (this->all_sizes_discovered()
&& this->global_counter() == this->owner->total_fakesize())
{
this->operator=(this->owner->end());
break;
}
} while(is_fake);
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->iter != other.iter
&& (RestIter::is_base_iter || this->rest_iter != other.rest_iter);
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
ProdIterDeref operator*() {
return std::tuple_cat(
std::tuple<iterator_deref<Container>>{*this->iter}, *this->rest_iter);
}
ArrowProxy<ProdIterDeref> operator->() {
return {**this};
}
bool all_sizes_discovered() const {
return owner->fakesize_ != 0 && rest_iter.all_sizes_discovered();
}
};
friend Iterator;
Iterator begin() {
return {std::begin(this->container), std::begin(this->rest_products),
std::end(this->container), this};
}
Iterator end() {
return {std::end(this->container), std::end(this->rest_products),
std::end(this->container), this};
}
};
template <>
class iter::impl::MixedProductor<> {
public:
using bigint_t = size_t;
MixedProductor() = default;
MixedProductor(MixedProductor&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, std::tuple<>> {
public:
constexpr static const bool is_base_iter = true;
bool is_fake() const {
return false;
}
bool all_sizes_discovered() const {
return true;
}
// 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 {};
}
bigint_t global_counter() const {
return counter;
}
bool do_increment() {
++counter;
return false;
}
private:
bigint_t counter{0};
};
Iterator begin() {
return {};
}
Iterator end() {
return {};
}
bigint_t total_fakesize(const bigint_t fsz = 0) {
if (fsz != 0)
total_fakesize_ = fsz;
return total_fakesize_;
}
private:
bigint_t total_fakesize_{0};
};
template <typename... Containers>
iter::impl::MixedProductor<Containers...> iter::mixed_product(Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
namespace iter {
constexpr std::array<std::tuple<>, 1> mixed_product() {
return {{}};
}
}
#endif