stl_queue.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // Queue implementation -*- C++ -*-
  2. // Copyright (C) 2001-2019 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. #if __cplusplus >= 201703L
  109. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  110. // 2566. Requirements on the first template parameter of container
  111. // adaptors
  112. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  113. "value_type must be the same as the underlying container");
  114. #endif // C++17
  115. #endif // C++11
  116. public:
  117. typedef typename _Sequence::value_type value_type;
  118. typedef typename _Sequence::reference reference;
  119. typedef typename _Sequence::const_reference const_reference;
  120. typedef typename _Sequence::size_type size_type;
  121. typedef _Sequence container_type;
  122. protected:
  123. /* Maintainers wondering why this isn't uglified as per style
  124. * guidelines should note that this name is specified in the standard,
  125. * C++98 [23.2.3.1].
  126. * (Why? Presumably for the same reason that it's protected instead
  127. * of private: to allow derivation. But none of the other
  128. * containers allow for derivation. Odd.)
  129. */
  130. /// @c c is the underlying container.
  131. _Sequence c;
  132. public:
  133. /**
  134. * @brief Default constructor creates no elements.
  135. */
  136. #if __cplusplus < 201103L
  137. explicit
  138. queue(const _Sequence& __c = _Sequence())
  139. : c(__c) { }
  140. #else
  141. template<typename _Seq = _Sequence, typename _Requires = typename
  142. enable_if<is_default_constructible<_Seq>::value>::type>
  143. queue()
  144. : c() { }
  145. explicit
  146. queue(const _Sequence& __c)
  147. : c(__c) { }
  148. explicit
  149. queue(_Sequence&& __c)
  150. : c(std::move(__c)) { }
  151. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  152. explicit
  153. queue(const _Alloc& __a)
  154. : c(__a) { }
  155. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  156. queue(const _Sequence& __c, const _Alloc& __a)
  157. : c(__c, __a) { }
  158. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  159. queue(_Sequence&& __c, const _Alloc& __a)
  160. : c(std::move(__c), __a) { }
  161. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  162. queue(const queue& __q, const _Alloc& __a)
  163. : c(__q.c, __a) { }
  164. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  165. queue(queue&& __q, const _Alloc& __a)
  166. : c(std::move(__q.c), __a) { }
  167. #endif
  168. /**
  169. * Returns true if the %queue is empty.
  170. */
  171. _GLIBCXX_NODISCARD bool
  172. empty() const
  173. { return c.empty(); }
  174. /** Returns the number of elements in the %queue. */
  175. size_type
  176. size() const
  177. { return c.size(); }
  178. /**
  179. * Returns a read/write reference to the data at the first
  180. * element of the %queue.
  181. */
  182. reference
  183. front()
  184. {
  185. __glibcxx_requires_nonempty();
  186. return c.front();
  187. }
  188. /**
  189. * Returns a read-only (constant) reference to the data at the first
  190. * element of the %queue.
  191. */
  192. const_reference
  193. front() const
  194. {
  195. __glibcxx_requires_nonempty();
  196. return c.front();
  197. }
  198. /**
  199. * Returns a read/write reference to the data at the last
  200. * element of the %queue.
  201. */
  202. reference
  203. back()
  204. {
  205. __glibcxx_requires_nonempty();
  206. return c.back();
  207. }
  208. /**
  209. * Returns a read-only (constant) reference to the data at the last
  210. * element of the %queue.
  211. */
  212. const_reference
  213. back() const
  214. {
  215. __glibcxx_requires_nonempty();
  216. return c.back();
  217. }
  218. /**
  219. * @brief Add data to the end of the %queue.
  220. * @param __x Data to be added.
  221. *
  222. * This is a typical %queue operation. The function creates an
  223. * element at the end of the %queue and assigns the given data
  224. * to it. The time complexity of the operation depends on the
  225. * underlying sequence.
  226. */
  227. void
  228. push(const value_type& __x)
  229. { c.push_back(__x); }
  230. #if __cplusplus >= 201103L
  231. void
  232. push(value_type&& __x)
  233. { c.push_back(std::move(__x)); }
  234. #if __cplusplus > 201402L
  235. template<typename... _Args>
  236. decltype(auto)
  237. emplace(_Args&&... __args)
  238. { return c.emplace_back(std::forward<_Args>(__args)...); }
  239. #else
  240. template<typename... _Args>
  241. void
  242. emplace(_Args&&... __args)
  243. { c.emplace_back(std::forward<_Args>(__args)...); }
  244. #endif
  245. #endif
  246. /**
  247. * @brief Removes first element.
  248. *
  249. * This is a typical %queue operation. It shrinks the %queue by one.
  250. * The time complexity of the operation depends on the underlying
  251. * sequence.
  252. *
  253. * Note that no data is returned, and if the first element's
  254. * data is needed, it should be retrieved before pop() is
  255. * called.
  256. */
  257. void
  258. pop()
  259. {
  260. __glibcxx_requires_nonempty();
  261. c.pop_front();
  262. }
  263. #if __cplusplus >= 201103L
  264. void
  265. swap(queue& __q)
  266. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  267. noexcept(__is_nothrow_swappable<_Sequence>::value)
  268. #else
  269. noexcept(__is_nothrow_swappable<_Tp>::value)
  270. #endif
  271. {
  272. using std::swap;
  273. swap(c, __q.c);
  274. }
  275. #endif // __cplusplus >= 201103L
  276. };
  277. #if __cpp_deduction_guides >= 201606
  278. template<typename _Container,
  279. typename = _RequireNotAllocator<_Container>>
  280. queue(_Container) -> queue<typename _Container::value_type, _Container>;
  281. template<typename _Container, typename _Allocator,
  282. typename = _RequireNotAllocator<_Container>,
  283. typename = _RequireAllocator<_Allocator>>
  284. queue(_Container, _Allocator)
  285. -> queue<typename _Container::value_type, _Container>;
  286. #endif
  287. /**
  288. * @brief Queue equality comparison.
  289. * @param __x A %queue.
  290. * @param __y A %queue of the same type as @a __x.
  291. * @return True iff the size and elements of the queues are equal.
  292. *
  293. * This is an equivalence relation. Complexity and semantics depend on the
  294. * underlying sequence type, but the expected rules are: this relation is
  295. * linear in the size of the sequences, and queues are considered equivalent
  296. * if their sequences compare equal.
  297. */
  298. template<typename _Tp, typename _Seq>
  299. inline bool
  300. operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  301. { return __x.c == __y.c; }
  302. /**
  303. * @brief Queue ordering relation.
  304. * @param __x A %queue.
  305. * @param __y A %queue of the same type as @a x.
  306. * @return True iff @a __x is lexicographically less than @a __y.
  307. *
  308. * This is an total ordering relation. Complexity and semantics
  309. * depend on the underlying sequence type, but the expected rules
  310. * are: this relation is linear in the size of the sequences, the
  311. * elements must be comparable with @c <, and
  312. * std::lexicographical_compare() is usually used to make the
  313. * determination.
  314. */
  315. template<typename _Tp, typename _Seq>
  316. inline bool
  317. operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  318. { return __x.c < __y.c; }
  319. /// Based on operator==
  320. template<typename _Tp, typename _Seq>
  321. inline bool
  322. operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  323. { return !(__x == __y); }
  324. /// Based on operator<
  325. template<typename _Tp, typename _Seq>
  326. inline bool
  327. operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  328. { return __y < __x; }
  329. /// Based on operator<
  330. template<typename _Tp, typename _Seq>
  331. inline bool
  332. operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  333. { return !(__y < __x); }
  334. /// Based on operator<
  335. template<typename _Tp, typename _Seq>
  336. inline bool
  337. operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  338. { return !(__x < __y); }
  339. #if __cplusplus >= 201103L
  340. template<typename _Tp, typename _Seq>
  341. inline
  342. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  343. // Constrained free swap overload, see p0185r1
  344. typename enable_if<__is_swappable<_Seq>::value>::type
  345. #else
  346. void
  347. #endif
  348. swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
  349. noexcept(noexcept(__x.swap(__y)))
  350. { __x.swap(__y); }
  351. template<typename _Tp, typename _Seq, typename _Alloc>
  352. struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
  353. : public uses_allocator<_Seq, _Alloc>::type { };
  354. #endif // __cplusplus >= 201103L
  355. /**
  356. * @brief A standard container automatically sorting its contents.
  357. *
  358. * @ingroup sequences
  359. *
  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. *
  365. * This is not a true container, but an @e adaptor. It holds
  366. * another container, and provides a wrapper interface to that
  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).
  370. *
  371. * The second template parameter defines the type of the underlying
  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.
  376. *
  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.
  380. *
  381. * Members not found in @a normal containers are @c container_type,
  382. * which is a typedef for the second Sequence parameter, and @c
  383. * push, @c pop, and @c top, which are standard %queue operations.
  384. *
  385. * @note No equality/comparison operators are provided for
  386. * %priority_queue.
  387. *
  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?)
  394. */
  395. template<typename _Tp, typename _Sequence = vector<_Tp>,
  396. typename _Compare = less<typename _Sequence::value_type> >
  397. class priority_queue
  398. {
  399. #ifdef _GLIBCXX_CONCEPT_CHECKS
  400. // concept requirements
  401. typedef typename _Sequence::value_type _Sequence_value_type;
  402. # if __cplusplus < 201103L
  403. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  404. # endif
  405. __glibcxx_class_requires(_Sequence, _SequenceConcept)
  406. __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
  407. __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
  408. __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
  409. _BinaryFunctionConcept)
  410. #endif
  411. #if __cplusplus >= 201103L
  412. template<typename _Alloc>
  413. using _Uses = typename
  414. enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
  415. #if __cplusplus >= 201703L
  416. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  417. // 2566. Requirements on the first template parameter of container
  418. // adaptors
  419. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  420. "value_type must be the same as the underlying container");
  421. #endif // C++17
  422. #endif // C++11
  423. public:
  424. typedef typename _Sequence::value_type value_type;
  425. typedef typename _Sequence::reference reference;
  426. typedef typename _Sequence::const_reference const_reference;
  427. typedef typename _Sequence::size_type size_type;
  428. typedef _Sequence container_type;
  429. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  430. // DR 2684. priority_queue lacking comparator typedef
  431. typedef _Compare value_compare;
  432. protected:
  433. // See queue::c for notes on these names.
  434. _Sequence c;
  435. _Compare comp;
  436. public:
  437. /**
  438. * @brief Default constructor creates no elements.
  439. */
  440. #if __cplusplus < 201103L
  441. explicit
  442. priority_queue(const _Compare& __x = _Compare(),
  443. const _Sequence& __s = _Sequence())
  444. : c(__s), comp(__x)
  445. { std::make_heap(c.begin(), c.end(), comp); }
  446. #else
  447. template<typename _Seq = _Sequence, typename _Requires = typename
  448. enable_if<__and_<is_default_constructible<_Compare>,
  449. is_default_constructible<_Seq>>::value>::type>
  450. priority_queue()
  451. : c(), comp() { }
  452. explicit
  453. priority_queue(const _Compare& __x, const _Sequence& __s)
  454. : c(__s), comp(__x)
  455. { std::make_heap(c.begin(), c.end(), comp); }
  456. explicit
  457. priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
  458. : c(std::move(__s)), comp(__x)
  459. { std::make_heap(c.begin(), c.end(), comp); }
  460. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  461. explicit
  462. priority_queue(const _Alloc& __a)
  463. : c(__a), comp() { }
  464. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  465. priority_queue(const _Compare& __x, const _Alloc& __a)
  466. : c(__a), comp(__x) { }
  467. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  468. // 2537. Constructors [...] taking allocators should call make_heap
  469. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  470. priority_queue(const _Compare& __x, const _Sequence& __c,
  471. const _Alloc& __a)
  472. : c(__c, __a), comp(__x)
  473. { std::make_heap(c.begin(), c.end(), comp); }
  474. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  475. priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
  476. : c(std::move(__c), __a), comp(__x)
  477. { std::make_heap(c.begin(), c.end(), comp); }
  478. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  479. priority_queue(const priority_queue& __q, const _Alloc& __a)
  480. : c(__q.c, __a), comp(__q.comp) { }
  481. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  482. priority_queue(priority_queue&& __q, const _Alloc& __a)
  483. : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { }
  484. #endif
  485. /**
  486. * @brief Builds a %queue from a range.
  487. * @param __first An input iterator.
  488. * @param __last An input iterator.
  489. * @param __x A comparison functor describing a strict weak ordering.
  490. * @param __s An initial sequence with which to start.
  491. *
  492. * Begins by copying @a __s, inserting a copy of the elements
  493. * from @a [first,last) into the copy of @a __s, then ordering
  494. * the copy according to @a __x.
  495. *
  496. * For more information on function objects, see the
  497. * documentation on @link functors functor base
  498. * classes@endlink.
  499. */
  500. #if __cplusplus < 201103L
  501. template<typename _InputIterator>
  502. priority_queue(_InputIterator __first, _InputIterator __last,
  503. const _Compare& __x = _Compare(),
  504. const _Sequence& __s = _Sequence())
  505. : c(__s), comp(__x)
  506. {
  507. __glibcxx_requires_valid_range(__first, __last);
  508. c.insert(c.end(), __first, __last);
  509. std::make_heap(c.begin(), c.end(), comp);
  510. }
  511. #else
  512. template<typename _InputIterator>
  513. priority_queue(_InputIterator __first, _InputIterator __last,
  514. const _Compare& __x,
  515. const _Sequence& __s)
  516. : c(__s), comp(__x)
  517. {
  518. __glibcxx_requires_valid_range(__first, __last);
  519. c.insert(c.end(), __first, __last);
  520. std::make_heap(c.begin(), c.end(), comp);
  521. }
  522. template<typename _InputIterator>
  523. priority_queue(_InputIterator __first, _InputIterator __last,
  524. const _Compare& __x = _Compare(),
  525. _Sequence&& __s = _Sequence())
  526. : c(std::move(__s)), comp(__x)
  527. {
  528. __glibcxx_requires_valid_range(__first, __last);
  529. c.insert(c.end(), __first, __last);
  530. std::make_heap(c.begin(), c.end(), comp);
  531. }
  532. #endif
  533. /**
  534. * Returns true if the %queue is empty.
  535. */
  536. _GLIBCXX_NODISCARD bool
  537. empty() const
  538. { return c.empty(); }
  539. /** Returns the number of elements in the %queue. */
  540. size_type
  541. size() const
  542. { return c.size(); }
  543. /**
  544. * Returns a read-only (constant) reference to the data at the first
  545. * element of the %queue.
  546. */
  547. const_reference
  548. top() const
  549. {
  550. __glibcxx_requires_nonempty();
  551. return c.front();
  552. }
  553. /**
  554. * @brief Add data to the %queue.
  555. * @param __x Data to be added.
  556. *
  557. * This is a typical %queue operation.
  558. * The time complexity of the operation depends on the underlying
  559. * sequence.
  560. */
  561. void
  562. push(const value_type& __x)
  563. {
  564. c.push_back(__x);
  565. std::push_heap(c.begin(), c.end(), comp);
  566. }
  567. #if __cplusplus >= 201103L
  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. template<typename... _Args>
  575. void
  576. emplace(_Args&&... __args)
  577. {
  578. c.emplace_back(std::forward<_Args>(__args)...);
  579. std::push_heap(c.begin(), c.end(), comp);
  580. }
  581. #endif
  582. /**
  583. * @brief Removes first element.
  584. *
  585. * This is a typical %queue operation. It shrinks the %queue
  586. * by one. The time complexity of the operation depends on the
  587. * underlying sequence.
  588. *
  589. * Note that no data is returned, and if the first element's
  590. * data is needed, it should be retrieved before pop() is
  591. * called.
  592. */
  593. void
  594. pop()
  595. {
  596. __glibcxx_requires_nonempty();
  597. std::pop_heap(c.begin(), c.end(), comp);
  598. c.pop_back();
  599. }
  600. #if __cplusplus >= 201103L
  601. void
  602. swap(priority_queue& __pq)
  603. noexcept(__and_<
  604. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  605. __is_nothrow_swappable<_Sequence>,
  606. #else
  607. __is_nothrow_swappable<_Tp>,
  608. #endif
  609. __is_nothrow_swappable<_Compare>
  610. >::value)
  611. {
  612. using std::swap;
  613. swap(c, __pq.c);
  614. swap(comp, __pq.comp);
  615. }
  616. #endif // __cplusplus >= 201103L
  617. };
  618. #if __cpp_deduction_guides >= 201606
  619. template<typename _Compare, typename _Container,
  620. typename = _RequireNotAllocator<_Compare>,
  621. typename = _RequireNotAllocator<_Container>>
  622. priority_queue(_Compare, _Container)
  623. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  624. template<typename _InputIterator, typename _ValT
  625. = typename iterator_traits<_InputIterator>::value_type,
  626. typename _Compare = less<_ValT>,
  627. typename _Container = vector<_ValT>,
  628. typename = _RequireInputIter<_InputIterator>,
  629. typename = _RequireNotAllocator<_Compare>,
  630. typename = _RequireNotAllocator<_Container>>
  631. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(),
  632. _Container = _Container())
  633. -> priority_queue<_ValT, _Container, _Compare>;
  634. template<typename _Compare, typename _Container, typename _Allocator,
  635. typename = _RequireNotAllocator<_Compare>,
  636. typename = _RequireNotAllocator<_Container>,
  637. typename = _RequireAllocator<_Allocator>>
  638. priority_queue(_Compare, _Container, _Allocator)
  639. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  640. #endif
  641. // No equality/comparison operators are provided for priority_queue.
  642. #if __cplusplus >= 201103L
  643. template<typename _Tp, typename _Sequence, typename _Compare>
  644. inline
  645. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  646. // Constrained free swap overload, see p0185r1
  647. typename enable_if<__and_<__is_swappable<_Sequence>,
  648. __is_swappable<_Compare>>::value>::type
  649. #else
  650. void
  651. #endif
  652. swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
  653. priority_queue<_Tp, _Sequence, _Compare>& __y)
  654. noexcept(noexcept(__x.swap(__y)))
  655. { __x.swap(__y); }
  656. template<typename _Tp, typename _Sequence, typename _Compare,
  657. typename _Alloc>
  658. struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
  659. : public uses_allocator<_Sequence, _Alloc>::type { };
  660. #endif // __cplusplus >= 201103L
  661. _GLIBCXX_END_NAMESPACE_VERSION
  662. } // namespace
  663. #endif /* _STL_QUEUE_H */