]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_queue.h
* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_queue.h
CommitLineData
42526146
PE
1// Queue implementation -*- C++ -*-
2
999209d0 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
d632488a 4// 2010, 2011, 2012
3cbc7af0 5// Free Software Foundation, Inc.
42526146
PE
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
42526146
PE
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
748086b7
JJ
18// Under Section 7 of GPL version 3, you are granted additional
19// permissions described in the GCC Runtime Library Exception, version
20// 3.1, as published by the Free Software Foundation.
42526146 21
748086b7
JJ
22// You should have received a copy of the GNU General Public License and
23// a copy of the GCC Runtime Library Exception along with this program;
24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25// <http://www.gnu.org/licenses/>.
42526146 26
725dc051
BK
27/*
28 *
29 * Copyright (c) 1994
30 * Hewlett-Packard Company
31 *
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
39 *
40 *
41 * Copyright (c) 1996,1997
42 * Silicon Graphics Computer Systems, Inc.
43 *
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
51 */
52
f910786b 53/** @file bits/stl_queue.h
729e3d3f 54 * This is an internal header file, included by other library headers.
f910786b 55 * Do not attempt to use it directly. @headername{queue}
725dc051
BK
56 */
57
046d30f4
PC
58#ifndef _STL_QUEUE_H
59#define _STL_QUEUE_H 1
725dc051 60
30a20a1e 61#include <bits/concept_check.h>
285b36d6 62#include <debug/debug.h>
725dc051 63
12ffa228
BK
64namespace std _GLIBCXX_VISIBILITY(default)
65{
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 67
4df6abc6 68 /**
3971a4d2 69 * @brief A standard container giving FIFO behavior.
4df6abc6 70 *
aac2878e 71 * @ingroup sequences
4df6abc6 72 *
d632488a
BK
73 * @tparam _Tp Type of element.
74 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
75 *
3971a4d2
PE
76 * Meets many of the requirements of a
77 * <a href="tables.html#65">container</a>,
78 * but does not define anything to do with iterators. Very few of the
79 * other standard container interfaces are defined.
4df6abc6 80 *
3971a4d2
PE
81 * This is not a true container, but an @e adaptor. It holds another
82 * container, and provides a wrapper interface to that container. The
83 * wrapper is what enforces strict first-in-first-out %queue behavior.
4df6abc6 84 *
3971a4d2
PE
85 * The second template parameter defines the type of the underlying
86 * sequence/container. It defaults to std::deque, but it can be any type
87 * that supports @c front, @c back, @c push_back, and @c pop_front,
88 * such as std::list or an appropriate user-defined type.
89 *
2a60a9f6 90 * Members not found in @a normal containers are @c container_type,
3971a4d2
PE
91 * which is a typedef for the second Sequence parameter, and @c push and
92 * @c pop, which are standard %queue/FIFO operations.
4df6abc6 93 */
7ffb61d5 94 template<typename _Tp, typename _Sequence = deque<_Tp> >
3971a4d2 95 class queue
285b36d6
BK
96 {
97 // concept requirements
98 typedef typename _Sequence::value_type _Sequence_value_type;
99 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
100 __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept)
101 __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
102 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
ed6814f7 103
7c920151 104 template<typename _Tp1, typename _Seq1>
ed6814f7 105 friend bool
7c920151 106 operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
285b36d6
BK
107
108 template<typename _Tp1, typename _Seq1>
ed6814f7 109 friend bool
285b36d6 110 operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
ed6814f7 111
285b36d6
BK
112 public:
113 typedef typename _Sequence::value_type value_type;
114 typedef typename _Sequence::reference reference;
115 typedef typename _Sequence::const_reference const_reference;
116 typedef typename _Sequence::size_type size_type;
117 typedef _Sequence container_type;
ed6814f7 118
285b36d6
BK
119 protected:
120 /**
121 * 'c' is the underlying container. Maintainers wondering why
122 * this isn't uglified as per style guidelines should note that
123 * this name is specified in the standard, [23.2.3.1]. (Why?
124 * Presumably for the same reason that it's protected instead
125 * of private: to allow derivation. But none of the other
126 * containers allow for derivation. Odd.)
127 */
128 _Sequence c;
ed6814f7 129
285b36d6
BK
130 public:
131 /**
132 * @brief Default constructor creates no elements.
133 */
734f5023 134#if __cplusplus < 201103L
285b36d6 135 explicit
7aa1cb97
PC
136 queue(const _Sequence& __c = _Sequence())
137 : c(__c) { }
138#else
139 explicit
140 queue(const _Sequence& __c)
141 : c(__c) { }
142
143 explicit
144 queue(_Sequence&& __c = _Sequence())
145 : c(std::move(__c)) { }
7aa1cb97 146#endif
ed6814f7 147
285b36d6
BK
148 /**
149 * Returns true if the %queue is empty.
150 */
151 bool
7c920151
PC
152 empty() const
153 { return c.empty(); }
ed6814f7 154
285b36d6
BK
155 /** Returns the number of elements in the %queue. */
156 size_type
7c920151
PC
157 size() const
158 { return c.size(); }
ed6814f7 159
285b36d6
BK
160 /**
161 * Returns a read/write reference to the data at the first
162 * element of the %queue.
163 */
164 reference
ed6814f7
BI
165 front()
166 {
285b36d6 167 __glibcxx_requires_nonempty();
ed6814f7 168 return c.front();
285b36d6 169 }
ed6814f7 170
285b36d6
BK
171 /**
172 * Returns a read-only (constant) reference to the data at the first
173 * element of the %queue.
174 */
175 const_reference
ed6814f7
BI
176 front() const
177 {
285b36d6 178 __glibcxx_requires_nonempty();
ed6814f7 179 return c.front();
285b36d6 180 }
ed6814f7 181
285b36d6
BK
182 /**
183 * Returns a read/write reference to the data at the last
184 * element of the %queue.
185 */
186 reference
ed6814f7 187 back()
285b36d6
BK
188 {
189 __glibcxx_requires_nonempty();
ed6814f7 190 return c.back();
285b36d6 191 }
ed6814f7 192
285b36d6
BK
193 /**
194 * Returns a read-only (constant) reference to the data at the last
195 * element of the %queue.
196 */
197 const_reference
ed6814f7 198 back() const
285b36d6
BK
199 {
200 __glibcxx_requires_nonempty();
ed6814f7 201 return c.back();
285b36d6 202 }
ed6814f7 203
285b36d6
BK
204 /**
205 * @brief Add data to the end of the %queue.
93c66bc6 206 * @param __x Data to be added.
285b36d6
BK
207 *
208 * This is a typical %queue operation. The function creates an
209 * element at the end of the %queue and assigns the given data
210 * to it. The time complexity of the operation depends on the
211 * underlying sequence.
212 */
213 void
7c920151
PC
214 push(const value_type& __x)
215 { c.push_back(__x); }
4dc3e453 216
734f5023 217#if __cplusplus >= 201103L
4dc3e453
PC
218 void
219 push(value_type&& __x)
220 { c.push_back(std::move(__x)); }
221
c54171fe
PC
222 template<typename... _Args>
223 void
4dc3e453
PC
224 emplace(_Args&&... __args)
225 { c.emplace_back(std::forward<_Args>(__args)...); }
7aa1cb97
PC
226#endif
227
285b36d6
BK
228 /**
229 * @brief Removes first element.
230 *
231 * This is a typical %queue operation. It shrinks the %queue by one.
232 * The time complexity of the operation depends on the underlying
233 * sequence.
234 *
235 * Note that no data is returned, and if the first element's
236 * data is needed, it should be retrieved before pop() is
237 * called.
238 */
239 void
ed6814f7
BI
240 pop()
241 {
285b36d6 242 __glibcxx_requires_nonempty();
ed6814f7 243 c.pop_front();
285b36d6 244 }
ed6814f7 245
734f5023 246#if __cplusplus >= 201103L
7aa1cb97 247 void
ff74fd13 248 swap(queue& __q)
c688bbdd 249 noexcept(noexcept(swap(c, __q.c)))
999209d0
PC
250 {
251 using std::swap;
252 swap(c, __q.c);
253 }
7aa1cb97
PC
254#endif
255 };
ed6814f7 256
4df6abc6 257 /**
3971a4d2 258 * @brief Queue equality comparison.
93c66bc6
BK
259 * @param __x A %queue.
260 * @param __y A %queue of the same type as @a __x.
3971a4d2
PE
261 * @return True iff the size and elements of the queues are equal.
262 *
263 * This is an equivalence relation. Complexity and semantics depend on the
264 * underlying sequence type, but the expected rules are: this relation is
265 * linear in the size of the sequences, and queues are considered equivalent
266 * if their sequences compare equal.
4df6abc6 267 */
d508327c 268 template<typename _Tp, typename _Seq>
ed6814f7 269 inline bool
d508327c 270 operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 271 { return __x.c == __y.c; }
ed6814f7 272
4df6abc6 273 /**
3971a4d2 274 * @brief Queue ordering relation.
93c66bc6
BK
275 * @param __x A %queue.
276 * @param __y A %queue of the same type as @a x.
277 * @return True iff @a __x is lexicographically less than @a __y.
4df6abc6 278 *
285b36d6
BK
279 * This is an total ordering relation. Complexity and semantics
280 * depend on the underlying sequence type, but the expected rules
281 * are: this relation is linear in the size of the sequences, the
282 * elements must be comparable with @c <, and
283 * std::lexicographical_compare() is usually used to make the
3971a4d2 284 * determination.
4df6abc6 285 */
d508327c 286 template<typename _Tp, typename _Seq>
3971a4d2 287 inline bool
d508327c 288 operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 289 { return __x.c < __y.c; }
ed6814f7 290
3971a4d2 291 /// Based on operator==
d508327c 292 template<typename _Tp, typename _Seq>
3971a4d2 293 inline bool
d508327c 294 operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 295 { return !(__x == __y); }
ed6814f7 296
3971a4d2 297 /// Based on operator<
d508327c 298 template<typename _Tp, typename _Seq>
ed6814f7 299 inline bool
d508327c 300 operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 301 { return __y < __x; }
ed6814f7 302
3971a4d2 303 /// Based on operator<
d508327c 304 template<typename _Tp, typename _Seq>
ed6814f7 305 inline bool
d508327c 306 operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 307 { return !(__y < __x); }
ed6814f7 308
3971a4d2 309 /// Based on operator<
d508327c 310 template<typename _Tp, typename _Seq>
ed6814f7 311 inline bool
d508327c 312 operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
3971a4d2 313 { return !(__x < __y); }
ed6814f7 314
734f5023 315#if __cplusplus >= 201103L
7aa1cb97
PC
316 template<typename _Tp, typename _Seq>
317 inline void
318 swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
c688bbdd 319 noexcept(noexcept(__x.swap(__y)))
7aa1cb97 320 { __x.swap(__y); }
aa2b7414
PC
321
322 template<typename _Tp, typename _Seq, typename _Alloc>
323 struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
324 : public uses_allocator<_Seq, _Alloc>::type { };
7aa1cb97
PC
325#endif
326
4df6abc6 327 /**
3971a4d2 328 * @brief A standard container automatically sorting its contents.
4df6abc6 329 *
aac2878e 330 * @ingroup sequences
4df6abc6 331 *
d632488a
BK
332 * @tparam _Tp Type of element.
333 * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
334 * @tparam _Compare Comparison function object type, defaults to
335 * less<_Sequence::value_type>.
336 *
285b36d6
BK
337 * This is not a true container, but an @e adaptor. It holds
338 * another container, and provides a wrapper interface to that
3a498393
PC
339 * container. The wrapper is what enforces priority-based sorting
340 * and %queue behavior. Very few of the standard container/sequence
341 * interface requirements are met (e.g., iterators).
3971a4d2
PE
342 *
343 * The second template parameter defines the type of the underlying
285b36d6
BK
344 * sequence/container. It defaults to std::vector, but it can be
345 * any type that supports @c front(), @c push_back, @c pop_back,
346 * and random-access iterators, such as std::deque or an
347 * appropriate user-defined type.
3971a4d2 348 *
285b36d6
BK
349 * The third template parameter supplies the means of making
350 * priority comparisons. It defaults to @c less<value_type> but
351 * can be anything defining a strict weak ordering.
3971a4d2 352 *
2a60a9f6 353 * Members not found in @a normal containers are @c container_type,
285b36d6 354 * which is a typedef for the second Sequence parameter, and @c
3a498393 355 * push, @c pop, and @c top, which are standard %queue operations.
3971a4d2 356 *
285b36d6
BK
357 * @note No equality/comparison operators are provided for
358 * %priority_queue.
3971a4d2 359 *
285b36d6
BK
360 * @note Sorting of the elements takes place as they are added to,
361 * and removed from, the %priority_queue using the
362 * %priority_queue's member functions. If you access the elements
363 * by other means, and change their data such that the sorting
364 * order would be different, the %priority_queue will not re-sort
365 * the elements for you. (How could it know to do so?)
4df6abc6 366 */
285b36d6 367 template<typename _Tp, typename _Sequence = vector<_Tp>,
7c920151 368 typename _Compare = less<typename _Sequence::value_type> >
3971a4d2 369 class priority_queue
3971a4d2 370 {
285b36d6
BK
371 // concept requirements
372 typedef typename _Sequence::value_type _Sequence_value_type;
373 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
374 __glibcxx_class_requires(_Sequence, _SequenceConcept)
375 __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
376 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
d508327c
PC
377 __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
378 _BinaryFunctionConcept)
ed6814f7 379
7c920151 380 public:
285b36d6
BK
381 typedef typename _Sequence::value_type value_type;
382 typedef typename _Sequence::reference reference;
383 typedef typename _Sequence::const_reference const_reference;
384 typedef typename _Sequence::size_type size_type;
385 typedef _Sequence container_type;
ed6814f7 386
285b36d6
BK
387 protected:
388 // See queue::c for notes on these names.
389 _Sequence c;
390 _Compare comp;
ed6814f7 391
285b36d6
BK
392 public:
393 /**
394 * @brief Default constructor creates no elements.
395 */
734f5023 396#if __cplusplus < 201103L
285b36d6 397 explicit
ed6814f7
BI
398 priority_queue(const _Compare& __x = _Compare(),
399 const _Sequence& __s = _Sequence())
400 : c(__s), comp(__x)
285b36d6 401 { std::make_heap(c.begin(), c.end(), comp); }
7aa1cb97
PC
402#else
403 explicit
404 priority_queue(const _Compare& __x,
405 const _Sequence& __s)
406 : c(__s), comp(__x)
407 { std::make_heap(c.begin(), c.end(), comp); }
408
409 explicit
410 priority_queue(const _Compare& __x = _Compare(),
411 _Sequence&& __s = _Sequence())
412 : c(std::move(__s)), comp(__x)
413 { std::make_heap(c.begin(), c.end(), comp); }
414#endif
ed6814f7 415
285b36d6
BK
416 /**
417 * @brief Builds a %queue from a range.
93c66bc6
BK
418 * @param __first An input iterator.
419 * @param __last An input iterator.
420 * @param __x A comparison functor describing a strict weak ordering.
421 * @param __s An initial sequence with which to start.
ed6814f7 422 *
93c66bc6
BK
423 * Begins by copying @a __s, inserting a copy of the elements
424 * from @a [first,last) into the copy of @a __s, then ordering
425 * the copy according to @a __x.
285b36d6
BK
426 *
427 * For more information on function objects, see the
aac2878e 428 * documentation on @link functors functor base
285b36d6
BK
429 * classes@endlink.
430 */
734f5023 431#if __cplusplus < 201103L
285b36d6
BK
432 template<typename _InputIterator>
433 priority_queue(_InputIterator __first, _InputIterator __last,
434 const _Compare& __x = _Compare(),
435 const _Sequence& __s = _Sequence())
436 : c(__s), comp(__x)
ed6814f7 437 {
285b36d6
BK
438 __glibcxx_requires_valid_range(__first, __last);
439 c.insert(c.end(), __first, __last);
440 std::make_heap(c.begin(), c.end(), comp);
441 }
7aa1cb97
PC
442#else
443 template<typename _InputIterator>
444 priority_queue(_InputIterator __first, _InputIterator __last,
445 const _Compare& __x,
446 const _Sequence& __s)
447 : c(__s), comp(__x)
448 {
449 __glibcxx_requires_valid_range(__first, __last);
450 c.insert(c.end(), __first, __last);
451 std::make_heap(c.begin(), c.end(), comp);
452 }
453
454 template<typename _InputIterator>
455 priority_queue(_InputIterator __first, _InputIterator __last,
456 const _Compare& __x = _Compare(),
457 _Sequence&& __s = _Sequence())
458 : c(std::move(__s)), comp(__x)
459 {
460 __glibcxx_requires_valid_range(__first, __last);
461 c.insert(c.end(), __first, __last);
462 std::make_heap(c.begin(), c.end(), comp);
463 }
7aa1cb97 464#endif
ed6814f7 465
285b36d6
BK
466 /**
467 * Returns true if the %queue is empty.
468 */
469 bool
d508327c
PC
470 empty() const
471 { return c.empty(); }
ed6814f7 472
285b36d6
BK
473 /** Returns the number of elements in the %queue. */
474 size_type
d508327c
PC
475 size() const
476 { return c.size(); }
ed6814f7 477
285b36d6
BK
478 /**
479 * Returns a read-only (constant) reference to the data at the first
480 * element of the %queue.
481 */
482 const_reference
ed6814f7 483 top() const
285b36d6
BK
484 {
485 __glibcxx_requires_nonempty();
ed6814f7 486 return c.front();
285b36d6 487 }
ed6814f7 488
285b36d6
BK
489 /**
490 * @brief Add data to the %queue.
93c66bc6 491 * @param __x Data to be added.
285b36d6
BK
492 *
493 * This is a typical %queue operation.
494 * The time complexity of the operation depends on the underlying
495 * sequence.
496 */
ed6814f7
BI
497 void
498 push(const value_type& __x)
285b36d6 499 {
8443c250
PC
500 c.push_back(__x);
501 std::push_heap(c.begin(), c.end(), comp);
285b36d6 502 }
4dc3e453 503
734f5023 504#if __cplusplus >= 201103L
4dc3e453
PC
505 void
506 push(value_type&& __x)
507 {
508 c.push_back(std::move(__x));
509 std::push_heap(c.begin(), c.end(), comp);
510 }
511
c54171fe
PC
512 template<typename... _Args>
513 void
4dc3e453
PC
514 emplace(_Args&&... __args)
515 {
516 c.emplace_back(std::forward<_Args>(__args)...);
c54171fe
PC
517 std::push_heap(c.begin(), c.end(), comp);
518 }
7aa1cb97
PC
519#endif
520
285b36d6
BK
521 /**
522 * @brief Removes first element.
523 *
524 * This is a typical %queue operation. It shrinks the %queue
525 * by one. The time complexity of the operation depends on the
526 * underlying sequence.
527 *
528 * Note that no data is returned, and if the first element's
529 * data is needed, it should be retrieved before pop() is
530 * called.
531 */
ed6814f7
BI
532 void
533 pop()
285b36d6
BK
534 {
535 __glibcxx_requires_nonempty();
8443c250
PC
536 std::pop_heap(c.begin(), c.end(), comp);
537 c.pop_back();
285b36d6 538 }
7aa1cb97 539
734f5023 540#if __cplusplus >= 201103L
7aa1cb97 541 void
ff74fd13 542 swap(priority_queue& __pq)
c688bbdd 543 noexcept(noexcept(swap(c, __pq.c)) && noexcept(swap(comp, __pq.comp)))
7aa1cb97
PC
544 {
545 using std::swap;
999209d0 546 swap(c, __pq.c);
7aa1cb97
PC
547 swap(comp, __pq.comp);
548 }
549#endif
285b36d6 550 };
ed6814f7 551
3971a4d2 552 // No equality/comparison operators are provided for priority_queue.
3cbc7af0 553
734f5023 554#if __cplusplus >= 201103L
7aa1cb97
PC
555 template<typename _Tp, typename _Sequence, typename _Compare>
556 inline void
557 swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
558 priority_queue<_Tp, _Sequence, _Compare>& __y)
c688bbdd 559 noexcept(noexcept(__x.swap(__y)))
7aa1cb97 560 { __x.swap(__y); }
aa2b7414
PC
561
562 template<typename _Tp, typename _Sequence, typename _Compare,
563 typename _Alloc>
564 struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
565 : public uses_allocator<_Sequence, _Alloc>::type { };
7aa1cb97
PC
566#endif
567
12ffa228
BK
568_GLIBCXX_END_NAMESPACE_VERSION
569} // namespace
725dc051 570
046d30f4 571#endif /* _STL_QUEUE_H */