HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
stack.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <type_traits>
10#include <memory>
11#include <initializer_list>
12
13
14
15namespace hi::inline v1 {
16
24template<typename T, std::size_t MaxSize>
25class stack {
26public:
27 using value_type = T;
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;
34 using size_type = std::size_t;
35 using difference_type = ptrdiff_t;
36
39 stack() noexcept : _top(begin()) {}
40
44 stack(std::initializer_list<value_type> init) noexcept : _top(begin())
45 {
46 for (hilet &init_item : init) {
47 push_back(init_item);
48 }
49 }
50
51 ~stack() noexcept
52 {
53 clear();
54 }
55
56 [[nodiscard]] const_pointer data() const noexcept
57 {
58 return reinterpret_cast<const_pointer>(_buffer);
59 }
60
61 [[nodiscard]] pointer data() noexcept
62 {
63 return reinterpret_cast<pointer>(_buffer);
64 }
65
69 [[nodiscard]] iterator begin() noexcept
70 {
71 return data();
72 }
73
77 [[nodiscard]] const_iterator begin() const noexcept
78 {
79 return data();
80 }
81
85 [[nodiscard]] const_iterator cbegin() const noexcept
86 {
87 return data();
88 }
89
93 [[nodiscard]] iterator end() noexcept
94 {
95 return _top;
96 }
97
101 [[nodiscard]] const_iterator end() const noexcept
102 {
103 return _top;
104 }
105
109 [[nodiscard]] const_iterator cend() const noexcept
110 {
111 return _top;
112 }
113
117 [[nodiscard]] constexpr size_type max_size() const noexcept
118 {
119 return MaxSize;
120 }
121
125 [[nodiscard]] size_type size() const noexcept
126 {
127 return narrow_cast<size_type>(std::distance(begin(), end()));
128 }
129
133 [[nodiscard]] bool full() const noexcept
134 {
135 return _top == (data() + max_size());
136 }
137
141 [[nodiscard]] bool empty() const noexcept
142 {
143 return _top == data();
144 }
145
150 [[nodiscard]] reference_type operator[](std::size_t index) noexcept
151 {
152 hi_assert_bounds(index, *this);
153 return *(data() + index);
154 }
155
160 [[nodiscard]] const_reference_type operator[](std::size_t index) const noexcept
161 {
162 hi_assert_bounds(index, *this);
163 return *(data() + index);
164 }
165
170 [[nodiscard]] reference_type at(std::size_t index) noexcept
171 {
172 if (index >= size()) {
173 throw std::out_of_range("stack::at");
174 }
175 return (*this)[index];
176 }
177
182 [[nodiscard]] const_reference_type at(std::size_t index) const noexcept
183 {
184 if (index >= size()) {
185 throw std::out_of_range("stack::at");
186 }
187 return (*this)[index];
188 }
189
193 [[nodiscard]] reference_type back() noexcept
194 {
195 hi_axiom(not empty());
196 return *(_top - 1);
197 }
198
202 [[nodiscard]] const_reference_type back() const noexcept
203 {
204 hi_axiom(not empty());
205 return *(_top - 1);
206 }
207
212 template<typename... Args>
213 void emplace_back(Args &&...args) noexcept
214 {
215 hi_axiom(not full());
216 new (_top++) value_type(std::forward<Args>(args)...);
217 }
218
223 template<typename Arg>
224 requires(std::is_convertible_v<Arg, value_type>) void push_back(Arg &&arg) noexcept
225 {
226 emplace_back(std::forward<Arg>(arg));
227 }
228
232 void pop_back() noexcept
233 {
234 hi_axiom(not empty());
235 std::destroy_at(--_top);
236 }
237
242 void pop_back(iterator new_end) noexcept
243 {
244 while (_top != new_end) {
245 pop_back();
246 }
247 }
248
251 void clear() noexcept
252 {
253 std::destroy(data(), _top);
254 _top = data();
255 }
256
257private:
258 pointer _top;
259 alignas(T) std::byte _buffer[MaxSize * sizeof(T)];
260};
261
262} // namespace hi::inline v1
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
T distance(T... args)