Dead
[official-gcc.git] / gomp-20050608-branch / libstdc++-v3 / include / bits / stl_queue.h
blobe0da22fb21355935ecd375a441689d6531caaf9e
1 // Queue implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
33 * Copyright (c) 1994
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.
57 /** @file stl_queue.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _QUEUE_H
63 #define _QUEUE_H 1
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> >
72 class queue;
74 template<typename _Tp, typename _Seq>
75 inline bool
76 operator==(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
78 template<typename _Tp, typename _Seq>
79 inline bool
80 operator<(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&);
82 /**
83 * @brief A standard container giving FIFO behavior.
85 * @ingroup Containers
86 * @ingroup Sequences
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>
107 class queue
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>
117 friend bool
118 operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
120 template<typename _Tp1, typename _Seq1>
121 friend bool
122 operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
124 public:
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;
131 protected:
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.)
140 _Sequence c;
142 public:
144 * @brief Default constructor creates no elements.
146 explicit
147 queue(const _Sequence& __c = _Sequence()) : c(__c) {}
150 * Returns true if the %queue is empty.
152 bool
153 empty() const
154 { return c.empty(); }
156 /** Returns the number of elements in the %queue. */
157 size_type
158 size() const
159 { return c.size(); }
162 * Returns a read/write reference to the data at the first
163 * element of the %queue.
165 reference
166 front()
168 __glibcxx_requires_nonempty();
169 return c.front();
173 * Returns a read-only (constant) reference to the data at the first
174 * element of the %queue.
176 const_reference
177 front() const
179 __glibcxx_requires_nonempty();
180 return c.front();
184 * Returns a read/write reference to the data at the last
185 * element of the %queue.
187 reference
188 back()
190 __glibcxx_requires_nonempty();
191 return c.back();
195 * Returns a read-only (constant) reference to the data at the last
196 * element of the %queue.
198 const_reference
199 back() const
201 __glibcxx_requires_nonempty();
202 return c.back();
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.
214 void
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
223 * sequence.
225 * Note that no data is returned, and if the first element's
226 * data is needed, it should be retrieved before pop() is
227 * called.
229 void
230 pop()
232 __glibcxx_requires_nonempty();
233 c.pop_front();
239 * @brief Queue equality comparison.
240 * @param x A %queue.
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>
250 inline bool
251 operator==(const queue<_Tp,_Sequence>& __x,
252 const queue<_Tp,_Sequence>& __y)
253 { return __x.c == __y.c; }
256 * @brief Queue ordering relation.
257 * @param x A %queue.
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
266 * determination.
268 template<typename _Tp, typename _Sequence>
269 inline bool
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>
275 inline bool
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>
282 inline bool
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>
288 inline bool
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>
295 inline bool
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
304 * @ingroup Sequences
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
327 * %priority_queue.
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> >
338 class priority_queue
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)
348 public:
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;
355 protected:
356 // See queue::c for notes on these names.
357 _Sequence c;
358 _Compare comp;
360 public:
362 * @brief Default constructor creates no elements.
364 explicit
365 priority_queue(const _Compare& __x = _Compare(),
366 const _Sequence& __s = _Sequence())
367 : c(__s), comp(__x)
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
383 * classes@endlink.
385 template<typename _InputIterator>
386 priority_queue(_InputIterator __first, _InputIterator __last,
387 const _Compare& __x = _Compare(),
388 const _Sequence& __s = _Sequence())
389 : c(__s), comp(__x)
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.
399 bool
400 empty() const { return c.empty(); }
402 /** Returns the number of elements in the %queue. */
403 size_type
404 size() const { return c.size(); }
407 * Returns a read-only (constant) reference to the data at the first
408 * element of the %queue.
410 const_reference
411 top() const
413 __glibcxx_requires_nonempty();
414 return c.front();
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
423 * sequence.
425 void
426 push(const value_type& __x)
430 c.push_back(__x);
431 std::push_heap(c.begin(), c.end(), comp);
433 catch(...)
435 c.clear();
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
449 * called.
451 void
452 pop()
454 __glibcxx_requires_nonempty();
457 std::pop_heap(c.begin(), c.end(), comp);
458 c.pop_back();
460 catch(...)
462 c.clear();
463 __throw_exception_again;
468 // No equality/comparison operators are provided for priority_queue.
470 _GLIBCXX_END_NAMESPACE
472 #endif /* _QUEUE_H */