forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.hpp
More file actions
152 lines (120 loc) · 3.71 KB
/
Copy pathzip.hpp
File metadata and controls
152 lines (120 loc) · 3.71 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
#ifndef ITER_ZIP_HPP_
#define ITER_ZIP_HPP_
#include "internal/iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
namespace iter {
namespace impl {
template <typename... RestContainers>
class Zipped;
template <typename Container, typename... RestContainers>
class Zipped<Container, RestContainers...>;
template <>
class Zipped<>;
}
template <typename... Containers>
impl::Zipped<Containers...> zip(Containers&&...);
}
// specialization for at least 1 template argument
template <typename Container, typename... RestContainers>
class iter::impl::Zipped<Container, RestContainers...> {
using ZipIterDeref =
std::tuple<iterator_deref<Container>, iterator_deref<RestContainers>...>;
friend Zipped iter::zip<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename...>
friend class Zipped;
private:
Container container;
Zipped<RestContainers...> rest_zipped;
Zipped(Container&& in_container, RestContainers&&... rest)
: container(std::forward<Container>(in_container)),
rest_zipped{std::forward<RestContainers>(rest)...} {}
public:
Zipped(Zipped&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, ZipIterDeref> {
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, RestIter&& rest)
: iter{std::move(it)}, rest_iter{std::move(rest)} {}
Iterator& operator++() {
++this->iter;
++this->rest_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);
}
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);
}
auto operator -> () -> ArrowProxy<decltype(**this)> {
return {**this};
}
};
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 iter::impl::Zipped<> {
public:
Zipped(Zipped&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, std::tuple<>> {
public:
constexpr static const bool is_base_iter = true;
Iterator& operator++() {
return *this;
}
Iterator operator++(int) {
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;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
std::tuple<> operator*() {
return std::tuple<>{};
}
auto operator -> () -> ArrowProxy<decltype(**this)> {
return {**this};
}
};
Iterator begin() {
return {};
}
Iterator end() {
return {};
}
};
template <typename... Containers>
iter::impl::Zipped<Containers...> iter::zip(Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
#endif