libstdc++
stl_queue.h
Go to the documentation of this file.
1// Queue implementation -*- C++ -*-
2
3// Copyright (C) 2001-2025 Free Software Foundation, Inc.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
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
51/** @file bits/stl_queue.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{queue}
54 */
55
56#ifndef _STL_QUEUE_H
57#define _STL_QUEUE_H 1
58
59#include <bits/concept_check.h>
60#include <debug/debug.h>
61#if __cplusplus >= 201103L
62# include <bits/uses_allocator.h>
63#endif
64
65namespace std _GLIBCXX_VISIBILITY(default)
66{
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
68
69 /**
70 * @brief A standard container giving FIFO behavior.
71 *
72 * @ingroup sequences
73 *
74 * @tparam _Tp Type of element.
75 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
76 *
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.
81 *
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.
85 *
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 *
91 * Members not found in @a normal containers are @c container_type,
92 * which is a typedef for the second Sequence parameter, and @c push and
93 * @c pop, which are standard %queue/FIFO operations.
94 */
95 template<typename _Tp, typename _Sequence = deque<_Tp> >
96 class queue
97 {
98#ifdef _GLIBCXX_CONCEPT_CHECKS
99 // concept requirements
100 typedef typename _Sequence::value_type _Sequence_value_type;
101# if __cplusplus < 201103L
102 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
103# endif
104 __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
105 __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
106 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
107#endif
108
109 template<typename _Tp1, typename _Seq1>
110 friend bool
111 operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
112
113 template<typename _Tp1, typename _Seq1>
114 friend bool
115 operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
116
117#if __cpp_lib_three_way_comparison
118 template<typename _Tp1, three_way_comparable _Seq1>
120 operator<=>(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
121#endif
122
123#if __cplusplus >= 201103L
124 template<typename _Alloc>
125 using _Uses = typename
127
128#if __cplusplus >= 201703L
129 // _GLIBCXX_RESOLVE_LIB_DEFECTS
130 // 2566. Requirements on the first template parameter of container
131 // adaptors
133 "value_type must be the same as the underlying container");
134#endif // C++17
135#endif // C++11
136
137 public:
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;
143
144 protected:
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
149 * of private: to allow derivation. But none of the other
150 * containers allow for derivation. Odd.)
151 */
152 /// @c c is the underlying container.
153 _Sequence c;
154
155 public:
156 /**
157 * @brief Default constructor creates no elements.
158 */
159#if __cplusplus < 201103L
160 explicit
161 queue(const _Sequence& __c = _Sequence())
162 : c(__c) { }
163#else
164 template<typename _Seq = _Sequence, typename _Requires = typename
167 : c() { }
168
169 explicit
170 queue(const _Sequence& __c)
171 : c(__c) { }
172
173 explicit
174 queue(_Sequence&& __c)
175 : c(std::move(__c)) { }
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) { }
197#endif
198
199#ifdef __glibcxx_adaptor_iterator_pair_constructor // C++ >= 23 && HOSTED
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) { }
210#endif
211
212 /**
213 * Returns true if the %queue is empty.
214 */
215 _GLIBCXX_NODISCARD bool
216 empty() const
217 { return c.empty(); }
218
219 /** Returns the number of elements in the %queue. */
220 _GLIBCXX_NODISCARD
221 size_type
222 size() const
223 { return c.size(); }
224
225 /**
226 * Returns a read/write reference to the data at the first
227 * element of the %queue.
228 */
229 _GLIBCXX_NODISCARD
230 reference
232 {
233 __glibcxx_requires_nonempty();
234 return c.front();
235 }
236
237 /**
238 * Returns a read-only (constant) reference to the data at the first
239 * element of the %queue.
240 */
241 _GLIBCXX_NODISCARD
242 const_reference
243 front() const
244 {
245 __glibcxx_requires_nonempty();
246 return c.front();
247 }
248
249 /**
250 * Returns a read/write reference to the data at the last
251 * element of the %queue.
252 */
253 _GLIBCXX_NODISCARD
254 reference
256 {
257 __glibcxx_requires_nonempty();
258 return c.back();
259 }
260
261 /**
262 * Returns a read-only (constant) reference to the data at the last
263 * element of the %queue.
264 */
265 _GLIBCXX_NODISCARD
266 const_reference
267 back() const
268 {
269 __glibcxx_requires_nonempty();
270 return c.back();
271 }
272
273 /**
274 * @brief Add data to the end of the %queue.
275 * @param __x Data to be added.
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
283 push(const value_type& __x)
284 { c.push_back(__x); }
285
286#if __cplusplus >= 201103L
287 void
288 push(value_type&& __x)
289 { c.push_back(std::move(__x)); }
290
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
297 template<typename... _Args>
298 void
299 emplace(_Args&&... __args)
300 { c.emplace_back(std::forward<_Args>(__args)...); }
301#endif
302#endif
303
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
317 {
318 __glibcxx_requires_nonempty();
319 c.pop_front();
320 }
321
322#if __cplusplus >= 201103L
323 void
324 swap(queue& __q)
325#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
326 noexcept(__is_nothrow_swappable<_Sequence>::value)
327#else
328 noexcept(__is_nothrow_swappable<_Tp>::value)
329#endif
330 {
331 using std::swap;
332 swap(c, __q.c);
333 }
334#endif // __cplusplus >= 201103L
335 };
336
337#if __cpp_deduction_guides >= 201606
338 template<typename _Container,
339 typename = _RequireNotAllocator<_Container>>
340 queue(_Container) -> queue<typename _Container::value_type, _Container>;
341
342 template<typename _Container, typename _Allocator,
343 typename = _RequireNotAllocator<_Container>>
344 queue(_Container, _Allocator)
345 -> queue<typename _Container::value_type, _Container>;
346
347#ifdef __glibcxx_adaptor_iterator_pair_constructor
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
362#endif
363
364 /**
365 * @brief Queue equality comparison.
366 * @param __x A %queue.
367 * @param __y A %queue of the same type as @a __x.
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.
374 */
375 template<typename _Tp, typename _Seq>
376 _GLIBCXX_NODISCARD
377 inline bool
378 operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
379 { return __x.c == __y.c; }
380
381 /**
382 * @brief Queue ordering relation.
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.
386 *
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
392 * determination.
393 */
394 template<typename _Tp, typename _Seq>
395 _GLIBCXX_NODISCARD
396 inline bool
397 operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
398 { return __x.c < __y.c; }
399
400 /// Based on operator==
401 template<typename _Tp, typename _Seq>
402 _GLIBCXX_NODISCARD
403 inline bool
404 operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
405 { return !(__x == __y); }
406
407 /// Based on operator<
408 template<typename _Tp, typename _Seq>
409 _GLIBCXX_NODISCARD
410 inline bool
411 operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
412 { return __y < __x; }
413
414 /// Based on operator<
415 template<typename _Tp, typename _Seq>
416 _GLIBCXX_NODISCARD
417 inline bool
418 operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
419 { return !(__y < __x); }
420
421 /// Based on operator<
422 template<typename _Tp, typename _Seq>
423 _GLIBCXX_NODISCARD
424 inline bool
425 operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
426 { return !(__x < __y); }
427
428#if __cpp_lib_three_way_comparison
429 template<typename _Tp, three_way_comparable _Seq>
430 [[nodiscard]]
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
436#if __cplusplus >= 201103L
437 template<typename _Tp, typename _Seq>
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
445 swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
446 noexcept(noexcept(__x.swap(__y)))
447 { __x.swap(__y); }
448
449 template<typename _Tp, typename _Seq, typename _Alloc>
450 struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
451 : public uses_allocator<_Seq, _Alloc>::type { };
452#endif // __cplusplus >= 201103L
453
454 /**
455 * @brief A standard container automatically sorting its contents.
456 *
457 * @ingroup sequences
458 *
459 * @tparam _Tp Type of element.
460 * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
461 * @tparam _Compare Comparison function object type, defaults to
462 * less<_Sequence::value_type>.
463 *
464 * This is not a true container, but an @e adaptor. It holds
465 * another container, and provides a wrapper interface to that
466 * container. The wrapper is what enforces priority-based sorting
467 * and %queue behavior. Very few of the standard container/sequence
468 * interface requirements are met (e.g., iterators).
469 *
470 * The second template parameter defines the type of the underlying
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.
475 *
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.
479 *
480 * Members not found in @a normal containers are @c container_type,
481 * which is a typedef for the second Sequence parameter, and @c
482 * push, @c pop, and @c top, which are standard %queue operations.
483 *
484 * @note No equality/comparison operators are provided for
485 * %priority_queue.
486 *
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?)
493 */
494 template<typename _Tp, typename _Sequence = vector<_Tp>,
495 typename _Compare = less<typename _Sequence::value_type> >
497 {
498#ifdef _GLIBCXX_CONCEPT_CHECKS
499 // concept requirements
500 typedef typename _Sequence::value_type _Sequence_value_type;
501# if __cplusplus < 201103L
502 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
503# endif
504 __glibcxx_class_requires(_Sequence, _SequenceConcept)
505 __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
506 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
507 __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
508 _BinaryFunctionConcept)
509#endif
510
511#if __cplusplus >= 201103L
512 template<typename _Alloc>
513 using _Uses = typename
515
516#if __cplusplus >= 201703L
517 // _GLIBCXX_RESOLVE_LIB_DEFECTS
518 // 2566. Requirements on the first template parameter of container
519 // adaptors
521 "value_type must be the same as the underlying container");
522#endif // C++17
523#endif // C++11
524
525 public:
526 typedef typename _Sequence::value_type value_type;
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;
531 // _GLIBCXX_RESOLVE_LIB_DEFECTS
532 // DR 2684. priority_queue lacking comparator typedef
533 typedef _Compare value_compare;
534
535 protected:
536 // See queue::c for notes on these names.
537 _Sequence c;
538 _Compare comp;
539
540 public:
541 /**
542 * @brief Default constructor creates no elements.
543 */
544#if __cplusplus < 201103L
545 explicit
546 priority_queue(const _Compare& __x = _Compare(),
547 const _Sequence& __s = _Sequence())
548 : c(__s), comp(__x)
549 { std::make_heap(c.begin(), c.end(), comp); }
550#else
551 template<typename _Seq = _Sequence, typename _Requires = typename
553 is_default_constructible<_Seq>>::value>::type>
555 : c(), comp() { }
556
557 explicit
558 priority_queue(const _Compare& __x, const _Sequence& __s)
559 : c(__s), comp(__x)
560 { std::make_heap(c.begin(), c.end(), comp); }
561
562 explicit
563 priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
564 : c(std::move(__s)), comp(__x)
565 { std::make_heap(c.begin(), c.end(), comp); }
566
567 priority_queue(const priority_queue&) = default;
568 priority_queue& operator=(const priority_queue&) = default;
569
571 noexcept(__and_<is_nothrow_move_constructible<_Sequence>,
572 is_nothrow_move_constructible<_Compare>>::value)
573 : c(std::move(__q.c)), comp(std::move(__q.comp))
574 { __q.c.clear(); }
575
577 operator=(priority_queue&& __q)
578 noexcept(__and_<is_nothrow_move_assignable<_Sequence>,
579 is_nothrow_move_assignable<_Compare>>::value)
580 {
581 c = std::move(__q.c);
582 __q.c.clear();
583 comp = std::move(__q.comp);
584 return *this;
585 }
586
587 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
588 explicit
589 priority_queue(const _Alloc& __a)
590 : c(__a), comp() { }
591
592 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
593 priority_queue(const _Compare& __x, const _Alloc& __a)
594 : c(__a), comp(__x) { }
595
596 // _GLIBCXX_RESOLVE_LIB_DEFECTS
597 // 2537. Constructors [...] taking allocators should call make_heap
598 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
599 priority_queue(const _Compare& __x, const _Sequence& __c,
600 const _Alloc& __a)
601 : c(__c, __a), comp(__x)
602 { std::make_heap(c.begin(), c.end(), comp); }
603
604 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
605 priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
606 : c(std::move(__c), __a), comp(__x)
607 { std::make_heap(c.begin(), c.end(), comp); }
608
609 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
610 priority_queue(const priority_queue& __q, const _Alloc& __a)
611 : c(__q.c, __a), comp(__q.comp) { }
612
613 template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
614 priority_queue(priority_queue&& __q, const _Alloc& __a)
615 : c(std::move(__q.c), __a), comp(std::move(__q.comp))
616 { __q.c.clear(); }
617#endif
618
619 /**
620 * @brief Builds a %queue from a range.
621 * @param __first An input iterator.
622 * @param __last An input iterator.
623 * @param __x A comparison functor describing a strict weak ordering.
624 * @param __s An initial sequence with which to start.
625 *
626 * Begins by copying @a __s, inserting a copy of the elements
627 * from @a [first,last) into the copy of @a __s, then ordering
628 * the copy according to @a __x.
629 *
630 * For more information on function objects, see the
631 * documentation on @link functors functor base classes@endlink.
632 */
633#if __cplusplus < 201103L
634 template<typename _InputIterator>
635 priority_queue(_InputIterator __first, _InputIterator __last,
636 const _Compare& __x = _Compare(),
637 const _Sequence& __s = _Sequence())
638 : c(__s), comp(__x)
639 {
640 __glibcxx_requires_valid_range(__first, __last);
641 c.insert(c.end(), __first, __last);
642 std::make_heap(c.begin(), c.end(), comp);
643 }
644#else
645 // _GLIBCXX_RESOLVE_LIB_DEFECTS
646 // 3529. priority_queue(first, last) should construct c with (first, last)
647 template<typename _InputIterator,
648 typename = std::_RequireInputIter<_InputIterator>>
649 priority_queue(_InputIterator __first, _InputIterator __last,
650 const _Compare& __x = _Compare())
651 : c(__first, __last), comp(__x)
652 { std::make_heap(c.begin(), c.end(), comp); }
653
654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
655 // 3522. Missing requirement on InputIterator template parameter
656 template<typename _InputIterator,
657 typename = std::_RequireInputIter<_InputIterator>>
658 priority_queue(_InputIterator __first, _InputIterator __last,
659 const _Compare& __x, const _Sequence& __s)
660 : c(__s), comp(__x)
661 {
662 __glibcxx_requires_valid_range(__first, __last);
663 c.insert(c.end(), __first, __last);
664 std::make_heap(c.begin(), c.end(), comp);
665 }
666
667 template<typename _InputIterator,
668 typename = std::_RequireInputIter<_InputIterator>>
669 priority_queue(_InputIterator __first, _InputIterator __last,
670 const _Compare& __x, _Sequence&& __s)
671 : c(std::move(__s)), comp(__x)
672 {
673 __glibcxx_requires_valid_range(__first, __last);
674 c.insert(c.end(), __first, __last);
675 std::make_heap(c.begin(), c.end(), comp);
676 }
677
678 // _GLIBCXX_RESOLVE_LIB_DEFECTS
679 // 3506. Missing allocator-extended constructors for priority_queue
680 template<typename _InputIterator, typename _Alloc,
681 typename = std::_RequireInputIter<_InputIterator>,
682 typename _Requires = _Uses<_Alloc>>
683 priority_queue(_InputIterator __first, _InputIterator __last,
684 const _Alloc& __alloc)
685 : c(__first, __last, __alloc), comp()
686 { std::make_heap(c.begin(), c.end(), comp); }
687
688 template<typename _InputIterator, typename _Alloc,
689 typename = std::_RequireInputIter<_InputIterator>,
690 typename _Requires = _Uses<_Alloc>>
691 priority_queue(_InputIterator __first, _InputIterator __last,
692 const _Compare& __x, const _Alloc& __alloc)
693 : c(__first, __last, __alloc), comp(__x)
694 { std::make_heap(c.begin(), c.end(), comp); }
695
696 template<typename _InputIterator, typename _Alloc,
697 typename = std::_RequireInputIter<_InputIterator>,
698 typename _Requires = _Uses<_Alloc>>
699 priority_queue(_InputIterator __first, _InputIterator __last,
700 const _Compare& __x, const _Sequence& __s,
701 const _Alloc& __alloc)
702 : c(__s, __alloc), comp(__x)
703 {
704 __glibcxx_requires_valid_range(__first, __last);
705 c.insert(c.end(), __first, __last);
706 std::make_heap(c.begin(), c.end(), comp);
707 }
708
709 template<typename _InputIterator, typename _Alloc,
710 typename _Requires = _Uses<_Alloc>>
711 priority_queue(_InputIterator __first, _InputIterator __last,
712 const _Compare& __x, _Sequence&& __s,
713 const _Alloc& __alloc)
714 : c(std::move(__s), __alloc), comp(__x)
715 {
716 __glibcxx_requires_valid_range(__first, __last);
717 c.insert(c.end(), __first, __last);
718 std::make_heap(c.begin(), c.end(), comp);
719 }
720#endif
721
722 /**
723 * Returns true if the %queue is empty.
724 */
725 _GLIBCXX_NODISCARD bool
726 empty() const
727 { return c.empty(); }
728
729 /** Returns the number of elements in the %queue. */
730 _GLIBCXX_NODISCARD
731 size_type
732 size() const
733 { return c.size(); }
734
735 /**
736 * Returns a read-only (constant) reference to the data at the first
737 * element of the %queue.
738 */
739 _GLIBCXX_NODISCARD
740 const_reference
741 top() const
742 {
743 __glibcxx_requires_nonempty();
744 return c.front();
745 }
746
747 /**
748 * @brief Add data to the %queue.
749 * @param __x Data to be added.
750 *
751 * This is a typical %queue operation.
752 * The time complexity of the operation depends on the underlying
753 * sequence.
754 */
755 void
756 push(const value_type& __x)
757 {
758 c.push_back(__x);
759 std::push_heap(c.begin(), c.end(), comp);
760 }
761
762#if __cplusplus >= 201103L
763 void
764 push(value_type&& __x)
765 {
766 c.push_back(std::move(__x));
767 std::push_heap(c.begin(), c.end(), comp);
768 }
769
770 template<typename... _Args>
771 void
772 emplace(_Args&&... __args)
773 {
774 c.emplace_back(std::forward<_Args>(__args)...);
775 std::push_heap(c.begin(), c.end(), comp);
776 }
777#endif
778
779 /**
780 * @brief Removes first element.
781 *
782 * This is a typical %queue operation. It shrinks the %queue
783 * by one. The time complexity of the operation depends on the
784 * underlying sequence.
785 *
786 * Note that no data is returned, and if the first element's
787 * data is needed, it should be retrieved before pop() is
788 * called.
789 */
790 void
792 {
793 __glibcxx_requires_nonempty();
794 std::pop_heap(c.begin(), c.end(), comp);
795 c.pop_back();
796 }
797
798#if __cplusplus >= 201103L
799 void
800 swap(priority_queue& __pq)
801 noexcept(__and_<
802#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
803 __is_nothrow_swappable<_Sequence>,
804#else
805 __is_nothrow_swappable<_Tp>,
806#endif
807 __is_nothrow_swappable<_Compare>
808 >::value)
809 {
810 using std::swap;
811 swap(c, __pq.c);
812 swap(comp, __pq.comp);
813 }
814#endif // __cplusplus >= 201103L
815 };
816
817#if __cpp_deduction_guides >= 201606
818 template<typename _Compare, typename _Container,
819 typename = _RequireNotAllocator<_Compare>,
820 typename = _RequireNotAllocator<_Container>>
821 priority_queue(_Compare, _Container)
822 -> priority_queue<typename _Container::value_type, _Container, _Compare>;
823
824 template<typename _InputIterator, typename _ValT
825 = typename iterator_traits<_InputIterator>::value_type,
826 typename _Compare = less<_ValT>,
827 typename _Container = vector<_ValT>,
828 typename = _RequireInputIter<_InputIterator>,
829 typename = _RequireNotAllocator<_Compare>,
830 typename = _RequireNotAllocator<_Container>>
831 priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(),
832 _Container = _Container())
833 -> priority_queue<_ValT, _Container, _Compare>;
834
835 template<typename _Compare, typename _Container, typename _Allocator,
836 typename = _RequireNotAllocator<_Compare>,
837 typename = _RequireNotAllocator<_Container>>
838 priority_queue(_Compare, _Container, _Allocator)
839 -> priority_queue<typename _Container::value_type, _Container, _Compare>;
840#endif
841
842 // No equality/comparison operators are provided for priority_queue.
843
844#if __cplusplus >= 201103L
845 template<typename _Tp, typename _Sequence, typename _Compare>
846 inline
847#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
848 // Constrained free swap overload, see p0185r1
849 typename enable_if<__and_<__is_swappable<_Sequence>,
850 __is_swappable<_Compare>>::value>::type
851#else
852 void
853#endif
854 swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
855 priority_queue<_Tp, _Sequence, _Compare>& __y)
856 noexcept(noexcept(__x.swap(__y)))
857 { __x.swap(__y); }
858
859 template<typename _Tp, typename _Sequence, typename _Compare,
860 typename _Alloc>
861 struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
862 : public uses_allocator<_Sequence, _Alloc>::type { };
863#endif // __cplusplus >= 201103L
864
865_GLIBCXX_END_NAMESPACE_VERSION
866} // namespace
867
868#endif /* _STL_QUEUE_H */
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
ISO C++ entities toplevel namespace is std.
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
Definition compare:526
Define a member typedef type only if a boolean constant is true.
Definition type_traits:134
is_default_constructible
Definition type_traits:1167
A standard container giving FIFO behavior.
Definition stl_queue.h:97
void push(const value_type &__x)
Add data to the end of the queue.
Definition stl_queue.h:283
_Sequence c
c is the underlying container.
Definition stl_queue.h:153
size_type size() const
Definition stl_queue.h:222
reference front()
Definition stl_queue.h:231
const_reference back() const
Definition stl_queue.h:267
void pop()
Removes first element.
Definition stl_queue.h:316
queue()
Default constructor creates no elements.
Definition stl_queue.h:166
const_reference front() const
Definition stl_queue.h:243
bool empty() const
Definition stl_queue.h:216
reference back()
Definition stl_queue.h:255
A standard container automatically sorting its contents.
Definition stl_queue.h:497
size_type size() const
Definition stl_queue.h:732
bool empty() const
Definition stl_queue.h:726
void pop()
Removes first element.
Definition stl_queue.h:791
const_reference top() const
Definition stl_queue.h:741
priority_queue(_InputIterator __first, _InputIterator __last, const _Compare &__x=_Compare())
Builds a queue from a range.
Definition stl_queue.h:649
void push(const value_type &__x)
Add data to the queue.
Definition stl_queue.h:756
priority_queue()
Default constructor creates no elements.
Definition stl_queue.h:554