2#include <UnCompute/Containers/ArraySlice.h>
3#include <UnCompute/Memory/IAllocator.h>
4#include <UnCompute/Memory/SystemAllocator.h>
15 static_assert(!std::is_const_v<T>);
20 inline void AllocateStorage(USize count)
22 if (m_pAllocator ==
nullptr)
28 void* pData = m_pAllocator->
Allocate(count *
sizeof(T), std::max(
static_cast<USize
>(16),
alignof(T)));
32 inline void AllocateStorage(USize count,
const T& value)
34 AllocateStorage(count);
35 for (USize i = 0; i < count; ++i)
51 inline void DeallocateStorage()
53 DeallocateStorage(m_Storage);
63 : m_pAllocator(other.m_pAllocator)
65 AllocateStorage(other.
Length());
71 : m_Storage(other.m_Storage)
72 , m_pAllocator(other.m_pAllocator)
83 AllocateStorage(data.
Length());
96 AllocateStorage(other.
Length());
106 m_Storage = other.m_Storage;
107 m_pAllocator = other.m_pAllocator;
108 other.m_Storage = {};
121 : m_pAllocator(pAllocator)
129 inline explicit HeapArray(USize size,
const T& value = {})
132 AllocateStorage(size, value);
141 : m_pAllocator(pAllocator)
143 AllocateStorage(size, value);
150 inline void Resize(USize size,
const T& value = {})
153 AllocateStorage(size);
157 m_Storage[i] = value;
159 DeallocateStorage(temp);
170 return m_Storage(beginIndex, endIndex);
178 return m_Storage[index];
186 [[nodiscard]]
inline SSize
IndexOf(
const T& value)
const
188 return m_Storage.IndexOf(value);
198 return m_Storage.LastIndexOf(value);
210 [[nodiscard]]
inline USize
Length()
const
212 return m_Storage.Length();
216 [[nodiscard]]
inline bool Empty()
const
222 [[nodiscard]]
inline bool Any()
const
236 return m_Storage.Data();
240 [[nodiscard]]
inline const T*
Data()
const
242 return m_Storage.Data();
252 return m_Storage.CopyDataTo(destination);
263 [[nodiscard]]
inline const T* begin()
const
265 return m_Storage.begin();
268 [[nodiscard]]
inline const T* end()
const
270 return m_Storage.end();
273 inline operator ArraySlice<const T>() const
278 inline operator ArraySlice<T>()
This class represents a non-owning slice of contiguously stored elements.
Definition: ArraySlice.h:12
T * Data() const noexcept
Get pointer to the beginning of the slice.
Definition: ArraySlice.h:185
bool Empty() const noexcept
Check if the slice is empty.
Definition: ArraySlice.h:167
USize CopyDataTo(ArraySlice< std::remove_const_t< T > > destination) const
Copy data from this slice to another.
Definition: ArraySlice.h:195
USize Length() const noexcept
Length of the slice.
Definition: ArraySlice.h:161
Fixed-size heap-allocated array.
Definition: HeapArray.h:14
void Reset()
Reset the array to empty state.
Definition: HeapArray.h:228
HeapArray & operator=(HeapArray &&other) noexcept
Move assignment.
Definition: HeapArray.h:103
bool Empty() const
Check if the array is empty.
Definition: HeapArray.h:216
ArraySlice< T > operator()(USize beginIndex, USize endIndex) const noexcept
Create a subslice from this array.
Definition: HeapArray.h:168
bool Contains(const T &value)
Check if the element is present in the array.
Definition: HeapArray.h:204
HeapArray()=default
Create an empty array.
bool Any() const
Check if the array has any elements.
Definition: HeapArray.h:222
HeapArray(USize size, const T &value={})
Create an array.
Definition: HeapArray.h:129
USize CopyDataTo(ArraySlice< T > destination) const
Copy data from this array to a slice.
Definition: HeapArray.h:250
const T * Data() const
Get pointer to the beginning of the array.
Definition: HeapArray.h:240
HeapArray(IAllocator *pAllocator)
Create an array.
Definition: HeapArray.h:120
SSize LastIndexOf(const T &value) const
Get index of the last element equal to the passed value.
Definition: HeapArray.h:196
HeapArray(const ArraySlice< const T > &data)
Create an array.
Definition: HeapArray.h:80
HeapArray(const HeapArray &other)
Copy constructor.
Definition: HeapArray.h:62
HeapArray & operator=(const HeapArray &other)
Copy assignment.
Definition: HeapArray.h:88
T & operator[](USize index) const noexcept
Get an element by index.
Definition: HeapArray.h:176
SSize IndexOf(const T &value) const
Get index of the first element equal to the passed value.
Definition: HeapArray.h:186
HeapArray(HeapArray &&other) noexcept
Move constructor.
Definition: HeapArray.h:70
T * Data()
Get pointer to the beginning of the array.
Definition: HeapArray.h:234
static HeapArray< T > CopyFrom(const ArraySlice< const T > &arraySlice)
Create a heap array by copying data from another container.
Definition: HeapArray.h:258
HeapArray(IAllocator *pAllocator, USize size, const T &value={})
Create an array.
Definition: HeapArray.h:140
USize Length() const
\bried Length of the array.
Definition: HeapArray.h:210
void Resize(USize size, const T &value={})
Set size of the array.
Definition: HeapArray.h:150
An interface for memory allocators.
Definition: IAllocator.h:8
virtual void Deallocate(void *pointer)=0
Deallocate memory.
virtual void * Allocate(USize size, USize alignment)=0
Allocate memory.
This allocator uses platform-specific aligned versions of malloc() and free().
Definition: SystemAllocator.h:8
static SystemAllocator * Get()
Get global static instance of the system allocator.
Definition: SystemAllocator.h:17