]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Queue implementation -*- C++ -*- |
2 | ||
3cbc7af0 BK |
3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005 |
4 | // Free Software Foundation, Inc. | |
42526146 PE |
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. | |
11 | ||
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. | |
16 | ||
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 | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
42526146 PE |
20 | // USA. |
21 | ||
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. | |
30 | ||
725dc051 BK |
31 | /* |
32 | * | |
33 | * Copyright (c) 1994 | |
34 | * Hewlett-Packard Company | |
35 | * | |
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. | |
43 | * | |
44 | * | |
45 | * Copyright (c) 1996,1997 | |
46 | * Silicon Graphics Computer Systems, Inc. | |
47 | * | |
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. | |
55 | */ | |
56 | ||
729e3d3f PE |
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. | |
725dc051 BK |
60 | */ |
61 | ||
3d7c150e BK |
62 | #ifndef _QUEUE_H |
63 | #define _QUEUE_H 1 | |
725dc051 | 64 | |
30a20a1e | 65 | #include <bits/concept_check.h> |
285b36d6 | 66 | #include <debug/debug.h> |
725dc051 | 67 | |
3cbc7af0 BK |
68 | _GLIBCXX_BEGIN_NAMESPACE(std) |
69 | ||
3971a4d2 | 70 | // Forward declarations of operators < and ==, needed for friend declaration. |
285b36d6 BK |
71 | template<typename _Tp, typename _Sequence = deque<_Tp> > |
72 | class queue; | |
ed6814f7 | 73 | |
285b36d6 | 74 | template<typename _Tp, typename _Seq> |
ed6814f7 | 75 | inline bool |
285b36d6 | 76 | operator==(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&); |
ed6814f7 | 77 | |
285b36d6 | 78 | template<typename _Tp, typename _Seq> |
ed6814f7 | 79 | inline bool |
285b36d6 | 80 | operator<(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&); |
ed6814f7 | 81 | |
4df6abc6 | 82 | /** |
3971a4d2 | 83 | * @brief A standard container giving FIFO behavior. |
4df6abc6 | 84 | * |
3971a4d2 PE |
85 | * @ingroup Containers |
86 | * @ingroup Sequences | |
4df6abc6 | 87 | * |
3971a4d2 PE |
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. | |
4df6abc6 | 92 | * |
3971a4d2 PE |
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. | |
4df6abc6 | 96 | * |
3971a4d2 PE |
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. | |
101 | * | |
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. | |
4df6abc6 | 105 | */ |
285b36d6 | 106 | template<typename _Tp, typename _Sequence> |
3971a4d2 | 107 | class queue |
285b36d6 BK |
108 | { |
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) | |
ed6814f7 | 115 | |
7c920151 | 116 | template<typename _Tp1, typename _Seq1> |
ed6814f7 | 117 | friend bool |
7c920151 | 118 | operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); |
285b36d6 BK |
119 | |
120 | template<typename _Tp1, typename _Seq1> | |
ed6814f7 | 121 | friend bool |
285b36d6 | 122 | operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); |
ed6814f7 | 123 | |
285b36d6 BK |
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; | |
ed6814f7 | 130 | |
285b36d6 BK |
131 | protected: |
132 | /** | |
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.) | |
139 | */ | |
140 | _Sequence c; | |
ed6814f7 | 141 | |
285b36d6 BK |
142 | public: |
143 | /** | |
144 | * @brief Default constructor creates no elements. | |
145 | */ | |
146 | explicit | |
147 | queue(const _Sequence& __c = _Sequence()) : c(__c) {} | |
ed6814f7 | 148 | |
285b36d6 BK |
149 | /** |
150 | * Returns true if the %queue is empty. | |
151 | */ | |
152 | bool | |
7c920151 PC |
153 | empty() const |
154 | { return c.empty(); } | |
ed6814f7 | 155 | |
285b36d6 BK |
156 | /** Returns the number of elements in the %queue. */ |
157 | size_type | |
7c920151 PC |
158 | size() const |
159 | { return c.size(); } | |
ed6814f7 | 160 | |
285b36d6 BK |
161 | /** |
162 | * Returns a read/write reference to the data at the first | |
163 | * element of the %queue. | |
164 | */ | |
165 | reference | |
ed6814f7 BI |
166 | front() |
167 | { | |
285b36d6 | 168 | __glibcxx_requires_nonempty(); |
ed6814f7 | 169 | return c.front(); |
285b36d6 | 170 | } |
ed6814f7 | 171 | |
285b36d6 BK |
172 | /** |
173 | * Returns a read-only (constant) reference to the data at the first | |
174 | * element of the %queue. | |
175 | */ | |
176 | const_reference | |
ed6814f7 BI |
177 | front() const |
178 | { | |
285b36d6 | 179 | __glibcxx_requires_nonempty(); |
ed6814f7 | 180 | return c.front(); |
285b36d6 | 181 | } |
ed6814f7 | 182 | |
285b36d6 BK |
183 | /** |
184 | * Returns a read/write reference to the data at the last | |
185 | * element of the %queue. | |
186 | */ | |
187 | reference | |
ed6814f7 | 188 | back() |
285b36d6 BK |
189 | { |
190 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 191 | return c.back(); |
285b36d6 | 192 | } |
ed6814f7 | 193 | |
285b36d6 BK |
194 | /** |
195 | * Returns a read-only (constant) reference to the data at the last | |
196 | * element of the %queue. | |
197 | */ | |
198 | const_reference | |
ed6814f7 | 199 | back() const |
285b36d6 BK |
200 | { |
201 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 202 | return c.back(); |
285b36d6 | 203 | } |
ed6814f7 | 204 | |
285b36d6 BK |
205 | /** |
206 | * @brief Add data to the end of the %queue. | |
207 | * @param x Data to be added. | |
208 | * | |
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. | |
213 | */ | |
214 | void | |
7c920151 PC |
215 | push(const value_type& __x) |
216 | { c.push_back(__x); } | |
ed6814f7 | 217 | |
285b36d6 BK |
218 | /** |
219 | * @brief Removes first element. | |
220 | * | |
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. | |
224 | * | |
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. | |
228 | */ | |
229 | void | |
ed6814f7 BI |
230 | pop() |
231 | { | |
285b36d6 | 232 | __glibcxx_requires_nonempty(); |
ed6814f7 | 233 | c.pop_front(); |
285b36d6 BK |
234 | } |
235 | }; | |
ed6814f7 BI |
236 | |
237 | ||
4df6abc6 | 238 | /** |
3971a4d2 PE |
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. | |
243 | * | |
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. | |
4df6abc6 | 248 | */ |
285b36d6 | 249 | template<typename _Tp, typename _Sequence> |
ed6814f7 BI |
250 | inline bool |
251 | operator==(const queue<_Tp,_Sequence>& __x, | |
285b36d6 | 252 | const queue<_Tp,_Sequence>& __y) |
3971a4d2 | 253 | { return __x.c == __y.c; } |
ed6814f7 | 254 | |
4df6abc6 | 255 | /** |
3971a4d2 PE |
256 | * @brief Queue ordering relation. |
257 | * @param x A %queue. | |
258 | * @param y A %queue of the same type as @a x. | |
9536ca34 | 259 | * @return True iff @a x is lexicographically less than @a y. |
4df6abc6 | 260 | * |
285b36d6 BK |
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 | |
3971a4d2 | 266 | * determination. |
4df6abc6 | 267 | */ |
285b36d6 | 268 | template<typename _Tp, typename _Sequence> |
3971a4d2 PE |
269 | inline bool |
270 | operator<(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) | |
271 | { return __x.c < __y.c; } | |
ed6814f7 | 272 | |
3971a4d2 | 273 | /// Based on operator== |
285b36d6 | 274 | template<typename _Tp, typename _Sequence> |
3971a4d2 | 275 | inline bool |
ed6814f7 | 276 | operator!=(const queue<_Tp,_Sequence>& __x, |
285b36d6 | 277 | const queue<_Tp,_Sequence>& __y) |
3971a4d2 | 278 | { return !(__x == __y); } |
ed6814f7 | 279 | |
3971a4d2 | 280 | /// Based on operator< |
285b36d6 | 281 | template<typename _Tp, typename _Sequence> |
ed6814f7 | 282 | inline bool |
3971a4d2 PE |
283 | operator>(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) |
284 | { return __y < __x; } | |
ed6814f7 | 285 | |
3971a4d2 | 286 | /// Based on operator< |
285b36d6 | 287 | template<typename _Tp, typename _Sequence> |
ed6814f7 BI |
288 | inline bool |
289 | operator<=(const queue<_Tp,_Sequence>& __x, | |
285b36d6 | 290 | const queue<_Tp,_Sequence>& __y) |
3971a4d2 | 291 | { return !(__y < __x); } |
ed6814f7 | 292 | |
3971a4d2 | 293 | /// Based on operator< |
285b36d6 | 294 | template<typename _Tp, typename _Sequence> |
ed6814f7 BI |
295 | inline bool |
296 | operator>=(const queue<_Tp,_Sequence>& __x, | |
285b36d6 | 297 | const queue<_Tp,_Sequence>& __y) |
3971a4d2 | 298 | { return !(__x < __y); } |
ed6814f7 | 299 | |
4df6abc6 | 300 | /** |
3971a4d2 | 301 | * @brief A standard container automatically sorting its contents. |
4df6abc6 | 302 | * |
3971a4d2 PE |
303 | * @ingroup Containers |
304 | * @ingroup Sequences | |
4df6abc6 | 305 | * |
285b36d6 BK |
306 | * This is not a true container, but an @e adaptor. It holds |
307 | * another container, and provides a wrapper interface to that | |
3a498393 PC |
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). | |
3971a4d2 PE |
311 | * |
312 | * The second template parameter defines the type of the underlying | |
285b36d6 BK |
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. | |
3971a4d2 | 317 | * |
285b36d6 BK |
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. | |
3971a4d2 PE |
321 | * |
322 | * Members not found in "normal" containers are @c container_type, | |
285b36d6 | 323 | * which is a typedef for the second Sequence parameter, and @c |
3a498393 | 324 | * push, @c pop, and @c top, which are standard %queue operations. |
3971a4d2 | 325 | * |
285b36d6 BK |
326 | * @note No equality/comparison operators are provided for |
327 | * %priority_queue. | |
3971a4d2 | 328 | * |
285b36d6 BK |
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?) | |
4df6abc6 | 335 | */ |
285b36d6 | 336 | template<typename _Tp, typename _Sequence = vector<_Tp>, |
7c920151 | 337 | typename _Compare = less<typename _Sequence::value_type> > |
3971a4d2 | 338 | class priority_queue |
3971a4d2 | 339 | { |
285b36d6 BK |
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) | |
ed6814f7 | 347 | |
7c920151 | 348 | public: |
285b36d6 BK |
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; | |
ed6814f7 | 354 | |
285b36d6 BK |
355 | protected: |
356 | // See queue::c for notes on these names. | |
357 | _Sequence c; | |
358 | _Compare comp; | |
ed6814f7 | 359 | |
285b36d6 BK |
360 | public: |
361 | /** | |
362 | * @brief Default constructor creates no elements. | |
363 | */ | |
364 | explicit | |
ed6814f7 BI |
365 | priority_queue(const _Compare& __x = _Compare(), |
366 | const _Sequence& __s = _Sequence()) | |
367 | : c(__s), comp(__x) | |
285b36d6 | 368 | { std::make_heap(c.begin(), c.end(), comp); } |
ed6814f7 | 369 | |
285b36d6 BK |
370 | /** |
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. | |
ed6814f7 | 376 | * |
285b36d6 BK |
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. | |
380 | * | |
381 | * For more information on function objects, see the | |
382 | * documentation on @link s20_3_1_base functor base | |
383 | * classes@endlink. | |
384 | */ | |
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) | |
ed6814f7 | 390 | { |
285b36d6 BK |
391 | __glibcxx_requires_valid_range(__first, __last); |
392 | c.insert(c.end(), __first, __last); | |
393 | std::make_heap(c.begin(), c.end(), comp); | |
394 | } | |
ed6814f7 | 395 | |
285b36d6 BK |
396 | /** |
397 | * Returns true if the %queue is empty. | |
398 | */ | |
399 | bool | |
400 | empty() const { return c.empty(); } | |
ed6814f7 | 401 | |
285b36d6 BK |
402 | /** Returns the number of elements in the %queue. */ |
403 | size_type | |
404 | size() const { return c.size(); } | |
ed6814f7 | 405 | |
285b36d6 BK |
406 | /** |
407 | * Returns a read-only (constant) reference to the data at the first | |
408 | * element of the %queue. | |
409 | */ | |
410 | const_reference | |
ed6814f7 | 411 | top() const |
285b36d6 BK |
412 | { |
413 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 414 | return c.front(); |
285b36d6 | 415 | } |
ed6814f7 | 416 | |
285b36d6 BK |
417 | /** |
418 | * @brief Add data to the %queue. | |
419 | * @param x Data to be added. | |
420 | * | |
421 | * This is a typical %queue operation. | |
422 | * The time complexity of the operation depends on the underlying | |
423 | * sequence. | |
424 | */ | |
ed6814f7 BI |
425 | void |
426 | push(const value_type& __x) | |
285b36d6 | 427 | { |
ed6814f7 | 428 | try |
3971a4d2 | 429 | { |
ed6814f7 | 430 | c.push_back(__x); |
9dd90ac6 | 431 | std::push_heap(c.begin(), c.end(), comp); |
3971a4d2 | 432 | } |
285b36d6 | 433 | catch(...) |
3971a4d2 PE |
434 | { |
435 | c.clear(); | |
ed6814f7 | 436 | __throw_exception_again; |
3971a4d2 | 437 | } |
285b36d6 | 438 | } |
ed6814f7 | 439 | |
285b36d6 BK |
440 | /** |
441 | * @brief Removes first element. | |
442 | * | |
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. | |
446 | * | |
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. | |
450 | */ | |
ed6814f7 BI |
451 | void |
452 | pop() | |
285b36d6 BK |
453 | { |
454 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 455 | try |
3971a4d2 | 456 | { |
9dd90ac6 | 457 | std::pop_heap(c.begin(), c.end(), comp); |
3971a4d2 PE |
458 | c.pop_back(); |
459 | } | |
285b36d6 | 460 | catch(...) |
3971a4d2 PE |
461 | { |
462 | c.clear(); | |
ed6814f7 | 463 | __throw_exception_again; |
3971a4d2 | 464 | } |
285b36d6 BK |
465 | } |
466 | }; | |
ed6814f7 | 467 | |
3971a4d2 | 468 | // No equality/comparison operators are provided for priority_queue. |
3cbc7af0 BK |
469 | |
470 | _GLIBCXX_END_NAMESPACE | |
725dc051 | 471 | |
3d7c150e | 472 | #endif /* _QUEUE_H */ |