stack 14 KB

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