]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Stack implementation -*- C++ -*- |
2 | ||
85ec4feb | 3 | // Copyright (C) 2001-2018 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
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. | |
15 | ||
748086b7 JJ |
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. | |
42526146 | 19 | |
748086b7 JJ |
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/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
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. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
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. | |
49 | */ | |
50 | ||
f910786b | 51 | /** @file bits/stl_stack.h |
729e3d3f | 52 | * This is an internal header file, included by other library headers. |
f910786b | 53 | * Do not attempt to use it directly. @headername{stack} |
725dc051 BK |
54 | */ |
55 | ||
046d30f4 PC |
56 | #ifndef _STL_STACK_H |
57 | #define _STL_STACK_H 1 | |
725dc051 | 58 | |
30a20a1e | 59 | #include <bits/concept_check.h> |
285b36d6 | 60 | #include <debug/debug.h> |
7666d649 JW |
61 | #if __cplusplus >= 201103L |
62 | # include <bits/uses_allocator.h> | |
63 | #endif | |
725dc051 | 64 | |
12ffa228 BK |
65 | namespace std _GLIBCXX_VISIBILITY(default) |
66 | { | |
67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 68 | |
224a45d0 | 69 | /** |
3971a4d2 PE |
70 | * @brief A standard container giving FILO behavior. |
71 | * | |
aac2878e | 72 | * @ingroup sequences |
3971a4d2 | 73 | * |
d632488a BK |
74 | * @tparam _Tp Type of element. |
75 | * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>. | |
76 | * | |
3971a4d2 PE |
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. | |
81 | * | |
285b36d6 BK |
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. | |
3971a4d2 PE |
86 | * |
87 | * The second template parameter defines the type of the underlying | |
285b36d6 | 88 | * sequence/container. It defaults to std::deque, but it can be |
4ab033db | 89 | * any type that supports @c back, @c push_back, and @c pop_back, |
285b36d6 BK |
90 | * such as std::list, std::vector, or an appropriate user-defined |
91 | * type. | |
3971a4d2 | 92 | * |
2a60a9f6 | 93 | * Members not found in @a normal containers are @c container_type, |
285b36d6 BK |
94 | * which is a typedef for the second Sequence parameter, and @c |
95 | * push, @c pop, and @c top, which are standard %stack/FILO | |
96 | * operations. | |
224a45d0 | 97 | */ |
7ffb61d5 | 98 | template<typename _Tp, typename _Sequence = deque<_Tp> > |
3971a4d2 | 99 | class stack |
285b36d6 | 100 | { |
fe62dd04 | 101 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
285b36d6 BK |
102 | // concept requirements |
103 | typedef typename _Sequence::value_type _Sequence_value_type; | |
fe62dd04 | 104 | # if __cplusplus < 201103L |
285b36d6 BK |
105 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
106 | __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) | |
fe62dd04 | 107 | # endif |
285b36d6 | 108 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) |
fe62dd04 | 109 | #endif |
ed6814f7 | 110 | |
15d72060 | 111 | template<typename _Tp1, typename _Seq1> |
fe62dd04 FD |
112 | friend bool |
113 | operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); | |
285b36d6 BK |
114 | |
115 | template<typename _Tp1, typename _Seq1> | |
fe62dd04 FD |
116 | friend bool |
117 | operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); | |
ed6814f7 | 118 | |
997ed914 JW |
119 | #if __cplusplus >= 201103L |
120 | template<typename _Alloc> | |
121 | using _Uses = typename | |
122 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; | |
123 | #endif | |
124 | ||
285b36d6 | 125 | public: |
fe62dd04 FD |
126 | typedef typename _Sequence::value_type value_type; |
127 | typedef typename _Sequence::reference reference; | |
128 | typedef typename _Sequence::const_reference const_reference; | |
129 | typedef typename _Sequence::size_type size_type; | |
130 | typedef _Sequence container_type; | |
ed6814f7 | 131 | |
285b36d6 BK |
132 | protected: |
133 | // See queue::c for notes on this name. | |
134 | _Sequence c; | |
ed6814f7 | 135 | |
285b36d6 BK |
136 | public: |
137 | // XXX removed old def ctor, added def arg to this one to match 14882 | |
138 | /** | |
139 | * @brief Default constructor creates no elements. | |
140 | */ | |
734f5023 | 141 | #if __cplusplus < 201103L |
285b36d6 | 142 | explicit |
15d72060 | 143 | stack(const _Sequence& __c = _Sequence()) |
d508327c | 144 | : c(__c) { } |
7aa1cb97 | 145 | #else |
a1f009a6 JW |
146 | template<typename _Seq = _Sequence, typename _Requires = typename |
147 | enable_if<is_default_constructible<_Seq>::value>::type> | |
148 | stack() | |
149 | : c() { } | |
d2e1d4b7 | 150 | |
7aa1cb97 PC |
151 | explicit |
152 | stack(const _Sequence& __c) | |
153 | : c(__c) { } | |
154 | ||
155 | explicit | |
d2e1d4b7 | 156 | stack(_Sequence&& __c) |
7aa1cb97 | 157 | : c(std::move(__c)) { } |
997ed914 JW |
158 | |
159 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
160 | explicit | |
161 | stack(const _Alloc& __a) | |
162 | : c(__a) { } | |
163 | ||
164 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
165 | stack(const _Sequence& __c, const _Alloc& __a) | |
166 | : c(__c, __a) { } | |
167 | ||
168 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
169 | stack(_Sequence&& __c, const _Alloc& __a) | |
170 | : c(std::move(__c), __a) { } | |
171 | ||
172 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
173 | stack(const stack& __q, const _Alloc& __a) | |
174 | : c(__q.c, __a) { } | |
175 | ||
176 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> | |
177 | stack(stack&& __q, const _Alloc& __a) | |
178 | : c(std::move(__q.c), __a) { } | |
7aa1cb97 | 179 | #endif |
ed6814f7 | 180 | |
285b36d6 BK |
181 | /** |
182 | * Returns true if the %stack is empty. | |
183 | */ | |
184 | bool | |
15d72060 PC |
185 | empty() const |
186 | { return c.empty(); } | |
ed6814f7 | 187 | |
285b36d6 BK |
188 | /** Returns the number of elements in the %stack. */ |
189 | size_type | |
15d72060 PC |
190 | size() const |
191 | { return c.size(); } | |
ed6814f7 | 192 | |
285b36d6 BK |
193 | /** |
194 | * Returns a read/write reference to the data at the first | |
195 | * element of the %stack. | |
196 | */ | |
197 | reference | |
ed6814f7 BI |
198 | top() |
199 | { | |
285b36d6 | 200 | __glibcxx_requires_nonempty(); |
ed6814f7 | 201 | return c.back(); |
285b36d6 | 202 | } |
ed6814f7 | 203 | |
285b36d6 BK |
204 | /** |
205 | * Returns a read-only (constant) reference to the data at the first | |
206 | * element of the %stack. | |
207 | */ | |
208 | const_reference | |
ed6814f7 | 209 | top() const |
285b36d6 BK |
210 | { |
211 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 212 | return c.back(); |
285b36d6 | 213 | } |
ed6814f7 | 214 | |
285b36d6 BK |
215 | /** |
216 | * @brief Add data to the top of the %stack. | |
93c66bc6 | 217 | * @param __x Data to be added. |
285b36d6 BK |
218 | * |
219 | * This is a typical %stack operation. The function creates an | |
220 | * element at the top of the %stack and assigns the given data | |
221 | * to it. The time complexity of the operation depends on the | |
222 | * underlying sequence. | |
223 | */ | |
224 | void | |
15d72060 PC |
225 | push(const value_type& __x) |
226 | { c.push_back(__x); } | |
4dc3e453 | 227 | |
734f5023 | 228 | #if __cplusplus >= 201103L |
4dc3e453 PC |
229 | void |
230 | push(value_type&& __x) | |
231 | { c.push_back(std::move(__x)); } | |
232 | ||
594ef205 JW |
233 | #if __cplusplus > 201402L |
234 | template<typename... _Args> | |
235 | decltype(auto) | |
236 | emplace(_Args&&... __args) | |
237 | { return c.emplace_back(std::forward<_Args>(__args)...); } | |
238 | #else | |
c54171fe | 239 | template<typename... _Args> |
fe62dd04 FD |
240 | void |
241 | emplace(_Args&&... __args) | |
4dc3e453 | 242 | { c.emplace_back(std::forward<_Args>(__args)...); } |
594ef205 | 243 | #endif |
7aa1cb97 PC |
244 | #endif |
245 | ||
285b36d6 BK |
246 | /** |
247 | * @brief Removes first element. | |
248 | * | |
249 | * This is a typical %stack operation. It shrinks the %stack | |
250 | * by one. The time complexity of the operation depends on the | |
251 | * underlying sequence. | |
252 | * | |
253 | * Note that no data is returned, and if the first element's | |
254 | * data is needed, it should be retrieved before pop() is | |
255 | * called. | |
256 | */ | |
257 | void | |
ed6814f7 | 258 | pop() |
285b36d6 BK |
259 | { |
260 | __glibcxx_requires_nonempty(); | |
ed6814f7 | 261 | c.pop_back(); |
285b36d6 | 262 | } |
7aa1cb97 | 263 | |
734f5023 | 264 | #if __cplusplus >= 201103L |
7aa1cb97 | 265 | void |
ff74fd13 | 266 | swap(stack& __s) |
6b9539e2 DK |
267 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
268 | noexcept(__is_nothrow_swappable<_Sequence>::value) | |
269 | #else | |
ddb63209 | 270 | noexcept(__is_nothrow_swappable<_Tp>::value) |
6b9539e2 | 271 | #endif |
999209d0 PC |
272 | { |
273 | using std::swap; | |
274 | swap(c, __s.c); | |
275 | } | |
6b9539e2 | 276 | #endif // __cplusplus >= 201103L |
285b36d6 | 277 | }; |
ed6814f7 | 278 | |
224a45d0 | 279 | /** |
3971a4d2 | 280 | * @brief Stack equality comparison. |
93c66bc6 BK |
281 | * @param __x A %stack. |
282 | * @param __y A %stack of the same type as @a __x. | |
3971a4d2 | 283 | * @return True iff the size and elements of the stacks are equal. |
224a45d0 | 284 | * |
285b36d6 BK |
285 | * This is an equivalence relation. Complexity and semantics |
286 | * depend on the underlying sequence type, but the expected rules | |
287 | * are: this relation is linear in the size of the sequences, and | |
288 | * stacks are considered equivalent if their sequences compare | |
289 | * equal. | |
224a45d0 | 290 | */ |
285b36d6 | 291 | template<typename _Tp, typename _Seq> |
3971a4d2 | 292 | inline bool |
15d72060 | 293 | operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 294 | { return __x.c == __y.c; } |
ed6814f7 | 295 | |
224a45d0 | 296 | /** |
3971a4d2 | 297 | * @brief Stack ordering relation. |
93c66bc6 BK |
298 | * @param __x A %stack. |
299 | * @param __y A %stack of the same type as @a x. | |
300 | * @return True iff @a x is lexicographically less than @a __y. | |
224a45d0 | 301 | * |
285b36d6 BK |
302 | * This is an total ordering relation. Complexity and semantics |
303 | * depend on the underlying sequence type, but the expected rules | |
304 | * are: this relation is linear in the size of the sequences, the | |
305 | * elements must be comparable with @c <, and | |
306 | * std::lexicographical_compare() is usually used to make the | |
3971a4d2 | 307 | * determination. |
224a45d0 | 308 | */ |
285b36d6 | 309 | template<typename _Tp, typename _Seq> |
3971a4d2 | 310 | inline bool |
15d72060 | 311 | operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 312 | { return __x.c < __y.c; } |
ed6814f7 | 313 | |
3971a4d2 | 314 | /// Based on operator== |
285b36d6 | 315 | template<typename _Tp, typename _Seq> |
3971a4d2 | 316 | inline bool |
15d72060 | 317 | operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 318 | { return !(__x == __y); } |
ed6814f7 | 319 | |
3971a4d2 | 320 | /// Based on operator< |
285b36d6 | 321 | template<typename _Tp, typename _Seq> |
3971a4d2 | 322 | inline bool |
15d72060 | 323 | operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 324 | { return __y < __x; } |
ed6814f7 | 325 | |
3971a4d2 | 326 | /// Based on operator< |
285b36d6 | 327 | template<typename _Tp, typename _Seq> |
3971a4d2 | 328 | inline bool |
15d72060 | 329 | operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 330 | { return !(__y < __x); } |
ed6814f7 | 331 | |
3971a4d2 | 332 | /// Based on operator< |
285b36d6 | 333 | template<typename _Tp, typename _Seq> |
3971a4d2 | 334 | inline bool |
15d72060 | 335 | operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
3971a4d2 | 336 | { return !(__x < __y); } |
3cbc7af0 | 337 | |
734f5023 | 338 | #if __cplusplus >= 201103L |
7aa1cb97 | 339 | template<typename _Tp, typename _Seq> |
6b9539e2 DK |
340 | inline |
341 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
342 | // Constrained free swap overload, see p0185r1 | |
343 | typename enable_if<__is_swappable<_Seq>::value>::type | |
344 | #else | |
345 | void | |
346 | #endif | |
7aa1cb97 | 347 | swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y) |
c688bbdd | 348 | noexcept(noexcept(__x.swap(__y))) |
7aa1cb97 | 349 | { __x.swap(__y); } |
aa2b7414 PC |
350 | |
351 | template<typename _Tp, typename _Seq, typename _Alloc> | |
352 | struct uses_allocator<stack<_Tp, _Seq>, _Alloc> | |
353 | : public uses_allocator<_Seq, _Alloc>::type { }; | |
6b9539e2 | 354 | #endif // __cplusplus >= 201103L |
7aa1cb97 | 355 | |
12ffa228 BK |
356 | _GLIBCXX_END_NAMESPACE_VERSION |
357 | } // namespace | |
725dc051 | 358 | |
046d30f4 | 359 | #endif /* _STL_STACK_H */ |