9#include <initializer_list>
10#include "required.hpp"
22template<
typename T,
size_t MaxSize>
26 using pointer_type = value_type *;
27 using const_pointer_type = value_type
const *;
28 using reference_type = value_type &;
29 using const_reference_type = value_type
const &;
30 using iterator_type = pointer_type;
31 using const_iterator_type = const_pointer_type;
32 using size_type = size_t;
33 using difference_type = ptrdiff_t;
45 for (ttlet &init_item : init) {
58 [[nodiscard]] iterator_type
begin() noexcept
60 return std::launder(
reinterpret_cast<pointer_type
>(&_buffer[0]));
66 [[nodiscard]] const_iterator_type
begin() const noexcept
68 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
74 [[nodiscard]] const_iterator_type
cbegin() const noexcept
76 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
82 [[nodiscard]] iterator_type
end() noexcept
90 [[nodiscard]] const_iterator_type
end() const noexcept
98 [[nodiscard]] const_iterator_type
cend() const noexcept
106 [[nodiscard]]
constexpr size_type
max_size() const noexcept
114 [[nodiscard]] size_type
size() const noexcept
116 return narrow_cast<size_type>(
end() -
begin());
122 [[nodiscard]]
bool full() const noexcept
130 [[nodiscard]]
bool empty() const noexcept
132 return _top ==
begin();
139 [[nodiscard]] reference_type
operator[](
size_t index)
noexcept
141 tt_axiom(index <
size());
142 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
149 [[nodiscard]] const_reference_type
operator[](
size_t index)
const noexcept
151 tt_axiom(index <
size());
152 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
159 [[nodiscard]] reference_type
at(
size_t index)
noexcept
161 if (index >=
size()) {
164 return (*
this)[index];
171 [[nodiscard]] const_reference_type
at(
size_t index)
const noexcept
173 if (index >=
size()) {
176 return (*
this)[index];
182 [[nodiscard]] reference_type
back() noexcept
185 return *std::launder(
reinterpret_cast<pointer_type
>(_top - 1));
191 [[nodiscard]] const_reference_type
back() const noexcept
194 return *std::launder(
reinterpret_cast<pointer_type
>(_top - 1));
201 template<
typename... Args>
205 new (
end()) value_type(std::forward<Args>(args)...);
213 template<
typename Arg>
requires (std::is_convertible_v<Arg,value_type>)
225 auto *old_item = std::launder(--_top);
226 std::destroy_at(old_item);
235 while (_top != new_end) {
249 std::aligned_storage_t<
sizeof(T), std::alignment_of_v<T>> _buffer[MaxSize];
A static sized stack.
Definition stack.hpp:23
reference_type operator[](size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:139
const_iterator_type end() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:90
const_iterator_type cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:98
reference_type back() noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:182
const_reference_type back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:191
void emplace_back(Args &&... args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:202
bool empty() const noexcept
Check if the stack is empty.
Definition stack.hpp:130
reference_type at(size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:159
stack() noexcept
Construct an empty stack.
Definition stack.hpp:37
void pop_back(iterator_type new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:233
stack(std::initializer_list< value_type > init) noexcept
Construct a stack with the given data.
Definition stack.hpp:42
void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:214
void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:242
bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:122
size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:114
const_iterator_type cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:74
iterator_type end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:82
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:106
void pop_back() noexcept
Remove the value at the top of the stack.
Definition stack.hpp:222
const_reference_type at(size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:171
const_reference_type operator[](size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:149
const_iterator_type begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:66
iterator_type begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:58