選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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