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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
//
// Copyright 2013 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#ifndef CLOVER_UTIL_RANGE_HPP
#define CLOVER_UTIL_RANGE_HPP
#include <array>
#include <vector>
#include "util/adaptor.hpp"
namespace clover {
///
/// Class that identifies container types where the elements of a
/// range can be stored by the type conversion operator.
///
/// \a T identifies the range element type.
///
template<typename T, typename V>
struct range_store_traits;
template<typename T, typename S>
struct range_store_traits<T, std::vector<S>> {
typedef void enable;
template<typename R>
static std::vector<S>
create(const R &r) {
return { r.begin(), r.end() };
}
};
template<typename T, typename S, std::size_t N>
struct range_store_traits<T, std::array<S, N>> {
typedef void enable;
template<typename R>
static std::array<S, N>
create(const R &r) {
std::array<S, N> v;
assert(r.size() == v.size());
copy(r, v.begin());
return v;
}
};
namespace detail {
///
/// Common functionality that is shared by other implementations
/// of the container concept.
///
template<typename R, typename I, typename CI>
class basic_range {
public:
typedef I iterator;
typedef CI const_iterator;
typedef typename std::iterator_traits<iterator>::value_type value_type;
typedef typename std::iterator_traits<iterator>::reference
reference;
typedef typename std::iterator_traits<const_iterator>::reference
const_reference;
typedef typename std::iterator_traits<iterator>::difference_type
difference_type;
typedef std::size_t size_type;
bool
operator==(const basic_range &r) const {
return *static_cast<const R *>(this) == r;
}
bool
operator!=(const basic_range &r) const {
return !(*this == r);
}
iterator
begin() {
return static_cast<R *>(this)->begin();
}
iterator
end() {
return static_cast<R *>(this)->end();
}
const_iterator
begin() const {
return static_cast<const R *>(this)->begin();
}
const_iterator
end() const {
return static_cast<const R *>(this)->end();
}
std::reverse_iterator<iterator>
rbegin() {
return { begin() };
}
std::reverse_iterator<iterator>
rend() {
return { end() };
}
reference
front() {
return *begin();
}
reference
back() {
return *(end() - 1);
}
bool
empty() const {
return begin() == end();
}
reference
at(size_type i) {
if (i >= static_cast<const R *>(this)->size())
throw std::out_of_range("");
return begin()[i];
}
const_reference
at(size_type i) const {
if (i >= static_cast<const R *>(this)->size())
throw std::out_of_range("");
return begin()[i];
}
reference
operator[](size_type i) {
return begin()[i];
}
const_reference
operator[](size_type i) const {
return begin()[i];
}
template<typename V>
using store_traits = range_store_traits<
typename std::remove_cv<value_type>::type, V
>;
template<typename V,
typename = typename store_traits<V>::enable>
operator V() const {
return store_traits<V>::create(*static_cast<const R *>(this));
}
};
}
///
/// Range that contains all elements delimited by an iterator pair
/// (\a i, \a j). Use range() as convenience constructor.
///
template<typename I>
class iterator_range : public detail::basic_range<iterator_range<I>, I, I> {
public:
typedef detail::basic_range<iterator_range<I>, I, I> super;
iterator_range() : i(), j() {
}
iterator_range(I i, I j) : i(i), j(j) {
}
bool
operator==(const iterator_range &r) const {
return i == r.i && j == r.j;
}
I
begin() const {
return i;
}
I
end() const {
return j;
}
typename super::size_type
size() const {
return end() - begin();
}
private:
I i, j;
};
namespace detail {
template<typename T>
using preferred_iterator_type = decltype(std::declval<T>().begin());
}
///
/// Range that transforms the contents of a number of source ranges
/// \a os element-wise by using the provided functor \a f. Use
/// map() as convenience constructor.
///
template<typename F, typename... Os>
class adaptor_range :
public detail::basic_range<adaptor_range<F, Os...>,
detail::iterator_adaptor<
F, detail::preferred_iterator_type<Os>...>,
detail::iterator_adaptor<
F, detail::preferred_iterator_type<const Os>...>
> {
public:
typedef detail::basic_range<adaptor_range<F, Os...>,
detail::iterator_adaptor<
F, detail::preferred_iterator_type<Os>...>,
detail::iterator_adaptor<
F, detail::preferred_iterator_type<const Os>...>
> super;
template<typename G, typename... Rs>
adaptor_range(G &&f, Rs &&... os) :
f(std::forward<G>(f)), os(std::forward<Rs>(os)...) {
}
bool
operator==(const adaptor_range &r) const {
return f == r.f && os == r.os;
}
typename super::iterator
begin() {
return { f, tuple::map(begins(), os) };
}
typename super::iterator
end() {
return { f, tuple::map(advances_by(size()),
tuple::map(begins(), os)) };
}
typename super::const_iterator
begin() const {
return { f, tuple::map(begins(), os) };
}
typename super::const_iterator
end() const {
return { f, tuple::map(advances_by(size()),
tuple::map(begins(), os)) };
}
typename super::size_type
size() const {
return tuple::apply(minimum(), tuple::map(sizes(), os));
}
private:
F f;
std::tuple<Os...> os;
};
///
/// Range that contains all elements delimited by the index pair
/// (\a i, \a j) in the source range \a r. Use slice() as
/// convenience constructor.
///
template<typename O>
class slice_range :
public detail::basic_range<slice_range<O>,
detail::preferred_iterator_type<O>,
detail::preferred_iterator_type<const O>> {
public:
typedef detail::basic_range<slice_range<O>,
detail::preferred_iterator_type<O>,
detail::preferred_iterator_type<const O>
> super;
template<typename R>
slice_range(R &&r, typename super::size_type i,
typename super::size_type j) :
o(std::forward<R>(r)), i(i), j(j) {
}
bool
operator==(const slice_range &r) const {
return o == r.o && i == r.i && j == r.j;
}
typename super::iterator
begin() {
return std::next(o.begin(), i);
}
typename super::iterator
end() {
return std::next(o.begin(), j);
}
typename super::const_iterator
begin() const {
return std::next(o.begin(), i);
}
typename super::const_iterator
end() const {
return std::next(o.begin(), j);
}
typename super::size_type
size() const {
return j - i;
}
private:
O o;
typename super::size_type i, j;
};
///
/// Create a range from an iterator pair (\a i, \a j).
///
/// \sa iterator_range.
///
template<typename T>
iterator_range<T>
range(T i, T j) {
return { i, j };
}
///
/// Create a range of \a n elements starting from iterator \a i.
///
/// \sa iterator_range.
///
template<typename T>
iterator_range<T>
range(T i, typename std::iterator_traits<T>::difference_type n) {
return { i, i + n };
}
///
/// Create a range by transforming the contents of a number of
/// source ranges \a rs element-wise using a provided functor \a f.
///
/// \sa adaptor_range.
///
template<typename F, typename... Rs>
adaptor_range<F, Rs...>
map(F &&f, Rs &&... rs) {
return { std::forward<F>(f), std::forward<Rs>(rs)... };
}
///
/// Create a range identical to another range \a r.
///
template<typename R>
adaptor_range<identity, R>
range(R &&r) {
return { identity(), std::forward<R>(r) };
}
///
/// Create a range by taking the elements delimited by the index
/// pair (\a i, \a j) in a source range \a r.
///
/// \sa slice_range.
///
template<typename R>
slice_range<R>
slice(R &&r, typename slice_range<R>::size_type i,
typename slice_range<R>::size_type j) {
return { std::forward<R>(r), i, j };
}
///
/// Range that behaves as a vector of references of type \a T.
///
/// Useful because STL containers cannot contain references to
/// objects as elements.
///
template<typename T>
class ref_vector : public adaptor_range<derefs, std::vector<T *>> {
public:
ref_vector(std::initializer_list<std::reference_wrapper<T>> il) :
adaptor_range<derefs, std::vector<T *>>(derefs(), map(addresses(), il)) {
}
template<typename R>
ref_vector(R &&r) : adaptor_range<derefs, std::vector<T *>>(
derefs(), map(addresses(), std::forward<R>(r))) {
}
};
}
#endif
|