This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.hpp
171 lines (137 loc) · 5.26 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
#ifndef ITER_PRODUCT_HPP_
#define ITER_PRODUCT_HPP_
#include "iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
#include <array>
namespace iter {
template <typename... RestContainers>
class Productor;
template <typename... Containers>
Productor<Containers...> product(Containers&&...);
// specialization for at least 1 template argument
template <typename Container, typename... RestContainers>
class Productor <Container, RestContainers...> {
static_assert(!std::is_rvalue_reference<Container>::value,
"Itertools cannot be templated with rvalue references");
friend Productor product<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename... RC>
friend class Productor;
using ProdIterDeref = std::tuple<
iterator_deref<Container>, iterator_deref<RestContainers>...>;
private:
Container container;
Productor<RestContainers...> rest_products;
Productor(Container&& in_container, RestContainers&&... rest)
: container(std::forward<Container>(in_container)),
rest_products{std::forward<RestContainers>(rest)...}
{ }
public:
class Iterator
: public std::iterator<std::input_iterator_tag, ProdIterDeref>
{
private:
using RestIter =
typename Productor<RestContainers...>::Iterator;
iterator_type<Container> iter;
iterator_type<Container> begin;
RestIter rest_iter;
RestIter rest_end;
public:
constexpr static const bool is_base_iter = false;
Iterator(const iterator_type<Container>& it,
RestIter&& rest,
RestIter&& in_rest_end)
: iter{it},
begin{it},
rest_iter{rest},
rest_end{in_rest_end}
{ }
void reset() {
this->iter = this->begin;
}
Iterator& operator++() {
++this->rest_iter;
if (!(this->rest_iter != this->rest_end)) {
this->rest_iter.reset();
++this->iter;
}
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);
}
};
Iterator begin() {
return {std::begin(this->container),
std::begin(this->rest_products),
std::end(this->rest_products)};
}
Iterator end() {
return {std::end(this->container),
std::end(this->rest_products),
std::end(this->rest_products)};
}
};
template <>
class Productor<> {
public:
class Iterator
: public std::iterator<std::input_iterator_tag, std::tuple<>>
{
public:
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 {};
}
};
Iterator begin() {
return {};
}
Iterator end() {
return {};
}
};
template <typename... Containers>
Productor<Containers...> product(Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
constexpr std::array<std::tuple<>, 1> product() {
return {{}};
}
}
#endif