forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_longest.hpp
More file actions
157 lines (125 loc) · 3.78 KB
/
Copy pathzip_longest.hpp
File metadata and controls
157 lines (125 loc) · 3.78 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
153
154
155
156
157
#ifndef ITER_ZIP_LONGEST_HPP_
#define ITER_ZIP_LONGEST_HPP_
#include "internal/iterbase.hpp"
#include <boost/optional.hpp>
#include <iterator>
#include <tuple>
#include <utility>
namespace iter {
namespace impl {
template <typename... RestContainers>
class ZippedLongest;
template <typename Container, typename... RestContainers>
class ZippedLongest<Container, RestContainers...>;
template <>
class ZippedLongest<>;
}
template <typename... Containers>
impl::ZippedLongest<Containers...> zip_longest(Containers&&...);
}
template <typename Container, typename... RestContainers>
class iter::impl::ZippedLongest<Container, RestContainers...> {
friend ZippedLongest zip_longest<Container, RestContainers...>(
Container&&, RestContainers&&...);
template <typename...>
friend class ZippedLongest;
private:
template <typename C>
using OptIterDeref = boost::optional<iterator_deref<C>>;
using OptType = OptIterDeref<Container>;
using ZipIterDeref = std::tuple<OptType, OptIterDeref<RestContainers>...>;
Container container;
ZippedLongest<RestContainers...> rest_zipped;
ZippedLongest(Container&& in_container, RestContainers&&... rest)
: container(std::forward<Container>(in_container)),
rest_zipped{std::forward<RestContainers>(rest)...} {}
public:
ZippedLongest(ZippedLongest&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, ZipIterDeref> {
private:
using RestIter = typename ZippedLongest<RestContainers...>::Iterator;
iterator_type<Container> iter;
iterator_type<Container> end;
RestIter rest_iter;
public:
Iterator(iterator_type<Container>&& it, iterator_type<Container>&& in_end,
RestIter&& rest)
: iter{std::move(it)},
end{std::move(in_end)},
rest_iter{std::move(rest)} {}
Iterator& operator++() {
if (this->iter != this->end) {
++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 || this->rest_iter != other.rest_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
ZipIterDeref operator*() {
if (this->iter != this->end) {
return std::tuple_cat(
std::tuple<OptType>{{*this->iter}}, *this->rest_iter);
}
return std::tuple_cat(std::tuple<OptType>{{}}, *this->rest_iter);
}
ArrowProxy<ZipIterDeref> operator->() {
return {**this};
}
};
Iterator begin() {
return {std::begin(this->container), std::end(this->container),
std::begin(this->rest_zipped)};
}
Iterator end() {
return {std::end(this->container), std::end(this->container),
std::end(this->rest_zipped)};
}
};
template <>
class iter::impl::ZippedLongest<> {
public:
ZippedLongest(ZippedLongest&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, std::tuple<>> {
public:
Iterator& operator++() {
return *this;
}
constexpr Iterator operator++(int) const {
return *this;
}
constexpr bool operator!=(const Iterator&) const {
return false;
}
constexpr bool operator==(const Iterator&) const {
return true;
}
constexpr std::tuple<> operator*() const {
return {};
}
constexpr ArrowProxy<std::tuple<>> operator->() const {
return {{}};
}
};
constexpr Iterator begin() const {
return {};
}
constexpr Iterator end() const {
return {};
}
};
template <typename... Containers>
iter::impl::ZippedLongest<Containers...> iter::zip_longest(
Containers&&... containers) {
return {std::forward<Containers>(containers)...};
}
#endif