71 Array() : mData(nullptr), mSize(0), mCapacity(0) {}
88 Array(
size_t size,
const T* values) : mData(nullptr), mSize(0), mCapacity(0) {
89 if (values &&
size > 0) {
90 mData =
new (std::nothrow) T[
size];
95 if constexpr (std::is_trivially_copyable<T>::value) {
96 memcpy(
reinterpret_cast<void*
>(mData), values,
sizeof(T) *
size);
98 for (
size_t i = 0; i <
size; ++i) {
119 : mData(other.mData), mSize(other.mSize), mCapacity(other.mCapacity) {
120 other.mData =
nullptr;
137 if (
this != &other) {
142 mCapacity = other.mCapacity;
144 other.mData =
nullptr;
189 bool get(
size_t index, T& outValue)
const {
191 outValue = mData[index];
205 bool set(
size_t index,
const T& value) {
207 mData[index] = value;
218 size_t size()
const {
return mSize; }
240 const T*
raw()
const {
return mData; }
250 if (newCapacity <= mCapacity) {
254 T* new_data =
new (std::nothrow) T[newCapacity];
260 for (
size_t i = 0; i < mSize; ++i) {
261 new_data[i] = std::move(mData[i]);
265 if constexpr (std::is_trivially_default_constructible<T>::value) {
266 memset(
reinterpret_cast<void*
>(new_data + mSize), 0,
sizeof(T) * (newCapacity - mSize));
268 for (
size_t i = mSize; i < newCapacity; ++i) {
275 mCapacity = newCapacity;
287 if (mSize >= mCapacity) {
288 size_t newCapacity = (mCapacity == 0) ? 1 : mCapacity * 2;
293 mData[mSize++] = value;
305 if (mSize >= mCapacity) {
306 size_t newCapacity = (mCapacity == 0) ? 1 : mCapacity * 2;
311 mData[mSize++] = std::move(value);
334 if (mCapacity == mSize) {
338 T* new_data =
nullptr;
340 new_data =
new (std::nothrow) T[mSize];
345 for (
size_t i = 0; i < mSize; ++i) {
346 new_data[i] = std::move(mData[i]);
bool isEmpty() const
Checks if the array is empty.
Definition Array.h:233
const T & operator[](size_t index) const
Provides read-only access to the array elements without bounds checking.
Definition Array.h:177
Array()
Constructs an empty Array.
Definition Array.h:71
Array(size_t capacity)
Constructs an Array with the given capacity, allocating memory.
Definition Array.h:78
bool shrink()
Reduces the capacity to fit the current size.
Definition Array.h:333
bool push(const T &value)
Adds a new element to the end of the array using copy semantics.
Definition Array.h:286
Array & operator=(const Array &other)=delete
Copy assignment operator (deleted to prevent accidental copying).
const T * raw() const
Retrieves a const pointer to the underlying data.
Definition Array.h:240
bool get(size_t index, T &outValue) const
Provides access to the array elements with bounds checking.
Definition Array.h:189
Array(Array &&other) noexcept
Move constructor.
Definition Array.h:118
size_t capacity() const
Returns the capacity of the array.
Definition Array.h:225
bool push(T &&value)
Adds a new element to the end of the array using move semantics.
Definition Array.h:304
Array(const Array &other)=delete
Copy constructor (deleted to prevent accidental copying).
size_t size() const
Returns the size of the array.
Definition Array.h:218
Array(size_t size, const T *values)
Constructs an Array from an existing C-style array.
Definition Array.h:88
Array & operator=(Array &&other) noexcept
Move assignment operator.
Definition Array.h:136
T & operator[](size_t index)
Provides access to the array elements without bounds checking.
Definition Array.h:167
void clear()
Clears the array, setting its size to zero.
Definition Array.h:318
~Array()
Destructor that deallocates the array memory.
Definition Array.h:154
bool reserve(size_t newCapacity)
Reserves memory for at least the specified number of elements.
Definition Array.h:249
bool set(size_t index, const T &value)
Sets the value of an element in the array.
Definition Array.h:205
Contains all classes and functions related to the Uniot Core.