queue 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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 <class Alloc>
  38. explicit queue(const Alloc& a);
  39. template <class Alloc>
  40. queue(const container_type& c, const Alloc& a);
  41. template <class Alloc>
  42. queue(container_type&& c, const Alloc& a);
  43. template <class Alloc>
  44. queue(const queue& q, const Alloc& a);
  45. template <class Alloc>
  46. queue(queue&& q, const Alloc& a);
  47. template <class InputIterator, class Alloc>
  48. queue(InputIterator first, InputIterator last, const Alloc&); // since C++23
  49. bool empty() const;
  50. size_type size() const;
  51. reference front();
  52. const_reference front() const;
  53. reference back();
  54. const_reference back() const;
  55. void push(const value_type& v);
  56. void push(value_type&& v);
  57. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  58. void pop();
  59. void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>)
  60. };
  61. template<class Container>
  62. queue(Container) -> queue<typename Container::value_type, Container>; // C++17
  63. template<class InputIterator>
  64. queue(InputIterator, InputIterator) -> queue<iter-value-type<InputIterator>>; // since C++23
  65. template<class Container, class Allocator>
  66. queue(Container, Allocator) -> queue<typename Container::value_type, Container>; // C++17
  67. template<class InputIterator, class Allocator>
  68. queue(InputIterator, InputIterator, Allocator)
  69. -> queue<iter-value-type<InputIterator>,
  70. deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
  71. template <class T, class Container>
  72. bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
  73. template <class T, class Container>
  74. bool operator< (const queue<T, Container>& x,const queue<T, Container>& y);
  75. template <class T, class Container>
  76. bool operator!=(const queue<T, Container>& x,const queue<T, Container>& y);
  77. template <class T, class Container>
  78. bool operator> (const queue<T, Container>& x,const queue<T, Container>& y);
  79. template <class T, class Container>
  80. bool operator>=(const queue<T, Container>& x,const queue<T, Container>& y);
  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. void swap(queue<T, Container>& x, queue<T, Container>& y)
  85. noexcept(noexcept(x.swap(y)));
  86. template <class T, class Container = vector<T>,
  87. class Compare = less<typename Container::value_type>>
  88. class priority_queue
  89. {
  90. public:
  91. typedef Container container_type;
  92. typedef typename container_type::value_type value_type;
  93. typedef typename container_type::reference reference;
  94. typedef typename container_type::const_reference const_reference;
  95. typedef typename container_type::size_type size_type;
  96. protected:
  97. container_type c;
  98. Compare comp;
  99. public:
  100. priority_queue() : priority_queue(Compare()) {} // C++20
  101. explicit priority_queue(const Compare& x) : priority_queue(x, Container()) {}
  102. priority_queue(const Compare& x, const Container&);
  103. explicit priority_queue(const Compare& x = Compare(), Container&& = Container()); // before C++20
  104. priority_queue(const Compare& x, Container&&); // C++20
  105. template <class InputIterator>
  106. priority_queue(InputIterator first, InputIterator last,
  107. const Compare& comp = Compare());
  108. template <class InputIterator>
  109. priority_queue(InputIterator first, InputIterator last,
  110. const Compare& comp, const Container& c);
  111. template <class InputIterator>
  112. priority_queue(InputIterator first, InputIterator last,
  113. const Compare& comp, Container&& c);
  114. template <class Alloc>
  115. explicit priority_queue(const Alloc& a);
  116. template <class Alloc>
  117. priority_queue(const Compare& comp, const Alloc& a);
  118. template <class Alloc>
  119. priority_queue(const Compare& comp, const Container& c,
  120. const Alloc& a);
  121. template <class Alloc>
  122. priority_queue(const Compare& comp, Container&& c,
  123. const Alloc& a);
  124. template <class InputIterator>
  125. priority_queue(InputIterator first, InputIterator last,
  126. const Alloc& a);
  127. template <class InputIterator>
  128. priority_queue(InputIterator first, InputIterator last,
  129. const Compare& comp, const Alloc& a);
  130. template <class InputIterator>
  131. priority_queue(InputIterator first, InputIterator last,
  132. const Compare& comp, const Container& c, const Alloc& a);
  133. template <class InputIterator>
  134. priority_queue(InputIterator first, InputIterator last,
  135. const Compare& comp, Container&& c, const Alloc& a);
  136. template <class Alloc>
  137. priority_queue(const priority_queue& q, const Alloc& a);
  138. template <class Alloc>
  139. priority_queue(priority_queue&& q, const Alloc& a);
  140. bool empty() const;
  141. size_type size() const;
  142. const_reference top() const;
  143. void push(const value_type& v);
  144. void push(value_type&& v);
  145. template <class... Args> void emplace(Args&&... args);
  146. void pop();
  147. void swap(priority_queue& q)
  148. noexcept(is_nothrow_swappable_v<Container> &&
  149. is_nothrow_swappable_v<Comp>)
  150. };
  151. template <class Compare, class Container>
  152. priority_queue(Compare, Container)
  153. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  154. template<class InputIterator,
  155. class Compare = less<iter-value-type<InputIterator>>,
  156. class Container = vector<iter-value-type<InputIterator>>>
  157. priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
  158. -> priority_queue<iter-value-type<InputIterator>, Container, Compare>; // C++17
  159. template<class Compare, class Container, class Allocator>
  160. priority_queue(Compare, Container, Allocator)
  161. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  162. template<class InputIterator, class Allocator>
  163. priority_queue(InputIterator, InputIterator, Allocator)
  164. -> priority_queue<iter-value-type<InputIterator>,
  165. vector<iter-value-type<InputIterator>, Allocator>,
  166. less<iter-value-type<InputIterator>>>; // C++17
  167. template<class InputIterator, class Compare, class Allocator>
  168. priority_queue(InputIterator, InputIterator, Compare, Allocator)
  169. -> priority_queue<iter-value-type<InputIterator>,
  170. vector<iter-value-type<InputIterator>, Allocator>, Compare>; // C++17
  171. template<class InputIterator, class Compare, class Container, class Allocator>
  172. priority_queue(InputIterator, InputIterator, Compare, Container, Allocator)
  173. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  174. template <class T, class Container, class Compare>
  175. void swap(priority_queue<T, Container, Compare>& x,
  176. priority_queue<T, Container, Compare>& y)
  177. noexcept(noexcept(x.swap(y)));
  178. } // std
  179. */
  180. #include <__algorithm/make_heap.h>
  181. #include <__algorithm/pop_heap.h>
  182. #include <__algorithm/push_heap.h>
  183. #include <__assert> // all public C++ headers provide the assertion handler
  184. #include <__config>
  185. #include <__functional/operations.h>
  186. #include <__iterator/iterator_traits.h>
  187. #include <__memory/uses_allocator.h>
  188. #include <__utility/forward.h>
  189. #include <deque>
  190. #include <type_traits>
  191. #include <vector>
  192. #include <version>
  193. // standard-mandated includes
  194. // [queue.syn]
  195. #include <compare>
  196. #include <initializer_list>
  197. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  198. # pragma GCC system_header
  199. #endif
  200. _LIBCPP_BEGIN_NAMESPACE_STD
  201. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS queue;
  202. template <class _Tp, class _Container>
  203. _LIBCPP_INLINE_VISIBILITY
  204. bool
  205. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  206. template <class _Tp, class _Container>
  207. _LIBCPP_INLINE_VISIBILITY
  208. bool
  209. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  210. template <class _Tp, class _Container /*= deque<_Tp>*/>
  211. class _LIBCPP_TEMPLATE_VIS queue
  212. {
  213. public:
  214. typedef _Container container_type;
  215. typedef typename container_type::value_type value_type;
  216. typedef typename container_type::reference reference;
  217. typedef typename container_type::const_reference const_reference;
  218. typedef typename container_type::size_type size_type;
  219. static_assert((is_same<_Tp, value_type>::value), "" );
  220. protected:
  221. container_type c;
  222. public:
  223. _LIBCPP_INLINE_VISIBILITY
  224. queue()
  225. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  226. : c() {}
  227. _LIBCPP_INLINE_VISIBILITY
  228. queue(const queue& __q) : c(__q.c) {}
  229. #if _LIBCPP_STD_VER > 20
  230. template <class _InputIterator,
  231. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  232. _LIBCPP_HIDE_FROM_ABI
  233. queue(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  234. template <class _InputIterator,
  235. class _Alloc,
  236. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  237. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  238. _LIBCPP_HIDE_FROM_ABI
  239. queue(_InputIterator __first, _InputIterator __second, const _Alloc& __alloc) : c(__first, __second, __alloc) {}
  240. #endif
  241. _LIBCPP_INLINE_VISIBILITY
  242. queue& operator=(const queue& __q) {c = __q.c; return *this;}
  243. #ifndef _LIBCPP_CXX03_LANG
  244. _LIBCPP_INLINE_VISIBILITY
  245. queue(queue&& __q)
  246. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  247. : c(_VSTD::move(__q.c)) {}
  248. _LIBCPP_INLINE_VISIBILITY
  249. queue& operator=(queue&& __q)
  250. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  251. {c = _VSTD::move(__q.c); return *this;}
  252. #endif // _LIBCPP_CXX03_LANG
  253. _LIBCPP_INLINE_VISIBILITY
  254. explicit queue(const container_type& __c) : c(__c) {}
  255. #ifndef _LIBCPP_CXX03_LANG
  256. _LIBCPP_INLINE_VISIBILITY
  257. explicit queue(container_type&& __c) : c(_VSTD::move(__c)) {}
  258. #endif // _LIBCPP_CXX03_LANG
  259. template <class _Alloc>
  260. _LIBCPP_INLINE_VISIBILITY
  261. explicit queue(const _Alloc& __a,
  262. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  263. : c(__a) {}
  264. template <class _Alloc>
  265. _LIBCPP_INLINE_VISIBILITY
  266. queue(const queue& __q, const _Alloc& __a,
  267. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  268. : c(__q.c, __a) {}
  269. template <class _Alloc>
  270. _LIBCPP_INLINE_VISIBILITY
  271. queue(const container_type& __c, const _Alloc& __a,
  272. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  273. : c(__c, __a) {}
  274. #ifndef _LIBCPP_CXX03_LANG
  275. template <class _Alloc>
  276. _LIBCPP_INLINE_VISIBILITY
  277. queue(container_type&& __c, const _Alloc& __a,
  278. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  279. : c(_VSTD::move(__c), __a) {}
  280. template <class _Alloc>
  281. _LIBCPP_INLINE_VISIBILITY
  282. queue(queue&& __q, const _Alloc& __a,
  283. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  284. : c(_VSTD::move(__q.c), __a) {}
  285. #endif // _LIBCPP_CXX03_LANG
  286. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  287. bool empty() const {return c.empty();}
  288. _LIBCPP_INLINE_VISIBILITY
  289. size_type size() const {return c.size();}
  290. _LIBCPP_INLINE_VISIBILITY
  291. reference front() {return c.front();}
  292. _LIBCPP_INLINE_VISIBILITY
  293. const_reference front() const {return c.front();}
  294. _LIBCPP_INLINE_VISIBILITY
  295. reference back() {return c.back();}
  296. _LIBCPP_INLINE_VISIBILITY
  297. const_reference back() const {return c.back();}
  298. _LIBCPP_INLINE_VISIBILITY
  299. void push(const value_type& __v) {c.push_back(__v);}
  300. #ifndef _LIBCPP_CXX03_LANG
  301. _LIBCPP_INLINE_VISIBILITY
  302. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  303. template <class... _Args>
  304. _LIBCPP_INLINE_VISIBILITY
  305. #if _LIBCPP_STD_VER > 14
  306. decltype(auto) emplace(_Args&&... __args)
  307. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  308. #else
  309. void emplace(_Args&&... __args)
  310. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  311. #endif
  312. #endif // _LIBCPP_CXX03_LANG
  313. _LIBCPP_INLINE_VISIBILITY
  314. void pop() {c.pop_front();}
  315. _LIBCPP_INLINE_VISIBILITY
  316. void swap(queue& __q)
  317. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  318. {
  319. using _VSTD::swap;
  320. swap(c, __q.c);
  321. }
  322. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  323. template <class _T1, class _C1>
  324. friend
  325. _LIBCPP_INLINE_VISIBILITY
  326. bool
  327. operator==(const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  328. template <class _T1, class _C1>
  329. friend
  330. _LIBCPP_INLINE_VISIBILITY
  331. bool
  332. operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  333. };
  334. #if _LIBCPP_STD_VER > 14
  335. template<class _Container,
  336. class = enable_if_t<!__is_allocator<_Container>::value>
  337. >
  338. queue(_Container)
  339. -> queue<typename _Container::value_type, _Container>;
  340. template<class _Container,
  341. class _Alloc,
  342. class = enable_if_t<!__is_allocator<_Container>::value>,
  343. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  344. >
  345. queue(_Container, _Alloc)
  346. -> queue<typename _Container::value_type, _Container>;
  347. #endif
  348. #if _LIBCPP_STD_VER > 20
  349. template <class _InputIterator,
  350. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  351. queue(_InputIterator, _InputIterator)
  352. -> queue<__iter_value_type<_InputIterator>>;
  353. template <class _InputIterator,
  354. class _Alloc,
  355. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  356. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  357. queue(_InputIterator, _InputIterator, _Alloc)
  358. -> queue<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  359. #endif
  360. template <class _Tp, class _Container>
  361. inline _LIBCPP_INLINE_VISIBILITY
  362. bool
  363. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  364. {
  365. return __x.c == __y.c;
  366. }
  367. template <class _Tp, class _Container>
  368. inline _LIBCPP_INLINE_VISIBILITY
  369. bool
  370. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  371. {
  372. return __x.c < __y.c;
  373. }
  374. template <class _Tp, class _Container>
  375. inline _LIBCPP_INLINE_VISIBILITY
  376. bool
  377. operator!=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  378. {
  379. return !(__x == __y);
  380. }
  381. template <class _Tp, class _Container>
  382. inline _LIBCPP_INLINE_VISIBILITY
  383. bool
  384. operator> (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  385. {
  386. return __y < __x;
  387. }
  388. template <class _Tp, class _Container>
  389. inline _LIBCPP_INLINE_VISIBILITY
  390. bool
  391. operator>=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  392. {
  393. return !(__x < __y);
  394. }
  395. template <class _Tp, class _Container>
  396. inline _LIBCPP_INLINE_VISIBILITY
  397. bool
  398. operator<=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  399. {
  400. return !(__y < __x);
  401. }
  402. template <class _Tp, class _Container>
  403. inline _LIBCPP_INLINE_VISIBILITY
  404. __enable_if_t<__is_swappable<_Container>::value, void>
  405. swap(queue<_Tp, _Container>& __x, queue<_Tp, _Container>& __y)
  406. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  407. {
  408. __x.swap(__y);
  409. }
  410. template <class _Tp, class _Container, class _Alloc>
  411. struct _LIBCPP_TEMPLATE_VIS uses_allocator<queue<_Tp, _Container>, _Alloc>
  412. : public uses_allocator<_Container, _Alloc>
  413. {
  414. };
  415. template <class _Tp, class _Container = vector<_Tp>,
  416. class _Compare = less<typename _Container::value_type> >
  417. class _LIBCPP_TEMPLATE_VIS priority_queue
  418. {
  419. public:
  420. typedef _Container container_type;
  421. typedef _Compare value_compare;
  422. typedef typename container_type::value_type value_type;
  423. typedef typename container_type::reference reference;
  424. typedef typename container_type::const_reference const_reference;
  425. typedef typename container_type::size_type size_type;
  426. static_assert((is_same<_Tp, value_type>::value), "" );
  427. protected:
  428. container_type c;
  429. value_compare comp;
  430. public:
  431. _LIBCPP_INLINE_VISIBILITY
  432. priority_queue()
  433. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value &&
  434. is_nothrow_default_constructible<value_compare>::value)
  435. : c(), comp() {}
  436. _LIBCPP_INLINE_VISIBILITY
  437. priority_queue(const priority_queue& __q) : c(__q.c), comp(__q.comp) {}
  438. _LIBCPP_INLINE_VISIBILITY
  439. priority_queue& operator=(const priority_queue& __q)
  440. {c = __q.c; comp = __q.comp; return *this;}
  441. #ifndef _LIBCPP_CXX03_LANG
  442. _LIBCPP_INLINE_VISIBILITY
  443. priority_queue(priority_queue&& __q)
  444. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value &&
  445. is_nothrow_move_constructible<value_compare>::value)
  446. : c(_VSTD::move(__q.c)), comp(_VSTD::move(__q.comp)) {}
  447. _LIBCPP_INLINE_VISIBILITY
  448. priority_queue& operator=(priority_queue&& __q)
  449. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value &&
  450. is_nothrow_move_assignable<value_compare>::value)
  451. {c = _VSTD::move(__q.c); comp = _VSTD::move(__q.comp); return *this;}
  452. #endif // _LIBCPP_CXX03_LANG
  453. _LIBCPP_INLINE_VISIBILITY
  454. explicit priority_queue(const value_compare& __comp)
  455. : c(), comp(__comp) {}
  456. _LIBCPP_INLINE_VISIBILITY
  457. priority_queue(const value_compare& __comp, const container_type& __c);
  458. #ifndef _LIBCPP_CXX03_LANG
  459. _LIBCPP_INLINE_VISIBILITY
  460. priority_queue(const value_compare& __comp, container_type&& __c);
  461. #endif
  462. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  463. _LIBCPP_INLINE_VISIBILITY
  464. priority_queue(_InputIter __f, _InputIter __l,
  465. const value_compare& __comp = value_compare());
  466. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  467. _LIBCPP_INLINE_VISIBILITY
  468. priority_queue(_InputIter __f, _InputIter __l,
  469. const value_compare& __comp, const container_type& __c);
  470. #ifndef _LIBCPP_CXX03_LANG
  471. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  472. _LIBCPP_INLINE_VISIBILITY
  473. priority_queue(_InputIter __f, _InputIter __l,
  474. const value_compare& __comp, container_type&& __c);
  475. #endif // _LIBCPP_CXX03_LANG
  476. template <class _Alloc>
  477. _LIBCPP_INLINE_VISIBILITY
  478. explicit priority_queue(const _Alloc& __a,
  479. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  480. template <class _Alloc>
  481. _LIBCPP_INLINE_VISIBILITY
  482. priority_queue(const value_compare& __comp, const _Alloc& __a,
  483. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  484. template <class _Alloc>
  485. _LIBCPP_INLINE_VISIBILITY
  486. priority_queue(const value_compare& __comp, const container_type& __c,
  487. const _Alloc& __a,
  488. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  489. template <class _Alloc>
  490. _LIBCPP_INLINE_VISIBILITY
  491. priority_queue(const priority_queue& __q, const _Alloc& __a,
  492. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  493. #ifndef _LIBCPP_CXX03_LANG
  494. template <class _Alloc>
  495. _LIBCPP_INLINE_VISIBILITY
  496. priority_queue(const value_compare& __comp, container_type&& __c,
  497. const _Alloc& __a,
  498. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  499. template <class _Alloc>
  500. _LIBCPP_INLINE_VISIBILITY
  501. priority_queue(priority_queue&& __q, const _Alloc& __a,
  502. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  503. #endif // _LIBCPP_CXX03_LANG
  504. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  505. _LIBCPP_INLINE_VISIBILITY
  506. priority_queue(_InputIter __f, _InputIter __l, const _Alloc& __a,
  507. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  508. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  509. _LIBCPP_INLINE_VISIBILITY
  510. priority_queue(_InputIter __f, _InputIter __l,
  511. const value_compare& __comp, const _Alloc& __a,
  512. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  513. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  514. _LIBCPP_INLINE_VISIBILITY
  515. priority_queue(_InputIter __f, _InputIter __l,
  516. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  517. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  518. #ifndef _LIBCPP_CXX03_LANG
  519. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  520. _LIBCPP_INLINE_VISIBILITY
  521. priority_queue(_InputIter __f, _InputIter __l,
  522. const value_compare& __comp, container_type&& __c, const _Alloc& __a,
  523. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  524. #endif // _LIBCPP_CXX03_LANG
  525. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  526. bool empty() const {return c.empty();}
  527. _LIBCPP_INLINE_VISIBILITY
  528. size_type size() const {return c.size();}
  529. _LIBCPP_INLINE_VISIBILITY
  530. const_reference top() const {return c.front();}
  531. _LIBCPP_INLINE_VISIBILITY
  532. void push(const value_type& __v);
  533. #ifndef _LIBCPP_CXX03_LANG
  534. _LIBCPP_INLINE_VISIBILITY
  535. void push(value_type&& __v);
  536. template <class... _Args>
  537. _LIBCPP_INLINE_VISIBILITY
  538. void emplace(_Args&&... __args);
  539. #endif // _LIBCPP_CXX03_LANG
  540. _LIBCPP_INLINE_VISIBILITY
  541. void pop();
  542. _LIBCPP_INLINE_VISIBILITY
  543. void swap(priority_queue& __q)
  544. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  545. __is_nothrow_swappable<value_compare>::value);
  546. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  547. };
  548. #if _LIBCPP_STD_VER >= 17
  549. template <class _Compare,
  550. class _Container,
  551. class = enable_if_t<!__is_allocator<_Compare>::value>,
  552. class = enable_if_t<!__is_allocator<_Container>::value>
  553. >
  554. priority_queue(_Compare, _Container)
  555. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  556. template<class _InputIterator,
  557. class _Compare = less<__iter_value_type<_InputIterator>>,
  558. class _Container = vector<__iter_value_type<_InputIterator>>,
  559. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  560. class = enable_if_t<!__is_allocator<_Compare>::value>,
  561. class = enable_if_t<!__is_allocator<_Container>::value>
  562. >
  563. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), _Container = _Container())
  564. -> priority_queue<__iter_value_type<_InputIterator>, _Container, _Compare>;
  565. template<class _Compare,
  566. class _Container,
  567. class _Alloc,
  568. class = enable_if_t<!__is_allocator<_Compare>::value>,
  569. class = enable_if_t<!__is_allocator<_Container>::value>,
  570. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  571. >
  572. priority_queue(_Compare, _Container, _Alloc)
  573. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  574. template<class _InputIterator, class _Allocator,
  575. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  576. class = enable_if_t<__is_allocator<_Allocator>::value>
  577. >
  578. priority_queue(_InputIterator, _InputIterator, _Allocator)
  579. -> priority_queue<__iter_value_type<_InputIterator>,
  580. vector<__iter_value_type<_InputIterator>, _Allocator>,
  581. less<__iter_value_type<_InputIterator>>>;
  582. template<class _InputIterator, class _Compare, class _Allocator,
  583. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  584. class = enable_if_t<!__is_allocator<_Compare>::value>,
  585. class = enable_if_t<__is_allocator<_Allocator>::value>
  586. >
  587. priority_queue(_InputIterator, _InputIterator, _Compare, _Allocator)
  588. -> priority_queue<__iter_value_type<_InputIterator>,
  589. vector<__iter_value_type<_InputIterator>, _Allocator>, _Compare>;
  590. template<class _InputIterator, class _Compare, class _Container, class _Alloc,
  591. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  592. class = enable_if_t<!__is_allocator<_Compare>::value>,
  593. class = enable_if_t<!__is_allocator<_Container>::value>,
  594. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  595. >
  596. priority_queue(_InputIterator, _InputIterator, _Compare, _Container, _Alloc)
  597. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  598. #endif
  599. template <class _Tp, class _Container, class _Compare>
  600. inline
  601. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp,
  602. const container_type& __c)
  603. : c(__c),
  604. comp(__comp)
  605. {
  606. _VSTD::make_heap(c.begin(), c.end(), comp);
  607. }
  608. #ifndef _LIBCPP_CXX03_LANG
  609. template <class _Tp, class _Container, class _Compare>
  610. inline
  611. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  612. container_type&& __c)
  613. : c(_VSTD::move(__c)),
  614. comp(__comp)
  615. {
  616. _VSTD::make_heap(c.begin(), c.end(), comp);
  617. }
  618. #endif // _LIBCPP_CXX03_LANG
  619. template <class _Tp, class _Container, class _Compare>
  620. template <class _InputIter, class>
  621. inline
  622. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  623. const value_compare& __comp)
  624. : c(__f, __l),
  625. comp(__comp)
  626. {
  627. _VSTD::make_heap(c.begin(), c.end(), comp);
  628. }
  629. template <class _Tp, class _Container, class _Compare>
  630. template <class _InputIter, class>
  631. inline
  632. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  633. const value_compare& __comp,
  634. const container_type& __c)
  635. : c(__c),
  636. comp(__comp)
  637. {
  638. c.insert(c.end(), __f, __l);
  639. _VSTD::make_heap(c.begin(), c.end(), comp);
  640. }
  641. #ifndef _LIBCPP_CXX03_LANG
  642. template <class _Tp, class _Container, class _Compare>
  643. template <class _InputIter, class>
  644. inline
  645. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  646. const value_compare& __comp,
  647. container_type&& __c)
  648. : c(_VSTD::move(__c)),
  649. comp(__comp)
  650. {
  651. c.insert(c.end(), __f, __l);
  652. _VSTD::make_heap(c.begin(), c.end(), comp);
  653. }
  654. #endif // _LIBCPP_CXX03_LANG
  655. template <class _Tp, class _Container, class _Compare>
  656. template <class _Alloc>
  657. inline
  658. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Alloc& __a,
  659. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  660. : c(__a)
  661. {
  662. }
  663. template <class _Tp, class _Container, class _Compare>
  664. template <class _Alloc>
  665. inline
  666. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  667. const _Alloc& __a,
  668. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  669. : c(__a),
  670. comp(__comp)
  671. {
  672. }
  673. template <class _Tp, class _Container, class _Compare>
  674. template <class _Alloc>
  675. inline
  676. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  677. const container_type& __c,
  678. const _Alloc& __a,
  679. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  680. : c(__c, __a),
  681. comp(__comp)
  682. {
  683. _VSTD::make_heap(c.begin(), c.end(), comp);
  684. }
  685. template <class _Tp, class _Container, class _Compare>
  686. template <class _Alloc>
  687. inline
  688. priority_queue<_Tp, _Container, _Compare>::priority_queue(const priority_queue& __q,
  689. const _Alloc& __a,
  690. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  691. : c(__q.c, __a),
  692. comp(__q.comp)
  693. {
  694. }
  695. #ifndef _LIBCPP_CXX03_LANG
  696. template <class _Tp, class _Container, class _Compare>
  697. template <class _Alloc>
  698. inline
  699. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  700. container_type&& __c,
  701. const _Alloc& __a,
  702. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  703. : c(_VSTD::move(__c), __a),
  704. comp(__comp)
  705. {
  706. _VSTD::make_heap(c.begin(), c.end(), comp);
  707. }
  708. template <class _Tp, class _Container, class _Compare>
  709. template <class _Alloc>
  710. inline
  711. priority_queue<_Tp, _Container, _Compare>::priority_queue(priority_queue&& __q,
  712. const _Alloc& __a,
  713. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  714. : c(_VSTD::move(__q.c), __a),
  715. comp(_VSTD::move(__q.comp))
  716. {
  717. }
  718. #endif // _LIBCPP_CXX03_LANG
  719. template <class _Tp, class _Container, class _Compare>
  720. template <class _InputIter, class _Alloc, class>
  721. inline
  722. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  723. _InputIter __f, _InputIter __l, const _Alloc& __a,
  724. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  725. : c(__f, __l, __a),
  726. comp()
  727. {
  728. _VSTD::make_heap(c.begin(), c.end(), comp);
  729. }
  730. template <class _Tp, class _Container, class _Compare>
  731. template <class _InputIter, class _Alloc, class>
  732. inline
  733. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  734. _InputIter __f, _InputIter __l,
  735. const value_compare& __comp, const _Alloc& __a,
  736. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  737. : c(__f, __l, __a),
  738. comp(__comp)
  739. {
  740. _VSTD::make_heap(c.begin(), c.end(), comp);
  741. }
  742. template <class _Tp, class _Container, class _Compare>
  743. template <class _InputIter, class _Alloc, class>
  744. inline
  745. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  746. _InputIter __f, _InputIter __l,
  747. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  748. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  749. : c(__c, __a),
  750. comp(__comp)
  751. {
  752. c.insert(c.end(), __f, __l);
  753. _VSTD::make_heap(c.begin(), c.end(), comp);
  754. }
  755. #ifndef _LIBCPP_CXX03_LANG
  756. template <class _Tp, class _Container, class _Compare>
  757. template <class _InputIter, class _Alloc, class>
  758. inline
  759. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  760. _InputIter __f, _InputIter __l, const value_compare& __comp,
  761. container_type&& __c, const _Alloc& __a,
  762. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  763. : c(_VSTD::move(__c), __a),
  764. comp(__comp)
  765. {
  766. c.insert(c.end(), __f, __l);
  767. _VSTD::make_heap(c.begin(), c.end(), comp);
  768. }
  769. #endif // _LIBCPP_CXX03_LANG
  770. template <class _Tp, class _Container, class _Compare>
  771. inline
  772. void
  773. priority_queue<_Tp, _Container, _Compare>::push(const value_type& __v)
  774. {
  775. c.push_back(__v);
  776. _VSTD::push_heap(c.begin(), c.end(), comp);
  777. }
  778. #ifndef _LIBCPP_CXX03_LANG
  779. template <class _Tp, class _Container, class _Compare>
  780. inline
  781. void
  782. priority_queue<_Tp, _Container, _Compare>::push(value_type&& __v)
  783. {
  784. c.push_back(_VSTD::move(__v));
  785. _VSTD::push_heap(c.begin(), c.end(), comp);
  786. }
  787. template <class _Tp, class _Container, class _Compare>
  788. template <class... _Args>
  789. inline
  790. void
  791. priority_queue<_Tp, _Container, _Compare>::emplace(_Args&&... __args)
  792. {
  793. c.emplace_back(_VSTD::forward<_Args>(__args)...);
  794. _VSTD::push_heap(c.begin(), c.end(), comp);
  795. }
  796. #endif // _LIBCPP_CXX03_LANG
  797. template <class _Tp, class _Container, class _Compare>
  798. inline
  799. void
  800. priority_queue<_Tp, _Container, _Compare>::pop()
  801. {
  802. _VSTD::pop_heap(c.begin(), c.end(), comp);
  803. c.pop_back();
  804. }
  805. template <class _Tp, class _Container, class _Compare>
  806. inline
  807. void
  808. priority_queue<_Tp, _Container, _Compare>::swap(priority_queue& __q)
  809. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  810. __is_nothrow_swappable<value_compare>::value)
  811. {
  812. using _VSTD::swap;
  813. swap(c, __q.c);
  814. swap(comp, __q.comp);
  815. }
  816. template <class _Tp, class _Container, class _Compare>
  817. inline _LIBCPP_INLINE_VISIBILITY
  818. __enable_if_t<
  819. __is_swappable<_Container>::value && __is_swappable<_Compare>::value,
  820. void
  821. >
  822. swap(priority_queue<_Tp, _Container, _Compare>& __x,
  823. priority_queue<_Tp, _Container, _Compare>& __y)
  824. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  825. {
  826. __x.swap(__y);
  827. }
  828. template <class _Tp, class _Container, class _Compare, class _Alloc>
  829. struct _LIBCPP_TEMPLATE_VIS uses_allocator<priority_queue<_Tp, _Container, _Compare>, _Alloc>
  830. : public uses_allocator<_Container, _Alloc>
  831. {
  832. };
  833. _LIBCPP_END_NAMESPACE_STD
  834. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  835. # include <concepts>
  836. # include <functional>
  837. #endif
  838. #endif // _LIBCPP_QUEUE