-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapped_range.h
292 lines (210 loc) · 8.52 KB
/
mapped_range.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef RO_MAPPED_RANGE_H
#define RO_MAPPED_RANGE_H
#include <ro/basic_range.h>
#include <ro/debug.h>
namespace {
namespace ro {
// forward dcl
template< class Rg, class F = void*, class O = rg_elem_type<Rg> > struct mapped_range;
template <class Rg, class F, class O, bool RO>
struct mapped_range_iterator : basic_range_iterator<Rg,RO> {
typedef basic_range_iterator<Rg,RO> b;
typedef SEL <
RO,
mapped_range<Rg,F,O> const,
mapped_range<Rg,F,O>
> parent_t;
// if this line is uncommented - clang ICE in pre 2012-11-11 versions
// parent_t * const & parent_p = (parent_t*)(b::parent_p);
// STL ITERATOR TYPES
// to edit
typedef mapped_range_iterator<Rg,F,O,RO> iterator;
typedef mapped_range_iterator<Rg,F,O,true> const_iterator;
typedef rm_ref<mapped_range_iterator> self_type;
typedef typename b::iterator_category iterator_category;
typedef typename b::org_iterator org_iterator;
typedef O value_type;
using typename b::size_type;
using typename b::difference_type;
using typename b::pointer;
typedef value_type const_reference;
typedef value_type reference;
// non-STL
using typename b::elem_type;
////// MEMBERS
////// CTOR
mapped_range_iterator (const self_type& rhs) : b(rhs) {}; // copy
mapped_range_iterator (parent_t* parent_p, const org_iterator current) : b(parent_p, current) {};
////// CONVERSION non-const --> const
operator mapped_range_iterator<Rg&&,F,O,true>() { return mapped_range_iterator<Rg&&,F,O,true>((parent_t*)b::parent_p, b::current); };
////// IFACE
reference operator*() { return ((parent_t*)b::parent_p)->f(*b::current); };
const_reference operator*() const { return ((parent_t*)b::parent_p)->f(*b::current); };
pointer operator->() { return &(operator*()); }
pointer const operator->() const { return &(operator*()); }
// ++ It
using b::operator++;
bool operator==(const_iterator rhs) const { return b::current == rhs.current; }
bool operator!=(const_iterator rhs) const { return b::current != rhs.current; }
///////////////////////////////////////////////////////////////////// INPORT ORG_ITERATOR METHODS
// bidiractional
using b::operator--;
// random access
using b::operator+=;
using b::operator-=;
using b::operator[];
using b::operator<;
using b::operator<=;
using b::operator>;
using b::operator>=;
};
///////////////////////////////////////////////////////////////////////////////////////// MAPPED_RANGE
template<class Rg, class F, class O>
struct mapped_range : basic_range<Rg> {
typedef basic_range<Rg> b;
// STL IFACE
typedef O value_type;
typedef mapped_range_iterator<Rg,F,O,false> iterator;
typedef mapped_range_iterator<Rg,F,O,true> const_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type ;
typedef rm_ref<value_type>* pointer;
typedef rg_const_reference<Rg> const_reference; // non-STL
typedef typename std::conditional <
std::is_const<Rg>::value /* || RO*/, // <-- different from interator
rg_const_reference<Rg>,
rg_reference<Rg>
>::type reference;
// non-STL
typedef rg_elem_type<Rg> elem_type;
typedef mapped_range self_type;
// MEMBERS
F f;
//// CTOR
explicit mapped_range (Rg&& rg, F f) : b(std::forward<Rg>(rg)), f(f) {};
//// ASSIGNMENT
using b::operator=;
//// ITERATOR
iterator end() { return iterator(this, endz(b::rg)); }
const_iterator end() const { return const_iterator(this, endz(b::rg)); }
iterator begin() { return iterator(this, std::begin(b::rg)); };
const_iterator begin() const { return const_iterator(this, std::begin(b::rg)); };
//// RG PROPERTIES
using b::size;
using b::empty;
using b::operator bool;
//// ELEM ACCESS
reference const front() const { return *std::begin(b::rg); }
reference front() { return *std::begin(b::rg); }
reference const back() const { return *std::prev(ro::endz(b::rg)); }
reference back() { return *std::prev(ro::endz(b::rg)); }
//// INPORTED RG METHODS
using b::push_front;
using b::push_back;
using b::insert;
using b::pop_front;
using b::pop_back;
using b::operator[];
// conversion to stl-containers
#include <ro/conversion.h>
};
//////////////////////////////////////////////////////////////// TRAITS
template<class Rg, class F, class O> struct is_range_t<mapped_range<Rg,F,O>> : std::true_type {};
template<class Rg, class F, class O, bool RO> struct is_range_t<mapped_range_iterator<Rg,F,O,RO>> : std::false_type {};
template<class Rg, class F, class O> struct is_ro_range<mapped_range<Rg,F,O>> : std::true_type {};
template<class Rg, class F, class O, bool RO> struct is_ro_range_iterator <mapped_range_iterator<Rg,F,O,RO>> : std::true_type {};
//////////////////////////////////////////////////////////////////////////////// MAP: Rg<T> * F --> Rg<U>
//// generic overload (O!=E) (lambdas, other)
template<
class Rg,
class E = rg_elem_type<Rg>,
class F,
class O= rm_ref<decltype(std::declval<F>()(std::declval<E>()))>
>
eIF <is_range<Rg>::value && is_callable<F, O(E)>::value && !has_result_type<F>::value && !is_lambda_functor<F>::value, mapped_range<Rg&&,F,O>>
operator* (Rg&& rg, F f) {
return mapped_range<Rg&&,F,O> (std::forward<Rg>(rg), f);
};
//// FO
template<
class Rg,
class F,
class E = rg_elem_type<Rg>,
class O = typename F::result_type
>
eIF <is_range<Rg>::value && is_callable<F, O(E)>::value , mapped_range<Rg&&,F,O>>
operator* (Rg&& rg, F f) {
return mapped_range<Rg&&,F,O> (std::forward<Rg>(rg), f);
};
//// RO::LAMBDA_FUNCTOR
template<
class Rg,
class F,
class E = rg_reference<Rg>,
class = typename F::is_lambda_functor
>
auto operator* (Rg&& rg, F f) ->
eIF <is_range<Rg>::value, mapped_range<Rg&&,F,decltype(f(std::declval<E>()))>> {
return mapped_range<Rg&&,F,decltype(f(std::declval<E>()))> (std::forward<Rg>(rg), f);
};
/////////////////////////////////// TUPLES
// limitation: max N must equal 2, used tuple-arg N's must be >= 2
#define MK_TUPLE_OVERLOAD(N,CONST) \
\
template< \
class Rg, \
class E=rg_elem_type<Rg>, \
class O=typename std::tuple_element<N,E>::type \
> \
\
eIF < \
is_range<Rg>::value \
&& is_tuple<rg_elem_type<Rg>>::value \
&& ( N==0 || !std::is_same<rm_qualifier<typename std::tuple_element<0,E>::type>, rm_qualifier<typename std::tuple_element<N,E>::type>>::value) \
, mapped_range<Rg&&, O CONST&(*)(E CONST&), O> \
> \
\
operator* (Rg&& rg, typename std::tuple_element<N,E>::type CONST &(*f)(E CONST &)) { \
return mapped_range<Rg&&, O CONST& (*)(E CONST&), O> (std::forward<Rg>(rg), f); \
};
// if we have T1==T2, then we should not define OL for N=2,
// because signatures will be the same => ambigues OL
MK_TUPLE_OVERLOAD(0,)
MK_TUPLE_OVERLOAD(0,const)
MK_TUPLE_OVERLOAD(1,)
MK_TUPLE_OVERLOAD(1,const)
#define MK_PAIR_OVERLOAD(N,CONST) \
\
template< \
class Rg, \
class E=rg_elem_type<Rg>, \
class O=typename std::tuple_element<N,E>::type \
> \
\
eIF < \
is_range<Rg>::value \
&& is_pair<rg_elem_type<Rg>>::value \
&& ( N==0 || !std::is_same<rm_qualifier<typename std::tuple_element<0,E>::type>, rm_qualifier<typename std::tuple_element<N,E>::type>>::value) \
, mapped_range<Rg&&, O CONST&(*)(E CONST&), O> \
> \
\
operator* (Rg&& rg, typename std::tuple_element<N,E>::type CONST &(*f)(E CONST &)) { \
return mapped_range<Rg&&, O CONST& (*)(E CONST&), O> (std::forward<Rg>(rg), f); \
};
MK_PAIR_OVERLOAD(0,)
MK_PAIR_OVERLOAD(0,const)
MK_PAIR_OVERLOAD(1,)
MK_PAIR_OVERLOAD(1,const)
//// non-converting overload (O == E), needed for functions like std::abs()
template<
class Rg,
class E = rg_elem_type<Rg>,
class O = E
>
eIF <is_range<Rg>::value && std::is_same<rm_qualifier<O>,rm_qualifier<E>>::value, mapped_range<Rg&&, O(*)(E), E>>
operator * (Rg&& rg, O (*f)(E) ) {
return mapped_range<Rg&&, O(*)(E), E> (std::forward<Rg>(rg), f);
};
};
};
#endif // RO_MAPPED_RANGE_H