queue 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 <__config>
  181. #include <__iterator/iterator_traits.h>
  182. #include <__memory/uses_allocator.h>
  183. #include <__utility/forward.h>
  184. #include <algorithm>
  185. #include <compare>
  186. #include <deque>
  187. #include <functional>
  188. #include <type_traits>
  189. #include <vector>
  190. #include <version>
  191. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  192. #pragma GCC system_header
  193. #endif
  194. _LIBCPP_BEGIN_NAMESPACE_STD
  195. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS queue;
  196. template <class _Tp, class _Container>
  197. _LIBCPP_INLINE_VISIBILITY
  198. bool
  199. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  200. template <class _Tp, class _Container>
  201. _LIBCPP_INLINE_VISIBILITY
  202. bool
  203. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  204. template <class _Tp, class _Container /*= deque<_Tp>*/>
  205. class _LIBCPP_TEMPLATE_VIS queue
  206. {
  207. public:
  208. typedef _Container container_type;
  209. typedef typename container_type::value_type value_type;
  210. typedef typename container_type::reference reference;
  211. typedef typename container_type::const_reference const_reference;
  212. typedef typename container_type::size_type size_type;
  213. static_assert((is_same<_Tp, value_type>::value), "" );
  214. protected:
  215. container_type c;
  216. public:
  217. _LIBCPP_INLINE_VISIBILITY
  218. queue()
  219. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  220. : c() {}
  221. _LIBCPP_INLINE_VISIBILITY
  222. queue(const queue& __q) : c(__q.c) {}
  223. #if _LIBCPP_STD_VER > 20
  224. template <class _InputIterator,
  225. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  226. _LIBCPP_HIDE_FROM_ABI
  227. queue(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  228. template <class _InputIterator,
  229. class _Alloc,
  230. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  231. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  232. _LIBCPP_HIDE_FROM_ABI
  233. queue(_InputIterator __first, _InputIterator __second, const _Alloc& __alloc) : c(__first, __second, __alloc) {}
  234. #endif
  235. _LIBCPP_INLINE_VISIBILITY
  236. queue& operator=(const queue& __q) {c = __q.c; return *this;}
  237. #ifndef _LIBCPP_CXX03_LANG
  238. _LIBCPP_INLINE_VISIBILITY
  239. queue(queue&& __q)
  240. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  241. : c(_VSTD::move(__q.c)) {}
  242. _LIBCPP_INLINE_VISIBILITY
  243. queue& operator=(queue&& __q)
  244. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  245. {c = _VSTD::move(__q.c); return *this;}
  246. #endif // _LIBCPP_CXX03_LANG
  247. _LIBCPP_INLINE_VISIBILITY
  248. explicit queue(const container_type& __c) : c(__c) {}
  249. #ifndef _LIBCPP_CXX03_LANG
  250. _LIBCPP_INLINE_VISIBILITY
  251. explicit queue(container_type&& __c) : c(_VSTD::move(__c)) {}
  252. #endif // _LIBCPP_CXX03_LANG
  253. template <class _Alloc>
  254. _LIBCPP_INLINE_VISIBILITY
  255. explicit queue(const _Alloc& __a,
  256. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  257. : c(__a) {}
  258. template <class _Alloc>
  259. _LIBCPP_INLINE_VISIBILITY
  260. queue(const queue& __q, const _Alloc& __a,
  261. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  262. : c(__q.c, __a) {}
  263. template <class _Alloc>
  264. _LIBCPP_INLINE_VISIBILITY
  265. queue(const container_type& __c, const _Alloc& __a,
  266. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  267. : c(__c, __a) {}
  268. #ifndef _LIBCPP_CXX03_LANG
  269. template <class _Alloc>
  270. _LIBCPP_INLINE_VISIBILITY
  271. queue(container_type&& __c, const _Alloc& __a,
  272. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  273. : c(_VSTD::move(__c), __a) {}
  274. template <class _Alloc>
  275. _LIBCPP_INLINE_VISIBILITY
  276. queue(queue&& __q, const _Alloc& __a,
  277. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  278. : c(_VSTD::move(__q.c), __a) {}
  279. #endif // _LIBCPP_CXX03_LANG
  280. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  281. bool empty() const {return c.empty();}
  282. _LIBCPP_INLINE_VISIBILITY
  283. size_type size() const {return c.size();}
  284. _LIBCPP_INLINE_VISIBILITY
  285. reference front() {return c.front();}
  286. _LIBCPP_INLINE_VISIBILITY
  287. const_reference front() const {return c.front();}
  288. _LIBCPP_INLINE_VISIBILITY
  289. reference back() {return c.back();}
  290. _LIBCPP_INLINE_VISIBILITY
  291. const_reference back() const {return c.back();}
  292. _LIBCPP_INLINE_VISIBILITY
  293. void push(const value_type& __v) {c.push_back(__v);}
  294. #ifndef _LIBCPP_CXX03_LANG
  295. _LIBCPP_INLINE_VISIBILITY
  296. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  297. template <class... _Args>
  298. _LIBCPP_INLINE_VISIBILITY
  299. #if _LIBCPP_STD_VER > 14
  300. decltype(auto) emplace(_Args&&... __args)
  301. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  302. #else
  303. void emplace(_Args&&... __args)
  304. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  305. #endif
  306. #endif // _LIBCPP_CXX03_LANG
  307. _LIBCPP_INLINE_VISIBILITY
  308. void pop() {c.pop_front();}
  309. _LIBCPP_INLINE_VISIBILITY
  310. void swap(queue& __q)
  311. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  312. {
  313. using _VSTD::swap;
  314. swap(c, __q.c);
  315. }
  316. template <class _T1, class _C1>
  317. friend
  318. _LIBCPP_INLINE_VISIBILITY
  319. bool
  320. operator==(const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  321. template <class _T1, class _C1>
  322. friend
  323. _LIBCPP_INLINE_VISIBILITY
  324. bool
  325. operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  326. };
  327. #if _LIBCPP_STD_VER > 14
  328. template<class _Container,
  329. class = enable_if_t<!__is_allocator<_Container>::value>
  330. >
  331. queue(_Container)
  332. -> queue<typename _Container::value_type, _Container>;
  333. template<class _Container,
  334. class _Alloc,
  335. class = enable_if_t<!__is_allocator<_Container>::value>,
  336. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  337. >
  338. queue(_Container, _Alloc)
  339. -> queue<typename _Container::value_type, _Container>;
  340. #endif
  341. #if _LIBCPP_STD_VER > 20
  342. template <class _InputIterator,
  343. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  344. queue(_InputIterator, _InputIterator)
  345. -> queue<__iter_value_type<_InputIterator>>;
  346. template <class _InputIterator,
  347. class _Alloc,
  348. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  349. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  350. queue(_InputIterator, _InputIterator, _Alloc)
  351. -> queue<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  352. #endif
  353. template <class _Tp, class _Container>
  354. inline _LIBCPP_INLINE_VISIBILITY
  355. bool
  356. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  357. {
  358. return __x.c == __y.c;
  359. }
  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 == __y);
  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 __y < __x;
  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 !(__x < __y);
  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 !(__y < __x);
  394. }
  395. template <class _Tp, class _Container>
  396. inline _LIBCPP_INLINE_VISIBILITY
  397. __enable_if_t<__is_swappable<_Container>::value, void>
  398. swap(queue<_Tp, _Container>& __x, queue<_Tp, _Container>& __y)
  399. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  400. {
  401. __x.swap(__y);
  402. }
  403. template <class _Tp, class _Container, class _Alloc>
  404. struct _LIBCPP_TEMPLATE_VIS uses_allocator<queue<_Tp, _Container>, _Alloc>
  405. : public uses_allocator<_Container, _Alloc>
  406. {
  407. };
  408. template <class _Tp, class _Container = vector<_Tp>,
  409. class _Compare = less<typename _Container::value_type> >
  410. class _LIBCPP_TEMPLATE_VIS priority_queue
  411. {
  412. public:
  413. typedef _Container container_type;
  414. typedef _Compare value_compare;
  415. typedef typename container_type::value_type value_type;
  416. typedef typename container_type::reference reference;
  417. typedef typename container_type::const_reference const_reference;
  418. typedef typename container_type::size_type size_type;
  419. static_assert((is_same<_Tp, value_type>::value), "" );
  420. protected:
  421. container_type c;
  422. value_compare comp;
  423. public:
  424. _LIBCPP_INLINE_VISIBILITY
  425. priority_queue()
  426. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value &&
  427. is_nothrow_default_constructible<value_compare>::value)
  428. : c(), comp() {}
  429. _LIBCPP_INLINE_VISIBILITY
  430. priority_queue(const priority_queue& __q) : c(__q.c), comp(__q.comp) {}
  431. _LIBCPP_INLINE_VISIBILITY
  432. priority_queue& operator=(const priority_queue& __q)
  433. {c = __q.c; comp = __q.comp; return *this;}
  434. #ifndef _LIBCPP_CXX03_LANG
  435. _LIBCPP_INLINE_VISIBILITY
  436. priority_queue(priority_queue&& __q)
  437. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value &&
  438. is_nothrow_move_constructible<value_compare>::value)
  439. : c(_VSTD::move(__q.c)), comp(_VSTD::move(__q.comp)) {}
  440. _LIBCPP_INLINE_VISIBILITY
  441. priority_queue& operator=(priority_queue&& __q)
  442. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value &&
  443. is_nothrow_move_assignable<value_compare>::value)
  444. {c = _VSTD::move(__q.c); comp = _VSTD::move(__q.comp); return *this;}
  445. #endif // _LIBCPP_CXX03_LANG
  446. _LIBCPP_INLINE_VISIBILITY
  447. explicit priority_queue(const value_compare& __comp)
  448. : c(), comp(__comp) {}
  449. _LIBCPP_INLINE_VISIBILITY
  450. priority_queue(const value_compare& __comp, const container_type& __c);
  451. #ifndef _LIBCPP_CXX03_LANG
  452. _LIBCPP_INLINE_VISIBILITY
  453. priority_queue(const value_compare& __comp, container_type&& __c);
  454. #endif
  455. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  456. _LIBCPP_INLINE_VISIBILITY
  457. priority_queue(_InputIter __f, _InputIter __l,
  458. const value_compare& __comp = value_compare());
  459. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  460. _LIBCPP_INLINE_VISIBILITY
  461. priority_queue(_InputIter __f, _InputIter __l,
  462. const value_compare& __comp, const container_type& __c);
  463. #ifndef _LIBCPP_CXX03_LANG
  464. template <class _InputIter, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  465. _LIBCPP_INLINE_VISIBILITY
  466. priority_queue(_InputIter __f, _InputIter __l,
  467. const value_compare& __comp, container_type&& __c);
  468. #endif // _LIBCPP_CXX03_LANG
  469. template <class _Alloc>
  470. _LIBCPP_INLINE_VISIBILITY
  471. explicit priority_queue(const _Alloc& __a,
  472. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  473. template <class _Alloc>
  474. _LIBCPP_INLINE_VISIBILITY
  475. priority_queue(const value_compare& __comp, const _Alloc& __a,
  476. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  477. template <class _Alloc>
  478. _LIBCPP_INLINE_VISIBILITY
  479. priority_queue(const value_compare& __comp, const container_type& __c,
  480. const _Alloc& __a,
  481. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  482. template <class _Alloc>
  483. _LIBCPP_INLINE_VISIBILITY
  484. priority_queue(const priority_queue& __q, const _Alloc& __a,
  485. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  486. #ifndef _LIBCPP_CXX03_LANG
  487. template <class _Alloc>
  488. _LIBCPP_INLINE_VISIBILITY
  489. priority_queue(const value_compare& __comp, container_type&& __c,
  490. const _Alloc& __a,
  491. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  492. template <class _Alloc>
  493. _LIBCPP_INLINE_VISIBILITY
  494. priority_queue(priority_queue&& __q, const _Alloc& __a,
  495. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  496. #endif // _LIBCPP_CXX03_LANG
  497. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  498. _LIBCPP_INLINE_VISIBILITY
  499. priority_queue(_InputIter __f, _InputIter __l, const _Alloc& __a,
  500. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  501. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  502. _LIBCPP_INLINE_VISIBILITY
  503. priority_queue(_InputIter __f, _InputIter __l,
  504. const value_compare& __comp, const _Alloc& __a,
  505. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  506. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  507. _LIBCPP_INLINE_VISIBILITY
  508. priority_queue(_InputIter __f, _InputIter __l,
  509. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  510. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  511. #ifndef _LIBCPP_CXX03_LANG
  512. template <class _InputIter, class _Alloc, class = __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value> >
  513. _LIBCPP_INLINE_VISIBILITY
  514. priority_queue(_InputIter __f, _InputIter __l,
  515. const value_compare& __comp, container_type&& __c, const _Alloc& __a,
  516. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0);
  517. #endif // _LIBCPP_CXX03_LANG
  518. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  519. bool empty() const {return c.empty();}
  520. _LIBCPP_INLINE_VISIBILITY
  521. size_type size() const {return c.size();}
  522. _LIBCPP_INLINE_VISIBILITY
  523. const_reference top() const {return c.front();}
  524. _LIBCPP_INLINE_VISIBILITY
  525. void push(const value_type& __v);
  526. #ifndef _LIBCPP_CXX03_LANG
  527. _LIBCPP_INLINE_VISIBILITY
  528. void push(value_type&& __v);
  529. template <class... _Args>
  530. _LIBCPP_INLINE_VISIBILITY
  531. void emplace(_Args&&... __args);
  532. #endif // _LIBCPP_CXX03_LANG
  533. _LIBCPP_INLINE_VISIBILITY
  534. void pop();
  535. _LIBCPP_INLINE_VISIBILITY
  536. void swap(priority_queue& __q)
  537. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  538. __is_nothrow_swappable<value_compare>::value);
  539. };
  540. #if _LIBCPP_STD_VER >= 17
  541. template <class _Compare,
  542. class _Container,
  543. class = enable_if_t<!__is_allocator<_Compare>::value>,
  544. class = enable_if_t<!__is_allocator<_Container>::value>
  545. >
  546. priority_queue(_Compare, _Container)
  547. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  548. template<class _InputIterator,
  549. class _Compare = less<__iter_value_type<_InputIterator>>,
  550. class _Container = vector<__iter_value_type<_InputIterator>>,
  551. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  552. class = enable_if_t<!__is_allocator<_Compare>::value>,
  553. class = enable_if_t<!__is_allocator<_Container>::value>
  554. >
  555. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), _Container = _Container())
  556. -> priority_queue<__iter_value_type<_InputIterator>, _Container, _Compare>;
  557. template<class _Compare,
  558. class _Container,
  559. class _Alloc,
  560. class = enable_if_t<!__is_allocator<_Compare>::value>,
  561. class = enable_if_t<!__is_allocator<_Container>::value>,
  562. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  563. >
  564. priority_queue(_Compare, _Container, _Alloc)
  565. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  566. template<class _InputIterator, class _Allocator,
  567. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  568. class = enable_if_t<__is_allocator<_Allocator>::value>
  569. >
  570. priority_queue(_InputIterator, _InputIterator, _Allocator)
  571. -> priority_queue<__iter_value_type<_InputIterator>,
  572. vector<__iter_value_type<_InputIterator>, _Allocator>,
  573. less<__iter_value_type<_InputIterator>>>;
  574. template<class _InputIterator, class _Compare, class _Allocator,
  575. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  576. class = enable_if_t<!__is_allocator<_Compare>::value>,
  577. class = enable_if_t<__is_allocator<_Allocator>::value>
  578. >
  579. priority_queue(_InputIterator, _InputIterator, _Compare, _Allocator)
  580. -> priority_queue<__iter_value_type<_InputIterator>,
  581. vector<__iter_value_type<_InputIterator>, _Allocator>, _Compare>;
  582. template<class _InputIterator, class _Compare, class _Container, class _Alloc,
  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<_Container>::value>,
  586. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  587. >
  588. priority_queue(_InputIterator, _InputIterator, _Compare, _Container, _Alloc)
  589. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  590. #endif
  591. template <class _Tp, class _Container, class _Compare>
  592. inline
  593. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp,
  594. const container_type& __c)
  595. : c(__c),
  596. comp(__comp)
  597. {
  598. _VSTD::make_heap(c.begin(), c.end(), comp);
  599. }
  600. #ifndef _LIBCPP_CXX03_LANG
  601. template <class _Tp, class _Container, class _Compare>
  602. inline
  603. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  604. container_type&& __c)
  605. : c(_VSTD::move(__c)),
  606. comp(__comp)
  607. {
  608. _VSTD::make_heap(c.begin(), c.end(), comp);
  609. }
  610. #endif // _LIBCPP_CXX03_LANG
  611. template <class _Tp, class _Container, class _Compare>
  612. template <class _InputIter, class>
  613. inline
  614. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  615. const value_compare& __comp)
  616. : c(__f, __l),
  617. comp(__comp)
  618. {
  619. _VSTD::make_heap(c.begin(), c.end(), comp);
  620. }
  621. template <class _Tp, class _Container, class _Compare>
  622. template <class _InputIter, class>
  623. inline
  624. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  625. const value_compare& __comp,
  626. const container_type& __c)
  627. : c(__c),
  628. comp(__comp)
  629. {
  630. c.insert(c.end(), __f, __l);
  631. _VSTD::make_heap(c.begin(), c.end(), comp);
  632. }
  633. #ifndef _LIBCPP_CXX03_LANG
  634. template <class _Tp, class _Container, class _Compare>
  635. template <class _InputIter, class>
  636. inline
  637. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  638. const value_compare& __comp,
  639. container_type&& __c)
  640. : c(_VSTD::move(__c)),
  641. comp(__comp)
  642. {
  643. c.insert(c.end(), __f, __l);
  644. _VSTD::make_heap(c.begin(), c.end(), comp);
  645. }
  646. #endif // _LIBCPP_CXX03_LANG
  647. template <class _Tp, class _Container, class _Compare>
  648. template <class _Alloc>
  649. inline
  650. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Alloc& __a,
  651. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  652. : c(__a)
  653. {
  654. }
  655. template <class _Tp, class _Container, class _Compare>
  656. template <class _Alloc>
  657. inline
  658. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  659. const _Alloc& __a,
  660. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  661. : c(__a),
  662. comp(__comp)
  663. {
  664. }
  665. template <class _Tp, class _Container, class _Compare>
  666. template <class _Alloc>
  667. inline
  668. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  669. const container_type& __c,
  670. const _Alloc& __a,
  671. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  672. : c(__c, __a),
  673. comp(__comp)
  674. {
  675. _VSTD::make_heap(c.begin(), c.end(), comp);
  676. }
  677. template <class _Tp, class _Container, class _Compare>
  678. template <class _Alloc>
  679. inline
  680. priority_queue<_Tp, _Container, _Compare>::priority_queue(const priority_queue& __q,
  681. const _Alloc& __a,
  682. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  683. : c(__q.c, __a),
  684. comp(__q.comp)
  685. {
  686. }
  687. #ifndef _LIBCPP_CXX03_LANG
  688. template <class _Tp, class _Container, class _Compare>
  689. template <class _Alloc>
  690. inline
  691. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  692. container_type&& __c,
  693. const _Alloc& __a,
  694. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  695. : c(_VSTD::move(__c), __a),
  696. comp(__comp)
  697. {
  698. _VSTD::make_heap(c.begin(), c.end(), comp);
  699. }
  700. template <class _Tp, class _Container, class _Compare>
  701. template <class _Alloc>
  702. inline
  703. priority_queue<_Tp, _Container, _Compare>::priority_queue(priority_queue&& __q,
  704. const _Alloc& __a,
  705. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  706. : c(_VSTD::move(__q.c), __a),
  707. comp(_VSTD::move(__q.comp))
  708. {
  709. }
  710. #endif // _LIBCPP_CXX03_LANG
  711. template <class _Tp, class _Container, class _Compare>
  712. template <class _InputIter, class _Alloc, class>
  713. inline
  714. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  715. _InputIter __f, _InputIter __l, const _Alloc& __a,
  716. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  717. : c(__f, __l, __a),
  718. comp()
  719. {
  720. _VSTD::make_heap(c.begin(), c.end(), comp);
  721. }
  722. template <class _Tp, class _Container, class _Compare>
  723. template <class _InputIter, class _Alloc, class>
  724. inline
  725. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  726. _InputIter __f, _InputIter __l,
  727. const value_compare& __comp, const _Alloc& __a,
  728. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  729. : c(__f, __l, __a),
  730. comp(__comp)
  731. {
  732. _VSTD::make_heap(c.begin(), c.end(), comp);
  733. }
  734. template <class _Tp, class _Container, class _Compare>
  735. template <class _InputIter, class _Alloc, class>
  736. inline
  737. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  738. _InputIter __f, _InputIter __l,
  739. const value_compare& __comp, const container_type& __c, const _Alloc& __a,
  740. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  741. : c(__c, __a),
  742. comp(__comp)
  743. {
  744. c.insert(c.end(), __f, __l);
  745. _VSTD::make_heap(c.begin(), c.end(), comp);
  746. }
  747. #ifndef _LIBCPP_CXX03_LANG
  748. template <class _Tp, class _Container, class _Compare>
  749. template <class _InputIter, class _Alloc, class>
  750. inline
  751. priority_queue<_Tp, _Container, _Compare>::priority_queue(
  752. _InputIter __f, _InputIter __l, const value_compare& __comp,
  753. container_type&& __c, const _Alloc& __a,
  754. __enable_if_t<uses_allocator<container_type, _Alloc>::value>*)
  755. : c(_VSTD::move(__c), __a),
  756. comp(__comp)
  757. {
  758. c.insert(c.end(), __f, __l);
  759. _VSTD::make_heap(c.begin(), c.end(), comp);
  760. }
  761. #endif // _LIBCPP_CXX03_LANG
  762. template <class _Tp, class _Container, class _Compare>
  763. inline
  764. void
  765. priority_queue<_Tp, _Container, _Compare>::push(const value_type& __v)
  766. {
  767. c.push_back(__v);
  768. _VSTD::push_heap(c.begin(), c.end(), comp);
  769. }
  770. #ifndef _LIBCPP_CXX03_LANG
  771. template <class _Tp, class _Container, class _Compare>
  772. inline
  773. void
  774. priority_queue<_Tp, _Container, _Compare>::push(value_type&& __v)
  775. {
  776. c.push_back(_VSTD::move(__v));
  777. _VSTD::push_heap(c.begin(), c.end(), comp);
  778. }
  779. template <class _Tp, class _Container, class _Compare>
  780. template <class... _Args>
  781. inline
  782. void
  783. priority_queue<_Tp, _Container, _Compare>::emplace(_Args&&... __args)
  784. {
  785. c.emplace_back(_VSTD::forward<_Args>(__args)...);
  786. _VSTD::push_heap(c.begin(), c.end(), comp);
  787. }
  788. #endif // _LIBCPP_CXX03_LANG
  789. template <class _Tp, class _Container, class _Compare>
  790. inline
  791. void
  792. priority_queue<_Tp, _Container, _Compare>::pop()
  793. {
  794. _VSTD::pop_heap(c.begin(), c.end(), comp);
  795. c.pop_back();
  796. }
  797. template <class _Tp, class _Container, class _Compare>
  798. inline
  799. void
  800. priority_queue<_Tp, _Container, _Compare>::swap(priority_queue& __q)
  801. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  802. __is_nothrow_swappable<value_compare>::value)
  803. {
  804. using _VSTD::swap;
  805. swap(c, __q.c);
  806. swap(comp, __q.comp);
  807. }
  808. template <class _Tp, class _Container, class _Compare>
  809. inline _LIBCPP_INLINE_VISIBILITY
  810. __enable_if_t<
  811. __is_swappable<_Container>::value && __is_swappable<_Compare>::value,
  812. void
  813. >
  814. swap(priority_queue<_Tp, _Container, _Compare>& __x,
  815. priority_queue<_Tp, _Container, _Compare>& __y)
  816. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  817. {
  818. __x.swap(__y);
  819. }
  820. template <class _Tp, class _Container, class _Compare, class _Alloc>
  821. struct _LIBCPP_TEMPLATE_VIS uses_allocator<priority_queue<_Tp, _Container, _Compare>, _Alloc>
  822. : public uses_allocator<_Container, _Alloc>
  823. {
  824. };
  825. _LIBCPP_END_NAMESPACE_STD
  826. #endif // _LIBCPP_QUEUE