re PR libstdc++/44015 (template parameters not documented)
[gcc.git] / libstdc++-v3 / include / bits / stl_stack.h
1 // Stack implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 // 2010, 2011, 2012
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
12
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.
17
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.
21
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/>.
26
27 /*
28 *
29 * Copyright (c) 1994
30 * Hewlett-Packard Company
31 *
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.
39 *
40 *
41 * Copyright (c) 1996,1997
42 * Silicon Graphics Computer Systems, Inc.
43 *
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.
51 */
52
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}
56 */
57
58 #ifndef _STL_STACK_H
59 #define _STL_STACK_H 1
60
61 #include <bits/concept_check.h>
62 #include <debug/debug.h>
63
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67
68 /**
69 * @brief A standard container giving FILO behavior.
70 *
71 * @ingroup sequences
72 *
73 * @tparam _Tp Type of element.
74 * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
75 *
76 * Meets many of the requirements of a
77 * <a href="tables.html#65">container</a>,
78 * but does not define anything to do with iterators. Very few of the
79 * other standard container interfaces are defined.
80 *
81 * This is not a true container, but an @e adaptor. It holds
82 * another container, and provides a wrapper interface to that
83 * container. The wrapper is what enforces strict
84 * first-in-last-out %stack behavior.
85 *
86 * The second template parameter defines the type of the underlying
87 * sequence/container. It defaults to std::deque, but it can be
88 * any type that supports @c back, @c push_back, and @c pop_front,
89 * such as std::list, std::vector, or an appropriate user-defined
90 * type.
91 *
92 * Members not found in @a normal containers are @c container_type,
93 * which is a typedef for the second Sequence parameter, and @c
94 * push, @c pop, and @c top, which are standard %stack/FILO
95 * operations.
96 */
97 template<typename _Tp, typename _Sequence = deque<_Tp> >
98 class stack
99 {
100 // concept requirements
101 typedef typename _Sequence::value_type _Sequence_value_type;
102 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
103 __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
104 __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
105
106 template<typename _Tp1, typename _Seq1>
107 friend bool
108 operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
109
110 template<typename _Tp1, typename _Seq1>
111 friend bool
112 operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
113
114 public:
115 typedef typename _Sequence::value_type value_type;
116 typedef typename _Sequence::reference reference;
117 typedef typename _Sequence::const_reference const_reference;
118 typedef typename _Sequence::size_type size_type;
119 typedef _Sequence container_type;
120
121 protected:
122 // See queue::c for notes on this name.
123 _Sequence c;
124
125 public:
126 // XXX removed old def ctor, added def arg to this one to match 14882
127 /**
128 * @brief Default constructor creates no elements.
129 */
130 #ifndef __GXX_EXPERIMENTAL_CXX0X__
131 explicit
132 stack(const _Sequence& __c = _Sequence())
133 : c(__c) { }
134 #else
135 explicit
136 stack(const _Sequence& __c)
137 : c(__c) { }
138
139 explicit
140 stack(_Sequence&& __c = _Sequence())
141 : c(std::move(__c)) { }
142 #endif
143
144 /**
145 * Returns true if the %stack is empty.
146 */
147 bool
148 empty() const
149 { return c.empty(); }
150
151 /** Returns the number of elements in the %stack. */
152 size_type
153 size() const
154 { return c.size(); }
155
156 /**
157 * Returns a read/write reference to the data at the first
158 * element of the %stack.
159 */
160 reference
161 top()
162 {
163 __glibcxx_requires_nonempty();
164 return c.back();
165 }
166
167 /**
168 * Returns a read-only (constant) reference to the data at the first
169 * element of the %stack.
170 */
171 const_reference
172 top() const
173 {
174 __glibcxx_requires_nonempty();
175 return c.back();
176 }
177
178 /**
179 * @brief Add data to the top of the %stack.
180 * @param __x Data to be added.
181 *
182 * This is a typical %stack operation. The function creates an
183 * element at the top of the %stack and assigns the given data
184 * to it. The time complexity of the operation depends on the
185 * underlying sequence.
186 */
187 void
188 push(const value_type& __x)
189 { c.push_back(__x); }
190
191 #ifdef __GXX_EXPERIMENTAL_CXX0X__
192 void
193 push(value_type&& __x)
194 { c.push_back(std::move(__x)); }
195
196 template<typename... _Args>
197 void
198 emplace(_Args&&... __args)
199 { c.emplace_back(std::forward<_Args>(__args)...); }
200 #endif
201
202 /**
203 * @brief Removes first element.
204 *
205 * This is a typical %stack operation. It shrinks the %stack
206 * by one. The time complexity of the operation depends on the
207 * underlying sequence.
208 *
209 * Note that no data is returned, and if the first element's
210 * data is needed, it should be retrieved before pop() is
211 * called.
212 */
213 void
214 pop()
215 {
216 __glibcxx_requires_nonempty();
217 c.pop_back();
218 }
219
220 #ifdef __GXX_EXPERIMENTAL_CXX0X__
221 void
222 swap(stack& __s)
223 noexcept(noexcept(swap(c, __s.c)))
224 {
225 using std::swap;
226 swap(c, __s.c);
227 }
228 #endif
229 };
230
231 /**
232 * @brief Stack equality comparison.
233 * @param __x A %stack.
234 * @param __y A %stack of the same type as @a __x.
235 * @return True iff the size and elements of the stacks are equal.
236 *
237 * This is an equivalence relation. Complexity and semantics
238 * depend on the underlying sequence type, but the expected rules
239 * are: this relation is linear in the size of the sequences, and
240 * stacks are considered equivalent if their sequences compare
241 * equal.
242 */
243 template<typename _Tp, typename _Seq>
244 inline bool
245 operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
246 { return __x.c == __y.c; }
247
248 /**
249 * @brief Stack ordering relation.
250 * @param __x A %stack.
251 * @param __y A %stack of the same type as @a x.
252 * @return True iff @a x is lexicographically less than @a __y.
253 *
254 * This is an total ordering relation. Complexity and semantics
255 * depend on the underlying sequence type, but the expected rules
256 * are: this relation is linear in the size of the sequences, the
257 * elements must be comparable with @c <, and
258 * std::lexicographical_compare() is usually used to make the
259 * determination.
260 */
261 template<typename _Tp, typename _Seq>
262 inline bool
263 operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
264 { return __x.c < __y.c; }
265
266 /// Based on operator==
267 template<typename _Tp, typename _Seq>
268 inline bool
269 operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
270 { return !(__x == __y); }
271
272 /// Based on operator<
273 template<typename _Tp, typename _Seq>
274 inline bool
275 operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
276 { return __y < __x; }
277
278 /// Based on operator<
279 template<typename _Tp, typename _Seq>
280 inline bool
281 operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
282 { return !(__y < __x); }
283
284 /// Based on operator<
285 template<typename _Tp, typename _Seq>
286 inline bool
287 operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
288 { return !(__x < __y); }
289
290 #ifdef __GXX_EXPERIMENTAL_CXX0X__
291 template<typename _Tp, typename _Seq>
292 inline void
293 swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
294 noexcept(noexcept(__x.swap(__y)))
295 { __x.swap(__y); }
296
297 template<typename _Tp, typename _Seq, typename _Alloc>
298 struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
299 : public uses_allocator<_Seq, _Alloc>::type { };
300 #endif
301
302 _GLIBCXX_END_NAMESPACE_VERSION
303 } // namespace
304
305 #endif /* _STL_STACK_H */