stl_stack.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Stack implementation -*- C++ -*-
  2. // Copyright (C) 2001-2019 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 __cplusplus >= 201103L
  107. template<typename _Alloc>
  108. using _Uses = typename
  109. enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
  110. #if __cplusplus >= 201703L
  111. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  112. // 2566. Requirements on the first template parameter of container
  113. // adaptors
  114. static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
  115. "value_type must be the same as the underlying container");
  116. #endif // C++17
  117. #endif // C++11
  118. public:
  119. typedef typename _Sequence::value_type value_type;
  120. typedef typename _Sequence::reference reference;
  121. typedef typename _Sequence::const_reference const_reference;
  122. typedef typename _Sequence::size_type size_type;
  123. typedef _Sequence container_type;
  124. protected:
  125. // See queue::c for notes on this name.
  126. _Sequence c;
  127. public:
  128. // XXX removed old def ctor, added def arg to this one to match 14882
  129. /**
  130. * @brief Default constructor creates no elements.
  131. */
  132. #if __cplusplus < 201103L
  133. explicit
  134. stack(const _Sequence& __c = _Sequence())
  135. : c(__c) { }
  136. #else
  137. template<typename _Seq = _Sequence, typename _Requires = typename
  138. enable_if<is_default_constructible<_Seq>::value>::type>
  139. stack()
  140. : c() { }
  141. explicit
  142. stack(const _Sequence& __c)
  143. : c(__c) { }
  144. explicit
  145. stack(_Sequence&& __c)
  146. : c(std::move(__c)) { }
  147. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  148. explicit
  149. stack(const _Alloc& __a)
  150. : c(__a) { }
  151. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  152. stack(const _Sequence& __c, const _Alloc& __a)
  153. : c(__c, __a) { }
  154. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  155. stack(_Sequence&& __c, const _Alloc& __a)
  156. : c(std::move(__c), __a) { }
  157. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  158. stack(const stack& __q, const _Alloc& __a)
  159. : c(__q.c, __a) { }
  160. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  161. stack(stack&& __q, const _Alloc& __a)
  162. : c(std::move(__q.c), __a) { }
  163. #endif
  164. /**
  165. * Returns true if the %stack is empty.
  166. */
  167. _GLIBCXX_NODISCARD bool
  168. empty() const
  169. { return c.empty(); }
  170. /** Returns the number of elements in the %stack. */
  171. size_type
  172. size() const
  173. { return c.size(); }
  174. /**
  175. * Returns a read/write reference to the data at the first
  176. * element of the %stack.
  177. */
  178. reference
  179. top()
  180. {
  181. __glibcxx_requires_nonempty();
  182. return c.back();
  183. }
  184. /**
  185. * Returns a read-only (constant) reference to the data at the first
  186. * element of the %stack.
  187. */
  188. const_reference
  189. top() const
  190. {
  191. __glibcxx_requires_nonempty();
  192. return c.back();
  193. }
  194. /**
  195. * @brief Add data to the top of the %stack.
  196. * @param __x Data to be added.
  197. *
  198. * This is a typical %stack operation. The function creates an
  199. * element at the top of the %stack and assigns the given data
  200. * to it. The time complexity of the operation depends on the
  201. * underlying sequence.
  202. */
  203. void
  204. push(const value_type& __x)
  205. { c.push_back(__x); }
  206. #if __cplusplus >= 201103L
  207. void
  208. push(value_type&& __x)
  209. { c.push_back(std::move(__x)); }
  210. #if __cplusplus > 201402L
  211. template<typename... _Args>
  212. decltype(auto)
  213. emplace(_Args&&... __args)
  214. { return c.emplace_back(std::forward<_Args>(__args)...); }
  215. #else
  216. template<typename... _Args>
  217. void
  218. emplace(_Args&&... __args)
  219. { c.emplace_back(std::forward<_Args>(__args)...); }
  220. #endif
  221. #endif
  222. /**
  223. * @brief Removes first element.
  224. *
  225. * This is a typical %stack operation. It shrinks the %stack
  226. * by one. The time complexity of the operation depends on the
  227. * underlying sequence.
  228. *
  229. * Note that no data is returned, and if the first element's
  230. * data is needed, it should be retrieved before pop() is
  231. * called.
  232. */
  233. void
  234. pop()
  235. {
  236. __glibcxx_requires_nonempty();
  237. c.pop_back();
  238. }
  239. #if __cplusplus >= 201103L
  240. void
  241. swap(stack& __s)
  242. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  243. noexcept(__is_nothrow_swappable<_Sequence>::value)
  244. #else
  245. noexcept(__is_nothrow_swappable<_Tp>::value)
  246. #endif
  247. {
  248. using std::swap;
  249. swap(c, __s.c);
  250. }
  251. #endif // __cplusplus >= 201103L
  252. };
  253. #if __cpp_deduction_guides >= 201606
  254. template<typename _Container,
  255. typename = _RequireNotAllocator<_Container>>
  256. stack(_Container) -> stack<typename _Container::value_type, _Container>;
  257. template<typename _Container, typename _Allocator,
  258. typename = _RequireNotAllocator<_Container>,
  259. typename = _RequireAllocator<_Allocator>>
  260. stack(_Container, _Allocator)
  261. -> stack<typename _Container::value_type, _Container>;
  262. #endif
  263. /**
  264. * @brief Stack equality comparison.
  265. * @param __x A %stack.
  266. * @param __y A %stack of the same type as @a __x.
  267. * @return True iff the size and elements of the stacks are equal.
  268. *
  269. * This is an equivalence relation. Complexity and semantics
  270. * depend on the underlying sequence type, but the expected rules
  271. * are: this relation is linear in the size of the sequences, and
  272. * stacks are considered equivalent if their sequences compare
  273. * equal.
  274. */
  275. template<typename _Tp, typename _Seq>
  276. inline bool
  277. operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  278. { return __x.c == __y.c; }
  279. /**
  280. * @brief Stack ordering relation.
  281. * @param __x A %stack.
  282. * @param __y A %stack of the same type as @a x.
  283. * @return True iff @a x is lexicographically less than @a __y.
  284. *
  285. * This is an total ordering relation. Complexity and semantics
  286. * depend on the underlying sequence type, but the expected rules
  287. * are: this relation is linear in the size of the sequences, the
  288. * elements must be comparable with @c <, and
  289. * std::lexicographical_compare() is usually used to make the
  290. * determination.
  291. */
  292. template<typename _Tp, typename _Seq>
  293. inline bool
  294. operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  295. { return __x.c < __y.c; }
  296. /// Based on operator==
  297. template<typename _Tp, typename _Seq>
  298. inline bool
  299. operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  300. { return !(__x == __y); }
  301. /// Based on operator<
  302. template<typename _Tp, typename _Seq>
  303. inline bool
  304. operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  305. { return __y < __x; }
  306. /// Based on operator<
  307. template<typename _Tp, typename _Seq>
  308. inline bool
  309. operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  310. { return !(__y < __x); }
  311. /// Based on operator<
  312. template<typename _Tp, typename _Seq>
  313. inline bool
  314. operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  315. { return !(__x < __y); }
  316. #if __cplusplus >= 201103L
  317. template<typename _Tp, typename _Seq>
  318. inline
  319. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  320. // Constrained free swap overload, see p0185r1
  321. typename enable_if<__is_swappable<_Seq>::value>::type
  322. #else
  323. void
  324. #endif
  325. swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
  326. noexcept(noexcept(__x.swap(__y)))
  327. { __x.swap(__y); }
  328. template<typename _Tp, typename _Seq, typename _Alloc>
  329. struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
  330. : public uses_allocator<_Seq, _Alloc>::type { };
  331. #endif // __cplusplus >= 201103L
  332. _GLIBCXX_END_NAMESPACE_VERSION
  333. } // namespace
  334. #endif /* _STL_STACK_H */