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
107 lines (83 loc) · 3.23 KB
/
Copy pathzip.hpp
File metadata and controls
107 lines (83 loc) · 3.23 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
#ifndef ITER_ZIP_HPP_
#define ITER_ZIP_HPP_
#include "iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
#include <algorithm>
namespace iter {
template <typename TupleType, std::size_t... Is>
class Zipped;
template <typename TupleType, std::size_t... Is>
Zipped<TupleType, Is...> zip_impl(TupleType&&, std::index_sequence<Is...>);
template <typename TupleType, std::size_t... Is>
class Zipped {
private:
TupleType containers;
friend Zipped zip_impl<TupleType, Is...>(
TupleType&&, std::index_sequence<Is...>);
using ZipIterDeref = iterator_deref_tuple<TupleType>;
Zipped(TupleType&& in_containers)
: containers(std::move(in_containers))
{ }
public:
class Iterator
: public std::iterator<std::input_iterator_tag, ZipIterDeref>
{
private:
iterator_tuple_type<TupleType> iters;
public:
Iterator(iterator_tuple_type<TupleType>&& its)
: iters(std::move(its))
{ }
Iterator& operator++() {
absorb(++std::get<Is>(this->iters)...);
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
if (sizeof...(Is) == 0) return false;
bool results[] = { true,
(std::get<Is>(this->iters) !=
std::get<Is>(other.iters))...
};
return std::all_of(
std::begin(results), std::end(results),
[](bool b){ return b; } );
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
ZipIterDeref operator*() {
return ZipIterDeref{(*std::get<Is>(this->iters))...};
}
auto operator->() -> ArrowProxy<decltype(**this)> {
return {**this};
}
};
Iterator begin() {
return {iterator_tuple_type<TupleType>{
std::begin(std::get<Is>(this->containers))...}};
}
Iterator end() {
return {iterator_tuple_type<TupleType>{
std::end(std::get<Is>(this->containers))...}};
}
};
template <typename TupleType, std::size_t... Is>
Zipped<TupleType, Is...> zip_impl(
TupleType&& in_containers, std::index_sequence<Is...>) {
return {std::move(in_containers)};
}
template <typename... Containers>
auto zip(Containers&&... containers) {
return zip_impl(std::tuple<Containers...>{
std::forward<Containers>(containers)...},
std::index_sequence_for<Containers...>{});
}
}
#endif