xtd 0.2.0
Loading...
Searching...
No Matches
queue.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 queue : 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 queue() = default;
72 data_ = std::move(queue.data_);
73 queue.data_ = new_ptr<queue_data>();
74 }
75
77 queue(const queue& queue) = default;
80 queue(std::queue<type_t>&& queue) {
81 while (queue.size()) {
82 data_->items.push(queue.front());
83 queue.pop();
84 }
86 }
87
89 queue(const std::queue<type_t>& queue) {
90 struct std_queue : public std::queue<type_t> {
91 std_queue(const std::queue<type_t>& queue) : ptr {reinterpret_cast<const std_queue*>(&queue)} {}
92 auto begin() const {return ptr->c.begin();}
93 auto end() const {return ptr->c.end();}
94 const std_queue* ptr;
95 };
96 auto items = std_queue {queue};
97 data_->items = base_type(items.begin(), items.end());
99 }
100
102 queue(const ienumerable<value_type>& collection) {
103 for (auto item : collection)
104 data_->items.push(item);
106 }
107
110 data_->items.reserve(capacity);
111 }
112
114 queue(std::initializer_list<type_t> il) {
115 data_->items = base_type {il};
117 }
118
121 template < std::input_iterator input_iterator_t >
122 queue(input_iterator_t first, input_iterator_t last) {
123 for (auto iterator = first; iterator != last; ++iterator)
124 enqueue(*iterator);
126 }
127
128
130
134 auto capacity() const noexcept -> size_type {return data_->items.capacity();}
138 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.size();}
139
142 auto items() const {return std::queue<type_t>(std::deque<type_t>(data_->items.begin(), data_->items.end()));}
144
146
152 auto clear() -> void override {
153 data_->items.clear();
154 ++data_->version;
155 }
156
160 auto contains(const_reference value) const noexcept -> bool override {
161 for (const auto& item : data_->items)
162 if (xtd::collections::generic::helpers::equator<type_t> {}(item, value)) return true;
163 return false;
164 }
165
173 auto copy_to(xtd::array<type_t>& array, size_type array_index) const -> void override {
175 for (const auto& item : data_->items)
176 array[array_index++] = item;
177 }
178
182 auto dequeue() -> value_type {
183 auto result = value_type {};
185 return result;
186 }
187
190 auto enqueue(const_reference value) -> void {
191 data_->items.push(value);
193 ++data_->version;
194 }
195
198 auto enqueue(value_type&& value) -> void {
199 data_->items.push(std::move(value));
201 ++data_->version;
202 }
203
208 if (data_->items.capacity() < capacity) data_->items.reserve(capacity);
209 return data_->items.capacity();
210 }
211
214 enumerator<value_type> get_enumerator() const noexcept override {
215 struct queue_enumerator : public ienumerator < value_type > {
216 explicit queue_enumerator(const queue & items, xtd::size version) : items_(items), version_(version) {}
217
218 const value_type& current() const override {
220 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
221 return *iterator_;
222 }
223
224 bool move_next() override {
225 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
226 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
227 else iterator_ = items_.data_->items.cbegin();
228 return iterator_ != items_.data_->items.cend();
229 }
230
231 void reset() override {
232 index_ = 0;
233 version_ = items_.data_->version;
234 iterator_ = items_.data_->items.cend();
235 }
236
237 private:
238 size_type index_ = 0;
239 const queue& items_;
240 typename base_type::const_iterator iterator_ = items_.data_->items.cend();
241 size_type version_ = 0;
242 };
243 return {new_ptr < queue_enumerator > (self_, data_->version)};
244 }
245
250 [[nodiscard]] auto peek() const -> value_type {
251 auto result = value_type {};
253 return result;
254 }
255
258 auto to_array() const -> xtd::array<value_type> {
260 copy_to(array, 0);
261 return array;
262 }
263
266 auto to_string() const noexcept -> string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
267
272 auto trim_excess() -> void {
273 if (count() < static_cast<xtd::size>(capacity() * 0.9)) trim_excess(count());
274 }
275
281 auto temp = base_type {};
282 temp.reserve(capacity);
283 for (auto& i : data_->items)
284 temp.push(i);
285 data_->items = std::move(temp);
286 ++data_->version;
287 }
288
292 [[nodiscard]] auto try_dequeue(value_type& result) noexcept -> bool {
293 if (!try_peek(result)) return false;
294 data_->items.pop();
295 ++data_->version;
296 return true;
297 }
298
302 [[nodiscard]] auto try_peek(value_type& result) const noexcept -> bool {
303 result = count() ? data_->items.front() : type_t {};
304 return count();
305 }
306
307
309
314 auto operator =(const queue& other) -> queue& = default;
318 auto operator =(queue&& other) noexcept -> queue& {
319 data_->items = std::move(other.data_->items);
320 ++data_->version;
321 return self_;
322 }
323
326 auto operator =(const std::initializer_list<type_t>& items) -> queue& {
327 data_->items = items;
328 ++data_->version;
329 return self_;
330 }
331
334 operator std::queue<type_t>() const {return items();}
336
337 private:
338 auto is_read_only() const noexcept -> bool override {return false;}
339 auto is_synchronized() const noexcept -> bool override {return false;}
340 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
341 auto add(const type_t& value) -> void override {enqueue(value);}
342 auto remove(const type_t&) -> bool override {return false;}
343
344 struct queue_data {
346 xtd::object sync_root;
347 xtd::size version = 0;
348 };
349
351 };
352
354 // Deduction guides for xtd::collections::generic::queue
355 // {
356 template < class type_t, class container_t>
358
359 template < class type_t, class container_t>
361
362 template < class type_t>
364
365 template < class type_t>
366 queue(std::initializer_list<type_t>) -> queue<type_t>;
367
368 template <class input_iterator_t>
369 queue(input_iterator_t, input_iterator_t) -> queue<std::iter_value_t<input_iterator_t>>;
370 // }
372 }
373 }
374}
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 FIFO (first-in, first-out) queue.
Definition raw_queue.hpp:43
typename container_type::const_iterator const_iterator
Const iterator type.
Definition raw_queue.hpp:54
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 first-in, first-out collection of objects.
Definition queue.hpp:47
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:109
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:138
queue(std::initializer_list< type_t > il)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition queue.hpp:114
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:173
queue(const queue &queue)=default
Default copy constructor with specified queue.
auto items() const
Definition queue.hpp:142
auto peek() const -> value_type
Returns the object at the beginning of the xtd::collections::generic::queue <type_t> without removing...
Definition queue.hpp:250
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition queue.hpp:57
auto to_array() const -> xtd::array< value_type >
Copies the xtd::collections::generic::queue <type_t> elements to a new array.
Definition queue.hpp:258
auto to_string() const noexcept -> string override
Returns a xtd::string that represents the current object.
Definition queue.hpp:266
auto operator=(const queue &other) -> queue &=default
Copy assignment operator. Replaces the contents with a copy of the contents of other.
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:160
auto clear() -> void override
Removes all elements from the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:152
queue(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition queue.hpp:122
auto enqueue(const_reference value) -> void
Adds an object to the end of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:190
auto enqueue(value_type &&value) -> void
Adds an object to the end of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:198
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:302
queue(std::queue< type_t > &&queue)
Move constructor with specified queue.
Definition queue.hpp:80
auto capacity() const noexcept -> size_type
Definition queue.hpp:134
typename icollection< type_t >::value_type value_type
Represents the list value type.
Definition queue.hpp:53
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:279
queue()=default
Initializes a new instance of the xtd::collections::generic::queue <type_t> class that is empty and h...
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:102
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:207
queue(queue &&queue)
Move constructor with specified queue.
Definition queue.hpp:71
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:214
queue(const std::queue< type_t > &queue)
Default copy constructor with specified queue.
Definition queue.hpp:89
auto trim_excess() -> void
Sets the capacity to the actual number of elements in the xtd::collections::generic::queue <type_t>,...
Definition queue.hpp:272
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:292
auto dequeue() -> value_type
Removes and returns the object at the beginning of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:182
value_type & reference
Represents the reference of list value type.
Definition queue.hpp:59
xtd::collections::generic::helpers::raw_queue< value_type, container_t > base_type
Represents the list base type.
Definition queue.hpp:55
const value_type & const_reference
Represents the const reference of list value type.
Definition queue.hpp:61
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::queue< xtd::any_object > queue
Represents a first-in, first-out collection of objects.
Definition queue.hpp:27
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_queue 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.