forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap.hpp
141 lines (116 loc) · 4.56 KB
/
imap.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
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
#ifndef IMAP__H__
#define IMAP__H__
#include "zip.hpp"
#include <utility>
#include <type_traits>
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::get<Index-1>(tup),
std::forward<Ts>(args)...))
{
// recurse
return Expander<Index-1, Functor, Tup>::call(
std::forward<Functor>(f),
std::forward<Tup>(tup),
std::get<Index-1>(tup), // pull out one element
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
//Forward declarations of IMap and imap
template <typename MapFunc, typename... Containers>
class IMap;
template <typename MapFunc, typename... Containers>
IMap<MapFunc, Containers...> imap(MapFunc, Containers&&...);
template <typename MapFunc, typename... Containers>
class IMap {
// The imap function is the only thing allowed to create a IMap
friend IMap imap<MapFunc, Containers...>(MapFunc, Containers&& ...);
using ZippedIterType = iterator_type<Zipped<Containers...>>;
private:
MapFunc map_func;
Zipped<Containers...> zipped;
// Value constructor for use only in the imap function
IMap(MapFunc map_func, Containers&& ... containers) :
map_func(map_func),
zipped(zip(std::forward<Containers>(containers)...))
{ }
IMap() = delete;
IMap& operator=(const IMap&) = delete;
public:
IMap(const IMap&) = default;
IMap(IMap&&) = default;
class Iterator {
private:
MapFunc map_func;
mutable ZippedIterType zipiter;
public:
Iterator(MapFunc map_func, ZippedIterType zipiter) :
map_func(map_func),
zipiter(zipiter)
{ }
auto operator*() ->
decltype(detail::call_with_tuple(
this->map_func, *(this->zipiter)))
{
return detail::call_with_tuple(
this->map_func, *(this->zipiter));
}
Iterator& operator++() {
++this->zipiter;
return *this;
}
bool operator!=(const Iterator& other) const {
return this->zipiter != other.zipiter;
}
};
Iterator begin() {
return {this->map_func, this->zipped.begin()};
}
Iterator end() {
return {this->map_func, this->zipped.end()};
}
};
// Helper function to instantiate a IMap
template <typename MapFunc, typename... Containers>
IMap<MapFunc, Containers...> imap(
MapFunc map_func, Containers&& ... containers) {
return {map_func, std::forward<Containers>(containers)...};
}
}
#endif //ifndef IMAP__H__