1 // Stack implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
65 #include <debug/debug.h>
69 // Forward declarations of operators == and <, needed for friend
71 template<typename _Tp
, typename _Sequence
= deque
<_Tp
> >
74 template<typename _Tp
, typename _Seq
>
76 operator==(const stack
<_Tp
,_Seq
>& __x
, const stack
<_Tp
,_Seq
>& __y
);
78 template<typename _Tp
, typename _Seq
>
80 operator<(const stack
<_Tp
,_Seq
>& __x
, const stack
<_Tp
,_Seq
>& __y
);
83 * @brief A standard container giving FILO behavior.
88 * Meets many of the requirements of a
89 * <a href="tables.html#65">container</a>,
90 * but does not define anything to do with iterators. Very few of the
91 * other standard container interfaces are defined.
93 * This is not a true container, but an @e adaptor. It holds
94 * another container, and provides a wrapper interface to that
95 * container. The wrapper is what enforces strict
96 * first-in-last-out %stack behavior.
98 * The second template parameter defines the type of the underlying
99 * sequence/container. It defaults to std::deque, but it can be
100 * any type that supports @c back, @c push_back, and @c pop_front,
101 * such as std::list, std::vector, or an appropriate user-defined
104 * Members not found in "normal" containers are @c container_type,
105 * which is a typedef for the second Sequence parameter, and @c
106 * push, @c pop, and @c top, which are standard %stack/FILO
109 template<typename _Tp
, typename _Sequence
>
112 // concept requirements
113 typedef typename
_Sequence::value_type _Sequence_value_type
;
114 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
115 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
116 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
118 template<typename _Tp1
, typename _Seq1
>
120 operator==(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
122 template<typename _Tp1
, typename _Seq1
>
124 operator<(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
127 typedef typename
_Sequence::value_type value_type
;
128 typedef typename
_Sequence::reference reference
;
129 typedef typename
_Sequence::const_reference const_reference
;
130 typedef typename
_Sequence::size_type size_type
;
131 typedef _Sequence container_type
;
134 // See queue::c for notes on this name.
138 // XXX removed old def ctor, added def arg to this one to match 14882
140 * @brief Default constructor creates no elements.
143 stack(const _Sequence
& __c
= _Sequence())
147 * Returns true if the %stack is empty.
151 { return c
.empty(); }
153 /** Returns the number of elements in the %stack. */
159 * Returns a read/write reference to the data at the first
160 * element of the %stack.
165 __glibcxx_requires_nonempty();
170 * Returns a read-only (constant) reference to the data at the first
171 * element of the %stack.
176 __glibcxx_requires_nonempty();
181 * @brief Add data to the top of the %stack.
182 * @param x Data to be added.
184 * This is a typical %stack operation. The function creates an
185 * element at the top of the %stack and assigns the given data
186 * to it. The time complexity of the operation depends on the
187 * underlying sequence.
190 push(const value_type
& __x
)
191 { c
.push_back(__x
); }
194 * @brief Removes first element.
196 * This is a typical %stack operation. It shrinks the %stack
197 * by one. The time complexity of the operation depends on the
198 * underlying sequence.
200 * Note that no data is returned, and if the first element's
201 * data is needed, it should be retrieved before pop() is
207 __glibcxx_requires_nonempty();
213 * @brief Stack equality comparison.
215 * @param y A %stack of the same type as @a x.
216 * @return True iff the size and elements of the stacks are equal.
218 * This is an equivalence relation. Complexity and semantics
219 * depend on the underlying sequence type, but the expected rules
220 * are: this relation is linear in the size of the sequences, and
221 * stacks are considered equivalent if their sequences compare
224 template<typename _Tp
, typename _Seq
>
226 operator==(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
227 { return __x
.c
== __y
.c
; }
230 * @brief Stack ordering relation.
232 * @param y A %stack of the same type as @a x.
233 * @return True iff @a x is lexicographically less than @a y.
235 * This is an total ordering relation. Complexity and semantics
236 * depend on the underlying sequence type, but the expected rules
237 * are: this relation is linear in the size of the sequences, the
238 * elements must be comparable with @c <, and
239 * std::lexicographical_compare() is usually used to make the
242 template<typename _Tp
, typename _Seq
>
244 operator<(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
245 { return __x
.c
< __y
.c
; }
247 /// Based on operator==
248 template<typename _Tp
, typename _Seq
>
250 operator!=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
251 { return !(__x
== __y
); }
253 /// Based on operator<
254 template<typename _Tp
, typename _Seq
>
256 operator>(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
257 { return __y
< __x
; }
259 /// Based on operator<
260 template<typename _Tp
, typename _Seq
>
262 operator<=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
263 { return !(__y
< __x
); }
265 /// Based on operator<
266 template<typename _Tp
, typename _Seq
>
268 operator>=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
269 { return !(__x
< __y
); }
272 #endif /* _STACK_H */