forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimap.hpp
More file actions
143 lines (117 loc) · 4.29 KB
/
Copy pathimap.hpp
File metadata and controls
143 lines (117 loc) · 4.29 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
#ifndef ITER_IMAP_H_
#define ITER_IMAP_H_
#include "zip.hpp"
#include <utility>
#include <type_traits>
#include <tuple>
namespace iter {
namespace detail {
template <std::size_t Index, typename Functor, typename Tup>
struct Expander {
template <typename... Ts>
static auto call(Functor&& f, Tup&& tup, Ts&&... args)
-> decltype(Expander<Index - 1, Functor, Tup>::call(
std::forward<Functor>(f), std::forward<Tup>(tup),
std::forward<typename std::tuple_element<Index - 1,
typename std::remove_reference<Tup>::type>::type>(
std::get<Index - 1>(tup)),
std::forward<Ts>(args)...)) {
// recurse
return Expander<Index - 1, Functor, Tup>::call(
std::forward<Functor>(f), std::forward<Tup>(tup),
// pull out one element
std::forward<typename std::tuple_element<Index - 1,
typename std::remove_reference<Tup>::type>::type>(
std::get<Index - 1>(tup)),
std::forward<Ts>(args)...); // everything already expanded
}
};
template <typename Functor, typename Tup>
struct Expander<0, Functor, Tup> {
template <typename... Ts>
static auto call(Functor&& f, Tup&&, Ts&&... args)
-> decltype(f(std::forward<Ts>(args)...)) {
static_assert(
std::tuple_size<typename std::remove_reference<Tup>::type>::value
== sizeof...(Ts),
"tuple has not been fully expanded");
return f(std::forward<Ts>(args)...); // the actual call
}
};
template <typename Functor, typename Tup>
auto call_with_tuple(Functor&& f, Tup&& tup) -> decltype(
Expander<std::tuple_size<
typename std::remove_reference<Tup>::type>::value,
Functor, Tup>::call(std::forward<Functor>(f),
std::forward<Tup>(tup))) {
return Expander<std::tuple_size<
typename std::remove_reference<Tup>::type>::value,
Functor, Tup>::call(std::forward<Functor>(f), std::forward<Tup>(tup));
}
} // end detail
namespace impl {
template <typename MapFunc, typename... Containers>
class IMapper;
}
template <typename MapFunc, typename... Containers>
impl::IMapper<MapFunc, Containers...> imap(MapFunc, Containers&&...);
}
template <typename MapFunc, typename... Containers>
class iter::impl::IMapper {
// The imap function is the only thing allowed to create a IMapper
friend IMapper iter::imap<MapFunc, Containers...>(MapFunc, Containers&&...);
using ZippedType = decltype(zip(std::declval<Containers>()...));
using ZippedIterType = iterator_type<ZippedType>;
private:
MapFunc map_func;
ZippedType zipped;
using IMapIterDeref =
decltype(iter::detail::call_with_tuple(map_func, *std::begin(zipped)));
IMapper(MapFunc in_map_func, Containers&&... in_containers)
: map_func(in_map_func),
zipped(zip(std::forward<Containers>(in_containers)...)) {}
public:
IMapper(IMapper&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
typename std::remove_reference<IMapIterDeref>::type> {
private:
MapFunc* map_func;
ZippedIterType zipiter;
public:
Iterator(MapFunc& in_map_func, ZippedIterType&& in_zipiter)
: map_func(&in_map_func), zipiter(std::move(in_zipiter)) {}
IMapIterDeref operator*() {
return iter::detail::call_with_tuple(*this->map_func, *(this->zipiter));
}
ArrowProxy<IMapIterDeref> operator->() {
return {**this};
}
Iterator& operator++() {
++this->zipiter;
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->zipiter != other.zipiter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {this->map_func, this->zipped.begin()};
}
Iterator end() {
return {this->map_func, this->zipped.end()};
}
};
template <typename MapFunc, typename... Containers>
iter::impl::IMapper<MapFunc, Containers...> iter::imap(
MapFunc map_func, Containers&&... containers) {
return {map_func, std::forward<Containers>(containers)...};
}
#endif