]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Queue implementation -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2001-2024 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
f910786b | 51 | /** @file bits/stl_queue.h |
729e3d3f | 52 | * This is an internal header file, included by other library headers. |
f910786b | 53 | * Do not attempt to use it directly. @headername{queue} |
725dc051 BK |
54 | */ |
55 | ||
046d30f4 PC |
56 | #ifndef _STL_QUEUE_H |
57 | #define _STL_QUEUE_H 1 | |
725dc051 | 58 | |
30a20a1e | 59 | #include <bits/concept_check.h> |
285b36d6 | 60 | #include <debug/debug.h> |
7666d649 JW |
61 | #if __cplusplus >= 201103L |
62 | # include <bits/uses_allocator.h> | |
63 | #endif | |
725dc051 | 64 | |
12ffa228 BK |
65 | namespace std _GLIBCXX_VISIBILITY(default) |
66 | { | |
67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 68 | |
4df6abc6 | 69 | /** |
3971a4d2 | 70 | * @brief A standard container giving FIFO behavior. |
4df6abc6 | 71 | * |
aac2878e | 72 | * @ingroup sequences |
4df6abc6 | 73 | * |
d632488a BK |
74 | * @tparam _Tp Type of element. |
75 | * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>. | |
76 | * | |
3971a4d2 PE |
77 | * Meets many of the requirements of a |
78 | * <a href="tables.html#65">container</a>, | |
79 | * but does not define anything to do with iterators. Very few of the | |
80 | * other standard container interfaces are defined. | |
4df6abc6 | 81 | * |
3971a4d2 PE |
82 | * This is not a true container, but an @e adaptor. It holds another |
83 | * container, and provides a wrapper interface to that container. The | |
84 | * wrapper is what enforces strict first-in-first-out %queue behavior. | |
4df6abc6 | 85 | * |
3971a4d2 PE |
86 | * The second template parameter defines the type of the underlying |
87 | * sequence/container. It defaults to std::deque, but it can be any type | |
88 | * that supports @c front, @c back, @c push_back, and @c pop_front, | |
89 | * such as std::list or an appropriate user-defined type. | |
90 | * | |
2a60a9f6 | 91 | * Members not found in @a normal containers are @c container_type, |
3971a4d2 PE |
92 | * which is a typedef for the second Sequence parameter, and @c push and |
93 | * @c pop, which are standard %queue/FIFO operations. | |
4df6abc6 | 94 | */ |
7ffb61d5 | 95 | template<typename _Tp, typename _Sequence = deque<_Tp> > |
3971a4d2 | 96 | class queue |
285b36d6 | 97 | { |
fe62dd04 | 98 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
285b36d6 BK |
99 | // concept requirements |
100 | typedef typename _Sequence::value_type _Sequence_value_type; | |
fe62dd04 | 101 | # if __cplusplus < 201103L |
285b36d6 | 102 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
fe62dd04 | 103 | # endif |
285b36d6 BK |
104 | __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept) |
105 | __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) | |
106 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) | |
fe62dd04 | 107 | #endif |
ed6814f7 | 108 | |
7c920151 | 109 | template<typename _Tp1, typename _Seq1> |
fe62dd04 FD |
110 | friend bool |
111 | operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); | |
285b36d6 BK |
112 | |
113 | template<typename _Tp1, typename _Seq1> | |
fe62dd04 FD |
114 | friend bool |
115 | operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); | |
ed6814f7 | 116 | |
717e91db JW |
117 | #if __cpp_lib_three_way_comparison |
118 | template<typename _Tp1, three_way_comparable _Seq1> | |
119 | friend compare_three_way_result_t<_Seq1> | |
120 | operator<=>(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); | |
121 | #endif | |
122 | ||
997ed914 JW |
123 | #if __cplusplus >= 201103L |
124 | template<typename _Alloc> | |
125 | using _Uses = typename | |
126 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; | |
1138a19d JW |
127 | |
128 | #if __cplusplus >= 201703L | |
129 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
130 | // 2566. Requirements on the first template parameter of container | |
131 | // adaptors | |
132 | static_assert(is_same<_Tp, typename _Sequence::value_type>::value, | |
133 | "value_type must be the same as the underlying container"); | |
134 | #endif // C++17 | |
135 | #endif // C++11 | |
997ed914 | 136 | |
285b36d6 | 137 | public: |
fe62dd04 FD |
138 | typedef typename _Sequence::value_type value_type; |
139 | typedef typename _Sequence::reference reference; | |
140 | typedef typename _Sequence::const_reference const_reference; | |
141 | typedef typename _Sequence::size_type size_type; | |
142 | typedef _Sequence container_type; | |
ed6814f7 | 143 | |
285b36d6 | 144 | protected: |
d2e1d4b7 JW |
145 | /* Maintainers wondering why this isn't uglified as per style |
146 | * guidelines should note that this name is specified in the standard, | |
147 | * C++98 [23.2.3.1]. | |
148 | * (Why? Presumably for the same reason that it's protected instead | |
285b36d6 BK |
149 | * of private: to allow derivation. But none of the other |
150 | * containers allow for derivation. Odd.) | |
151 | */ | |
a1f009a6 | 152 | /// @c c is the underlying container. |
285b36d6 | 153 | _Sequence c; |
ed6814f7 | 154 | |
285b36d6 BK |
155 | public: |
156 | /** | |
157 | * @brief Default constructor creates no elements. | |
158 | */ | |
734f5023 | 159 | #if __cplusplus < 201103L |
285b36d6 | 160 | explicit |
7aa1cb97 PC |
161 | queue(const _Sequence& __c = _Sequence()) |
162 | : c(__c) { } | |
163 | #else | |
a1f009a6 JW |
164 | template<typename _Seq = _Sequence, typename _Requires = typename |
165 | enable_if<is_default_constructible<_Seq>::value>::type> | |
166 | queue() | |
167 | : c() { } | |
d2e1d4b7 | 168 | |
7aa1cb97 PC |
169 | explicit |
170 | queue(const _Sequence& __c) | |
171 | : c(__c) { } | |
172 | ||
173 | explicit | |
d2e1d4b7 | 174 | queue(_Sequence&& __c) |
7aa1cb97 | 175 | : c(std::move(__c)) { } |
997ed914 JW |
176 | |
177 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
178 | explicit | |
179 | queue(const _Alloc& __a) | |
180 | : c(__a) { } | |
181 | ||
182 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
183 | queue(const _Sequence& __c, const _Alloc& __a) | |
184 | : c(__c, __a) { } | |
185 | ||
186 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
187 | queue(_Sequence&& __c, const _Alloc& __a) | |
188 | : c(std::move(__c), __a) { } | |
189 | ||
190 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
191 | queue(const queue& __q, const _Alloc& __a) | |
192 | : c(__q.c, __a) { } | |
193 | ||
194 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
195 | queue(queue&& __q, const _Alloc& __a) | |
196 | : c(std::move(__q.c), __a) { } | |
083b7f28 | 197 | #endif |
b7e8fb5e | 198 | |
7ffa63df | 199 | #ifdef __glibcxx_adaptor_iterator_pair_constructor // C++ >= 23 && HOSTED |
b7e8fb5e JW |
200 | template<typename _InputIterator, |
201 | typename = _RequireInputIter<_InputIterator>> | |
202 | queue(_InputIterator __first, _InputIterator __last) | |
203 | : c(__first, __last) { } | |
204 | ||
205 | template<typename _InputIterator, typename _Alloc, | |
206 | typename = _RequireInputIter<_InputIterator>, | |
207 | typename = _Uses<_Alloc>> | |
208 | queue(_InputIterator __first, _InputIterator __last, const _Alloc& __a) | |
209 | : c(__first, __last, __a) { } | |
7aa1cb97 | 210 | #endif |
ed6814f7 | 211 | |
285b36d6 BK |
212 | /** |
213 | * Returns true if the %queue is empty. | |
214 | */ | |
d715f554 | 215 | _GLIBCXX_NODISCARD bool |
7c920151 PC |
216 | empty() const |
217 | { return c.empty(); } | |
ed6814f7 | 218 | |
285b36d6 | 219 | /** Returns the number of elements in the %queue. */ |
0d04fe49 | 220 | _GLIBCXX_NODISCARD |
285b36d6 | 221 | size_type |
7c920151 PC |
222 | size() const |
223 | { return c.size(); } | |
ed6814f7 | 224 | |
285b36d6 BK |
225 | /** |
226 | * Returns a read/write reference to the data at the first | |
227 | * element of the %queue. | |
228 | */ | |
0d04fe49 | 229 | _GLIBCXX_NODISCARD |
285b36d6 | 230 | reference |
ed6814f7 BI |
231 | front() |
232 | { | |
285b36d6 | 233 | __glibcxx_requires_nonempty(); |
ed6814f7 | 234 | return c.front(); |
285b36d6 | 235 | } |
ed6814f7 | 236 | |
285b36d6 BK |
237 | /** |
238 | * Returns a read-only (constant) reference to the data at the first | |
239 | * element of the %queue. | |
240 | */ | |
0d04fe49 | 241 | _GLIBCXX_NODISCARD |
285b36d6 | 242 | const_reference |
ed6814f7 BI |
243 | front() const |
244 | { | |
285b36d6 | 245 | __glibcxx_requires_nonempty(); |
ed6814f7 | 246 | return c.front(); |
285b36d6 | 247 | } |
ed6814f7 | 248 | |
285b36d6 BK |
249 | /** |
250 | * Returns a read/write reference to the data at the last | |
251 | * element of the %queue. | |
252 | */ | |
0d04fe49 | 253 | _GLIBCXX_NODISCARD |
285b36d6 | 254 | reference |
ed6814f7 | 255 | back() |
285b36d6 BK |
256 | { |
257 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 258 | return c.back(); |
285b36d6 | 259 | } |
ed6814f7 | 260 | |
285b36d6 BK |
261 | /** |
262 | * Returns a read-only (constant) reference to the data at the last | |
263 | * element of the %queue. | |
264 | */ | |
0d04fe49 | 265 | _GLIBCXX_NODISCARD |
285b36d6 | 266 | const_reference |
ed6814f7 | 267 | back() const |
285b36d6 BK |
268 | { |
269 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 270 | return c.back(); |
285b36d6 | 271 | } |
ed6814f7 | 272 | |
285b36d6 BK |
273 | /** |
274 | * @brief Add data to the end of the %queue. | |
93c66bc6 | 275 | * @param __x Data to be added. |
285b36d6 BK |
276 | * |
277 | * This is a typical %queue operation. The function creates an | |
278 | * element at the end of the %queue and assigns the given data | |
279 | * to it. The time complexity of the operation depends on the | |
280 | * underlying sequence. | |
281 | */ | |
282 | void | |
7c920151 PC |
283 | push(const value_type& __x) |
284 | { c.push_back(__x); } | |
4dc3e453 | 285 | |
734f5023 | 286 | #if __cplusplus >= 201103L |
4dc3e453 PC |
287 | void |
288 | push(value_type&& __x) | |
289 | { c.push_back(std::move(__x)); } | |
290 | ||
594ef205 JW |
291 | #if __cplusplus > 201402L |
292 | template<typename... _Args> | |
293 | decltype(auto) | |
294 | emplace(_Args&&... __args) | |
295 | { return c.emplace_back(std::forward<_Args>(__args)...); } | |
296 | #else | |
c54171fe | 297 | template<typename... _Args> |
fe62dd04 FD |
298 | void |
299 | emplace(_Args&&... __args) | |
4dc3e453 | 300 | { c.emplace_back(std::forward<_Args>(__args)...); } |
594ef205 | 301 | #endif |
7aa1cb97 PC |
302 | #endif |
303 | ||
285b36d6 BK |
304 | /** |
305 | * @brief Removes first element. | |
306 | * | |
307 | * This is a typical %queue operation. It shrinks the %queue by one. | |
308 | * The time complexity of the operation depends on the underlying | |
309 | * sequence. | |
310 | * | |
311 | * Note that no data is returned, and if the first element's | |
312 | * data is needed, it should be retrieved before pop() is | |
313 | * called. | |
314 | */ | |
315 | void | |
ed6814f7 BI |
316 | pop() |
317 | { | |
285b36d6 | 318 | __glibcxx_requires_nonempty(); |
ed6814f7 | 319 | c.pop_front(); |
285b36d6 | 320 | } |
ed6814f7 | 321 | |
734f5023 | 322 | #if __cplusplus >= 201103L |
7aa1cb97 | 323 | void |
ff74fd13 | 324 | swap(queue& __q) |
6b9539e2 DK |
325 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
326 | noexcept(__is_nothrow_swappable<_Sequence>::value) | |
327 | #else | |
ddb63209 | 328 | noexcept(__is_nothrow_swappable<_Tp>::value) |
6b9539e2 | 329 | #endif |
999209d0 PC |
330 | { |
331 | using std::swap; | |
332 | swap(c, __q.c); | |
333 | } | |
6b9539e2 | 334 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 335 | }; |
ed6814f7 | 336 | |
5bb89e0a JW |
337 | #if __cpp_deduction_guides >= 201606 |
338 | template<typename _Container, | |
08abbdda | 339 | typename = _RequireNotAllocator<_Container>> |
5bb89e0a JW |
340 | queue(_Container) -> queue<typename _Container::value_type, _Container>; |
341 | ||
342 | template<typename _Container, typename _Allocator, | |
22d34a2a | 343 | typename = _RequireNotAllocator<_Container>> |
5bb89e0a JW |
344 | queue(_Container, _Allocator) |
345 | -> queue<typename _Container::value_type, _Container>; | |
b7e8fb5e | 346 | |
7ffa63df | 347 | #ifdef __glibcxx_adaptor_iterator_pair_constructor |
b7e8fb5e JW |
348 | template<typename _InputIterator, |
349 | typename _ValT | |
350 | = typename iterator_traits<_InputIterator>::value_type, | |
351 | typename = _RequireInputIter<_InputIterator>> | |
352 | queue(_InputIterator, _InputIterator) -> queue<_ValT>; | |
353 | ||
354 | template<typename _InputIterator, typename _Allocator, | |
355 | typename _ValT | |
356 | = typename iterator_traits<_InputIterator>::value_type, | |
357 | typename = _RequireInputIter<_InputIterator>, | |
358 | typename = _RequireAllocator<_Allocator>> | |
359 | queue(_InputIterator, _InputIterator, _Allocator) | |
360 | -> queue<_ValT, deque<_ValT, _Allocator>>; | |
361 | #endif | |
5bb89e0a JW |
362 | #endif |
363 | ||
4df6abc6 | 364 | /** |
3971a4d2 | 365 | * @brief Queue equality comparison. |
93c66bc6 BK |
366 | * @param __x A %queue. |
367 | * @param __y A %queue of the same type as @a __x. | |
3971a4d2 PE |
368 | * @return True iff the size and elements of the queues are equal. |
369 | * | |
370 | * This is an equivalence relation. Complexity and semantics depend on the | |
371 | * underlying sequence type, but the expected rules are: this relation is | |
372 | * linear in the size of the sequences, and queues are considered equivalent | |
373 | * if their sequences compare equal. | |
4df6abc6 | 374 | */ |
d508327c | 375 | template<typename _Tp, typename _Seq> |
0d04fe49 | 376 | _GLIBCXX_NODISCARD |
ed6814f7 | 377 | inline bool |
d508327c | 378 | operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 379 | { return __x.c == __y.c; } |
ed6814f7 | 380 | |
4df6abc6 | 381 | /** |
3971a4d2 | 382 | * @brief Queue ordering relation. |
93c66bc6 BK |
383 | * @param __x A %queue. |
384 | * @param __y A %queue of the same type as @a x. | |
385 | * @return True iff @a __x is lexicographically less than @a __y. | |
4df6abc6 | 386 | * |
285b36d6 BK |
387 | * This is an total ordering relation. Complexity and semantics |
388 | * depend on the underlying sequence type, but the expected rules | |
389 | * are: this relation is linear in the size of the sequences, the | |
390 | * elements must be comparable with @c <, and | |
391 | * std::lexicographical_compare() is usually used to make the | |
3971a4d2 | 392 | * determination. |
4df6abc6 | 393 | */ |
d508327c | 394 | template<typename _Tp, typename _Seq> |
0d04fe49 | 395 | _GLIBCXX_NODISCARD |
3971a4d2 | 396 | inline bool |
d508327c | 397 | operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 398 | { return __x.c < __y.c; } |
ed6814f7 | 399 | |
3971a4d2 | 400 | /// Based on operator== |
d508327c | 401 | template<typename _Tp, typename _Seq> |
0d04fe49 | 402 | _GLIBCXX_NODISCARD |
3971a4d2 | 403 | inline bool |
d508327c | 404 | operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 405 | { return !(__x == __y); } |
ed6814f7 | 406 | |
3971a4d2 | 407 | /// Based on operator< |
d508327c | 408 | template<typename _Tp, typename _Seq> |
0d04fe49 | 409 | _GLIBCXX_NODISCARD |
ed6814f7 | 410 | inline bool |
d508327c | 411 | operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 412 | { return __y < __x; } |
ed6814f7 | 413 | |
3971a4d2 | 414 | /// Based on operator< |
d508327c | 415 | template<typename _Tp, typename _Seq> |
0d04fe49 | 416 | _GLIBCXX_NODISCARD |
ed6814f7 | 417 | inline bool |
d508327c | 418 | operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 419 | { return !(__y < __x); } |
ed6814f7 | 420 | |
3971a4d2 | 421 | /// Based on operator< |
d508327c | 422 | template<typename _Tp, typename _Seq> |
0d04fe49 | 423 | _GLIBCXX_NODISCARD |
ed6814f7 | 424 | inline bool |
d508327c | 425 | operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 426 | { return !(__x < __y); } |
ed6814f7 | 427 | |
717e91db JW |
428 | #if __cpp_lib_three_way_comparison |
429 | template<typename _Tp, three_way_comparable _Seq> | |
0d04fe49 | 430 | [[nodiscard]] |
717e91db JW |
431 | inline compare_three_way_result_t<_Seq> |
432 | operator<=>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) | |
433 | { return __x.c <=> __y.c; } | |
434 | #endif | |
435 | ||
734f5023 | 436 | #if __cplusplus >= 201103L |
7aa1cb97 | 437 | template<typename _Tp, typename _Seq> |
6b9539e2 DK |
438 | inline |
439 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
440 | // Constrained free swap overload, see p0185r1 | |
441 | typename enable_if<__is_swappable<_Seq>::value>::type | |
442 | #else | |
443 | void | |
444 | #endif | |
7aa1cb97 | 445 | swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y) |
c688bbdd | 446 | noexcept(noexcept(__x.swap(__y))) |
7aa1cb97 | 447 | { __x.swap(__y); } |
aa2b7414 PC |
448 | |
449 | template<typename _Tp, typename _Seq, typename _Alloc> | |
450 | struct uses_allocator<queue<_Tp, _Seq>, _Alloc> | |
451 | : public uses_allocator<_Seq, _Alloc>::type { }; | |
6b9539e2 | 452 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 453 | |
4df6abc6 | 454 | /** |
3971a4d2 | 455 | * @brief A standard container automatically sorting its contents. |
4df6abc6 | 456 | * |
aac2878e | 457 | * @ingroup sequences |
4df6abc6 | 458 | * |
d632488a BK |
459 | * @tparam _Tp Type of element. |
460 | * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>. | |
fe62dd04 | 461 | * @tparam _Compare Comparison function object type, defaults to |
d632488a BK |
462 | * less<_Sequence::value_type>. |
463 | * | |
285b36d6 BK |
464 | * This is not a true container, but an @e adaptor. It holds |
465 | * another container, and provides a wrapper interface to that | |
fe62dd04 | 466 | * container. The wrapper is what enforces priority-based sorting |
3a498393 PC |
467 | * and %queue behavior. Very few of the standard container/sequence |
468 | * interface requirements are met (e.g., iterators). | |
3971a4d2 PE |
469 | * |
470 | * The second template parameter defines the type of the underlying | |
285b36d6 BK |
471 | * sequence/container. It defaults to std::vector, but it can be |
472 | * any type that supports @c front(), @c push_back, @c pop_back, | |
473 | * and random-access iterators, such as std::deque or an | |
474 | * appropriate user-defined type. | |
3971a4d2 | 475 | * |
285b36d6 BK |
476 | * The third template parameter supplies the means of making |
477 | * priority comparisons. It defaults to @c less<value_type> but | |
478 | * can be anything defining a strict weak ordering. | |
3971a4d2 | 479 | * |
2a60a9f6 | 480 | * Members not found in @a normal containers are @c container_type, |
285b36d6 | 481 | * which is a typedef for the second Sequence parameter, and @c |
3a498393 | 482 | * push, @c pop, and @c top, which are standard %queue operations. |
3971a4d2 | 483 | * |
285b36d6 BK |
484 | * @note No equality/comparison operators are provided for |
485 | * %priority_queue. | |
3971a4d2 | 486 | * |
285b36d6 BK |
487 | * @note Sorting of the elements takes place as they are added to, |
488 | * and removed from, the %priority_queue using the | |
489 | * %priority_queue's member functions. If you access the elements | |
490 | * by other means, and change their data such that the sorting | |
491 | * order would be different, the %priority_queue will not re-sort | |
492 | * the elements for you. (How could it know to do so?) | |
4df6abc6 | 493 | */ |
285b36d6 | 494 | template<typename _Tp, typename _Sequence = vector<_Tp>, |
7c920151 | 495 | typename _Compare = less<typename _Sequence::value_type> > |
3971a4d2 | 496 | class priority_queue |
3971a4d2 | 497 | { |
fe62dd04 | 498 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
285b36d6 BK |
499 | // concept requirements |
500 | typedef typename _Sequence::value_type _Sequence_value_type; | |
fe62dd04 | 501 | # if __cplusplus < 201103L |
285b36d6 | 502 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
fe62dd04 | 503 | # endif |
285b36d6 BK |
504 | __glibcxx_class_requires(_Sequence, _SequenceConcept) |
505 | __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) | |
506 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) | |
d508327c PC |
507 | __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, |
508 | _BinaryFunctionConcept) | |
fe62dd04 | 509 | #endif |
ed6814f7 | 510 | |
997ed914 JW |
511 | #if __cplusplus >= 201103L |
512 | template<typename _Alloc> | |
513 | using _Uses = typename | |
514 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; | |
1138a19d JW |
515 | |
516 | #if __cplusplus >= 201703L | |
517 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
518 | // 2566. Requirements on the first template parameter of container | |
519 | // adaptors | |
520 | static_assert(is_same<_Tp, typename _Sequence::value_type>::value, | |
521 | "value_type must be the same as the underlying container"); | |
522 | #endif // C++17 | |
523 | #endif // C++11 | |
997ed914 | 524 | |
7c920151 | 525 | public: |
fe62dd04 | 526 | typedef typename _Sequence::value_type value_type; |
1138a19d JW |
527 | typedef typename _Sequence::reference reference; |
528 | typedef typename _Sequence::const_reference const_reference; | |
529 | typedef typename _Sequence::size_type size_type; | |
530 | typedef _Sequence container_type; | |
8be062c6 JW |
531 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
532 | // DR 2684. priority_queue lacking comparator typedef | |
1138a19d | 533 | typedef _Compare value_compare; |
ed6814f7 | 534 | |
285b36d6 BK |
535 | protected: |
536 | // See queue::c for notes on these names. | |
a1f009a6 | 537 | _Sequence c; |
285b36d6 | 538 | _Compare comp; |
ed6814f7 | 539 | |
285b36d6 BK |
540 | public: |
541 | /** | |
542 | * @brief Default constructor creates no elements. | |
543 | */ | |
734f5023 | 544 | #if __cplusplus < 201103L |
285b36d6 | 545 | explicit |
ed6814f7 BI |
546 | priority_queue(const _Compare& __x = _Compare(), |
547 | const _Sequence& __s = _Sequence()) | |
548 | : c(__s), comp(__x) | |
285b36d6 | 549 | { std::make_heap(c.begin(), c.end(), comp); } |
7aa1cb97 | 550 | #else |
a1f009a6 JW |
551 | template<typename _Seq = _Sequence, typename _Requires = typename |
552 | enable_if<__and_<is_default_constructible<_Compare>, | |
fe62dd04 | 553 | is_default_constructible<_Seq>>::value>::type> |
a1f009a6 JW |
554 | priority_queue() |
555 | : c(), comp() { } | |
d2e1d4b7 | 556 | |
7aa1cb97 | 557 | explicit |
a1f009a6 | 558 | priority_queue(const _Compare& __x, const _Sequence& __s) |
7aa1cb97 PC |
559 | : c(__s), comp(__x) |
560 | { std::make_heap(c.begin(), c.end(), comp); } | |
561 | ||
562 | explicit | |
a1f009a6 | 563 | priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence()) |
7aa1cb97 PC |
564 | : c(std::move(__s)), comp(__x) |
565 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
566 | |
567 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
568 | explicit | |
569 | priority_queue(const _Alloc& __a) | |
92805612 | 570 | : c(__a), comp() { } |
997ed914 JW |
571 | |
572 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
573 | priority_queue(const _Compare& __x, const _Alloc& __a) | |
92805612 | 574 | : c(__a), comp(__x) { } |
997ed914 | 575 | |
1f4dcbf7 JW |
576 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
577 | // 2537. Constructors [...] taking allocators should call make_heap | |
997ed914 JW |
578 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
579 | priority_queue(const _Compare& __x, const _Sequence& __c, | |
580 | const _Alloc& __a) | |
1f4dcbf7 JW |
581 | : c(__c, __a), comp(__x) |
582 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
583 | |
584 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
585 | priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a) | |
1f4dcbf7 JW |
586 | : c(std::move(__c), __a), comp(__x) |
587 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
588 | |
589 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
590 | priority_queue(const priority_queue& __q, const _Alloc& __a) | |
92805612 | 591 | : c(__q.c, __a), comp(__q.comp) { } |
997ed914 JW |
592 | |
593 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
594 | priority_queue(priority_queue&& __q, const _Alloc& __a) | |
92805612 | 595 | : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { } |
7aa1cb97 | 596 | #endif |
ed6814f7 | 597 | |
285b36d6 BK |
598 | /** |
599 | * @brief Builds a %queue from a range. | |
93c66bc6 BK |
600 | * @param __first An input iterator. |
601 | * @param __last An input iterator. | |
602 | * @param __x A comparison functor describing a strict weak ordering. | |
603 | * @param __s An initial sequence with which to start. | |
ed6814f7 | 604 | * |
93c66bc6 BK |
605 | * Begins by copying @a __s, inserting a copy of the elements |
606 | * from @a [first,last) into the copy of @a __s, then ordering | |
607 | * the copy according to @a __x. | |
285b36d6 BK |
608 | * |
609 | * For more information on function objects, see the | |
aac2878e | 610 | * documentation on @link functors functor base |
285b36d6 BK |
611 | * classes@endlink. |
612 | */ | |
734f5023 | 613 | #if __cplusplus < 201103L |
285b36d6 | 614 | template<typename _InputIterator> |
fe62dd04 | 615 | priority_queue(_InputIterator __first, _InputIterator __last, |
285b36d6 BK |
616 | const _Compare& __x = _Compare(), |
617 | const _Sequence& __s = _Sequence()) | |
618 | : c(__s), comp(__x) | |
fe62dd04 | 619 | { |
285b36d6 BK |
620 | __glibcxx_requires_valid_range(__first, __last); |
621 | c.insert(c.end(), __first, __last); | |
622 | std::make_heap(c.begin(), c.end(), comp); | |
623 | } | |
7aa1cb97 | 624 | #else |
e79bde6a JW |
625 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
626 | // 3529. priority_queue(first, last) should construct c with (first, last) | |
627 | template<typename _InputIterator, | |
628 | typename = std::_RequireInputIter<_InputIterator>> | |
629 | priority_queue(_InputIterator __first, _InputIterator __last, | |
630 | const _Compare& __x = _Compare()) | |
631 | : c(__first, __last), comp(__x) | |
632 | { std::make_heap(c.begin(), c.end(), comp); } | |
633 | ||
e5c093e5 JW |
634 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
635 | // 3522. Missing requirement on InputIterator template parameter | |
636 | template<typename _InputIterator, | |
637 | typename = std::_RequireInputIter<_InputIterator>> | |
fe62dd04 | 638 | priority_queue(_InputIterator __first, _InputIterator __last, |
e79bde6a | 639 | const _Compare& __x, const _Sequence& __s) |
7aa1cb97 | 640 | : c(__s), comp(__x) |
fe62dd04 | 641 | { |
7aa1cb97 PC |
642 | __glibcxx_requires_valid_range(__first, __last); |
643 | c.insert(c.end(), __first, __last); | |
644 | std::make_heap(c.begin(), c.end(), comp); | |
645 | } | |
646 | ||
e5c093e5 JW |
647 | template<typename _InputIterator, |
648 | typename = std::_RequireInputIter<_InputIterator>> | |
fe62dd04 | 649 | priority_queue(_InputIterator __first, _InputIterator __last, |
e79bde6a | 650 | const _Compare& __x, _Sequence&& __s) |
7aa1cb97 | 651 | : c(std::move(__s)), comp(__x) |
fe62dd04 | 652 | { |
7aa1cb97 PC |
653 | __glibcxx_requires_valid_range(__first, __last); |
654 | c.insert(c.end(), __first, __last); | |
655 | std::make_heap(c.begin(), c.end(), comp); | |
656 | } | |
6ccffeb5 JW |
657 | |
658 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
659 | // 3506. Missing allocator-extended constructors for priority_queue | |
660 | template<typename _InputIterator, typename _Alloc, | |
661 | typename = std::_RequireInputIter<_InputIterator>, | |
662 | typename _Requires = _Uses<_Alloc>> | |
663 | priority_queue(_InputIterator __first, _InputIterator __last, | |
664 | const _Alloc& __alloc) | |
665 | : c(__first, __last, __alloc), comp() | |
666 | { std::make_heap(c.begin(), c.end(), comp); } | |
667 | ||
668 | template<typename _InputIterator, typename _Alloc, | |
669 | typename = std::_RequireInputIter<_InputIterator>, | |
670 | typename _Requires = _Uses<_Alloc>> | |
671 | priority_queue(_InputIterator __first, _InputIterator __last, | |
672 | const _Compare& __x, const _Alloc& __alloc) | |
673 | : c(__first, __last, __alloc), comp(__x) | |
674 | { std::make_heap(c.begin(), c.end(), comp); } | |
675 | ||
676 | template<typename _InputIterator, typename _Alloc, | |
677 | typename = std::_RequireInputIter<_InputIterator>, | |
678 | typename _Requires = _Uses<_Alloc>> | |
679 | priority_queue(_InputIterator __first, _InputIterator __last, | |
680 | const _Compare& __x, const _Sequence& __s, | |
681 | const _Alloc& __alloc) | |
682 | : c(__s, __alloc), comp(__x) | |
683 | { | |
684 | __glibcxx_requires_valid_range(__first, __last); | |
685 | c.insert(c.end(), __first, __last); | |
686 | std::make_heap(c.begin(), c.end(), comp); | |
687 | } | |
688 | ||
689 | template<typename _InputIterator, typename _Alloc, | |
690 | typename _Requires = _Uses<_Alloc>> | |
691 | priority_queue(_InputIterator __first, _InputIterator __last, | |
692 | const _Compare& __x, _Sequence&& __s, | |
693 | const _Alloc& __alloc) | |
694 | : c(std::move(__s), __alloc), comp(__x) | |
695 | { | |
696 | __glibcxx_requires_valid_range(__first, __last); | |
697 | c.insert(c.end(), __first, __last); | |
698 | std::make_heap(c.begin(), c.end(), comp); | |
699 | } | |
7aa1cb97 | 700 | #endif |
ed6814f7 | 701 | |
285b36d6 BK |
702 | /** |
703 | * Returns true if the %queue is empty. | |
704 | */ | |
d715f554 | 705 | _GLIBCXX_NODISCARD bool |
d508327c PC |
706 | empty() const |
707 | { return c.empty(); } | |
ed6814f7 | 708 | |
285b36d6 | 709 | /** Returns the number of elements in the %queue. */ |
0d04fe49 | 710 | _GLIBCXX_NODISCARD |
285b36d6 | 711 | size_type |
d508327c PC |
712 | size() const |
713 | { return c.size(); } | |
ed6814f7 | 714 | |
285b36d6 BK |
715 | /** |
716 | * Returns a read-only (constant) reference to the data at the first | |
717 | * element of the %queue. | |
718 | */ | |
0d04fe49 | 719 | _GLIBCXX_NODISCARD |
285b36d6 | 720 | const_reference |
ed6814f7 | 721 | top() const |
285b36d6 BK |
722 | { |
723 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 724 | return c.front(); |
285b36d6 | 725 | } |
ed6814f7 | 726 | |
285b36d6 BK |
727 | /** |
728 | * @brief Add data to the %queue. | |
93c66bc6 | 729 | * @param __x Data to be added. |
285b36d6 BK |
730 | * |
731 | * This is a typical %queue operation. | |
732 | * The time complexity of the operation depends on the underlying | |
733 | * sequence. | |
734 | */ | |
ed6814f7 BI |
735 | void |
736 | push(const value_type& __x) | |
285b36d6 | 737 | { |
8443c250 PC |
738 | c.push_back(__x); |
739 | std::push_heap(c.begin(), c.end(), comp); | |
285b36d6 | 740 | } |
4dc3e453 | 741 | |
734f5023 | 742 | #if __cplusplus >= 201103L |
4dc3e453 PC |
743 | void |
744 | push(value_type&& __x) | |
745 | { | |
746 | c.push_back(std::move(__x)); | |
747 | std::push_heap(c.begin(), c.end(), comp); | |
748 | } | |
749 | ||
c54171fe | 750 | template<typename... _Args> |
fe62dd04 FD |
751 | void |
752 | emplace(_Args&&... __args) | |
4dc3e453 PC |
753 | { |
754 | c.emplace_back(std::forward<_Args>(__args)...); | |
c54171fe PC |
755 | std::push_heap(c.begin(), c.end(), comp); |
756 | } | |
7aa1cb97 PC |
757 | #endif |
758 | ||
285b36d6 BK |
759 | /** |
760 | * @brief Removes first element. | |
761 | * | |
762 | * This is a typical %queue operation. It shrinks the %queue | |
763 | * by one. The time complexity of the operation depends on the | |
764 | * underlying sequence. | |
765 | * | |
766 | * Note that no data is returned, and if the first element's | |
767 | * data is needed, it should be retrieved before pop() is | |
768 | * called. | |
769 | */ | |
ed6814f7 BI |
770 | void |
771 | pop() | |
285b36d6 BK |
772 | { |
773 | __glibcxx_requires_nonempty(); | |
8443c250 PC |
774 | std::pop_heap(c.begin(), c.end(), comp); |
775 | c.pop_back(); | |
285b36d6 | 776 | } |
7aa1cb97 | 777 | |
734f5023 | 778 | #if __cplusplus >= 201103L |
7aa1cb97 | 779 | void |
ff74fd13 | 780 | swap(priority_queue& __pq) |
6b9539e2 DK |
781 | noexcept(__and_< |
782 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
fe62dd04 | 783 | __is_nothrow_swappable<_Sequence>, |
6b9539e2 | 784 | #else |
fe62dd04 | 785 | __is_nothrow_swappable<_Tp>, |
6b9539e2 | 786 | #endif |
fe62dd04 FD |
787 | __is_nothrow_swappable<_Compare> |
788 | >::value) | |
7aa1cb97 PC |
789 | { |
790 | using std::swap; | |
999209d0 | 791 | swap(c, __pq.c); |
7aa1cb97 PC |
792 | swap(comp, __pq.comp); |
793 | } | |
6b9539e2 | 794 | #endif // __cplusplus >= 201103L |
285b36d6 | 795 | }; |
ed6814f7 | 796 | |
5bb89e0a JW |
797 | #if __cpp_deduction_guides >= 201606 |
798 | template<typename _Compare, typename _Container, | |
08abbdda JW |
799 | typename = _RequireNotAllocator<_Compare>, |
800 | typename = _RequireNotAllocator<_Container>> | |
5bb89e0a JW |
801 | priority_queue(_Compare, _Container) |
802 | -> priority_queue<typename _Container::value_type, _Container, _Compare>; | |
803 | ||
804 | template<typename _InputIterator, typename _ValT | |
805 | = typename iterator_traits<_InputIterator>::value_type, | |
806 | typename _Compare = less<_ValT>, | |
807 | typename _Container = vector<_ValT>, | |
808 | typename = _RequireInputIter<_InputIterator>, | |
08abbdda JW |
809 | typename = _RequireNotAllocator<_Compare>, |
810 | typename = _RequireNotAllocator<_Container>> | |
5bb89e0a JW |
811 | priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), |
812 | _Container = _Container()) | |
813 | -> priority_queue<_ValT, _Container, _Compare>; | |
814 | ||
815 | template<typename _Compare, typename _Container, typename _Allocator, | |
08abbdda | 816 | typename = _RequireNotAllocator<_Compare>, |
22d34a2a | 817 | typename = _RequireNotAllocator<_Container>> |
5bb89e0a JW |
818 | priority_queue(_Compare, _Container, _Allocator) |
819 | -> priority_queue<typename _Container::value_type, _Container, _Compare>; | |
820 | #endif | |
821 | ||
3971a4d2 | 822 | // No equality/comparison operators are provided for priority_queue. |
3cbc7af0 | 823 | |
734f5023 | 824 | #if __cplusplus >= 201103L |
7aa1cb97 | 825 | template<typename _Tp, typename _Sequence, typename _Compare> |
6b9539e2 DK |
826 | inline |
827 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
828 | // Constrained free swap overload, see p0185r1 | |
829 | typename enable_if<__and_<__is_swappable<_Sequence>, | |
fe62dd04 | 830 | __is_swappable<_Compare>>::value>::type |
6b9539e2 DK |
831 | #else |
832 | void | |
833 | #endif | |
7aa1cb97 PC |
834 | swap(priority_queue<_Tp, _Sequence, _Compare>& __x, |
835 | priority_queue<_Tp, _Sequence, _Compare>& __y) | |
c688bbdd | 836 | noexcept(noexcept(__x.swap(__y))) |
7aa1cb97 | 837 | { __x.swap(__y); } |
aa2b7414 PC |
838 | |
839 | template<typename _Tp, typename _Sequence, typename _Compare, | |
840 | typename _Alloc> | |
841 | struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc> | |
842 | : public uses_allocator<_Sequence, _Alloc>::type { }; | |
6b9539e2 | 843 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 844 | |
12ffa228 BK |
845 | _GLIBCXX_END_NAMESPACE_VERSION |
846 | } // namespace | |
725dc051 | 847 | |
046d30f4 | 848 | #endif /* _STL_QUEUE_H */ |