-
Notifications
You must be signed in to change notification settings - Fork 1
/
ranges.hpp
237 lines (186 loc) · 4.74 KB
/
ranges.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
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
#pragma once
#include "c++17_features.hpp"
#include "meta_programming.hpp"
#include "rank.hpp"
#include <algorithm>
#include <cassert>
namespace cu
{
/**********************
* Generic put method *
**********************/
namespace detail
{
template <typename OutputRange,
typename Value>
auto put_impl( OutputRange && out, Value && val, cu::Rank<1> )
-> decltype(out.put(std::forward<Value>(val)))
{
return out.put(std::forward<Value>(val));
}
template <typename OutputRange,
typename Value>
auto put_impl( OutputRange && out, Value && val, cu::Rank<0> )
-> decltype(static_cast<void>(out.front()=std::forward<Value>(val)),
out.pop_front())
{
return static_cast<void>(out.front()=std::forward<Value>(val)),
out.pop_front();
}
} // namespace detail
template <typename OutputRange,
typename Value>
decltype(auto) put( OutputRange out, Value && val )
{
return detail::put_impl( std::move(out), std::forward<Value>(val), cu::Rank<1>() );
}
/***************************
* Range factory functions *
***************************/
namespace detail
{
template <typename Iter1,
typename Iter2>
class IteratorRange
{
public:
IteratorRange( Iter1 && first_,
Iter2 && last_ )
: first( std::move(first_) )
, last ( std::move(last_ ) )
{}
auto begin() const { return first; }
auto end () const { return last; }
bool empty() const { return first == last; }
void pop_front() { assert( !empty() ); ++first; }
decltype(auto) front() const { return *first; }
private:
Iter1 first;
Iter2 last;
};
} // namespace detail
template <typename Iter1,
typename Iter2>
auto makeIteratorRange( Iter1 first, Iter2 last )
{
return detail::IteratorRange<Iter1,Iter2>(
std::move(first),
std::move(last) );
}
template <typename Container>
auto makeRange( Container && container )
{
using std::begin;
using std::end;
return makeIteratorRange( begin(container), end(container) );
}
namespace detail
{
template <typename Container>
class PushBackRange
{
public:
PushBackRange( Container & container_ )
: container( container_ )
{}
template <typename Value>
void put( Value && val ) const
{
container.push_back( std::forward<Value>(val) );
}
private:
Container & container;
};
} // namespace detail
template <typename Container>
auto makePushBackRange( Container && container )
{
return detail::PushBackRange<Container>( container );
}
/********************
* Range algorithms *
********************/
template <typename InputRange,
typename F>
void for_each( InputRange range, F && f )
{
for ( ; !range.empty(); range.pop_front() )
f( range.front() );
}
template <typename InputRange,
typename OutputRange>
void copy( InputRange in, OutputRange out )
{
for ( ; !in.empty(); in.pop_front() )
put( out, in.front() );
}
template <typename RandomAccessRange>
void sort( RandomAccessRange range )
{
::std::sort( range.begin(), range.end() );
}
/******************
* Range adapters *
******************/
namespace detail
{
template <template <typename...> class AdaptedRange,
typename ...Ts>
class RangeAdapter
{
private:
std::tuple<Ts...> data;
public:
template <typename ...Args>
RangeAdapter( Ts &&... args )
: data( std::forward<Ts>(args)... )
{}
template <typename OrigRange>
friend AdaptedRange<OrigRange,Ts...> operator|( OrigRange orig, RangeAdapter adapter )
{
const auto f = [&orig]( auto &&...args )
{
return AdaptedRange<OrigRange,Ts...>(
std::move(orig),
std::move(args)... );
};
return cu::apply( f, adapter.data );
}
};
} // namespace detail
template <template <typename...> class AdaptedRange,
typename ...Ts>
auto makeRangeAdapter( Ts...data )
{
return detail::RangeAdapter<AdaptedRange,Ts...>( std::move(data)... );
}
namespace detail
{
template <typename InputRange,
typename F>
class TransformedRange
{
private:
InputRange in;
F f;
public:
TransformedRange( InputRange && in_, F && f_ )
: in( std::forward<InputRange>(in_) )
, f( std::forward<F>(f_) )
{}
bool empty() const { return in.empty(); }
void pop_front() { in.pop_front(); }
decltype(auto) front() const { return f( in.front() ); }
decltype(auto) front() { return f( in.front() ); }
};
} // namespace detail
template <typename F>
auto transformed( F && f )
{
return makeRangeAdapter<detail::TransformedRange,F>( std::forward<F>(f) );
}
inline auto moved()
{
return transformed( []( auto && x ){ return std::move(x); } );
}
} // namespace cu