stl_queue.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // Queue implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 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 __cpp_lib_three_way_comparison
  105. template<typename _Tp1, three_way_comparable _Seq1>
  106. friend compare_three_way_result_t<_Seq1>
  107. operator<=>(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&);
  108. #endif
  109. #if __cplusplus >= 201103L
  110. template<typename _Alloc>
  111. using _Uses = typename
  112. enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
  113. #if __cplusplus >= 201703L
  114. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  115. // 2566. Requirements on the first template parameter of container
  116. // adaptors
  117. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  118. "value_type must be the same as the underlying container");
  119. #endif // C++17
  120. #endif // C++11
  121. public:
  122. typedef typename _Sequence::value_type value_type;
  123. typedef typename _Sequence::reference reference;
  124. typedef typename _Sequence::const_reference const_reference;
  125. typedef typename _Sequence::size_type size_type;
  126. typedef _Sequence container_type;
  127. protected:
  128. /* Maintainers wondering why this isn't uglified as per style
  129. * guidelines should note that this name is specified in the standard,
  130. * C++98 [23.2.3.1].
  131. * (Why? 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. /// @c c is the underlying container.
  136. _Sequence c;
  137. public:
  138. /**
  139. * @brief Default constructor creates no elements.
  140. */
  141. #if __cplusplus < 201103L
  142. explicit
  143. queue(const _Sequence& __c = _Sequence())
  144. : c(__c) { }
  145. #else
  146. template<typename _Seq = _Sequence, typename _Requires = typename
  147. enable_if<is_default_constructible<_Seq>::value>::type>
  148. queue()
  149. : c() { }
  150. explicit
  151. queue(const _Sequence& __c)
  152. : c(__c) { }
  153. explicit
  154. queue(_Sequence&& __c)
  155. : c(std::move(__c)) { }
  156. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  157. explicit
  158. queue(const _Alloc& __a)
  159. : c(__a) { }
  160. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  161. queue(const _Sequence& __c, const _Alloc& __a)
  162. : c(__c, __a) { }
  163. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  164. queue(_Sequence&& __c, const _Alloc& __a)
  165. : c(std::move(__c), __a) { }
  166. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  167. queue(const queue& __q, const _Alloc& __a)
  168. : c(__q.c, __a) { }
  169. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  170. queue(queue&& __q, const _Alloc& __a)
  171. : c(std::move(__q.c), __a) { }
  172. #if __cplusplus > 202002L
  173. #define __cpp_lib_adaptor_iterator_pair_constructor 202106L
  174. template<typename _InputIterator,
  175. typename = _RequireInputIter<_InputIterator>>
  176. queue(_InputIterator __first, _InputIterator __last)
  177. : c(__first, __last) { }
  178. template<typename _InputIterator, typename _Alloc,
  179. typename = _RequireInputIter<_InputIterator>,
  180. typename = _Uses<_Alloc>>
  181. queue(_InputIterator __first, _InputIterator __last, const _Alloc& __a)
  182. : c(__first, __last, __a) { }
  183. #endif
  184. #endif
  185. /**
  186. * Returns true if the %queue is empty.
  187. */
  188. _GLIBCXX_NODISCARD bool
  189. empty() const
  190. { return c.empty(); }
  191. /** Returns the number of elements in the %queue. */
  192. _GLIBCXX_NODISCARD
  193. size_type
  194. size() const
  195. { return c.size(); }
  196. /**
  197. * Returns a read/write reference to the data at the first
  198. * element of the %queue.
  199. */
  200. _GLIBCXX_NODISCARD
  201. reference
  202. front()
  203. {
  204. __glibcxx_requires_nonempty();
  205. return c.front();
  206. }
  207. /**
  208. * Returns a read-only (constant) reference to the data at the first
  209. * element of the %queue.
  210. */
  211. _GLIBCXX_NODISCARD
  212. const_reference
  213. front() const
  214. {
  215. __glibcxx_requires_nonempty();
  216. return c.front();
  217. }
  218. /**
  219. * Returns a read/write reference to the data at the last
  220. * element of the %queue.
  221. */
  222. _GLIBCXX_NODISCARD
  223. reference
  224. back()
  225. {
  226. __glibcxx_requires_nonempty();
  227. return c.back();
  228. }
  229. /**
  230. * Returns a read-only (constant) reference to the data at the last
  231. * element of the %queue.
  232. */
  233. _GLIBCXX_NODISCARD
  234. const_reference
  235. back() const
  236. {
  237. __glibcxx_requires_nonempty();
  238. return c.back();
  239. }
  240. /**
  241. * @brief Add data to the end of the %queue.
  242. * @param __x Data to be added.
  243. *
  244. * This is a typical %queue operation. The function creates an
  245. * element at the end of the %queue and assigns the given data
  246. * to it. The time complexity of the operation depends on the
  247. * underlying sequence.
  248. */
  249. void
  250. push(const value_type& __x)
  251. { c.push_back(__x); }
  252. #if __cplusplus >= 201103L
  253. void
  254. push(value_type&& __x)
  255. { c.push_back(std::move(__x)); }
  256. #if __cplusplus > 201402L
  257. template<typename... _Args>
  258. decltype(auto)
  259. emplace(_Args&&... __args)
  260. { return c.emplace_back(std::forward<_Args>(__args)...); }
  261. #else
  262. template<typename... _Args>
  263. void
  264. emplace(_Args&&... __args)
  265. { c.emplace_back(std::forward<_Args>(__args)...); }
  266. #endif
  267. #endif
  268. /**
  269. * @brief Removes first element.
  270. *
  271. * This is a typical %queue operation. It shrinks the %queue by one.
  272. * The time complexity of the operation depends on the underlying
  273. * sequence.
  274. *
  275. * Note that no data is returned, and if the first element's
  276. * data is needed, it should be retrieved before pop() is
  277. * called.
  278. */
  279. void
  280. pop()
  281. {
  282. __glibcxx_requires_nonempty();
  283. c.pop_front();
  284. }
  285. #if __cplusplus >= 201103L
  286. void
  287. swap(queue& __q)
  288. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  289. noexcept(__is_nothrow_swappable<_Sequence>::value)
  290. #else
  291. noexcept(__is_nothrow_swappable<_Tp>::value)
  292. #endif
  293. {
  294. using std::swap;
  295. swap(c, __q.c);
  296. }
  297. #endif // __cplusplus >= 201103L
  298. };
  299. #if __cpp_deduction_guides >= 201606
  300. template<typename _Container,
  301. typename = _RequireNotAllocator<_Container>>
  302. queue(_Container) -> queue<typename _Container::value_type, _Container>;
  303. template<typename _Container, typename _Allocator,
  304. typename = _RequireNotAllocator<_Container>>
  305. queue(_Container, _Allocator)
  306. -> queue<typename _Container::value_type, _Container>;
  307. #ifdef __cpp_lib_adaptor_iterator_pair_constructor
  308. template<typename _InputIterator,
  309. typename _ValT
  310. = typename iterator_traits<_InputIterator>::value_type,
  311. typename = _RequireInputIter<_InputIterator>>
  312. queue(_InputIterator, _InputIterator) -> queue<_ValT>;
  313. template<typename _InputIterator, typename _Allocator,
  314. typename _ValT
  315. = typename iterator_traits<_InputIterator>::value_type,
  316. typename = _RequireInputIter<_InputIterator>,
  317. typename = _RequireAllocator<_Allocator>>
  318. queue(_InputIterator, _InputIterator, _Allocator)
  319. -> queue<_ValT, deque<_ValT, _Allocator>>;
  320. #endif
  321. #endif
  322. /**
  323. * @brief Queue equality comparison.
  324. * @param __x A %queue.
  325. * @param __y A %queue of the same type as @a __x.
  326. * @return True iff the size and elements of the queues are equal.
  327. *
  328. * This is an equivalence relation. Complexity and semantics depend on the
  329. * underlying sequence type, but the expected rules are: this relation is
  330. * linear in the size of the sequences, and queues are considered equivalent
  331. * if their sequences compare equal.
  332. */
  333. template<typename _Tp, typename _Seq>
  334. _GLIBCXX_NODISCARD
  335. inline bool
  336. operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  337. { return __x.c == __y.c; }
  338. /**
  339. * @brief Queue ordering relation.
  340. * @param __x A %queue.
  341. * @param __y A %queue of the same type as @a x.
  342. * @return True iff @a __x is lexicographically less than @a __y.
  343. *
  344. * This is an total ordering relation. Complexity and semantics
  345. * depend on the underlying sequence type, but the expected rules
  346. * are: this relation is linear in the size of the sequences, the
  347. * elements must be comparable with @c <, and
  348. * std::lexicographical_compare() is usually used to make the
  349. * determination.
  350. */
  351. template<typename _Tp, typename _Seq>
  352. _GLIBCXX_NODISCARD
  353. inline bool
  354. operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  355. { return __x.c < __y.c; }
  356. /// Based on operator==
  357. template<typename _Tp, typename _Seq>
  358. _GLIBCXX_NODISCARD
  359. inline bool
  360. operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  361. { return !(__x == __y); }
  362. /// Based on operator<
  363. template<typename _Tp, typename _Seq>
  364. _GLIBCXX_NODISCARD
  365. inline bool
  366. operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  367. { return __y < __x; }
  368. /// Based on operator<
  369. template<typename _Tp, typename _Seq>
  370. _GLIBCXX_NODISCARD
  371. inline bool
  372. operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  373. { return !(__y < __x); }
  374. /// Based on operator<
  375. template<typename _Tp, typename _Seq>
  376. _GLIBCXX_NODISCARD
  377. inline bool
  378. operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  379. { return !(__x < __y); }
  380. #if __cpp_lib_three_way_comparison
  381. template<typename _Tp, three_way_comparable _Seq>
  382. [[nodiscard]]
  383. inline compare_three_way_result_t<_Seq>
  384. operator<=>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
  385. { return __x.c <=> __y.c; }
  386. #endif
  387. #if __cplusplus >= 201103L
  388. template<typename _Tp, typename _Seq>
  389. inline
  390. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  391. // Constrained free swap overload, see p0185r1
  392. typename enable_if<__is_swappable<_Seq>::value>::type
  393. #else
  394. void
  395. #endif
  396. swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
  397. noexcept(noexcept(__x.swap(__y)))
  398. { __x.swap(__y); }
  399. template<typename _Tp, typename _Seq, typename _Alloc>
  400. struct uses_allocator<queue<_Tp, _Seq>, _Alloc>
  401. : public uses_allocator<_Seq, _Alloc>::type { };
  402. #endif // __cplusplus >= 201103L
  403. /**
  404. * @brief A standard container automatically sorting its contents.
  405. *
  406. * @ingroup sequences
  407. *
  408. * @tparam _Tp Type of element.
  409. * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>.
  410. * @tparam _Compare Comparison function object type, defaults to
  411. * less<_Sequence::value_type>.
  412. *
  413. * This is not a true container, but an @e adaptor. It holds
  414. * another container, and provides a wrapper interface to that
  415. * container. The wrapper is what enforces priority-based sorting
  416. * and %queue behavior. Very few of the standard container/sequence
  417. * interface requirements are met (e.g., iterators).
  418. *
  419. * The second template parameter defines the type of the underlying
  420. * sequence/container. It defaults to std::vector, but it can be
  421. * any type that supports @c front(), @c push_back, @c pop_back,
  422. * and random-access iterators, such as std::deque or an
  423. * appropriate user-defined type.
  424. *
  425. * The third template parameter supplies the means of making
  426. * priority comparisons. It defaults to @c less<value_type> but
  427. * can be anything defining a strict weak ordering.
  428. *
  429. * Members not found in @a normal containers are @c container_type,
  430. * which is a typedef for the second Sequence parameter, and @c
  431. * push, @c pop, and @c top, which are standard %queue operations.
  432. *
  433. * @note No equality/comparison operators are provided for
  434. * %priority_queue.
  435. *
  436. * @note Sorting of the elements takes place as they are added to,
  437. * and removed from, the %priority_queue using the
  438. * %priority_queue's member functions. If you access the elements
  439. * by other means, and change their data such that the sorting
  440. * order would be different, the %priority_queue will not re-sort
  441. * the elements for you. (How could it know to do so?)
  442. */
  443. template<typename _Tp, typename _Sequence = vector<_Tp>,
  444. typename _Compare = less<typename _Sequence::value_type> >
  445. class priority_queue
  446. {
  447. #ifdef _GLIBCXX_CONCEPT_CHECKS
  448. // concept requirements
  449. typedef typename _Sequence::value_type _Sequence_value_type;
  450. # if __cplusplus < 201103L
  451. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  452. # endif
  453. __glibcxx_class_requires(_Sequence, _SequenceConcept)
  454. __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept)
  455. __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
  456. __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp,
  457. _BinaryFunctionConcept)
  458. #endif
  459. #if __cplusplus >= 201103L
  460. template<typename _Alloc>
  461. using _Uses = typename
  462. enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
  463. #if __cplusplus >= 201703L
  464. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  465. // 2566. Requirements on the first template parameter of container
  466. // adaptors
  467. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  468. "value_type must be the same as the underlying container");
  469. #endif // C++17
  470. #endif // C++11
  471. public:
  472. typedef typename _Sequence::value_type value_type;
  473. typedef typename _Sequence::reference reference;
  474. typedef typename _Sequence::const_reference const_reference;
  475. typedef typename _Sequence::size_type size_type;
  476. typedef _Sequence container_type;
  477. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  478. // DR 2684. priority_queue lacking comparator typedef
  479. typedef _Compare value_compare;
  480. protected:
  481. // See queue::c for notes on these names.
  482. _Sequence c;
  483. _Compare comp;
  484. public:
  485. /**
  486. * @brief Default constructor creates no elements.
  487. */
  488. #if __cplusplus < 201103L
  489. explicit
  490. priority_queue(const _Compare& __x = _Compare(),
  491. const _Sequence& __s = _Sequence())
  492. : c(__s), comp(__x)
  493. { std::make_heap(c.begin(), c.end(), comp); }
  494. #else
  495. template<typename _Seq = _Sequence, typename _Requires = typename
  496. enable_if<__and_<is_default_constructible<_Compare>,
  497. is_default_constructible<_Seq>>::value>::type>
  498. priority_queue()
  499. : c(), comp() { }
  500. explicit
  501. priority_queue(const _Compare& __x, const _Sequence& __s)
  502. : c(__s), comp(__x)
  503. { std::make_heap(c.begin(), c.end(), comp); }
  504. explicit
  505. priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
  506. : c(std::move(__s)), comp(__x)
  507. { std::make_heap(c.begin(), c.end(), comp); }
  508. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  509. explicit
  510. priority_queue(const _Alloc& __a)
  511. : c(__a), comp() { }
  512. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  513. priority_queue(const _Compare& __x, const _Alloc& __a)
  514. : c(__a), comp(__x) { }
  515. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  516. // 2537. Constructors [...] taking allocators should call make_heap
  517. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  518. priority_queue(const _Compare& __x, const _Sequence& __c,
  519. const _Alloc& __a)
  520. : c(__c, __a), comp(__x)
  521. { std::make_heap(c.begin(), c.end(), comp); }
  522. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  523. priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
  524. : c(std::move(__c), __a), comp(__x)
  525. { std::make_heap(c.begin(), c.end(), comp); }
  526. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  527. priority_queue(const priority_queue& __q, const _Alloc& __a)
  528. : c(__q.c, __a), comp(__q.comp) { }
  529. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  530. priority_queue(priority_queue&& __q, const _Alloc& __a)
  531. : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { }
  532. #endif
  533. /**
  534. * @brief Builds a %queue from a range.
  535. * @param __first An input iterator.
  536. * @param __last An input iterator.
  537. * @param __x A comparison functor describing a strict weak ordering.
  538. * @param __s An initial sequence with which to start.
  539. *
  540. * Begins by copying @a __s, inserting a copy of the elements
  541. * from @a [first,last) into the copy of @a __s, then ordering
  542. * the copy according to @a __x.
  543. *
  544. * For more information on function objects, see the
  545. * documentation on @link functors functor base
  546. * classes@endlink.
  547. */
  548. #if __cplusplus < 201103L
  549. template<typename _InputIterator>
  550. priority_queue(_InputIterator __first, _InputIterator __last,
  551. const _Compare& __x = _Compare(),
  552. const _Sequence& __s = _Sequence())
  553. : c(__s), comp(__x)
  554. {
  555. __glibcxx_requires_valid_range(__first, __last);
  556. c.insert(c.end(), __first, __last);
  557. std::make_heap(c.begin(), c.end(), comp);
  558. }
  559. #else
  560. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  561. // 3529. priority_queue(first, last) should construct c with (first, last)
  562. template<typename _InputIterator,
  563. typename = std::_RequireInputIter<_InputIterator>>
  564. priority_queue(_InputIterator __first, _InputIterator __last,
  565. const _Compare& __x = _Compare())
  566. : c(__first, __last), comp(__x)
  567. { std::make_heap(c.begin(), c.end(), comp); }
  568. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  569. // 3522. Missing requirement on InputIterator template parameter
  570. template<typename _InputIterator,
  571. typename = std::_RequireInputIter<_InputIterator>>
  572. priority_queue(_InputIterator __first, _InputIterator __last,
  573. const _Compare& __x, const _Sequence& __s)
  574. : c(__s), comp(__x)
  575. {
  576. __glibcxx_requires_valid_range(__first, __last);
  577. c.insert(c.end(), __first, __last);
  578. std::make_heap(c.begin(), c.end(), comp);
  579. }
  580. template<typename _InputIterator,
  581. typename = std::_RequireInputIter<_InputIterator>>
  582. priority_queue(_InputIterator __first, _InputIterator __last,
  583. const _Compare& __x, _Sequence&& __s)
  584. : c(std::move(__s)), comp(__x)
  585. {
  586. __glibcxx_requires_valid_range(__first, __last);
  587. c.insert(c.end(), __first, __last);
  588. std::make_heap(c.begin(), c.end(), comp);
  589. }
  590. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  591. // 3506. Missing allocator-extended constructors for priority_queue
  592. template<typename _InputIterator, typename _Alloc,
  593. typename = std::_RequireInputIter<_InputIterator>,
  594. typename _Requires = _Uses<_Alloc>>
  595. priority_queue(_InputIterator __first, _InputIterator __last,
  596. const _Alloc& __alloc)
  597. : c(__first, __last, __alloc), comp()
  598. { std::make_heap(c.begin(), c.end(), comp); }
  599. template<typename _InputIterator, typename _Alloc,
  600. typename = std::_RequireInputIter<_InputIterator>,
  601. typename _Requires = _Uses<_Alloc>>
  602. priority_queue(_InputIterator __first, _InputIterator __last,
  603. const _Compare& __x, const _Alloc& __alloc)
  604. : c(__first, __last, __alloc), comp(__x)
  605. { std::make_heap(c.begin(), c.end(), comp); }
  606. template<typename _InputIterator, typename _Alloc,
  607. typename = std::_RequireInputIter<_InputIterator>,
  608. typename _Requires = _Uses<_Alloc>>
  609. priority_queue(_InputIterator __first, _InputIterator __last,
  610. const _Compare& __x, const _Sequence& __s,
  611. const _Alloc& __alloc)
  612. : c(__s, __alloc), comp(__x)
  613. {
  614. __glibcxx_requires_valid_range(__first, __last);
  615. c.insert(c.end(), __first, __last);
  616. std::make_heap(c.begin(), c.end(), comp);
  617. }
  618. template<typename _InputIterator, typename _Alloc,
  619. typename _Requires = _Uses<_Alloc>>
  620. priority_queue(_InputIterator __first, _InputIterator __last,
  621. const _Compare& __x, _Sequence&& __s,
  622. const _Alloc& __alloc)
  623. : c(std::move(__s), __alloc), comp(__x)
  624. {
  625. __glibcxx_requires_valid_range(__first, __last);
  626. c.insert(c.end(), __first, __last);
  627. std::make_heap(c.begin(), c.end(), comp);
  628. }
  629. #endif
  630. /**
  631. * Returns true if the %queue is empty.
  632. */
  633. _GLIBCXX_NODISCARD bool
  634. empty() const
  635. { return c.empty(); }
  636. /** Returns the number of elements in the %queue. */
  637. _GLIBCXX_NODISCARD
  638. size_type
  639. size() const
  640. { return c.size(); }
  641. /**
  642. * Returns a read-only (constant) reference to the data at the first
  643. * element of the %queue.
  644. */
  645. _GLIBCXX_NODISCARD
  646. const_reference
  647. top() const
  648. {
  649. __glibcxx_requires_nonempty();
  650. return c.front();
  651. }
  652. /**
  653. * @brief Add data to the %queue.
  654. * @param __x Data to be added.
  655. *
  656. * This is a typical %queue operation.
  657. * The time complexity of the operation depends on the underlying
  658. * sequence.
  659. */
  660. void
  661. push(const value_type& __x)
  662. {
  663. c.push_back(__x);
  664. std::push_heap(c.begin(), c.end(), comp);
  665. }
  666. #if __cplusplus >= 201103L
  667. void
  668. push(value_type&& __x)
  669. {
  670. c.push_back(std::move(__x));
  671. std::push_heap(c.begin(), c.end(), comp);
  672. }
  673. template<typename... _Args>
  674. void
  675. emplace(_Args&&... __args)
  676. {
  677. c.emplace_back(std::forward<_Args>(__args)...);
  678. std::push_heap(c.begin(), c.end(), comp);
  679. }
  680. #endif
  681. /**
  682. * @brief Removes first element.
  683. *
  684. * This is a typical %queue operation. It shrinks the %queue
  685. * by one. The time complexity of the operation depends on the
  686. * underlying sequence.
  687. *
  688. * Note that no data is returned, and if the first element's
  689. * data is needed, it should be retrieved before pop() is
  690. * called.
  691. */
  692. void
  693. pop()
  694. {
  695. __glibcxx_requires_nonempty();
  696. std::pop_heap(c.begin(), c.end(), comp);
  697. c.pop_back();
  698. }
  699. #if __cplusplus >= 201103L
  700. void
  701. swap(priority_queue& __pq)
  702. noexcept(__and_<
  703. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  704. __is_nothrow_swappable<_Sequence>,
  705. #else
  706. __is_nothrow_swappable<_Tp>,
  707. #endif
  708. __is_nothrow_swappable<_Compare>
  709. >::value)
  710. {
  711. using std::swap;
  712. swap(c, __pq.c);
  713. swap(comp, __pq.comp);
  714. }
  715. #endif // __cplusplus >= 201103L
  716. };
  717. #if __cpp_deduction_guides >= 201606
  718. template<typename _Compare, typename _Container,
  719. typename = _RequireNotAllocator<_Compare>,
  720. typename = _RequireNotAllocator<_Container>>
  721. priority_queue(_Compare, _Container)
  722. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  723. template<typename _InputIterator, typename _ValT
  724. = typename iterator_traits<_InputIterator>::value_type,
  725. typename _Compare = less<_ValT>,
  726. typename _Container = vector<_ValT>,
  727. typename = _RequireInputIter<_InputIterator>,
  728. typename = _RequireNotAllocator<_Compare>,
  729. typename = _RequireNotAllocator<_Container>>
  730. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(),
  731. _Container = _Container())
  732. -> priority_queue<_ValT, _Container, _Compare>;
  733. template<typename _Compare, typename _Container, typename _Allocator,
  734. typename = _RequireNotAllocator<_Compare>,
  735. typename = _RequireNotAllocator<_Container>>
  736. priority_queue(_Compare, _Container, _Allocator)
  737. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  738. #endif
  739. // No equality/comparison operators are provided for priority_queue.
  740. #if __cplusplus >= 201103L
  741. template<typename _Tp, typename _Sequence, typename _Compare>
  742. inline
  743. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  744. // Constrained free swap overload, see p0185r1
  745. typename enable_if<__and_<__is_swappable<_Sequence>,
  746. __is_swappable<_Compare>>::value>::type
  747. #else
  748. void
  749. #endif
  750. swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
  751. priority_queue<_Tp, _Sequence, _Compare>& __y)
  752. noexcept(noexcept(__x.swap(__y)))
  753. { __x.swap(__y); }
  754. template<typename _Tp, typename _Sequence, typename _Compare,
  755. typename _Alloc>
  756. struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc>
  757. : public uses_allocator<_Sequence, _Alloc>::type { };
  758. #endif // __cplusplus >= 201103L
  759. _GLIBCXX_END_NAMESPACE_VERSION
  760. } // namespace
  761. #endif /* _STL_QUEUE_H */