-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_iterator.hpp
323 lines (309 loc) · 8.98 KB
/
hpc_iterator.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
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
#pragma once
#include <cassert>
#include <cstddef>
#include <hpc_limits.hpp>
#include <hpc_macros.hpp>
#include <iterator>
namespace hpc {
namespace impl {
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr typename std::iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last, std::random_access_iterator_tag) noexcept
{
return last - first;
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr typename std::iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last, std::input_iterator_tag) noexcept
{
using difference_type = typename std::iterator_traits<Iterator>::difference_type;
difference_type i(0);
for (; first != last; ++first) ++i;
return i;
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr Iterator
advance(
Iterator first,
typename std::iterator_traits<Iterator>::difference_type n,
std::random_access_iterator_tag) noexcept
{
return first + n;
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr Iterator
advance(Iterator first, typename std::iterator_traits<Iterator>::difference_type n, std::input_iterator_tag) noexcept
{
auto last = first;
for (decltype(n) i(0); i != n; ++i) ++last;
return last;
}
} // namespace impl
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr typename std::iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last) noexcept
{
using tag_type = typename std::iterator_traits<Iterator>::iterator_category;
return hpc::impl::distance(first, last, tag_type());
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr Iterator
advance(Iterator first, typename std::iterator_traits<Iterator>::difference_type n) noexcept
{
using tag_type = typename std::iterator_traits<Iterator>::iterator_category;
return hpc::impl::advance(first, n, tag_type());
}
template <class T, class Index = std::ptrdiff_t>
class pointer_iterator
{
T* m_pointer;
#ifdef HPC_CHECK_BOUNDS
T* m_allocation_begin;
T* m_allocation_end;
#endif
public:
using value_type = std::remove_const_t<T>;
using difference_type = Index;
using reference = T&;
using pointer = T*;
using iterator_category = std::random_access_iterator_tag;
HPC_ALWAYS_INLINE HPC_HOST_DEVICE explicit constexpr pointer_iterator(T* pointer_in) noexcept
: m_pointer(pointer_in)
#ifdef HPC_CHECK_BOUNDS
,
m_allocation_begin(nullptr),
m_allocation_end(pointer(nullptr) + ::hpc::numeric_limits<std::ptrdiff_t>::max())
#endif
{
}
#ifndef HPC_CHECK_BOUNDS
HPC_ALWAYS_INLINE
HPC_HOST_DEVICE constexpr pointer_iterator(T* pointer_in, T*, T*) noexcept : m_pointer(pointer_in)
{
}
#else
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr pointer_iterator(T* pointer_in, T* alloc_begin, T* alloc_end) noexcept
: m_pointer(pointer_in), m_allocation_begin(alloc_begin), m_allocation_end(alloc_end)
{
}
#endif
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(pointer_iterator const& other) const noexcept
{
return m_pointer == other.m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(pointer_iterator const& other) const noexcept
{
return m_pointer != other.m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator*() const noexcept
{
#ifdef HPC_CHECK_BOUNDS
assert(m_allocation_begin <= m_pointer);
assert(m_pointer < m_allocation_end);
#endif
return *m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator&
operator++() noexcept
{
++m_pointer;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator
operator++(int) noexcept
{
auto ret = *this;
++m_pointer;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator&
operator--() noexcept
{
--m_pointer;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator
operator--(int) noexcept
{
auto ret = *this;
--m_pointer;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator&
operator+=(difference_type const n) noexcept
{
m_pointer = m_pointer + n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE pointer_iterator&
operator-=(difference_type const n) noexcept
{
m_pointer = m_pointer - n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr pointer_iterator
operator+(difference_type const n) const noexcept
{
return pointer_iterator(
m_pointer + n
#ifdef HPC_CHECK_BOUNDS
,
m_allocation_begin,
m_allocation_end
#endif
);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr pointer_iterator
operator-(difference_type const n) const noexcept
{
return pointer_iterator(
m_pointer - n
#ifdef HPC_CHECK_BOUNDS
,
m_allocation_begin,
m_allocation_end
#endif
);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr difference_type
operator-(pointer_iterator const& other) const noexcept
{
return difference_type(m_pointer - other.m_pointer);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator[](difference_type const i) const noexcept
{
return *((*this) + i);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(pointer_iterator const& other) const noexcept
{
return m_pointer < other.m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(pointer_iterator const& other) const noexcept
{
return m_pointer > other.m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(pointer_iterator const& other) const noexcept
{
return m_pointer <= other.m_pointer;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(pointer_iterator const& other) const noexcept
{
return m_pointer >= other.m_pointer;
}
};
template <class T>
class counting_iterator
{
T m_value;
public:
using value_type = T;
using difference_type = T;
using reference = T;
using pointer = T const*;
using iterator_category = std::random_access_iterator_tag;
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr counting_iterator(T value_in) noexcept : m_value(value_in)
{
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(counting_iterator const& other) const noexcept
{
return m_value == other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(counting_iterator const& other) const noexcept
{
return m_value != other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator*() const noexcept
{
return m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator&
operator++() noexcept
{
++m_value;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator
operator++(int) noexcept
{
auto ret = *this;
++m_value;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator&
operator--() noexcept
{
--m_value;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator
operator--(int) noexcept
{
auto ret = *this;
--m_value;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator&
operator+=(difference_type const n) noexcept
{
m_value += n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE counting_iterator&
operator-=(difference_type const n) noexcept
{
m_value -= n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr counting_iterator
operator+(difference_type const n) const noexcept
{
return counting_iterator(m_value + n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr counting_iterator
operator-(difference_type const n) const noexcept
{
return counting_iterator(m_value - n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr difference_type
operator-(counting_iterator const& other) const noexcept
{
return m_value - other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator[](difference_type const n) const noexcept
{
return *((*this) + n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(counting_iterator const& other) const noexcept
{
return m_value < other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(counting_iterator const& other) const noexcept
{
return m_value > other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(counting_iterator const& other) const noexcept
{
return m_value <= other.m_value;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(counting_iterator const& other) const noexcept
{
return m_value >= other.m_value;
}
};
} // namespace hpc