forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.hpp
134 lines (108 loc) · 4.26 KB
/
zip.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
#ifndef ITER_ZIP_HPP_
#define ITER_ZIP_HPP_
#include "iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
namespace iter {
template <typename... RestContainers>
class Zipped;
template <typename... Containers>
Zipped<Containers...> zip(Containers&&...);
// specialization for at least 1 template argument
template <typename Container, typename... RestContainers>
class Zipped <Container, RestContainers...> {
static_assert(!std::is_rvalue_reference<Container>::value,
"Itertools cannot be templated with rvalue references");
friend Zipped zip<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename... RC>
friend class Zipped;
private:
Container container;
Zipped<RestContainers...> rest_zipped;
Zipped(Container container, RestContainers&&... rest)
: container(std::forward<Container>(container)),
rest_zipped{std::forward<RestContainers>(rest)...}
{ }
public:
class Iterator {
private:
using RestIter =
typename Zipped<RestContainers...>::Iterator;
iterator_type<Container> iter;
RestIter rest_iter;
public:
constexpr static const bool is_base_iter = false;
Iterator(iterator_type<Container> it, const RestIter& rest)
: iter{it},
rest_iter{rest}
{ }
Iterator& operator++() {
++this->iter;
++this->rest_iter;
return *this;
}
bool operator!=(const Iterator& other) const {
return this->iter != other.iter &&
(RestIter::is_base_iter ||
this->rest_iter != other.rest_iter);
}
auto operator*() ->
decltype(std::tuple_cat(
std::tuple<iterator_deref<Container>>{
*this->iter},
*this->rest_iter))
{
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_zipped)};
}
Iterator end() {
return {std::end(this->container),
std::end(this->rest_zipped)};
}
};
template <>
class Zipped<> {
public:
class Iterator {
public:
constexpr static const bool is_base_iter = true;
Iterator() { }
Iterator(const Iterator&) { }
Iterator& operator=(const Iterator&) { return *this; }
Iterator& operator++() {
return *this;
}
// if this were to return true, there would be no need
// for the is_base_iter static class attribute.
// However, returning false causes an empty zip() call
// to reach the "end" immediately. Returning true here
// instead results in an infinite loop in the zip() case
bool operator!=(const Iterator&) const {
return false;
}
std::tuple<> operator*() {
return std::tuple<>{};
}
};
Iterator begin() {
return {};
}
Iterator end() {
return {};
}
};
template <typename... Containers>
Zipped<Containers...> zip(Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
}
#endif // #ifndef ITER_ZIP_HPP_