stack 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // -*- C++ -*-
  2. //===---------------------------- stack -----------------------------------===//
  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 Alloc> explicit stack(const Alloc& a);
  36. template <class Alloc> stack(const container_type& c, const Alloc& a);
  37. template <class Alloc> stack(container_type&& c, const Alloc& a);
  38. template <class Alloc> stack(const stack& c, const Alloc& a);
  39. template <class Alloc> stack(stack&& c, const Alloc& a);
  40. bool empty() const;
  41. size_type size() const;
  42. reference top();
  43. const_reference top() const;
  44. void push(const value_type& x);
  45. void push(value_type&& x);
  46. template <class... Args> reference emplace(Args&&... args); // reference in C++17
  47. void pop();
  48. void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
  49. };
  50. template<class Container>
  51. stack(Container) -> stack<typename Container::value_type, Container>; // C++17
  52. template<class Container, class Allocator>
  53. stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
  54. template <class T, class Container>
  55. bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  56. template <class T, class Container>
  57. bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
  58. template <class T, class Container>
  59. bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  60. template <class T, class Container>
  61. bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
  62. template <class T, class Container>
  63. bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  64. template <class T, class Container>
  65. bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
  66. template <class T, class Container>
  67. void swap(stack<T, Container>& x, stack<T, Container>& y)
  68. noexcept(noexcept(x.swap(y)));
  69. } // std
  70. */
  71. #include <__config>
  72. #include <deque>
  73. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  74. #pragma GCC system_header
  75. #endif
  76. _LIBCPP_BEGIN_NAMESPACE_STD
  77. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
  78. template <class _Tp, class _Container>
  79. _LIBCPP_INLINE_VISIBILITY
  80. bool
  81. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  82. template <class _Tp, class _Container>
  83. _LIBCPP_INLINE_VISIBILITY
  84. bool
  85. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  86. template <class _Tp, class _Container /*= deque<_Tp>*/>
  87. class _LIBCPP_TEMPLATE_VIS stack
  88. {
  89. public:
  90. typedef _Container container_type;
  91. typedef typename container_type::value_type value_type;
  92. typedef typename container_type::reference reference;
  93. typedef typename container_type::const_reference const_reference;
  94. typedef typename container_type::size_type size_type;
  95. static_assert((is_same<_Tp, value_type>::value), "" );
  96. protected:
  97. container_type c;
  98. public:
  99. _LIBCPP_INLINE_VISIBILITY
  100. stack()
  101. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  102. : c() {}
  103. _LIBCPP_INLINE_VISIBILITY
  104. stack(const stack& __q) : c(__q.c) {}
  105. _LIBCPP_INLINE_VISIBILITY
  106. stack& operator=(const stack& __q) {c = __q.c; return *this;}
  107. #ifndef _LIBCPP_CXX03_LANG
  108. _LIBCPP_INLINE_VISIBILITY
  109. stack(stack&& __q)
  110. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  111. : c(_VSTD::move(__q.c)) {}
  112. _LIBCPP_INLINE_VISIBILITY
  113. stack& operator=(stack&& __q)
  114. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  115. {c = _VSTD::move(__q.c); return *this;}
  116. _LIBCPP_INLINE_VISIBILITY
  117. explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
  118. #endif // _LIBCPP_CXX03_LANG
  119. _LIBCPP_INLINE_VISIBILITY
  120. explicit stack(const container_type& __c) : c(__c) {}
  121. template <class _Alloc>
  122. _LIBCPP_INLINE_VISIBILITY
  123. explicit stack(const _Alloc& __a,
  124. _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
  125. : c(__a) {}
  126. template <class _Alloc>
  127. _LIBCPP_INLINE_VISIBILITY
  128. stack(const container_type& __c, const _Alloc& __a,
  129. _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
  130. : c(__c, __a) {}
  131. template <class _Alloc>
  132. _LIBCPP_INLINE_VISIBILITY
  133. stack(const stack& __s, const _Alloc& __a,
  134. _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
  135. : c(__s.c, __a) {}
  136. #ifndef _LIBCPP_CXX03_LANG
  137. template <class _Alloc>
  138. _LIBCPP_INLINE_VISIBILITY
  139. stack(container_type&& __c, const _Alloc& __a,
  140. _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
  141. : c(_VSTD::move(__c), __a) {}
  142. template <class _Alloc>
  143. _LIBCPP_INLINE_VISIBILITY
  144. stack(stack&& __s, const _Alloc& __a,
  145. _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
  146. : c(_VSTD::move(__s.c), __a) {}
  147. #endif // _LIBCPP_CXX03_LANG
  148. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  149. bool empty() const {return c.empty();}
  150. _LIBCPP_INLINE_VISIBILITY
  151. size_type size() const {return c.size();}
  152. _LIBCPP_INLINE_VISIBILITY
  153. reference top() {return c.back();}
  154. _LIBCPP_INLINE_VISIBILITY
  155. const_reference top() const {return c.back();}
  156. _LIBCPP_INLINE_VISIBILITY
  157. void push(const value_type& __v) {c.push_back(__v);}
  158. #ifndef _LIBCPP_CXX03_LANG
  159. _LIBCPP_INLINE_VISIBILITY
  160. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  161. template <class... _Args>
  162. _LIBCPP_INLINE_VISIBILITY
  163. #if _LIBCPP_STD_VER > 14
  164. decltype(auto) emplace(_Args&&... __args)
  165. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  166. #else
  167. void emplace(_Args&&... __args)
  168. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  169. #endif
  170. #endif // _LIBCPP_CXX03_LANG
  171. _LIBCPP_INLINE_VISIBILITY
  172. void pop() {c.pop_back();}
  173. _LIBCPP_INLINE_VISIBILITY
  174. void swap(stack& __s)
  175. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  176. {
  177. using _VSTD::swap;
  178. swap(c, __s.c);
  179. }
  180. template <class T1, class _C1>
  181. friend
  182. bool
  183. operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  184. template <class T1, class _C1>
  185. friend
  186. bool
  187. operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  188. };
  189. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  190. template<class _Container,
  191. class = _EnableIf<!__is_allocator<_Container>::value>
  192. >
  193. stack(_Container)
  194. -> stack<typename _Container::value_type, _Container>;
  195. template<class _Container,
  196. class _Alloc,
  197. class = _EnableIf<!__is_allocator<_Container>::value>,
  198. class = _EnableIf<__is_allocator<_Alloc>::value>
  199. >
  200. stack(_Container, _Alloc)
  201. -> stack<typename _Container::value_type, _Container>;
  202. #endif
  203. template <class _Tp, class _Container>
  204. inline _LIBCPP_INLINE_VISIBILITY
  205. bool
  206. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  207. {
  208. return __x.c == __y.c;
  209. }
  210. template <class _Tp, class _Container>
  211. inline _LIBCPP_INLINE_VISIBILITY
  212. bool
  213. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  214. {
  215. return __x.c < __y.c;
  216. }
  217. template <class _Tp, class _Container>
  218. inline _LIBCPP_INLINE_VISIBILITY
  219. bool
  220. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  221. {
  222. return !(__x == __y);
  223. }
  224. template <class _Tp, class _Container>
  225. inline _LIBCPP_INLINE_VISIBILITY
  226. bool
  227. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  228. {
  229. return __y < __x;
  230. }
  231. template <class _Tp, class _Container>
  232. inline _LIBCPP_INLINE_VISIBILITY
  233. bool
  234. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  235. {
  236. return !(__x < __y);
  237. }
  238. template <class _Tp, class _Container>
  239. inline _LIBCPP_INLINE_VISIBILITY
  240. bool
  241. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  242. {
  243. return !(__y < __x);
  244. }
  245. template <class _Tp, class _Container>
  246. inline _LIBCPP_INLINE_VISIBILITY
  247. _EnableIf<__is_swappable<_Container>::value, void>
  248. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  249. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  250. {
  251. __x.swap(__y);
  252. }
  253. template <class _Tp, class _Container, class _Alloc>
  254. struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
  255. : public uses_allocator<_Container, _Alloc>
  256. {
  257. };
  258. _LIBCPP_END_NAMESPACE_STD
  259. #endif // _LIBCPP_STACK