]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Queue implementation -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2001-2021 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) { } | |
b7e8fb5e JW |
197 | |
198 | #if __cplusplus > 202002L | |
199 | #define __cpp_lib_adaptor_iterator_pair_constructor 202100L | |
200 | ||
201 | template<typename _InputIterator, | |
202 | typename = _RequireInputIter<_InputIterator>> | |
203 | queue(_InputIterator __first, _InputIterator __last) | |
204 | : c(__first, __last) { } | |
205 | ||
206 | template<typename _InputIterator, typename _Alloc, | |
207 | typename = _RequireInputIter<_InputIterator>, | |
208 | typename = _Uses<_Alloc>> | |
209 | queue(_InputIterator __first, _InputIterator __last, const _Alloc& __a) | |
210 | : c(__first, __last, __a) { } | |
211 | #endif | |
7aa1cb97 | 212 | #endif |
ed6814f7 | 213 | |
285b36d6 BK |
214 | /** |
215 | * Returns true if the %queue is empty. | |
216 | */ | |
d715f554 | 217 | _GLIBCXX_NODISCARD bool |
7c920151 PC |
218 | empty() const |
219 | { return c.empty(); } | |
ed6814f7 | 220 | |
285b36d6 | 221 | /** Returns the number of elements in the %queue. */ |
0d04fe49 | 222 | _GLIBCXX_NODISCARD |
285b36d6 | 223 | size_type |
7c920151 PC |
224 | size() const |
225 | { return c.size(); } | |
ed6814f7 | 226 | |
285b36d6 BK |
227 | /** |
228 | * Returns a read/write reference to the data at the first | |
229 | * element of the %queue. | |
230 | */ | |
0d04fe49 | 231 | _GLIBCXX_NODISCARD |
285b36d6 | 232 | reference |
ed6814f7 BI |
233 | front() |
234 | { | |
285b36d6 | 235 | __glibcxx_requires_nonempty(); |
ed6814f7 | 236 | return c.front(); |
285b36d6 | 237 | } |
ed6814f7 | 238 | |
285b36d6 BK |
239 | /** |
240 | * Returns a read-only (constant) reference to the data at the first | |
241 | * element of the %queue. | |
242 | */ | |
0d04fe49 | 243 | _GLIBCXX_NODISCARD |
285b36d6 | 244 | const_reference |
ed6814f7 BI |
245 | front() const |
246 | { | |
285b36d6 | 247 | __glibcxx_requires_nonempty(); |
ed6814f7 | 248 | return c.front(); |
285b36d6 | 249 | } |
ed6814f7 | 250 | |
285b36d6 BK |
251 | /** |
252 | * Returns a read/write reference to the data at the last | |
253 | * element of the %queue. | |
254 | */ | |
0d04fe49 | 255 | _GLIBCXX_NODISCARD |
285b36d6 | 256 | reference |
ed6814f7 | 257 | back() |
285b36d6 BK |
258 | { |
259 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 260 | return c.back(); |
285b36d6 | 261 | } |
ed6814f7 | 262 | |
285b36d6 BK |
263 | /** |
264 | * Returns a read-only (constant) reference to the data at the last | |
265 | * element of the %queue. | |
266 | */ | |
0d04fe49 | 267 | _GLIBCXX_NODISCARD |
285b36d6 | 268 | const_reference |
ed6814f7 | 269 | back() const |
285b36d6 BK |
270 | { |
271 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 272 | return c.back(); |
285b36d6 | 273 | } |
ed6814f7 | 274 | |
285b36d6 BK |
275 | /** |
276 | * @brief Add data to the end of the %queue. | |
93c66bc6 | 277 | * @param __x Data to be added. |
285b36d6 BK |
278 | * |
279 | * This is a typical %queue operation. The function creates an | |
280 | * element at the end of the %queue and assigns the given data | |
281 | * to it. The time complexity of the operation depends on the | |
282 | * underlying sequence. | |
283 | */ | |
284 | void | |
7c920151 PC |
285 | push(const value_type& __x) |
286 | { c.push_back(__x); } | |
4dc3e453 | 287 | |
734f5023 | 288 | #if __cplusplus >= 201103L |
4dc3e453 PC |
289 | void |
290 | push(value_type&& __x) | |
291 | { c.push_back(std::move(__x)); } | |
292 | ||
594ef205 JW |
293 | #if __cplusplus > 201402L |
294 | template<typename... _Args> | |
295 | decltype(auto) | |
296 | emplace(_Args&&... __args) | |
297 | { return c.emplace_back(std::forward<_Args>(__args)...); } | |
298 | #else | |
c54171fe | 299 | template<typename... _Args> |
fe62dd04 FD |
300 | void |
301 | emplace(_Args&&... __args) | |
4dc3e453 | 302 | { c.emplace_back(std::forward<_Args>(__args)...); } |
594ef205 | 303 | #endif |
7aa1cb97 PC |
304 | #endif |
305 | ||
285b36d6 BK |
306 | /** |
307 | * @brief Removes first element. | |
308 | * | |
309 | * This is a typical %queue operation. It shrinks the %queue by one. | |
310 | * The time complexity of the operation depends on the underlying | |
311 | * sequence. | |
312 | * | |
313 | * Note that no data is returned, and if the first element's | |
314 | * data is needed, it should be retrieved before pop() is | |
315 | * called. | |
316 | */ | |
317 | void | |
ed6814f7 BI |
318 | pop() |
319 | { | |
285b36d6 | 320 | __glibcxx_requires_nonempty(); |
ed6814f7 | 321 | c.pop_front(); |
285b36d6 | 322 | } |
ed6814f7 | 323 | |
734f5023 | 324 | #if __cplusplus >= 201103L |
7aa1cb97 | 325 | void |
ff74fd13 | 326 | swap(queue& __q) |
6b9539e2 DK |
327 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
328 | noexcept(__is_nothrow_swappable<_Sequence>::value) | |
329 | #else | |
ddb63209 | 330 | noexcept(__is_nothrow_swappable<_Tp>::value) |
6b9539e2 | 331 | #endif |
999209d0 PC |
332 | { |
333 | using std::swap; | |
334 | swap(c, __q.c); | |
335 | } | |
6b9539e2 | 336 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 337 | }; |
ed6814f7 | 338 | |
5bb89e0a JW |
339 | #if __cpp_deduction_guides >= 201606 |
340 | template<typename _Container, | |
08abbdda | 341 | typename = _RequireNotAllocator<_Container>> |
5bb89e0a JW |
342 | queue(_Container) -> queue<typename _Container::value_type, _Container>; |
343 | ||
344 | template<typename _Container, typename _Allocator, | |
08abbdda JW |
345 | typename = _RequireNotAllocator<_Container>, |
346 | typename = _RequireAllocator<_Allocator>> | |
5bb89e0a JW |
347 | queue(_Container, _Allocator) |
348 | -> queue<typename _Container::value_type, _Container>; | |
b7e8fb5e JW |
349 | |
350 | #ifdef __cpp_lib_adaptor_iterator_pair_constructor | |
351 | template<typename _InputIterator, | |
352 | typename _ValT | |
353 | = typename iterator_traits<_InputIterator>::value_type, | |
354 | typename = _RequireInputIter<_InputIterator>> | |
355 | queue(_InputIterator, _InputIterator) -> queue<_ValT>; | |
356 | ||
357 | template<typename _InputIterator, typename _Allocator, | |
358 | typename _ValT | |
359 | = typename iterator_traits<_InputIterator>::value_type, | |
360 | typename = _RequireInputIter<_InputIterator>, | |
361 | typename = _RequireAllocator<_Allocator>> | |
362 | queue(_InputIterator, _InputIterator, _Allocator) | |
363 | -> queue<_ValT, deque<_ValT, _Allocator>>; | |
364 | #endif | |
5bb89e0a JW |
365 | #endif |
366 | ||
4df6abc6 | 367 | /** |
3971a4d2 | 368 | * @brief Queue equality comparison. |
93c66bc6 BK |
369 | * @param __x A %queue. |
370 | * @param __y A %queue of the same type as @a __x. | |
3971a4d2 PE |
371 | * @return True iff the size and elements of the queues are equal. |
372 | * | |
373 | * This is an equivalence relation. Complexity and semantics depend on the | |
374 | * underlying sequence type, but the expected rules are: this relation is | |
375 | * linear in the size of the sequences, and queues are considered equivalent | |
376 | * if their sequences compare equal. | |
4df6abc6 | 377 | */ |
d508327c | 378 | template<typename _Tp, typename _Seq> |
0d04fe49 | 379 | _GLIBCXX_NODISCARD |
ed6814f7 | 380 | inline bool |
d508327c | 381 | operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 382 | { return __x.c == __y.c; } |
ed6814f7 | 383 | |
4df6abc6 | 384 | /** |
3971a4d2 | 385 | * @brief Queue ordering relation. |
93c66bc6 BK |
386 | * @param __x A %queue. |
387 | * @param __y A %queue of the same type as @a x. | |
388 | * @return True iff @a __x is lexicographically less than @a __y. | |
4df6abc6 | 389 | * |
285b36d6 BK |
390 | * This is an total ordering relation. Complexity and semantics |
391 | * depend on the underlying sequence type, but the expected rules | |
392 | * are: this relation is linear in the size of the sequences, the | |
393 | * elements must be comparable with @c <, and | |
394 | * std::lexicographical_compare() is usually used to make the | |
3971a4d2 | 395 | * determination. |
4df6abc6 | 396 | */ |
d508327c | 397 | template<typename _Tp, typename _Seq> |
0d04fe49 | 398 | _GLIBCXX_NODISCARD |
3971a4d2 | 399 | inline bool |
d508327c | 400 | operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 401 | { return __x.c < __y.c; } |
ed6814f7 | 402 | |
3971a4d2 | 403 | /// Based on operator== |
d508327c | 404 | template<typename _Tp, typename _Seq> |
0d04fe49 | 405 | _GLIBCXX_NODISCARD |
3971a4d2 | 406 | inline bool |
d508327c | 407 | operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 408 | { return !(__x == __y); } |
ed6814f7 | 409 | |
3971a4d2 | 410 | /// Based on operator< |
d508327c | 411 | template<typename _Tp, typename _Seq> |
0d04fe49 | 412 | _GLIBCXX_NODISCARD |
ed6814f7 | 413 | inline bool |
d508327c | 414 | operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 415 | { return __y < __x; } |
ed6814f7 | 416 | |
3971a4d2 | 417 | /// Based on operator< |
d508327c | 418 | template<typename _Tp, typename _Seq> |
0d04fe49 | 419 | _GLIBCXX_NODISCARD |
ed6814f7 | 420 | inline bool |
d508327c | 421 | operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 422 | { return !(__y < __x); } |
ed6814f7 | 423 | |
3971a4d2 | 424 | /// Based on operator< |
d508327c | 425 | template<typename _Tp, typename _Seq> |
0d04fe49 | 426 | _GLIBCXX_NODISCARD |
ed6814f7 | 427 | inline bool |
d508327c | 428 | operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
3971a4d2 | 429 | { return !(__x < __y); } |
ed6814f7 | 430 | |
717e91db JW |
431 | #if __cpp_lib_three_way_comparison |
432 | template<typename _Tp, three_way_comparable _Seq> | |
0d04fe49 | 433 | [[nodiscard]] |
717e91db JW |
434 | inline compare_three_way_result_t<_Seq> |
435 | operator<=>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) | |
436 | { return __x.c <=> __y.c; } | |
437 | #endif | |
438 | ||
734f5023 | 439 | #if __cplusplus >= 201103L |
7aa1cb97 | 440 | template<typename _Tp, typename _Seq> |
6b9539e2 DK |
441 | inline |
442 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
443 | // Constrained free swap overload, see p0185r1 | |
444 | typename enable_if<__is_swappable<_Seq>::value>::type | |
445 | #else | |
446 | void | |
447 | #endif | |
7aa1cb97 | 448 | swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y) |
c688bbdd | 449 | noexcept(noexcept(__x.swap(__y))) |
7aa1cb97 | 450 | { __x.swap(__y); } |
aa2b7414 PC |
451 | |
452 | template<typename _Tp, typename _Seq, typename _Alloc> | |
453 | struct uses_allocator<queue<_Tp, _Seq>, _Alloc> | |
454 | : public uses_allocator<_Seq, _Alloc>::type { }; | |
6b9539e2 | 455 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 456 | |
4df6abc6 | 457 | /** |
3971a4d2 | 458 | * @brief A standard container automatically sorting its contents. |
4df6abc6 | 459 | * |
aac2878e | 460 | * @ingroup sequences |
4df6abc6 | 461 | * |
d632488a BK |
462 | * @tparam _Tp Type of element. |
463 | * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>. | |
fe62dd04 | 464 | * @tparam _Compare Comparison function object type, defaults to |
d632488a BK |
465 | * less<_Sequence::value_type>. |
466 | * | |
285b36d6 BK |
467 | * This is not a true container, but an @e adaptor. It holds |
468 | * another container, and provides a wrapper interface to that | |
fe62dd04 | 469 | * container. The wrapper is what enforces priority-based sorting |
3a498393 PC |
470 | * and %queue behavior. Very few of the standard container/sequence |
471 | * interface requirements are met (e.g., iterators). | |
3971a4d2 PE |
472 | * |
473 | * The second template parameter defines the type of the underlying | |
285b36d6 BK |
474 | * sequence/container. It defaults to std::vector, but it can be |
475 | * any type that supports @c front(), @c push_back, @c pop_back, | |
476 | * and random-access iterators, such as std::deque or an | |
477 | * appropriate user-defined type. | |
3971a4d2 | 478 | * |
285b36d6 BK |
479 | * The third template parameter supplies the means of making |
480 | * priority comparisons. It defaults to @c less<value_type> but | |
481 | * can be anything defining a strict weak ordering. | |
3971a4d2 | 482 | * |
2a60a9f6 | 483 | * Members not found in @a normal containers are @c container_type, |
285b36d6 | 484 | * which is a typedef for the second Sequence parameter, and @c |
3a498393 | 485 | * push, @c pop, and @c top, which are standard %queue operations. |
3971a4d2 | 486 | * |
285b36d6 BK |
487 | * @note No equality/comparison operators are provided for |
488 | * %priority_queue. | |
3971a4d2 | 489 | * |
285b36d6 BK |
490 | * @note Sorting of the elements takes place as they are added to, |
491 | * and removed from, the %priority_queue using the | |
492 | * %priority_queue's member functions. If you access the elements | |
493 | * by other means, and change their data such that the sorting | |
494 | * order would be different, the %priority_queue will not re-sort | |
495 | * the elements for you. (How could it know to do so?) | |
4df6abc6 | 496 | */ |
285b36d6 | 497 | template<typename _Tp, typename _Sequence = vector<_Tp>, |
7c920151 | 498 | typename _Compare = less<typename _Sequence::value_type> > |
3971a4d2 | 499 | class priority_queue |
3971a4d2 | 500 | { |
fe62dd04 | 501 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
285b36d6 BK |
502 | // concept requirements |
503 | typedef typename _Sequence::value_type _Sequence_value_type; | |
fe62dd04 | 504 | # if __cplusplus < 201103L |
285b36d6 | 505 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
fe62dd04 | 506 | # endif |
285b36d6 BK |
507 | __glibcxx_class_requires(_Sequence, _SequenceConcept) |
508 | __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) | |
509 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) | |
d508327c PC |
510 | __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, |
511 | _BinaryFunctionConcept) | |
fe62dd04 | 512 | #endif |
ed6814f7 | 513 | |
997ed914 JW |
514 | #if __cplusplus >= 201103L |
515 | template<typename _Alloc> | |
516 | using _Uses = typename | |
517 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; | |
1138a19d JW |
518 | |
519 | #if __cplusplus >= 201703L | |
520 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
521 | // 2566. Requirements on the first template parameter of container | |
522 | // adaptors | |
523 | static_assert(is_same<_Tp, typename _Sequence::value_type>::value, | |
524 | "value_type must be the same as the underlying container"); | |
525 | #endif // C++17 | |
526 | #endif // C++11 | |
997ed914 | 527 | |
7c920151 | 528 | public: |
fe62dd04 | 529 | typedef typename _Sequence::value_type value_type; |
1138a19d JW |
530 | typedef typename _Sequence::reference reference; |
531 | typedef typename _Sequence::const_reference const_reference; | |
532 | typedef typename _Sequence::size_type size_type; | |
533 | typedef _Sequence container_type; | |
8be062c6 JW |
534 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
535 | // DR 2684. priority_queue lacking comparator typedef | |
1138a19d | 536 | typedef _Compare value_compare; |
ed6814f7 | 537 | |
285b36d6 BK |
538 | protected: |
539 | // See queue::c for notes on these names. | |
a1f009a6 | 540 | _Sequence c; |
285b36d6 | 541 | _Compare comp; |
ed6814f7 | 542 | |
285b36d6 BK |
543 | public: |
544 | /** | |
545 | * @brief Default constructor creates no elements. | |
546 | */ | |
734f5023 | 547 | #if __cplusplus < 201103L |
285b36d6 | 548 | explicit |
ed6814f7 BI |
549 | priority_queue(const _Compare& __x = _Compare(), |
550 | const _Sequence& __s = _Sequence()) | |
551 | : c(__s), comp(__x) | |
285b36d6 | 552 | { std::make_heap(c.begin(), c.end(), comp); } |
7aa1cb97 | 553 | #else |
a1f009a6 JW |
554 | template<typename _Seq = _Sequence, typename _Requires = typename |
555 | enable_if<__and_<is_default_constructible<_Compare>, | |
fe62dd04 | 556 | is_default_constructible<_Seq>>::value>::type> |
a1f009a6 JW |
557 | priority_queue() |
558 | : c(), comp() { } | |
d2e1d4b7 | 559 | |
7aa1cb97 | 560 | explicit |
a1f009a6 | 561 | priority_queue(const _Compare& __x, const _Sequence& __s) |
7aa1cb97 PC |
562 | : c(__s), comp(__x) |
563 | { std::make_heap(c.begin(), c.end(), comp); } | |
564 | ||
565 | explicit | |
a1f009a6 | 566 | priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence()) |
7aa1cb97 PC |
567 | : c(std::move(__s)), comp(__x) |
568 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
569 | |
570 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
571 | explicit | |
572 | priority_queue(const _Alloc& __a) | |
92805612 | 573 | : c(__a), comp() { } |
997ed914 JW |
574 | |
575 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
576 | priority_queue(const _Compare& __x, const _Alloc& __a) | |
92805612 | 577 | : c(__a), comp(__x) { } |
997ed914 | 578 | |
1f4dcbf7 JW |
579 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
580 | // 2537. Constructors [...] taking allocators should call make_heap | |
997ed914 JW |
581 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
582 | priority_queue(const _Compare& __x, const _Sequence& __c, | |
583 | const _Alloc& __a) | |
1f4dcbf7 JW |
584 | : c(__c, __a), comp(__x) |
585 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
586 | |
587 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
588 | priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a) | |
1f4dcbf7 JW |
589 | : c(std::move(__c), __a), comp(__x) |
590 | { std::make_heap(c.begin(), c.end(), comp); } | |
997ed914 JW |
591 | |
592 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
593 | priority_queue(const priority_queue& __q, const _Alloc& __a) | |
92805612 | 594 | : c(__q.c, __a), comp(__q.comp) { } |
997ed914 JW |
595 | |
596 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
597 | priority_queue(priority_queue&& __q, const _Alloc& __a) | |
92805612 | 598 | : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { } |
7aa1cb97 | 599 | #endif |
ed6814f7 | 600 | |
285b36d6 BK |
601 | /** |
602 | * @brief Builds a %queue from a range. | |
93c66bc6 BK |
603 | * @param __first An input iterator. |
604 | * @param __last An input iterator. | |
605 | * @param __x A comparison functor describing a strict weak ordering. | |
606 | * @param __s An initial sequence with which to start. | |
ed6814f7 | 607 | * |
93c66bc6 BK |
608 | * Begins by copying @a __s, inserting a copy of the elements |
609 | * from @a [first,last) into the copy of @a __s, then ordering | |
610 | * the copy according to @a __x. | |
285b36d6 BK |
611 | * |
612 | * For more information on function objects, see the | |
aac2878e | 613 | * documentation on @link functors functor base |
285b36d6 BK |
614 | * classes@endlink. |
615 | */ | |
734f5023 | 616 | #if __cplusplus < 201103L |
285b36d6 | 617 | template<typename _InputIterator> |
fe62dd04 | 618 | priority_queue(_InputIterator __first, _InputIterator __last, |
285b36d6 BK |
619 | const _Compare& __x = _Compare(), |
620 | const _Sequence& __s = _Sequence()) | |
621 | : c(__s), comp(__x) | |
fe62dd04 | 622 | { |
285b36d6 BK |
623 | __glibcxx_requires_valid_range(__first, __last); |
624 | c.insert(c.end(), __first, __last); | |
625 | std::make_heap(c.begin(), c.end(), comp); | |
626 | } | |
7aa1cb97 | 627 | #else |
e79bde6a JW |
628 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
629 | // 3529. priority_queue(first, last) should construct c with (first, last) | |
630 | template<typename _InputIterator, | |
631 | typename = std::_RequireInputIter<_InputIterator>> | |
632 | priority_queue(_InputIterator __first, _InputIterator __last, | |
633 | const _Compare& __x = _Compare()) | |
634 | : c(__first, __last), comp(__x) | |
635 | { std::make_heap(c.begin(), c.end(), comp); } | |
636 | ||
e5c093e5 JW |
637 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
638 | // 3522. Missing requirement on InputIterator template parameter | |
639 | template<typename _InputIterator, | |
640 | typename = std::_RequireInputIter<_InputIterator>> | |
fe62dd04 | 641 | priority_queue(_InputIterator __first, _InputIterator __last, |
e79bde6a | 642 | const _Compare& __x, const _Sequence& __s) |
7aa1cb97 | 643 | : c(__s), comp(__x) |
fe62dd04 | 644 | { |
7aa1cb97 PC |
645 | __glibcxx_requires_valid_range(__first, __last); |
646 | c.insert(c.end(), __first, __last); | |
647 | std::make_heap(c.begin(), c.end(), comp); | |
648 | } | |
649 | ||
e5c093e5 JW |
650 | template<typename _InputIterator, |
651 | typename = std::_RequireInputIter<_InputIterator>> | |
fe62dd04 | 652 | priority_queue(_InputIterator __first, _InputIterator __last, |
e79bde6a | 653 | const _Compare& __x, _Sequence&& __s) |
7aa1cb97 | 654 | : c(std::move(__s)), comp(__x) |
fe62dd04 | 655 | { |
7aa1cb97 PC |
656 | __glibcxx_requires_valid_range(__first, __last); |
657 | c.insert(c.end(), __first, __last); | |
658 | std::make_heap(c.begin(), c.end(), comp); | |
659 | } | |
6ccffeb5 JW |
660 | |
661 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
662 | // 3506. Missing allocator-extended constructors for priority_queue | |
663 | template<typename _InputIterator, typename _Alloc, | |
664 | typename = std::_RequireInputIter<_InputIterator>, | |
665 | typename _Requires = _Uses<_Alloc>> | |
666 | priority_queue(_InputIterator __first, _InputIterator __last, | |
667 | const _Alloc& __alloc) | |
668 | : c(__first, __last, __alloc), comp() | |
669 | { std::make_heap(c.begin(), c.end(), comp); } | |
670 | ||
671 | template<typename _InputIterator, typename _Alloc, | |
672 | typename = std::_RequireInputIter<_InputIterator>, | |
673 | typename _Requires = _Uses<_Alloc>> | |
674 | priority_queue(_InputIterator __first, _InputIterator __last, | |
675 | const _Compare& __x, const _Alloc& __alloc) | |
676 | : c(__first, __last, __alloc), comp(__x) | |
677 | { std::make_heap(c.begin(), c.end(), comp); } | |
678 | ||
679 | template<typename _InputIterator, typename _Alloc, | |
680 | typename = std::_RequireInputIter<_InputIterator>, | |
681 | typename _Requires = _Uses<_Alloc>> | |
682 | priority_queue(_InputIterator __first, _InputIterator __last, | |
683 | const _Compare& __x, const _Sequence& __s, | |
684 | const _Alloc& __alloc) | |
685 | : c(__s, __alloc), comp(__x) | |
686 | { | |
687 | __glibcxx_requires_valid_range(__first, __last); | |
688 | c.insert(c.end(), __first, __last); | |
689 | std::make_heap(c.begin(), c.end(), comp); | |
690 | } | |
691 | ||
692 | template<typename _InputIterator, typename _Alloc, | |
693 | typename _Requires = _Uses<_Alloc>> | |
694 | priority_queue(_InputIterator __first, _InputIterator __last, | |
695 | const _Compare& __x, _Sequence&& __s, | |
696 | const _Alloc& __alloc) | |
697 | : c(std::move(__s), __alloc), comp(__x) | |
698 | { | |
699 | __glibcxx_requires_valid_range(__first, __last); | |
700 | c.insert(c.end(), __first, __last); | |
701 | std::make_heap(c.begin(), c.end(), comp); | |
702 | } | |
7aa1cb97 | 703 | #endif |
ed6814f7 | 704 | |
285b36d6 BK |
705 | /** |
706 | * Returns true if the %queue is empty. | |
707 | */ | |
d715f554 | 708 | _GLIBCXX_NODISCARD bool |
d508327c PC |
709 | empty() const |
710 | { return c.empty(); } | |
ed6814f7 | 711 | |
285b36d6 | 712 | /** Returns the number of elements in the %queue. */ |
0d04fe49 | 713 | _GLIBCXX_NODISCARD |
285b36d6 | 714 | size_type |
d508327c PC |
715 | size() const |
716 | { return c.size(); } | |
ed6814f7 | 717 | |
285b36d6 BK |
718 | /** |
719 | * Returns a read-only (constant) reference to the data at the first | |
720 | * element of the %queue. | |
721 | */ | |
0d04fe49 | 722 | _GLIBCXX_NODISCARD |
285b36d6 | 723 | const_reference |
ed6814f7 | 724 | top() const |
285b36d6 BK |
725 | { |
726 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 727 | return c.front(); |
285b36d6 | 728 | } |
ed6814f7 | 729 | |
285b36d6 BK |
730 | /** |
731 | * @brief Add data to the %queue. | |
93c66bc6 | 732 | * @param __x Data to be added. |
285b36d6 BK |
733 | * |
734 | * This is a typical %queue operation. | |
735 | * The time complexity of the operation depends on the underlying | |
736 | * sequence. | |
737 | */ | |
ed6814f7 BI |
738 | void |
739 | push(const value_type& __x) | |
285b36d6 | 740 | { |
8443c250 PC |
741 | c.push_back(__x); |
742 | std::push_heap(c.begin(), c.end(), comp); | |
285b36d6 | 743 | } |
4dc3e453 | 744 | |
734f5023 | 745 | #if __cplusplus >= 201103L |
4dc3e453 PC |
746 | void |
747 | push(value_type&& __x) | |
748 | { | |
749 | c.push_back(std::move(__x)); | |
750 | std::push_heap(c.begin(), c.end(), comp); | |
751 | } | |
752 | ||
c54171fe | 753 | template<typename... _Args> |
fe62dd04 FD |
754 | void |
755 | emplace(_Args&&... __args) | |
4dc3e453 PC |
756 | { |
757 | c.emplace_back(std::forward<_Args>(__args)...); | |
c54171fe PC |
758 | std::push_heap(c.begin(), c.end(), comp); |
759 | } | |
7aa1cb97 PC |
760 | #endif |
761 | ||
285b36d6 BK |
762 | /** |
763 | * @brief Removes first element. | |
764 | * | |
765 | * This is a typical %queue operation. It shrinks the %queue | |
766 | * by one. The time complexity of the operation depends on the | |
767 | * underlying sequence. | |
768 | * | |
769 | * Note that no data is returned, and if the first element's | |
770 | * data is needed, it should be retrieved before pop() is | |
771 | * called. | |
772 | */ | |
ed6814f7 BI |
773 | void |
774 | pop() | |
285b36d6 BK |
775 | { |
776 | __glibcxx_requires_nonempty(); | |
8443c250 PC |
777 | std::pop_heap(c.begin(), c.end(), comp); |
778 | c.pop_back(); | |
285b36d6 | 779 | } |
7aa1cb97 | 780 | |
734f5023 | 781 | #if __cplusplus >= 201103L |
7aa1cb97 | 782 | void |
ff74fd13 | 783 | swap(priority_queue& __pq) |
6b9539e2 DK |
784 | noexcept(__and_< |
785 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
fe62dd04 | 786 | __is_nothrow_swappable<_Sequence>, |
6b9539e2 | 787 | #else |
fe62dd04 | 788 | __is_nothrow_swappable<_Tp>, |
6b9539e2 | 789 | #endif |
fe62dd04 FD |
790 | __is_nothrow_swappable<_Compare> |
791 | >::value) | |
7aa1cb97 PC |
792 | { |
793 | using std::swap; | |
999209d0 | 794 | swap(c, __pq.c); |
7aa1cb97 PC |
795 | swap(comp, __pq.comp); |
796 | } | |
6b9539e2 | 797 | #endif // __cplusplus >= 201103L |
285b36d6 | 798 | }; |
ed6814f7 | 799 | |
5bb89e0a JW |
800 | #if __cpp_deduction_guides >= 201606 |
801 | template<typename _Compare, typename _Container, | |
08abbdda JW |
802 | typename = _RequireNotAllocator<_Compare>, |
803 | typename = _RequireNotAllocator<_Container>> | |
5bb89e0a JW |
804 | priority_queue(_Compare, _Container) |
805 | -> priority_queue<typename _Container::value_type, _Container, _Compare>; | |
806 | ||
807 | template<typename _InputIterator, typename _ValT | |
808 | = typename iterator_traits<_InputIterator>::value_type, | |
809 | typename _Compare = less<_ValT>, | |
810 | typename _Container = vector<_ValT>, | |
811 | typename = _RequireInputIter<_InputIterator>, | |
08abbdda JW |
812 | typename = _RequireNotAllocator<_Compare>, |
813 | typename = _RequireNotAllocator<_Container>> | |
5bb89e0a JW |
814 | priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), |
815 | _Container = _Container()) | |
816 | -> priority_queue<_ValT, _Container, _Compare>; | |
817 | ||
818 | template<typename _Compare, typename _Container, typename _Allocator, | |
08abbdda JW |
819 | typename = _RequireNotAllocator<_Compare>, |
820 | typename = _RequireNotAllocator<_Container>, | |
821 | typename = _RequireAllocator<_Allocator>> | |
5bb89e0a JW |
822 | priority_queue(_Compare, _Container, _Allocator) |
823 | -> priority_queue<typename _Container::value_type, _Container, _Compare>; | |
824 | #endif | |
825 | ||
3971a4d2 | 826 | // No equality/comparison operators are provided for priority_queue. |
3cbc7af0 | 827 | |
734f5023 | 828 | #if __cplusplus >= 201103L |
7aa1cb97 | 829 | template<typename _Tp, typename _Sequence, typename _Compare> |
6b9539e2 DK |
830 | inline |
831 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
832 | // Constrained free swap overload, see p0185r1 | |
833 | typename enable_if<__and_<__is_swappable<_Sequence>, | |
fe62dd04 | 834 | __is_swappable<_Compare>>::value>::type |
6b9539e2 DK |
835 | #else |
836 | void | |
837 | #endif | |
7aa1cb97 PC |
838 | swap(priority_queue<_Tp, _Sequence, _Compare>& __x, |
839 | priority_queue<_Tp, _Sequence, _Compare>& __y) | |
c688bbdd | 840 | noexcept(noexcept(__x.swap(__y))) |
7aa1cb97 | 841 | { __x.swap(__y); } |
aa2b7414 PC |
842 | |
843 | template<typename _Tp, typename _Sequence, typename _Compare, | |
844 | typename _Alloc> | |
845 | struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc> | |
846 | : public uses_allocator<_Sequence, _Alloc>::type { }; | |
6b9539e2 | 847 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 848 | |
12ffa228 BK |
849 | _GLIBCXX_END_NAMESPACE_VERSION |
850 | } // namespace | |
725dc051 | 851 | |
046d30f4 | 852 | #endif /* _STL_QUEUE_H */ |