libstdc++
stl_queue.h
Go to the documentation of this file.
1 // Queue implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2024 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 
65 namespace 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
316  pop()
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  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
568  explicit
569  priority_queue(const _Alloc& __a)
570  : c(__a), comp() { }
571 
572  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
573  priority_queue(const _Compare& __x, const _Alloc& __a)
574  : c(__a), comp(__x) { }
575 
576  // _GLIBCXX_RESOLVE_LIB_DEFECTS
577  // 2537. Constructors [...] taking allocators should call make_heap
578  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
579  priority_queue(const _Compare& __x, const _Sequence& __c,
580  const _Alloc& __a)
581  : c(__c, __a), comp(__x)
582  { std::make_heap(c.begin(), c.end(), comp); }
583 
584  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
585  priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
586  : c(std::move(__c), __a), comp(__x)
587  { std::make_heap(c.begin(), c.end(), comp); }
588 
589  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
590  priority_queue(const priority_queue& __q, const _Alloc& __a)
591  : c(__q.c, __a), comp(__q.comp) { }
592 
593  template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
594  priority_queue(priority_queue&& __q, const _Alloc& __a)
595  : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { }
596 #endif
597 
598  /**
599  * @brief Builds a %queue from a range.
600  * @param __first An input iterator.
601  * @param __last An input iterator.
602  * @param __x A comparison functor describing a strict weak ordering.
603  * @param __s An initial sequence with which to start.
604  *
605  * Begins by copying @a __s, inserting a copy of the elements
606  * from @a [first,last) into the copy of @a __s, then ordering
607  * the copy according to @a __x.
608  *
609  * For more information on function objects, see the
610  * documentation on @link functors functor base
611  * classes@endlink.
612  */
613 #if __cplusplus < 201103L
614  template<typename _InputIterator>
615  priority_queue(_InputIterator __first, _InputIterator __last,
616  const _Compare& __x = _Compare(),
617  const _Sequence& __s = _Sequence())
618  : c(__s), comp(__x)
619  {
620  __glibcxx_requires_valid_range(__first, __last);
621  c.insert(c.end(), __first, __last);
622  std::make_heap(c.begin(), c.end(), comp);
623  }
624 #else
625  // _GLIBCXX_RESOLVE_LIB_DEFECTS
626  // 3529. priority_queue(first, last) should construct c with (first, last)
627  template<typename _InputIterator,
628  typename = std::_RequireInputIter<_InputIterator>>
629  priority_queue(_InputIterator __first, _InputIterator __last,
630  const _Compare& __x = _Compare())
631  : c(__first, __last), comp(__x)
632  { std::make_heap(c.begin(), c.end(), comp); }
633 
634  // _GLIBCXX_RESOLVE_LIB_DEFECTS
635  // 3522. Missing requirement on InputIterator template parameter
636  template<typename _InputIterator,
637  typename = std::_RequireInputIter<_InputIterator>>
638  priority_queue(_InputIterator __first, _InputIterator __last,
639  const _Compare& __x, const _Sequence& __s)
640  : c(__s), comp(__x)
641  {
642  __glibcxx_requires_valid_range(__first, __last);
643  c.insert(c.end(), __first, __last);
644  std::make_heap(c.begin(), c.end(), comp);
645  }
646 
647  template<typename _InputIterator,
648  typename = std::_RequireInputIter<_InputIterator>>
649  priority_queue(_InputIterator __first, _InputIterator __last,
650  const _Compare& __x, _Sequence&& __s)
651  : c(std::move(__s)), comp(__x)
652  {
653  __glibcxx_requires_valid_range(__first, __last);
654  c.insert(c.end(), __first, __last);
655  std::make_heap(c.begin(), c.end(), comp);
656  }
657 
658  // _GLIBCXX_RESOLVE_LIB_DEFECTS
659  // 3506. Missing allocator-extended constructors for priority_queue
660  template<typename _InputIterator, typename _Alloc,
661  typename = std::_RequireInputIter<_InputIterator>,
662  typename _Requires = _Uses<_Alloc>>
663  priority_queue(_InputIterator __first, _InputIterator __last,
664  const _Alloc& __alloc)
665  : c(__first, __last, __alloc), comp()
666  { std::make_heap(c.begin(), c.end(), comp); }
667 
668  template<typename _InputIterator, typename _Alloc,
669  typename = std::_RequireInputIter<_InputIterator>,
670  typename _Requires = _Uses<_Alloc>>
671  priority_queue(_InputIterator __first, _InputIterator __last,
672  const _Compare& __x, const _Alloc& __alloc)
673  : c(__first, __last, __alloc), comp(__x)
674  { std::make_heap(c.begin(), c.end(), comp); }
675 
676  template<typename _InputIterator, typename _Alloc,
677  typename = std::_RequireInputIter<_InputIterator>,
678  typename _Requires = _Uses<_Alloc>>
679  priority_queue(_InputIterator __first, _InputIterator __last,
680  const _Compare& __x, const _Sequence& __s,
681  const _Alloc& __alloc)
682  : c(__s, __alloc), comp(__x)
683  {
684  __glibcxx_requires_valid_range(__first, __last);
685  c.insert(c.end(), __first, __last);
686  std::make_heap(c.begin(), c.end(), comp);
687  }
688 
689  template<typename _InputIterator, typename _Alloc,
690  typename _Requires = _Uses<_Alloc>>
691  priority_queue(_InputIterator __first, _InputIterator __last,
692  const _Compare& __x, _Sequence&& __s,
693  const _Alloc& __alloc)
694  : c(std::move(__s), __alloc), comp(__x)
695  {
696  __glibcxx_requires_valid_range(__first, __last);
697  c.insert(c.end(), __first, __last);
698  std::make_heap(c.begin(), c.end(), comp);
699  }
700 #endif
701 
702  /**
703  * Returns true if the %queue is empty.
704  */
705  _GLIBCXX_NODISCARD bool
706  empty() const
707  { return c.empty(); }
708 
709  /** Returns the number of elements in the %queue. */
710  _GLIBCXX_NODISCARD
711  size_type
712  size() const
713  { return c.size(); }
714 
715  /**
716  * Returns a read-only (constant) reference to the data at the first
717  * element of the %queue.
718  */
719  _GLIBCXX_NODISCARD
720  const_reference
721  top() const
722  {
723  __glibcxx_requires_nonempty();
724  return c.front();
725  }
726 
727  /**
728  * @brief Add data to the %queue.
729  * @param __x Data to be added.
730  *
731  * This is a typical %queue operation.
732  * The time complexity of the operation depends on the underlying
733  * sequence.
734  */
735  void
736  push(const value_type& __x)
737  {
738  c.push_back(__x);
739  std::push_heap(c.begin(), c.end(), comp);
740  }
741 
742 #if __cplusplus >= 201103L
743  void
744  push(value_type&& __x)
745  {
746  c.push_back(std::move(__x));
747  std::push_heap(c.begin(), c.end(), comp);
748  }
749 
750  template<typename... _Args>
751  void
752  emplace(_Args&&... __args)
753  {
754  c.emplace_back(std::forward<_Args>(__args)...);
755  std::push_heap(c.begin(), c.end(), comp);
756  }
757 #endif
758 
759  /**
760  * @brief Removes first element.
761  *
762  * This is a typical %queue operation. It shrinks the %queue
763  * by one. The time complexity of the operation depends on the
764  * underlying sequence.
765  *
766  * Note that no data is returned, and if the first element's
767  * data is needed, it should be retrieved before pop() is
768  * called.
769  */
770  void
771  pop()
772  {
773  __glibcxx_requires_nonempty();
774  std::pop_heap(c.begin(), c.end(), comp);
775  c.pop_back();
776  }
777 
778 #if __cplusplus >= 201103L
779  void
780  swap(priority_queue& __pq)
781  noexcept(__and_<
782 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
783  __is_nothrow_swappable<_Sequence>,
784 #else
785  __is_nothrow_swappable<_Tp>,
786 #endif
787  __is_nothrow_swappable<_Compare>
788  >::value)
789  {
790  using std::swap;
791  swap(c, __pq.c);
792  swap(comp, __pq.comp);
793  }
794 #endif // __cplusplus >= 201103L
795  };
796 
797 #if __cpp_deduction_guides >= 201606
798  template<typename _Compare, typename _Container,
799  typename = _RequireNotAllocator<_Compare>,
800  typename = _RequireNotAllocator<_Container>>
801  priority_queue(_Compare, _Container)
802  -> priority_queue<typename _Container::value_type, _Container, _Compare>;
803 
804  template<typename _InputIterator, typename _ValT
805  = typename iterator_traits<_InputIterator>::value_type,
806  typename _Compare = less<_ValT>,
807  typename _Container = vector<_ValT>,
808  typename = _RequireInputIter<_InputIterator>,
809  typename = _RequireNotAllocator<_Compare>,
810  typename = _RequireNotAllocator<_Container>>
811  priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(),
812  _Container = _Container())
813  -> priority_queue<_ValT, _Container, _Compare>;
814 
815  template<typename _Compare, typename _Container, typename _Allocator,
816  typename = _RequireNotAllocator<_Compare>,
817  typename = _RequireNotAllocator<_Container>>
818  priority_queue(_Compare, _Container, _Allocator)
819  -> priority_queue<typename _Container::value_type, _Container, _Compare>;
820 #endif
821 
822  // No equality/comparison operators are provided for priority_queue.
823 
824 #if __cplusplus >= 201103L
825  template<typename _Tp, typename _Sequence, typename _Compare>
826  inline
827 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
828  // Constrained free swap overload, see p0185r1
829  typename enable_if<__and_<__is_swappable<_Sequence>,
830  __is_swappable<_Compare>>::value>::type
831 #else
832  void
833 #endif
834  swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
835  priority_queue<_Tp, _Sequence, _Compare>& __y)
836  noexcept(noexcept(__x.swap(__y)))
837  { __x.swap(__y); }
838 
839  template<typename _Tp, typename _Sequence, typename _Compare,
840  typename _Alloc>
841  struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
842  : public uses_allocator<_Sequence, _Alloc>::type { };
843 #endif // __cplusplus >= 201103L
844 
845 _GLIBCXX_END_NAMESPACE_VERSION
846 } // namespace
847 
848 #endif /* _STL_QUEUE_H */
ISO C++ entities toplevel namespace is std.
is_same
Definition: type_traits:780
void push(const value_type &__x)
Add data to the queue.
Definition: stl_queue.h:736
void pop()
Removes first element.
Definition: stl_queue.h:316
constexpr void push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Push an element onto a heap using comparison functor.
Definition: stl_heap.h:198
constexpr void pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Pop an element off a heap using comparison functor.
Definition: stl_heap.h:317
bool empty() const
Definition: stl_queue.h:706
size_type size() const
Definition: stl_queue.h:712
priority_queue()
Default constructor creates no elements.
Definition: stl_queue.h:554
_Sequence c
c is the underlying container.
Definition: stl_queue.h:153
A standard container giving FIFO behavior.
Definition: stl_queue.h:96
reference front()
Definition: stl_queue.h:231
size_type size() const
Definition: stl_queue.h:222
A standard container automatically sorting its contents.
Definition: stl_queue.h:496
queue()
Default constructor creates no elements.
Definition: stl_queue.h:166
const_reference front() const
Definition: stl_queue.h:243
const_reference back() const
Definition: stl_queue.h:267
void push(const value_type &__x)
Add data to the end of the queue.
Definition: stl_queue.h:283
priority_queue(_InputIterator __first, _InputIterator __last, const _Compare &__x=_Compare())
Builds a queue from a range.
Definition: stl_queue.h:629
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:128
bool empty() const
Definition: stl_queue.h:216
const_reference top() const
Definition: stl_queue.h:721
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:126
void pop()
Removes first element.
Definition: stl_queue.h:771
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
Definition: compare:520
is_default_constructible
Definition: type_traits:1124
reference back()
Definition: stl_queue.h:255
constexpr void make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Construct a heap over a range using comparison functor.
Definition: stl_heap.h:402