1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | #ifndef _STL_QUEUE_H |
57 | #define _STL_QUEUE_H 1 |
58 | |
59 | #include <bits/concept_check.h> |
60 | #include <debug/debug.h> |
61 | #if __cplusplus >= 201103L |
62 | # include <bits/uses_allocator.h> |
63 | #endif |
64 | |
65 | namespace std _GLIBCXX_VISIBILITY(default) |
66 | { |
67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | template<typename _Tp, typename _Sequence = deque<_Tp> > |
96 | class queue |
97 | { |
98 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
99 | |
100 | typedef typename _Sequence::value_type _Sequence_value_type; |
101 | # if __cplusplus < 201103L |
102 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
103 | # endif |
104 | __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept) |
105 | __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) |
106 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) |
107 | #endif |
108 | |
109 | template<typename _Tp1, typename _Seq1> |
110 | friend bool |
111 | operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); |
112 | |
113 | template<typename _Tp1, typename _Seq1> |
114 | friend bool |
115 | operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); |
116 | |
117 | #if __cplusplus >= 201103L |
118 | template<typename _Alloc> |
119 | using _Uses = typename |
120 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; |
121 | #endif |
122 | |
123 | public: |
124 | typedef typename _Sequence::value_type value_type; |
125 | typedef typename _Sequence::reference reference; |
126 | typedef typename _Sequence::const_reference const_reference; |
127 | typedef typename _Sequence::size_type size_type; |
128 | typedef _Sequence container_type; |
129 | |
130 | protected: |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | _Sequence c; |
140 | |
141 | public: |
142 | |
143 | |
144 | |
145 | #if __cplusplus < 201103L |
146 | explicit |
147 | queue(const _Sequence& __c = _Sequence()) |
148 | : c(__c) { } |
149 | #else |
150 | template<typename _Seq = _Sequence, typename _Requires = typename |
151 | enable_if<is_default_constructible<_Seq>::value>::type> |
152 | queue() |
153 | : c() { } |
154 | |
155 | explicit |
156 | queue(const _Sequence& __c) |
157 | : c(__c) { } |
158 | |
159 | explicit |
160 | queue(_Sequence&& __c) |
161 | : c(std::move(__c)) { } |
162 | |
163 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
164 | explicit |
165 | queue(const _Alloc& __a) |
166 | : c(__a) { } |
167 | |
168 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
169 | queue(const _Sequence& __c, const _Alloc& __a) |
170 | : c(__c, __a) { } |
171 | |
172 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
173 | queue(_Sequence&& __c, const _Alloc& __a) |
174 | : c(std::move(__c), __a) { } |
175 | |
176 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
177 | queue(const queue& __q, const _Alloc& __a) |
178 | : c(__q.c, __a) { } |
179 | |
180 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
181 | queue(queue&& __q, const _Alloc& __a) |
182 | : c(std::move(__q.c), __a) { } |
183 | #endif |
184 | |
185 | |
186 | |
187 | |
188 | bool |
189 | empty() const |
190 | { return c.empty(); } |
191 | |
192 | |
193 | size_type |
194 | size() const |
195 | { return c.size(); } |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | reference |
202 | front() |
203 | { |
204 | __glibcxx_requires_nonempty(); |
205 | return c.front(); |
206 | } |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | const_reference |
213 | front() const |
214 | { |
215 | __glibcxx_requires_nonempty(); |
216 | return c.front(); |
217 | } |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | reference |
224 | back() |
225 | { |
226 | __glibcxx_requires_nonempty(); |
227 | return c.back(); |
228 | } |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | const_reference |
235 | back() const |
236 | { |
237 | __glibcxx_requires_nonempty(); |
238 | return c.back(); |
239 | } |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | |
250 | void |
251 | push(const value_type& __x) |
252 | { c.push_back(__x); } |
253 | |
254 | #if __cplusplus >= 201103L |
255 | void |
256 | push(value_type&& __x) |
257 | { c.push_back(std::move(__x)); } |
258 | |
259 | #if __cplusplus > 201402L |
260 | template<typename... _Args> |
261 | decltype(auto) |
262 | emplace(_Args&&... __args) |
263 | { return c.emplace_back(std::forward<_Args>(__args)...); } |
264 | #else |
265 | template<typename... _Args> |
266 | void |
267 | emplace(_Args&&... __args) |
268 | { c.emplace_back(std::forward<_Args>(__args)...); } |
269 | #endif |
270 | #endif |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | void |
284 | pop() |
285 | { |
286 | __glibcxx_requires_nonempty(); |
287 | c.pop_front(); |
288 | } |
289 | |
290 | #if __cplusplus >= 201103L |
291 | void |
292 | swap(queue& __q) |
293 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
294 | noexcept(__is_nothrow_swappable<_Sequence>::value) |
295 | #else |
296 | noexcept(__is_nothrow_swappable<_Tp>::value) |
297 | #endif |
298 | { |
299 | using std::swap; |
300 | swap(c, __q.c); |
301 | } |
302 | #endif |
303 | }; |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | template<typename _Tp, typename _Seq> |
317 | inline bool |
318 | operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
319 | { return __x.c == __y.c; } |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | template<typename _Tp, typename _Seq> |
335 | inline bool |
336 | operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
337 | { return __x.c < __y.c; } |
338 | |
339 | |
340 | template<typename _Tp, typename _Seq> |
341 | inline bool |
342 | operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
343 | { return !(__x == __y); } |
344 | |
345 | |
346 | template<typename _Tp, typename _Seq> |
347 | inline bool |
348 | operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
349 | { return __y < __x; } |
350 | |
351 | |
352 | template<typename _Tp, typename _Seq> |
353 | inline bool |
354 | operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
355 | { return !(__y < __x); } |
356 | |
357 | |
358 | template<typename _Tp, typename _Seq> |
359 | inline bool |
360 | operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) |
361 | { return !(__x < __y); } |
362 | |
363 | #if __cplusplus >= 201103L |
364 | template<typename _Tp, typename _Seq> |
365 | inline |
366 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
367 | |
368 | typename enable_if<__is_swappable<_Seq>::value>::type |
369 | #else |
370 | void |
371 | #endif |
372 | swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y) |
373 | noexcept(noexcept(__x.swap(__y))) |
374 | { __x.swap(__y); } |
375 | |
376 | template<typename _Tp, typename _Seq, typename _Alloc> |
377 | struct uses_allocator<queue<_Tp, _Seq>, _Alloc> |
378 | : public uses_allocator<_Seq, _Alloc>::type { }; |
379 | #endif |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | template<typename _Tp, typename _Sequence = vector<_Tp>, |
422 | typename _Compare = less<typename _Sequence::value_type> > |
423 | class priority_queue |
424 | { |
425 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
426 | |
427 | typedef typename _Sequence::value_type _Sequence_value_type; |
428 | # if __cplusplus < 201103L |
429 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
430 | # endif |
431 | __glibcxx_class_requires(_Sequence, _SequenceConcept) |
432 | __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) |
433 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) |
434 | __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, |
435 | _BinaryFunctionConcept) |
436 | #endif |
437 | |
438 | #if __cplusplus >= 201103L |
439 | template<typename _Alloc> |
440 | using _Uses = typename |
441 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; |
442 | #endif |
443 | |
444 | public: |
445 | typedef typename _Sequence::value_type value_type; |
446 | typedef typename _Sequence::reference reference; |
447 | typedef typename _Sequence::const_reference const_reference; |
448 | typedef typename _Sequence::size_type size_type; |
449 | typedef _Sequence container_type; |
450 | |
451 | |
452 | typedef _Compare value_compare; |
453 | |
454 | protected: |
455 | |
456 | _Sequence c; |
457 | _Compare comp; |
458 | |
459 | public: |
460 | |
461 | |
462 | |
463 | #if __cplusplus < 201103L |
464 | explicit |
465 | priority_queue(const _Compare& __x = _Compare(), |
466 | const _Sequence& __s = _Sequence()) |
467 | : c(__s), comp(__x) |
468 | { std::make_heap(c.begin(), c.end(), comp); } |
469 | #else |
470 | template<typename _Seq = _Sequence, typename _Requires = typename |
471 | enable_if<__and_<is_default_constructible<_Compare>, |
472 | is_default_constructible<_Seq>>::value>::type> |
473 | priority_queue() |
474 | : c(), comp() { } |
475 | |
476 | explicit |
477 | priority_queue(const _Compare& __x, const _Sequence& __s) |
478 | : c(__s), comp(__x) |
479 | { std::make_heap(c.begin(), c.end(), comp); } |
480 | |
481 | explicit |
482 | priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence()) |
483 | : c(std::move(__s)), comp(__x) |
484 | { std::make_heap(c.begin(), c.end(), comp); } |
485 | |
486 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
487 | explicit |
488 | priority_queue(const _Alloc& __a) |
489 | : c(__a), comp() { } |
490 | |
491 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
492 | priority_queue(const _Compare& __x, const _Alloc& __a) |
493 | : c(__a), comp(__x) { } |
494 | |
495 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
496 | priority_queue(const _Compare& __x, const _Sequence& __c, |
497 | const _Alloc& __a) |
498 | : c(__c, __a), comp(__x) { } |
499 | |
500 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
501 | priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a) |
502 | : c(std::move(__c), __a), comp(__x) { } |
503 | |
504 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
505 | priority_queue(const priority_queue& __q, const _Alloc& __a) |
506 | : c(__q.c, __a), comp(__q.comp) { } |
507 | |
508 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
509 | priority_queue(priority_queue&& __q, const _Alloc& __a) |
510 | : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { } |
511 | #endif |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | |
525 | |
526 | |
527 | |
528 | #if __cplusplus < 201103L |
529 | template<typename _InputIterator> |
530 | priority_queue(_InputIterator __first, _InputIterator __last, |
531 | const _Compare& __x = _Compare(), |
532 | const _Sequence& __s = _Sequence()) |
533 | : c(__s), comp(__x) |
534 | { |
535 | __glibcxx_requires_valid_range(__first, __last); |
536 | c.insert(c.end(), __first, __last); |
537 | std::make_heap(c.begin(), c.end(), comp); |
538 | } |
539 | #else |
540 | template<typename _InputIterator> |
541 | priority_queue(_InputIterator __first, _InputIterator __last, |
542 | const _Compare& __x, |
543 | const _Sequence& __s) |
544 | : c(__s), comp(__x) |
545 | { |
546 | __glibcxx_requires_valid_range(__first, __last); |
547 | c.insert(c.end(), __first, __last); |
548 | std::make_heap(c.begin(), c.end(), comp); |
549 | } |
550 | |
551 | template<typename _InputIterator> |
552 | priority_queue(_InputIterator __first, _InputIterator __last, |
553 | const _Compare& __x = _Compare(), |
554 | _Sequence&& __s = _Sequence()) |
555 | : c(std::move(__s)), comp(__x) |
556 | { |
557 | __glibcxx_requires_valid_range(__first, __last); |
558 | c.insert(c.end(), __first, __last); |
559 | std::make_heap(c.begin(), c.end(), comp); |
560 | } |
561 | #endif |
562 | |
563 | |
564 | |
565 | |
566 | bool |
567 | empty() const |
568 | { return c.empty(); } |
569 | |
570 | |
571 | size_type |
572 | size() const |
573 | { return c.size(); } |
574 | |
575 | |
576 | |
577 | |
578 | |
579 | const_reference |
580 | top() const |
581 | { |
582 | __glibcxx_requires_nonempty(); |
583 | return c.front(); |
584 | } |
585 | |
586 | |
587 | |
588 | |
589 | |
590 | |
591 | |
592 | |
593 | |
594 | void |
595 | push(const value_type& __x) |
596 | { |
597 | c.push_back(__x); |
598 | std::push_heap(c.begin(), c.end(), comp); |
599 | } |
600 | |
601 | #if __cplusplus >= 201103L |
602 | void |
603 | push(value_type&& __x) |
604 | { |
605 | c.push_back(std::move(__x)); |
606 | std::push_heap(c.begin(), c.end(), comp); |
607 | } |
608 | |
609 | template<typename... _Args> |
610 | void |
611 | emplace(_Args&&... __args) |
612 | { |
613 | c.emplace_back(std::forward<_Args>(__args)...); |
614 | std::push_heap(c.begin(), c.end(), comp); |
615 | } |
616 | #endif |
617 | |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | |
626 | |
627 | |
628 | |
629 | void |
630 | pop() |
631 | { |
632 | __glibcxx_requires_nonempty(); |
633 | std::pop_heap(c.begin(), c.end(), comp); |
634 | c.pop_back(); |
635 | } |
636 | |
637 | #if __cplusplus >= 201103L |
638 | void |
639 | swap(priority_queue& __pq) |
640 | noexcept(__and_< |
641 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
642 | __is_nothrow_swappable<_Sequence>, |
643 | #else |
644 | __is_nothrow_swappable<_Tp>, |
645 | #endif |
646 | __is_nothrow_swappable<_Compare> |
647 | >::value) |
648 | { |
649 | using std::swap; |
650 | swap(c, __pq.c); |
651 | swap(comp, __pq.comp); |
652 | } |
653 | #endif |
654 | }; |
655 | |
656 | |
657 | |
658 | #if __cplusplus >= 201103L |
659 | template<typename _Tp, typename _Sequence, typename _Compare> |
660 | inline |
661 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
662 | |
663 | typename enable_if<__and_<__is_swappable<_Sequence>, |
664 | __is_swappable<_Compare>>::value>::type |
665 | #else |
666 | void |
667 | #endif |
668 | swap(priority_queue<_Tp, _Sequence, _Compare>& __x, |
669 | priority_queue<_Tp, _Sequence, _Compare>& __y) |
670 | noexcept(noexcept(__x.swap(__y))) |
671 | { __x.swap(__y); } |
672 | |
673 | template<typename _Tp, typename _Sequence, typename _Compare, |
674 | typename _Alloc> |
675 | struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc> |
676 | : public uses_allocator<_Sequence, _Alloc>::type { }; |
677 | #endif |
678 | |
679 | _GLIBCXX_END_NAMESPACE_VERSION |
680 | } |
681 | |
682 | #endif |
683 | |