xtd 0.2.0
Loading...
Searching...
No Matches
stack.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "icollection.hpp"
7#include "../../size.hpp"
8#include "../../string.hpp"
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
46 template<class type_t, class container_t = std::deque<type_t>>
47 class stack : public xtd::object, public xtd::collections::generic::icollection<type_t> {
48 public:
50
53 using value_type = typename icollection<type_t>::value_type;
63
65
68 stack() = default;
72 data_ = std::move(stack.data_);
73 stack.data_ = new_ptr<stack_data>();
74 }
75
77 stack(const stack& stack) = default;
80 stack(std::stack<type_t>&& stack) {
81 auto tmp = std::vector<type_t> {};
82 tmp.reserve(stack.size());
83
84 while (!stack.empty()) {
85 tmp.push_back(std::move(stack.top()));
86 stack.pop();
87 }
88
89 for (auto iterator = tmp.rbegin(); iterator != tmp.rend(); ++iterator)
90 data_->items.push(*iterator);
91
93 }
94
96 stack(const std::stack<type_t>& stack) {
97 struct std_stack : public std::stack<type_t> {
98 std_stack(const std::stack<type_t>& stack) : ptr {reinterpret_cast<const std_stack*>(&stack)} {}
99 auto begin() const {return ptr->c.begin();}
100 auto end() const {return ptr->c.end();}
101 const std_stack* ptr;
102 };
103 auto items = std_stack {stack};
104 data_->items = base_type(items.begin(), items.end());
106 }
107
109 stack(const ienumerable<value_type>& collection) {
110 for (auto item : collection)
111 data_->items.push(item);
113 }
114
117 data_->items.reserve(capacity);
118 }
119
121 stack(std::initializer_list<type_t> il) {
122 data_->items = base_type {il};
124 }
125
128 template < std::input_iterator input_iterator_t >
129 stack(input_iterator_t first, input_iterator_t last) {
130 for (auto iterator = first; iterator != last; ++iterator)
131 push(*iterator);
133 }
134
135
137
141 auto capacity() const noexcept -> size_type {return data_->items.capacity();}
145 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.size();}
146
149 auto items() const {return std::stack<type_t>(std::deque<type_t>(data_->items.begin(), data_->items.end()));}
151
153
159 auto clear() -> void override {
160 data_->items.clear();
161 ++data_->version;
162 }
163
167 auto contains(const_reference value) const noexcept -> bool override {
168 for (const auto& item : data_->items)
169 if (xtd::collections::generic::helpers::equator<type_t> {}(item, value)) return true;
170 return false;
171 }
172
180 auto copy_to(xtd::array<type_t>& array, size_type array_index) const -> void override {
182 array_index += count() - 1;
183 for (const auto& item : data_->items)
184 array[array_index--] = item;
185 }
186
190 auto pop() -> value_type {
191 auto result = value_type {};
193 return result;
194 }
195
198 auto push(const_reference value) -> void {
199 data_->items.push(value);
201 ++data_->version;
202 }
203
206 auto push(value_type&& value) -> void {
207 data_->items.push(std::move(value));
209 ++data_->version;
210 }
211
216 if (data_->items.capacity() < capacity) data_->items.reserve(capacity);
217 return data_->items.capacity();
218 }
219
222 enumerator<value_type> get_enumerator() const noexcept override {
223 struct stack_enumerator : public ienumerator < value_type > {
224 explicit stack_enumerator(const stack & items, xtd::size version) : items_(items), version_(version) {}
225
226 const value_type& current() const override {
227 if (iterator_ == items_.data_->items.crend()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
228 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
229 return *iterator_;
230 }
231
232 bool move_next() override {
233 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
234 if (index_++ && iterator_ != items_.data_->items.crend()) ++iterator_;
235 else iterator_ = items_.data_->items.crbegin();
236 return iterator_ != items_.data_->items.crend();
237 }
238
239 void reset() override {
240 index_ = 0;
241 version_ = items_.data_->version;
242 iterator_ = items_.data_->items.crend();
243 }
244
245 private:
246 size_type index_ = 0;
247 const stack& items_;
248 typename base_type::const_reverse_iterator iterator_ = items_.data_->items.crend();
249 size_type version_ = 0;
250 };
251 return {new_ptr < stack_enumerator > (self_, data_->version)};
252 }
253
258 [[nodiscard]] auto peek() const -> value_type {
259 auto result = value_type {};
261 return result;
262 }
263
266 auto to_array() const -> xtd::array<value_type> {
268 copy_to(array, 0);
269 return array;
270 }
271
274 auto to_string() const noexcept -> string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
275
280 auto trim_excess() -> void {
281 if (count() < static_cast<xtd::size>(capacity() * 0.9)) trim_excess(count());
282 }
283
289 auto temp = base_type {};
290 temp.reserve(capacity);
291 for (auto& i : data_->items)
292 temp.push(i);
293 data_->items = std::move(temp);
294 ++data_->version;
295 }
296
300 [[nodiscard]] auto try_pop(value_type& result) noexcept -> bool {
301 if (!try_peek(result)) return false;
302 data_->items.pop();
303 ++data_->version;
304 return true;
305 }
306
310 [[nodiscard]] auto try_peek(value_type& result) const noexcept -> bool {
311 result = count() ? data_->items.top() : type_t {};
312 return count();
313 }
314
315
317
322 auto operator =(const stack& other) -> stack& = default;
326 auto operator =(stack&& other) noexcept -> stack& {
327 data_->items = std::move(other.data_->items);
328 ++data_->version;
329 return self_;
330 }
331
334 auto operator =(const std::initializer_list<type_t>& items) -> stack& {
335 data_->items = items;
336 ++data_->version;
337 return self_;
338 }
339
342 operator std::stack<type_t>() const {return items();}
344
345 private:
346 auto is_read_only() const noexcept -> bool override {return false;}
347 auto is_synchronized() const noexcept -> bool override {return false;}
348 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
349 auto add(const type_t& value) -> void override {push(value);}
350 auto remove(const type_t&) -> bool override {return false;}
351
352 struct stack_data {
354 xtd::object sync_root;
355 xtd::size version = 0;
356 };
357
359 };
360
362 // Deduction guides for xtd::collections::generic::stack
363 // {
364 template < class type_t, class container_t>
366
367 template < class type_t, class container_t>
369
370 template < class type_t>
372
373 template < class type_t>
374 stack(std::initializer_list<type_t>) -> stack<type_t>;
375
376 template <class input_iterator_t>
377 stack(input_iterator_t, input_iterator_t) -> stack<std::iter_value_t<input_iterator_t>>;
378 // }
380 }
381 }
382}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:124
static basic_string join(const basic_string &separator, const collection_t &values) noexcept
Definition basic_string.hpp:1702
Provides a raw, contiguous memory based LIFO (last-in, first-out) stack.
Definition raw_stack.hpp:43
typename container_type::const_reverse_iterator const_reverse_iterator
Const reverse iterator type.
Definition raw_stack.hpp:55
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a variable size last-in-first-out (LIFO) collection of instances of the same specified typ...
Definition stack.hpp:47
auto push(const_reference value) -> void
Adds an object to the end of the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:198
const value_type & const_reference
Represents the const reference of list value type.
Definition stack.hpp:61
stack()=default
Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and h...
stack(const ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::generic::stack <type_t> class that contains eleme...
Definition stack.hpp:109
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition stack.hpp:57
auto ensure_capacity(size_type capacity) -> size_type
Ensures that the capacity of this stack is at least the specified capacity. If the current capacity i...
Definition stack.hpp:215
stack(std::stack< type_t > &&stack)
Move constructor with specified stack.
Definition stack.hpp:80
auto try_peek(value_type &result) const noexcept -> bool
Returns a value that indicates whether there is an object at the beginning of the xtd::collections::g...
Definition stack.hpp:310
stack(const std::stack< type_t > &stack)
Default copy constructor with specified stack.
Definition stack.hpp:96
auto capacity() const noexcept -> size_type
Definition stack.hpp:141
auto trim_excess(size_type capacity) -> void
Sets the capacity of a xtd::collections::generic::stack <type_t> object to the specified number of en...
Definition stack.hpp:287
auto try_pop(value_type &result) noexcept -> bool
Removes the object at the beginning of the xtd::collections::generic::stack <type_t>,...
Definition stack.hpp:300
auto operator=(const stack &other) -> stack &=default
Copy assignment operator. Replaces the contents with a copy of the contents of other.
xtd::collections::generic::helpers::raw_stack< value_type, container_t > base_type
Represents the list base type.
Definition stack.hpp:55
auto items() const
Definition stack.hpp:149
auto count() const noexcept -> size_type override
Gets the number of nodes actually contained in the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:145
auto to_string() const noexcept -> string override
Returns a xtd::string that represents the current object.
Definition stack.hpp:274
stack(stack &&stack)
Move constructor with specified stack.
Definition stack.hpp:71
stack(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition stack.hpp:129
auto peek() const -> value_type
Returns the object at the beginning of the xtd::collections::generic::stack <type_t> without removing...
Definition stack.hpp:258
auto trim_excess() -> void
Sets the capacity to the actual number of elements in the xtd::collections::generic::stack <type_t>,...
Definition stack.hpp:280
stack(const stack &stack)=default
Default copy constructor with specified stack.
typename icollection< type_t >::value_type value_type
Represents the list value type.
Definition stack.hpp:53
auto to_array() const -> xtd::array< value_type >
Copies the xtd::collections::generic::stack <type_t> elements to a new array.
Definition stack.hpp:266
auto copy_to(xtd::array< type_t > &array, size_type array_index) const -> void override
Copies the entire xtd::colllections::generic::linked_list <type_t> to a compatible one-dimensional ar...
Definition stack.hpp:180
auto contains(const_reference value) const noexcept -> bool override
Determines whether an element is in the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:167
auto pop() -> value_type
Removes and returns the object at the beginning of the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:190
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:222
stack(std::initializer_list< type_t > il)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition stack.hpp:121
stack(size_type capacity)
Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and h...
Definition stack.hpp:116
auto clear() -> void override
Removes all elements from the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:159
auto push(value_type &&value) -> void
Adds an object to the end of the xtd::collections::generic::stack <type_t>.
Definition stack.hpp:206
value_type & reference
Represents the reference of list value type.
Definition stack.hpp:59
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
object()=default
Create a new instance of the ultimate base class object.
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
Contains xtd::collections::generic::icollection <type_t> interface.
generic::stack< xtd::any_object > stack
Represents a collection of xtd::any_object.
Definition stack.hpp:31
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
@ add
The Add key.
Definition console_key.hpp:170
@ i
The I key.
Definition console_key.hpp:104
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
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
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
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
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 xtd::collections::generic::helpers::raw_stack class.
Contains xtd::string alias.
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Contains xtd::size type.