1 // Stack implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_stack.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{stack}
57 #define _STL_STACK_H 1
59 #include <bits/concept_check.h>
60 #include <debug/debug.h>
61 #if __cplusplus >= 201103L
62 # include <bits/uses_allocator.h>
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 * @brief A standard container giving FILO behavior.
74 * @tparam _Tp Type of element.
75 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
77 * Meets many of the requirements of a
78 * <a href="tables.html#65">container</a>,
79 * but does not define anything to do with iterators. Very few of the
80 * other standard container interfaces are defined.
82 * This is not a true container, but an @e adaptor. It holds
83 * another container, and provides a wrapper interface to that
84 * container. The wrapper is what enforces strict
85 * first-in-last-out %stack behavior.
87 * The second template parameter defines the type of the underlying
88 * sequence/container. It defaults to std::deque, but it can be
89 * any type that supports @c back, @c push_back, and @c pop_front,
90 * such as std::list, std::vector, or an appropriate user-defined
93 * Members not found in @a normal containers are @c container_type,
94 * which is a typedef for the second Sequence parameter, and @c
95 * push, @c pop, and @c top, which are standard %stack/FILO
98 template<typename _Tp
, typename _Sequence
= deque
<_Tp
> >
101 // concept requirements
102 typedef typename
_Sequence::value_type _Sequence_value_type
;
103 #if __cplusplus < 201103L
104 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
105 __glibcxx_class_requires(_Sequence
, _BackInsertionSequenceConcept
)
107 __glibcxx_class_requires2(_Tp
, _Sequence_value_type
, _SameTypeConcept
)
109 template<typename _Tp1
, typename _Seq1
>
111 operator==(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
113 template<typename _Tp1
, typename _Seq1
>
115 operator<(const stack
<_Tp1
, _Seq1
>&, const stack
<_Tp1
, _Seq1
>&);
117 #if __cplusplus >= 201103L
118 template<typename _Alloc
>
119 using _Uses
= typename
120 enable_if
<uses_allocator
<_Sequence
, _Alloc
>::value
>::type
;
124 typedef typename
_Sequence::value_type value_type
;
125 typedef typename
_Sequence::reference reference
;
126 typedef typename
_Sequence::const_reference const_reference
;
127 typedef typename
_Sequence::size_type size_type
;
128 typedef _Sequence container_type
;
131 // See queue::c for notes on this name.
135 // XXX removed old def ctor, added def arg to this one to match 14882
137 * @brief Default constructor creates no elements.
139 #if __cplusplus < 201103L
141 stack(const _Sequence
& __c
= _Sequence())
145 stack(const _Sequence
& __c
)
149 stack(_Sequence
&& __c
= _Sequence())
150 : c(std::move(__c
)) { }
152 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
154 stack(const _Alloc
& __a
)
157 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
158 stack(const _Sequence
& __c
, const _Alloc
& __a
)
161 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
162 stack(_Sequence
&& __c
, const _Alloc
& __a
)
163 : c(std::move(__c
), __a
) { }
165 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
166 stack(const stack
& __q
, const _Alloc
& __a
)
169 template<typename _Alloc
, typename _Requires
= _Uses
<_Alloc
>>
170 stack(stack
&& __q
, const _Alloc
& __a
)
171 : c(std::move(__q
.c
), __a
) { }
175 * Returns true if the %stack is empty.
179 { return c
.empty(); }
181 /** Returns the number of elements in the %stack. */
187 * Returns a read/write reference to the data at the first
188 * element of the %stack.
193 __glibcxx_requires_nonempty();
198 * Returns a read-only (constant) reference to the data at the first
199 * element of the %stack.
204 __glibcxx_requires_nonempty();
209 * @brief Add data to the top of the %stack.
210 * @param __x Data to be added.
212 * This is a typical %stack operation. The function creates an
213 * element at the top of the %stack and assigns the given data
214 * to it. The time complexity of the operation depends on the
215 * underlying sequence.
218 push(const value_type
& __x
)
219 { c
.push_back(__x
); }
221 #if __cplusplus >= 201103L
223 push(value_type
&& __x
)
224 { c
.push_back(std::move(__x
)); }
226 #if __cplusplus > 201402L
227 template<typename
... _Args
>
229 emplace(_Args
&&... __args
)
230 { return c
.emplace_back(std::forward
<_Args
>(__args
)...); }
232 template<typename
... _Args
>
234 emplace(_Args
&&... __args
)
235 { c
.emplace_back(std::forward
<_Args
>(__args
)...); }
240 * @brief Removes first element.
242 * This is a typical %stack operation. It shrinks the %stack
243 * by one. The time complexity of the operation depends on the
244 * underlying sequence.
246 * Note that no data is returned, and if the first element's
247 * data is needed, it should be retrieved before pop() is
253 __glibcxx_requires_nonempty();
257 #if __cplusplus >= 201103L
260 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
261 noexcept(__is_nothrow_swappable
<_Sequence
>::value
)
263 noexcept(__is_nothrow_swappable
<_Tp
>::value
)
269 #endif // __cplusplus >= 201103L
273 * @brief Stack equality comparison.
274 * @param __x A %stack.
275 * @param __y A %stack of the same type as @a __x.
276 * @return True iff the size and elements of the stacks are equal.
278 * This is an equivalence relation. Complexity and semantics
279 * depend on the underlying sequence type, but the expected rules
280 * are: this relation is linear in the size of the sequences, and
281 * stacks are considered equivalent if their sequences compare
284 template<typename _Tp
, typename _Seq
>
286 operator==(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
287 { return __x
.c
== __y
.c
; }
290 * @brief Stack ordering relation.
291 * @param __x A %stack.
292 * @param __y A %stack of the same type as @a x.
293 * @return True iff @a x is lexicographically less than @a __y.
295 * This is an total ordering relation. Complexity and semantics
296 * depend on the underlying sequence type, but the expected rules
297 * are: this relation is linear in the size of the sequences, the
298 * elements must be comparable with @c <, and
299 * std::lexicographical_compare() is usually used to make the
302 template<typename _Tp
, typename _Seq
>
304 operator<(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
305 { return __x
.c
< __y
.c
; }
307 /// Based on operator==
308 template<typename _Tp
, typename _Seq
>
310 operator!=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
311 { return !(__x
== __y
); }
313 /// Based on operator<
314 template<typename _Tp
, typename _Seq
>
316 operator>(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
317 { return __y
< __x
; }
319 /// Based on operator<
320 template<typename _Tp
, typename _Seq
>
322 operator<=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
323 { return !(__y
< __x
); }
325 /// Based on operator<
326 template<typename _Tp
, typename _Seq
>
328 operator>=(const stack
<_Tp
, _Seq
>& __x
, const stack
<_Tp
, _Seq
>& __y
)
329 { return !(__x
< __y
); }
331 #if __cplusplus >= 201103L
332 template<typename _Tp
, typename _Seq
>
334 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
335 // Constrained free swap overload, see p0185r1
336 typename enable_if
<__is_swappable
<_Seq
>::value
>::type
340 swap(stack
<_Tp
, _Seq
>& __x
, stack
<_Tp
, _Seq
>& __y
)
341 noexcept(noexcept(__x
.swap(__y
)))
344 template<typename _Tp
, typename _Seq
, typename _Alloc
>
345 struct uses_allocator
<stack
<_Tp
, _Seq
>, _Alloc
>
346 : public uses_allocator
<_Seq
, _Alloc
>::type
{ };
347 #endif // __cplusplus >= 201103L
349 _GLIBCXX_END_NAMESPACE_VERSION
352 #endif /* _STL_STACK_H */