18#ifndef INCLUDED_PS_STATICVECTOR
19#define INCLUDED_PS_STATICVECTOR
24#include <fmt/format.h>
25#include <initializer_list>
36 using std::length_error::length_error;
42 if constexpr (N <= std::numeric_limits<uint_fast8_t>::max())
43 return static_cast<uint_fast8_t
>(0);
44 else if constexpr (N <= std::numeric_limits<uint_fast16_t>::max())
45 return static_cast<uint_fast16_t
>(0);
46 else if constexpr (N <= std::numeric_limits<uint_fast32_t>::max())
47 return static_cast<uint_fast32_t
>(0);
48 else if constexpr (N <= std::numeric_limits<uint_fast64_t>::max())
49 return static_cast<uint_fast64_t
>(0);
52 static_assert(N <= std::numeric_limits<uintmax_t>::max());
53 return static_cast<uintmax_t
>(0);
61 if constexpr (N <= static_cast<uintmax_t>(std::numeric_limits<int_fast8_t>::max()) &&
62 -
static_cast<intmax_t
>(N) >= std::numeric_limits<int_fast8_t>::min())
63 return static_cast<int_fast8_t
>(0);
64 else if constexpr (N <= static_cast<uintmax_t>(std::numeric_limits<int_fast16_t>::max()) &&
65 -
static_cast<intmax_t
>(N) >= std::numeric_limits<int_fast16_t>::min())
66 return static_cast<int_fast16_t
>(0);
67 else if constexpr (N <= static_cast<uintmax_t>(std::numeric_limits<int_fast32_t>::max()) &&
68 -
static_cast<intmax_t
>(N) >= std::numeric_limits<int_fast32_t>::min())
69 return static_cast<int_fast32_t
>(0);
70 else if constexpr (N <= static_cast<uintmax_t>(std::numeric_limits<int_fast64_t>::max()) &&
71 -
static_cast<intmax_t
>(N) >= std::numeric_limits<int_fast64_t>::min())
72 return static_cast<int_fast64_t
>(0);
75 static_assert(N <= static_cast<uintmax_t>(std::numeric_limits<intmax_t>::max()) &&
76 -
static_cast<intmax_t
>(N) >= std::numeric_limits<intmax_t>::min());
77 return static_cast<intmax_t
>(0);
87template<
typename T,
size_t N>
91 static_assert(std::is_nothrow_destructible_v<T>);
94 using size_type =
decltype(MakeSmallestCapableUnsigned<N>());
110 std::uninitialized_copy(other.begin(), other.end(),
begin());
113 template<
size_t OtherN>
115 std::is_nothrow_copy_constructible_v<T>)
118 static_assert(OtherN < N);
120 std::uninitialized_copy(other.begin(), other.end(),
begin());
124 && std::is_nothrow_copy_assignable_v<T>)
126 const size_type initializedCopies{std::min(other.size(),
size())};
127 std::copy_n(other.begin(), initializedCopies,
begin());
128 std::uninitialized_copy(other.begin() + initializedCopies, other.end(),
129 begin() + initializedCopies);
130 std::destroy(
begin() + initializedCopies,
end());
136 template<
size_t OtherN>
138 std::is_nothrow_copy_constructible_v<T> && std::is_nothrow_copy_assignable_v<T>)
140 static_assert(OtherN < N);
142 const size_type initializedCopies{std::min(other.size(),
size())};
143 std::copy_n(other.begin(), initializedCopies,
begin());
144 std::uninitialized_copy(other.begin() + initializedCopies, other.end(),
145 begin() + initializedCopies);
146 std::destroy(
begin() + initializedCopies,
end());
155 std::uninitialized_move(other.begin(), other.end(),
begin());
158 template<
size_t OtherN>
160 noexcept(std::is_nothrow_move_constructible_v<T>)
163 static_assert(OtherN < N);
165 std::uninitialized_move(other.begin(), other.end(),
begin());
169 std::is_nothrow_move_assignable_v<T>)
171 const size_type initializedMoves{std::min(other.size(),
size())};
172 std::move(other.begin(), other.begin() + initializedMoves,
begin());
173 std::uninitialized_move(other.begin() + initializedMoves, other.end(),
174 begin() + initializedMoves);
175 std::destroy(
begin() + initializedMoves,
end());
181 template<
size_t OtherN>
183 std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>)
185 static_assert(OtherN < N);
187 const size_type initializedMoves{std::min(other.size(),
size())};
188 std::move(other.begin(), other.begin() + initializedMoves,
begin());
189 std::uninitialized_move(other.begin() + initializedMoves, other.end(),
190 begin() + initializedMoves);
191 std::destroy(
begin() + initializedMoves,
end());
207 "Tried to construct a StaticVector with a size of {} but the capacity is only {}",
210 std::uninitialized_fill(
begin(),
end(), value);
218 "Tried to construct a StaticVector with a size of {} but the capacity is only {}",
221 std::uninitialized_default_construct(
begin(),
end());
229 "Tried to construct a StaticVector with a size of {} but the capacity is only {}",
232 std::uninitialized_copy(init.begin(), init.end(),
begin());
239 "Tried to construct a StaticVector with a size of {} but the capacity is only {}",
243 std::uninitialized_copy(init.begin(), init.end(),
begin());
252 throw std::out_of_range{fmt::format(
"Called at({}) but there are only {} elements.",
255 return (*
this)[index];
261 throw std::out_of_range{fmt::format(
"Called at({}) but there are only {} elements.",
264 return (*
this)[index];
270 return *(
begin() + index);
276 return *(
begin() + index);
294 return *std::prev(
end());
300 return *std::prev(
end());
305 return std::launder(
reinterpret_cast<pointer>(
m_Data.data()));
346 return std::make_reverse_iterator(
end());
356 return std::make_reverse_iterator(
end());
361 return std::make_reverse_iterator(
begin());
371 return std::make_reverse_iterator(
cbegin());
415 if (location ==
end())
421 const iterator mutableLocation{MutableIter(location)};
422 std::move_backward(mutableLocation, std::prev(
end(), 2), std::prev(
end(), 1));
424 *mutableLocation = value;
425 return mutableLocation;
441 if (location ==
end())
448 std::move_backward(mutableLocation,
end() - 2,
end() -1);
450 *mutableLocation = std::move(value);
451 return mutableLocation;
476 template<
typename... Args>
481 "Called emplace_back but the StaticVector is already full"};
484 new(location)
T{std::forward<Args>(args)...};
492 std::destroy_at(std::addressof(
back()));
511 "Can not resize StaticVector to {} the capacity is {}", newSize, N)};
513 if (newSize >
size())
514 std::uninitialized_default_construct(
end(),
begin() + newSize);
516 std::destroy(
begin() + newSize,
end());
533 "Can't resize the StaticVector to {} the capacity is {}", newSize, N)};
535 if (newSize >
size())
536 std::uninitialized_fill(
end(),
begin() + newSize, value);
538 std::destroy(
begin() + newSize,
end());
543 template<
size_t OtherN>
549 template<
size_t OtherN>
552 return !(lhs == rhs);
A conntainer close to std::vector but the elements are stored in place: There is a fixed capacity and...
Definition: StaticVector.h:89
reference at(const size_type index)
Definition: StaticVector.h:249
void pop_back() noexcept
Definition: StaticVector.h:489
void clear() noexcept
Definition: StaticVector.h:395
decltype(MakeSmallestCapableUnsigned< N >()) size_type
Definition: StaticVector.h:94
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: StaticVector.h:103
StaticVector & operator=(const std::initializer_list< T > init)
Definition: StaticVector.h:235
std::array< std::byte, sizeof(T) *N > m_Data
Definition: StaticVector.h:562
const_iterator end() const noexcept
Definition: StaticVector.h:334
reference operator[](const size_type index) noexcept
Definition: StaticVector.h:267
friend bool operator!=(const StaticVector< T, N > &lhs, const StaticVector< T, OtherN > &rhs)
Definition: StaticVector.h:550
void push_back(T &&value)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:467
const_pointer const_iterator
Definition: StaticVector.h:101
iterator MakeMutableIterator(const const_iterator iter) noexcept
Definition: StaticVector.h:556
reverse_iterator rend() noexcept
Definition: StaticVector.h:359
StaticVector(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition: StaticVector.h:152
size_type size() const noexcept
Definition: StaticVector.h:384
void push_back(const T &value)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:458
iterator insert(const const_iterator location, const T &value)
Inserts an element at location.
Definition: StaticVector.h:410
pointer data() noexcept
Definition: StaticVector.h:303
const_pointer data() const noexcept
Definition: StaticVector.h:308
const_iterator cend() const noexcept
Definition: StaticVector.h:339
const_reference operator[](const size_type index) const noexcept
Definition: StaticVector.h:273
const_reverse_iterator rbegin() const noexcept
Definition: StaticVector.h:349
StaticVector(const StaticVector< T, OtherN > &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Definition: StaticVector.h:114
StaticVector & operator=(const StaticVector< T, OtherN > &other) noexcept(std::is_nothrow_copy_constructible_v< T > &&std::is_nothrow_copy_assignable_v< T >)
Definition: StaticVector.h:137
StaticVector & operator=(const StaticVector &other) noexcept(std::is_nothrow_copy_constructible_v< T > &&std::is_nothrow_copy_assignable_v< T >)
Definition: StaticVector.h:123
bool full() const noexcept
Definition: StaticVector.h:379
iterator insert(const const_iterator location, T &&value)
Same as above but the new element is move-constructed.
Definition: StaticVector.h:436
const_reverse_iterator crbegin() const noexcept
Definition: StaticVector.h:354
void resize(const size_type newSize)
Constructs or destructs elements to adjust to newSize.
Definition: StaticVector.h:507
value_type * pointer
Definition: StaticVector.h:98
size_type m_Size
Definition: StaticVector.h:563
StaticVector & operator=(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
Definition: StaticVector.h:168
StaticVector(StaticVector< T, OtherN > &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition: StaticVector.h:159
constexpr size_type capacity() const noexcept
Definition: StaticVector.h:389
reference front() noexcept
Definition: StaticVector.h:279
value_type & reference
Definition: StaticVector.h:96
~StaticVector()
Definition: StaticVector.h:197
StaticVector(const size_type count)
Definition: StaticVector.h:213
decltype(MakeSmallestCapableSigned< N >()) difference_type
Definition: StaticVector.h:95
std::array< T, N > EagerInitialized
Definition: StaticVector.h:561
StaticVector(const size_type count, const T &value)
Definition: StaticVector.h:202
iterator end() noexcept
Definition: StaticVector.h:329
const value_type * const_pointer
Definition: StaticVector.h:99
const_iterator begin() const noexcept
Definition: StaticVector.h:319
void resize(const size_type newSize, const T &value)
Same as above but uses value to copy-construct the new elements.
Definition: StaticVector.h:529
StaticVector & operator=(StaticVector< T, OtherN > &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
Definition: StaticVector.h:182
const value_type & const_reference
Definition: StaticVector.h:97
bool empty() const noexcept
Definition: StaticVector.h:374
const_reverse_iterator crend() const noexcept
Definition: StaticVector.h:369
const_reference front() const noexcept
Definition: StaticVector.h:285
iterator begin() noexcept
Definition: StaticVector.h:314
reference emplace_back(Args &&... args)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:477
const_reference back() const noexcept
Definition: StaticVector.h:297
T value_type
Definition: StaticVector.h:93
StaticVector(const std::initializer_list< T > init)
Definition: StaticVector.h:224
const_reference at(const size_type index) const
Definition: StaticVector.h:258
friend bool operator==(const StaticVector< T, N > &lhs, const StaticVector< T, OtherN > &rhs)
Definition: StaticVector.h:544
std::reverse_iterator< iterator > reverse_iterator
Definition: StaticVector.h:102
const_reverse_iterator rend() const noexcept
Definition: StaticVector.h:364
const_iterator cbegin() const noexcept
Definition: StaticVector.h:324
pointer iterator
Definition: StaticVector.h:100
reference back() noexcept
Definition: StaticVector.h:291
reverse_iterator rbegin() noexcept
Definition: StaticVector.h:344
StaticVector(const StaticVector &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Definition: StaticVector.h:107
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:305
Definition: NetEnet.cpp:26
constexpr auto MakeSmallestCapableSigned()
Definition: StaticVector.h:58
constexpr auto MakeSmallestCapableUnsigned()
Definition: StaticVector.h:40
#define T(string_literal)
Definition: secure_crt.cpp:77
Definition: StaticVector.h:35