xtd 0.2.0
Loading...
Searching...
No Matches
raw_queue.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <queue>
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_queue final : public std::queue<type_t, container_t> {
44 public:
46
48 using base_type = std::queue<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;
56
58
63
69 ensure_capacity(capacity);
70 }
71
74
79
84 template<class input_iterator_t>
85 raw_queue(input_iterator_t first, input_iterator_t last) : base_type(first, last) {shrink_to_fit();}
86
88 template<class allocator_t>
89 explicit raw_queue(const allocator_t& alloc) : base_type(alloc) {shrink_to_fit();}
91
93
97 auto begin() const -> const_iterator {return base_type::c.cbegin();}
98
101 auto capacity() const noexcept -> size_type {return capacity_;}
102
105 auto cbegin() const -> const_iterator {return base_type::c.cbegin();}
106
109 auto cend() const -> const_iterator {return base_type::c.cend();}
110
113 auto end() const -> const_iterator {return base_type::c.cend();}
114
117 auto items() noexcept -> base_type& {return self_;}
120 auto items() const noexcept -> const base_type& {return self_;}
121
124 auto size() const noexcept -> size_type {return base_type::c.size();}
126
128
131 auto clear() -> void {base_type::c.clear();}
132
136 auto push(const value_type& value) -> void {
137 base_type::push(value);
138 ensure_capacity(base_type::c.size());
139 }
140
144 auto push(value_type&& value) -> void {
145 base_type::push(std::move(value));
146 ensure_capacity(base_type::c.size());
147 }
148
151 auto pop() -> void {
152 base_type::pop();
153 ensure_capacity(size());
154 }
155
159 auto reserve(size_type count) -> void {
160 if (capacity_ >= count) return;
161 ensure_capacity(count);
162 }
163
166 auto shrink_to_fit() -> void {
167 base_type::c.shrink_to_fit();
168 capacity_ = base_type::c.size();
169 }
170
171
173
175 raw_queue& operator=(const raw_queue& other) = default;
177 operator const base_type& () const noexcept {return self_;}
178 operator base_type& () noexcept {return self_;}
180
181 private:
182 auto ensure_capacity(size_type capacity) -> void {
183 if (capacity <= capacity_) return;
184 capacity_ = capacity;
185 auto original_size = base_type::size();
186 base_type::c.resize(capacity_);
187 base_type::c.resize(original_size); // Restore size while keeping capacity.
188 }
189
190 size_type capacity_ = base_type::size();
191 };
192
194 // Deduction guides for xtd::collections::generic::helpers::raw_queue
195 // {
196 template<class container_t>
197 raw_queue(container_t) -> raw_queue<typename container_t::value_type, container_t>;
198
199 template<class container_t, class allocator_t>
200 raw_queue(container_t, allocator_t) -> raw_queue<typename container_t::value_type, container_t>;
201
202 template< class input_iterator_t>
203 raw_queue(input_iterator_t, input_iterator_t) -> raw_queue<typename std::iterator_traits<input_iterator_t>::value_type>;
204
205 template< class input_iterator_t, class allocator_t>
206 raw_queue(input_iterator_t, input_iterator_t, allocator_t) -> raw_queue<typename std::iterator_traits<input_iterator_t>::value_type, std::deque<typename std::iterator_traits<input_iterator_t>::value_type, allocator_t>>;
207 // }
209 }
210 }
211 }
212}
auto clear() -> void
Removes all elements from the queue.
Definition raw_queue.hpp:131
auto capacity() const noexcept -> size_type
Definition raw_queue.hpp:101
typename base_type::size_type size_type
Unsigned integer type used for size and capacity.
Definition raw_queue.hpp:51
auto pop() -> void
Removes the element at the front of the queue.
Definition raw_queue.hpp:151
raw_queue & operator=(raw_queue &&other)=default
Move assignment.
auto cend() const -> const_iterator
Gets a const iterator to the end of the queue.
Definition raw_queue.hpp:109
auto shrink_to_fit() -> void
Reduces capacity to fit the current size.
Definition raw_queue.hpp:166
raw_queue & operator=(const raw_queue &other)=default
Copy assignment.
auto push(const value_type &value) -> void
Adds a copy of the element at the back of the queue.
Definition raw_queue.hpp:136
auto size() const noexcept -> size_type
Gets the number of elements in the queue.
Definition raw_queue.hpp:124
auto items() const noexcept -> const base_type &
Access to the underlying base queue.
Definition raw_queue.hpp:120
std::queue< type_t, container_t > base_type
The base STL queue type.
Definition raw_queue.hpp:48
raw_queue()
Default constructor. Initializes an empty queue.
Definition raw_queue.hpp:62
auto end() const -> const_iterator
Gets a const iterator to the end of the queue.
Definition raw_queue.hpp:113
auto push(value_type &&value) -> void
Moves the element into the back of the queue.
Definition raw_queue.hpp:144
raw_queue(size_type capacity)
Constructs an empty queue with reserved capacity.
Definition raw_queue.hpp:67
typename base_type::container_type container_type
The underlying container type.
Definition raw_queue.hpp:49
auto items() noexcept -> base_type &
Access to the underlying base queue.
Definition raw_queue.hpp:117
raw_queue(input_iterator_t first, input_iterator_t last)
Constructs a queue from a range of iterators.
Definition raw_queue.hpp:85
auto cbegin() const -> const_iterator
Gets a const iterator to the beginning of the queue.
Definition raw_queue.hpp:105
auto reserve(size_type count) -> void
Reserves storage to hold at least count elements.
Definition raw_queue.hpp:159
raw_queue(const raw_queue &other)
Copy constructor.
Definition raw_queue.hpp:73
typename base_type::reference reference
Reference to element type.
Definition raw_queue.hpp:52
raw_queue(const allocator_t &alloc)
Constructs a queue with a specific allocator.
Definition raw_queue.hpp:89
raw_queue(raw_queue &&other)
Move constructor.
Definition raw_queue.hpp:78
typename container_type::const_iterator const_iterator
Const iterator type.
Definition raw_queue.hpp:54
typename base_type::value_type value_type
Type of elements stored.
Definition raw_queue.hpp:50
auto begin() const -> const_iterator
Gets a const iterator to the beginning of the queue.
Definition raw_queue.hpp:97
typename base_type::const_reference const_reference
Const reference to element type.
Definition raw_queue.hpp:53
#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.