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