1 // Stack implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
41 * Copyright (c) 1996,1997
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_stack.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{stack}
59 #define _STL_STACK_H 1
61 #include <bits/concept_check.h>
62 #include <debug/debug.h>
64 _GLIBCXX_BEGIN_NAMESPACE(std
)
67 * @brief A standard container giving FILO behavior.
71 * Meets many of the requirements of a
72 * <a href="tables.html#65">container</a>,
73 * but does not define anything to do with iterators. Very few of the
74 * other standard container interfaces are defined.
76 * This is not a true container, but an @e adaptor. It holds
77 * another container, and provides a wrapper interface to that
78 * container. The wrapper is what enforces strict
79 * first-in-last-out %stack behavior.
81 * The second template parameter defines the type of the underlying
82 * sequence/container. It defaults to std::deque, but it can be
83 * any type that supports @c back, @c push_back, and @c pop_front,
84 * such as std::list, std::vector, or an appropriate user-defined
87 * Members not found in @a normal containers are @c container_type,
88 * which is a typedef for the second Sequence parameter, and @c
89 * push, @c pop, and @c top, which are standard %stack/FILO
92 template<typename _Tp
, typename _Sequence
= deque
<_Tp
> >
95 // concept requirements
96 typedef typename
_Sequence::value_type _Sequence_value_type
;
97 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
98 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
99 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
101 template<typename _Tp1
, typename _Seq1
>
103 operator==(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
105 template<typename _Tp1
, typename _Seq1
>
107 operator<(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
110 typedef typename
_Sequence::value_type value_type
;
111 typedef typename
_Sequence::reference reference
;
112 typedef typename
_Sequence::const_reference const_reference
;
113 typedef typename
_Sequence::size_type size_type
;
114 typedef _Sequence container_type
;
117 // See queue::c for notes on this name.
121 // XXX removed old def ctor, added def arg to this one to match 14882
123 * @brief Default constructor creates no elements.
125 #ifndef __GXX_EXPERIMENTAL_CXX0X__
127 stack(const _Sequence
& __c
= _Sequence())
131 stack(const _Sequence
& __c
)
135 stack(_Sequence
&& __c
= _Sequence())
136 : c(std::move(__c
)) { }
140 * Returns true if the %stack is empty.
144 { return c
.empty(); }
146 /** Returns the number of elements in the %stack. */
152 * Returns a read/write reference to the data at the first
153 * element of the %stack.
158 __glibcxx_requires_nonempty();
163 * Returns a read-only (constant) reference to the data at the first
164 * element of the %stack.
169 __glibcxx_requires_nonempty();
174 * @brief Add data to the top of the %stack.
175 * @param x Data to be added.
177 * This is a typical %stack operation. The function creates an
178 * element at the top of the %stack and assigns the given data
179 * to it. The time complexity of the operation depends on the
180 * underlying sequence.
183 push(const value_type
& __x
)
184 { c
.push_back(__x
); }
186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
188 push(value_type
&& __x
)
189 { c
.push_back(std::move(__x
)); }
191 template<typename
... _Args
>
193 emplace(_Args
&&... __args
)
194 { c
.emplace_back(std::forward
<_Args
>(__args
)...); }
198 * @brief Removes first element.
200 * This is a typical %stack operation. It shrinks the %stack
201 * by one. The time complexity of the operation depends on the
202 * underlying sequence.
204 * Note that no data is returned, and if the first element's
205 * data is needed, it should be retrieved before pop() is
211 __glibcxx_requires_nonempty();
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
226 * @brief Stack equality comparison.
228 * @param y A %stack of the same type as @a x.
229 * @return True iff the size and elements of the stacks are equal.
231 * This is an equivalence relation. Complexity and semantics
232 * depend on the underlying sequence type, but the expected rules
233 * are: this relation is linear in the size of the sequences, and
234 * stacks are considered equivalent if their sequences compare
237 template<typename _Tp
, typename _Seq
>
239 operator==(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
240 { return __x
.c
== __y
.c
; }
243 * @brief Stack ordering relation.
245 * @param y A %stack of the same type as @a x.
246 * @return True iff @a x is lexicographically less than @a y.
248 * This is an total ordering relation. Complexity and semantics
249 * depend on the underlying sequence type, but the expected rules
250 * are: this relation is linear in the size of the sequences, the
251 * elements must be comparable with @c <, and
252 * std::lexicographical_compare() is usually used to make the
255 template<typename _Tp
, typename _Seq
>
257 operator<(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
258 { return __x
.c
< __y
.c
; }
260 /// Based on operator==
261 template<typename _Tp
, typename _Seq
>
263 operator!=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
264 { return !(__x
== __y
); }
266 /// Based on operator<
267 template<typename _Tp
, typename _Seq
>
269 operator>(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
270 { return __y
< __x
; }
272 /// Based on operator<
273 template<typename _Tp
, typename _Seq
>
275 operator<=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
276 { return !(__y
< __x
); }
278 /// Based on operator<
279 template<typename _Tp
, typename _Seq
>
281 operator>=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
282 { return !(__x
< __y
); }
284 #ifdef __GXX_EXPERIMENTAL_CXX0X__
285 template<typename _Tp
, typename _Seq
>
287 swap(stack
<_Tp
, _Seq
>& __x
, stack
<_Tp
, _Seq
>& __y
)
290 template<typename _Tp
, typename _Seq
, typename _Alloc
>
291 struct uses_allocator
<stack
<_Tp
, _Seq
>, _Alloc
>
292 : public uses_allocator
<_Seq
, _Alloc
>::type
{ };
295 _GLIBCXX_END_NAMESPACE
297 #endif /* _STL_STACK_H */