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#include <stdexcept>
13#include <array>
14
15hi_export_module(hikogui.container.stack);
16
17hi_export namespace hi::inline v1 {
18
29template<typename T, std::size_t MaxSize>
30class stack {
31public:
32 using value_type = T;
34 using pointer = array_type::pointer;
35 using const_pointer = array_type::const_pointer;
36 using reference = array_type::reference;
37 using const_reference = array_type::const_reference;
38 using iterator = array_type::iterator;
39 using const_iterator = array_type::const_iterator;
40 using size_type = array_type::size_type;
41 using difference_type = array_type::difference_type;
42
43 constexpr ~stack() noexcept = default;
44 stack(stack const&) = delete;
45 stack(stack&&) = delete;
46 stack& operator=(stack const&) = delete;
47 stack& operator=(stack&&) = delete;
48
51 constexpr stack() noexcept
52 {
53 _top = _data.begin();
54 }
55
56 [[nodiscard]] constexpr const_pointer data() const noexcept
57 {
58 return _data.data();
59 }
60
61 [[nodiscard]] constexpr pointer data() noexcept
62 {
63 return _data.data();
64 }
65
69 [[nodiscard]] constexpr iterator begin() noexcept
70 {
71 return _data.begin();
72 }
73
77 [[nodiscard]] constexpr const_iterator begin() const noexcept
78 {
79 return _data.begin();
80 }
81
85 [[nodiscard]] constexpr const_iterator cbegin() const noexcept
86 {
87 return _data.cbegin();
88 }
89
93 [[nodiscard]] constexpr iterator end() noexcept
94 {
95 return _top;
96 }
97
101 [[nodiscard]] constexpr const_iterator end() const noexcept
102 {
103 return _top;
104 }
105
109 [[nodiscard]] constexpr 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]] constexpr size_type size() const noexcept
126 {
127 return narrow_cast<size_type>(std::distance(begin(), end()));
128 }
129
133 [[nodiscard]] constexpr bool full() const noexcept
134 {
135 return _top == _data.end();
136 }
137
141 [[nodiscard]] constexpr bool empty() const noexcept
142 {
143 return _top == _data.begin();
144 }
145
150 [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept
151 {
152 hi_axiom_bounds(index, *this);
153 return _data[index];
154 }
155
160 [[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept
161 {
162 hi_axiom_bounds(index, *this);
163 return _data[index];
164 }
165
170 [[nodiscard]] constexpr reference at(std::size_t index) noexcept
171 {
172 if (index >= size()) {
173 throw std::out_of_range("stack::at");
174 }
175 return _data[index];
176 }
177
182 [[nodiscard]] constexpr const_reference at(std::size_t index) const noexcept
183 {
184 if (index >= size()) {
185 throw std::out_of_range("stack::at");
186 }
187 return _data[index];
188 }
189
193 [[nodiscard]] constexpr reference back() noexcept
194 {
195 hi_axiom(not empty());
196 return *(_top - 1);
197 }
198
202 [[nodiscard]] constexpr const_reference back() const noexcept
203 {
204 hi_axiom(not empty());
205 return *(_top - 1);
206 }
207
212 template<typename... Args>
213 constexpr void emplace_back(Args&&...args) noexcept
214 {
215 hi_axiom(not full());
216 *(_top++) = value_type(std::forward<Args>(args)...);
217 }
218
223 template<typename Arg>
224 constexpr void push_back(Arg&& arg) noexcept
225 requires(std::is_convertible_v<Arg, value_type>)
226 {
227 hi_axiom(not full());
228 *(_top++) = std::forward<Arg>(arg);
229 }
230
234 constexpr void pop_back() noexcept
235 {
236 hi_axiom(not empty());
237 --_top;
238 }
239
244 constexpr void pop_back(const_iterator new_end) noexcept
245 {
246 while (_top != new_end) {
247 pop_back();
248 }
249 }
250
253 constexpr void clear() noexcept
254 {
255 _top = _data.begin();
256 }
257
258private:
259 array_type _data;
260 array_type::iterator _top;
261};
262
263} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A static sized stack.
Definition stack.hpp:30
constexpr const_iterator end() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:101
constexpr bool full() const noexcept
Check if the stack is full.
Definition stack.hpp:133
constexpr const_iterator cend() const noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:109
constexpr const_iterator begin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:77
constexpr iterator end() noexcept
Get an iterator to the last element on the stack.
Definition stack.hpp:93
constexpr stack() noexcept
Construct an empty stack.
Definition stack.hpp:51
constexpr bool empty() const noexcept
Check if the stack is empty.
Definition stack.hpp:141
constexpr void emplace_back(Args &&...args) noexcept
Construct an object after the current top of the stack.
Definition stack.hpp:213
constexpr size_type size() const noexcept
The number of elements that fit on the stack.
Definition stack.hpp:125
constexpr void pop_back(const_iterator new_end) noexcept
Pop elements of the stack through the given iterator.
Definition stack.hpp:244
constexpr reference at(std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:170
constexpr const_reference back() const noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:202
constexpr void push_back(Arg &&arg) noexcept
Push a new value to after the current top of the stack.
Definition stack.hpp:224
constexpr const_reference operator[](std::size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:160
constexpr reference operator[](std::size_t index) noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:150
constexpr void pop_back() noexcept
Remove the value at the top of the stack.
Definition stack.hpp:234
constexpr reference back() noexcept
Get a reference to the element at the top of the stack.
Definition stack.hpp:193
constexpr iterator begin() noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:69
constexpr size_type max_size() const noexcept
The maximum number of elements that fit on the stack.
Definition stack.hpp:117
constexpr const_reference at(std::size_t index) const noexcept
Get a reference to an element on the stack at an index.
Definition stack.hpp:182
constexpr const_iterator cbegin() const noexcept
Get an iterator to the first element on the stack.
Definition stack.hpp:85
constexpr void clear() noexcept
Remove all elements from the stack.
Definition stack.hpp:253
T distance(T... args)