xtd 1.0.0
Loading...
Searching...
No Matches
queue.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "icollection.hpp"
7#include "../../string.hpp"
8#include "../../usize.hpp"
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
46 template<typename type_t, typename container_t = std::deque<type_t>>
48 public:
50
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(queue& queue) = default;
83 queue(std::queue<type_t>&& queue) {
84 while (queue.size()) {
85 data_->items.push(queue.front());
86 queue.pop();
87 }
89 }
90
92 queue(const std::queue<type_t>& queue) {
93 struct accessor : public std::queue<type_t> {static auto get() {return &accessor::c;}};
94 const auto& underlying_items = queue.*accessor::get();
95 data_->items = base_type(underlying_items.begin(), underlying_items.end());
97 }
98
100 queue(const ienumerable<value_type>& collection) {
101 for (const auto& item : collection)
102 data_->items.push(item);
104 }
105
107 template<xtd::iterable iterable_t>
108 queue(iterable_t&& items) {
109 for (const auto& item : items)
110 data_->items.push(item);
112 }
113
116 data_->items.reserve(capacity);
117 }
118
120 queue(std::initializer_list<type_t> il) {
121 data_->items = base_type {il};
123 }
124
127 template < std::input_iterator input_iterator_t >
128 queue(input_iterator_t first, input_iterator_t last) {
129 for (auto iterator = first; iterator != last; ++iterator)
132 }
133
134
136
140 [[nodiscard]] auto capacity() const noexcept -> size_type {return data_->items.capacity();}
144 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.size();}
145
148 [[nodiscard]] auto items() const {return std::queue<type_t>(std::deque<type_t>(data_->items.begin(), data_->items.end()));}
150
152
158 auto clear() -> void override {
159 data_->items.clear();
160 ++data_->version;
161 }
162
166 [[nodiscard]] auto contains(const_reference value) const noexcept -> bool override {
167 for (const auto& item : data_->items)
168 if (xtd::collections::generic::helpers::equator<type_t> {}(item, value)) return true;
169 return false;
170 }
171
179 auto copy_to(xtd::array<type_t>& array, size_type array_index) const -> void override {
181 for (const auto& item : data_->items)
182 array[array_index++] = item;
183 }
184
188 auto dequeue() -> value_type {
189 auto result = value_type {};
191 return result;
192 }
193
196 auto enqueue(const_reference value) -> void {
197 data_->items.push(value);
199 ++data_->version;
200 }
201
204 auto enqueue(value_type&& value) -> void {
205 data_->items.push(std::move(value));
207 ++data_->version;
208 }
209
214 if (data_->items.capacity() < capacity) data_->items.reserve(capacity);
215 return data_->items.capacity();
216 }
217
220 [[nodiscard]] enumerator<value_type> get_enumerator() const noexcept override {
221 struct queue_enumerator : public ienumerator < value_type > {
222 explicit queue_enumerator(const queue & items, xtd::usize version) : items_(items), version_(version) {}
223
224 [[nodiscard]] const value_type& current() const override {
226 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
227 return *iterator_;
228 }
229
230 [[nodiscard]] bool move_next() override {
231 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
232 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
233 else iterator_ = items_.data_->items.cbegin();
234 return iterator_ != items_.data_->items.cend();
235 }
236
237 void reset() override {
238 index_ = 0;
239 version_ = items_.data_->version;
240 iterator_ = items_.data_->items.cend();
241 }
242
243 private:
244 size_type index_ = 0;
245 const queue& items_;
246 typename base_type::const_iterator iterator_ = items_.data_->items.cend();
247 size_type version_ = 0;
248 };
249 return {new_ptr < queue_enumerator > (self_, data_->version)};
250 }
251
256 [[nodiscard]] auto peek() const -> value_type {
257 auto result = value_type {};
259 return result;
260 }
261
264 [[nodiscard]] auto to_array() const -> xtd::array<value_type> {
266 copy_to(array, 0);
267 return array;
268 }
269
272 [[nodiscard]] auto to_string() const noexcept -> string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
273
278 auto trim_excess() -> void {
279 if (count() < static_cast<xtd::usize>(capacity() * 0.9)) trim_excess(count());
280 }
281
287 auto temp = base_type {};
288 temp.reserve(capacity);
289 for (auto& i : data_->items)
290 temp.push(i);
291 data_->items = std::move(temp);
292 ++data_->version;
293 }
294
298 [[nodiscard]] auto try_dequeue(value_type& result) noexcept -> bool {
299 if (!try_peek(result)) return false;
300 data_->items.pop();
301 ++data_->version;
302 return true;
303 }
304
308 [[nodiscard]] auto try_peek(value_type& result) const noexcept -> bool {
309 result = count() ? data_->items.front() : type_t {};
310 return count();
311 }
312
313
315
320 auto operator =(const queue& other) -> queue& = default;
324 auto operator =(queue&& other) noexcept -> queue& {
325 data_->items = std::move(other.data_->items);
326 ++data_->version;
327 return self_;
328 }
329
332 auto operator =(const std::initializer_list<type_t>& items) -> queue& {
333 data_->items = items;
334 ++data_->version;
335 return self_;
336 }
337
340 operator std::queue<type_t>() const {return items();}
342
343 private:
344 auto is_read_only() const noexcept -> bool override {return false;}
345 auto is_synchronized() const noexcept -> bool override {return false;}
346 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
347 auto add(const type_t& value) -> void override {enqueue(value);}
348 auto remove(const type_t&) -> bool override {return false;}
349
350 struct queue_data {
352 xtd::object sync_root;
353 xtd::usize version = 0;
354 };
355
357 };
358
360 // Deduction guides for xtd::collections::generic::queue
361 // {
362 template < class type_t, typename container_t>
364
365 template < class type_t, typename container_t>
367
368 template < class type_t, typename container_t>
370
371 template < class type_t>
373
374 template <xtd::iterable iterable_t>
376
377 template < class type_t>
378 queue(std::initializer_list<type_t>) -> queue<type_t>;
379
380 template<typename input_iterator_t>
381 queue(input_iterator_t, input_iterator_t) -> queue<std::iter_value_t<input_iterator_t>>;
382 // }
384 }
385 }
386}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:122
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1259
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
Defines methods to manipulate generic collections.
Definition icollection.hpp:45
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto is_synchronized() const noexcept -> bool=0
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
virtual auto count() const noexcept -> xtd::usize=0
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
typename xtd::collections::generic::ienumerable< xtd::any_object >::value_type value_type
Definition icollection.hpp:51
virtual auto is_read_only() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
typename xtd::collections::generic::extensions::enumerable_iterators< value_type, xtd::collections::generic::ienumerable< value_type > >::iterator iterator
Definition ienumerable.hpp:48
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:115
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:144
queue(std::initializer_list< type_t > il)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition queue.hpp:120
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:179
queue(const queue &queue)=default
Default copy constructor with specified queue.
auto items() const
Definition queue.hpp:148
auto peek() const -> value_type
Returns the object at the beginning of the xtd::collections::generic::queue <type_t> without removing...
Definition queue.hpp:256
auto to_array() const -> xtd::array< value_type >
Copies the xtd::collections::generic::queue <type_t> elements to a new array.
Definition queue.hpp:264
auto to_string() const noexcept -> string override
Returns a xtd::string that represents the current object.
Definition queue.hpp:272
queue(queue &queue)=default
Default copy constructor with specified queue.
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:166
auto clear() -> void override
Removes all elements from the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:158
queue(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition queue.hpp:128
auto enqueue(const_reference value) -> void
Adds an object to the end of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:196
auto enqueue(value_type &&value) -> void
Adds an object to the end of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:204
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:308
queue(std::queue< type_t > &&queue)
Move constructor with specified queue.
Definition queue.hpp:83
auto capacity() const noexcept -> size_type
Definition queue.hpp:140
typename icollection< value_type >::value_type 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:285
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:100
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:213
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:220
queue(const std::queue< type_t > &queue)
Default copy constructor with specified queue.
Definition queue.hpp:92
auto trim_excess() -> void
Sets the capacity to the actual number of elements in the xtd::collections::generic::queue <type_t>,...
Definition queue.hpp:278
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:298
auto dequeue() -> value_type
Removes and returns the object at the beginning of the xtd::collections::generic::queue <type_t>.
Definition queue.hpp:188
value_type & reference
Definition queue.hpp:59
xtd::collections::generic::helpers::raw_queue< value_type, std::deque< value_type > > base_type
Definition queue.hpp:55
queue(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition queue.hpp:108
xtd::usize size_type
Definition queue.hpp:57
const value_type & const_reference
Definition queue.hpp:61
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
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:40
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 auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ 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
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
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
@ 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
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:262
auto last() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:287
Contains xtd::collections::generic::helpers::raw_queue class.
Contains xtd::string alias.
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Contains xtd::usize type.