This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap.hpp
158 lines (131 loc) · 5.33 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#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
//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 ZippedType = decltype(zip(std::declval<Containers>()...));
using ZippedIterType = iterator_type<ZippedType>;
private:
MapFunc map_func;
ZippedType zipped;
using IMapIterDeref = decltype(detail::call_with_tuple(
map_func, *std::begin(zipped)));
// Value constructor for use only in the imap function
IMap(MapFunc in_map_func, Containers&&... in_containers) :
map_func(in_map_func),
zipped(zip(std::forward<Containers>(in_containers)...))
{ }
public:
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 detail::call_with_tuple(
*this->map_func, *(this->zipiter));
}
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()};
}
};
// 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 ITER_IMAP_H_