138fd1498Szrj // Stack implementation -*- C++ -*- 238fd1498Szrj 338fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc. 438fd1498Szrj // 538fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 638fd1498Szrj // software; you can redistribute it and/or modify it under the 738fd1498Szrj // terms of the GNU General Public License as published by the 838fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 938fd1498Szrj // any later version. 1038fd1498Szrj 1138fd1498Szrj // This library is distributed in the hope that it will be useful, 1238fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 1338fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1438fd1498Szrj // GNU General Public License for more details. 1538fd1498Szrj 1638fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 1738fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 1838fd1498Szrj // 3.1, as published by the Free Software Foundation. 1938fd1498Szrj 2038fd1498Szrj // You should have received a copy of the GNU General Public License and 2138fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 2238fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2338fd1498Szrj // <http://www.gnu.org/licenses/>. 2438fd1498Szrj 2538fd1498Szrj /* 2638fd1498Szrj * 2738fd1498Szrj * Copyright (c) 1994 2838fd1498Szrj * Hewlett-Packard Company 2938fd1498Szrj * 3038fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 3138fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 3238fd1498Szrj * provided that the above copyright notice appear in all copies and 3338fd1498Szrj * that both that copyright notice and this permission notice appear 3438fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no 3538fd1498Szrj * representations about the suitability of this software for any 3638fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 3738fd1498Szrj * 3838fd1498Szrj * 3938fd1498Szrj * Copyright (c) 1996,1997 4038fd1498Szrj * Silicon Graphics Computer Systems, Inc. 4138fd1498Szrj * 4238fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 4338fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 4438fd1498Szrj * provided that the above copyright notice appear in all copies and 4538fd1498Szrj * that both that copyright notice and this permission notice appear 4638fd1498Szrj * in supporting documentation. Silicon Graphics makes no 4738fd1498Szrj * representations about the suitability of this software for any 4838fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 4938fd1498Szrj */ 5038fd1498Szrj 5138fd1498Szrj /** @file bits/stl_stack.h 5238fd1498Szrj * This is an internal header file, included by other library headers. 5338fd1498Szrj * Do not attempt to use it directly. @headername{stack} 5438fd1498Szrj */ 5538fd1498Szrj 5638fd1498Szrj #ifndef _STL_STACK_H 5738fd1498Szrj #define _STL_STACK_H 1 5838fd1498Szrj 5938fd1498Szrj #include <bits/concept_check.h> 6038fd1498Szrj #include <debug/debug.h> 6138fd1498Szrj #if __cplusplus >= 201103L 6238fd1498Szrj # include <bits/uses_allocator.h> 6338fd1498Szrj #endif 6438fd1498Szrj 6538fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 6638fd1498Szrj { 6738fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 6838fd1498Szrj 6938fd1498Szrj /** 7038fd1498Szrj * @brief A standard container giving FILO behavior. 7138fd1498Szrj * 7238fd1498Szrj * @ingroup sequences 7338fd1498Szrj * 7438fd1498Szrj * @tparam _Tp Type of element. 7538fd1498Szrj * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>. 7638fd1498Szrj * 7738fd1498Szrj * Meets many of the requirements of a 7838fd1498Szrj * <a href="tables.html#65">container</a>, 7938fd1498Szrj * but does not define anything to do with iterators. Very few of the 8038fd1498Szrj * other standard container interfaces are defined. 8138fd1498Szrj * 8238fd1498Szrj * This is not a true container, but an @e adaptor. It holds 8338fd1498Szrj * another container, and provides a wrapper interface to that 8438fd1498Szrj * container. The wrapper is what enforces strict 8538fd1498Szrj * first-in-last-out %stack behavior. 8638fd1498Szrj * 8738fd1498Szrj * The second template parameter defines the type of the underlying 8838fd1498Szrj * sequence/container. It defaults to std::deque, but it can be 8938fd1498Szrj * any type that supports @c back, @c push_back, and @c pop_back, 9038fd1498Szrj * such as std::list, std::vector, or an appropriate user-defined 9138fd1498Szrj * type. 9238fd1498Szrj * 9338fd1498Szrj * Members not found in @a normal containers are @c container_type, 9438fd1498Szrj * which is a typedef for the second Sequence parameter, and @c 9538fd1498Szrj * push, @c pop, and @c top, which are standard %stack/FILO 9638fd1498Szrj * operations. 9738fd1498Szrj */ 9838fd1498Szrj template<typename _Tp, typename _Sequence = deque<_Tp> > 9938fd1498Szrj class stack 10038fd1498Szrj { 10138fd1498Szrj #ifdef _GLIBCXX_CONCEPT_CHECKS 10238fd1498Szrj // concept requirements 10338fd1498Szrj typedef typename _Sequence::value_type _Sequence_value_type; 10438fd1498Szrj # if __cplusplus < 201103L 10538fd1498Szrj __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 10638fd1498Szrj __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) 10738fd1498Szrj # endif 10838fd1498Szrj __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 10938fd1498Szrj #endif 11038fd1498Szrj 11138fd1498Szrj template<typename _Tp1, typename _Seq1> 11238fd1498Szrj friend bool 11338fd1498Szrj operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); 11438fd1498Szrj 11538fd1498Szrj template<typename _Tp1, typename _Seq1> 11638fd1498Szrj friend bool 11738fd1498Szrj operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); 11838fd1498Szrj 11938fd1498Szrj #if __cplusplus >= 201103L 12038fd1498Szrj template<typename _Alloc> 12138fd1498Szrj using _Uses = typename 12238fd1498Szrj enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; 12338fd1498Szrj #endif 12438fd1498Szrj 12538fd1498Szrj public: 12638fd1498Szrj typedef typename _Sequence::value_type value_type; 12738fd1498Szrj typedef typename _Sequence::reference reference; 12838fd1498Szrj typedef typename _Sequence::const_reference const_reference; 12938fd1498Szrj typedef typename _Sequence::size_type size_type; 13038fd1498Szrj typedef _Sequence container_type; 13138fd1498Szrj 13238fd1498Szrj protected: 13338fd1498Szrj // See queue::c for notes on this name. 13438fd1498Szrj _Sequence c; 13538fd1498Szrj 13638fd1498Szrj public: 13738fd1498Szrj // XXX removed old def ctor, added def arg to this one to match 14882 13838fd1498Szrj /** 13938fd1498Szrj * @brief Default constructor creates no elements. 14038fd1498Szrj */ 14138fd1498Szrj #if __cplusplus < 201103L 14238fd1498Szrj explicit 14338fd1498Szrj stack(const _Sequence& __c = _Sequence()) 14438fd1498Szrj : c(__c) { } 14538fd1498Szrj #else 14638fd1498Szrj template<typename _Seq = _Sequence, typename _Requires = typename 14738fd1498Szrj enable_if<is_default_constructible<_Seq>::value>::type> 14838fd1498Szrj stack() 14938fd1498Szrj : c() { } 15038fd1498Szrj 15138fd1498Szrj explicit 15238fd1498Szrj stack(const _Sequence& __c) 15338fd1498Szrj : c(__c) { } 15438fd1498Szrj 15538fd1498Szrj explicit 15638fd1498Szrj stack(_Sequence&& __c) 15738fd1498Szrj : c(std::move(__c)) { } 15838fd1498Szrj 15938fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 16038fd1498Szrj explicit 16138fd1498Szrj stack(const _Alloc& __a) 16238fd1498Szrj : c(__a) { } 16338fd1498Szrj 16438fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 16538fd1498Szrj stack(const _Sequence& __c, const _Alloc& __a) 16638fd1498Szrj : c(__c, __a) { } 16738fd1498Szrj 16838fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 16938fd1498Szrj stack(_Sequence&& __c, const _Alloc& __a) 17038fd1498Szrj : c(std::move(__c), __a) { } 17138fd1498Szrj 17238fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 17338fd1498Szrj stack(const stack& __q, const _Alloc& __a) 17438fd1498Szrj : c(__q.c, __a) { } 17538fd1498Szrj 17638fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 17738fd1498Szrj stack(stack&& __q, const _Alloc& __a) 17838fd1498Szrj : c(std::move(__q.c), __a) { } 17938fd1498Szrj #endif 18038fd1498Szrj 18138fd1498Szrj /** 18238fd1498Szrj * Returns true if the %stack is empty. 18338fd1498Szrj */ 18438fd1498Szrj bool 18538fd1498Szrj empty() const 18638fd1498Szrj { return c.empty(); } 18738fd1498Szrj 18838fd1498Szrj /** Returns the number of elements in the %stack. */ 18938fd1498Szrj size_type 19038fd1498Szrj size() const 19138fd1498Szrj { return c.size(); } 19238fd1498Szrj 19338fd1498Szrj /** 19438fd1498Szrj * Returns a read/write reference to the data at the first 19538fd1498Szrj * element of the %stack. 19638fd1498Szrj */ 19738fd1498Szrj reference 19838fd1498Szrj top() 19938fd1498Szrj { 20038fd1498Szrj __glibcxx_requires_nonempty(); 20138fd1498Szrj return c.back(); 20238fd1498Szrj } 20338fd1498Szrj 20438fd1498Szrj /** 20538fd1498Szrj * Returns a read-only (constant) reference to the data at the first 20638fd1498Szrj * element of the %stack. 20738fd1498Szrj */ 20838fd1498Szrj const_reference 20938fd1498Szrj top() const 21038fd1498Szrj { 21138fd1498Szrj __glibcxx_requires_nonempty(); 21238fd1498Szrj return c.back(); 21338fd1498Szrj } 21438fd1498Szrj 21538fd1498Szrj /** 21638fd1498Szrj * @brief Add data to the top of the %stack. 21738fd1498Szrj * @param __x Data to be added. 21838fd1498Szrj * 21938fd1498Szrj * This is a typical %stack operation. The function creates an 22038fd1498Szrj * element at the top of the %stack and assigns the given data 22138fd1498Szrj * to it. The time complexity of the operation depends on the 22238fd1498Szrj * underlying sequence. 22338fd1498Szrj */ 22438fd1498Szrj void 22538fd1498Szrj push(const value_type& __x) 22638fd1498Szrj { c.push_back(__x); } 22738fd1498Szrj 22838fd1498Szrj #if __cplusplus >= 201103L 22938fd1498Szrj void 23038fd1498Szrj push(value_type&& __x) 23138fd1498Szrj { c.push_back(std::move(__x)); } 23238fd1498Szrj 23338fd1498Szrj #if __cplusplus > 201402L 23438fd1498Szrj template<typename... _Args> 23538fd1498Szrj decltype(auto) 23638fd1498Szrj emplace(_Args&&... __args) 23738fd1498Szrj { return c.emplace_back(std::forward<_Args>(__args)...); } 23838fd1498Szrj #else 23938fd1498Szrj template<typename... _Args> 24038fd1498Szrj void 24138fd1498Szrj emplace(_Args&&... __args) 24238fd1498Szrj { c.emplace_back(std::forward<_Args>(__args)...); } 24338fd1498Szrj #endif 24438fd1498Szrj #endif 24538fd1498Szrj 24638fd1498Szrj /** 24738fd1498Szrj * @brief Removes first element. 24838fd1498Szrj * 24938fd1498Szrj * This is a typical %stack operation. It shrinks the %stack 25038fd1498Szrj * by one. The time complexity of the operation depends on the 25138fd1498Szrj * underlying sequence. 25238fd1498Szrj * 25338fd1498Szrj * Note that no data is returned, and if the first element's 25438fd1498Szrj * data is needed, it should be retrieved before pop() is 25538fd1498Szrj * called. 25638fd1498Szrj */ 25738fd1498Szrj void 25838fd1498Szrj pop() 25938fd1498Szrj { 26038fd1498Szrj __glibcxx_requires_nonempty(); 26138fd1498Szrj c.pop_back(); 26238fd1498Szrj } 26338fd1498Szrj 26438fd1498Szrj #if __cplusplus >= 201103L 26538fd1498Szrj void 26638fd1498Szrj swap(stack& __s) 26738fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 26838fd1498Szrj noexcept(__is_nothrow_swappable<_Sequence>::value) 26938fd1498Szrj #else 27038fd1498Szrj noexcept(__is_nothrow_swappable<_Tp>::value) 27138fd1498Szrj #endif 27238fd1498Szrj { 27338fd1498Szrj using std::swap; 27438fd1498Szrj swap(c, __s.c); 27538fd1498Szrj } 27638fd1498Szrj #endif // __cplusplus >= 201103L 27738fd1498Szrj }; 27838fd1498Szrj 279*58e805e6Szrj #if __cpp_deduction_guides >= 201606 280*58e805e6Szrj template<typename _Container, 281*58e805e6Szrj typename = enable_if_t<!__is_allocator<_Container>::value>> 282*58e805e6Szrj stack(_Container) -> stack<typename _Container::value_type, _Container>; 283*58e805e6Szrj 284*58e805e6Szrj template<typename _Container, typename _Allocator, 285*58e805e6Szrj typename = enable_if_t<!__is_allocator<_Container>::value>, 286*58e805e6Szrj typename = enable_if_t<__is_allocator<_Allocator>::value>> 287*58e805e6Szrj stack(_Container, _Allocator) 288*58e805e6Szrj -> stack<typename _Container::value_type, _Container>; 289*58e805e6Szrj #endif 290*58e805e6Szrj 29138fd1498Szrj /** 29238fd1498Szrj * @brief Stack equality comparison. 29338fd1498Szrj * @param __x A %stack. 29438fd1498Szrj * @param __y A %stack of the same type as @a __x. 29538fd1498Szrj * @return True iff the size and elements of the stacks are equal. 29638fd1498Szrj * 29738fd1498Szrj * This is an equivalence relation. Complexity and semantics 29838fd1498Szrj * depend on the underlying sequence type, but the expected rules 29938fd1498Szrj * are: this relation is linear in the size of the sequences, and 30038fd1498Szrj * stacks are considered equivalent if their sequences compare 30138fd1498Szrj * equal. 30238fd1498Szrj */ 30338fd1498Szrj template<typename _Tp, typename _Seq> 30438fd1498Szrj inline bool 30538fd1498Szrj operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 30638fd1498Szrj { return __x.c == __y.c; } 30738fd1498Szrj 30838fd1498Szrj /** 30938fd1498Szrj * @brief Stack ordering relation. 31038fd1498Szrj * @param __x A %stack. 31138fd1498Szrj * @param __y A %stack of the same type as @a x. 31238fd1498Szrj * @return True iff @a x is lexicographically less than @a __y. 31338fd1498Szrj * 31438fd1498Szrj * This is an total ordering relation. Complexity and semantics 31538fd1498Szrj * depend on the underlying sequence type, but the expected rules 31638fd1498Szrj * are: this relation is linear in the size of the sequences, the 31738fd1498Szrj * elements must be comparable with @c <, and 31838fd1498Szrj * std::lexicographical_compare() is usually used to make the 31938fd1498Szrj * determination. 32038fd1498Szrj */ 32138fd1498Szrj template<typename _Tp, typename _Seq> 32238fd1498Szrj inline bool 32338fd1498Szrj operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 32438fd1498Szrj { return __x.c < __y.c; } 32538fd1498Szrj 32638fd1498Szrj /// Based on operator== 32738fd1498Szrj template<typename _Tp, typename _Seq> 32838fd1498Szrj inline bool 32938fd1498Szrj operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 33038fd1498Szrj { return !(__x == __y); } 33138fd1498Szrj 33238fd1498Szrj /// Based on operator< 33338fd1498Szrj template<typename _Tp, typename _Seq> 33438fd1498Szrj inline bool 33538fd1498Szrj operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 33638fd1498Szrj { return __y < __x; } 33738fd1498Szrj 33838fd1498Szrj /// Based on operator< 33938fd1498Szrj template<typename _Tp, typename _Seq> 34038fd1498Szrj inline bool 34138fd1498Szrj operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 34238fd1498Szrj { return !(__y < __x); } 34338fd1498Szrj 34438fd1498Szrj /// Based on operator< 34538fd1498Szrj template<typename _Tp, typename _Seq> 34638fd1498Szrj inline bool 34738fd1498Szrj operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) 34838fd1498Szrj { return !(__x < __y); } 34938fd1498Szrj 35038fd1498Szrj #if __cplusplus >= 201103L 35138fd1498Szrj template<typename _Tp, typename _Seq> 35238fd1498Szrj inline 35338fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 35438fd1498Szrj // Constrained free swap overload, see p0185r1 35538fd1498Szrj typename enable_if<__is_swappable<_Seq>::value>::type 35638fd1498Szrj #else 35738fd1498Szrj void 35838fd1498Szrj #endif 35938fd1498Szrj swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y) 36038fd1498Szrj noexcept(noexcept(__x.swap(__y))) 36138fd1498Szrj { __x.swap(__y); } 36238fd1498Szrj 36338fd1498Szrj template<typename _Tp, typename _Seq, typename _Alloc> 36438fd1498Szrj struct uses_allocator<stack<_Tp, _Seq>, _Alloc> 36538fd1498Szrj : public uses_allocator<_Seq, _Alloc>::type { }; 36638fd1498Szrj #endif // __cplusplus >= 201103L 36738fd1498Szrj 36838fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 36938fd1498Szrj } // namespace 37038fd1498Szrj 37138fd1498Szrj #endif /* _STL_STACK_H */ 372