queue 34 KB

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