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