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);
87 template<
typename T,
size_t N>
91 static_assert(std::is_nothrow_destructible_v<T>);
94 using size_type = decltype(MakeSmallestCapableUnsigned<N>());
108 : m_Size{other.size()}
110 std::uninitialized_copy(other.begin(), other.end(), begin());
113 template<
size_t OtherN>
115 std::is_nothrow_copy_constructible_v<T>)
116 : m_Size{other.size()}
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());
132 m_Size = other.size();
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());
148 m_Size = other.size();
153 : m_Size{other.size()}
155 std::uninitialized_move(other.begin(), other.end(), begin());
158 template<
size_t OtherN>
160 noexcept(std::is_nothrow_move_constructible_v<T>)
161 : m_Size{other.size()}
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());
177 m_Size = other.size();
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());
193 m_Size = other.size();
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());
225 : m_Size{
static_cast<size_type>(init.size())}
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());
244 m_Size = init.size();
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()));
310 return std::launder(reinterpret_cast<const_pointer>(m_Data.data()));
331 return begin() + size();
341 return cbegin() + size();
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());
397 std::destroy(begin(), end());
415 if (location == end())
416 return std::addressof(emplace_back(value));
418 new(end())
T{std::move(back())};
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())
442 return std::addressof(emplace_back(std::move(value)));
444 const iterator mutableLocation{MakeMutableIterator(location)};
445 new(end())
T{std::move(back())};
448 std::move_backward(mutableLocation, end() - 2, end() -1);
450 *mutableLocation = std::move(value);
451 return mutableLocation;
469 emplace_back(std::move(value));
476 template<
typename... Args>
481 "Called emplace_back but the StaticVector is already full"};
483 const iterator location{begin() + size()};
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);
558 return begin() + (iter - begin());
568 #endif // INCLUDED_PS_STATICVECTOR decltype(MakeSmallestCapableSigned< N >()) difference_type
Definition: StaticVector.h:95
const_pointer data() const noexcept
Definition: StaticVector.h:308
reference operator[](const size_type index) noexcept
Definition: StaticVector.h:267
const_reverse_iterator crbegin() const noexcept
Definition: StaticVector.h:354
const_iterator cbegin() const noexcept
Definition: StaticVector.h:324
StaticVector(const size_type count, const T &value)
Definition: StaticVector.h:202
iterator insert(const const_iterator location, T &&value)
Same as above but the new element is move-constructed.
Definition: StaticVector.h:436
Definition: StaticVector.h:34
pointer data() noexcept
Definition: StaticVector.h:303
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
reference emplace_back(Args &&... args)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:477
reverse_iterator rend() noexcept
Definition: StaticVector.h:359
const_iterator begin() const noexcept
Definition: StaticVector.h:319
const_reverse_iterator rend() const noexcept
Definition: StaticVector.h:364
StaticVector(const StaticVector< T, OtherN > &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Definition: StaticVector.h:114
~StaticVector()
Definition: StaticVector.h:197
constexpr auto MakeSmallestCapableSigned()
Definition: StaticVector.h:58
bool empty() const noexcept
Definition: StaticVector.h:374
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:318
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_reverse_iterator crend() const noexcept
Definition: StaticVector.h:369
const_reference front() const noexcept
Definition: StaticVector.h:285
bool full() const noexcept
Definition: StaticVector.h:379
const_iterator cend() const noexcept
Definition: StaticVector.h:339
iterator MakeMutableIterator(const const_iterator iter) noexcept
Definition: StaticVector.h:556
const_iterator end() const noexcept
Definition: StaticVector.h:334
void push_back(T &&value)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:467
std::reverse_iterator< iterator > reverse_iterator
Definition: StaticVector.h:102
void pop_back() noexcept
Definition: StaticVector.h:489
constexpr size_type capacity() const noexcept
Definition: StaticVector.h:389
constexpr auto MakeSmallestCapableUnsigned()
Definition: StaticVector.h:40
StaticVector(const size_type count)
Definition: StaticVector.h:213
const_reverse_iterator rbegin() const noexcept
Definition: StaticVector.h:349
StaticVector & operator=(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
Definition: StaticVector.h:168
decltype(MakeSmallestCapableUnsigned< N >()) size_type
Definition: StaticVector.h:94
const_reference operator[](const size_type index) const noexcept
Definition: StaticVector.h:273
Represents a texture object.
Definition: TextureManager.h:256
void clear() noexcept
Definition: StaticVector.h:395
iterator begin() noexcept
Definition: StaticVector.h:314
#define T(string_literal)
Definition: secure_crt.cpp:77
size_type size() const noexcept
Definition: StaticVector.h:384
const_reference back() const noexcept
Definition: StaticVector.h:297
void push_back(const T &value)
If an exception is thrown this function has no effect (strong exception guarantee).
Definition: StaticVector.h:458
reference back() noexcept
Definition: StaticVector.h:291
StaticVector & operator=(const StaticVector &other) noexcept(std::is_nothrow_copy_constructible_v< T > &&std::is_nothrow_copy_assignable_v< T >)
Definition: StaticVector.h:123
void resize(const size_type newSize)
Constructs or destructs elements to adjust to newSize.
Definition: StaticVector.h:507
friend bool operator!=(const StaticVector< T, N > &lhs, const StaticVector< T, OtherN > &rhs)
Definition: StaticVector.h:550
A conntainer close to std::vector but the elements are stored in place: There is a fixed capacity and...
Definition: StaticVector.h:88
StaticVector & operator=(const std::initializer_list< T > init)
Definition: StaticVector.h:235
friend bool operator==(const StaticVector< T, N > &lhs, const StaticVector< T, OtherN > &rhs)
Definition: StaticVector.h:544
iterator end() noexcept
Definition: StaticVector.h:329
StaticVector(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition: StaticVector.h:152
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: StaticVector.h:103
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
std::array< CTexture *, N > EagerInitialized
Definition: StaticVector.h:561
reverse_iterator rbegin() noexcept
Definition: StaticVector.h:344
reference at(const size_type index)
Definition: StaticVector.h:249
std::array< std::byte, sizeof(T) *N > m_Data
Definition: StaticVector.h:562
StaticVector(const std::initializer_list< T > init)
Definition: StaticVector.h:224
StaticVector(const StaticVector &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Definition: StaticVector.h:107
StaticVector(StaticVector< T, OtherN > &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition: StaticVector.h:159
iterator insert(const const_iterator location, const T &value)
Inserts an element at location.
Definition: StaticVector.h:410
reference front() noexcept
Definition: StaticVector.h:279
const_reference at(const size_type index) const
Definition: StaticVector.h:258