2020-02-25 00:44:40 +01:00
|
|
|
#pragma once
|
2020-04-01 02:17:49 +02:00
|
|
|
#include "EngineTypes.h"
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
#include <iterator>
|
2020-02-25 00:44:40 +01:00
|
|
|
#include <assert.h>
|
2020-08-06 00:54:43 +02:00
|
|
|
#include <memory_resource>
|
|
|
|
|
#include <boost/serialization/serialization.hpp>
|
2020-02-25 00:44:40 +01:00
|
|
|
|
|
|
|
|
#ifndef DEFAULT_ALLOC_SIZE
|
|
|
|
|
#define DEFAULT_ALLOC_SIZE 16
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
namespace Seele
|
2020-02-25 00:44:40 +01:00
|
|
|
{
|
2021-10-23 00:22:35 +02:00
|
|
|
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>>
|
|
|
|
|
struct Array
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
template <typename X>
|
|
|
|
|
class IteratorBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
using value_type = X;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
|
using reference = X&;
|
|
|
|
|
using pointer = X*;
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
IteratorBase(X *x = nullptr)
|
|
|
|
|
: p(x)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
reference operator*() const
|
|
|
|
|
{
|
|
|
|
|
return *p;
|
|
|
|
|
}
|
|
|
|
|
pointer operator->() const
|
|
|
|
|
{
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator!=(const IteratorBase &other) const
|
|
|
|
|
{
|
|
|
|
|
return p != other.p;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator==(const IteratorBase &other) const
|
|
|
|
|
{
|
|
|
|
|
return p == other.p;
|
|
|
|
|
}
|
|
|
|
|
inline int operator-(const IteratorBase &other) const
|
|
|
|
|
{
|
|
|
|
|
return (int)(p - other.p);
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator++()
|
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator--()
|
|
|
|
|
{
|
|
|
|
|
p--;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator++(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
++*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator--(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
--*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
private:
|
|
|
|
|
X *p;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using value_type = T;
|
|
|
|
|
using allocator_type = Allocator;
|
|
|
|
|
using size_type = std::size_t;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
|
using pointer = T*;
|
|
|
|
|
using const_pointer = const T*;
|
|
|
|
|
using reference = value_type&;
|
|
|
|
|
using const_reference = const value_type&;
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
using Iterator = IteratorBase<T>;
|
|
|
|
|
using ConstIterator = IteratorBase<const T>;
|
|
|
|
|
using iterator = Iterator;
|
|
|
|
|
using const_iterator = ConstIterator;
|
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
constexpr Array() noexcept(noexcept(Allocator()))
|
|
|
|
|
: arraySize(0)
|
|
|
|
|
, allocated(DEFAULT_ALLOC_SIZE)
|
|
|
|
|
, allocator(Allocator())
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(DEFAULT_ALLOC_SIZE);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
constexpr explicit Array(const allocator_type& alloc)
|
|
|
|
|
: arraySize(0)
|
|
|
|
|
, allocated(DEFAULT_ALLOC_SIZE)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(DEFAULT_ALLOC_SIZE);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr Array(size_type size, const value_type& value, const allocator_type& alloc = allocator_type())
|
2021-10-23 00:22:35 +02:00
|
|
|
: arraySize(size)
|
|
|
|
|
, allocated(size)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(size);
|
|
|
|
|
for (size_type i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = value;
|
|
|
|
|
}
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
constexpr explicit Array(size_type size, const allocator_type& alloc = allocator_type())
|
|
|
|
|
: arraySize(size)
|
|
|
|
|
, allocated(size)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(size);
|
|
|
|
|
for (size_type i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[i]);
|
|
|
|
|
}
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
constexpr Array(std::initializer_list<T> init, const allocator_type& alloc = allocator_type())
|
|
|
|
|
: arraySize(init.size())
|
|
|
|
|
, allocated(init.size())
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(init.size());
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
std::uninitialized_copy(init.begin(), init.end(), begin());
|
|
|
|
|
}
|
|
|
|
|
Array(const Array &other)
|
|
|
|
|
: arraySize(other.arraySize)
|
|
|
|
|
, allocated(other.allocated)
|
|
|
|
|
, allocator(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.allocator))
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
std::uninitialized_copy(other.begin(), other.end(), begin());
|
|
|
|
|
}
|
|
|
|
|
Array(Array &&other) noexcept
|
|
|
|
|
: arraySize(std::move(other.arraySize))
|
|
|
|
|
, allocated(std::move(other.allocated))
|
|
|
|
|
, allocator(std::move(other.allocator))
|
|
|
|
|
{
|
|
|
|
|
_data = other._data;
|
|
|
|
|
other._data = nullptr;
|
|
|
|
|
other.allocated = 0;
|
|
|
|
|
other.arraySize = 0;
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
Array &operator=(const Array &other) noexcept
|
|
|
|
|
{
|
|
|
|
|
if (this != &other)
|
|
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
if (!std::allocator_traits<allocator_type>::is_always_equal::value
|
|
|
|
|
&& allocator != other.allocator)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
_data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
allocator = other.allocator;
|
|
|
|
|
}
|
2021-11-14 20:36:53 +01:00
|
|
|
if(_data == nullptr || other.arraySize > allocated)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
if(_data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
}
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
allocated = other.allocated;
|
|
|
|
|
}
|
|
|
|
|
arraySize = other.arraySize;
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
std::uninitialized_copy(other.begin(), other.end(), begin());
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
Array &operator=(Array &&other) noexcept
|
|
|
|
|
{
|
|
|
|
|
if (this != &other)
|
|
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
if (!std::allocator_traits<allocator_type>::is_always_equal::value
|
|
|
|
|
&& allocator != other.allocator)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
_data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
allocator = std::move(other.allocator);
|
|
|
|
|
}
|
|
|
|
|
if (_data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
_data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
allocated = std::move(other.allocated);
|
|
|
|
|
arraySize = std::move(other.arraySize);
|
|
|
|
|
_data = other._data;
|
|
|
|
|
other._data = nullptr;
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
~Array()
|
|
|
|
|
{
|
2021-12-02 13:00:03 +01:00
|
|
|
clear();
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
constexpr bool operator==(const Array &other)
|
|
|
|
|
{
|
|
|
|
|
return _data == other._data;
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
constexpr bool operator!=(const Array &other)
|
|
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr iterator find(const value_type &item)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
for (uint32 i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_data[i] == item)
|
|
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
return iterator(&_data[i]);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr iterator find(value_type&& item)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
for (uint32 i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_data[i] == item)
|
|
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
return iterator(&_data[i]);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr allocator_type get_allocator() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return allocator;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr iterator begin() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr iterator end() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr const_iterator cbegin() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr const_iterator cend() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr reference add(const value_type &item = value_type())
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return addInternal(item);
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr reference add(value_type&& item)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return addInternal(std::forward<T>(item));
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr reference addUnique(const value_type &item = value_type())
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
iterator it;
|
2021-10-23 00:22:35 +02:00
|
|
|
if((it = std::move(find(item))) != endIt)
|
|
|
|
|
{
|
|
|
|
|
return *it;
|
|
|
|
|
}
|
|
|
|
|
return addInternal(item);
|
|
|
|
|
}
|
|
|
|
|
template<typename... args>
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr reference emplace(args... arguments)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
if (arraySize == allocated)
|
|
|
|
|
{
|
|
|
|
|
size_type newSize = arraySize + 1;
|
|
|
|
|
allocated = calculateGrowth(newSize);
|
|
|
|
|
T *tempArray = allocateArray(allocated);
|
|
|
|
|
assert(tempArray != nullptr);
|
2021-11-01 20:25:16 +01:00
|
|
|
|
|
|
|
|
std::uninitialized_move(begin(), end(), Iterator(tempArray));
|
2021-10-23 00:22:35 +02:00
|
|
|
deallocateArray(_data, arraySize);
|
|
|
|
|
_data = tempArray;
|
|
|
|
|
}
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[arraySize++], arguments...);
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr void remove(iterator it, bool keepOrder = true)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
remove(it - beginIt, keepOrder);
|
|
|
|
|
}
|
|
|
|
|
constexpr void remove(int index, bool keepOrder = true)
|
|
|
|
|
{
|
|
|
|
|
if (keepOrder)
|
|
|
|
|
{
|
|
|
|
|
for(uint32 i = index; i < arraySize-1; ++i)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = std::move(_data[i+1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_data[index] = std::move(_data[arraySize - 1]);
|
|
|
|
|
}
|
|
|
|
|
arraySize--;
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
constexpr void resize(size_type newSize)
|
|
|
|
|
{
|
|
|
|
|
resizeInternal(newSize, std::move(T()));
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr void resize(size_type newSize, const value_type& value)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
resizeInternal(newSize, value);
|
|
|
|
|
}
|
|
|
|
|
constexpr void clear()
|
|
|
|
|
{
|
2021-12-02 13:00:03 +01:00
|
|
|
if(_data == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-23 00:22:35 +02:00
|
|
|
for(size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
_data[i].~T();
|
|
|
|
|
}
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
_data = nullptr;
|
|
|
|
|
arraySize = 0;
|
|
|
|
|
allocated = 0;
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
inline size_type indexOf(iterator iterator)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return iterator - beginIt;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
inline size_type indexOf(const_iterator iterator) const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return iterator.p - beginIt.p;
|
|
|
|
|
}
|
|
|
|
|
inline size_type indexOf(T& t)
|
|
|
|
|
{
|
|
|
|
|
return indexOf(find(t));
|
|
|
|
|
}
|
|
|
|
|
inline size_type indexOf(const T& t) const
|
|
|
|
|
{
|
|
|
|
|
return indexOf(find(t));
|
|
|
|
|
}
|
|
|
|
|
inline size_type size() const
|
|
|
|
|
{
|
|
|
|
|
return arraySize;
|
|
|
|
|
}
|
|
|
|
|
inline size_type empty() const
|
|
|
|
|
{
|
|
|
|
|
return arraySize == 0;
|
|
|
|
|
}
|
|
|
|
|
inline size_type capacity() const
|
|
|
|
|
{
|
|
|
|
|
return allocated;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
inline pointer data() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
inline reference front() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2021-11-01 20:25:16 +01:00
|
|
|
assert(arraySize > 0);
|
|
|
|
|
return _data[0];
|
|
|
|
|
}
|
|
|
|
|
inline reference back() const
|
|
|
|
|
{
|
|
|
|
|
assert(arraySize > 0);
|
2021-10-23 00:22:35 +02:00
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
|
|
|
|
void pop()
|
|
|
|
|
{
|
2021-11-04 23:47:51 +01:00
|
|
|
_data[--arraySize].~T();
|
2021-10-23 00:22:35 +02:00
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr inline reference operator[](size_type index)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
assert(index < arraySize);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
2021-11-01 20:25:16 +01:00
|
|
|
constexpr inline const reference operator[](size_type index) const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
assert(index < arraySize);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
size_type calculateGrowth(size_type newSize) const
|
|
|
|
|
{
|
|
|
|
|
const size_type oldCapacity = capacity();
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
if (oldCapacity > SIZE_MAX - oldCapacity / 2)
|
|
|
|
|
{
|
|
|
|
|
return newSize; // geometric growth would overflow
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
const size_type geometric = oldCapacity + oldCapacity / 2;
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
if (geometric < newSize)
|
|
|
|
|
{
|
|
|
|
|
return newSize; // geometric growth would be insufficient
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
return geometric; // geometric growth is sufficient
|
|
|
|
|
}
|
|
|
|
|
void markIteratorDirty()
|
|
|
|
|
{
|
|
|
|
|
beginIt = Iterator(_data);
|
|
|
|
|
endIt = Iterator(_data + arraySize);
|
|
|
|
|
}
|
|
|
|
|
T* allocateArray(size_type size)
|
|
|
|
|
{
|
|
|
|
|
T* result = allocator.allocate(size);
|
|
|
|
|
assert(result != nullptr);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
void deallocateArray(T* ptr, size_type size)
|
|
|
|
|
{
|
|
|
|
|
allocator.deallocate(ptr, size);
|
|
|
|
|
}
|
|
|
|
|
template<typename Type>
|
|
|
|
|
T& addInternal(Type&& t)
|
|
|
|
|
{
|
|
|
|
|
if (arraySize == allocated)
|
|
|
|
|
{
|
|
|
|
|
size_type newSize = arraySize + 1;
|
|
|
|
|
allocated = calculateGrowth(newSize);
|
|
|
|
|
T *tempArray = allocateArray(allocated);
|
|
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &tempArray[i], std::forward<Type>(_data[i]));
|
|
|
|
|
}
|
|
|
|
|
deallocateArray(_data, arraySize);
|
|
|
|
|
_data = tempArray;
|
|
|
|
|
}
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[arraySize++], std::forward<Type>(t));
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
|
|
|
|
template<typename Type>
|
|
|
|
|
void resizeInternal(size_type newSize, Type&& value)
|
|
|
|
|
{
|
|
|
|
|
if (newSize <= allocated)
|
|
|
|
|
{
|
|
|
|
|
// The array is already big enough
|
|
|
|
|
if(newSize < arraySize)
|
|
|
|
|
{
|
|
|
|
|
// But since we are sizing down we destruct some of them
|
|
|
|
|
for(size_type i = newSize; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Or construct the new elements by default
|
|
|
|
|
for(size_type i = arraySize; i < newSize; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[i], std::move(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
arraySize = newSize;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The array is not big enough, so we make a new one
|
|
|
|
|
T *newData = allocateArray(newSize);
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
// And move the current elements into that one
|
|
|
|
|
for(size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
newData[i] = std::forward<Type>(_data[i]);
|
|
|
|
|
}
|
|
|
|
|
// As well as default initialize the others
|
|
|
|
|
for(size_type i = arraySize; i < newSize; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[i], std::move(value));
|
|
|
|
|
}
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
arraySize = newSize;
|
|
|
|
|
allocated = newSize;
|
|
|
|
|
_data = newData;
|
|
|
|
|
}
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
|
template<class Archive>
|
|
|
|
|
void serialize(Archive& ar, const unsigned int)
|
|
|
|
|
{
|
|
|
|
|
ar & arraySize;
|
|
|
|
|
resize(arraySize);
|
|
|
|
|
for(size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
ar & _data[i];
|
|
|
|
|
markIteratorDirty();
|
|
|
|
|
}
|
|
|
|
|
size_type arraySize = 0;
|
|
|
|
|
size_type allocated = 0;
|
|
|
|
|
Iterator beginIt;
|
|
|
|
|
Iterator endIt;
|
|
|
|
|
T *_data = nullptr;
|
|
|
|
|
allocator_type allocator;
|
|
|
|
|
};
|
2021-03-31 12:18:16 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
template <typename T, size_t N>
|
|
|
|
|
struct StaticArray
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
template <typename X>
|
|
|
|
|
class IteratorBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
using value_type = X;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
|
using reference = X&;
|
|
|
|
|
using pointer = X*;
|
2021-03-31 12:18:16 +02:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
IteratorBase(X *x = nullptr)
|
|
|
|
|
: p(x)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
reference operator*() const
|
|
|
|
|
{
|
|
|
|
|
return *p;
|
|
|
|
|
}
|
|
|
|
|
pointer operator->() const
|
|
|
|
|
{
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator!=(const IteratorBase &other)
|
|
|
|
|
{
|
|
|
|
|
return p != other.p;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator==(const IteratorBase &other)
|
|
|
|
|
{
|
|
|
|
|
return p == other.p;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator++()
|
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator++(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
++*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator--()
|
|
|
|
|
{
|
|
|
|
|
p--;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator--(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
--*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
X *p;
|
|
|
|
|
};
|
|
|
|
|
using value_type = T;
|
|
|
|
|
using size_type = size_t;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
|
using pointer = T*;
|
|
|
|
|
using const_pointer = const T*;
|
|
|
|
|
using reference = T&;
|
|
|
|
|
using const_reference = const T&;
|
|
|
|
|
|
|
|
|
|
using iterator = IteratorBase<T>;
|
|
|
|
|
using const_iterator = IteratorBase<const T>;
|
|
|
|
|
|
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
|
|
|
|
|
|
StaticArray()
|
|
|
|
|
{
|
|
|
|
|
beginIt = iterator(_data);
|
|
|
|
|
endIt = iterator(_data + N);
|
|
|
|
|
}
|
|
|
|
|
StaticArray(T value)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = value;
|
|
|
|
|
}
|
|
|
|
|
beginIt = iterator(_data);
|
|
|
|
|
endIt = iterator(_data + N);
|
|
|
|
|
}
|
|
|
|
|
~StaticArray()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_type size() const
|
|
|
|
|
{
|
|
|
|
|
return N;
|
|
|
|
|
}
|
|
|
|
|
inline pointer data()
|
|
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
|
|
|
|
inline const_pointer data() const
|
|
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
|
|
|
|
constexpr reference operator[](size_type index) noexcept
|
|
|
|
|
{
|
|
|
|
|
assert(index < N);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
|
|
|
|
constexpr const_reference operator[](size_type index) const noexcept
|
|
|
|
|
{
|
|
|
|
|
assert(index < N);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
|
|
|
|
iterator begin()
|
|
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
|
|
|
|
iterator end()
|
|
|
|
|
{
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
|
|
|
|
const_iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
|
|
|
|
const_iterator end() const
|
|
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
T _data[N];
|
|
|
|
|
iterator beginIt;
|
|
|
|
|
iterator endIt;
|
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
|
template<class Archive>
|
|
|
|
|
void serialize(Archive& ar, const unsigned int version)
|
|
|
|
|
{
|
|
|
|
|
ar & version;
|
|
|
|
|
ar & N;
|
|
|
|
|
ar & _data;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-04-12 15:47:19 +02:00
|
|
|
} // namespace Seele
|