You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
12KB

  1. // Stack implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 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. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  153. explicit
  154. stack(const _Alloc& __a)
  155. : c(__a) { }
  156. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  157. stack(const _Sequence& __c, const _Alloc& __a)
  158. : c(__c, __a) { }
  159. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  160. stack(_Sequence&& __c, const _Alloc& __a)
  161. : c(std::move(__c), __a) { }
  162. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  163. stack(const stack& __q, const _Alloc& __a)
  164. : c(__q.c, __a) { }
  165. template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
  166. stack(stack&& __q, const _Alloc& __a)
  167. : c(std::move(__q.c), __a) { }
  168. #endif
  169. /**
  170. * Returns true if the %stack is empty.
  171. */
  172. _GLIBCXX_NODISCARD bool
  173. empty() const
  174. { return c.empty(); }
  175. /** Returns the number of elements in the %stack. */
  176. size_type
  177. size() const
  178. { return c.size(); }
  179. /**
  180. * Returns a read/write reference to the data at the first
  181. * element of the %stack.
  182. */
  183. reference
  184. top()
  185. {
  186. __glibcxx_requires_nonempty();
  187. return c.back();
  188. }
  189. /**
  190. * Returns a read-only (constant) reference to the data at the first
  191. * element of the %stack.
  192. */
  193. const_reference
  194. top() const
  195. {
  196. __glibcxx_requires_nonempty();
  197. return c.back();
  198. }
  199. /**
  200. * @brief Add data to the top of the %stack.
  201. * @param __x Data to be added.
  202. *
  203. * This is a typical %stack operation. The function creates an
  204. * element at the top of the %stack and assigns the given data
  205. * to it. The time complexity of the operation depends on the
  206. * underlying sequence.
  207. */
  208. void
  209. push(const value_type& __x)
  210. { c.push_back(__x); }
  211. #if __cplusplus >= 201103L
  212. void
  213. push(value_type&& __x)
  214. { c.push_back(std::move(__x)); }
  215. #if __cplusplus > 201402L
  216. template<typename... _Args>
  217. decltype(auto)
  218. emplace(_Args&&... __args)
  219. { return c.emplace_back(std::forward<_Args>(__args)...); }
  220. #else
  221. template<typename... _Args>
  222. void
  223. emplace(_Args&&... __args)
  224. { c.emplace_back(std::forward<_Args>(__args)...); }
  225. #endif
  226. #endif
  227. /**
  228. * @brief Removes first element.
  229. *
  230. * This is a typical %stack operation. It shrinks the %stack
  231. * by one. The time complexity of the operation depends on the
  232. * underlying sequence.
  233. *
  234. * Note that no data is returned, and if the first element's
  235. * data is needed, it should be retrieved before pop() is
  236. * called.
  237. */
  238. void
  239. pop()
  240. {
  241. __glibcxx_requires_nonempty();
  242. c.pop_back();
  243. }
  244. #if __cplusplus >= 201103L
  245. void
  246. swap(stack& __s)
  247. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  248. noexcept(__is_nothrow_swappable<_Sequence>::value)
  249. #else
  250. noexcept(__is_nothrow_swappable<_Tp>::value)
  251. #endif
  252. {
  253. using std::swap;
  254. swap(c, __s.c);
  255. }
  256. #endif // __cplusplus >= 201103L
  257. };
  258. #if __cpp_deduction_guides >= 201606
  259. template<typename _Container,
  260. typename = _RequireNotAllocator<_Container>>
  261. stack(_Container) -> stack<typename _Container::value_type, _Container>;
  262. template<typename _Container, typename _Allocator,
  263. typename = _RequireNotAllocator<_Container>,
  264. typename = _RequireAllocator<_Allocator>>
  265. stack(_Container, _Allocator)
  266. -> stack<typename _Container::value_type, _Container>;
  267. #endif
  268. /**
  269. * @brief Stack equality comparison.
  270. * @param __x A %stack.
  271. * @param __y A %stack of the same type as @a __x.
  272. * @return True iff the size and elements of the stacks are equal.
  273. *
  274. * This is an equivalence relation. Complexity and semantics
  275. * depend on the underlying sequence type, but the expected rules
  276. * are: this relation is linear in the size of the sequences, and
  277. * stacks are considered equivalent if their sequences compare
  278. * equal.
  279. */
  280. template<typename _Tp, typename _Seq>
  281. inline bool
  282. operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  283. { return __x.c == __y.c; }
  284. /**
  285. * @brief Stack ordering relation.
  286. * @param __x A %stack.
  287. * @param __y A %stack of the same type as @a x.
  288. * @return True iff @a x is lexicographically less than @a __y.
  289. *
  290. * This is an total ordering relation. Complexity and semantics
  291. * depend on the underlying sequence type, but the expected rules
  292. * are: this relation is linear in the size of the sequences, the
  293. * elements must be comparable with @c <, and
  294. * std::lexicographical_compare() is usually used to make the
  295. * determination.
  296. */
  297. template<typename _Tp, typename _Seq>
  298. inline bool
  299. operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  300. { return __x.c < __y.c; }
  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 !(__x == __y); }
  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 !(__y < __x); }
  316. /// Based on operator<
  317. template<typename _Tp, typename _Seq>
  318. inline bool
  319. operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  320. { return !(__x < __y); }
  321. #if __cpp_lib_three_way_comparison
  322. template<typename _Tp, three_way_comparable _Seq>
  323. inline compare_three_way_result_t<_Seq>
  324. operator<=>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
  325. { return __x.c <=> __y.c; }
  326. #endif
  327. #if __cplusplus >= 201103L
  328. template<typename _Tp, typename _Seq>
  329. inline
  330. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  331. // Constrained free swap overload, see p0185r1
  332. typename enable_if<__is_swappable<_Seq>::value>::type
  333. #else
  334. void
  335. #endif
  336. swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
  337. noexcept(noexcept(__x.swap(__y)))
  338. { __x.swap(__y); }
  339. template<typename _Tp, typename _Seq, typename _Alloc>
  340. struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
  341. : public uses_allocator<_Seq, _Alloc>::type { };
  342. #endif // __cplusplus >= 201103L
  343. _GLIBCXX_END_NAMESPACE_VERSION
  344. } // namespace
  345. #endif /* _STL_STACK_H */