2#include <UnCompute/Base/Base.h>
13 T* m_pBegin =
nullptr;
20 using AddConst = std::conditional_t<std::is_const_v<T>,
const TCont, TCont>;
22 using StdVector = AddConst<std::vector<std::remove_const_t<T>>>;
24 using StdArray = AddConst<std::array<std::remove_const_t<T>, N>>;
35 , m_pEnd(pData + count)
65 : m_pBegin(vector.data())
66 , m_pEnd(vector.data() + vector.size())
77 : m_pBegin(array.data())
78 , m_pEnd(array.data() + array.size())
88 : m_pBegin(lst.begin())
101 UN_Assert(beginIndex <
Length() && endIndex <=
Length(),
"Index out of range");
102 return ArraySlice(m_pBegin + beginIndex, m_pBegin + endIndex);
110 UN_Assert(index <
Length(),
"Index out of range");
111 return m_pBegin[index];
119 [[nodiscard]]
inline SSize
IndexOf(
const T& value)
const noexcept
121 auto length =
static_cast<SSize
>(
Length());
122 for (SSize i = 0; i < length; ++i)
124 if (value == m_pBegin[i])
138 [[nodiscard]]
inline SSize
LastIndexOf(
const T& value)
const noexcept
140 auto length =
static_cast<SSize
>(
Length());
141 for (SSize i = length - 1; i >= 0; --i)
143 if (value == m_pBegin[i])
155 [[nodiscard]]
inline bool Contains(
const T& value)
const noexcept
161 [[nodiscard]]
inline USize
Length() const noexcept
163 return m_pEnd - m_pBegin;
167 [[nodiscard]]
inline bool Empty() const noexcept
173 [[nodiscard]]
inline bool Any() const noexcept
181 m_pBegin = m_pEnd =
nullptr;
185 [[nodiscard]]
inline T*
Data() const noexcept
197 USize size = std::min(
Length(), destination.Length());
199 if constexpr (std::is_trivially_copyable_v<T>)
201 memcpy(destination.Data(),
Data(), size *
sizeof(T));
205 for (USize i = 0; i < size; ++i)
207 destination.m_pBegin[i] = m_pBegin[i];
218 result.m_pBegin =
reinterpret_cast<T1*
>(m_pBegin);
219 result.m_pEnd =
reinterpret_cast<T1*
>(m_pEnd);
223 [[nodiscard]]
inline const T* begin() const noexcept
228 [[nodiscard]]
inline const T* end() const noexcept
233 inline operator ArraySlice<std::add_const_t<T>>()
const noexcept
238 inline friend bool operator==(
const ArraySlice& lhs,
const ArraySlice& rhs)
noexcept
240 return lhs.m_pBegin == rhs.m_pBegin && lhs.m_pEnd == rhs.m_pEnd;
243 inline friend bool operator!=(
const ArraySlice& lhs,
const ArraySlice& rhs)
noexcept
245 return lhs.m_pBegin != rhs.m_pBegin || lhs.m_pEnd != rhs.m_pEnd;
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