queue 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_QUEUE
  10. #define _LIBCPP_QUEUE
  11. /*
  12. queue synopsis
  13. namespace std
  14. {
  15. template <class T, class Container = deque<T>>
  16. class queue
  17. {
  18. public:
  19. typedef Container container_type;
  20. typedef typename container_type::value_type value_type;
  21. typedef typename container_type::reference reference;
  22. typedef typename container_type::const_reference const_reference;
  23. typedef typename container_type::size_type size_type;
  24. protected:
  25. container_type c;
  26. public:
  27. queue() = default;
  28. ~queue() = default;
  29. queue(const queue& q) = default;
  30. queue(queue&& q) = default;
  31. queue& operator=(const queue& q) = default;
  32. queue& operator=(queue&& q) = default;
  33. explicit queue(const container_type& c);
  34. explicit queue(container_type&& c)
  35. template<class InputIterator>
  36. queue(InputIterator first, InputIterator last); // since C++23
  37. template<container-compatible-range<T> R> queue(from_range_t, R&& rg); // since C++23
  38. template <class Alloc>
  39. explicit queue(const Alloc& a);
  40. template <class Alloc>
  41. queue(const container_type& c, const Alloc& a);
  42. template <class Alloc>
  43. queue(container_type&& c, const Alloc& a);
  44. template <class Alloc>
  45. queue(const queue& q, const Alloc& a);
  46. template <class Alloc>
  47. queue(queue&& q, const Alloc& a);
  48. template <class InputIterator, class Alloc>
  49. queue(InputIterator first, InputIterator last, const Alloc&); // since C++23
  50. template<container-compatible-range<T> R, class Alloc>
  51. queue(from_range_t, R&& rg, const Alloc&); // since C++23
  52. bool empty() const;
  53. size_type size() const;
  54. reference front();
  55. const_reference front() const;
  56. reference back();
  57. const_reference back() const;
  58. void push(const value_type& v);
  59. void push(value_type&& v);
  60. template<container-compatible-range<T> R>
  61. void push_range(R&& rg); // C++23
  62. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  63. void pop();
  64. void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>)
  65. };
  66. template<class Container>
  67. queue(Container) -> queue<typename Container::value_type, Container>; // C++17
  68. template<class InputIterator>
  69. queue(InputIterator, InputIterator) -> queue<iter-value-type<InputIterator>>; // since C++23
  70. template<ranges::input_range R>
  71. queue(from_range_t, R&&) -> queue<ranges::range_value_t<R>>; // since C++23
  72. template<class Container, class Allocator>
  73. queue(Container, Allocator) -> queue<typename Container::value_type, Container>; // C++17
  74. template<class InputIterator, class Allocator>
  75. queue(InputIterator, InputIterator, Allocator)
  76. -> queue<iter-value-type<InputIterator>,
  77. deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
  78. template<ranges::input_range R, class Allocator>
  79. queue(from_range_t, R&&, Allocator)
  80. -> queue<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
  81. template <class T, class Container>
  82. bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
  83. template <class T, class Container>
  84. bool operator< (const queue<T, Container>& x,const queue<T, Container>& y);
  85. template <class T, class Container>
  86. bool operator!=(const queue<T, Container>& x,const queue<T, Container>& y);
  87. template <class T, class Container>
  88. bool operator> (const queue<T, Container>& x,const queue<T, Container>& y);
  89. template <class T, class Container>
  90. bool operator>=(const queue<T, Container>& x,const queue<T, Container>& y);
  91. template <class T, class Container>
  92. bool operator<=(const queue<T, Container>& x,const queue<T, Container>& y);
  93. template<class T, three_way_comparable Container>
  94. compare_three_way_result_t<Container>
  95. operator<=>(const queue<T, Container>& x, const queue<T, Container>& y); // since C++20
  96. template <class T, class Container>
  97. void swap(queue<T, Container>& x, queue<T, Container>& y)
  98. noexcept(noexcept(x.swap(y)));
  99. template <class T, class Container = vector<T>,
  100. class Compare = less<typename Container::value_type>>
  101. class priority_queue
  102. {
  103. public:
  104. typedef Container container_type;
  105. typedef typename container_type::value_type value_type;
  106. typedef typename container_type::reference reference;
  107. typedef typename container_type::const_reference const_reference;
  108. typedef typename container_type::size_type size_type;
  109. protected:
  110. container_type c;
  111. Compare comp;
  112. public:
  113. priority_queue() : priority_queue(Compare()) {} // C++20
  114. explicit priority_queue(const Compare& x) : priority_queue(x, Container()) {}
  115. priority_queue(const Compare& x, const Container&);
  116. explicit priority_queue(const Compare& x = Compare(), Container&& = Container()); // before C++20
  117. priority_queue(const Compare& x, Container&&); // C++20
  118. template <class InputIterator>
  119. priority_queue(InputIterator first, InputIterator last,
  120. const Compare& comp = Compare());
  121. template <class InputIterator>
  122. priority_queue(InputIterator first, InputIterator last,
  123. const Compare& comp, const Container& c);
  124. template <class InputIterator>
  125. priority_queue(InputIterator first, InputIterator last,
  126. const Compare& comp, Container&& c);
  127. template <container-compatible-range<T> R>
  128. priority_queue(from_range_t, R&& rg, const Compare& x = Compare()); // since C++23
  129. template <class Alloc>
  130. explicit priority_queue(const Alloc& a);
  131. template <class Alloc>
  132. priority_queue(const Compare& comp, const Alloc& a);
  133. template <class Alloc>
  134. priority_queue(const Compare& comp, const Container& c,
  135. const Alloc& a);
  136. template <class Alloc>
  137. priority_queue(const Compare& comp, Container&& c,
  138. const Alloc& a);
  139. template <class InputIterator>
  140. priority_queue(InputIterator first, InputIterator last,
  141. const Alloc& a);
  142. template <class InputIterator>
  143. priority_queue(InputIterator first, InputIterator last,
  144. const Compare& comp, const Alloc& a);
  145. template <class InputIterator>
  146. priority_queue(InputIterator first, InputIterator last,
  147. const Compare& comp, const Container& c, const Alloc& a);
  148. template <class InputIterator>
  149. priority_queue(InputIterator first, InputIterator last,
  150. const Compare& comp, Container&& c, const Alloc& a);
  151. template <container-compatible-range<T> R, class Alloc>
  152. priority_queue(from_range_t, R&& rg, const Compare&, const Alloc&); // since C++23
  153. template <container-compatible-range<T> R, class Alloc>
  154. priority_queue(from_range_t, R&& rg, const Alloc&); // since C++23
  155. template <class Alloc>
  156. priority_queue(const priority_queue& q, const Alloc& a);
  157. template <class Alloc>
  158. priority_queue(priority_queue&& q, const Alloc& a);
  159. bool empty() const;
  160. size_type size() const;
  161. const_reference top() const;
  162. void push(const value_type& v);
  163. void push(value_type&& v);
  164. template<container-compatible-range<T> R>
  165. void push_range(R&& rg); // C++23
  166. template <class... Args> void emplace(Args&&... args);
  167. void pop();
  168. void swap(priority_queue& q)
  169. noexcept(is_nothrow_swappable_v<Container> &&
  170. is_nothrow_swappable_v<Comp>)
  171. };
  172. template <class Compare, class Container>
  173. priority_queue(Compare, Container)
  174. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  175. template<class InputIterator,
  176. class Compare = less<iter-value-type<InputIterator>>,
  177. class Container = vector<iter-value-type<InputIterator>>>
  178. priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
  179. -> priority_queue<iter-value-type<InputIterator>, Container, Compare>; // C++17
  180. template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>>
  181. priority_queue(from_range_t, R&&, Compare = Compare())
  182. -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>>, Compare>; // C++23
  183. template<class Compare, class Container, class Allocator>
  184. priority_queue(Compare, Container, Allocator)
  185. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  186. template<class InputIterator, class Allocator>
  187. priority_queue(InputIterator, InputIterator, Allocator)
  188. -> priority_queue<iter-value-type<InputIterator>,
  189. vector<iter-value-type<InputIterator>, Allocator>,
  190. less<iter-value-type<InputIterator>>>; // C++17
  191. template<class InputIterator, class Compare, class Allocator>
  192. priority_queue(InputIterator, InputIterator, Compare, Allocator)
  193. -> priority_queue<iter-value-type<InputIterator>,
  194. vector<iter-value-type<InputIterator>, Allocator>, Compare>; // C++17
  195. template<class InputIterator, class Compare, class Container, class Allocator>
  196. priority_queue(InputIterator, InputIterator, Compare, Container, Allocator)
  197. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  198. template<ranges::input_range R, class Compare, class Allocator>
  199. priority_queue(from_range_t, R&&, Compare, Allocator)
  200. -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>, Allocator>,
  201. Compare>; // C++23
  202. template<ranges::input_range R, class Allocator>
  203. priority_queue(from_range_t, R&&, Allocator)
  204. -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>, Allocator>>; // C++23
  205. template <class T, class Container, class Compare>
  206. void swap(priority_queue<T, Container, Compare>& x,
  207. priority_queue<T, Container, Compare>& y)
  208. noexcept(noexcept(x.swap(y)));
  209. } // std
  210. */
  211. #include <__algorithm/make_heap.h>
  212. #include <__algorithm/pop_heap.h>
  213. #include <__algorithm/push_heap.h>
  214. #include <__algorithm/ranges_copy.h>
  215. #include <__assert> // all public C++ headers provide the assertion handler
  216. #include <__config>
  217. #include <__functional/operations.h>
  218. #include <__iterator/back_insert_iterator.h>
  219. #include <__iterator/iterator_traits.h>
  220. #include <__memory/uses_allocator.h>
  221. #include <__ranges/access.h>
  222. #include <__ranges/concepts.h>
  223. #include <__ranges/container_compatible_range.h>
  224. #include <__ranges/from_range.h>
  225. #include <__utility/forward.h>
  226. #include <deque>
  227. #include <vector>
  228. #include <version>
  229. // standard-mandated includes
  230. // [queue.syn]
  231. #include <compare>
  232. #include <initializer_list>
  233. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  234. # pragma GCC system_header
  235. #endif
  236. _LIBCPP_BEGIN_NAMESPACE_STD
  237. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS queue;
  238. template <class _Tp, class _Container>
  239. _LIBCPP_INLINE_VISIBILITY
  240. bool
  241. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  242. template <class _Tp, class _Container>
  243. _LIBCPP_INLINE_VISIBILITY
  244. bool
  245. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  246. template <class _Tp, class _Container /*= deque<_Tp>*/>
  247. class _LIBCPP_TEMPLATE_VIS queue
  248. {
  249. public:
  250. typedef _Container container_type;
  251. typedef typename container_type::value_type value_type;
  252. typedef typename container_type::reference reference;
  253. typedef typename container_type::const_reference const_reference;
  254. typedef typename container_type::size_type size_type;
  255. static_assert((is_same<_Tp, value_type>::value), "" );
  256. protected:
  257. container_type c;
  258. public:
  259. _LIBCPP_INLINE_VISIBILITY
  260. queue()
  261. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  262. : c() {}
  263. _LIBCPP_INLINE_VISIBILITY
  264. queue(const queue& __q) : c(__q.c) {}
  265. #if _LIBCPP_STD_VER >= 23
  266. template <class _InputIterator,
  267. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
  268. _LIBCPP_HIDE_FROM_ABI
  269. queue(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  270. template <_ContainerCompatibleRange<_Tp> _Range>
  271. _LIBCPP_HIDE_FROM_ABI
  272. queue(from_range_t, _Range&& __range) : c(from_range, std::forward<_Range>(__range)) {}
  273. template <class _InputIterator,
  274. class _Alloc,
  275. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  276. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  277. _LIBCPP_HIDE_FROM_ABI
  278. queue(_InputIterator __first, _InputIterator __second, const _Alloc& __alloc) : c(__first, __second, __alloc) {}
  279. template <_ContainerCompatibleRange<_Tp> _Range,
  280. class _Alloc,
  281. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  282. _LIBCPP_HIDE_FROM_ABI
  283. queue(from_range_t, _Range&& __range, const _Alloc& __alloc)
  284. : c(from_range, std::forward<_Range>(__range), __alloc) {}
  285. #endif
  286. _LIBCPP_INLINE_VISIBILITY
  287. queue& operator=(const queue& __q) {c = __q.c; return *this;}
  288. #ifndef _LIBCPP_CXX03_LANG
  289. _LIBCPP_INLINE_VISIBILITY
  290. queue(queue&& __q)
  291. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  292. : c(_VSTD::move(__q.c)) {}
  293. _LIBCPP_INLINE_VISIBILITY
  294. queue& operator=(queue&& __q)
  295. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  296. {c = _VSTD::move(__q.c); return *this;}
  297. #endif // _LIBCPP_CXX03_LANG
  298. _LIBCPP_INLINE_VISIBILITY
  299. explicit queue(const container_type& __c) : c(__c) {}
  300. #ifndef _LIBCPP_CXX03_LANG
  301. _LIBCPP_INLINE_VISIBILITY
  302. explicit queue(container_type&& __c) : c(_VSTD::move(__c)) {}
  303. #endif // _LIBCPP_CXX03_LANG
  304. template <class _Alloc>
  305. _LIBCPP_INLINE_VISIBILITY
  306. explicit queue(const _Alloc& __a,
  307. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  308. : c(__a) {}
  309. template <class _Alloc>
  310. _LIBCPP_INLINE_VISIBILITY
  311. queue(const queue& __q, const _Alloc& __a,
  312. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  313. : c(__q.c, __a) {}
  314. template <class _Alloc>
  315. _LIBCPP_INLINE_VISIBILITY
  316. queue(const container_type& __c, const _Alloc& __a,
  317. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  318. : c(__c, __a) {}
  319. #ifndef _LIBCPP_CXX03_LANG
  320. template <class _Alloc>
  321. _LIBCPP_INLINE_VISIBILITY
  322. queue(container_type&& __c, const _Alloc& __a,
  323. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  324. : c(_VSTD::move(__c), __a) {}
  325. template <class _Alloc>
  326. _LIBCPP_INLINE_VISIBILITY
  327. queue(queue&& __q, const _Alloc& __a,
  328. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  329. : c(_VSTD::move(__q.c), __a) {}
  330. #endif // _LIBCPP_CXX03_LANG
  331. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  332. bool empty() const {return c.empty();}
  333. _LIBCPP_INLINE_VISIBILITY
  334. size_type size() const {return c.size();}
  335. _LIBCPP_INLINE_VISIBILITY
  336. reference front() {return c.front();}
  337. _LIBCPP_INLINE_VISIBILITY
  338. const_reference front() const {return c.front();}
  339. _LIBCPP_INLINE_VISIBILITY
  340. reference back() {return c.back();}
  341. _LIBCPP_INLINE_VISIBILITY
  342. const_reference back() const {return c.back();}
  343. _LIBCPP_INLINE_VISIBILITY
  344. void push(const value_type& __v) {c.push_back(__v);}
  345. #ifndef _LIBCPP_CXX03_LANG
  346. _LIBCPP_INLINE_VISIBILITY
  347. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  348. #if _LIBCPP_STD_VER >= 23
  349. template <_ContainerCompatibleRange<_Tp> _Range>
  350. _LIBCPP_HIDE_FROM_ABI
  351. void push_range(_Range&& __range) {
  352. if constexpr (requires (container_type& __c) {
  353. __c.append_range(std::forward<_Range>(__range));
  354. }) {
  355. c.append_range(std::forward<_Range>(__range));
  356. } else {
  357. ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
  358. }
  359. }
  360. #endif
  361. template <class... _Args>
  362. _LIBCPP_INLINE_VISIBILITY
  363. #if _LIBCPP_STD_VER >= 17
  364. decltype(auto) emplace(_Args&&... __args)
  365. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  366. #else
  367. void emplace(_Args&&... __args)
  368. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  369. #endif
  370. #endif // _LIBCPP_CXX03_LANG
  371. _LIBCPP_INLINE_VISIBILITY
  372. void pop() {c.pop_front();}
  373. _LIBCPP_INLINE_VISIBILITY
  374. void swap(queue& __q)
  375. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  376. {
  377. using _VSTD::swap;
  378. swap(c, __q.c);
  379. }
  380. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  381. template <class _T1, class _OtherContainer>
  382. friend
  383. _LIBCPP_INLINE_VISIBILITY
  384. bool
  385. operator==(const queue<_T1, _OtherContainer>& __x,const queue<_T1, _OtherContainer>& __y);
  386. template <class _T1, class _OtherContainer>
  387. friend
  388. _LIBCPP_INLINE_VISIBILITY
  389. bool
  390. operator< (const queue<_T1, _OtherContainer>& __x,const queue<_T1, _OtherContainer>& __y);
  391. };
  392. #if _LIBCPP_STD_VER >= 17
  393. template<class _Container,
  394. class = enable_if_t<!__is_allocator<_Container>::value>
  395. >
  396. queue(_Container)
  397. -> queue<typename _Container::value_type, _Container>;
  398. template<class _Container,
  399. class _Alloc,
  400. class = enable_if_t<!__is_allocator<_Container>::value>,
  401. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  402. >
  403. queue(_Container, _Alloc)
  404. -> queue<typename _Container::value_type, _Container>;
  405. #endif
  406. #if _LIBCPP_STD_VER >= 23
  407. template <class _InputIterator,
  408. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
  409. queue(_InputIterator, _InputIterator)
  410. -> queue<__iter_value_type<_InputIterator>>;
  411. template <ranges::input_range _Range>
  412. queue(from_range_t, _Range&&)
  413. -> queue<ranges::range_value_t<_Range>>;
  414. template <class _InputIterator,
  415. class _Alloc,
  416. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  417. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  418. queue(_InputIterator, _InputIterator, _Alloc)
  419. -> queue<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  420. template <ranges::input_range _Range,
  421. class _Alloc,
  422. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  423. queue(from_range_t, _Range&&, _Alloc)
  424. -> queue<ranges::range_value_t<_Range>, deque<ranges::range_value_t<_Range>, _Alloc>>;
  425. #endif
  426. template <class _Tp, class _Container>
  427. inline _LIBCPP_INLINE_VISIBILITY
  428. bool
  429. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  430. {
  431. return __x.c == __y.c;
  432. }
  433. template <class _Tp, class _Container>
  434. inline _LIBCPP_INLINE_VISIBILITY
  435. bool
  436. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  437. {
  438. return __x.c < __y.c;
  439. }
  440. template <class _Tp, class _Container>
  441. inline _LIBCPP_INLINE_VISIBILITY
  442. bool
  443. operator!=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  444. {
  445. return !(__x == __y);
  446. }
  447. template <class _Tp, class _Container>
  448. inline _LIBCPP_INLINE_VISIBILITY
  449. bool
  450. operator> (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  451. {
  452. return __y < __x;
  453. }
  454. template <class _Tp, class _Container>
  455. inline _LIBCPP_INLINE_VISIBILITY
  456. bool
  457. operator>=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  458. {
  459. return !(__x < __y);
  460. }
  461. template <class _Tp, class _Container>
  462. inline _LIBCPP_INLINE_VISIBILITY
  463. bool
  464. operator<=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  465. {
  466. return !(__y < __x);
  467. }
  468. #if _LIBCPP_STD_VER >= 20
  469. template <class _Tp, three_way_comparable _Container>
  470. _LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_Container>
  471. operator<=>(const queue<_Tp, _Container>& __x, const queue<_Tp, _Container>& __y) {
  472. // clang 16 bug: declaring `friend operator<=>` causes "use of overloaded operator '*' is ambiguous" errors
  473. return __x.__get_container() <=> __y.__get_container();
  474. }
  475. #endif
  476. template <class _Tp, class _Container, __enable_if_t<__is_swappable<_Container>::value, int> = 0>
  477. inline _LIBCPP_INLINE_VISIBILITY
  478. void
  479. swap(queue<_Tp, _Container>& __x, queue<_Tp, _Container>& __y)
  480. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  481. {
  482. __x.swap(__y);
  483. }
  484. template <class _Tp, class _Container, class _Alloc>
  485. struct _LIBCPP_TEMPLATE_VIS uses_allocator<queue<_Tp, _Container>, _Alloc>
  486. : public uses_allocator<_Container, _Alloc>
  487. {
  488. };
  489. template <class _Tp, class _Container = vector<_Tp>,
  490. class _Compare = less<typename _Container::value_type> >
  491. class _LIBCPP_TEMPLATE_VIS priority_queue
  492. {
  493. public:
  494. typedef _Container container_type;
  495. typedef _Compare value_compare;
  496. typedef typename container_type::value_type value_type;
  497. typedef typename container_type::reference reference;
  498. typedef typename container_type::const_reference const_reference;
  499. typedef typename container_type::size_type size_type;
  500. static_assert((is_same<_Tp, value_type>::value), "" );
  501. protected:
  502. container_type c;
  503. value_compare comp;
  504. public:
  505. _LIBCPP_INLINE_VISIBILITY
  506. priority_queue()
  507. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value &&
  508. is_nothrow_default_constructible<value_compare>::value)
  509. : c(), comp() {}
  510. _LIBCPP_INLINE_VISIBILITY
  511. priority_queue(const priority_queue& __q) : c(__q.c), comp(__q.comp) {}
  512. _LIBCPP_INLINE_VISIBILITY
  513. priority_queue& operator=(const priority_queue& __q)
  514. {c = __q.c; comp = __q.comp; return *this;}
  515. #ifndef _LIBCPP_CXX03_LANG
  516. _LIBCPP_INLINE_VISIBILITY
  517. priority_queue(priority_queue&& __q)
  518. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value &&
  519. is_nothrow_move_constructible<value_compare>::value)
  520. : c(_VSTD::move(__q.c)), comp(_VSTD::move(__q.comp)) {}
  521. _LIBCPP_INLINE_VISIBILITY
  522. priority_queue& operator=(priority_queue&& __q)
  523. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value &&
  524. is_nothrow_move_assignable<value_compare>::value)
  525. {c = _VSTD::move(__q.c); comp = _VSTD::move(__q.comp); return *this;}
  526. #endif // _LIBCPP_CXX03_LANG
  527. _LIBCPP_INLINE_VISIBILITY
  528. explicit priority_queue(const value_compare& __comp)
  529. : c(), comp(__comp) {}
  530. _LIBCPP_INLINE_VISIBILITY
  531. priority_queue(const value_compare& __comp, const container_type& __c);
  532. #ifndef _LIBCPP_CXX03_LANG
  533. _LIBCPP_INLINE_VISIBILITY
  534. priority_queue(const value_compare& __comp, container_type&& __c);
  535. #endif
  536. template <class _InputIter, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  537. _LIBCPP_INLINE_VISIBILITY
  538. priority_queue(_InputIter __f, _InputIter __l,
  539. const value_compare& __comp = value_compare());
  540. template <class _InputIter, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  541. _LIBCPP_INLINE_VISIBILITY
  542. priority_queue(_InputIter __f, _InputIter __l,
  543. const value_compare& __comp, const container_type& __c);
  544. #ifndef _LIBCPP_CXX03_LANG
  545. template <class _InputIter, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  546. _LIBCPP_INLINE_VISIBILITY
  547. priority_queue(_InputIter __f, _InputIter __l,
  548. const value_compare& __comp, container_type&& __c);
  549. #endif // _LIBCPP_CXX03_LANG
  550. #if _LIBCPP_STD_VER >= 23
  551. template <_ContainerCompatibleRange<_Tp> _Range>
  552. _LIBCPP_HIDE_FROM_ABI
  553. priority_queue(from_range_t, _Range&& __range, const value_compare& __comp = value_compare())
  554. : c(from_range, std::forward<_Range>(__range)),
  555. comp(__comp) {
  556. std::make_heap(c.begin(), c.end(), comp);
  557. }
  558. #endif
  559. template <class _Alloc>
  560. _LIBCPP_INLINE_VISIBILITY
  561. explicit priority_queue(const _Alloc& __a,
  562. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  563. template <class _Alloc>
  564. _LIBCPP_INLINE_VISIBILITY
  565. priority_queue(const value_compare& __comp, const _Alloc& __a,
  566. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  567. template <class _Alloc>
  568. _LIBCPP_INLINE_VISIBILITY
  569. priority_queue(const value_compare& __comp, const container_type& __c,
  570. const _Alloc& __a,
  571. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  572. template <class _Alloc>
  573. _LIBCPP_INLINE_VISIBILITY
  574. priority_queue(const priority_queue& __q, const _Alloc& __a,
  575. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  576. #ifndef _LIBCPP_CXX03_LANG
  577. template <class _Alloc>
  578. _LIBCPP_INLINE_VISIBILITY
  579. priority_queue(const value_compare& __comp, container_type&& __c,
  580. const _Alloc& __a,
  581. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  582. template <class _Alloc>
  583. _LIBCPP_INLINE_VISIBILITY
  584. priority_queue(priority_queue&& __q, const _Alloc& __a,
  585. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  586. #endif // _LIBCPP_CXX03_LANG
  587. template <class _InputIter, class _Alloc, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  588. _LIBCPP_INLINE_VISIBILITY
  589. priority_queue(_InputIter __f, _InputIter __l, const _Alloc& __a,
  590. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  591. template <class _InputIter, class _Alloc, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  592. _LIBCPP_INLINE_VISIBILITY
  593. priority_queue(_InputIter __f, _InputIter __l,
  594. const value_compare& __comp, const _Alloc& __a,
  595. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  596. template <class _InputIter, class _Alloc, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  597. _LIBCPP_INLINE_VISIBILITY
  598. priority_queue(_InputIter __f, _InputIter __l,
  599. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  600. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  601. #ifndef _LIBCPP_CXX03_LANG
  602. template <class _InputIter, class _Alloc, class = __enable_if_t<__has_input_iterator_category<_InputIter>::value> >
  603. _LIBCPP_INLINE_VISIBILITY
  604. priority_queue(_InputIter __f, _InputIter __l,
  605. const value_compare& __comp, container_type&& __c, const _Alloc& __a,
  606. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  607. #endif // _LIBCPP_CXX03_LANG
  608. #if _LIBCPP_STD_VER >= 23
  609. template <_ContainerCompatibleRange<_Tp> _Range,
  610. class _Alloc,
  611. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>>
  612. _LIBCPP_HIDE_FROM_ABI
  613. priority_queue(from_range_t, _Range&& __range, const value_compare& __comp, const _Alloc& __a)
  614. : c(from_range, std::forward<_Range>(__range), __a),
  615. comp(__comp) {
  616. std::make_heap(c.begin(), c.end(), comp);
  617. }
  618. template <_ContainerCompatibleRange<_Tp> _Range,
  619. class _Alloc,
  620. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>>
  621. _LIBCPP_HIDE_FROM_ABI
  622. priority_queue(from_range_t, _Range&& __range, const _Alloc& __a)
  623. : c(from_range, std::forward<_Range>(__range), __a),
  624. comp() {
  625. std::make_heap(c.begin(), c.end(), comp);
  626. }
  627. #endif
  628. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  629. bool empty() const {return c.empty();}
  630. _LIBCPP_INLINE_VISIBILITY
  631. size_type size() const {return c.size();}
  632. _LIBCPP_INLINE_VISIBILITY
  633. const_reference top() const {return c.front();}
  634. _LIBCPP_INLINE_VISIBILITY
  635. void push(const value_type& __v);
  636. #ifndef _LIBCPP_CXX03_LANG
  637. _LIBCPP_INLINE_VISIBILITY
  638. void push(value_type&& __v);
  639. #if _LIBCPP_STD_VER >= 23
  640. template <_ContainerCompatibleRange<_Tp> _Range>
  641. _LIBCPP_HIDE_FROM_ABI
  642. void push_range(_Range&& __range) {
  643. if constexpr (requires (container_type& __c) {
  644. __c.append_range(std::forward<_Range>(__range));
  645. }) {
  646. c.append_range(std::forward<_Range>(__range));
  647. } else {
  648. ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
  649. }
  650. std::make_heap(c.begin(), c.end(), comp);
  651. }
  652. #endif
  653. template <class... _Args>
  654. _LIBCPP_INLINE_VISIBILITY
  655. void emplace(_Args&&... __args);
  656. #endif // _LIBCPP_CXX03_LANG
  657. _LIBCPP_INLINE_VISIBILITY
  658. void pop();
  659. _LIBCPP_INLINE_VISIBILITY
  660. void swap(priority_queue& __q)
  661. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  662. __is_nothrow_swappable<value_compare>::value);
  663. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  664. };
  665. #if _LIBCPP_STD_VER >= 17
  666. template <class _Compare,
  667. class _Container,
  668. class = enable_if_t<!__is_allocator<_Compare>::value>,
  669. class = enable_if_t<!__is_allocator<_Container>::value>
  670. >
  671. priority_queue(_Compare, _Container)
  672. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  673. template<class _InputIterator,
  674. class _Compare = less<__iter_value_type<_InputIterator>>,
  675. class _Container = vector<__iter_value_type<_InputIterator>>,
  676. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  677. class = enable_if_t<!__is_allocator<_Compare>::value>,
  678. class = enable_if_t<!__is_allocator<_Container>::value>
  679. >
  680. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), _Container = _Container())
  681. -> priority_queue<__iter_value_type<_InputIterator>, _Container, _Compare>;
  682. template<class _Compare,
  683. class _Container,
  684. class _Alloc,
  685. class = enable_if_t<!__is_allocator<_Compare>::value>,
  686. class = enable_if_t<!__is_allocator<_Container>::value>,
  687. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  688. >
  689. priority_queue(_Compare, _Container, _Alloc)
  690. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  691. template<class _InputIterator, class _Allocator,
  692. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  693. class = enable_if_t<__is_allocator<_Allocator>::value>
  694. >
  695. priority_queue(_InputIterator, _InputIterator, _Allocator)
  696. -> priority_queue<__iter_value_type<_InputIterator>,
  697. vector<__iter_value_type<_InputIterator>, _Allocator>,
  698. less<__iter_value_type<_InputIterator>>>;
  699. template<class _InputIterator, class _Compare, class _Allocator,
  700. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  701. class = enable_if_t<!__is_allocator<_Compare>::value>,
  702. class = enable_if_t<__is_allocator<_Allocator>::value>
  703. >
  704. priority_queue(_InputIterator, _InputIterator, _Compare, _Allocator)
  705. -> priority_queue<__iter_value_type<_InputIterator>,
  706. vector<__iter_value_type<_InputIterator>, _Allocator>, _Compare>;
  707. template<class _InputIterator, class _Compare, class _Container, class _Alloc,
  708. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  709. class = enable_if_t<!__is_allocator<_Compare>::value>,
  710. class = enable_if_t<!__is_allocator<_Container>::value>,
  711. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  712. >
  713. priority_queue(_InputIterator, _InputIterator, _Compare, _Container, _Alloc)
  714. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  715. #endif
  716. #if _LIBCPP_STD_VER >= 23
  717. template <ranges::input_range _Range,
  718. class _Compare = less<ranges::range_value_t<_Range>>,
  719. class = enable_if_t<!__is_allocator<_Compare>::value>>
  720. priority_queue(from_range_t, _Range&&, _Compare = _Compare())
  721. -> priority_queue<ranges::range_value_t<_Range>, vector<ranges::range_value_t<_Range>>, _Compare>;
  722. template <ranges::input_range _Range,
  723. class _Compare,
  724. class _Alloc,
  725. class = enable_if_t<!__is_allocator<_Compare>::value>,
  726. class = enable_if_t<__is_allocator<_Alloc>::value>>
  727. priority_queue(from_range_t, _Range&&, _Compare, _Alloc)
  728. -> priority_queue<ranges::range_value_t<_Range>, vector<ranges::range_value_t<_Range>, _Alloc>,
  729. _Compare>;
  730. template <ranges::input_range _Range,
  731. class _Alloc,
  732. class = enable_if_t<__is_allocator<_Alloc>::value>>
  733. priority_queue(from_range_t, _Range&&, _Alloc)
  734. -> priority_queue<ranges::range_value_t<_Range>, vector<ranges::range_value_t<_Range>, _Alloc>>;
  735. #endif
  736. template <class _Tp, class _Container, class _Compare>
  737. inline
  738. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp,
  739. const container_type& __c)
  740. : c(__c),
  741. comp(__comp)
  742. {
  743. _VSTD::make_heap(c.begin(), c.end(), comp);
  744. }
  745. #ifndef _LIBCPP_CXX03_LANG
  746. template <class _Tp, class _Container, class _Compare>
  747. inline
  748. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  749. container_type&& __c)
  750. : c(_VSTD::move(__c)),
  751. comp(__comp)
  752. {
  753. _VSTD::make_heap(c.begin(), c.end(), comp);
  754. }
  755. #endif // _LIBCPP_CXX03_LANG
  756. template <class _Tp, class _Container, class _Compare>
  757. template <class _InputIter, class>
  758. inline
  759. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  760. const value_compare& __comp)
  761. : c(__f, __l),
  762. comp(__comp)
  763. {
  764. _VSTD::make_heap(c.begin(), c.end(), comp);
  765. }
  766. template <class _Tp, class _Container, class _Compare>
  767. template <class _InputIter, class>
  768. inline
  769. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  770. const value_compare& __comp,
  771. const container_type& __c)
  772. : c(__c),
  773. comp(__comp)
  774. {
  775. c.insert(c.end(), __f, __l);
  776. _VSTD::make_heap(c.begin(), c.end(), comp);
  777. }
  778. #ifndef _LIBCPP_CXX03_LANG
  779. template <class _Tp, class _Container, class _Compare>
  780. template <class _InputIter, class>
  781. inline
  782. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  783. const value_compare& __comp,
  784. container_type&& __c)
  785. : c(_VSTD::move(__c)),
  786. comp(__comp)
  787. {
  788. c.insert(c.end(), __f, __l);
  789. _VSTD::make_heap(c.begin(), c.end(), comp);
  790. }
  791. #endif // _LIBCPP_CXX03_LANG
  792. template <class _Tp, class _Container, class _Compare>
  793. template <class _Alloc>
  794. inline
  795. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Alloc& __a,
  796. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  797. : c(__a)
  798. {
  799. }
  800. template <class _Tp, class _Container, class _Compare>
  801. template <class _Alloc>
  802. inline
  803. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  804. const _Alloc& __a,
  805. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  806. : c(__a),
  807. comp(__comp)
  808. {
  809. }
  810. template <class _Tp, class _Container, class _Compare>
  811. template <class _Alloc>
  812. inline
  813. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  814. const container_type& __c,
  815. const _Alloc& __a,
  816. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  817. : c(__c, __a),
  818. comp(__comp)
  819. {
  820. _VSTD::make_heap(c.begin(), c.end(), comp);
  821. }
  822. template <class _Tp, class _Container, class _Compare>
  823. template <class _Alloc>
  824. inline
  825. priority_queue<_Tp, _Container, _Compare>::priority_queue(const priority_queue& __q,
  826. const _Alloc& __a,
  827. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  828. : c(__q.c, __a),
  829. comp(__q.comp)
  830. {
  831. }
  832. #ifndef _LIBCPP_CXX03_LANG
  833. template <class _Tp, class _Container, class _Compare>
  834. template <class _Alloc>
  835. inline
  836. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  837. container_type&& __c,
  838. const _Alloc& __a,
  839. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  840. : c(_VSTD::move(__c), __a),
  841. comp(__comp)
  842. {
  843. _VSTD::make_heap(c.begin(), c.end(), comp);
  844. }
  845. template <class _Tp, class _Container, class _Compare>
  846. template <class _Alloc>
  847. inline
  848. priority_queue<_Tp, _Container, _Compare>::priority_queue(priority_queue&& __q,
  849. const _Alloc& __a,
  850. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  851. : c(_VSTD::move(__q.c), __a),
  852. comp(_VSTD::move(__q.comp))
  853. {
  854. }
  855. #endif // _LIBCPP_CXX03_LANG
  856. template <class _Tp, class _Container, class _Compare>
  857. template <class _InputIter, class _Alloc, class>
  858. inline
  859. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  860. _InputIter __f, _InputIter __l, const _Alloc& __a,
  861. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  862. : c(__f, __l, __a),
  863. comp()
  864. {
  865. _VSTD::make_heap(c.begin(), c.end(), comp);
  866. }
  867. template <class _Tp, class _Container, class _Compare>
  868. template <class _InputIter, class _Alloc, class>
  869. inline
  870. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  871. _InputIter __f, _InputIter __l,
  872. const value_compare& __comp, const _Alloc& __a,
  873. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  874. : c(__f, __l, __a),
  875. comp(__comp)
  876. {
  877. _VSTD::make_heap(c.begin(), c.end(), comp);
  878. }
  879. template <class _Tp, class _Container, class _Compare>
  880. template <class _InputIter, class _Alloc, class>
  881. inline
  882. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  883. _InputIter __f, _InputIter __l,
  884. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  885. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  886. : c(__c, __a),
  887. comp(__comp)
  888. {
  889. c.insert(c.end(), __f, __l);
  890. _VSTD::make_heap(c.begin(), c.end(), comp);
  891. }
  892. #ifndef _LIBCPP_CXX03_LANG
  893. template <class _Tp, class _Container, class _Compare>
  894. template <class _InputIter, class _Alloc, class>
  895. inline
  896. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  897. _InputIter __f, _InputIter __l, const value_compare& __comp,
  898. container_type&& __c, const _Alloc& __a,
  899. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  900. : c(_VSTD::move(__c), __a),
  901. comp(__comp)
  902. {
  903. c.insert(c.end(), __f, __l);
  904. _VSTD::make_heap(c.begin(), c.end(), comp);
  905. }
  906. #endif // _LIBCPP_CXX03_LANG
  907. template <class _Tp, class _Container, class _Compare>
  908. inline
  909. void
  910. priority_queue<_Tp, _Container, _Compare>::push(const value_type& __v)
  911. {
  912. c.push_back(__v);
  913. _VSTD::push_heap(c.begin(), c.end(), comp);
  914. }
  915. #ifndef _LIBCPP_CXX03_LANG
  916. template <class _Tp, class _Container, class _Compare>
  917. inline
  918. void
  919. priority_queue<_Tp, _Container, _Compare>::push(value_type&& __v)
  920. {
  921. c.push_back(_VSTD::move(__v));
  922. _VSTD::push_heap(c.begin(), c.end(), comp);
  923. }
  924. template <class _Tp, class _Container, class _Compare>
  925. template <class... _Args>
  926. inline
  927. void
  928. priority_queue<_Tp, _Container, _Compare>::emplace(_Args&&... __args)
  929. {
  930. c.emplace_back(_VSTD::forward<_Args>(__args)...);
  931. _VSTD::push_heap(c.begin(), c.end(), comp);
  932. }
  933. #endif // _LIBCPP_CXX03_LANG
  934. template <class _Tp, class _Container, class _Compare>
  935. inline
  936. void
  937. priority_queue<_Tp, _Container, _Compare>::pop()
  938. {
  939. _VSTD::pop_heap(c.begin(), c.end(), comp);
  940. c.pop_back();
  941. }
  942. template <class _Tp, class _Container, class _Compare>
  943. inline
  944. void
  945. priority_queue<_Tp, _Container, _Compare>::swap(priority_queue& __q)
  946. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  947. __is_nothrow_swappable<value_compare>::value)
  948. {
  949. using _VSTD::swap;
  950. swap(c, __q.c);
  951. swap(comp, __q.comp);
  952. }
  953. template <class _Tp, class _Container, class _Compare,
  954. __enable_if_t<__is_swappable<_Container>::value && __is_swappable<_Compare>::value, int> = 0>
  955. inline _LIBCPP_INLINE_VISIBILITY
  956. void
  957. swap(priority_queue<_Tp, _Container, _Compare>& __x,
  958. priority_queue<_Tp, _Container, _Compare>& __y)
  959. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  960. {
  961. __x.swap(__y);
  962. }
  963. template <class _Tp, class _Container, class _Compare, class _Alloc>
  964. struct _LIBCPP_TEMPLATE_VIS uses_allocator<priority_queue<_Tp, _Container, _Compare>, _Alloc>
  965. : public uses_allocator<_Container, _Alloc>
  966. {
  967. };
  968. _LIBCPP_END_NAMESPACE_STD
  969. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  970. # include <concepts>
  971. # include <cstdlib>
  972. # include <functional>
  973. # include <type_traits>
  974. #endif
  975. #endif // _LIBCPP_QUEUE