UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
ArraySlice.h
1#pragma once
2#include <UnCompute/Base/Base.h>
3#include <array>
4#include <cstring>
5#include <vector>
6
7namespace UN
8{
10 template<class T>
11 class ArraySlice final
12 {
13 T* m_pBegin = nullptr;
14 T* m_pEnd = nullptr;
15
16 template<class T1>
17 friend class ArraySlice;
18
19 template<class TCont>
20 using AddConst = std::conditional_t<std::is_const_v<T>, const TCont, TCont>;
21
22 using StdVector = AddConst<std::vector<std::remove_const_t<T>>>;
23 template<USize N>
24 using StdArray = AddConst<std::array<std::remove_const_t<T>, N>>;
25
26 public:
27 inline ArraySlice() = default;
28
33 inline ArraySlice(T* pData, USize count) noexcept
34 : m_pBegin(pData)
35 , m_pEnd(pData + count)
36 {
37 }
38
43 inline ArraySlice(T* pBegin, T* pEnd) noexcept
44 : m_pBegin(pBegin)
45 , m_pEnd(pEnd)
46 {
47 }
48
54 template<USize N>
55 inline ArraySlice(T (&arr)[N]) noexcept
56 : m_pBegin(arr)
57 , m_pEnd(arr + N)
58 {
59 }
60
64 inline ArraySlice(StdVector& vector) noexcept // NOLINT
65 : m_pBegin(vector.data())
66 , m_pEnd(vector.data() + vector.size())
67 {
68 }
69
75 template<USize N>
76 inline ArraySlice(StdArray<N>& array) noexcept // NOLINT
77 : m_pBegin(array.data())
78 , m_pEnd(array.data() + array.size())
79 {
80 }
81
87 inline ArraySlice(std::initializer_list<T> lst) noexcept
88 : m_pBegin(lst.begin())
89 , m_pEnd(lst.end())
90 {
91 }
92
99 inline ArraySlice operator()(USize beginIndex, USize endIndex) const
100 {
101 UN_Assert(beginIndex < Length() && endIndex <= Length(), "Index out of range");
102 return ArraySlice(m_pBegin + beginIndex, m_pBegin + endIndex);
103 }
104
108 inline T& operator[](USize index) const
109 {
110 UN_Assert(index < Length(), "Index out of range");
111 return m_pBegin[index];
112 }
113
119 [[nodiscard]] inline SSize IndexOf(const T& value) const noexcept
120 {
121 auto length = static_cast<SSize>(Length());
122 for (SSize i = 0; i < length; ++i)
123 {
124 if (value == m_pBegin[i])
125 {
126 return i;
127 }
128 }
129
130 return -1;
131 }
132
138 [[nodiscard]] inline SSize LastIndexOf(const T& value) const noexcept
139 {
140 auto length = static_cast<SSize>(Length());
141 for (SSize i = length - 1; i >= 0; --i)
142 {
143 if (value == m_pBegin[i])
144 {
145 return i;
146 }
147 }
148
149 return -1;
150 }
151
155 [[nodiscard]] inline bool Contains(const T& value) const noexcept
156 {
157 return IndexOf(value) != -1;
158 }
159
161 [[nodiscard]] inline USize Length() const noexcept
162 {
163 return m_pEnd - m_pBegin;
164 }
165
167 [[nodiscard]] inline bool Empty() const noexcept
168 {
169 return Length() == 0;
170 }
171
173 [[nodiscard]] inline bool Any() const noexcept
174 {
175 return Length() > 0;
176 }
177
179 inline void Reset() noexcept
180 {
181 m_pBegin = m_pEnd = nullptr;
182 }
183
185 [[nodiscard]] inline T* Data() const noexcept
186 {
187 return m_pBegin;
188 }
189
195 inline USize CopyDataTo(ArraySlice<std::remove_const_t<T>> destination) const
196 {
197 USize size = std::min(Length(), destination.Length());
198
199 if constexpr (std::is_trivially_copyable_v<T>)
200 {
201 memcpy(destination.Data(), Data(), size * sizeof(T));
202 }
203 else
204 {
205 for (USize i = 0; i < size; ++i)
206 {
207 destination.m_pBegin[i] = m_pBegin[i];
208 }
209 }
210
211 return size;
212 }
213
214 template<class T1>
215 inline ArraySlice<T1> ReinterpretAs() const
216 {
217 ArraySlice<T1> result;
218 result.m_pBegin = reinterpret_cast<T1*>(m_pBegin);
219 result.m_pEnd = reinterpret_cast<T1*>(m_pEnd);
220 return result;
221 }
222
223 [[nodiscard]] inline const T* begin() const noexcept
224 {
225 return m_pBegin;
226 }
227
228 [[nodiscard]] inline const T* end() const noexcept
229 {
230 return m_pEnd;
231 }
232
233 inline operator ArraySlice<std::add_const_t<T>>() const noexcept // NOLINT(google-explicit-constructor)
234 {
235 return { Data(), Data() + Length() };
236 }
237
238 inline friend bool operator==(const ArraySlice& lhs, const ArraySlice& rhs) noexcept
239 {
240 return lhs.m_pBegin == rhs.m_pBegin && lhs.m_pEnd == rhs.m_pEnd;
241 }
242
243 inline friend bool operator!=(const ArraySlice& lhs, const ArraySlice& rhs) noexcept
244 {
245 return lhs.m_pBegin != rhs.m_pBegin || lhs.m_pEnd != rhs.m_pEnd;
246 }
247 };
248} // namespace UN
This class represents a non-owning slice of contiguously stored elements.
Definition: ArraySlice.h:12
ArraySlice(std::initializer_list< T > lst) noexcept
Create an array slice.
Definition: ArraySlice.h:87
T * Data() const noexcept
Get pointer to the beginning of the slice.
Definition: ArraySlice.h:185
ArraySlice(StdArray< N > &array) noexcept
Create an array slice.
Definition: ArraySlice.h:76
bool Contains(const T &value) const noexcept
Check if the element is present in the slice.
Definition: ArraySlice.h:155
T & operator[](USize index) const
Get an element by index.
Definition: ArraySlice.h:108
bool Empty() const noexcept
Check if the slice is empty.
Definition: ArraySlice.h:167
void Reset() noexcept
Reset the slice to empty state.
Definition: ArraySlice.h:179
USize CopyDataTo(ArraySlice< std::remove_const_t< T > > destination) const
Copy data from this slice to another.
Definition: ArraySlice.h:195
ArraySlice(T *pData, USize count) noexcept
Create an array slice.
Definition: ArraySlice.h:33
ArraySlice(T(&arr)[N]) noexcept
Create an array slice.
Definition: ArraySlice.h:55
ArraySlice(StdVector &vector) noexcept
Create an array slice.
Definition: ArraySlice.h:64
SSize IndexOf(const T &value) const noexcept
Get index of the first element equal to the passed value.
Definition: ArraySlice.h:119
ArraySlice(T *pBegin, T *pEnd) noexcept
Create an array slice.
Definition: ArraySlice.h:43
USize Length() const noexcept
Length of the slice.
Definition: ArraySlice.h:161
SSize LastIndexOf(const T &value) const noexcept
Get index of the last element equal to the passed value.
Definition: ArraySlice.h:138
bool Any() const noexcept
Check if the slice has any elements.
Definition: ArraySlice.h:173
ArraySlice operator()(USize beginIndex, USize endIndex) const
Create a subslice from this array slice.
Definition: ArraySlice.h:99