7#include "../utility/utility.hpp"
8#include "../macros.hpp"
11#include <initializer_list>
24template<
typename T, std::
size_t MaxSize>
28 using pointer = value_type *;
29 using const_pointer = value_type
const *;
30 using reference_type = value_type &;
31 using const_reference_type = value_type
const &;
32 using iterator = pointer;
33 using const_iterator = const_pointer;
35 using difference_type = ptrdiff_t;
39 stack() noexcept : _top(begin()) {}
46 for (hilet &init_item : init) {
56 [[nodiscard]] const_pointer data() const noexcept
58 return reinterpret_cast<const_pointer
>(_buffer);
61 [[nodiscard]] pointer data() noexcept
63 return reinterpret_cast<pointer
>(_buffer);
69 [[nodiscard]] iterator
begin() noexcept
77 [[nodiscard]] const_iterator
begin() const noexcept
85 [[nodiscard]] const_iterator
cbegin() const noexcept
93 [[nodiscard]] iterator
end() noexcept
101 [[nodiscard]] const_iterator
end() const noexcept
109 [[nodiscard]] const_iterator
cend() const noexcept
127 return narrow_cast<size_type>(
std::distance(begin(), end()));
133 [[nodiscard]]
bool full() const noexcept
135 return _top == (data() + max_size());
141 [[nodiscard]]
bool empty() const noexcept
143 return _top == data();
152 hi_assert_bounds(index, *
this);
153 return *(data() + index);
162 hi_assert_bounds(index, *
this);
163 return *(data() + index);
172 if (index >= size()) {
175 return (*
this)[index];
184 if (index >= size()) {
187 return (*
this)[index];
193 [[nodiscard]] reference_type
back() noexcept
195 hi_axiom(not empty());
202 [[nodiscard]] const_reference_type
back() const noexcept
204 hi_axiom(not empty());
212 template<
typename... Args>
215 hi_axiom(not full());
216 new (_top++) value_type(std::forward<Args>(args)...);
223 template<
typename Arg>
224 requires(std::is_convertible_v<Arg, value_type>)
void push_back(Arg &&arg)
noexcept
226 emplace_back(std::forward<Arg>(arg));
234 hi_axiom(not empty());
235 std::destroy_at(--_top);
244 while (_top != new_end) {
253 std::destroy(data(), _top);
259 alignas(T) std::byte _buffer[MaxSize *
sizeof(T)];
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A static sized stack.
Definition stack.hpp:25
void pop_back() noexcept
Remove the value at the top of the stack.
Definition stack.hpp:232
size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:125
reference_type operator[](std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:150
void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:224
void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:251
void pop_back(iterator new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:242
const_reference_type at(std::size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:182
const_iterator end() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:101
reference_type back() noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:193
bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:133
stack() noexcept
Construct an empty stack.
Definition stack.hpp:39
reference_type at(std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:170
const_iterator begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:77
void emplace_back(Args &&...args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:213
const_reference_type back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:202
const_reference_type operator[](std::size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:160
iterator end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:93
stack(std::initializer_list< value_type > init) noexcept
Construct a stack with the given data.
Definition stack.hpp:44
bool empty() const noexcept
Check if the stack is empty.
Definition stack.hpp:141
const_iterator cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:109
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:117
const_iterator cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:85
iterator begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:69