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