queue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // -*- C++ -*-
  2. //===--------------------------- queue ------------------------------------===//
  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 Alloc>
  36. explicit queue(const Alloc& a);
  37. template <class Alloc>
  38. queue(const container_type& c, const Alloc& a);
  39. template <class Alloc>
  40. queue(container_type&& c, const Alloc& a);
  41. template <class Alloc>
  42. queue(const queue& q, const Alloc& a);
  43. template <class Alloc>
  44. queue(queue&& q, const Alloc& a);
  45. bool empty() const;
  46. size_type size() const;
  47. reference front();
  48. const_reference front() const;
  49. reference back();
  50. const_reference back() const;
  51. void push(const value_type& v);
  52. void push(value_type&& v);
  53. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  54. void pop();
  55. void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>)
  56. };
  57. template<class Container>
  58. queue(Container) -> queue<typename Container::value_type, Container>; // C++17
  59. template<class Container, class Allocator>
  60. queue(Container, Allocator) -> queue<typename Container::value_type, Container>; // C++17
  61. template <class T, class Container>
  62. bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
  63. template <class T, class Container>
  64. bool operator< (const queue<T, Container>& x,const queue<T, Container>& y);
  65. template <class T, class Container>
  66. bool operator!=(const queue<T, Container>& x,const queue<T, Container>& y);
  67. template <class T, class Container>
  68. bool operator> (const queue<T, Container>& x,const queue<T, Container>& y);
  69. template <class T, class Container>
  70. bool operator>=(const queue<T, Container>& x,const queue<T, Container>& y);
  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. void swap(queue<T, Container>& x, queue<T, Container>& y)
  75. noexcept(noexcept(x.swap(y)));
  76. template <class T, class Container = vector<T>,
  77. class Compare = less<typename Container::value_type>>
  78. class priority_queue
  79. {
  80. public:
  81. typedef Container container_type;
  82. typedef typename container_type::value_type value_type;
  83. typedef typename container_type::reference reference;
  84. typedef typename container_type::const_reference const_reference;
  85. typedef typename container_type::size_type size_type;
  86. protected:
  87. container_type c;
  88. Compare comp;
  89. public:
  90. priority_queue() = default;
  91. ~priority_queue() = default;
  92. priority_queue(const priority_queue& q) = default;
  93. priority_queue(priority_queue&& q) = default;
  94. priority_queue& operator=(const priority_queue& q) = default;
  95. priority_queue& operator=(priority_queue&& q) = default;
  96. explicit priority_queue(const Compare& comp);
  97. priority_queue(const Compare& comp, const container_type& c);
  98. explicit priority_queue(const Compare& comp, container_type&& c);
  99. template <class InputIterator>
  100. priority_queue(InputIterator first, InputIterator last,
  101. const Compare& comp = Compare());
  102. template <class InputIterator>
  103. priority_queue(InputIterator first, InputIterator last,
  104. const Compare& comp, const container_type& c);
  105. template <class InputIterator>
  106. priority_queue(InputIterator first, InputIterator last,
  107. const Compare& comp, container_type&& c);
  108. template <class Alloc>
  109. explicit priority_queue(const Alloc& a);
  110. template <class Alloc>
  111. priority_queue(const Compare& comp, const Alloc& a);
  112. template <class Alloc>
  113. priority_queue(const Compare& comp, const container_type& c,
  114. const Alloc& a);
  115. template <class Alloc>
  116. priority_queue(const Compare& comp, container_type&& c,
  117. const Alloc& a);
  118. template <class Alloc>
  119. priority_queue(const priority_queue& q, const Alloc& a);
  120. template <class Alloc>
  121. priority_queue(priority_queue&& q, const Alloc& a);
  122. bool empty() const;
  123. size_type size() const;
  124. const_reference top() const;
  125. void push(const value_type& v);
  126. void push(value_type&& v);
  127. template <class... Args> void emplace(Args&&... args);
  128. void pop();
  129. void swap(priority_queue& q)
  130. noexcept(is_nothrow_swappable_v<Container> &&
  131. is_nothrow_swappable_v<Comp>)
  132. };
  133. template <class Compare, class Container>
  134. priority_queue(Compare, Container)
  135. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  136. template<class InputIterator,
  137. class Compare = less<typename iterator_traits<InputIterator>::value_type>,
  138. class Container = vector<typename iterator_traits<InputIterator>::value_type>>
  139. priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
  140. -> priority_queue<typename iterator_traits<InputIterator>::value_type, Container, Compare>; // C++17
  141. template<class Compare, class Container, class Allocator>
  142. priority_queue(Compare, Container, Allocator)
  143. -> priority_queue<typename Container::value_type, Container, Compare>; // C++17
  144. template <class T, class Container, class Compare>
  145. void swap(priority_queue<T, Container, Compare>& x,
  146. priority_queue<T, Container, Compare>& y)
  147. noexcept(noexcept(x.swap(y)));
  148. } // std
  149. */
  150. #include <__config>
  151. #include <deque>
  152. #include <vector>
  153. #include <functional>
  154. #include <algorithm>
  155. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  156. #pragma GCC system_header
  157. #endif
  158. _LIBCPP_BEGIN_NAMESPACE_STD
  159. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS queue;
  160. template <class _Tp, class _Container>
  161. _LIBCPP_INLINE_VISIBILITY
  162. bool
  163. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  164. template <class _Tp, class _Container>
  165. _LIBCPP_INLINE_VISIBILITY
  166. bool
  167. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y);
  168. template <class _Tp, class _Container /*= deque<_Tp>*/>
  169. class _LIBCPP_TEMPLATE_VIS queue
  170. {
  171. public:
  172. typedef _Container container_type;
  173. typedef typename container_type::value_type value_type;
  174. typedef typename container_type::reference reference;
  175. typedef typename container_type::const_reference const_reference;
  176. typedef typename container_type::size_type size_type;
  177. static_assert((is_same<_Tp, value_type>::value), "" );
  178. protected:
  179. container_type c;
  180. public:
  181. _LIBCPP_INLINE_VISIBILITY
  182. queue()
  183. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  184. : c() {}
  185. _LIBCPP_INLINE_VISIBILITY
  186. queue(const queue& __q) : c(__q.c) {}
  187. _LIBCPP_INLINE_VISIBILITY
  188. queue& operator=(const queue& __q) {c = __q.c; return *this;}
  189. #ifndef _LIBCPP_CXX03_LANG
  190. _LIBCPP_INLINE_VISIBILITY
  191. queue(queue&& __q)
  192. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  193. : c(_VSTD::move(__q.c)) {}
  194. _LIBCPP_INLINE_VISIBILITY
  195. queue& operator=(queue&& __q)
  196. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  197. {c = _VSTD::move(__q.c); return *this;}
  198. #endif // _LIBCPP_CXX03_LANG
  199. _LIBCPP_INLINE_VISIBILITY
  200. explicit queue(const container_type& __c) : c(__c) {}
  201. #ifndef _LIBCPP_CXX03_LANG
  202. _LIBCPP_INLINE_VISIBILITY
  203. explicit queue(container_type&& __c) : c(_VSTD::move(__c)) {}
  204. #endif // _LIBCPP_CXX03_LANG
  205. template <class _Alloc>
  206. _LIBCPP_INLINE_VISIBILITY
  207. explicit queue(const _Alloc& __a,
  208. typename enable_if<uses_allocator<container_type,
  209. _Alloc>::value>::type* = 0)
  210. : c(__a) {}
  211. template <class _Alloc>
  212. _LIBCPP_INLINE_VISIBILITY
  213. queue(const queue& __q, const _Alloc& __a,
  214. typename enable_if<uses_allocator<container_type,
  215. _Alloc>::value>::type* = 0)
  216. : c(__q.c, __a) {}
  217. template <class _Alloc>
  218. _LIBCPP_INLINE_VISIBILITY
  219. queue(const container_type& __c, const _Alloc& __a,
  220. typename enable_if<uses_allocator<container_type,
  221. _Alloc>::value>::type* = 0)
  222. : c(__c, __a) {}
  223. #ifndef _LIBCPP_CXX03_LANG
  224. template <class _Alloc>
  225. _LIBCPP_INLINE_VISIBILITY
  226. queue(container_type&& __c, const _Alloc& __a,
  227. typename enable_if<uses_allocator<container_type,
  228. _Alloc>::value>::type* = 0)
  229. : c(_VSTD::move(__c), __a) {}
  230. template <class _Alloc>
  231. _LIBCPP_INLINE_VISIBILITY
  232. queue(queue&& __q, const _Alloc& __a,
  233. typename enable_if<uses_allocator<container_type,
  234. _Alloc>::value>::type* = 0)
  235. : c(_VSTD::move(__q.c), __a) {}
  236. #endif // _LIBCPP_CXX03_LANG
  237. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  238. bool empty() const {return c.empty();}
  239. _LIBCPP_INLINE_VISIBILITY
  240. size_type size() const {return c.size();}
  241. _LIBCPP_INLINE_VISIBILITY
  242. reference front() {return c.front();}
  243. _LIBCPP_INLINE_VISIBILITY
  244. const_reference front() const {return c.front();}
  245. _LIBCPP_INLINE_VISIBILITY
  246. reference back() {return c.back();}
  247. _LIBCPP_INLINE_VISIBILITY
  248. const_reference back() const {return c.back();}
  249. _LIBCPP_INLINE_VISIBILITY
  250. void push(const value_type& __v) {c.push_back(__v);}
  251. #ifndef _LIBCPP_CXX03_LANG
  252. _LIBCPP_INLINE_VISIBILITY
  253. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  254. template <class... _Args>
  255. _LIBCPP_INLINE_VISIBILITY
  256. #if _LIBCPP_STD_VER > 14
  257. decltype(auto) emplace(_Args&&... __args)
  258. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  259. #else
  260. void emplace(_Args&&... __args)
  261. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  262. #endif
  263. #endif // _LIBCPP_CXX03_LANG
  264. _LIBCPP_INLINE_VISIBILITY
  265. void pop() {c.pop_front();}
  266. _LIBCPP_INLINE_VISIBILITY
  267. void swap(queue& __q)
  268. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  269. {
  270. using _VSTD::swap;
  271. swap(c, __q.c);
  272. }
  273. template <class _T1, class _C1>
  274. friend
  275. _LIBCPP_INLINE_VISIBILITY
  276. bool
  277. operator==(const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  278. template <class _T1, class _C1>
  279. friend
  280. _LIBCPP_INLINE_VISIBILITY
  281. bool
  282. operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
  283. };
  284. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  285. template<class _Container,
  286. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
  287. >
  288. queue(_Container)
  289. -> queue<typename _Container::value_type, _Container>;
  290. template<class _Container,
  291. class _Alloc,
  292. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
  293. class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
  294. >
  295. queue(_Container, _Alloc)
  296. -> queue<typename _Container::value_type, _Container>;
  297. #endif
  298. template <class _Tp, class _Container>
  299. inline _LIBCPP_INLINE_VISIBILITY
  300. bool
  301. operator==(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  302. {
  303. return __x.c == __y.c;
  304. }
  305. template <class _Tp, class _Container>
  306. inline _LIBCPP_INLINE_VISIBILITY
  307. bool
  308. operator< (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  309. {
  310. return __x.c < __y.c;
  311. }
  312. template <class _Tp, class _Container>
  313. inline _LIBCPP_INLINE_VISIBILITY
  314. bool
  315. operator!=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  316. {
  317. return !(__x == __y);
  318. }
  319. template <class _Tp, class _Container>
  320. inline _LIBCPP_INLINE_VISIBILITY
  321. bool
  322. operator> (const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  323. {
  324. return __y < __x;
  325. }
  326. template <class _Tp, class _Container>
  327. inline _LIBCPP_INLINE_VISIBILITY
  328. bool
  329. operator>=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  330. {
  331. return !(__x < __y);
  332. }
  333. template <class _Tp, class _Container>
  334. inline _LIBCPP_INLINE_VISIBILITY
  335. bool
  336. operator<=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y)
  337. {
  338. return !(__y < __x);
  339. }
  340. template <class _Tp, class _Container>
  341. inline _LIBCPP_INLINE_VISIBILITY
  342. typename enable_if<
  343. __is_swappable<_Container>::value,
  344. void
  345. >::type
  346. swap(queue<_Tp, _Container>& __x, queue<_Tp, _Container>& __y)
  347. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  348. {
  349. __x.swap(__y);
  350. }
  351. template <class _Tp, class _Container, class _Alloc>
  352. struct _LIBCPP_TEMPLATE_VIS uses_allocator<queue<_Tp, _Container>, _Alloc>
  353. : public uses_allocator<_Container, _Alloc>
  354. {
  355. };
  356. template <class _Tp, class _Container = vector<_Tp>,
  357. class _Compare = less<typename _Container::value_type> >
  358. class _LIBCPP_TEMPLATE_VIS priority_queue
  359. {
  360. public:
  361. typedef _Container container_type;
  362. typedef _Compare value_compare;
  363. typedef typename container_type::value_type value_type;
  364. typedef typename container_type::reference reference;
  365. typedef typename container_type::const_reference const_reference;
  366. typedef typename container_type::size_type size_type;
  367. static_assert((is_same<_Tp, value_type>::value), "" );
  368. protected:
  369. container_type c;
  370. value_compare comp;
  371. public:
  372. _LIBCPP_INLINE_VISIBILITY
  373. priority_queue()
  374. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value &&
  375. is_nothrow_default_constructible<value_compare>::value)
  376. : c(), comp() {}
  377. _LIBCPP_INLINE_VISIBILITY
  378. priority_queue(const priority_queue& __q) : c(__q.c), comp(__q.comp) {}
  379. _LIBCPP_INLINE_VISIBILITY
  380. priority_queue& operator=(const priority_queue& __q)
  381. {c = __q.c; comp = __q.comp; return *this;}
  382. #ifndef _LIBCPP_CXX03_LANG
  383. _LIBCPP_INLINE_VISIBILITY
  384. priority_queue(priority_queue&& __q)
  385. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value &&
  386. is_nothrow_move_constructible<value_compare>::value)
  387. : c(_VSTD::move(__q.c)), comp(_VSTD::move(__q.comp)) {}
  388. _LIBCPP_INLINE_VISIBILITY
  389. priority_queue& operator=(priority_queue&& __q)
  390. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value &&
  391. is_nothrow_move_assignable<value_compare>::value)
  392. {c = _VSTD::move(__q.c); comp = _VSTD::move(__q.comp); return *this;}
  393. #endif // _LIBCPP_CXX03_LANG
  394. _LIBCPP_INLINE_VISIBILITY
  395. explicit priority_queue(const value_compare& __comp)
  396. : c(), comp(__comp) {}
  397. _LIBCPP_INLINE_VISIBILITY
  398. priority_queue(const value_compare& __comp, const container_type& __c);
  399. #ifndef _LIBCPP_CXX03_LANG
  400. _LIBCPP_INLINE_VISIBILITY
  401. explicit priority_queue(const value_compare& __comp, container_type&& __c);
  402. #endif
  403. template <class _InputIter>
  404. _LIBCPP_INLINE_VISIBILITY
  405. priority_queue(_InputIter __f, _InputIter __l,
  406. const value_compare& __comp = value_compare());
  407. template <class _InputIter>
  408. _LIBCPP_INLINE_VISIBILITY
  409. priority_queue(_InputIter __f, _InputIter __l,
  410. const value_compare& __comp, const container_type& __c);
  411. #ifndef _LIBCPP_CXX03_LANG
  412. template <class _InputIter>
  413. _LIBCPP_INLINE_VISIBILITY
  414. priority_queue(_InputIter __f, _InputIter __l,
  415. const value_compare& __comp, container_type&& __c);
  416. #endif // _LIBCPP_CXX03_LANG
  417. template <class _Alloc>
  418. _LIBCPP_INLINE_VISIBILITY
  419. explicit priority_queue(const _Alloc& __a,
  420. typename enable_if<uses_allocator<container_type,
  421. _Alloc>::value>::type* = 0);
  422. template <class _Alloc>
  423. _LIBCPP_INLINE_VISIBILITY
  424. priority_queue(const value_compare& __comp, const _Alloc& __a,
  425. typename enable_if<uses_allocator<container_type,
  426. _Alloc>::value>::type* = 0);
  427. template <class _Alloc>
  428. _LIBCPP_INLINE_VISIBILITY
  429. priority_queue(const value_compare& __comp, const container_type& __c,
  430. const _Alloc& __a,
  431. typename enable_if<uses_allocator<container_type,
  432. _Alloc>::value>::type* = 0);
  433. template <class _Alloc>
  434. _LIBCPP_INLINE_VISIBILITY
  435. priority_queue(const priority_queue& __q, const _Alloc& __a,
  436. typename enable_if<uses_allocator<container_type,
  437. _Alloc>::value>::type* = 0);
  438. #ifndef _LIBCPP_CXX03_LANG
  439. template <class _Alloc>
  440. _LIBCPP_INLINE_VISIBILITY
  441. priority_queue(const value_compare& __comp, container_type&& __c,
  442. const _Alloc& __a,
  443. typename enable_if<uses_allocator<container_type,
  444. _Alloc>::value>::type* = 0);
  445. template <class _Alloc>
  446. _LIBCPP_INLINE_VISIBILITY
  447. priority_queue(priority_queue&& __q, const _Alloc& __a,
  448. typename enable_if<uses_allocator<container_type,
  449. _Alloc>::value>::type* = 0);
  450. #endif // _LIBCPP_CXX03_LANG
  451. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  452. bool empty() const {return c.empty();}
  453. _LIBCPP_INLINE_VISIBILITY
  454. size_type size() const {return c.size();}
  455. _LIBCPP_INLINE_VISIBILITY
  456. const_reference top() const {return c.front();}
  457. _LIBCPP_INLINE_VISIBILITY
  458. void push(const value_type& __v);
  459. #ifndef _LIBCPP_CXX03_LANG
  460. _LIBCPP_INLINE_VISIBILITY
  461. void push(value_type&& __v);
  462. template <class... _Args>
  463. _LIBCPP_INLINE_VISIBILITY
  464. void emplace(_Args&&... __args);
  465. #endif // _LIBCPP_CXX03_LANG
  466. _LIBCPP_INLINE_VISIBILITY
  467. void pop();
  468. _LIBCPP_INLINE_VISIBILITY
  469. void swap(priority_queue& __q)
  470. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  471. __is_nothrow_swappable<value_compare>::value);
  472. };
  473. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  474. template <class _Compare,
  475. class _Container,
  476. class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
  477. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
  478. >
  479. priority_queue(_Compare, _Container)
  480. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  481. template<class _InputIterator,
  482. class _Compare = less<typename iterator_traits<_InputIterator>::value_type>,
  483. class _Container = vector<typename iterator_traits<_InputIterator>::value_type>,
  484. class = typename enable_if< __is_input_iterator<_InputIterator>::value, nullptr_t>::type,
  485. class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
  486. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
  487. >
  488. priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), _Container = _Container())
  489. -> priority_queue<typename iterator_traits<_InputIterator>::value_type, _Container, _Compare>;
  490. template<class _Compare,
  491. class _Container,
  492. class _Alloc,
  493. class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
  494. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
  495. class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
  496. >
  497. priority_queue(_Compare, _Container, _Alloc)
  498. -> priority_queue<typename _Container::value_type, _Container, _Compare>;
  499. #endif
  500. template <class _Tp, class _Container, class _Compare>
  501. inline
  502. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp,
  503. const container_type& __c)
  504. : c(__c),
  505. comp(__comp)
  506. {
  507. _VSTD::make_heap(c.begin(), c.end(), comp);
  508. }
  509. #ifndef _LIBCPP_CXX03_LANG
  510. template <class _Tp, class _Container, class _Compare>
  511. inline
  512. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  513. container_type&& __c)
  514. : c(_VSTD::move(__c)),
  515. comp(__comp)
  516. {
  517. _VSTD::make_heap(c.begin(), c.end(), comp);
  518. }
  519. #endif // _LIBCPP_CXX03_LANG
  520. template <class _Tp, class _Container, class _Compare>
  521. template <class _InputIter>
  522. inline
  523. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  524. const value_compare& __comp)
  525. : c(__f, __l),
  526. comp(__comp)
  527. {
  528. _VSTD::make_heap(c.begin(), c.end(), comp);
  529. }
  530. template <class _Tp, class _Container, class _Compare>
  531. template <class _InputIter>
  532. inline
  533. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  534. const value_compare& __comp,
  535. const container_type& __c)
  536. : c(__c),
  537. comp(__comp)
  538. {
  539. c.insert(c.end(), __f, __l);
  540. _VSTD::make_heap(c.begin(), c.end(), comp);
  541. }
  542. #ifndef _LIBCPP_CXX03_LANG
  543. template <class _Tp, class _Container, class _Compare>
  544. template <class _InputIter>
  545. inline
  546. priority_queue<_Tp, _Container, _Compare>::priority_queue(_InputIter __f, _InputIter __l,
  547. const value_compare& __comp,
  548. container_type&& __c)
  549. : c(_VSTD::move(__c)),
  550. comp(__comp)
  551. {
  552. c.insert(c.end(), __f, __l);
  553. _VSTD::make_heap(c.begin(), c.end(), comp);
  554. }
  555. #endif // _LIBCPP_CXX03_LANG
  556. template <class _Tp, class _Container, class _Compare>
  557. template <class _Alloc>
  558. inline
  559. priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Alloc& __a,
  560. typename enable_if<uses_allocator<container_type,
  561. _Alloc>::value>::type*)
  562. : c(__a)
  563. {
  564. }
  565. template <class _Tp, class _Container, class _Compare>
  566. template <class _Alloc>
  567. inline
  568. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  569. const _Alloc& __a,
  570. typename enable_if<uses_allocator<container_type,
  571. _Alloc>::value>::type*)
  572. : c(__a),
  573. comp(__comp)
  574. {
  575. }
  576. template <class _Tp, class _Container, class _Compare>
  577. template <class _Alloc>
  578. inline
  579. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  580. const container_type& __c,
  581. const _Alloc& __a,
  582. typename enable_if<uses_allocator<container_type,
  583. _Alloc>::value>::type*)
  584. : c(__c, __a),
  585. comp(__comp)
  586. {
  587. _VSTD::make_heap(c.begin(), c.end(), comp);
  588. }
  589. template <class _Tp, class _Container, class _Compare>
  590. template <class _Alloc>
  591. inline
  592. priority_queue<_Tp, _Container, _Compare>::priority_queue(const priority_queue& __q,
  593. const _Alloc& __a,
  594. typename enable_if<uses_allocator<container_type,
  595. _Alloc>::value>::type*)
  596. : c(__q.c, __a),
  597. comp(__q.comp)
  598. {
  599. _VSTD::make_heap(c.begin(), c.end(), comp);
  600. }
  601. #ifndef _LIBCPP_CXX03_LANG
  602. template <class _Tp, class _Container, class _Compare>
  603. template <class _Alloc>
  604. inline
  605. priority_queue<_Tp, _Container, _Compare>::priority_queue(const value_compare& __comp,
  606. container_type&& __c,
  607. const _Alloc& __a,
  608. typename enable_if<uses_allocator<container_type,
  609. _Alloc>::value>::type*)
  610. : c(_VSTD::move(__c), __a),
  611. comp(__comp)
  612. {
  613. _VSTD::make_heap(c.begin(), c.end(), comp);
  614. }
  615. template <class _Tp, class _Container, class _Compare>
  616. template <class _Alloc>
  617. inline
  618. priority_queue<_Tp, _Container, _Compare>::priority_queue(priority_queue&& __q,
  619. const _Alloc& __a,
  620. typename enable_if<uses_allocator<container_type,
  621. _Alloc>::value>::type*)
  622. : c(_VSTD::move(__q.c), __a),
  623. comp(_VSTD::move(__q.comp))
  624. {
  625. _VSTD::make_heap(c.begin(), c.end(), comp);
  626. }
  627. #endif // _LIBCPP_CXX03_LANG
  628. template <class _Tp, class _Container, class _Compare>
  629. inline
  630. void
  631. priority_queue<_Tp, _Container, _Compare>::push(const value_type& __v)
  632. {
  633. c.push_back(__v);
  634. _VSTD::push_heap(c.begin(), c.end(), comp);
  635. }
  636. #ifndef _LIBCPP_CXX03_LANG
  637. template <class _Tp, class _Container, class _Compare>
  638. inline
  639. void
  640. priority_queue<_Tp, _Container, _Compare>::push(value_type&& __v)
  641. {
  642. c.push_back(_VSTD::move(__v));
  643. _VSTD::push_heap(c.begin(), c.end(), comp);
  644. }
  645. template <class _Tp, class _Container, class _Compare>
  646. template <class... _Args>
  647. inline
  648. void
  649. priority_queue<_Tp, _Container, _Compare>::emplace(_Args&&... __args)
  650. {
  651. c.emplace_back(_VSTD::forward<_Args>(__args)...);
  652. _VSTD::push_heap(c.begin(), c.end(), comp);
  653. }
  654. #endif // _LIBCPP_CXX03_LANG
  655. template <class _Tp, class _Container, class _Compare>
  656. inline
  657. void
  658. priority_queue<_Tp, _Container, _Compare>::pop()
  659. {
  660. _VSTD::pop_heap(c.begin(), c.end(), comp);
  661. c.pop_back();
  662. }
  663. template <class _Tp, class _Container, class _Compare>
  664. inline
  665. void
  666. priority_queue<_Tp, _Container, _Compare>::swap(priority_queue& __q)
  667. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value &&
  668. __is_nothrow_swappable<value_compare>::value)
  669. {
  670. using _VSTD::swap;
  671. swap(c, __q.c);
  672. swap(comp, __q.comp);
  673. }
  674. template <class _Tp, class _Container, class _Compare>
  675. inline _LIBCPP_INLINE_VISIBILITY
  676. typename enable_if<
  677. __is_swappable<_Container>::value
  678. && __is_swappable<_Compare>::value,
  679. void
  680. >::type
  681. swap(priority_queue<_Tp, _Container, _Compare>& __x,
  682. priority_queue<_Tp, _Container, _Compare>& __y)
  683. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  684. {
  685. __x.swap(__y);
  686. }
  687. template <class _Tp, class _Container, class _Compare, class _Alloc>
  688. struct _LIBCPP_TEMPLATE_VIS uses_allocator<priority_queue<_Tp, _Container, _Compare>, _Alloc>
  689. : public uses_allocator<_Container, _Alloc>
  690. {
  691. };
  692. _LIBCPP_END_NAMESPACE_STD
  693. #endif // _LIBCPP_QUEUE