9#include <initializer_list>
13namespace hi::inline
v1 {
22template<
typename T, std::
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;
33 using difference_type = ptrdiff_t;
37 stack() noexcept : _top(begin()) {}
44 for (
hilet &init_item : init) {
57 [[nodiscard]] iterator_type
begin() noexcept
59 return std::launder(
reinterpret_cast<pointer_type
>(&_buffer[0]));
65 [[nodiscard]] const_iterator_type
begin() const noexcept
67 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
73 [[nodiscard]] const_iterator_type
cbegin() const noexcept
75 return std::launder(
reinterpret_cast<const_pointer_type
>(&_buffer[0]));
81 [[nodiscard]] iterator_type
end() noexcept
89 [[nodiscard]] const_iterator_type
end() const noexcept
97 [[nodiscard]] const_iterator_type
cend() const noexcept
115 return narrow_cast<size_type>(end() - begin());
121 [[nodiscard]]
bool full() const noexcept
123 return _top == (begin() + max_size());
129 [[nodiscard]]
bool empty() const noexcept
131 return _top == begin();
140 hi_axiom(index < size());
141 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
150 hi_axiom(index < size());
151 return *std::launder(
reinterpret_cast<pointer_type
>(&_buffer[index]));
160 if (index >= size()) {
163 return (*
this)[index];
172 if (index >= size()) {
175 return (*
this)[index];
181 [[nodiscard]] reference_type
back() noexcept
184 return *std::launder(
reinterpret_cast<pointer_type
>(_top - 1));
190 [[nodiscard]] const_reference_type
back() const noexcept
193 return *std::launder(
reinterpret_cast<pointer_type
>(_top - 1));
200 template<
typename... Args>
204 new (end()) value_type(std::forward<Args>(args)...);
212 template<
typename Arg>
213 requires(std::is_convertible_v<Arg, value_type>)
void push_back(Arg &&arg)
noexcept
215 emplace_back(std::forward<Arg>(arg));
224 auto *old_item = std::launder(--_top);
225 std::destroy_at(old_item);
234 while (_top != new_end) {
243 std::destroy(begin(), end());
248 std::aligned_storage_t<
sizeof(T), std::alignment_of_v<T>> _buffer[MaxSize];
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
A static sized stack.
Definition stack.hpp:23
void pop_back() noexcept
Remove the value at the top of the stack.
Definition stack.hpp:221
size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:113
reference_type operator[](std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:138
const_iterator_type cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:73
void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:213
void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:241
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:170
void pop_back(iterator_type new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:232
reference_type back() noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:181
const_iterator_type begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:65
bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:121
stack() noexcept
Construct an empty stack.
Definition stack.hpp:37
iterator_type begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:57
reference_type at(std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:158
const_iterator_type end() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:89
void emplace_back(Args &&...args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:201
const_reference_type back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:190
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:148
stack(std::initializer_list< value_type > init) noexcept
Construct a stack with the given data.
Definition stack.hpp:42
bool empty() const noexcept
Check if the stack is empty.
Definition stack.hpp:129
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:105
const_iterator_type cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:97
iterator_type end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:81