tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
stack.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_STACK_STACK_HPP
4#define TETL_STACK_STACK_HPP
5
16#include <etl/_utility/move.hpp>
17
18namespace etl {
19
30template <typename T, typename Container>
31struct stack {
32 using value_type = typename Container::value_type;
33 using reference = typename Container::reference;
34 using const_reference = typename Container::const_reference;
35 using size_type = typename Container::size_type;
36 using container_type = Container;
37
39 constexpr stack()
40 : stack{Container{}}
41 {
42 }
43
46 constexpr explicit stack(Container const& cont)
47 : c{cont}
48 {
49 }
50
52 constexpr explicit stack(Container&& cont)
53 : c{etl::move(cont)}
54 {
55 }
56
58 constexpr stack(stack const& other) = default;
59
61 constexpr stack(stack&& other) noexcept = default;
62
64 [[nodiscard]] constexpr auto empty() const noexcept(noexcept(declval<Container>().empty())) -> bool
65 {
66 return c.empty();
67 }
68
70 [[nodiscard]] constexpr auto size() const noexcept(noexcept(declval<Container>().size())) -> size_type
71 {
72 return c.size();
73 }
74
78 [[nodiscard]] constexpr auto top() noexcept(noexcept(declval<Container>().back())) -> reference { return c.back(); }
79
83 [[nodiscard]] constexpr auto top() const noexcept(noexcept(declval<Container>().back())) -> const_reference
84 {
85 return c.back();
86 }
87
89 constexpr auto push(value_type const& x) noexcept(noexcept(declval<Container>().push_back(x))) -> void
90 {
91 c.push_back(x);
92 }
93
95 constexpr auto push(value_type&& x) noexcept(noexcept(declval<Container>().push_back(etl::move(x)))) -> void
96 {
97 c.push_back(etl::move(x));
98 }
99
104 template <typename... Args>
105 constexpr auto emplace(Args&&... args)
106 noexcept(noexcept(declval<Container>().emplace_back(etl::forward<Args>(args)...))) -> decltype(auto)
107 {
108 return c.emplace_back(etl::forward<Args>(args)...);
109 }
110
112 constexpr auto pop() noexcept(noexcept(declval<Container>().pop_back())) -> void { c.pop_back(); }
113
116 constexpr auto swap(stack& s) noexcept(is_nothrow_swappable_v<Container>) -> void
117 {
118 using etl::swap;
119 swap(c, s.c);
120 }
121
125 [[nodiscard]] friend constexpr auto operator==(stack const& lhs, stack const& rhs)
126 noexcept(noexcept(lhs.c == rhs.c)) -> bool
127 {
128 return lhs.c == rhs.c;
129 }
130
134 [[nodiscard]] friend constexpr auto operator!=(stack const& lhs, stack const& rhs)
135 noexcept(noexcept(lhs.c != rhs.c)) -> bool
136 {
137 return lhs.c != rhs.c;
138 }
139
143 [[nodiscard]] friend constexpr auto operator<(stack const& lhs, stack const& rhs) noexcept(noexcept(lhs.c < rhs.c))
144 -> bool
145 {
146 return lhs.c < rhs.c;
147 }
148
152 [[nodiscard]] friend constexpr auto operator<=(stack const& lhs, stack const& rhs)
153 noexcept(noexcept(lhs.c <= rhs.c)) -> bool
154 {
155 return lhs.c <= rhs.c;
156 }
157
161 [[nodiscard]] friend constexpr auto operator>(stack const& lhs, stack const& rhs) noexcept(noexcept(lhs.c > rhs.c))
162 -> bool
163 {
164 return lhs.c > rhs.c;
165 }
166
170 [[nodiscard]] friend constexpr auto operator>=(stack const& lhs, stack const& rhs)
171 noexcept(noexcept(lhs.c >= rhs.c)) -> bool
172 {
173 return lhs.c >= rhs.c;
174 }
175
176protected:
177 Container c;
178};
179
180// These deduction guides are provided for stack to allow deduction from
181// underlying container type.
182template <typename Container>
184
188template <typename T, typename C>
189 requires(is_swappable_v<C>)
190constexpr auto swap(stack<T, C>& lhs, stack<T, C>& rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
191{
192 lhs.swap(rhs);
193}
194
195} // namespace etl
196
197#endif // TETL_STACK_STACK_HPP
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
Definition adjacent_find.hpp:8
auto declval() noexcept -> add_rvalue_reference_t< T >
constexpr bool is_swappable_v
Definition is_swappable.hpp:21
stack(Container) -> stack< typename Container::value_type, Container >
auto swap(inplace_function< R(Args...), Capacity, Alignment > &lhs, inplace_function< R(Args...), Capacity, Alignment > &rhs) noexcept -> void
Overloads the etl::swap algorithm for etl::inplace_function. Exchanges the state of lhs with that of ...
Definition inplace_function.hpp:249
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
constexpr bool is_nothrow_swappable_v
Definition is_nothrow_swappable.hpp:19
The stack class is a container adapter that gives the programmer the functionality of a stack - speci...
Definition stack.hpp:31
constexpr auto swap(stack &s) noexcept(is_nothrow_swappable_v< Container >) -> void
Exchanges the contents of the container adaptor with those of other.
Definition stack.hpp:116
friend constexpr auto operator<=(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c<=rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:152
typename Container::const_reference const_reference
Definition stack.hpp:34
constexpr auto emplace(Args &&... args) noexcept(noexcept(declval< Container >().emplace_back(etl::forward< Args >(args)...))) -> decltype(auto)
Pushes a new element on top of the stack. The element is constructed in-place, i.e....
Definition stack.hpp:105
friend constexpr auto operator==(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c==rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:125
Container c
Definition stack.hpp:177
friend constexpr auto operator>(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c > rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:161
constexpr auto top() noexcept(noexcept(declval< Container >().back())) -> reference
Returns reference to the top element in the stack. This is the most recently pushed element....
Definition stack.hpp:78
constexpr stack(Container &&cont)
Move-constructs the underlying container c with cont .
Definition stack.hpp:52
typename Container::reference reference
Definition stack.hpp:33
typename Container::size_type size_type
Definition stack.hpp:35
friend constexpr auto operator>=(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c >=rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:170
constexpr stack(stack const &other)=default
Copy constructor.
constexpr auto empty() const noexcept(noexcept(declval< Container >().empty())) -> bool
Checks if the underlying container has no elements.
Definition stack.hpp:64
friend constexpr auto operator!=(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c !=rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:134
constexpr auto push(value_type const &x) noexcept(noexcept(declval< Container >().push_back(x))) -> void
Pushes the given element value to the top of the stack.
Definition stack.hpp:89
constexpr auto top() const noexcept(noexcept(declval< Container >().back())) -> const_reference
Returns reference to the top element in the stack. This is the most recently pushed element....
Definition stack.hpp:83
constexpr auto pop() noexcept(noexcept(declval< Container >().pop_back())) -> void
Removes the top element from the stack.
Definition stack.hpp:112
typename Container::value_type value_type
Definition stack.hpp:32
Container container_type
Definition stack.hpp:36
constexpr stack()
Default constructor. Value-initializes the container.
Definition stack.hpp:39
friend constexpr auto operator<(stack const &lhs, stack const &rhs) noexcept(noexcept(lhs.c< rhs.c)) -> bool
Compares the contents of the underlying containers of two container adaptors. The comparison is done ...
Definition stack.hpp:143
constexpr auto push(value_type &&x) noexcept(noexcept(declval< Container >().push_back(etl::move(x)))) -> void
Pushes the given element value to the top of the stack.
Definition stack.hpp:95
constexpr stack(stack &&other) noexcept=default
Move constructor.
constexpr stack(Container const &cont)
Copy-constructs the underlying container c with the contents of cont.
Definition stack.hpp:46
constexpr auto size() const noexcept(noexcept(declval< Container >().size())) -> size_type
Returns the number of elements in the underlying container.
Definition stack.hpp:70