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