stl_stack.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Stack implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_stack.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{stack}
  48. */
  49. #ifndef _STL_STACK_H
  50. #define _STL_STACK_H 1
  51. #include <bits/concept_check.h>
  52. #include <debug/debug.h>
  53. #if __cplusplus >= 201103L
  54. # include <bits/uses_allocator.h>
  55. #endif
  56. namespace std _GLIBCXX_VISIBILITY(default)
  57. {
  58. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  59. /**
  60. * @brief A standard container giving FILO behavior.
  61. *
  62. * @ingroup sequences
  63. *
  64. * @tparam _Tp Type of element.
  65. * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
  66. *
  67. * Meets many of the requirements of a
  68. * <a href="tables.html#65">container</a>,
  69. * but does not define anything to do with iterators. Very few of the
  70. * other standard container interfaces are defined.
  71. *
  72. * This is not a true container, but an @e adaptor. It holds
  73. * another container, and provides a wrapper interface to that
  74. * container. The wrapper is what enforces strict
  75. * first-in-last-out %stack behavior.
  76. *
  77. * The second template parameter defines the type of the underlying
  78. * sequence/container. It defaults to std::deque, but it can be
  79. * any type that supports @c back, @c push_back, and @c pop_back,
  80. * such as std::list, std::vector, or an appropriate user-defined
  81. * type.
  82. *
  83. * Members not found in @a normal containers are @c container_type,
  84. * which is a typedef for the second Sequence parameter, and @c
  85. * push, @c pop, and @c top, which are standard %stack/FILO
  86. * operations.
  87. */
  88. template<typename _Tp, typename _Sequence = deque<_Tp> >
  89. class stack
  90. {
  91. #ifdef _GLIBCXX_CONCEPT_CHECKS
  92. // concept requirements
  93. typedef typename _Sequence::value_type _Sequence_value_type;
  94. # if __cplusplus < 201103L
  95. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  96. __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
  97. # endif
  98. __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
  99. #endif
  100. template<typename _Tp1, typename _Seq1>
  101. friend bool
  102. operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
  103. template<typename _Tp1, typename _Seq1>
  104. friend bool
  105. operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
  106. #if __cpp_lib_three_way_comparison
  107. template<typename _Tp1, three_way_comparable _Seq1>
  108. friend compare_three_way_result_t<_Seq1>
  109. operator<=>(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
  110. #endif
  111. #if __cplusplus >= 201103L
  112. template<typename _Alloc>
  113. using _Uses = typename
  114. enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
  115. #if __cplusplus >= 201703L
  116. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  117. // 2566. Requirements on the first template parameter of container
  118. // adaptors
  119. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  120. "value_type must be the same as the underlying container");
  121. #endif // C++17
  122. #endif // C++11
  123. public:
  124. typedef typename _Sequence::value_type value_type;
  125. typedef typename _Sequence::reference reference;
  126. typedef typename _Sequence::const_reference const_reference;
  127. typedef typename _Sequence::size_type size_type;
  128. typedef _Sequence container_type;
  129. protected:
  130. // See queue::c for notes on this name.
  131. _Sequence c;
  132. public:
  133. // XXX removed old def ctor, added def arg to this one to match 14882
  134. /**
  135. * @brief Default constructor creates no elements.
  136. */
  137. #if __cplusplus < 201103L
  138. explicit
  139. stack(const _Sequence& __c = _Sequence())
  140. : c(__c) { }
  141. #else
  142. template<typename _Seq = _Sequence, typename _Requires = typename
  143. enable_if<is_default_constructible<_Seq>::value>::type>
  144. stack()
  145. : c() { }
  146. explicit
  147. stack(const _Sequence& __c)
  148. : c(__c) { }
  149. explicit
  150. stack(_Sequence&& __c)
  151. : c(std::move(__c)) { }
  152. #if __cplusplus > 202002L
  153. #define __cpp_lib_adaptor_iterator_pair_constructor 202106L
  154. template<typename _InputIterator,
  155. typename = _RequireInputIter<_InputIterator>>
  156. stack(_InputIterator __first, _InputIterator __last)
  157. : c(__first, __last) { }
  158. #endif
  159. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  160. explicit
  161. stack(const _Alloc& __a)
  162. : c(__a) { }
  163. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  164. stack(const _Sequence& __c, const _Alloc& __a)
  165. : c(__c, __a) { }
  166. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  167. stack(_Sequence&& __c, const _Alloc& __a)
  168. : c(std::move(__c), __a) { }
  169. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  170. stack(const stack& __q, const _Alloc& __a)
  171. : c(__q.c, __a) { }
  172. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  173. stack(stack&& __q, const _Alloc& __a)
  174. : c(std::move(__q.c), __a) { }
  175. #if __cplusplus > 202002L
  176. template<typename _InputIterator, typename _Alloc,
  177. typename = _RequireInputIter<_InputIterator>,
  178. typename = _Uses<_Alloc>>
  179. stack(_InputIterator __first, _InputIterator __last, const _Alloc& __a)
  180. : c(__first, __last, __a) { }
  181. #endif
  182. #endif
  183. /**
  184. * Returns true if the %stack is empty.
  185. */
  186. _GLIBCXX_NODISCARD bool
  187. empty() const
  188. { return c.empty(); }
  189. /** Returns the number of elements in the %stack. */
  190. _GLIBCXX_NODISCARD
  191. size_type
  192. size() const
  193. { return c.size(); }
  194. /**
  195. * Returns a read/write reference to the data at the first
  196. * element of the %stack.
  197. */
  198. _GLIBCXX_NODISCARD
  199. reference
  200. top()
  201. {
  202. __glibcxx_requires_nonempty();
  203. return c.back();
  204. }
  205. /**
  206. * Returns a read-only (constant) reference to the data at the first
  207. * element of the %stack.
  208. */
  209. _GLIBCXX_NODISCARD
  210. const_reference
  211. top() const
  212. {
  213. __glibcxx_requires_nonempty();
  214. return c.back();
  215. }
  216. /**
  217. * @brief Add data to the top of the %stack.
  218. * @param __x Data to be added.
  219. *
  220. * This is a typical %stack operation. The function creates an
  221. * element at the top of the %stack and assigns the given data
  222. * to it. The time complexity of the operation depends on the
  223. * underlying sequence.
  224. */
  225. void
  226. push(const value_type& __x)
  227. { c.push_back(__x); }
  228. #if __cplusplus >= 201103L
  229. void
  230. push(value_type&& __x)
  231. { c.push_back(std::move(__x)); }
  232. #if __cplusplus > 201402L
  233. template<typename... _Args>
  234. decltype(auto)
  235. emplace(_Args&&... __args)
  236. { return c.emplace_back(std::forward<_Args>(__args)...); }
  237. #else
  238. template<typename... _Args>
  239. void
  240. emplace(_Args&&... __args)
  241. { c.emplace_back(std::forward<_Args>(__args)...); }
  242. #endif
  243. #endif
  244. /**
  245. * @brief Removes first element.
  246. *
  247. * This is a typical %stack operation. It shrinks the %stack
  248. * by one. The time complexity of the operation depends on the
  249. * underlying sequence.
  250. *
  251. * Note that no data is returned, and if the first element's
  252. * data is needed, it should be retrieved before pop() is
  253. * called.
  254. */
  255. void
  256. pop()
  257. {
  258. __glibcxx_requires_nonempty();
  259. c.pop_back();
  260. }
  261. #if __cplusplus >= 201103L
  262. void
  263. swap(stack& __s)
  264. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  265. noexcept(__is_nothrow_swappable<_Sequence>::value)
  266. #else
  267. noexcept(__is_nothrow_swappable<_Tp>::value)
  268. #endif
  269. {
  270. using std::swap;
  271. swap(c, __s.c);
  272. }
  273. #endif // __cplusplus >= 201103L
  274. };
  275. #if __cpp_deduction_guides >= 201606
  276. template<typename _Container,
  277. typename = _RequireNotAllocator<_Container>>
  278. stack(_Container) -> stack<typename _Container::value_type, _Container>;
  279. template<typename _Container, typename _Allocator,
  280. typename = _RequireNotAllocator<_Container>>
  281. stack(_Container, _Allocator)
  282. -> stack<typename _Container::value_type, _Container>;
  283. #ifdef __cpp_lib_adaptor_iterator_pair_constructor
  284. template<typename _InputIterator,
  285. typename _ValT
  286. = typename iterator_traits<_InputIterator>::value_type,
  287. typename = _RequireInputIter<_InputIterator>>
  288. stack(_InputIterator, _InputIterator) -> stack<_ValT>;
  289. template<typename _InputIterator, typename _Allocator,
  290. typename _ValT
  291. = typename iterator_traits<_InputIterator>::value_type,
  292. typename = _RequireInputIter<_InputIterator>,
  293. typename = _RequireAllocator<_Allocator>>
  294. stack(_InputIterator, _InputIterator, _Allocator)
  295. -> stack<_ValT, deque<_ValT, _Allocator>>;
  296. #endif
  297. #endif
  298. /**
  299. * @brief Stack equality comparison.
  300. * @param __x A %stack.
  301. * @param __y A %stack of the same type as @a __x.
  302. * @return True iff the size and elements of the stacks are equal.
  303. *
  304. * This is an equivalence relation. Complexity and semantics
  305. * depend on the underlying sequence type, but the expected rules
  306. * are: this relation is linear in the size of the sequences, and
  307. * stacks are considered equivalent if their sequences compare
  308. * equal.
  309. */
  310. template<typename _Tp, typename _Seq>
  311. _GLIBCXX_NODISCARD
  312. inline bool
  313. operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  314. { return __x.c == __y.c; }
  315. /**
  316. * @brief Stack ordering relation.
  317. * @param __x A %stack.
  318. * @param __y A %stack of the same type as @a x.
  319. * @return True iff @a x is lexicographically less than @a __y.
  320. *
  321. * This is an total ordering relation. Complexity and semantics
  322. * depend on the underlying sequence type, but the expected rules
  323. * are: this relation is linear in the size of the sequences, the
  324. * elements must be comparable with @c <, and
  325. * std::lexicographical_compare() is usually used to make the
  326. * determination.
  327. */
  328. template<typename _Tp, typename _Seq>
  329. _GLIBCXX_NODISCARD
  330. inline bool
  331. operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  332. { return __x.c < __y.c; }
  333. /// Based on operator==
  334. template<typename _Tp, typename _Seq>
  335. _GLIBCXX_NODISCARD
  336. inline bool
  337. operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  338. { return !(__x == __y); }
  339. /// Based on operator<
  340. template<typename _Tp, typename _Seq>
  341. _GLIBCXX_NODISCARD
  342. inline bool
  343. operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  344. { return __y < __x; }
  345. /// Based on operator<
  346. template<typename _Tp, typename _Seq>
  347. _GLIBCXX_NODISCARD
  348. inline bool
  349. operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  350. { return !(__y < __x); }
  351. /// Based on operator<
  352. template<typename _Tp, typename _Seq>
  353. _GLIBCXX_NODISCARD
  354. inline bool
  355. operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  356. { return !(__x < __y); }
  357. #if __cpp_lib_three_way_comparison
  358. template<typename _Tp, three_way_comparable _Seq>
  359. [[nodiscard]]
  360. inline compare_three_way_result_t<_Seq>
  361. operator<=>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  362. { return __x.c <=> __y.c; }
  363. #endif
  364. #if __cplusplus >= 201103L
  365. template<typename _Tp, typename _Seq>
  366. inline
  367. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  368. // Constrained free swap overload, see p0185r1
  369. typename enable_if<__is_swappable<_Seq>::value>::type
  370. #else
  371. void
  372. #endif
  373. swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
  374. noexcept(noexcept(__x.swap(__y)))
  375. { __x.swap(__y); }
  376. template<typename _Tp, typename _Seq, typename _Alloc>
  377. struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
  378. : public uses_allocator<_Seq, _Alloc>::type { };
  379. #endif // __cplusplus >= 201103L
  380. _GLIBCXX_END_NAMESPACE_VERSION
  381. } // namespace
  382. #endif /* _STL_STACK_H */