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>
|
2022-04-13 13:01:35 +02:00
|
|
|
#include <algorithm>
|
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
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>>
|
|
|
|
|
struct Array
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
template <typename X>
|
|
|
|
|
class IteratorBase
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2022-01-12 14:40:26 +01:00
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
using value_type = X;
|
2021-10-23 00:22:35 +02:00
|
|
|
using difference_type = std::ptrdiff_t;
|
2022-01-12 14:40:26 +01:00
|
|
|
using reference = X&;
|
|
|
|
|
using pointer = X*;
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr IteratorBase(X *x = nullptr)
|
2022-01-12 14:40:26 +01:00
|
|
|
: p(x)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr reference operator*() const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return *p;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr pointer operator->() const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr bool operator!=(const IteratorBase &other) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return p != other.p;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr bool operator==(const IteratorBase &other) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return p == other.p;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr IteratorBase operator+(size_t other) const
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
tmp.p += other;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
constexpr int operator-(const IteratorBase &other) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return (int)(p - other.p);
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr bool operator<(const IteratorBase& other) const
|
|
|
|
|
{
|
|
|
|
|
return p < other.p;
|
|
|
|
|
}
|
|
|
|
|
constexpr IteratorBase operator-(difference_type diff) const
|
|
|
|
|
{
|
|
|
|
|
return IteratorBase(p - diff);
|
|
|
|
|
}
|
|
|
|
|
constexpr IteratorBase &operator++()
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr IteratorBase &operator--()
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
p--;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr IteratorBase operator++(int)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
++*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr IteratorBase operator--(int)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
--*this;
|
|
|
|
|
return tmp;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01: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&;
|
|
|
|
|
|
|
|
|
|
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>;
|
|
|
|
|
|
|
|
|
|
constexpr Array() noexcept(noexcept(Allocator()))
|
|
|
|
|
: arraySize(0)
|
|
|
|
|
, allocated(DEFAULT_ALLOC_SIZE)
|
|
|
|
|
, allocator(Allocator())
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(DEFAULT_ALLOC_SIZE);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr explicit Array(const allocator_type& alloc) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
: arraySize(0)
|
|
|
|
|
, allocated(DEFAULT_ALLOC_SIZE)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(DEFAULT_ALLOC_SIZE);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
}
|
|
|
|
|
constexpr Array(size_type size, const value_type& value, const allocator_type& alloc = allocator_type())
|
|
|
|
|
: arraySize(size)
|
|
|
|
|
, allocated(size)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(size);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
for (size_type i = 0; i < size; ++i)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
_data[i] = value;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
constexpr explicit Array(size_type size, const allocator_type& alloc = allocator_type())
|
|
|
|
|
: arraySize(size)
|
|
|
|
|
, allocated(size)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(size);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
for (size_type i = 0; i < size; ++i)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
std::uninitialized_copy(init.begin(), init.end(), begin());
|
|
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
|
|
|
|
|
constexpr Array(const Array &other)
|
2022-01-12 14:40:26 +01:00
|
|
|
: arraySize(other.arraySize)
|
|
|
|
|
, allocated(other.allocated)
|
|
|
|
|
, allocator(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.allocator))
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
std::uninitialized_copy(other.begin(), other.end(), begin());
|
|
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
constexpr Array(const Array& other, const Allocator& alloc)
|
|
|
|
|
: arraySize(other.arraySize)
|
|
|
|
|
, allocated(other.allocated)
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
assert(_data != nullptr);
|
|
|
|
|
std::uninitialized_copy(other.begin(), other.end(), begin());
|
|
|
|
|
}
|
|
|
|
|
constexpr Array(Array &&other) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
: 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;
|
|
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
constexpr Array(Array &&other, const Allocator& alloc) noexcept
|
|
|
|
|
: arraySize(std::move(other.arraySize))
|
|
|
|
|
, allocated(std::move(other.allocated))
|
|
|
|
|
, allocator(alloc)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
std::uninitialized_move(other.begin(), other.end(), begin());
|
|
|
|
|
other.deallocateArray(other._data, other.allocated);
|
|
|
|
|
other._data = nullptr;
|
|
|
|
|
other.allocated = 0;
|
|
|
|
|
other.arraySize = 0;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
Array &operator=(const Array &other)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
if (this != &other)
|
|
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
if (other.arraySize > allocated)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if (!std::allocator_traits<allocator_type>::is_always_equal::value
|
|
|
|
|
&& allocator != other.allocator)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
allocator = other.allocator;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
if(_data == nullptr)
|
|
|
|
|
{
|
|
|
|
|
_data = allocateArray(other.allocated);
|
|
|
|
|
allocated = other.allocated;
|
|
|
|
|
}
|
|
|
|
|
arraySize = other.arraySize;
|
2021-10-23 00:22:35 +02:00
|
|
|
std::uninitialized_copy(other.begin(), other.end(), begin());
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
Array &operator=(Array &&other) noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value
|
|
|
|
|
|| std::allocator_traits<Allocator>::is_always_equal::value)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
if (this != &other)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if (_data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
2023-01-21 18:43:21 +01:00
|
|
|
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
|
|
|
|
|
{
|
|
|
|
|
allocator = std::move(other.allocator);
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
allocated = std::move(other.allocated);
|
|
|
|
|
arraySize = std::move(other.arraySize);
|
2021-10-23 00:22:35 +02:00
|
|
|
_data = other._data;
|
|
|
|
|
other._data = nullptr;
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr ~Array()
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
|
2022-04-13 13:01:35 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr iterator find(const value_type &item) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-04-13 13:01:35 +02:00
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if (_data[i] == item)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
return iterator(&_data[i]);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
return end();
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr iterator find(value_type&& item) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-04-13 13:01:35 +02:00
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
if (_data[i] == item)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
return iterator(&_data[i]);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
return end();
|
|
|
|
|
}
|
|
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr const_iterator find(const value_type &item) const noexcept
|
|
|
|
|
{
|
|
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_data[i] == item)
|
|
|
|
|
{
|
|
|
|
|
return const_iterator(&_data[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return end();
|
|
|
|
|
}
|
|
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr const_iterator find(value_type&& item) const noexcept
|
|
|
|
|
{
|
|
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_data[i] == item)
|
|
|
|
|
{
|
|
|
|
|
return const_iterator(&_data[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return end();
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
template<class Pred>
|
2022-04-15 23:45:44 +02:00
|
|
|
requires std::predicate<Pred, value_type>
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr const_iterator find(Pred pred) const noexcept
|
2022-04-13 13:01:35 +02:00
|
|
|
{
|
|
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(pred(_data[i]))
|
|
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return const_iterator(&_data[i]);
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
return end();
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|
|
|
|
|
constexpr allocator_type get_allocator() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return allocator;
|
|
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr iterator begin() noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return iterator(_data);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-11-29 16:34:42 +01:00
|
|
|
constexpr iterator end() noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return iterator(_data + arraySize);
|
|
|
|
|
}
|
|
|
|
|
constexpr const_iterator begin() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return const_iterator(_data);
|
|
|
|
|
}
|
|
|
|
|
constexpr const_iterator end() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return const_iterator(_data + arraySize);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr const_iterator cbegin() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return const_iterator(_data);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr const_iterator cend() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return const_iterator(_data + arraySize);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
constexpr reference add(const value_type &item = value_type())
|
|
|
|
|
{
|
|
|
|
|
return addInternal(item);
|
|
|
|
|
}
|
|
|
|
|
constexpr reference add(value_type&& item)
|
|
|
|
|
{
|
|
|
|
|
return addInternal(std::forward<T>(item));
|
|
|
|
|
}
|
|
|
|
|
constexpr void addAll(Array other)
|
|
|
|
|
{
|
|
|
|
|
for(auto value : other)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
addInternal(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
constexpr reference addUnique(const value_type &item = value_type())
|
|
|
|
|
{
|
|
|
|
|
iterator it;
|
2022-11-29 16:34:42 +01:00
|
|
|
if((it = std::move(find(item))) != end())
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return *it;
|
|
|
|
|
}
|
|
|
|
|
return addInternal(item);
|
|
|
|
|
}
|
|
|
|
|
template<typename... args>
|
|
|
|
|
constexpr reference emplace(args... arguments)
|
|
|
|
|
{
|
|
|
|
|
if (arraySize == allocated)
|
|
|
|
|
{
|
|
|
|
|
size_type newSize = arraySize + 1;
|
|
|
|
|
allocated = calculateGrowth(newSize);
|
|
|
|
|
T *tempArray = allocateArray(allocated);
|
|
|
|
|
assert(tempArray != nullptr);
|
|
|
|
|
|
|
|
|
|
std::uninitialized_move(begin(), end(), Iterator(tempArray));
|
|
|
|
|
deallocateArray(_data, arraySize);
|
|
|
|
|
_data = tempArray;
|
|
|
|
|
}
|
|
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[arraySize++], arguments...);
|
|
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
template<std::predicate Pred>
|
|
|
|
|
constexpr void remove_if(Pred pred, bool keepOrder = true)
|
|
|
|
|
{
|
|
|
|
|
remove(find(pred), keepOrder);
|
|
|
|
|
}
|
|
|
|
|
constexpr void remove(const value_type& element, bool keepOrder = true)
|
|
|
|
|
{
|
|
|
|
|
remove(find(element), keepOrder);
|
|
|
|
|
}
|
|
|
|
|
constexpr void remove(value_type&& element, bool keepOrder = true)
|
|
|
|
|
{
|
|
|
|
|
remove(find(element), keepOrder);
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
constexpr void remove(iterator it, bool keepOrder = true)
|
|
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
removeAt(it - begin(), keepOrder);
|
|
|
|
|
}
|
|
|
|
|
constexpr void remove(const_iterator it, bool keepOrder = true)
|
|
|
|
|
{
|
|
|
|
|
removeAt(it - cbegin(), keepOrder);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-15 23:45:44 +02:00
|
|
|
constexpr void removeAt(size_type index, bool keepOrder = true)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
if (keepOrder)
|
|
|
|
|
{
|
2022-04-13 13:01:35 +02:00
|
|
|
for(size_type i = index; i < arraySize-1; ++i)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
_data[i] = std::move(_data[i+1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_data[index] = std::move(_data[arraySize - 1]);
|
|
|
|
|
}
|
2022-02-24 22:38:26 +01:00
|
|
|
std::allocator_traits<allocator_type>::destroy(allocator, &_data[--arraySize]);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
constexpr void resize(size_type newSize)
|
|
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
resizeInternal(newSize, T());
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
constexpr void resize(size_type newSize, const value_type& value)
|
|
|
|
|
{
|
|
|
|
|
resizeInternal(newSize, value);
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr void clear() noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
if(_data == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for(size_type i = 0; i < arraySize; ++i)
|
|
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
|
|
|
|
|
}
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
_data = nullptr;
|
|
|
|
|
arraySize = 0;
|
|
|
|
|
allocated = 0;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr size_type indexOf(iterator iterator)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return iterator - begin();
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr size_type indexOf(const_iterator iterator) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-11-29 16:34:42 +01:00
|
|
|
return iterator.p - begin().p;
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr size_type indexOf(T& t)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return indexOf(find(t));
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr size_type indexOf(const T& t) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return indexOf(find(t));
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr size_type size() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return arraySize;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
|
constexpr bool empty() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return arraySize == 0;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr void reserve(size_type new_cap)
|
|
|
|
|
{
|
|
|
|
|
if(new_cap > allocated)
|
|
|
|
|
{
|
|
|
|
|
T* temp = allocateArray(new_cap);
|
2022-11-29 16:34:42 +01:00
|
|
|
std::uninitialized_move_n(begin(), arraySize, temp);
|
2022-04-13 13:01:35 +02:00
|
|
|
_data = temp;
|
|
|
|
|
}
|
|
|
|
|
allocated = new_cap;
|
|
|
|
|
}
|
|
|
|
|
constexpr size_type capacity() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return allocated;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr pointer data() const noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr reference front() const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
assert(arraySize > 0);
|
|
|
|
|
return _data[0];
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr reference back() const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
assert(arraySize > 0);
|
|
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr void pop()
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
std::allocator_traits<allocator_type>::destroy(allocator, &_data[--arraySize]);
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr reference operator[](size_type index)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
assert(index < arraySize);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
constexpr const reference operator[](size_type index) const
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
assert(index < arraySize);
|
|
|
|
|
return _data[index];
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
size_type calculateGrowth(size_type newSize) const
|
|
|
|
|
{
|
|
|
|
|
const size_type oldCapacity = capacity();
|
|
|
|
|
|
|
|
|
|
if (oldCapacity > SIZE_MAX - oldCapacity)
|
|
|
|
|
{
|
|
|
|
|
return newSize; // geometric growth would overflow
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
const size_type geometric = oldCapacity + oldCapacity;
|
|
|
|
|
|
|
|
|
|
if (geometric < newSize)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
return newSize; // geometric growth would be insufficient
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
return geometric; // geometric growth is sufficient
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
[[nodiscard]]
|
2022-01-12 14:40:26 +01:00
|
|
|
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>
|
2022-04-13 13:01:35 +02:00
|
|
|
T& addInternal(Type&& t) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
if (arraySize == allocated)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
size_type newSize = arraySize + 1;
|
|
|
|
|
allocated = calculateGrowth(newSize);
|
|
|
|
|
T *tempArray = allocateArray(allocated);
|
|
|
|
|
for (size_type i = 0; i < arraySize; ++i)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
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));
|
|
|
|
|
return _data[arraySize - 1];
|
|
|
|
|
}
|
|
|
|
|
template<typename Type>
|
2023-01-21 18:43:21 +01:00
|
|
|
void resizeInternal(size_type newSize, const Type& value) noexcept
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
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)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
// Or construct the new elements by default
|
2021-10-23 00:22:35 +02:00
|
|
|
for(size_type i = arraySize; i < newSize; ++i)
|
|
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &_data[i], value);
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
arraySize = newSize;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
else
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
// The array is not big enough, so we make a new one
|
|
|
|
|
T *newData = allocateArray(newSize);
|
|
|
|
|
|
|
|
|
|
// And move the current elements into that one
|
2021-10-23 00:22:35 +02:00
|
|
|
for(size_type i = 0; i < arraySize; ++i)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
|
|
|
|
newData[i] = std::forward<Type>(_data[i]);
|
|
|
|
|
}
|
|
|
|
|
// As well as default initialize the others
|
|
|
|
|
for(size_type i = arraySize; i < newSize; ++i)
|
|
|
|
|
{
|
2023-01-21 18:43:21 +01:00
|
|
|
std::allocator_traits<allocator_type>::construct(allocator, &newData[i], value);
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
deallocateArray(_data, allocated);
|
|
|
|
|
arraySize = newSize;
|
|
|
|
|
allocated = newSize;
|
|
|
|
|
_data = newData;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
size_type arraySize = 0;
|
|
|
|
|
size_type allocated = 0;
|
|
|
|
|
T *_data = nullptr;
|
|
|
|
|
allocator_type allocator;
|
|
|
|
|
};
|
2021-03-31 12:18:16 +02:00
|
|
|
|
2022-04-13 13:01:35 +02:00
|
|
|
|
|
|
|
|
template<class Type, class Alloc>
|
|
|
|
|
constexpr bool operator==(const Array<Type, Alloc> &lhs, const Array<Type, Alloc>& rhs)
|
|
|
|
|
{
|
|
|
|
|
if(lhs.size() != rhs.size())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for(auto it1 = lhs.begin(), it2 = rhs.begin(); it1 != lhs.end() && it2 != rhs.end(); ++it1, ++it2)
|
|
|
|
|
{
|
|
|
|
|
if(*it1 != *it2)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Type, class Alloc>
|
|
|
|
|
constexpr auto operator<=>(const Array<Type, Alloc>& lhs, const Array<Type, Alloc>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 14:40:26 +01: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;
|
2021-10-23 00:22:35 +02:00
|
|
|
using difference_type = std::ptrdiff_t;
|
2022-01-12 14:40:26 +01:00
|
|
|
using reference = X&;
|
|
|
|
|
using pointer = X*;
|
2021-10-23 00:22:35 +02:00
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
IteratorBase(X *x = nullptr)
|
|
|
|
|
: p(x)
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
reference operator*() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
return *p;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
pointer operator->() const
|
2021-10-23 00:22:35 +02:00
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
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;
|
2021-10-23 00:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
|
2021-10-23 00:22:35 +02:00
|
|
|
private:
|
2022-01-12 14:40:26 +01:00
|
|
|
X *p;
|
2021-10-23 00:22:35 +02:00
|
|
|
};
|
2022-01-12 14:40:26 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2022-11-15 12:19:11 +01:00
|
|
|
StaticArray(std::initializer_list<T> init)
|
|
|
|
|
{
|
|
|
|
|
assert(init.size() == N);
|
|
|
|
|
auto beg = init.begin();
|
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = *beg;
|
|
|
|
|
beg++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
~StaticArray()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_type size() const
|
|
|
|
|
{
|
|
|
|
|
return N;
|
|
|
|
|
}
|
|
|
|
|
inline pointer data()
|
|
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
|
|
|
|
inline const_pointer data() const
|
|
|
|
|
{
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
2022-11-17 16:47:42 +01:00
|
|
|
template<typename I>
|
|
|
|
|
constexpr reference operator[](I index) noexcept
|
|
|
|
|
{
|
|
|
|
|
return operator[](static_cast<size_t>(index));
|
|
|
|
|
}
|
|
|
|
|
template<typename I>
|
|
|
|
|
constexpr const_reference operator[](I index) const noexcept
|
|
|
|
|
{
|
|
|
|
|
return operator[](static_cast<size_t>(index));
|
|
|
|
|
}
|
2022-01-12 14:40:26 +01:00
|
|
|
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;
|
|
|
|
|
};
|
2020-04-12 15:47:19 +02:00
|
|
|
} // namespace Seele
|