1 // Queue implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
57 #define _STL_QUEUE_H 1
59 #include <bits/concept_check.h>
60 #include <debug/debug.h>
61 #if __cplusplus >= 201103L
62 # include <bits/uses_allocator.h>
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 * @brief A standard container giving FIFO behavior.
74 * @tparam _Tp Type of element.
75 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
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.
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.
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.
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.
95 template<typename _Tp
, typename _Sequence
= deque
<_Tp
> >
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
)
104 __glibcxx_class_requires(_Sequence
, _FrontInsertionSequenceConcept
)
105 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
106 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
109 template<typename _Tp1
, typename _Seq1
>
111 operator==(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
113 template<typename _Tp1
, typename _Seq1
>
115 operator<(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
117 #if __cplusplus >= 201103L
118 template<typename _Alloc
>
119 using _Uses
= typename
120 enable_if
<uses_allocator
<_Sequence
, _Alloc
>::value
>::type
;
124 typedef typename
_Sequence::value_type value_type
;
125 typedef typename
_Sequence::reference reference
;
126 typedef typename
_Sequence::const_reference const_reference
;
127 typedef typename
_Sequence::size_type size_type
;
128 typedef _Sequence container_type
;
131 /* Maintainers wondering why this isn't uglified as per style
132 * guidelines should note that this name is specified in the standard,
134 * (Why? Presumably for the same reason that it's protected instead
135 * of private: to allow derivation. But none of the other
136 * containers allow for derivation. Odd.)
138 /// @c c is the underlying container.
143 * @brief Default constructor creates no elements.
145 #if __cplusplus < 201103L
147 queue(const _Sequence
& __c
= _Sequence())
150 template<typename _Seq
= _Sequence
, typename _Requires
= typename
151 enable_if
<is_default_constructible
<_Seq
>::value
>::type
>
156 queue(const _Sequence
& __c
)
160 queue(_Sequence
&& __c
)
161 : c(std::move(__c
)) { }
163 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
165 queue(const _Alloc
& __a
)
168 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
169 queue(const _Sequence
& __c
, const _Alloc
& __a
)
172 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
173 queue(_Sequence
&& __c
, const _Alloc
& __a
)
174 : c(std::move(__c
), __a
) { }
176 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
177 queue(const queue
& __q
, const _Alloc
& __a
)
180 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
181 queue(queue
&& __q
, const _Alloc
& __a
)
182 : c(std::move(__q
.c
), __a
) { }
186 * Returns true if the %queue is empty.
190 { return c
.empty(); }
192 /** Returns the number of elements in the %queue. */
198 * Returns a read/write reference to the data at the first
199 * element of the %queue.
204 __glibcxx_requires_nonempty();
209 * Returns a read-only (constant) reference to the data at the first
210 * element of the %queue.
215 __glibcxx_requires_nonempty();
220 * Returns a read/write reference to the data at the last
221 * element of the %queue.
226 __glibcxx_requires_nonempty();
231 * Returns a read-only (constant) reference to the data at the last
232 * element of the %queue.
237 __glibcxx_requires_nonempty();
242 * @brief Add data to the end of the %queue.
243 * @param __x Data to be added.
245 * This is a typical %queue operation. The function creates an
246 * element at the end of the %queue and assigns the given data
247 * to it. The time complexity of the operation depends on the
248 * underlying sequence.
251 push(const value_type
& __x
)
252 { c
.push_back(__x
); }
254 #if __cplusplus >= 201103L
256 push(value_type
&& __x
)
257 { c
.push_back(std::move(__x
)); }
259 #if __cplusplus > 201402L
260 template<typename
... _Args
>
262 emplace(_Args
&&... __args
)
263 { return c
.emplace_back(std::forward
<_Args
>(__args
)...); }
265 template<typename
... _Args
>
267 emplace(_Args
&&... __args
)
268 { c
.emplace_back(std::forward
<_Args
>(__args
)...); }
273 * @brief Removes first element.
275 * This is a typical %queue operation. It shrinks the %queue by one.
276 * The time complexity of the operation depends on the underlying
279 * Note that no data is returned, and if the first element's
280 * data is needed, it should be retrieved before pop() is
286 __glibcxx_requires_nonempty();
290 #if __cplusplus >= 201103L
293 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
294 noexcept(__is_nothrow_swappable
<_Sequence
>::value
)
296 noexcept(__is_nothrow_swappable
<_Tp
>::value
)
302 #endif // __cplusplus >= 201103L
306 * @brief Queue equality comparison.
307 * @param __x A %queue.
308 * @param __y A %queue of the same type as @a __x.
309 * @return True iff the size and elements of the queues are equal.
311 * This is an equivalence relation. Complexity and semantics depend on the
312 * underlying sequence type, but the expected rules are: this relation is
313 * linear in the size of the sequences, and queues are considered equivalent
314 * if their sequences compare equal.
316 template<typename _Tp
, typename _Seq
>
318 operator==(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
319 { return __x
.c
== __y
.c
; }
322 * @brief Queue ordering relation.
323 * @param __x A %queue.
324 * @param __y A %queue of the same type as @a x.
325 * @return True iff @a __x is lexicographically less than @a __y.
327 * This is an total ordering relation. Complexity and semantics
328 * depend on the underlying sequence type, but the expected rules
329 * are: this relation is linear in the size of the sequences, the
330 * elements must be comparable with @c <, and
331 * std::lexicographical_compare() is usually used to make the
334 template<typename _Tp
, typename _Seq
>
336 operator<(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
337 { return __x
.c
< __y
.c
; }
339 /// Based on operator==
340 template<typename _Tp
, typename _Seq
>
342 operator!=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
343 { return !(__x
== __y
); }
345 /// Based on operator<
346 template<typename _Tp
, typename _Seq
>
348 operator>(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
349 { return __y
< __x
; }
351 /// Based on operator<
352 template<typename _Tp
, typename _Seq
>
354 operator<=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
355 { return !(__y
< __x
); }
357 /// Based on operator<
358 template<typename _Tp
, typename _Seq
>
360 operator>=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
361 { return !(__x
< __y
); }
363 #if __cplusplus >= 201103L
364 template<typename _Tp
, typename _Seq
>
366 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
367 // Constrained free swap overload, see p0185r1
368 typename enable_if
<__is_swappable
<_Seq
>::value
>::type
372 swap(queue
<_Tp
, _Seq
>& __x
, queue
<_Tp
, _Seq
>& __y
)
373 noexcept(noexcept(__x
.swap(__y
)))
376 template<typename _Tp
, typename _Seq
, typename _Alloc
>
377 struct uses_allocator
<queue
<_Tp
, _Seq
>, _Alloc
>
378 : public uses_allocator
<_Seq
, _Alloc
>::type
{ };
379 #endif // __cplusplus >= 201103L
382 * @brief A standard container automatically sorting its contents.
386 * @tparam _Tp Type of element.
387 * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
388 * @tparam _Compare Comparison function object type, defaults to
389 * less<_Sequence::value_type>.
391 * This is not a true container, but an @e adaptor. It holds
392 * another container, and provides a wrapper interface to that
393 * container. The wrapper is what enforces priority-based sorting
394 * and %queue behavior. Very few of the standard container/sequence
395 * interface requirements are met (e.g., iterators).
397 * The second template parameter defines the type of the underlying
398 * sequence/container. It defaults to std::vector, but it can be
399 * any type that supports @c front(), @c push_back, @c pop_back,
400 * and random-access iterators, such as std::deque or an
401 * appropriate user-defined type.
403 * The third template parameter supplies the means of making
404 * priority comparisons. It defaults to @c less<value_type> but
405 * can be anything defining a strict weak ordering.
407 * Members not found in @a normal containers are @c container_type,
408 * which is a typedef for the second Sequence parameter, and @c
409 * push, @c pop, and @c top, which are standard %queue operations.
411 * @note No equality/comparison operators are provided for
414 * @note Sorting of the elements takes place as they are added to,
415 * and removed from, the %priority_queue using the
416 * %priority_queue's member functions. If you access the elements
417 * by other means, and change their data such that the sorting
418 * order would be different, the %priority_queue will not re-sort
419 * the elements for you. (How could it know to do so?)
421 template<typename _Tp
, typename _Sequence
= vector
<_Tp
>,
422 typename _Compare
= less
<typename
_Sequence::value_type
> >
425 #ifdef _GLIBCXX_CONCEPT_CHECKS
426 // concept requirements
427 typedef typename
_Sequence::value_type _Sequence_value_type
;
428 # if __cplusplus < 201103L
429 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
431 __glibcxx_class_requires(_Sequence
, _SequenceConcept
)
432 __glibcxx_class_requires(_Sequence
, _RandomAccessContainerConcept
)
433 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
434 __glibcxx_class_requires4(_Compare
, bool, _Tp
, _Tp
,
435 _BinaryFunctionConcept
)
438 #if __cplusplus >= 201103L
439 template<typename _Alloc
>
440 using _Uses
= typename
441 enable_if
<uses_allocator
<_Sequence
, _Alloc
>::value
>::type
;
445 typedef typename
_Sequence::value_type value_type
;
446 typedef typename
_Sequence::reference reference
;
447 typedef typename
_Sequence::const_reference const_reference
;
448 typedef typename
_Sequence::size_type size_type
;
449 typedef _Sequence container_type
;
450 // _GLIBCXX_RESOLVE_LIB_DEFECTS
451 // DR 2684. priority_queue lacking comparator typedef
452 typedef _Compare value_compare
;
455 // See queue::c for notes on these names.
461 * @brief Default constructor creates no elements.
463 #if __cplusplus < 201103L
465 priority_queue(const _Compare
& __x
= _Compare(),
466 const _Sequence
& __s
= _Sequence())
468 { std::make_heap(c
.begin(), c
.end(), comp
); }
470 template<typename _Seq
= _Sequence
, typename _Requires
= typename
471 enable_if
<__and_
<is_default_constructible
<_Compare
>,
472 is_default_constructible
<_Seq
>>::value
>::type
>
477 priority_queue(const _Compare
& __x
, const _Sequence
& __s
)
479 { std::make_heap(c
.begin(), c
.end(), comp
); }
482 priority_queue(const _Compare
& __x
, _Sequence
&& __s
= _Sequence())
483 : c(std::move(__s
)), comp(__x
)
484 { std::make_heap(c
.begin(), c
.end(), comp
); }
486 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
488 priority_queue(const _Alloc
& __a
)
491 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
492 priority_queue(const _Compare
& __x
, const _Alloc
& __a
)
493 : c(__a
), comp(__x
) { }
495 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
496 priority_queue(const _Compare
& __x
, const _Sequence
& __c
,
498 : c(__c
, __a
), comp(__x
) { }
500 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
501 priority_queue(const _Compare
& __x
, _Sequence
&& __c
, const _Alloc
& __a
)
502 : c(std::move(__c
), __a
), comp(__x
) { }
504 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
505 priority_queue(const priority_queue
& __q
, const _Alloc
& __a
)
506 : c(__q
.c
, __a
), comp(__q
.comp
) { }
508 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
509 priority_queue(priority_queue
&& __q
, const _Alloc
& __a
)
510 : c(std::move(__q
.c
), __a
), comp(std::move(__q
.comp
)) { }
514 * @brief Builds a %queue from a range.
515 * @param __first An input iterator.
516 * @param __last An input iterator.
517 * @param __x A comparison functor describing a strict weak ordering.
518 * @param __s An initial sequence with which to start.
520 * Begins by copying @a __s, inserting a copy of the elements
521 * from @a [first,last) into the copy of @a __s, then ordering
522 * the copy according to @a __x.
524 * For more information on function objects, see the
525 * documentation on @link functors functor base
528 #if __cplusplus < 201103L
529 template<typename _InputIterator
>
530 priority_queue(_InputIterator __first
, _InputIterator __last
,
531 const _Compare
& __x
= _Compare(),
532 const _Sequence
& __s
= _Sequence())
535 __glibcxx_requires_valid_range(__first
, __last
);
536 c
.insert(c
.end(), __first
, __last
);
537 std::make_heap(c
.begin(), c
.end(), comp
);
540 template<typename _InputIterator
>
541 priority_queue(_InputIterator __first
, _InputIterator __last
,
543 const _Sequence
& __s
)
546 __glibcxx_requires_valid_range(__first
, __last
);
547 c
.insert(c
.end(), __first
, __last
);
548 std::make_heap(c
.begin(), c
.end(), comp
);
551 template<typename _InputIterator
>
552 priority_queue(_InputIterator __first
, _InputIterator __last
,
553 const _Compare
& __x
= _Compare(),
554 _Sequence
&& __s
= _Sequence())
555 : c(std::move(__s
)), comp(__x
)
557 __glibcxx_requires_valid_range(__first
, __last
);
558 c
.insert(c
.end(), __first
, __last
);
559 std::make_heap(c
.begin(), c
.end(), comp
);
564 * Returns true if the %queue is empty.
568 { return c
.empty(); }
570 /** Returns the number of elements in the %queue. */
576 * Returns a read-only (constant) reference to the data at the first
577 * element of the %queue.
582 __glibcxx_requires_nonempty();
587 * @brief Add data to the %queue.
588 * @param __x Data to be added.
590 * This is a typical %queue operation.
591 * The time complexity of the operation depends on the underlying
595 push(const value_type
& __x
)
598 std::push_heap(c
.begin(), c
.end(), comp
);
601 #if __cplusplus >= 201103L
603 push(value_type
&& __x
)
605 c
.push_back(std::move(__x
));
606 std::push_heap(c
.begin(), c
.end(), comp
);
609 template<typename
... _Args
>
611 emplace(_Args
&&... __args
)
613 c
.emplace_back(std::forward
<_Args
>(__args
)...);
614 std::push_heap(c
.begin(), c
.end(), comp
);
619 * @brief Removes first element.
621 * This is a typical %queue operation. It shrinks the %queue
622 * by one. The time complexity of the operation depends on the
623 * underlying sequence.
625 * Note that no data is returned, and if the first element's
626 * data is needed, it should be retrieved before pop() is
632 __glibcxx_requires_nonempty();
633 std::pop_heap(c
.begin(), c
.end(), comp
);
637 #if __cplusplus >= 201103L
639 swap(priority_queue
& __pq
)
641 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
642 __is_nothrow_swappable
<_Sequence
>,
644 __is_nothrow_swappable
<_Tp
>,
646 __is_nothrow_swappable
<_Compare
>
651 swap(comp
, __pq
.comp
);
653 #endif // __cplusplus >= 201103L
656 // No equality/comparison operators are provided for priority_queue.
658 #if __cplusplus >= 201103L
659 template<typename _Tp
, typename _Sequence
, typename _Compare
>
661 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
662 // Constrained free swap overload, see p0185r1
663 typename enable_if
<__and_
<__is_swappable
<_Sequence
>,
664 __is_swappable
<_Compare
>>::value
>::type
668 swap(priority_queue
<_Tp
, _Sequence
, _Compare
>& __x
,
669 priority_queue
<_Tp
, _Sequence
, _Compare
>& __y
)
670 noexcept(noexcept(__x
.swap(__y
)))
673 template<typename _Tp
, typename _Sequence
, typename _Compare
,
675 struct uses_allocator
<priority_queue
<_Tp
, _Sequence
, _Compare
>, _Alloc
>
676 : public uses_allocator
<_Sequence
, _Alloc
>::type
{ };
677 #endif // __cplusplus >= 201103L
679 _GLIBCXX_END_NAMESPACE_VERSION
682 #endif /* _STL_QUEUE_H */