stl_queue.h 23 KB

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