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