Gyselalib++
 
Loading...
Searching...
No Matches
idx_range_slice.hpp
1// SPDX-License-Identifier: MIT
2#pragma once
3#include <map>
4#include <vector>
5
6#include <ddc/ddc.hpp>
7
8#include "ddc_aliases.hpp"
9
10
11template <class...>
12class IdxRangeSlice;
13
14template <class Grid1D>
16
17template <class T>
18struct is_subidx_range_collection : std::false_type
19{
20};
21
22template <class... Tags>
23struct is_subidx_range_collection<IdxRangeSlice<Tags...>> : std::true_type
24{
25};
26
28template <class T>
29inline constexpr bool is_subidx_range_collection_v = is_subidx_range_collection<T>::value;
30
36template <class... Dims>
38{
39 static_assert(sizeof...(Dims) > 0);
40
41private:
43 using tags_seq = ddc::detail::TypeSeq<Dims...>;
44
45 Idx<Dims...> m_front;
46 IdxStep<Dims...> m_size;
47 IdxStep<Dims...> m_stride;
48
49 template <class...>
50 friend class IdxRangeSlice;
51
52public:
54 IdxRangeSlice() = default;
55
63 KOKKOS_FUNCTION IdxRangeSlice(
64 Idx<Dims...> front,
65 IdxStep<Dims...> size,
66 IdxStep<Dims...> stride)
67 : m_front(front)
68 , m_size(size)
69 , m_stride(stride)
70 {
71 }
72
78 template <
79 class... DDoms,
80 class = std::enable_if_t<(is_subidx_range_collection_v<DDoms> && ...)>>
81 KOKKOS_FUNCTION IdxRangeSlice(DDoms const&... valid_indices)
82 : m_front(valid_indices.front()...)
83 , m_size(valid_indices.extents()...)
84 , m_stride(valid_indices.strides()...)
85 {
86 }
87
95 template <class... DDims>
96 KOKKOS_FUNCTION bool contains(Idx<DDims...> elem) const
97 {
98 static_assert(
99 (ddc::in_tags_v<DDims, tags_seq> && ...),
100 "Requested Tag absent from IdxRangeSlice");
101
102 IdxStep<DDims...> distance(elem - ddc::select<DDims...>(m_front));
103 return (((ddc::get<DDims>(distance) % ddc::get<DDims>(m_stride)) == 0) && ...);
104 }
105
114 template <class... DDims>
115 KOKKOS_FUNCTION bool contains(IdxRange<DDims...> idx_range) const
116 {
117 static_assert(
118 (ddc::in_tags_v<DDims, tags_seq> && ...),
119 "Requested Tag absent from IdxRangeSlice");
120 return contains(idx_range.front())
121 && (((idx_range.template extent<DDims>().value() == 1)
122 || (ddc::select<DDims>(m_stride) == 1))
123 && ...);
124 }
125
134 template <class Dim, class = std::enable_if_t<ddc::in_tags_v<Dim, tags_seq>>>
135 KOKKOS_FUNCTION std::size_t get_index(Idx<Dim> elem) const
136 {
137 assert(contains(elem));
138 return (elem - ddc::select<Dim>(m_front)).value() / ddc::get<Dim>(m_stride);
139 }
140
146 KOKKOS_FUNCTION constexpr IdxStep<Dims...> extents() const noexcept
147 {
148 return m_size;
149 }
150
158 template <class QueryDDim>
159 KOKKOS_FUNCTION constexpr IdxStep<QueryDDim> extent() const noexcept
160 {
161 return ddc::select<QueryDDim>(m_size);
162 }
163
169 KOKKOS_FUNCTION constexpr std::size_t size() const
170 {
171 return (1ul * ... * (ddc::get<Dims>(m_size)));
172 }
173
179 KOKKOS_FUNCTION constexpr Idx<Dims...> front() const noexcept
180 {
181 return m_front;
182 }
183
189 KOKKOS_FUNCTION constexpr Idx<Dims...> back() const noexcept
190 {
191 return m_front + m_stride * (m_size - 1);
192 }
193
201 template <class QueryDim>
202 KOKKOS_FUNCTION constexpr IdxStep<QueryDim> stride() const noexcept
203 {
204 return ddc::select<QueryDim>(m_stride);
205 }
206
212 KOKKOS_FUNCTION constexpr IdxStep<Dims...> strides() const noexcept
213 {
214 return m_stride;
215 }
216
223 KOKKOS_FUNCTION auto begin() const
224 {
225 static_assert(sizeof...(Dims) == 1);
226 return IdxRangeSliceIterator(m_front, m_stride);
227 }
228
235 KOKKOS_FUNCTION auto end() const
236 {
237 static_assert(sizeof...(Dims) == 1);
238 return IdxRangeSliceIterator(m_front + m_stride * m_size, m_stride);
239 }
240};
241
245template <class Grid1D>
247{
248private:
249 Idx<Grid1D> m_value;
250 IdxStep<Grid1D> m_stride;
251
252public:
254 using iterator_category = std::random_access_iterator_tag;
255
257 using value_type = Idx<Grid1D>;
258
260 using stride_type = IdxStep<Grid1D>;
261
263 using difference_type = std::ptrdiff_t;
264
265 KOKKOS_DEFAULTED_FUNCTION IdxRangeSliceIterator() = default;
266
273 KOKKOS_FUNCTION constexpr explicit IdxRangeSliceIterator(
274 Idx<Grid1D> value,
275 IdxStep<Grid1D> stride)
276 : m_value(value)
277 , m_stride(stride)
278 {
279 }
280
285 KOKKOS_FUNCTION constexpr Idx<Grid1D> operator*() const noexcept
286 {
287 return m_value;
288 }
289
294 KOKKOS_FUNCTION constexpr IdxRangeSliceIterator& operator++()
295 {
296 m_value = m_value + m_stride;
297 return *this;
298 }
299
304 KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator++(int)
305 {
306 auto tmp = *this;
307 ++*this;
308 return tmp;
309 }
310
315 KOKKOS_FUNCTION constexpr IdxRangeSliceIterator& operator--()
316 {
317 m_value = m_value - m_stride;
318 return *this;
319 }
320
325 KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator--(int)
326 {
327 auto tmp = *this;
328 --*this;
329 return tmp;
330 }
331
338 {
339 m_value = m_value + n * m_stride;
340 return *this;
341 }
342
349 {
350 m_value = m_value - n * m_stride;
351 return *this;
352 }
353
360 KOKKOS_FUNCTION constexpr Idx<Grid1D> operator[](difference_type n) const
361 {
362 return m_value + n * m_stride;
363 }
364
366 friend KOKKOS_FUNCTION constexpr bool operator==(
367 IdxRangeSliceIterator const& xx,
368 IdxRangeSliceIterator const& yy)
369 {
370 return xx.m_value == yy.m_value;
371 }
372
374 friend KOKKOS_FUNCTION constexpr bool operator!=(
375 IdxRangeSliceIterator const& xx,
376 IdxRangeSliceIterator const& yy)
377 {
378 return xx.m_value != yy.m_value;
379 }
380
382 friend KOKKOS_FUNCTION constexpr bool operator<(
383 IdxRangeSliceIterator const& xx,
384 IdxRangeSliceIterator const& yy)
385 {
386 return xx.m_value < yy.m_value;
387 }
388
390 friend KOKKOS_FUNCTION constexpr bool operator>(
391 IdxRangeSliceIterator const& xx,
392 IdxRangeSliceIterator const& yy)
393 {
394 return yy < xx;
395 }
396
398 friend KOKKOS_FUNCTION constexpr bool operator<=(
399 IdxRangeSliceIterator const& xx,
400 IdxRangeSliceIterator const& yy)
401 {
402 return !(yy < xx);
403 }
404
406 friend KOKKOS_FUNCTION constexpr bool operator>=(
407 IdxRangeSliceIterator const& xx,
408 IdxRangeSliceIterator const& yy)
409 {
410 return !(xx < yy);
411 }
412
414 friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator+(
417 {
418 return i += n;
419 }
420
422 friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator+(
425 {
426 return i += n;
427 }
428
430 friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator-(
433 {
434 return i -= n;
435 }
436
438 friend KOKKOS_FUNCTION constexpr difference_type operator-(
439 IdxRangeSliceIterator const& xx,
440 IdxRangeSliceIterator const& yy)
441 {
442 assert(xx.m_stride == yy.m_stride);
443 return (yy.m_value > xx.m_value)
444 ? ((-static_cast<difference_type>(yy.m_value - xx.m_value))
445 / ddc::get<Grid1D>(xx.m_stride))
446 : ((xx.m_value - yy.m_value) / ddc::get<Grid1D>(xx.m_stride));
447 }
448};
449
451template <class T>
453
454template <class... Dims>
455struct IdxRangeToSlice<ddc::detail::TypeSeq<Dims...>>
456{
457 using value = IdxRangeSlice<Dims...>;
458};
459
461template <class T>
462using to_subidx_range_collection = typename IdxRangeToSlice<T>::value;
A class which describes a collection of equally spaced Idxs which form a index range.
Definition idx_range_slice.hpp:38
KOKKOS_FUNCTION IdxRangeSlice(Idx< Dims... > front, IdxStep< Dims... > size, IdxStep< Dims... > stride)
Build a IdxRangeSlice from vectors of valid Idxs in each dimension.
Definition idx_range_slice.hpp:63
KOKKOS_FUNCTION IdxRangeSlice(DDoms const &... valid_indices)
Build a IdxRangeSlice from a set of 1D IdxRangeSlices.
Definition idx_range_slice.hpp:81
KOKKOS_FUNCTION std::size_t get_index(Idx< Dim > elem) const
Get the index of the Idx within the index range slice.
Definition idx_range_slice.hpp:135
KOKKOS_FUNCTION constexpr std::size_t size() const
Get the total number of elements in the index range slice.
Definition idx_range_slice.hpp:169
KOKKOS_FUNCTION constexpr IdxStep< QueryDDim > extent() const noexcept
Get the size of the index range slice in the specified dimension.
Definition idx_range_slice.hpp:159
KOKKOS_FUNCTION auto end() const
Get the iterator to the end of the IdxRangeSlice.
Definition idx_range_slice.hpp:235
KOKKOS_FUNCTION constexpr Idx< Dims... > front() const noexcept
Get the first element in the index range slice.
Definition idx_range_slice.hpp:179
KOKKOS_FUNCTION constexpr IdxStep< QueryDim > stride() const noexcept
Get the stride from one element of the index range slice to another.
Definition idx_range_slice.hpp:202
KOKKOS_FUNCTION auto begin() const
Get the iterator to the first element of the IdxRangeSlice.
Definition idx_range_slice.hpp:223
KOKKOS_FUNCTION bool contains(Idx< DDims... > elem) const
Check if the specified Idx is found in this index range slice.
Definition idx_range_slice.hpp:96
KOKKOS_FUNCTION constexpr IdxStep< Dims... > strides() const noexcept
Get the strides from one element of the index range slice to another.
Definition idx_range_slice.hpp:212
KOKKOS_FUNCTION constexpr Idx< Dims... > back() const noexcept
Get the last element in the index range slice.
Definition idx_range_slice.hpp:189
IdxRangeSlice()=default
Default constructor for IdxRangeSlice creating an empty index range.
KOKKOS_FUNCTION constexpr IdxStep< Dims... > extents() const noexcept
Get the size of the index range slice in each dimension.
Definition idx_range_slice.hpp:146
KOKKOS_FUNCTION bool contains(IdxRange< DDims... > idx_range) const
Check if all elements of the specified IdxRange are found in this index range slice.
Definition idx_range_slice.hpp:115
An iterator type for the IdxRangeSlice.
Definition idx_range_slice.hpp:247
friend KOKKOS_FUNCTION constexpr bool operator<=(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare the order of iterators.
Definition idx_range_slice.hpp:398
friend KOKKOS_FUNCTION constexpr bool operator>=(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare the order of iterators.
Definition idx_range_slice.hpp:406
IdxStep< Grid1D > stride_type
The type of the stride between values.
Definition idx_range_slice.hpp:260
friend KOKKOS_FUNCTION constexpr bool operator>(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare the order of iterators.
Definition idx_range_slice.hpp:390
KOKKOS_FUNCTION constexpr Idx< Grid1D > operator[](difference_type n) const
Access the n-th following element.
Definition idx_range_slice.hpp:360
friend KOKKOS_FUNCTION constexpr bool operator==(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare iterator equality.
Definition idx_range_slice.hpp:366
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator & operator++()
The prefix increment operator.
Definition idx_range_slice.hpp:294
friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator-(IdxRangeSliceIterator i, difference_type n)
Decrement an iterator by n elements.
Definition idx_range_slice.hpp:430
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator(Idx< Grid1D > value, IdxStep< Grid1D > stride)
Build an iterator using the current value and the distance to the following element.
Definition idx_range_slice.hpp:273
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator & operator--()
The prefix decrement operator.
Definition idx_range_slice.hpp:315
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator++(int)
The postfix increment operator.
Definition idx_range_slice.hpp:304
friend KOKKOS_FUNCTION constexpr difference_type operator-(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Decrement an iterator by n elements.
Definition idx_range_slice.hpp:438
friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator+(IdxRangeSliceIterator i, difference_type n)
Increment an iterator by n elements.
Definition idx_range_slice.hpp:414
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator & operator+=(difference_type n)
Increment the current iterator by n elements.
Definition idx_range_slice.hpp:337
std::ptrdiff_t difference_type
The type that can be used to increment the iterator.
Definition idx_range_slice.hpp:263
friend KOKKOS_FUNCTION constexpr bool operator!=(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare iterator non-equality.
Definition idx_range_slice.hpp:374
friend KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator+(difference_type n, IdxRangeSliceIterator i)
Increment an iterator by n elements.
Definition idx_range_slice.hpp:422
KOKKOS_FUNCTION constexpr Idx< Grid1D > operator*() const noexcept
Get the value referred to by the iterator.
Definition idx_range_slice.hpp:285
Idx< Grid1D > value_type
The type of the values stored in the iterator.
Definition idx_range_slice.hpp:257
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator operator--(int)
The postfix decrement operator.
Definition idx_range_slice.hpp:325
KOKKOS_FUNCTION constexpr IdxRangeSliceIterator & operator-=(difference_type n)
Decrement the current iterator by n elements.
Definition idx_range_slice.hpp:348
friend KOKKOS_FUNCTION constexpr bool operator<(IdxRangeSliceIterator const &xx, IdxRangeSliceIterator const &yy)
Compare the order of iterators.
Definition idx_range_slice.hpp:382
std::random_access_iterator_tag iterator_category
The type of iterator.
Definition idx_range_slice.hpp:254
A class to create a IdxRangeSlice type from a TypeSeq.
Definition idx_range_slice.hpp:452
Definition idx_range_slice.hpp:19