stack 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <class Alloc> explicit stack(const Alloc& a);
  37. template <class Alloc> stack(const container_type& c, const Alloc& a);
  38. template <class Alloc> stack(container_type&& c, const Alloc& a);
  39. template <class Alloc> stack(const stack& c, const Alloc& a);
  40. template <class Alloc> stack(stack&& c, const Alloc& a);
  41. template<class InputIterator, class Alloc>
  42. stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
  43. bool empty() const;
  44. size_type size() const;
  45. reference top();
  46. const_reference top() const;
  47. void push(const value_type& x);
  48. void push(value_type&& x);
  49. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  50. void pop();
  51. void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
  52. };
  53. template<class Container>
  54. stack(Container) -> stack<typename Container::value_type, Container>; // C++17
  55. template<class InputIterator>
  56. stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
  57. template<class Container, class Allocator>
  58. stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
  59. template<class InputIterator, class Allocator>
  60. stack(InputIterator, InputIterator, Allocator)
  61. -> stack<iter-value-type<InputIterator>,
  62. deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
  63. template <class T, class Container>
  64. bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  65. template <class T, class Container>
  66. bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
  67. template <class T, class Container>
  68. bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  69. template <class T, class Container>
  70. bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
  71. template <class T, class Container>
  72. bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  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. void swap(stack<T, Container>& x, stack<T, Container>& y)
  77. noexcept(noexcept(x.swap(y)));
  78. } // std
  79. */
  80. #include <__assert> // all public C++ headers provide the assertion handler
  81. #include <__config>
  82. #include <__iterator/iterator_traits.h>
  83. #include <__memory/uses_allocator.h>
  84. #include <__utility/forward.h>
  85. #include <deque>
  86. #include <type_traits>
  87. #include <version>
  88. // standard-mandated includes
  89. // [stack.syn]
  90. #include <compare>
  91. #include <initializer_list>
  92. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  93. # pragma GCC system_header
  94. #endif
  95. _LIBCPP_BEGIN_NAMESPACE_STD
  96. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
  97. template <class _Tp, class _Container>
  98. _LIBCPP_INLINE_VISIBILITY
  99. bool
  100. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  101. template <class _Tp, class _Container>
  102. _LIBCPP_INLINE_VISIBILITY
  103. bool
  104. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  105. template <class _Tp, class _Container /*= deque<_Tp>*/>
  106. class _LIBCPP_TEMPLATE_VIS stack
  107. {
  108. public:
  109. typedef _Container container_type;
  110. typedef typename container_type::value_type value_type;
  111. typedef typename container_type::reference reference;
  112. typedef typename container_type::const_reference const_reference;
  113. typedef typename container_type::size_type size_type;
  114. static_assert((is_same<_Tp, value_type>::value), "" );
  115. protected:
  116. container_type c;
  117. public:
  118. _LIBCPP_INLINE_VISIBILITY
  119. stack()
  120. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  121. : c() {}
  122. _LIBCPP_INLINE_VISIBILITY
  123. stack(const stack& __q) : c(__q.c) {}
  124. _LIBCPP_INLINE_VISIBILITY
  125. stack& operator=(const stack& __q) {c = __q.c; return *this;}
  126. #ifndef _LIBCPP_CXX03_LANG
  127. _LIBCPP_INLINE_VISIBILITY
  128. stack(stack&& __q)
  129. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  130. : c(_VSTD::move(__q.c)) {}
  131. _LIBCPP_INLINE_VISIBILITY
  132. stack& operator=(stack&& __q)
  133. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  134. {c = _VSTD::move(__q.c); return *this;}
  135. _LIBCPP_INLINE_VISIBILITY
  136. explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
  137. #endif // _LIBCPP_CXX03_LANG
  138. _LIBCPP_INLINE_VISIBILITY
  139. explicit stack(const container_type& __c) : c(__c) {}
  140. template <class _Alloc>
  141. _LIBCPP_INLINE_VISIBILITY
  142. explicit stack(const _Alloc& __a,
  143. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  144. : c(__a) {}
  145. template <class _Alloc>
  146. _LIBCPP_INLINE_VISIBILITY
  147. stack(const container_type& __c, const _Alloc& __a,
  148. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  149. : c(__c, __a) {}
  150. template <class _Alloc>
  151. _LIBCPP_INLINE_VISIBILITY
  152. stack(const stack& __s, const _Alloc& __a,
  153. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  154. : c(__s.c, __a) {}
  155. #ifndef _LIBCPP_CXX03_LANG
  156. template <class _Alloc>
  157. _LIBCPP_INLINE_VISIBILITY
  158. stack(container_type&& __c, const _Alloc& __a,
  159. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  160. : c(_VSTD::move(__c), __a) {}
  161. template <class _Alloc>
  162. _LIBCPP_INLINE_VISIBILITY
  163. stack(stack&& __s, const _Alloc& __a,
  164. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  165. : c(_VSTD::move(__s.c), __a) {}
  166. #endif // _LIBCPP_CXX03_LANG
  167. #if _LIBCPP_STD_VER > 20
  168. template <class _InputIterator,
  169. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  170. _LIBCPP_HIDE_FROM_ABI
  171. stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  172. template <class _InputIterator,
  173. class _Alloc,
  174. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  175. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  176. _LIBCPP_HIDE_FROM_ABI
  177. stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
  178. #endif
  179. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  180. bool empty() const {return c.empty();}
  181. _LIBCPP_INLINE_VISIBILITY
  182. size_type size() const {return c.size();}
  183. _LIBCPP_INLINE_VISIBILITY
  184. reference top() {return c.back();}
  185. _LIBCPP_INLINE_VISIBILITY
  186. const_reference top() const {return c.back();}
  187. _LIBCPP_INLINE_VISIBILITY
  188. void push(const value_type& __v) {c.push_back(__v);}
  189. #ifndef _LIBCPP_CXX03_LANG
  190. _LIBCPP_INLINE_VISIBILITY
  191. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  192. template <class... _Args>
  193. _LIBCPP_INLINE_VISIBILITY
  194. #if _LIBCPP_STD_VER > 14
  195. decltype(auto) emplace(_Args&&... __args)
  196. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  197. #else
  198. void emplace(_Args&&... __args)
  199. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  200. #endif
  201. #endif // _LIBCPP_CXX03_LANG
  202. _LIBCPP_INLINE_VISIBILITY
  203. void pop() {c.pop_back();}
  204. _LIBCPP_INLINE_VISIBILITY
  205. void swap(stack& __s)
  206. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  207. {
  208. using _VSTD::swap;
  209. swap(c, __s.c);
  210. }
  211. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
  212. template <class T1, class _C1>
  213. friend
  214. bool
  215. operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  216. template <class T1, class _C1>
  217. friend
  218. bool
  219. operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  220. };
  221. #if _LIBCPP_STD_VER > 14
  222. template<class _Container,
  223. class = enable_if_t<!__is_allocator<_Container>::value>
  224. >
  225. stack(_Container)
  226. -> stack<typename _Container::value_type, _Container>;
  227. template<class _Container,
  228. class _Alloc,
  229. class = enable_if_t<!__is_allocator<_Container>::value>,
  230. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  231. >
  232. stack(_Container, _Alloc)
  233. -> stack<typename _Container::value_type, _Container>;
  234. #endif
  235. #if _LIBCPP_STD_VER > 20
  236. template<class _InputIterator,
  237. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  238. stack(_InputIterator, _InputIterator)
  239. -> stack<__iter_value_type<_InputIterator>>;
  240. template<class _InputIterator,
  241. class _Alloc,
  242. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  243. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  244. stack(_InputIterator, _InputIterator, _Alloc)
  245. -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  246. #endif
  247. template <class _Tp, class _Container>
  248. inline _LIBCPP_INLINE_VISIBILITY
  249. bool
  250. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  251. {
  252. return __x.c == __y.c;
  253. }
  254. template <class _Tp, class _Container>
  255. inline _LIBCPP_INLINE_VISIBILITY
  256. bool
  257. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  258. {
  259. return __x.c < __y.c;
  260. }
  261. template <class _Tp, class _Container>
  262. inline _LIBCPP_INLINE_VISIBILITY
  263. bool
  264. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  265. {
  266. return !(__x == __y);
  267. }
  268. template <class _Tp, class _Container>
  269. inline _LIBCPP_INLINE_VISIBILITY
  270. bool
  271. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  272. {
  273. return __y < __x;
  274. }
  275. template <class _Tp, class _Container>
  276. inline _LIBCPP_INLINE_VISIBILITY
  277. bool
  278. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  279. {
  280. return !(__x < __y);
  281. }
  282. template <class _Tp, class _Container>
  283. inline _LIBCPP_INLINE_VISIBILITY
  284. bool
  285. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  286. {
  287. return !(__y < __x);
  288. }
  289. template <class _Tp, class _Container>
  290. inline _LIBCPP_INLINE_VISIBILITY
  291. __enable_if_t<__is_swappable<_Container>::value, void>
  292. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  293. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  294. {
  295. __x.swap(__y);
  296. }
  297. template <class _Tp, class _Container, class _Alloc>
  298. struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
  299. : public uses_allocator<_Container, _Alloc>
  300. {
  301. };
  302. _LIBCPP_END_NAMESPACE_STD
  303. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  304. # include <concepts>
  305. # include <functional>
  306. #endif
  307. #endif // _LIBCPP_STACK