xtd 0.2.0
Loading...
Searching...
No Matches
raw_stack.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <stack>
6#include "../../../self.hpp"
7
9namespace xtd {
11 namespace collections {
13 namespace generic {
15 namespace helpers {
42 template<class type_t, class container_t = std::deque<type_t>>
43 class raw_stack final : public std::stack<type_t, container_t> {
44 public:
46
48 using base_type = std::stack<type_t, container_t>;
49 using container_type = typename base_type::container_type;
50 using value_type = typename base_type::value_type;
51 using size_type = typename base_type::size_type;
52 using reference = typename base_type::reference;
53 using const_reference = typename base_type::const_reference;
54 using const_iterator = typename container_type::const_iterator;
55 using const_reverse_iterator = typename container_type::const_reverse_iterator;
57
59
64
70 ensure_capacity(capacity);
71 }
72
75
80
85 template<class input_iterator_t>
86 raw_stack(input_iterator_t first, input_iterator_t last) : base_type(first, last) {shrink_to_fit();}
87
89 template<class allocator_t>
90 explicit raw_stack(const allocator_t& alloc) : base_type(alloc) {shrink_to_fit();}
92
94
98 auto begin() const -> const_iterator {return base_type::c.cbegin();}
99
102 auto capacity() const noexcept -> size_type {return capacity_;}
103
106 auto cbegin() const -> const_iterator {return base_type::c.cbegin();}
107
110 auto cend() const -> const_iterator {return base_type::c.cend();}
111
114 auto crbegin() const -> const_reverse_iterator {return base_type::c.crbegin();}
115
118 auto crend() const -> const_reverse_iterator {return base_type::c.crend();}
119
122 auto end() const -> const_iterator {return base_type::c.cend();}
123
126 auto items() noexcept -> base_type& {return self_;}
129 auto items() const noexcept -> const base_type& {return self_;}
130
133 auto rbegin() const -> const_reverse_iterator {return base_type::c.crbegin();}
134
137 auto rend() const -> const_reverse_iterator {return base_type::c.crend();}
138
141 auto size() const noexcept -> size_type {return base_type::c.size();}
143
145
148 auto clear() -> void {base_type::c.clear();}
149
153 auto push(const value_type& value) -> void {
154 base_type::push(value);
155 ensure_capacity(base_type::c.size());
156 }
157
161 auto push(value_type&& value) -> void {
162 base_type::push(std::move(value));
163 ensure_capacity(base_type::c.size());
164 }
165
168 auto pop() -> void {
169 base_type::pop();
170 ensure_capacity(size());
171 }
172
176 auto reserve(size_type count) -> void {
177 if (capacity_ >= count) return;
178 ensure_capacity(count);
179 }
180
183 auto shrink_to_fit() -> void {
184 base_type::c.shrink_to_fit();
185 capacity_ = base_type::c.size();
186 }
187
188
190
192 raw_stack& operator=(const raw_stack& other) = default;
194 operator const base_type& () const noexcept {return self_;}
195 operator base_type& () noexcept {return self_;}
197
198 private:
199 auto ensure_capacity(size_type capacity) -> void {
200 if (capacity <= capacity_) return;
201 capacity_ = capacity;
202 auto original_size = base_type::size();
203 base_type::c.resize(capacity_);
204 base_type::c.resize(original_size); // Restore size while keeping capacity.
205 }
206
207 size_type capacity_ = base_type::size();
208 };
209
211 // Deduction guides for xtd::collections::generic::helpers::raw_stack
212 // {
213 template<class container_t>
214 raw_stack(container_t) -> raw_stack<typename container_t::value_type, container_t>;
215
216 template<class container_t, class allocator_t>
217 raw_stack(container_t, allocator_t) -> raw_stack<typename container_t::value_type, container_t>;
218
219 template< class input_iterator_t>
220 raw_stack(input_iterator_t, input_iterator_t) -> raw_stack<typename std::iterator_traits<input_iterator_t>::value_type>;
221
222 template< class input_iterator_t, class allocator_t>
223 raw_stack(input_iterator_t, input_iterator_t, allocator_t) -> raw_stack<typename std::iterator_traits<input_iterator_t>::value_type, std::deque<typename std::iterator_traits<input_iterator_t>::value_type, allocator_t>>;
224 // }
226 }
227 }
228 }
229}
auto shrink_to_fit() -> void
Reduces capacity to fit the current size.
Definition raw_stack.hpp:183
raw_stack()
Default constructor. Initializes an empty stack.
Definition raw_stack.hpp:63
auto cend() const -> const_iterator
Gets a const iterator to the end of the stack.
Definition raw_stack.hpp:110
typename base_type::value_type value_type
Type of elements stored.
Definition raw_stack.hpp:50
auto rend() const -> const_reverse_iterator
Gets a const reverse iterator to the end of the stack.
Definition raw_stack.hpp:137
raw_stack(const allocator_t &alloc)
Constructs a stack with a specific allocator.
Definition raw_stack.hpp:90
auto begin() const -> const_iterator
Gets a const iterator to the beginning of the stack.
Definition raw_stack.hpp:98
typename base_type::size_type size_type
Unsigned integer type used for size and capacity.
Definition raw_stack.hpp:51
auto clear() -> void
Removes all elements from the stack.
Definition raw_stack.hpp:148
auto reserve(size_type count) -> void
Reserves storage to hold at least count elements.
Definition raw_stack.hpp:176
typename base_type::container_type container_type
The underlying container type.
Definition raw_stack.hpp:49
auto crbegin() const -> const_reverse_iterator
Gets a const reverse iterator to the beginning of the stack.
Definition raw_stack.hpp:114
auto items() noexcept -> base_type &
Access to the underlying base stack.
Definition raw_stack.hpp:126
auto size() const noexcept -> size_type
Gets the number of elements in the stack.
Definition raw_stack.hpp:141
auto end() const -> const_iterator
Gets a const iterator to the end of the stack.
Definition raw_stack.hpp:122
auto items() const noexcept -> const base_type &
Access to the underlying base stack.
Definition raw_stack.hpp:129
raw_stack(input_iterator_t first, input_iterator_t last)
Constructs a stack from a range of iterators.
Definition raw_stack.hpp:86
auto push(value_type &&value) -> void
Moves the element into the back of the stack.
Definition raw_stack.hpp:161
auto capacity() const noexcept -> size_type
Definition raw_stack.hpp:102
raw_stack(raw_stack &&other)
Move constructor.
Definition raw_stack.hpp:79
typename base_type::const_reference const_reference
Const reference to element type.
Definition raw_stack.hpp:53
typename container_type::const_reverse_iterator const_reverse_iterator
Const reverse iterator type.
Definition raw_stack.hpp:55
raw_stack(const raw_stack &other)
Copy constructor.
Definition raw_stack.hpp:74
std::stack< type_t, container_t > base_type
The base STL stack type.
Definition raw_stack.hpp:48
auto push(const value_type &value) -> void
Adds a copy of the element at the back of the stack.
Definition raw_stack.hpp:153
auto rbegin() const -> const_reverse_iterator
Gets a const reverse iterator to the beginning of the stack.
Definition raw_stack.hpp:133
raw_stack(size_type capacity)
Constructs an empty stack with reserved capacity.
Definition raw_stack.hpp:68
raw_stack & operator=(const raw_stack &other)=default
Copy assignment.
typename base_type::reference reference
Reference to element type.
Definition raw_stack.hpp:52
typename container_type::const_iterator const_iterator
Const iterator type.
Definition raw_stack.hpp:54
auto pop() -> void
Removes the element at the front of the stack.
Definition raw_stack.hpp:168
auto cbegin() const -> const_iterator
Gets a const iterator to the beginning of the stack.
Definition raw_stack.hpp:106
auto crend() const -> const_reverse_iterator
Gets a const reverse iterator to the end of the stack.
Definition raw_stack.hpp:118
raw_stack & operator=(raw_stack &&other)=default
Move assignment.
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
@ other
The operating system is other.
Definition platform_id.hpp:60
The xtd::collections::generic::helpers namespace contains helpers for generic collections,...
Definition allocator.hpp:14
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
Contains self_ keyword.