9#include <initializer_list>
10#include "utility/module.hpp"
12namespace hi::inline
v1 {
21template<
typename T, std::
size_t MaxSize>
25 using pointer_type = value_type *;
26 using const_pointer_type = value_type
const *;
27 using reference_type = value_type &;
28 using const_reference_type = value_type
const &;
29 using iterator_type = pointer_type;
30 using const_iterator_type = const_pointer_type;
32 using difference_type = ptrdiff_t;
36 stack() noexcept : _top(begin()) {}
43 for (
hilet &init_item : init) {
56 [[nodiscard]] iterator_type
begin() noexcept
58 return std::launder(
reinterpret_cast<pointer_type
>(&_buffer[0]));
64 [[nodiscard]] const_iterator_type
begin() const noexcept
66 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
72 [[nodiscard]] const_iterator_type
cbegin() const noexcept
74 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
80 [[nodiscard]] iterator_type
end() noexcept
88 [[nodiscard]] const_iterator_type
end() const noexcept
96 [[nodiscard]] const_iterator_type
cend() const noexcept
114 return narrow_cast<size_type>(end() - begin());
120 [[nodiscard]]
bool full() const noexcept
122 return _top == (begin() + max_size());
128 [[nodiscard]]
bool empty() const noexcept
130 return _top == begin();
140 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
150 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
159 if (index >= size()) {
162 return (*
this)[index];
171 if (index >= size()) {
174 return (*
this)[index];
180 [[nodiscard]] reference_type
back() noexcept
183 return *std::launder(_top - 1);
189 [[nodiscard]] const_reference_type
back() const noexcept
192 return *std::launder(_top - 1);
199 template<
typename... Args>
203 new (end()) value_type(std::forward<Args>(args)...);
211 template<
typename Arg>
212 requires(std::is_convertible_v<Arg, value_type>)
void push_back(Arg &&arg)
noexcept
214 emplace_back(std::forward<Arg>(arg));
223 auto *old_item = std::launder(--_top);
224 std::destroy_at(old_item);
233 while (_top != new_end) {
242 std::destroy(begin(), end());
247 std::aligned_storage_t<
sizeof(T), std::alignment_of_v<T>> _buffer[MaxSize];
#define hi_assert_bounds(x,...)
Assert if a value is within bounds.
Definition assert.hpp:225
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
A static sized stack.
Definition stack.hpp:22
void pop_back() noexcept
Remove the value at the top of the stack.
Definition stack.hpp:220
size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:112
reference_type operator[](std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:137
const_iterator_type cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:72
void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:212
void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:240
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:169
void pop_back(iterator_type new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:231
reference_type back() noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:180
const_iterator_type begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:64
bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:120
stack() noexcept
Construct an empty stack.
Definition stack.hpp:36
iterator_type begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:56
reference_type at(std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:157
const_iterator_type end() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:88
void emplace_back(Args &&...args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:200
const_reference_type back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:189
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:147
stack(std::initializer_list< value_type > init) noexcept
Construct a stack with the given data.
Definition stack.hpp:41
bool empty() const noexcept
Check if the stack is empty.
Definition stack.hpp:128
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:104
const_iterator_type cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:96
iterator_type end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:80