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