5#include <Infinity/Types/Containers/ArrayBase.hpp>
6#include <Infinity/Types/Core/Span.hpp>
7#include <Infinity/Types/Core/Value.hpp>
20 class ArraySerializer;
95 Array(
size_t size) :
ArrayBase(), _data(_alloc(size), size), _capacity(size), _size(size)
121 std::copy(data.begin(), data.end(), _data.data());
134 std::copy(data.begin(), data.end(), _data.data());
147 _data(nullptr, size_t(0)),
148 _capacity(other._capacity),
153 _data = Core::span<T>(_alloc(_capacity), _size);
154 std::copy(other.
_data.begin(), other.
_data.end(), _data.data());
169 _capacity(other._capacity),
172 other._data = Core::span<T>();
209 Array temp(std::move(rhs));
231 if constexpr (std::is_base_of_v<Core::Data, T>)
233 return Infinity::Types::getTypeID<T>();
235 return Infinity::Types::getTypeID<Core::Value<T>>();
239 std::unique_ptr<Core::Base>
clone()
const override
241 return std::make_unique<Array<T>>(*this);
253 void copy(
const T* data,
size_t count)
257 _data = Core::span<T>(_alloc(count), count);
260 if constexpr (!std::is_base_of_v<Core::Data, T>)
262 std::memcpy((
void*)_data.data(), data, count *
sizeof(T));
264 for (
size_t i = 0; i < _size; ++i)
279 T& operator [](
size_t index)
292 const T& operator [](
size_t index)
const
320 const T&
at(
size_t index)
const
335 if (_size == _capacity)
337 _reallocate(_capacity == 0 ? 8 : _capacity * 2);
340 _data[_size] = value;
353 template <
typename... Args>
356 if (_size == _capacity)
358 _reallocate(_capacity == 0 ? 8 : _capacity * 2);
361 new (_data.data() + _size) T(std::forward<Args>(args)...);
376 void insert(
size_t index,
const T& value)
380 throw std::out_of_range(
"Index out of range");
383 if (_size == _capacity)
385 _reallocate(_capacity == 0 ? 8 : _capacity * 2);
388 for (
size_t i = _size; i > index; --i)
390 _data[i] = _data[i - 1];
393 _data[index] = value;
408 if (index >= _size) {
409 throw std::out_of_range(
"Index out of range");
411 if constexpr (!std::is_trivially_destructible_v<T>)
415 std::move(_data.begin() + index + 1, _data.begin() + _size, _data.begin() + index);
431 if (capacity > _capacity)
433 _reallocate(capacity);
446 virtual void resize(
size_t newSize)
override
448 if (newSize == _size)
453 if constexpr (!std::is_trivially_destructible_v<T>)
455 for (
size_t i = newSize; i < _size; ++i)
464 if (newSize > _capacity)
466 _reallocate(newSize);
469 if constexpr (!std::is_trivially_default_constructible_v<T>)
471 for (
size_t i = _size; i < newSize; ++i)
473 new (_data.data() + i) T();
488 if (_size < _capacity)
506 if constexpr (!std::is_trivially_destructible_v<T>)
508 for (
size_t i = 0; i < _size; ++i)
523 return std::is_base_of_v<Core::Data, T>;
533 return !std::is_base_of_v<Core::Data, T>;
545 virtual std::shared_ptr<Core::Data>
dataAt(
size_t index)
override
547 if constexpr (std::is_base_of_v<Core::Data, T>)
549 return std::make_shared<T>(at(index));
551 return std::make_shared<Core::Value<T>>(at(index));
564 virtual std::shared_ptr<const Core::Data>
dataAt(
size_t index)
const override
566 if constexpr (std::is_base_of_v<Core::Data, T>)
568 return std::make_shared<const T>(at(index));
570 return std::make_shared<const Core::Value<T>>(at(index));
585 if constexpr (std::is_base_of_v<Core::Data, T>)
587 const T* ptr =
dynamic_cast<const T*
>(&data);
588 if (!ptr)
throw std::invalid_argument(
"Invalid type for setData()");
592 if (!val)
throw std::invalid_argument(
"Invalid type for setData()");
593 _data[index] = val->
get();
636 _data = Core::span<T>();
652 return _data.front();
664 return _data.front();
676 return _data[size() - 1];
688 return _data[size() - 1];
696 size_t size() const noexcept
override
731 return &_data[index];
742 virtual const void*
pointer(
size_t index)
const override
744 return &_data[index];
825 std::swap(_data, other._data);
826 std::swap(_capacity, other._capacity);
827 std::swap(_size, other._size);
856 T* newData = _alloc(newCapacity);
859 if constexpr (std::is_trivially_copyable_v<T>)
861 std::memcpy((
void*)newData, _data.data(), _size *
sizeof(T));
862 }
else if constexpr (std::is_move_constructible_v<T>)
864 for (
size_t i = 0; i < _size; ++i)
866 newData[i] = std::move(_data[i]);
869 for (
size_t i = 0; i < _size; ++i)
871 newData[i] = _data[i];
878 _data = Core::span<T>(newData, newCapacity);
879 _capacity = newCapacity;
893 _data = Core::span<T>();
910 T* mem =
static_cast<T*
>(
operator new[](
sizeof(T) * size, std::align_val_t{64}));
911 if constexpr (!std::is_trivially_default_constructible_v<T>)
913 for(
size_t i = 0; i < size; ++i)
932 if constexpr (!std::is_trivially_destructible_v<T>)
934 for (
size_t i = 0; i < _size; ++i)
940 operator delete[](_data.data(), std::align_val_t{64});
943 friend class Infinity::IO::Data::ArraySerializer<T>;
Abstract base class for all array container types in the Infinity type system.
Definition ArrayBase.hpp:72
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Array(const std::vector< T > &data)
Constructs an array from a std::vector.
Definition Array.hpp:118
Array(Array &&other) noexcept
Move constructor.
Definition Array.hpp:166
virtual bool containsValues() const override
Checks if the array contains plain value types.
Definition Array.hpp:531
const T & back() const
Accesses the last element (const).
Definition Array.hpp:686
virtual bool containsData() const override
Checks if the array contains Core::Data-derived types.
Definition Array.hpp:521
T & front()
Accesses the first element.
Definition Array.hpp:650
Array & operator=(const Array &rhs)
Copy assignment operator.
Definition Array.hpp:192
Array & operator=(Array &&rhs) noexcept
Move assignment operator.
Definition Array.hpp:207
const_iterator begin() const noexcept
Returns a const iterator to the beginning.
Definition Array.hpp:779
static T * _alloc(size_t size)
Allocates aligned memory for the specified number of elements.
Definition Array.hpp:908
void reset() noexcept
Resets the array to an empty state.
Definition Array.hpp:836
virtual void * pointer(size_t index) override
Gets a void pointer to the element at the specified index.
Definition Array.hpp:729
std::unique_ptr< Core::Base > clone() const override
Definition Array.hpp:239
virtual size_t dimensions() const noexcept override
Gets the number of dimensions in this container.
Definition Array.hpp:752
void copy(const T *data, size_t count)
Replaces array contents by copying from raw data.
Definition Array.hpp:253
virtual std::shared_ptr< Core::Data > dataAt(size_t index) override
Gets a type-erased Data pointer to the element at the specified index.
Definition Array.hpp:545
virtual ~Array()
Destructor.
Definition Array.hpp:182
iterator begin() noexcept
Returns an iterator to the beginning.
Definition Array.hpp:767
void insert(size_t index, const T &value)
Inserts an element at the specified position.
Definition Array.hpp:376
void swap(Array &other) noexcept
Swaps the contents with another array.
Definition Array.hpp:823
Array(Core::span< T > data)
Constructs an array from a span.
Definition Array.hpp:131
virtual void setData(size_t index, const Core::Data &data) override
Sets an element from type-erased Data.
Definition Array.hpp:583
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition Array.hpp:797
void emplace_back(Args &&... args)
Constructs and appends an element in-place.
Definition Array.hpp:354
void push_back(const T &value)
Appends an element to the end of the array.
Definition Array.hpp:333
T * data()
Gets a pointer to the underlying data array.
Definition Array.hpp:604
size_t size() const noexcept override
Gets the number of elements in the array.
Definition Array.hpp:696
void _dealloc()
Deallocates the array's memory.
Definition Array.hpp:930
size_t _size
Current number of elements.
Definition Array.hpp:814
std::reverse_iterator< iterator > reverse_iterator
Definition Array.hpp:760
void reserve(size_t capacity)
Reserves capacity for at least the specified number of elements.
Definition Array.hpp:429
iterator end() noexcept
Returns an iterator to the end.
Definition Array.hpp:773
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the beginning.
Definition Array.hpp:803
T * releaseData()
Releases ownership of the underlying data array.
Definition Array.hpp:633
Core::span< T > _data
Span wrapping the allocated data.
Definition Array.hpp:812
const T & front() const
Accesses the first element (const).
Definition Array.hpp:662
void _reallocate(size_t newCapacity)
Reallocates the array with a new capacity.
Definition Array.hpp:854
Array()
Default constructor.
Definition Array.hpp:84
T * iterator
Definition Array.hpp:758
const T * const_iterator
Definition Array.hpp:759
virtual void resize(size_t newSize) override
Resizes the array to contain the specified number of elements.
Definition Array.hpp:446
T & at(size_t index)
Accesses element at specified index (checked).
Definition Array.hpp:306
Array(const T *data, size_t size)
Constructs an array by copying data from a raw pointer.
Definition Array.hpp:106
Array(size_t size)
Constructs an array with specified size.
Definition Array.hpp:95
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Array.hpp:761
size_t _capacity
Total allocated capacity.
Definition Array.hpp:813
Array(const Array &other)
Copy constructor.
Definition Array.hpp:145
void clear() noexcept
Removes all elements from the array.
Definition Array.hpp:504
virtual const Infinity::Types::TypeID & typeId() const override
Gets the TypeID for this array type.
Definition Array.hpp:219
virtual const Infinity::Types::TypeID & elementTypeId() override
Gets the TypeID for the element type.
Definition Array.hpp:229
size_t capacity() const noexcept
Gets the current capacity.
Definition Array.hpp:706
virtual const void * pointer(size_t index) const override
Gets a const void pointer to the element at the specified index.
Definition Array.hpp:742
const_reverse_iterator rend() const noexcept
Returns a const reverse iterator to the end.
Definition Array.hpp:809
void _delete() noexcept
Deallocates the array's memory.
Definition Array.hpp:888
virtual std::shared_ptr< const Core::Data > dataAt(size_t index) const override
Gets a const type-erased Data pointer to the element at the specified index.
Definition Array.hpp:564
void erase(size_t index)
Removes the element at the specified position.
Definition Array.hpp:406
T & back()
Accesses the last element.
Definition Array.hpp:674
const T * data() const
Gets a const pointer to the underlying data array.
Definition Array.hpp:616
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the beginning.
Definition Array.hpp:791
void shrink_to_fit()
Reduces capacity to match the current size.
Definition Array.hpp:486
const T & at(size_t index) const
Accesses element at specified index (checked, const).
Definition Array.hpp:320
bool empty() const noexcept
Checks if the array is empty.
Definition Array.hpp:716
const_iterator end() const noexcept
Returns a const iterator to the end.
Definition Array.hpp:785
Base class for complex data types with memory wrapping and property support.
Definition Data.hpp:49
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
T & get()
Gets a mutable reference to the underlying value.
Definition Value.inl:60
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71