stack 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_STACK
  10. #define _LIBCPP_STACK
  11. /*
  12. stack synopsis
  13. namespace std
  14. {
  15. template <class T, class Container = deque<T>>
  16. class stack
  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. stack() = default;
  28. ~stack() = default;
  29. stack(const stack& q) = default;
  30. stack(stack&& q) = default;
  31. stack& operator=(const stack& q) = default;
  32. stack& operator=(stack&& q) = default;
  33. explicit stack(const container_type& c);
  34. explicit stack(container_type&& c);
  35. template <class InputIterator> stack(InputIterator first, InputIterator last); // since C++23
  36. template<container-compatible-range<T> R> stack(from_range_t, R&& rg); // since C++23
  37. template <class Alloc> explicit stack(const Alloc& a);
  38. template <class Alloc> stack(const container_type& c, const Alloc& a);
  39. template <class Alloc> stack(container_type&& c, const Alloc& a);
  40. template <class Alloc> stack(const stack& c, const Alloc& a);
  41. template <class Alloc> stack(stack&& c, const Alloc& a);
  42. template<class InputIterator, class Alloc>
  43. stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
  44. template<container-compatible-range<T> R, class Alloc>
  45. stack(from_range_t, R&& rg, const Alloc&); // since C++23
  46. bool empty() const;
  47. size_type size() const;
  48. reference top();
  49. const_reference top() const;
  50. void push(const value_type& x);
  51. void push(value_type&& x);
  52. template<container-compatible-range<T> R>
  53. void push_range(R&& rg); // C++23
  54. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  55. void pop();
  56. void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
  57. };
  58. template<class Container>
  59. stack(Container) -> stack<typename Container::value_type, Container>; // C++17
  60. template<class InputIterator>
  61. stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
  62. template<ranges::input_range R>
  63. stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23
  64. template<class Container, class Allocator>
  65. stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
  66. template<class InputIterator, class Allocator>
  67. stack(InputIterator, InputIterator, Allocator)
  68. -> stack<iter-value-type<InputIterator>,
  69. deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
  70. template<ranges::input_range R, class Allocator>
  71. stack(from_range_t, R&&, Allocator)
  72. -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
  73. template <class T, class Container>
  74. bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  75. template <class T, class Container>
  76. bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
  77. template <class T, class Container>
  78. bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  79. template <class T, class Container>
  80. bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
  81. template <class T, class Container>
  82. bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  83. template <class T, class Container>
  84. bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
  85. template<class T, three_way_comparable Container>
  86. compare_three_way_result_t<Container>
  87. operator<=>(const stack<T, Container>& x, const stack<T, Container>& y); // since C++20
  88. template <class T, class Container>
  89. void swap(stack<T, Container>& x, stack<T, Container>& y)
  90. noexcept(noexcept(x.swap(y)));
  91. } // std
  92. */
  93. #include <__algorithm/ranges_copy.h>
  94. #include <__assert> // all public C++ headers provide the assertion handler
  95. #include <__config>
  96. #include <__iterator/back_insert_iterator.h>
  97. #include <__iterator/iterator_traits.h>
  98. #include <__memory/uses_allocator.h>
  99. #include <__ranges/access.h>
  100. #include <__ranges/concepts.h>
  101. #include <__ranges/container_compatible_range.h>
  102. #include <__ranges/from_range.h>
  103. #include <__type_traits/is_same.h>
  104. #include <__utility/forward.h>
  105. #include <deque>
  106. #include <version>
  107. // standard-mandated includes
  108. // [stack.syn]
  109. #include <compare>
  110. #include <initializer_list>
  111. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  112. # pragma GCC system_header
  113. #endif
  114. _LIBCPP_BEGIN_NAMESPACE_STD
  115. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
  116. template <class _Tp, class _Container>
  117. _LIBCPP_INLINE_VISIBILITY
  118. bool
  119. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  120. template <class _Tp, class _Container>
  121. _LIBCPP_INLINE_VISIBILITY
  122. bool
  123. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  124. template <class _Tp, class _Container /*= deque<_Tp>*/>
  125. class _LIBCPP_TEMPLATE_VIS stack
  126. {
  127. public:
  128. typedef _Container container_type;
  129. typedef typename container_type::value_type value_type;
  130. typedef typename container_type::reference reference;
  131. typedef typename container_type::const_reference const_reference;
  132. typedef typename container_type::size_type size_type;
  133. static_assert((is_same<_Tp, value_type>::value), "" );
  134. protected:
  135. container_type c;
  136. public:
  137. _LIBCPP_INLINE_VISIBILITY
  138. stack()
  139. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  140. : c() {}
  141. _LIBCPP_INLINE_VISIBILITY
  142. stack(const stack& __q) : c(__q.c) {}
  143. _LIBCPP_INLINE_VISIBILITY
  144. stack& operator=(const stack& __q) {c = __q.c; return *this;}
  145. #ifndef _LIBCPP_CXX03_LANG
  146. _LIBCPP_INLINE_VISIBILITY
  147. stack(stack&& __q)
  148. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  149. : c(_VSTD::move(__q.c)) {}
  150. _LIBCPP_INLINE_VISIBILITY
  151. stack& operator=(stack&& __q)
  152. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  153. {c = _VSTD::move(__q.c); return *this;}
  154. _LIBCPP_INLINE_VISIBILITY
  155. explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
  156. #endif // _LIBCPP_CXX03_LANG
  157. _LIBCPP_INLINE_VISIBILITY
  158. explicit stack(const container_type& __c) : c(__c) {}
  159. template <class _Alloc>
  160. _LIBCPP_INLINE_VISIBILITY
  161. explicit stack(const _Alloc& __a,
  162. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  163. : c(__a) {}
  164. template <class _Alloc>
  165. _LIBCPP_INLINE_VISIBILITY
  166. stack(const container_type& __c, const _Alloc& __a,
  167. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  168. : c(__c, __a) {}
  169. template <class _Alloc>
  170. _LIBCPP_INLINE_VISIBILITY
  171. stack(const stack& __s, const _Alloc& __a,
  172. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  173. : c(__s.c, __a) {}
  174. #ifndef _LIBCPP_CXX03_LANG
  175. template <class _Alloc>
  176. _LIBCPP_INLINE_VISIBILITY
  177. stack(container_type&& __c, const _Alloc& __a,
  178. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  179. : c(_VSTD::move(__c), __a) {}
  180. template <class _Alloc>
  181. _LIBCPP_INLINE_VISIBILITY
  182. stack(stack&& __s, const _Alloc& __a,
  183. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  184. : c(_VSTD::move(__s.c), __a) {}
  185. #endif // _LIBCPP_CXX03_LANG
  186. #if _LIBCPP_STD_VER >= 23
  187. template <class _InputIterator,
  188. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
  189. _LIBCPP_HIDE_FROM_ABI
  190. stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  191. template <_ContainerCompatibleRange<_Tp> _Range>
  192. _LIBCPP_HIDE_FROM_ABI
  193. stack(from_range_t, _Range&& __range) : c(from_range, std::forward<_Range>(__range)) {}
  194. template <class _InputIterator,
  195. class _Alloc,
  196. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  197. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  198. _LIBCPP_HIDE_FROM_ABI
  199. stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
  200. template <_ContainerCompatibleRange<_Tp> _Range,
  201. class _Alloc,
  202. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  203. _LIBCPP_HIDE_FROM_ABI
  204. stack(from_range_t, _Range&& __range, const _Alloc& __alloc)
  205. : c(from_range, std::forward<_Range>(__range), __alloc) {}
  206. #endif
  207. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  208. bool empty() const {return c.empty();}
  209. _LIBCPP_INLINE_VISIBILITY
  210. size_type size() const {return c.size();}
  211. _LIBCPP_INLINE_VISIBILITY
  212. reference top() {return c.back();}
  213. _LIBCPP_INLINE_VISIBILITY
  214. const_reference top() const {return c.back();}
  215. _LIBCPP_INLINE_VISIBILITY
  216. void push(const value_type& __v) {c.push_back(__v);}
  217. #ifndef _LIBCPP_CXX03_LANG
  218. _LIBCPP_INLINE_VISIBILITY
  219. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  220. #if _LIBCPP_STD_VER >= 23
  221. template <_ContainerCompatibleRange<_Tp> _Range>
  222. _LIBCPP_HIDE_FROM_ABI
  223. void push_range(_Range&& __range) {
  224. if constexpr (requires (container_type& __c) {
  225. __c.append_range(std::forward<_Range>(__range));
  226. }) {
  227. c.append_range(std::forward<_Range>(__range));
  228. } else {
  229. ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
  230. }
  231. }
  232. #endif
  233. template <class... _Args>
  234. _LIBCPP_INLINE_VISIBILITY
  235. #if _LIBCPP_STD_VER >= 17
  236. decltype(auto) emplace(_Args&&... __args)
  237. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  238. #else
  239. void emplace(_Args&&... __args)
  240. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  241. #endif
  242. #endif // _LIBCPP_CXX03_LANG
  243. _LIBCPP_INLINE_VISIBILITY
  244. void pop() {c.pop_back();}
  245. _LIBCPP_INLINE_VISIBILITY
  246. void swap(stack& __s)
  247. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  248. {
  249. using _VSTD::swap;
  250. swap(c, __s.c);
  251. }
  252. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  253. template <class _T1, class _OtherContainer>
  254. friend
  255. bool
  256. operator==(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
  257. template <class _T1, class _OtherContainer>
  258. friend
  259. bool
  260. operator< (const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
  261. };
  262. #if _LIBCPP_STD_VER >= 17
  263. template<class _Container,
  264. class = enable_if_t<!__is_allocator<_Container>::value>
  265. >
  266. stack(_Container)
  267. -> stack<typename _Container::value_type, _Container>;
  268. template<class _Container,
  269. class _Alloc,
  270. class = enable_if_t<!__is_allocator<_Container>::value>,
  271. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  272. >
  273. stack(_Container, _Alloc)
  274. -> stack<typename _Container::value_type, _Container>;
  275. #endif
  276. #if _LIBCPP_STD_VER >= 23
  277. template<class _InputIterator,
  278. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
  279. stack(_InputIterator, _InputIterator)
  280. -> stack<__iter_value_type<_InputIterator>>;
  281. template <ranges::input_range _Range>
  282. stack(from_range_t, _Range&&) -> stack<ranges::range_value_t<_Range>>;
  283. template<class _InputIterator,
  284. class _Alloc,
  285. class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  286. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  287. stack(_InputIterator, _InputIterator, _Alloc)
  288. -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  289. template <ranges::input_range _Range,
  290. class _Alloc,
  291. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  292. stack(from_range_t, _Range&&, _Alloc)
  293. -> stack<ranges::range_value_t<_Range>, deque<ranges::range_value_t<_Range>, _Alloc>>;
  294. #endif
  295. template <class _Tp, class _Container>
  296. inline _LIBCPP_INLINE_VISIBILITY
  297. bool
  298. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  299. {
  300. return __x.c == __y.c;
  301. }
  302. template <class _Tp, class _Container>
  303. inline _LIBCPP_INLINE_VISIBILITY
  304. bool
  305. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  306. {
  307. return __x.c < __y.c;
  308. }
  309. template <class _Tp, class _Container>
  310. inline _LIBCPP_INLINE_VISIBILITY
  311. bool
  312. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  313. {
  314. return !(__x == __y);
  315. }
  316. template <class _Tp, class _Container>
  317. inline _LIBCPP_INLINE_VISIBILITY
  318. bool
  319. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  320. {
  321. return __y < __x;
  322. }
  323. template <class _Tp, class _Container>
  324. inline _LIBCPP_INLINE_VISIBILITY
  325. bool
  326. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  327. {
  328. return !(__x < __y);
  329. }
  330. template <class _Tp, class _Container>
  331. inline _LIBCPP_INLINE_VISIBILITY
  332. bool
  333. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  334. {
  335. return !(__y < __x);
  336. }
  337. #if _LIBCPP_STD_VER >= 20
  338. template <class _Tp, three_way_comparable _Container>
  339. _LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_Container>
  340. operator<=>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
  341. // clang 16 bug: declaring `friend operator<=>` causes "use of overloaded operator '*' is ambiguous" errors
  342. return __x.__get_container() <=> __y.__get_container();
  343. }
  344. #endif
  345. template <class _Tp, class _Container, __enable_if_t<__is_swappable<_Container>::value, int> = 0>
  346. inline _LIBCPP_INLINE_VISIBILITY
  347. void
  348. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  349. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  350. {
  351. __x.swap(__y);
  352. }
  353. template <class _Tp, class _Container, class _Alloc>
  354. struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
  355. : public uses_allocator<_Container, _Alloc>
  356. {
  357. };
  358. _LIBCPP_END_NAMESPACE_STD
  359. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  360. # include <concepts>
  361. # include <functional>
  362. # include <type_traits>
  363. #endif
  364. #endif // _LIBCPP_STACK