forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.hpp
101 lines (79 loc) · 2.52 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
#ifndef ITER_ZIP_HPP_
#define ITER_ZIP_HPP_
#include "internal/iterbase.hpp"
#include <iterator>
#include <tuple>
#include <utility>
#include <algorithm>
namespace iter {
namespace impl {
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... Containers>
auto zip(Containers&&... containers);
}
template <typename TupleType, std::size_t... Is>
class iter::impl::Zipped {
private:
TupleType containers;
friend Zipped iter::impl::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:
Zipped(Zipped&&) = default;
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>
iter::impl::Zipped<TupleType, Is...> iter::impl::zip_impl(
TupleType&& in_containers, std::index_sequence<Is...>) {
return {std::move(in_containers)};
}
template <typename... Containers>
auto iter::zip(Containers&&... containers) {
return impl::zip_impl(
std::tuple<Containers...>{std::forward<Containers>(containers)...},
std::index_sequence_for<Containers...>{});
}
#endif