1 // Queue implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 // concept requirements
99 typedef typename
_Sequence::value_type _Sequence_value_type
;
100 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
101 __glibcxx_class_requires(_Sequence
, _FrontInsertionSequenceConcept
)
102 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
103 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
105 template<typename _Tp1
, typename _Seq1
>
107 operator==(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
109 template<typename _Tp1
, typename _Seq1
>
111 operator<(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
113 #if __cplusplus >= 201103L
114 template<typename _Alloc
>
115 using _Uses
= typename
116 enable_if
<uses_allocator
<_Sequence
, _Alloc
>::value
>::type
;
120 typedef typename
_Sequence::value_type value_type
;
121 typedef typename
_Sequence::reference reference
;
122 typedef typename
_Sequence::const_reference const_reference
;
123 typedef typename
_Sequence::size_type size_type
;
124 typedef _Sequence container_type
;
128 * 'c' is the underlying container. Maintainers wondering why
129 * this isn't uglified as per style guidelines should note that
130 * this name is specified in the standard, [23.2.3.1]. (Why?
131 * Presumably for the same reason that it's protected instead
132 * of private: to allow derivation. But none of the other
133 * containers allow for derivation. Odd.)
139 * @brief Default constructor creates no elements.
141 #if __cplusplus < 201103L
143 queue(const _Sequence
& __c
= _Sequence())
147 queue(const _Sequence
& __c
)
151 queue(_Sequence
&& __c
= _Sequence())
152 : c(std::move(__c
)) { }
154 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
156 queue(const _Alloc
& __a
)
159 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
160 queue(const _Sequence
& __c
, const _Alloc
& __a
)
163 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
164 queue(_Sequence
&& __c
, const _Alloc
& __a
)
165 : c(std::move(__c
), __a
) { }
167 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
168 queue(const queue
& __q
, const _Alloc
& __a
)
171 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
172 queue(queue
&& __q
, const _Alloc
& __a
)
173 : c(std::move(__q
.c
), __a
) { }
177 * Returns true if the %queue is empty.
181 { return c
.empty(); }
183 /** Returns the number of elements in the %queue. */
189 * Returns a read/write reference to the data at the first
190 * element of the %queue.
195 __glibcxx_requires_nonempty();
200 * Returns a read-only (constant) reference to the data at the first
201 * element of the %queue.
206 __glibcxx_requires_nonempty();
211 * Returns a read/write reference to the data at the last
212 * element of the %queue.
217 __glibcxx_requires_nonempty();
222 * Returns a read-only (constant) reference to the data at the last
223 * element of the %queue.
228 __glibcxx_requires_nonempty();
233 * @brief Add data to the end of the %queue.
234 * @param __x Data to be added.
236 * This is a typical %queue operation. The function creates an
237 * element at the end of the %queue and assigns the given data
238 * to it. The time complexity of the operation depends on the
239 * underlying sequence.
242 push(const value_type
& __x
)
243 { c
.push_back(__x
); }
245 #if __cplusplus >= 201103L
247 push(value_type
&& __x
)
248 { c
.push_back(std::move(__x
)); }
250 template<typename
... _Args
>
252 emplace(_Args
&&... __args
)
253 { c
.emplace_back(std::forward
<_Args
>(__args
)...); }
257 * @brief Removes first element.
259 * This is a typical %queue operation. It shrinks the %queue by one.
260 * The time complexity of the operation depends on the underlying
263 * Note that no data is returned, and if the first element's
264 * data is needed, it should be retrieved before pop() is
270 __glibcxx_requires_nonempty();
274 #if __cplusplus >= 201103L
277 noexcept(__is_nothrow_swappable
<_Tp
>::value
)
286 * @brief Queue equality comparison.
287 * @param __x A %queue.
288 * @param __y A %queue of the same type as @a __x.
289 * @return True iff the size and elements of the queues are equal.
291 * This is an equivalence relation. Complexity and semantics depend on the
292 * underlying sequence type, but the expected rules are: this relation is
293 * linear in the size of the sequences, and queues are considered equivalent
294 * if their sequences compare equal.
296 template<typename _Tp
, typename _Seq
>
298 operator==(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
299 { return __x
.c
== __y
.c
; }
302 * @brief Queue ordering relation.
303 * @param __x A %queue.
304 * @param __y A %queue of the same type as @a x.
305 * @return True iff @a __x is lexicographically less than @a __y.
307 * This is an total ordering relation. Complexity and semantics
308 * depend on the underlying sequence type, but the expected rules
309 * are: this relation is linear in the size of the sequences, the
310 * elements must be comparable with @c <, and
311 * std::lexicographical_compare() is usually used to make the
314 template<typename _Tp
, typename _Seq
>
316 operator<(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
317 { return __x
.c
< __y
.c
; }
319 /// Based on operator==
320 template<typename _Tp
, typename _Seq
>
322 operator!=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
323 { return !(__x
== __y
); }
325 /// Based on operator<
326 template<typename _Tp
, typename _Seq
>
328 operator>(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
329 { return __y
< __x
; }
331 /// Based on operator<
332 template<typename _Tp
, typename _Seq
>
334 operator<=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
335 { return !(__y
< __x
); }
337 /// Based on operator<
338 template<typename _Tp
, typename _Seq
>
340 operator>=(const queue
<_Tp
, _Seq
>& __x
, const queue
<_Tp
, _Seq
>& __y
)
341 { return !(__x
< __y
); }
343 #if __cplusplus >= 201103L
344 template<typename _Tp
, typename _Seq
>
346 swap(queue
<_Tp
, _Seq
>& __x
, queue
<_Tp
, _Seq
>& __y
)
347 noexcept(noexcept(__x
.swap(__y
)))
350 template<typename _Tp
, typename _Seq
, typename _Alloc
>
351 struct uses_allocator
<queue
<_Tp
, _Seq
>, _Alloc
>
352 : public uses_allocator
<_Seq
, _Alloc
>::type
{ };
356 * @brief A standard container automatically sorting its contents.
360 * @tparam _Tp Type of element.
361 * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
362 * @tparam _Compare Comparison function object type, defaults to
363 * less<_Sequence::value_type>.
365 * This is not a true container, but an @e adaptor. It holds
366 * another container, and provides a wrapper interface to that
367 * container. The wrapper is what enforces priority-based sorting
368 * and %queue behavior. Very few of the standard container/sequence
369 * interface requirements are met (e.g., iterators).
371 * The second template parameter defines the type of the underlying
372 * sequence/container. It defaults to std::vector, but it can be
373 * any type that supports @c front(), @c push_back, @c pop_back,
374 * and random-access iterators, such as std::deque or an
375 * appropriate user-defined type.
377 * The third template parameter supplies the means of making
378 * priority comparisons. It defaults to @c less<value_type> but
379 * can be anything defining a strict weak ordering.
381 * Members not found in @a normal containers are @c container_type,
382 * which is a typedef for the second Sequence parameter, and @c
383 * push, @c pop, and @c top, which are standard %queue operations.
385 * @note No equality/comparison operators are provided for
388 * @note Sorting of the elements takes place as they are added to,
389 * and removed from, the %priority_queue using the
390 * %priority_queue's member functions. If you access the elements
391 * by other means, and change their data such that the sorting
392 * order would be different, the %priority_queue will not re-sort
393 * the elements for you. (How could it know to do so?)
395 template<typename _Tp
, typename _Sequence
= vector
<_Tp
>,
396 typename _Compare
= less
<typename
_Sequence::value_type
> >
399 // concept requirements
400 typedef typename
_Sequence::value_type _Sequence_value_type
;
401 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
402 __glibcxx_class_requires(_Sequence
, _SequenceConcept
)
403 __glibcxx_class_requires(_Sequence
, _RandomAccessContainerConcept
)
404 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
405 __glibcxx_class_requires4(_Compare
, bool, _Tp
, _Tp
,
406 _BinaryFunctionConcept
)
408 #if __cplusplus >= 201103L
409 template<typename _Alloc
>
410 using _Uses
= typename
411 enable_if
<uses_allocator
<_Sequence
, _Alloc
>::value
>::type
;
415 typedef typename
_Sequence::value_type value_type
;
416 typedef typename
_Sequence::reference reference
;
417 typedef typename
_Sequence::const_reference const_reference
;
418 typedef typename
_Sequence::size_type size_type
;
419 typedef _Sequence container_type
;
422 // See queue::c for notes on these names.
428 * @brief Default constructor creates no elements.
430 #if __cplusplus < 201103L
432 priority_queue(const _Compare
& __x
= _Compare(),
433 const _Sequence
& __s
= _Sequence())
435 { std::make_heap(c
.begin(), c
.end(), comp
); }
438 priority_queue(const _Compare
& __x
,
439 const _Sequence
& __s
)
441 { std::make_heap(c
.begin(), c
.end(), comp
); }
444 priority_queue(const _Compare
& __x
= _Compare(),
445 _Sequence
&& __s
= _Sequence())
446 : c(std::move(__s
)), comp(__x
)
447 { std::make_heap(c
.begin(), c
.end(), comp
); }
449 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
451 priority_queue(const _Alloc
& __a
)
454 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
455 priority_queue(const _Compare
& __x
, const _Alloc
& __a
)
458 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
459 priority_queue(const _Compare
& __x
, const _Sequence
& __c
,
461 : c(__x
, __c
, __a
) { }
463 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
464 priority_queue(const _Compare
& __x
, _Sequence
&& __c
, const _Alloc
& __a
)
465 : c(__x
, std::move(__c
), __a
) { }
467 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
468 priority_queue(const priority_queue
& __q
, const _Alloc
& __a
)
471 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
472 priority_queue(priority_queue
&& __q
, const _Alloc
& __a
)
473 : c(std::move(__q
.c
), __a
) { }
477 * @brief Builds a %queue from a range.
478 * @param __first An input iterator.
479 * @param __last An input iterator.
480 * @param __x A comparison functor describing a strict weak ordering.
481 * @param __s An initial sequence with which to start.
483 * Begins by copying @a __s, inserting a copy of the elements
484 * from @a [first,last) into the copy of @a __s, then ordering
485 * the copy according to @a __x.
487 * For more information on function objects, see the
488 * documentation on @link functors functor base
491 #if __cplusplus < 201103L
492 template<typename _InputIterator
>
493 priority_queue(_InputIterator __first
, _InputIterator __last
,
494 const _Compare
& __x
= _Compare(),
495 const _Sequence
& __s
= _Sequence())
498 __glibcxx_requires_valid_range(__first
, __last
);
499 c
.insert(c
.end(), __first
, __last
);
500 std::make_heap(c
.begin(), c
.end(), comp
);
503 template<typename _InputIterator
>
504 priority_queue(_InputIterator __first
, _InputIterator __last
,
506 const _Sequence
& __s
)
509 __glibcxx_requires_valid_range(__first
, __last
);
510 c
.insert(c
.end(), __first
, __last
);
511 std::make_heap(c
.begin(), c
.end(), comp
);
514 template<typename _InputIterator
>
515 priority_queue(_InputIterator __first
, _InputIterator __last
,
516 const _Compare
& __x
= _Compare(),
517 _Sequence
&& __s
= _Sequence())
518 : c(std::move(__s
)), comp(__x
)
520 __glibcxx_requires_valid_range(__first
, __last
);
521 c
.insert(c
.end(), __first
, __last
);
522 std::make_heap(c
.begin(), c
.end(), comp
);
527 * Returns true if the %queue is empty.
531 { return c
.empty(); }
533 /** Returns the number of elements in the %queue. */
539 * Returns a read-only (constant) reference to the data at the first
540 * element of the %queue.
545 __glibcxx_requires_nonempty();
550 * @brief Add data to the %queue.
551 * @param __x Data to be added.
553 * This is a typical %queue operation.
554 * The time complexity of the operation depends on the underlying
558 push(const value_type
& __x
)
561 std::push_heap(c
.begin(), c
.end(), comp
);
564 #if __cplusplus >= 201103L
566 push(value_type
&& __x
)
568 c
.push_back(std::move(__x
));
569 std::push_heap(c
.begin(), c
.end(), comp
);
572 template<typename
... _Args
>
574 emplace(_Args
&&... __args
)
576 c
.emplace_back(std::forward
<_Args
>(__args
)...);
577 std::push_heap(c
.begin(), c
.end(), comp
);
582 * @brief Removes first element.
584 * This is a typical %queue operation. It shrinks the %queue
585 * by one. The time complexity of the operation depends on the
586 * underlying sequence.
588 * Note that no data is returned, and if the first element's
589 * data is needed, it should be retrieved before pop() is
595 __glibcxx_requires_nonempty();
596 std::pop_heap(c
.begin(), c
.end(), comp
);
600 #if __cplusplus >= 201103L
602 swap(priority_queue
& __pq
)
603 noexcept(__is_nothrow_swappable
<_Tp
>::value
604 && __is_nothrow_swappable
<_Compare
>::value
)
608 swap(comp
, __pq
.comp
);
613 // No equality/comparison operators are provided for priority_queue.
615 #if __cplusplus >= 201103L
616 template<typename _Tp
, typename _Sequence
, typename _Compare
>
618 swap(priority_queue
<_Tp
, _Sequence
, _Compare
>& __x
,
619 priority_queue
<_Tp
, _Sequence
, _Compare
>& __y
)
620 noexcept(noexcept(__x
.swap(__y
)))
623 template<typename _Tp
, typename _Sequence
, typename _Compare
,
625 struct uses_allocator
<priority_queue
<_Tp
, _Sequence
, _Compare
>, _Alloc
>
626 : public uses_allocator
<_Sequence
, _Alloc
>::type
{ };
629 _GLIBCXX_END_NAMESPACE_VERSION
632 #endif /* _STL_QUEUE_H */