stl_stack.h 12 KB

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