stack 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <__config>
  81. #include <__iterator/iterator_traits.h>
  82. #include <__memory/uses_allocator.h>
  83. #include <__utility/forward.h>
  84. #include <deque>
  85. #include <type_traits>
  86. #include <version>
  87. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  88. # pragma GCC system_header
  89. #endif
  90. _LIBCPP_BEGIN_NAMESPACE_STD
  91. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
  92. template <class _Tp, class _Container>
  93. _LIBCPP_INLINE_VISIBILITY
  94. bool
  95. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  96. template <class _Tp, class _Container>
  97. _LIBCPP_INLINE_VISIBILITY
  98. bool
  99. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  100. template <class _Tp, class _Container /*= deque<_Tp>*/>
  101. class _LIBCPP_TEMPLATE_VIS stack
  102. {
  103. public:
  104. typedef _Container container_type;
  105. typedef typename container_type::value_type value_type;
  106. typedef typename container_type::reference reference;
  107. typedef typename container_type::const_reference const_reference;
  108. typedef typename container_type::size_type size_type;
  109. static_assert((is_same<_Tp, value_type>::value), "" );
  110. protected:
  111. container_type c;
  112. public:
  113. _LIBCPP_INLINE_VISIBILITY
  114. stack()
  115. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  116. : c() {}
  117. _LIBCPP_INLINE_VISIBILITY
  118. stack(const stack& __q) : c(__q.c) {}
  119. _LIBCPP_INLINE_VISIBILITY
  120. stack& operator=(const stack& __q) {c = __q.c; return *this;}
  121. #ifndef _LIBCPP_CXX03_LANG
  122. _LIBCPP_INLINE_VISIBILITY
  123. stack(stack&& __q)
  124. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  125. : c(_VSTD::move(__q.c)) {}
  126. _LIBCPP_INLINE_VISIBILITY
  127. stack& operator=(stack&& __q)
  128. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  129. {c = _VSTD::move(__q.c); return *this;}
  130. _LIBCPP_INLINE_VISIBILITY
  131. explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
  132. #endif // _LIBCPP_CXX03_LANG
  133. _LIBCPP_INLINE_VISIBILITY
  134. explicit stack(const container_type& __c) : c(__c) {}
  135. template <class _Alloc>
  136. _LIBCPP_INLINE_VISIBILITY
  137. explicit stack(const _Alloc& __a,
  138. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  139. : c(__a) {}
  140. template <class _Alloc>
  141. _LIBCPP_INLINE_VISIBILITY
  142. stack(const container_type& __c, const _Alloc& __a,
  143. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  144. : c(__c, __a) {}
  145. template <class _Alloc>
  146. _LIBCPP_INLINE_VISIBILITY
  147. stack(const stack& __s, const _Alloc& __a,
  148. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  149. : c(__s.c, __a) {}
  150. #ifndef _LIBCPP_CXX03_LANG
  151. template <class _Alloc>
  152. _LIBCPP_INLINE_VISIBILITY
  153. stack(container_type&& __c, const _Alloc& __a,
  154. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  155. : c(_VSTD::move(__c), __a) {}
  156. template <class _Alloc>
  157. _LIBCPP_INLINE_VISIBILITY
  158. stack(stack&& __s, const _Alloc& __a,
  159. __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
  160. : c(_VSTD::move(__s.c), __a) {}
  161. #endif // _LIBCPP_CXX03_LANG
  162. #if _LIBCPP_STD_VER > 20
  163. template <class _InputIterator,
  164. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  165. _LIBCPP_HIDE_FROM_ABI
  166. stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
  167. template <class _InputIterator,
  168. class _Alloc,
  169. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  170. class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
  171. _LIBCPP_HIDE_FROM_ABI
  172. stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
  173. #endif
  174. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  175. bool empty() const {return c.empty();}
  176. _LIBCPP_INLINE_VISIBILITY
  177. size_type size() const {return c.size();}
  178. _LIBCPP_INLINE_VISIBILITY
  179. reference top() {return c.back();}
  180. _LIBCPP_INLINE_VISIBILITY
  181. const_reference top() const {return c.back();}
  182. _LIBCPP_INLINE_VISIBILITY
  183. void push(const value_type& __v) {c.push_back(__v);}
  184. #ifndef _LIBCPP_CXX03_LANG
  185. _LIBCPP_INLINE_VISIBILITY
  186. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  187. template <class... _Args>
  188. _LIBCPP_INLINE_VISIBILITY
  189. #if _LIBCPP_STD_VER > 14
  190. decltype(auto) emplace(_Args&&... __args)
  191. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  192. #else
  193. void emplace(_Args&&... __args)
  194. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  195. #endif
  196. #endif // _LIBCPP_CXX03_LANG
  197. _LIBCPP_INLINE_VISIBILITY
  198. void pop() {c.pop_back();}
  199. _LIBCPP_INLINE_VISIBILITY
  200. void swap(stack& __s)
  201. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  202. {
  203. using _VSTD::swap;
  204. swap(c, __s.c);
  205. }
  206. template <class T1, class _C1>
  207. friend
  208. bool
  209. operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  210. template <class T1, class _C1>
  211. friend
  212. bool
  213. operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  214. };
  215. #if _LIBCPP_STD_VER > 14
  216. template<class _Container,
  217. class = enable_if_t<!__is_allocator<_Container>::value>
  218. >
  219. stack(_Container)
  220. -> stack<typename _Container::value_type, _Container>;
  221. template<class _Container,
  222. class _Alloc,
  223. class = enable_if_t<!__is_allocator<_Container>::value>,
  224. class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
  225. >
  226. stack(_Container, _Alloc)
  227. -> stack<typename _Container::value_type, _Container>;
  228. #endif
  229. #if _LIBCPP_STD_VER > 20
  230. template<class _InputIterator,
  231. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
  232. stack(_InputIterator, _InputIterator)
  233. -> stack<__iter_value_type<_InputIterator>>;
  234. template<class _InputIterator,
  235. class _Alloc,
  236. class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  237. class = __enable_if_t<__is_allocator<_Alloc>::value>>
  238. stack(_InputIterator, _InputIterator, _Alloc)
  239. -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
  240. #endif
  241. template <class _Tp, class _Container>
  242. inline _LIBCPP_INLINE_VISIBILITY
  243. bool
  244. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  245. {
  246. return __x.c == __y.c;
  247. }
  248. template <class _Tp, class _Container>
  249. inline _LIBCPP_INLINE_VISIBILITY
  250. bool
  251. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  252. {
  253. return __x.c < __y.c;
  254. }
  255. template <class _Tp, class _Container>
  256. inline _LIBCPP_INLINE_VISIBILITY
  257. bool
  258. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  259. {
  260. return !(__x == __y);
  261. }
  262. template <class _Tp, class _Container>
  263. inline _LIBCPP_INLINE_VISIBILITY
  264. bool
  265. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  266. {
  267. return __y < __x;
  268. }
  269. template <class _Tp, class _Container>
  270. inline _LIBCPP_INLINE_VISIBILITY
  271. bool
  272. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  273. {
  274. return !(__x < __y);
  275. }
  276. template <class _Tp, class _Container>
  277. inline _LIBCPP_INLINE_VISIBILITY
  278. bool
  279. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  280. {
  281. return !(__y < __x);
  282. }
  283. template <class _Tp, class _Container>
  284. inline _LIBCPP_INLINE_VISIBILITY
  285. __enable_if_t<__is_swappable<_Container>::value, void>
  286. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  287. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  288. {
  289. __x.swap(__y);
  290. }
  291. template <class _Tp, class _Container, class _Alloc>
  292. struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
  293. : public uses_allocator<_Container, _Alloc>
  294. {
  295. };
  296. _LIBCPP_END_NAMESPACE_STD
  297. #endif // _LIBCPP_STACK