/home/runner/work/amr-wind/amr-wind/amr-wind/core/Slice.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/core/Slice.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
Slice.H
Go to the documentation of this file.
1#ifndef SLICE_H
2#define SLICE_H
3
4#include <iterator>
5#include "AMReX_Vector.H"
6#include "AMReX_Gpu.H"
7
8namespace amr_wind::utils {
9
10template <typename T>
11struct Slice
12{
13 using value_type = T;
14 using size_t = std::size_t;
15 using size_type = std::size_t;
16 using difference_type = std::ptrdiff_t;
17 using reference = T&;
18 using pointer = T*;
19 using iterator = T*;
20 using reverse_iterator = std::reverse_iterator<iterator>;
21 using const_reference = const T&;
22 using const_pointer = const T*;
23 using const_iterator = const T*;
24 using const_reverse_iterator = const std::reverse_iterator<iterator>;
25
28
29 explicit Slice() : ptr_begin(nullptr), ptr_end(nullptr) {}
30
31 explicit Slice(T* pbegin, const size_t n)
32 : ptr_begin(pbegin), ptr_end(pbegin + n)
33 {}
34
35 explicit Slice(T* pbegin, T* pend) : ptr_begin(pbegin), ptr_end(pend) {}
36
37 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE reference
38 operator[](const size_t idx)
39 {
40 return *(ptr_begin + idx);
41 }
42
43 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE const_reference
44 operator[](const size_type idx) const
45 {
46 return *(ptr_begin + idx);
47 }
48
49 AMREX_GPU_HOST_DEVICE size_type size() const
50 {
51 return (ptr_end - ptr_begin);
52 }
53
54 AMREX_GPU_HOST_DEVICE pointer data() { return ptr_begin; }
55 AMREX_GPU_HOST_DEVICE iterator begin() { return ptr_begin; }
56 AMREX_GPU_HOST_DEVICE iterator end() { return ptr_end; }
57 AMREX_GPU_HOST_DEVICE const_iterator data() const { return ptr_begin; }
58 AMREX_GPU_HOST_DEVICE const_iterator begin() const { return ptr_begin; }
59 AMREX_GPU_HOST_DEVICE const_iterator end() const { return ptr_end; }
60 AMREX_GPU_HOST_DEVICE const_iterator cbegin() const { return ptr_begin; }
61 AMREX_GPU_HOST_DEVICE const_iterator cend() const { return ptr_end; }
62};
63
64template <typename T>
65inline Slice<T>
66slice(std::vector<T>& vec, const size_t start, const size_t count)
67{
68 AMREX_ASSERT((start + count) <= vec.size());
69 return Slice<T>{&vec[start], count};
70}
71
72template <typename T>
73inline Slice<T> slice(std::vector<T>& vec, const size_t start)
74{
75 AMREX_ASSERT(start < vec.size());
76 return slice(vec, start, vec.size() - start);
77}
78
79template <typename T>
80inline Slice<T>
81slice(amrex::Vector<T>& vec, const size_t start, const size_t count)
82{
83 AMREX_ASSERT((start + count) <= static_cast<size_t>(vec.size()));
84 return Slice<T>{&vec[start], count};
85}
86
87template <typename T>
88inline Slice<T> slice(amrex::Vector<T>& vec, const size_t start)
89{
90 AMREX_ASSERT(start < static_cast<size_t>(vec.size()));
91 return slice(vec, start, vec.size() - start);
92}
93
94template <typename T>
95inline Slice<T>
96slice(amrex::Gpu::DeviceVector<T>& vec, const size_t start, const size_t count)
97{
98 AMREX_ASSERT((start + count) <= static_cast<size_t>(vec.size()));
99 return Slice<T>{(vec.data() + start), count};
100}
101
102template <typename T>
103inline Slice<T> slice(amrex::Gpu::DeviceVector<T>& vec, const size_t start)
104{
105 AMREX_ASSERT(start < static_cast<size_t>(vec.size()));
106 return slice(vec, start, vec.size() - start);
107}
108
109} // namespace amr_wind::utils
110
111#endif /* SLICE_H */
Definition MultiParser.H:7
Slice< T > slice(std::vector< T > &vec, const size_t start, const size_t count)
Definition Slice.H:66
Definition Slice.H:12
AMREX_GPU_HOST_DEVICE const_iterator cbegin() const
Definition Slice.H:60
T * pointer
Definition Slice.H:18
AMREX_GPU_HOST_DEVICE const_iterator cend() const
Definition Slice.H:61
pointer ptr_begin
Definition Slice.H:26
const T * const_iterator
Definition Slice.H:23
AMREX_GPU_HOST_DEVICE const_iterator end() const
Definition Slice.H:59
std::size_t size_type
Definition Slice.H:15
Slice(T *pbegin, const size_t n)
Definition Slice.H:31
AMREX_GPU_HOST_DEVICE pointer data()
Definition Slice.H:54
T value_type
Definition Slice.H:13
Slice(T *pbegin, T *pend)
Definition Slice.H:35
AMREX_GPU_HOST_DEVICE const_iterator begin() const
Definition Slice.H:58
T * iterator
Definition Slice.H:19
AMREX_GPU_HOST_DEVICE iterator end()
Definition Slice.H:56
std::ptrdiff_t difference_type
Definition Slice.H:16
std::reverse_iterator< iterator > reverse_iterator
Definition Slice.H:20
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE const_reference operator[](const size_type idx) const
Definition Slice.H:44
AMREX_GPU_HOST_DEVICE iterator begin()
Definition Slice.H:55
AMREX_GPU_HOST_DEVICE size_type size() const
Definition Slice.H:49
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE reference operator[](const size_t idx)
Definition Slice.H:38
AMREX_GPU_HOST_DEVICE const_iterator data() const
Definition Slice.H:57
Slice()
Definition Slice.H:29
const T * const_pointer
Definition Slice.H:22
std::size_t size_t
Definition Slice.H:14
pointer ptr_end
Definition Slice.H:27
const std::reverse_iterator< iterator > const_reverse_iterator
Definition Slice.H:24
const T & const_reference
Definition Slice.H:21
T & reference
Definition Slice.H:17