forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeImpl.hh
401 lines (345 loc) · 10.8 KB
/
RangeImpl.hh
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
//---------------------------------*-C++-*-----------------------------------//
// Copyright 2020-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/cont/detail/RangeImpl.hh
//---------------------------------------------------------------------------//
#pragma once
#include <iterator>
#include <type_traits>
#include <utility>
#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "corecel/OpaqueId.hh"
namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
template<class T, class Enable = void>
struct RangeTypeTraits
{
static_assert(std::is_integral_v<T>,
"Only integer types can be used in a range");
using value_type = T;
using counter_type = T;
using difference_type = std::make_signed_t<counter_type>;
template<class U>
using common_type = typename std::common_type<T, U>::type;
static CELER_CONSTEXPR_FUNCTION value_type zero() { return {}; }
static CELER_CONSTEXPR_FUNCTION bool is_valid(value_type) { return true; }
static CELER_CONSTEXPR_FUNCTION counter_type to_counter(value_type v)
{
return v;
}
static CELER_CONSTEXPR_FUNCTION value_type to_value(counter_type c)
{
return c;
}
static CELER_CONSTEXPR_FUNCTION value_type increment(value_type v,
difference_type i)
{
v += i;
return v;
}
static CELER_CONSTEXPR_FUNCTION value_type decrement(value_type v,
difference_type i)
{
v -= i;
return v;
}
};
template<class T, class Enable = void>
struct EnumWithSize
{
static CELER_CONSTEXPR_FUNCTION bool is_valid(T) { return true; }
};
template<class T>
struct EnumWithSize<T, typename std::enable_if<T::size_ >= 0>::type>
{
static CELER_CONSTEXPR_FUNCTION bool is_valid(T value)
{
return value <= T::size_;
}
};
//! Specialization for enums with a "size_" member
template<class T>
struct RangeTypeTraits<T, typename std::enable_if<std::is_enum<T>::value>::type>
{
using value_type = T;
using counter_type = typename std::underlying_type<T>::type;
using difference_type = std::make_signed_t<counter_type>;
template<class U>
using common_type = value_type;
static CELER_CONSTEXPR_FUNCTION value_type zero() { return {}; }
static CELER_CONSTEXPR_FUNCTION bool is_valid(value_type v)
{
return EnumWithSize<T>::is_valid(v);
}
static CELER_CONSTEXPR_FUNCTION counter_type to_counter(value_type v)
{
return static_cast<counter_type>(v);
}
static CELER_CONSTEXPR_FUNCTION value_type to_value(counter_type c)
{
return static_cast<value_type>(c);
}
static CELER_CONSTEXPR_FUNCTION value_type increment(value_type v,
difference_type i)
{
auto temp = to_counter(v);
temp += i;
return to_value(temp);
}
static CELER_CONSTEXPR_FUNCTION value_type decrement(value_type v,
difference_type i)
{
auto temp = to_counter(v);
temp -= i;
return to_value(temp);
}
};
//! Specialization for Opaque ID
template<class I, class T>
struct RangeTypeTraits<OpaqueId<I, T>, void>
{
using value_type = OpaqueId<I, T>;
using counter_type = T;
using difference_type = std::make_signed_t<counter_type>;
template<class U>
using common_type = value_type;
static CELER_CONSTEXPR_FUNCTION value_type zero() { return value_type(0); }
static CELER_CONSTEXPR_FUNCTION bool is_valid(value_type v)
{
return static_cast<bool>(v);
}
static CELER_CONSTEXPR_FUNCTION counter_type to_counter(value_type v)
{
return v.unchecked_get();
}
static CELER_CONSTEXPR_FUNCTION value_type to_value(difference_type c)
{
return static_cast<value_type>(c);
}
static CELER_CONSTEXPR_FUNCTION value_type increment(value_type v,
difference_type i)
{
counter_type temp = to_counter(v);
temp += i;
return to_value(temp);
}
static CELER_CONSTEXPR_FUNCTION value_type decrement(value_type v,
difference_type i)
{
counter_type temp = to_counter(v);
temp -= i;
return to_value(temp);
}
};
//---------------------------------------------------------------------------//
template<class T>
class range_iter
{
public:
//!@{
//! \name Type aliases
using TraitsT = RangeTypeTraits<T>;
using value_type = T;
using counter_type = typename TraitsT::counter_type;
using iterator_category = std::input_iterator_tag;
using difference_type = typename TraitsT::difference_type;
using pointer = void;
using reference = value_type;
//!@}
public:
//// CONSTRUCTOR ////
CELER_CONSTEXPR_FUNCTION range_iter(value_type value = TraitsT::zero())
: value_(value)
{
CELER_EXPECT(TraitsT::is_valid(value_));
}
//// ACCESSORS ////
CELER_CONSTEXPR_FUNCTION value_type operator*() const { return value_; }
CELER_CONSTEXPR_FUNCTION value_type const* operator->() const
{
return &value_;
}
CELER_CONSTEXPR_FUNCTION value_type operator[](counter_type inc) const
{
return TraitsT::increment(value_, inc);
}
//// ARITHMETIC ////
CELER_CONSTEXPR_FUNCTION range_iter& operator++()
{
value_ = TraitsT::increment(value_, 1);
return *this;
}
CELER_CONSTEXPR_FUNCTION range_iter operator++(int)
{
auto copy = *this;
value_ = TraitsT::increment(value_, 1);
return copy;
}
CELER_CONSTEXPR_FUNCTION range_iter operator+(difference_type inc) const
{
return {TraitsT::increment(value_, inc)};
}
CELER_CONSTEXPR_FUNCTION range_iter& operator--()
{
value_ = TraitsT::decrement(value_, 1);
return *this;
}
CELER_CONSTEXPR_FUNCTION range_iter operator--(int)
{
auto copy = *this;
value_ = TraitsT::decrement(value_, 1);
return copy;
}
CELER_CONSTEXPR_FUNCTION range_iter operator-(difference_type inc) const
{
return {TraitsT::decrement(value_, inc)};
}
CELER_CONSTEXPR_FUNCTION bool operator==(range_iter const& other) const
{
return value_ == other.value_;
}
CELER_CONSTEXPR_FUNCTION bool operator!=(range_iter const& other) const
{
return !(*this == other);
}
protected:
value_type value_;
};
//---------------------------------------------------------------------------//
template<class T>
class inf_range_iter : public range_iter<T>
{
using Base = range_iter<T>;
public:
using TraitsT = typename Base::TraitsT;
CELER_CONSTEXPR_FUNCTION inf_range_iter(T value = TraitsT::zero())
: Base(value)
{
}
CELER_CONSTEXPR_FUNCTION bool operator==(inf_range_iter const&) const
{
return false;
}
CELER_CONSTEXPR_FUNCTION bool operator!=(inf_range_iter const&) const
{
return true;
}
};
//---------------------------------------------------------------------------//
template<class T>
class step_range_iter : public range_iter<T>
{
using Base = range_iter<T>;
public:
using TraitsT = typename Base::TraitsT;
using counter_type = typename TraitsT::counter_type;
using difference_type = typename TraitsT::difference_type;
CELER_CONSTEXPR_FUNCTION step_range_iter(T value, counter_type step)
: Base(value), step_(step)
{
}
CELER_CONSTEXPR_FUNCTION step_range_iter& operator++()
{
value_ = TraitsT::increment(value_, step_);
return *this;
}
CELER_CONSTEXPR_FUNCTION step_range_iter operator++(int)
{
auto copy = *this;
++*this;
return copy;
}
CELER_CONSTEXPR_FUNCTION step_range_iter operator+(difference_type inc)
{
return {TraitsT::increment(value_, inc * step_)};
}
template<class U = counter_type>
CELER_FUNCTION typename std::enable_if_t<std::is_signed<U>::value, bool>
operator==(step_range_iter const& other) const
{
return step_ >= 0 ? !(value_ < other.value_) : value_ < other.value_;
}
template<class U = counter_type>
CELER_FUNCTION typename std::enable_if_t<std::is_unsigned<U>::value, bool>
operator==(step_range_iter const& other) const
{
return !(value_ < other.value_);
}
CELER_FUNCTION bool operator!=(step_range_iter const& other) const
{
return !(*this == other);
}
private:
using Base::value_;
counter_type step_;
};
//---------------------------------------------------------------------------//
template<class T>
class inf_step_range_iter : public step_range_iter<T>
{
using Base = step_range_iter<T>;
public:
using TraitsT = typename Base::TraitsT;
using counter_type = typename TraitsT::counter_type;
CELER_FUNCTION
inf_step_range_iter(T current = TraitsT::zero(), counter_type step = {})
: Base(current, step)
{
}
CELER_FUNCTION bool operator==(inf_step_range_iter const&) const
{
return false;
}
CELER_FUNCTION bool operator!=(inf_step_range_iter const&) const
{
return true;
}
};
//---------------------------------------------------------------------------//
/*!
* Proxy container for iterating over a finite range with a non-unit step
*/
template<class T>
class StepRange
{
public:
using IterT = step_range_iter<T>;
using counter_type = typename RangeTypeTraits<T>::counter_type;
CELER_FUNCTION StepRange(T begin, T end, counter_type step)
: begin_(begin, step), end_(end, step)
{
}
CELER_CONSTEXPR_FUNCTION IterT begin() const { return begin_; }
CELER_CONSTEXPR_FUNCTION IterT end() const { return end_; }
private:
IterT begin_;
IterT end_;
};
//---------------------------------------------------------------------------//
/*!
* Proxy container for iterating over an infinite range with a non-unit step
*/
template<class T>
class InfStepRange
{
public:
using IterT = inf_step_range_iter<T>;
using counter_type = typename RangeTypeTraits<T>::counter_type;
//! Construct from start/stop
CELER_FUNCTION InfStepRange(T begin, counter_type step)
: begin_(begin, step)
{
}
CELER_CONSTEXPR_FUNCTION IterT begin() const { return begin_; }
CELER_CONSTEXPR_FUNCTION IterT end() const { return IterT(); }
private:
IterT begin_;
};
} // namespace detail
} // namespace celeritas