stack 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. typename enable_if<uses_allocator<container_type,
  125. _Alloc>::value>::type* = 0)
  126. : c(__a) {}
  127. template <class _Alloc>
  128. _LIBCPP_INLINE_VISIBILITY
  129. stack(const container_type& __c, const _Alloc& __a,
  130. typename enable_if<uses_allocator<container_type,
  131. _Alloc>::value>::type* = 0)
  132. : c(__c, __a) {}
  133. template <class _Alloc>
  134. _LIBCPP_INLINE_VISIBILITY
  135. stack(const stack& __s, const _Alloc& __a,
  136. typename enable_if<uses_allocator<container_type,
  137. _Alloc>::value>::type* = 0)
  138. : c(__s.c, __a) {}
  139. #ifndef _LIBCPP_CXX03_LANG
  140. template <class _Alloc>
  141. _LIBCPP_INLINE_VISIBILITY
  142. stack(container_type&& __c, const _Alloc& __a,
  143. typename enable_if<uses_allocator<container_type,
  144. _Alloc>::value>::type* = 0)
  145. : c(_VSTD::move(__c), __a) {}
  146. template <class _Alloc>
  147. _LIBCPP_INLINE_VISIBILITY
  148. stack(stack&& __s, const _Alloc& __a,
  149. typename enable_if<uses_allocator<container_type,
  150. _Alloc>::value>::type* = 0)
  151. : c(_VSTD::move(__s.c), __a) {}
  152. #endif // _LIBCPP_CXX03_LANG
  153. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  154. bool empty() const {return c.empty();}
  155. _LIBCPP_INLINE_VISIBILITY
  156. size_type size() const {return c.size();}
  157. _LIBCPP_INLINE_VISIBILITY
  158. reference top() {return c.back();}
  159. _LIBCPP_INLINE_VISIBILITY
  160. const_reference top() const {return c.back();}
  161. _LIBCPP_INLINE_VISIBILITY
  162. void push(const value_type& __v) {c.push_back(__v);}
  163. #ifndef _LIBCPP_CXX03_LANG
  164. _LIBCPP_INLINE_VISIBILITY
  165. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  166. template <class... _Args>
  167. _LIBCPP_INLINE_VISIBILITY
  168. #if _LIBCPP_STD_VER > 14
  169. decltype(auto) emplace(_Args&&... __args)
  170. { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  171. #else
  172. void emplace(_Args&&... __args)
  173. { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  174. #endif
  175. #endif // _LIBCPP_CXX03_LANG
  176. _LIBCPP_INLINE_VISIBILITY
  177. void pop() {c.pop_back();}
  178. _LIBCPP_INLINE_VISIBILITY
  179. void swap(stack& __s)
  180. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  181. {
  182. using _VSTD::swap;
  183. swap(c, __s.c);
  184. }
  185. template <class T1, class _C1>
  186. friend
  187. bool
  188. operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  189. template <class T1, class _C1>
  190. friend
  191. bool
  192. operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  193. };
  194. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  195. template<class _Container,
  196. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
  197. >
  198. stack(_Container)
  199. -> stack<typename _Container::value_type, _Container>;
  200. template<class _Container,
  201. class _Alloc,
  202. class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
  203. class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
  204. >
  205. stack(_Container, _Alloc)
  206. -> stack<typename _Container::value_type, _Container>;
  207. #endif
  208. template <class _Tp, class _Container>
  209. inline _LIBCPP_INLINE_VISIBILITY
  210. bool
  211. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  212. {
  213. return __x.c == __y.c;
  214. }
  215. template <class _Tp, class _Container>
  216. inline _LIBCPP_INLINE_VISIBILITY
  217. bool
  218. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  219. {
  220. return __x.c < __y.c;
  221. }
  222. template <class _Tp, class _Container>
  223. inline _LIBCPP_INLINE_VISIBILITY
  224. bool
  225. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  226. {
  227. return !(__x == __y);
  228. }
  229. template <class _Tp, class _Container>
  230. inline _LIBCPP_INLINE_VISIBILITY
  231. bool
  232. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  233. {
  234. return __y < __x;
  235. }
  236. template <class _Tp, class _Container>
  237. inline _LIBCPP_INLINE_VISIBILITY
  238. bool
  239. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  240. {
  241. return !(__x < __y);
  242. }
  243. template <class _Tp, class _Container>
  244. inline _LIBCPP_INLINE_VISIBILITY
  245. bool
  246. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  247. {
  248. return !(__y < __x);
  249. }
  250. template <class _Tp, class _Container>
  251. inline _LIBCPP_INLINE_VISIBILITY
  252. typename enable_if<
  253. __is_swappable<_Container>::value,
  254. void
  255. >::type
  256. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  257. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  258. {
  259. __x.swap(__y);
  260. }
  261. template <class _Tp, class _Container, class _Alloc>
  262. struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
  263. : public uses_allocator<_Container, _Alloc>
  264. {
  265. };
  266. _LIBCPP_END_NAMESPACE_STD
  267. #endif // _LIBCPP_STACK