stack 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // -*- C++ -*-
  2. //===---------------------------- stack -----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_STACK
  11. #define _LIBCPP_STACK
  12. /*
  13. stack synopsis
  14. namespace std
  15. {
  16. template <class T, class Container = deque<T>>
  17. class stack
  18. {
  19. public:
  20. typedef Container container_type;
  21. typedef typename container_type::value_type value_type;
  22. typedef typename container_type::reference reference;
  23. typedef typename container_type::const_reference const_reference;
  24. typedef typename container_type::size_type size_type;
  25. protected:
  26. container_type c;
  27. public:
  28. stack() = default;
  29. ~stack() = default;
  30. stack(const stack& q) = default;
  31. stack(stack&& q) = default;
  32. stack& operator=(const stack& q) = default;
  33. stack& operator=(stack&& q) = default;
  34. explicit stack(const container_type& c);
  35. explicit stack(container_type&& c);
  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. bool empty() const;
  42. size_type size() const;
  43. reference top();
  44. const_reference top() const;
  45. void push(const value_type& x);
  46. void push(value_type&& x);
  47. template <class... Args> void emplace(Args&&... args);
  48. void pop();
  49. void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
  50. };
  51. template <class T, class Container>
  52. bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  53. template <class T, class Container>
  54. bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
  55. template <class T, class Container>
  56. bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  57. template <class T, class Container>
  58. bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
  59. template <class T, class Container>
  60. bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  61. template <class T, class Container>
  62. bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
  63. template <class T, class Container>
  64. void swap(stack<T, Container>& x, stack<T, Container>& y)
  65. noexcept(noexcept(x.swap(y)));
  66. } // std
  67. */
  68. #include <__config>
  69. #include <deque>
  70. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  71. #pragma GCC system_header
  72. #endif
  73. _LIBCPP_BEGIN_NAMESPACE_STD
  74. template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TYPE_VIS_ONLY stack;
  75. template <class _Tp, class _Container>
  76. _LIBCPP_INLINE_VISIBILITY
  77. bool
  78. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  79. template <class _Tp, class _Container>
  80. _LIBCPP_INLINE_VISIBILITY
  81. bool
  82. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
  83. template <class _Tp, class _Container /*= deque<_Tp>*/>
  84. class _LIBCPP_TYPE_VIS_ONLY stack
  85. {
  86. public:
  87. typedef _Container container_type;
  88. typedef typename container_type::value_type value_type;
  89. typedef typename container_type::reference reference;
  90. typedef typename container_type::const_reference const_reference;
  91. typedef typename container_type::size_type size_type;
  92. static_assert((is_same<_Tp, value_type>::value), "" );
  93. protected:
  94. container_type c;
  95. public:
  96. _LIBCPP_INLINE_VISIBILITY
  97. stack()
  98. _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
  99. : c() {}
  100. _LIBCPP_INLINE_VISIBILITY
  101. stack(const stack& __q) : c(__q.c) {}
  102. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  103. _LIBCPP_INLINE_VISIBILITY
  104. stack(stack&& __q)
  105. _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
  106. : c(_VSTD::move(__q.c)) {}
  107. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  108. _LIBCPP_INLINE_VISIBILITY
  109. stack& operator=(const stack& __q) {c = __q.c; return *this;}
  110. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  111. _LIBCPP_INLINE_VISIBILITY
  112. stack& operator=(stack&& __q)
  113. _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
  114. {c = _VSTD::move(__q.c); return *this;}
  115. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  116. _LIBCPP_INLINE_VISIBILITY
  117. explicit stack(const container_type& __c) : c(__c) {}
  118. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  119. _LIBCPP_INLINE_VISIBILITY
  120. explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
  121. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  122. template <class _Alloc>
  123. _LIBCPP_INLINE_VISIBILITY
  124. explicit stack(const _Alloc& __a,
  125. typename enable_if<uses_allocator<container_type,
  126. _Alloc>::value>::type* = 0)
  127. : c(__a) {}
  128. template <class _Alloc>
  129. _LIBCPP_INLINE_VISIBILITY
  130. stack(const container_type& __c, const _Alloc& __a,
  131. typename enable_if<uses_allocator<container_type,
  132. _Alloc>::value>::type* = 0)
  133. : c(__c, __a) {}
  134. template <class _Alloc>
  135. _LIBCPP_INLINE_VISIBILITY
  136. stack(const stack& __s, const _Alloc& __a,
  137. typename enable_if<uses_allocator<container_type,
  138. _Alloc>::value>::type* = 0)
  139. : c(__s.c, __a) {}
  140. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  141. template <class _Alloc>
  142. _LIBCPP_INLINE_VISIBILITY
  143. stack(container_type&& __c, const _Alloc& __a,
  144. typename enable_if<uses_allocator<container_type,
  145. _Alloc>::value>::type* = 0)
  146. : c(_VSTD::move(__c), __a) {}
  147. template <class _Alloc>
  148. _LIBCPP_INLINE_VISIBILITY
  149. stack(stack&& __s, const _Alloc& __a,
  150. typename enable_if<uses_allocator<container_type,
  151. _Alloc>::value>::type* = 0)
  152. : c(_VSTD::move(__s.c), __a) {}
  153. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  154. _LIBCPP_INLINE_VISIBILITY
  155. bool empty() const {return c.empty();}
  156. _LIBCPP_INLINE_VISIBILITY
  157. size_type size() const {return c.size();}
  158. _LIBCPP_INLINE_VISIBILITY
  159. reference top() {return c.back();}
  160. _LIBCPP_INLINE_VISIBILITY
  161. const_reference top() const {return c.back();}
  162. _LIBCPP_INLINE_VISIBILITY
  163. void push(const value_type& __v) {c.push_back(__v);}
  164. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  165. _LIBCPP_INLINE_VISIBILITY
  166. void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
  167. #ifndef _LIBCPP_HAS_NO_VARIADICS
  168. template <class... _Args>
  169. _LIBCPP_INLINE_VISIBILITY
  170. void emplace(_Args&&... __args)
  171. {c.emplace_back(_VSTD::forward<_Args>(__args)...);}
  172. #endif // _LIBCPP_HAS_NO_VARIADICS
  173. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  174. _LIBCPP_INLINE_VISIBILITY
  175. void pop() {c.pop_back();}
  176. _LIBCPP_INLINE_VISIBILITY
  177. void swap(stack& __s)
  178. _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
  179. {
  180. using _VSTD::swap;
  181. swap(c, __s.c);
  182. }
  183. template <class T1, class _C1>
  184. friend
  185. bool
  186. operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  187. template <class T1, class _C1>
  188. friend
  189. bool
  190. operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
  191. };
  192. template <class _Tp, class _Container>
  193. inline _LIBCPP_INLINE_VISIBILITY
  194. bool
  195. operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  196. {
  197. return __x.c == __y.c;
  198. }
  199. template <class _Tp, class _Container>
  200. inline _LIBCPP_INLINE_VISIBILITY
  201. bool
  202. operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  203. {
  204. return __x.c < __y.c;
  205. }
  206. template <class _Tp, class _Container>
  207. inline _LIBCPP_INLINE_VISIBILITY
  208. bool
  209. operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  210. {
  211. return !(__x == __y);
  212. }
  213. template <class _Tp, class _Container>
  214. inline _LIBCPP_INLINE_VISIBILITY
  215. bool
  216. operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  217. {
  218. return __y < __x;
  219. }
  220. template <class _Tp, class _Container>
  221. inline _LIBCPP_INLINE_VISIBILITY
  222. bool
  223. operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  224. {
  225. return !(__x < __y);
  226. }
  227. template <class _Tp, class _Container>
  228. inline _LIBCPP_INLINE_VISIBILITY
  229. bool
  230. operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
  231. {
  232. return !(__y < __x);
  233. }
  234. template <class _Tp, class _Container>
  235. inline _LIBCPP_INLINE_VISIBILITY
  236. typename enable_if<
  237. __is_swappable<_Container>::value,
  238. void
  239. >::type
  240. swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
  241. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  242. {
  243. __x.swap(__y);
  244. }
  245. template <class _Tp, class _Container, class _Alloc>
  246. struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<stack<_Tp, _Container>, _Alloc>
  247. : public uses_allocator<_Container, _Alloc>
  248. {
  249. };
  250. _LIBCPP_END_NAMESPACE_STD
  251. #endif // _LIBCPP_STACK