7#include "../utility/module.hpp"
10#include <initializer_list>
12namespace hi::inline
v1 {
21template<
typename T, std::
size_t MaxSize>
26 using const_pointer = value_type
const *;
27 using reference_type = value_type &;
28 using const_reference_type = value_type
const &;
30 using const_iterator = const_pointer;
32 using difference_type = ptrdiff_t;
36 stack() noexcept : _top(begin()) {}
43 for (
hilet &init_item : init) {
53 [[nodiscard]] const_pointer data() const noexcept
55 return reinterpret_cast<const_pointer
>(_buffer);
58 [[nodiscard]] pointer data() noexcept
60 return reinterpret_cast<pointer
>(_buffer);
66 [[nodiscard]] iterator
begin() noexcept
74 [[nodiscard]] const_iterator
begin() const noexcept
82 [[nodiscard]] const_iterator
cbegin() const noexcept
90 [[nodiscard]] iterator
end() noexcept
98 [[nodiscard]] const_iterator
end() const noexcept
106 [[nodiscard]] const_iterator
cend() const noexcept
124 return narrow_cast<size_type>(
std::distance(begin(), end()));
130 [[nodiscard]]
bool full() const noexcept
132 return _top == (data() + max_size());
138 [[nodiscard]]
bool empty() const noexcept
140 return _top == data();
150 return *(data() + index);
160 return *(data() + index);
169 if (index >= size()) {
172 return (*
this)[index];
181 if (index >= size()) {
184 return (*
this)[index];
190 [[nodiscard]] reference_type
back() noexcept
199 [[nodiscard]] const_reference_type
back() const noexcept
209 template<
typename... Args>
213 new (_top++) value_type(std::forward<Args>(args)...);
220 template<
typename Arg>
221 requires(std::is_convertible_v<Arg, value_type>)
void push_back(Arg &&arg)
noexcept
223 emplace_back(std::forward<Arg>(arg));
232 std::destroy_at(--_top);
241 while (_top != new_end) {
250 std::destroy(data(), _top);
256 alignas(T) std::byte _buffer[MaxSize *
sizeof(T)];
#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:229
size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:122
reference_type operator[](std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:147
void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:221
void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:248
void pop_back(iterator new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:239
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:179
const_iterator end() 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:190
bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:130
stack() noexcept
Construct an empty stack.
Definition stack.hpp:36
reference_type at(std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:167
const_iterator begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:74
void emplace_back(Args &&...args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:210
const_reference_type back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:199
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:157
iterator end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:90
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:138
const_iterator cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:106
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:114
const_iterator cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:82
iterator begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:66
Definition concepts.hpp:36