xtd 0.2.0
Loading...
Searching...
No Matches
queue.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "linked_list.hpp"
7
9namespace xtd {
11 namespace collections {
13 namespace generic {
42 template<class type_t, class allocator_t = xtd::collections::generic::helpers::allocator<type_t>>
43 class queue : public xtd::object, public xtd::collections::generic::icollection<type_t> {
44 public:
46
49 using value_type = typename icollection<type_t>::value_type;
59
61
64 queue() = default;
67 queue(queue&& queue) = default;
70 queue(const queue& queue) = default;
73 queue(std::queue<type_t>&& queue) {
74 while (queue.size())
75 data_->items.add(queue.pop());
77 }
78
80 queue(const std::queue<type_t>& queue) {
81 struct std_queue : public std::queue<type_t> {
82 std_queue(const std::queue<type_t>& queue) : ptr {reinterpret_cast<const std_queue*>(&queue)} {}
83 auto begin() const {return ptr->c.begin();}
84 auto end() const {return ptr->c.end();}
85 const std_queue* ptr;
86 };
87 auto items = std_queue {queue};
88 data_->items = base_type(items.begin(), items.end());
90 }
91
93 queue(const ienumerable<value_type>& collection) {
94 data_->items = base_type {collection};
96 }
97
100 data_->capacity = capacity;
101 }
102
104 queue(std::initializer_list<type_t> il) {
105 data_->items = base_type {il};
107 }
108
111 template < std::input_iterator input_iterator_t >
112 queue(input_iterator_t first, input_iterator_t last) {
113 for (auto iterator = first; iterator != last; ++iterator)
114 enqueue(*iterator);
116 }
117
118
120
124 auto capacity() const noexcept -> size_type {return data_->capacity;}
128 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.count();}
129
132 std::queue<type_t> items() const {return std::queue<type_t>(std::deque<type_t>(data_->items.begin(), data_->items.end()));}
134
136
142 auto clear() -> void override {
143 data_->items.clear();
144 }
145
149 auto contains(const_reference value) const noexcept -> bool override {
150 return data_->items.contains(value);
151 }
152
160 auto copy_to(xtd::array<type_t>& array, size_type array_index) const -> void override {
161 data_->items.copy_to(array, array_index);
162 }
163
167 auto dequeue() -> value_type {
168 auto result = value_type {};
170 return result;
171 }
172
175 auto enqueue(const_reference value) -> void {
176 data_->items.add_last(value);
178 }
179
184 if (data_->capacity < capacity) data_->capacity = capacity;
185 return data_->capacity;
186 }
187
190 enumerator<value_type> get_enumerator() const noexcept override {
191 return data_->items.get_enumerator();
192 }
193
198 [[nodiscard]] auto peek() const -> value_type {
199 auto result = value_type {};
201 return result;
202 }
203
206 auto to_array() const -> xtd::array<value_type> {
208 copy_to(array, 0);
209 return array;
210 }
211
214 auto to_string() const noexcept -> string override {return data_->items.to_string();}
215
220 auto trim_excess() -> void {
221 if (count() * 1. < capacity() * 0.9) trim_excess(count());
222 }
223
231
235 [[nodiscard]] auto try_dequeue(value_type& result) noexcept -> bool {
236 if (!try_peek(result)) return false;
237 data_->items.remove_first();
238 return true;
239 }
240
244 [[nodiscard]] auto try_peek(value_type& result) const noexcept -> bool {
245 result = count() ? data_->items.first()->value() : type_t {};
246 return count();
247 }
248
249
251
255 operator std::queue<type_t>() const {return items();}
257
258 private:
259 auto is_read_only() const noexcept -> bool override {return false;}
260 auto is_synchronized() const noexcept -> bool override {return false;}
261 const xtd::object& sync_root() const noexcept override {return xtd::as<icollection<value_type>>(data_->items).sync_root();}
262 auto add(const type_t& value) -> void override {enqueue(value);}
263 auto remove(const type_t&) -> bool override {return false;}
264
265 struct queue_data {
268 };
269
271 };
272
274 // Deduction guides for xtd::collections::generic::queue
275 // {
276 template < class type_t, class allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
278
279 template < class type_t, class allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
281
282 template < class type_t>
283 queue(const ienumerable < type_t >&) -> queue < type_t >;
284
285 template < class type_t>
286 queue(std::initializer_list < type_t >) -> queue < type_t >;
287
288 template <class input_iterator_t>
289 queue(input_iterator_t, input_iterator_t) -> queue<typename input_iterator_t::value_type>;
290 // }
292 }
293 }
294}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Represents a doubly linked list.
Definition linked_list.hpp:51
Represents a first-in, first-out collection of objects.
Definition queue.hpp:43
auto trim_excess() -> void
Sets the capacity to the actual number of elements in the xtd::collections::generic::queue <type_t>,...
Definition queue.hpp:220
auto peek() const -> value_type
Returns the object at the beginning of the xtd::collections::generic::queue <type_t> without removing...
Definition queue.hpp:198
queue(const queue &queue)=default
Default copy constructor with specified queue.
auto trim_excess(size_type capacity) -> void
Sets the capacity of a xtd::collections::generic::queue <type_t> object to the specified number of en...
Definition queue.hpp:227
queue(queue &&queue)=default
Move constructor with specified queue.
queue()=default
Initializes a new instance of the xtd::collections::generic::queue <type_t> class that is empty and h...
auto try_dequeue(value_type &result) noexcept -> bool
Removes the object at the beginning of the xtd::collections::generic::queue <type_t>,...
Definition queue.hpp:235
auto dequeue() -> value_type
Removes and returns the object at the beginning of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:167
auto capacity() const noexcept -> size_type
Definition queue.hpp:124
const value_type & const_reference
Represents the const reference of list value type.
Definition queue.hpp:57
queue(size_type capacity)
Initializes a new instance of the xtd::collections::generic::queue <type_t> class that is empty and h...
Definition queue.hpp:99
queue(const std::queue< type_t > &queue)
Default copy constructor with specified queue.
Definition queue.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 queue.hpp:244
xtd::collections::generic::linked_list< value_type, allocator_t > base_type
Represents the list base type.
Definition queue.hpp:51
auto ensure_capacity(size_type capacity) -> size_type
Ensures that the capacity of this queue is at least the specified capacity. If the current capacity i...
Definition queue.hpp:183
auto to_string() const noexcept -> string override
Returns a xtd::string that represents the current object.
Definition queue.hpp:214
queue(std::queue< type_t > &&queue)
Move constructor with specified queue.
Definition queue.hpp:73
queue(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition queue.hpp:112
std::queue< xtd::any_object > items() const
Definition queue.hpp:132
auto contains(const_reference value) const noexcept -> bool override
Determines whether an element is in the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:149
auto enqueue(const_reference value) -> void
Adds an object to the end of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:175
auto clear() -> void override
Removes all elements from the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:142
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 queue.hpp:160
queue(const ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::generic::queue <type_t> class that contains eleme...
Definition queue.hpp:93
queue(std::initializer_list< type_t > il)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition queue.hpp:104
value_type & reference
Represents the reference of list value type.
Definition queue.hpp:55
auto to_array() const -> xtd::array< value_type >
Copies the xtd::collections::generic::queue <type_t> elements to a new array.
Definition queue.hpp:206
typename icollection< type_t >::value_type value_type
Represents the list value type.
Definition queue.hpp:49
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:190
auto count() const noexcept -> size_type override
Gets the number of nodes actually contained in the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:128
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition queue.hpp:53
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:44
generic::queue< xtd::any_object > queue
Represents a first-in, first-out collection of objects.
Definition queue.hpp:27
generic::ienumerable< xtd::any_object > ienumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection.
Definition ienumerable.hpp:32
@ 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
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
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ add
The Add key.
Definition console_key.hpp:170
Contains xtd::collections::generic::linked_list <type_t> class.
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_array class.
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38