1 // Queue implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/concept_check.h>
66 #include <debug/debug.h>
68 _GLIBCXX_BEGIN_NAMESPACE(std
)
70 // Forward declarations of operators < and ==, needed for friend declaration.
71 template<typename _Tp
, typename _Sequence
= deque
<_Tp
> >
74 template<typename _Tp
, typename _Seq
>
76 operator==(const queue
<_Tp
,_Seq
>&, const queue
<_Tp
,_Seq
>&);
78 template<typename _Tp
, typename _Seq
>
80 operator<(const queue
<_Tp
,_Seq
>&, const queue
<_Tp
,_Seq
>&);
83 * @brief A standard container giving FIFO behavior.
88 * Meets many of the requirements of a
89 * <a href="tables.html#65">container</a>,
90 * but does not define anything to do with iterators. Very few of the
91 * other standard container interfaces are defined.
93 * This is not a true container, but an @e adaptor. It holds another
94 * container, and provides a wrapper interface to that container. The
95 * wrapper is what enforces strict first-in-first-out %queue behavior.
97 * The second template parameter defines the type of the underlying
98 * sequence/container. It defaults to std::deque, but it can be any type
99 * that supports @c front, @c back, @c push_back, and @c pop_front,
100 * such as std::list or an appropriate user-defined type.
102 * Members not found in "normal" containers are @c container_type,
103 * which is a typedef for the second Sequence parameter, and @c push and
104 * @c pop, which are standard %queue/FIFO operations.
106 template<typename _Tp
, typename _Sequence
>
109 // concept requirements
110 typedef typename
_Sequence::value_type _Sequence_value_type
;
111 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
112 __glibcxx_class_requires(_Sequence
, _FrontInsertionSequenceConcept
)
113 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
114 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
116 template<typename _Tp1
, typename _Seq1
>
118 operator==(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
120 template<typename _Tp1
, typename _Seq1
>
122 operator<(const queue
<_Tp1
, _Seq1
>&, const queue
<_Tp1
, _Seq1
>&);
125 typedef typename
_Sequence::value_type value_type
;
126 typedef typename
_Sequence::reference reference
;
127 typedef typename
_Sequence::const_reference const_reference
;
128 typedef typename
_Sequence::size_type size_type
;
129 typedef _Sequence container_type
;
133 * 'c' is the underlying container. Maintainers wondering why
134 * this isn't uglified as per style guidelines should note that
135 * this name is specified in the standard, [23.2.3.1]. (Why?
136 * Presumably for the same reason that it's protected instead
137 * of private: to allow derivation. But none of the other
138 * containers allow for derivation. Odd.)
144 * @brief Default constructor creates no elements.
147 queue(const _Sequence
& __c
= _Sequence()) : c(__c
) {}
150 * Returns true if the %queue is empty.
154 { return c
.empty(); }
156 /** Returns the number of elements in the %queue. */
162 * Returns a read/write reference to the data at the first
163 * element of the %queue.
168 __glibcxx_requires_nonempty();
173 * Returns a read-only (constant) reference to the data at the first
174 * element of the %queue.
179 __glibcxx_requires_nonempty();
184 * Returns a read/write reference to the data at the last
185 * element of the %queue.
190 __glibcxx_requires_nonempty();
195 * Returns a read-only (constant) reference to the data at the last
196 * element of the %queue.
201 __glibcxx_requires_nonempty();
206 * @brief Add data to the end of the %queue.
207 * @param x Data to be added.
209 * This is a typical %queue operation. The function creates an
210 * element at the end of the %queue and assigns the given data
211 * to it. The time complexity of the operation depends on the
212 * underlying sequence.
215 push(const value_type
& __x
)
216 { c
.push_back(__x
); }
219 * @brief Removes first element.
221 * This is a typical %queue operation. It shrinks the %queue by one.
222 * The time complexity of the operation depends on the underlying
225 * Note that no data is returned, and if the first element's
226 * data is needed, it should be retrieved before pop() is
232 __glibcxx_requires_nonempty();
239 * @brief Queue equality comparison.
241 * @param y A %queue of the same type as @a x.
242 * @return True iff the size and elements of the queues are equal.
244 * This is an equivalence relation. Complexity and semantics depend on the
245 * underlying sequence type, but the expected rules are: this relation is
246 * linear in the size of the sequences, and queues are considered equivalent
247 * if their sequences compare equal.
249 template<typename _Tp
, typename _Sequence
>
251 operator==(const queue
<_Tp
,_Sequence
>& __x
,
252 const queue
<_Tp
,_Sequence
>& __y
)
253 { return __x
.c
== __y
.c
; }
256 * @brief Queue ordering relation.
258 * @param y A %queue of the same type as @a x.
259 * @return True iff @a x is lexicographically less than @a y.
261 * This is an total ordering relation. Complexity and semantics
262 * depend on the underlying sequence type, but the expected rules
263 * are: this relation is linear in the size of the sequences, the
264 * elements must be comparable with @c <, and
265 * std::lexicographical_compare() is usually used to make the
268 template<typename _Tp
, typename _Sequence
>
270 operator<(const queue
<_Tp
,_Sequence
>& __x
, const queue
<_Tp
,_Sequence
>& __y
)
271 { return __x
.c
< __y
.c
; }
273 /// Based on operator==
274 template<typename _Tp
, typename _Sequence
>
276 operator!=(const queue
<_Tp
,_Sequence
>& __x
,
277 const queue
<_Tp
,_Sequence
>& __y
)
278 { return !(__x
== __y
); }
280 /// Based on operator<
281 template<typename _Tp
, typename _Sequence
>
283 operator>(const queue
<_Tp
,_Sequence
>& __x
, const queue
<_Tp
,_Sequence
>& __y
)
284 { return __y
< __x
; }
286 /// Based on operator<
287 template<typename _Tp
, typename _Sequence
>
289 operator<=(const queue
<_Tp
,_Sequence
>& __x
,
290 const queue
<_Tp
,_Sequence
>& __y
)
291 { return !(__y
< __x
); }
293 /// Based on operator<
294 template<typename _Tp
, typename _Sequence
>
296 operator>=(const queue
<_Tp
,_Sequence
>& __x
,
297 const queue
<_Tp
,_Sequence
>& __y
)
298 { return !(__x
< __y
); }
301 * @brief A standard container automatically sorting its contents.
303 * @ingroup Containers
306 * This is not a true container, but an @e adaptor. It holds
307 * another container, and provides a wrapper interface to that
308 * container. The wrapper is what enforces priority-based sorting
309 * and %queue behavior. Very few of the standard container/sequence
310 * interface requirements are met (e.g., iterators).
312 * The second template parameter defines the type of the underlying
313 * sequence/container. It defaults to std::vector, but it can be
314 * any type that supports @c front(), @c push_back, @c pop_back,
315 * and random-access iterators, such as std::deque or an
316 * appropriate user-defined type.
318 * The third template parameter supplies the means of making
319 * priority comparisons. It defaults to @c less<value_type> but
320 * can be anything defining a strict weak ordering.
322 * Members not found in "normal" containers are @c container_type,
323 * which is a typedef for the second Sequence parameter, and @c
324 * push, @c pop, and @c top, which are standard %queue operations.
326 * @note No equality/comparison operators are provided for
329 * @note Sorting of the elements takes place as they are added to,
330 * and removed from, the %priority_queue using the
331 * %priority_queue's member functions. If you access the elements
332 * by other means, and change their data such that the sorting
333 * order would be different, the %priority_queue will not re-sort
334 * the elements for you. (How could it know to do so?)
336 template<typename _Tp
, typename _Sequence
= vector
<_Tp
>,
337 typename _Compare
= less
<typename
_Sequence::value_type
> >
340 // concept requirements
341 typedef typename
_Sequence::value_type _Sequence_value_type
;
342 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
343 __glibcxx_class_requires(_Sequence
, _SequenceConcept
)
344 __glibcxx_class_requires(_Sequence
, _RandomAccessContainerConcept
)
345 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
346 __glibcxx_class_requires4(_Compare
, bool, _Tp
,_Tp
,_BinaryFunctionConcept
)
349 typedef typename
_Sequence::value_type value_type
;
350 typedef typename
_Sequence::reference reference
;
351 typedef typename
_Sequence::const_reference const_reference
;
352 typedef typename
_Sequence::size_type size_type
;
353 typedef _Sequence container_type
;
356 // See queue::c for notes on these names.
362 * @brief Default constructor creates no elements.
365 priority_queue(const _Compare
& __x
= _Compare(),
366 const _Sequence
& __s
= _Sequence())
368 { std::make_heap(c
.begin(), c
.end(), comp
); }
371 * @brief Builds a %queue from a range.
372 * @param first An input iterator.
373 * @param last An input iterator.
374 * @param x A comparison functor describing a strict weak ordering.
375 * @param s An initial sequence with which to start.
377 * Begins by copying @a s, inserting a copy of the elements
378 * from @a [first,last) into the copy of @a s, then ordering
379 * the copy according to @a x.
381 * For more information on function objects, see the
382 * documentation on @link s20_3_1_base functor base
385 template<typename _InputIterator
>
386 priority_queue(_InputIterator __first
, _InputIterator __last
,
387 const _Compare
& __x
= _Compare(),
388 const _Sequence
& __s
= _Sequence())
391 __glibcxx_requires_valid_range(__first
, __last
);
392 c
.insert(c
.end(), __first
, __last
);
393 std::make_heap(c
.begin(), c
.end(), comp
);
397 * Returns true if the %queue is empty.
400 empty() const { return c
.empty(); }
402 /** Returns the number of elements in the %queue. */
404 size() const { return c
.size(); }
407 * Returns a read-only (constant) reference to the data at the first
408 * element of the %queue.
413 __glibcxx_requires_nonempty();
418 * @brief Add data to the %queue.
419 * @param x Data to be added.
421 * This is a typical %queue operation.
422 * The time complexity of the operation depends on the underlying
426 push(const value_type
& __x
)
431 std::push_heap(c
.begin(), c
.end(), comp
);
436 __throw_exception_again
;
441 * @brief Removes first element.
443 * This is a typical %queue operation. It shrinks the %queue
444 * by one. The time complexity of the operation depends on the
445 * underlying sequence.
447 * Note that no data is returned, and if the first element's
448 * data is needed, it should be retrieved before pop() is
454 __glibcxx_requires_nonempty();
457 std::pop_heap(c
.begin(), c
.end(), comp
);
463 __throw_exception_again
;
468 // No equality/comparison operators are provided for priority_queue.
470 _GLIBCXX_END_NAMESPACE
472 #endif /* _QUEUE_H */