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