24#ifndef STELLARLIB_ECS_STACK_VECTOR_HPP
25#define STELLARLIB_ECS_STACK_VECTOR_HPP
27#include <stellarlib/ext/memory.hpp>
28#include <stellarlib/ext/type_traits.hpp>
34namespace stellarlib::ecs::internal
36template <
typename T,
typename SizeType = std::
size_t>
37class stack_vector final : ext::vector_allocator<T, SizeType>
41 constexpr stack_vector() noexcept = default;
44 constexpr stack_vector(const stack_vector &) noexcept = delete;
47 constexpr stack_vector(stack_vector &&other) noexcept
48 : _begin{std::exchange(other._begin, {})}
49 , _end{std::exchange(other._end, {})}
50 , _size{std::exchange(other._size, {})}
51 , _capacity{std::exchange(other._capacity, {})}
54 constexpr auto operator=(
const stack_vector &)
noexcept
55 -> stack_vector & =
delete;
57 constexpr auto operator=(stack_vector &&other)
noexcept
60 if (std::addressof(other) !=
this) {
61 std::destroy_at(
this);
62 std::construct_at(
this, std::move(other));
68 constexpr ~stack_vector() noexcept
70 std::ranges::destroy(*
this);
74 template <
typename ...Args>
75 constexpr void push(Args &&...args)
noexcept
77 if (_size == _capacity) {
79 _end = _begin + _size;
83 std::construct_at(_end++, std::forward<Args>(args)...);
86 template <
typename ...Args>
87 constexpr auto extend(
const SizeType size, Args &&...args)
noexcept
93 if (_capacity < size) {
99 std::uninitialized_fill(_begin + _size, _end, T{std::forward<Args>(args)...});
105 constexpr auto size() const noexcept
111 constexpr auto operator[](
const SizeType index)
const noexcept
114 return _begin[index];
118 constexpr auto operator[](
const SizeType index)
noexcept
121 return _begin[index];
125 constexpr auto begin() const noexcept
127 return static_cast<const T *
>(_begin);
131 constexpr auto begin() noexcept
137 constexpr auto end() const noexcept
139 return static_cast<const T *
>(_end);
143 constexpr auto end() noexcept
148 constexpr void pop() noexcept
151 std::destroy_at(--_end);
154 constexpr void clear() noexcept
156 std::ranges::destroy(*
this);
165 SizeType _capacity{};
166 [[no_unique_address]] ext::padding<T *, SizeType, SizeType> _padding;
constexpr void reallocate(value_type *&begin, const size_type capacity) const noexcept
Reallocates uninitialized memory for N instances of type T via bit-wise relocation.
Definition memory.hpp:128
constexpr void deallocate(value_type *begin) const noexcept
Deallocates uninitialized memory.
Definition memory.hpp:172