Line data Source code
1 : // Stack implementation -*- C++ -*-
2 :
3 : // Copyright (C) 2001-2019 Free Software Foundation, Inc.
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
8 : // Free Software Foundation; either version 3, or (at your option)
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 :
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.
19 :
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/>.
24 :
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 :
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}
54 : */
55 :
56 : #ifndef _STL_STACK_H
57 : #define _STL_STACK_H 1
58 :
59 : #include <bits/concept_check.h>
60 : #include <debug/debug.h>
61 : #if __cplusplus >= 201103L
62 : # include <bits/uses_allocator.h>
63 : #endif
64 :
65 : namespace std _GLIBCXX_VISIBILITY(default)
66 : {
67 : _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 :
69 : /**
70 : * @brief A standard container giving FILO behavior.
71 : *
72 : * @ingroup sequences
73 : *
74 : * @tparam _Tp Type of element.
75 : * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>.
76 : *
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 : *
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.
86 : *
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_back,
90 : * such as std::list, std::vector, or an appropriate user-defined
91 : * type.
92 : *
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
96 : * operations.
97 : */
98 : template<typename _Tp, typename _Sequence = deque<_Tp> >
99 239 : class stack
100 : {
101 : #ifdef _GLIBCXX_CONCEPT_CHECKS
102 : // concept requirements
103 : typedef typename _Sequence::value_type _Sequence_value_type;
104 : # if __cplusplus < 201103L
105 : __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
106 : __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
107 : # endif
108 : __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
109 : #endif
110 :
111 : template<typename _Tp1, typename _Seq1>
112 : friend bool
113 : operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
114 :
115 : template<typename _Tp1, typename _Seq1>
116 : friend bool
117 : operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
118 :
119 : #if __cplusplus >= 201103L
120 : template<typename _Alloc>
121 : using _Uses = typename
122 : enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
123 :
124 : #if __cplusplus >= 201703L
125 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
126 : // 2566. Requirements on the first template parameter of container
127 : // adaptors
128 : static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
129 : "value_type must be the same as the underlying container");
130 : #endif // C++17
131 : #endif // C++11
132 :
133 : public:
134 : typedef typename _Sequence::value_type value_type;
135 : typedef typename _Sequence::reference reference;
136 : typedef typename _Sequence::const_reference const_reference;
137 : typedef typename _Sequence::size_type size_type;
138 : typedef _Sequence container_type;
139 :
140 : protected:
141 : // See queue::c for notes on this name.
142 : _Sequence c;
143 :
144 : public:
145 : // XXX removed old def ctor, added def arg to this one to match 14882
146 : /**
147 : * @brief Default constructor creates no elements.
148 : */
149 : #if __cplusplus < 201103L
150 : explicit
151 : stack(const _Sequence& __c = _Sequence())
152 : : c(__c) { }
153 : #else
154 : template<typename _Seq = _Sequence, typename _Requires = typename
155 : enable_if<is_default_constructible<_Seq>::value>::type>
156 955 : stack()
157 955 : : c() { }
158 :
159 : explicit
160 : stack(const _Sequence& __c)
161 : : c(__c) { }
162 :
163 : explicit
164 : stack(_Sequence&& __c)
165 : : c(std::move(__c)) { }
166 :
167 : template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
168 : explicit
169 : stack(const _Alloc& __a)
170 : : c(__a) { }
171 :
172 : template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
173 : stack(const _Sequence& __c, const _Alloc& __a)
174 : : c(__c, __a) { }
175 :
176 : template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
177 : stack(_Sequence&& __c, const _Alloc& __a)
178 : : c(std::move(__c), __a) { }
179 :
180 : template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
181 : stack(const stack& __q, const _Alloc& __a)
182 : : c(__q.c, __a) { }
183 :
184 : template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
185 : stack(stack&& __q, const _Alloc& __a)
186 : : c(std::move(__q.c), __a) { }
187 : #endif
188 :
189 : /**
190 : * Returns true if the %stack is empty.
191 : */
192 : _GLIBCXX_NODISCARD bool
193 95151 : empty() const
194 95151 : { return c.empty(); }
195 :
196 : /** Returns the number of elements in the %stack. */
197 : size_type
198 0 : size() const
199 0 : { return c.size(); }
200 :
201 : /**
202 : * Returns a read/write reference to the data at the first
203 : * element of the %stack.
204 : */
205 : reference
206 105656 : top()
207 : {
208 : __glibcxx_requires_nonempty();
209 105656 : return c.back();
210 : }
211 :
212 : /**
213 : * Returns a read-only (constant) reference to the data at the first
214 : * element of the %stack.
215 : */
216 : const_reference
217 0 : top() const
218 : {
219 : __glibcxx_requires_nonempty();
220 0 : return c.back();
221 : }
222 :
223 : /**
224 : * @brief Add data to the top of the %stack.
225 : * @param __x Data to be added.
226 : *
227 : * This is a typical %stack operation. The function creates an
228 : * element at the top of the %stack and assigns the given data
229 : * to it. The time complexity of the operation depends on the
230 : * underlying sequence.
231 : */
232 : void
233 4775 : push(const value_type& __x)
234 4775 : { c.push_back(__x); }
235 :
236 : #if __cplusplus >= 201103L
237 : void
238 5767 : push(value_type&& __x)
239 5804 : { c.push_back(std::move(__x)); }
240 :
241 : #if __cplusplus > 201402L
242 : template<typename... _Args>
243 : decltype(auto)
244 : emplace(_Args&&... __args)
245 : { return c.emplace_back(std::forward<_Args>(__args)...); }
246 : #else
247 : template<typename... _Args>
248 : void
249 : emplace(_Args&&... __args)
250 : { c.emplace_back(std::forward<_Args>(__args)...); }
251 : #endif
252 : #endif
253 :
254 : /**
255 : * @brief Removes first element.
256 : *
257 : * This is a typical %stack operation. It shrinks the %stack
258 : * by one. The time complexity of the operation depends on the
259 : * underlying sequence.
260 : *
261 : * Note that no data is returned, and if the first element's
262 : * data is needed, it should be retrieved before pop() is
263 : * called.
264 : */
265 : void
266 10542 : pop()
267 : {
268 : __glibcxx_requires_nonempty();
269 10542 : c.pop_back();
270 0 : }
271 :
272 : #if __cplusplus >= 201103L
273 : void
274 : swap(stack& __s)
275 : #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
276 : noexcept(__is_nothrow_swappable<_Sequence>::value)
277 : #else
278 : noexcept(__is_nothrow_swappable<_Tp>::value)
279 : #endif
280 : {
281 : using std::swap;
282 : swap(c, __s.c);
283 : }
284 : #endif // __cplusplus >= 201103L
285 : };
286 :
287 : #if __cpp_deduction_guides >= 201606
288 : template<typename _Container,
289 : typename = _RequireNotAllocator<_Container>>
290 : stack(_Container) -> stack<typename _Container::value_type, _Container>;
291 :
292 : template<typename _Container, typename _Allocator,
293 : typename = _RequireNotAllocator<_Container>,
294 : typename = _RequireAllocator<_Allocator>>
295 : stack(_Container, _Allocator)
296 : -> stack<typename _Container::value_type, _Container>;
297 : #endif
298 :
299 : /**
300 : * @brief Stack equality comparison.
301 : * @param __x A %stack.
302 : * @param __y A %stack of the same type as @a __x.
303 : * @return True iff the size and elements of the stacks are equal.
304 : *
305 : * This is an equivalence relation. Complexity and semantics
306 : * depend on the underlying sequence type, but the expected rules
307 : * are: this relation is linear in the size of the sequences, and
308 : * stacks are considered equivalent if their sequences compare
309 : * equal.
310 : */
311 : template<typename _Tp, typename _Seq>
312 : inline bool
313 : operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
314 : { return __x.c == __y.c; }
315 :
316 : /**
317 : * @brief Stack ordering relation.
318 : * @param __x A %stack.
319 : * @param __y A %stack of the same type as @a x.
320 : * @return True iff @a x is lexicographically less than @a __y.
321 : *
322 : * This is an total ordering relation. Complexity and semantics
323 : * depend on the underlying sequence type, but the expected rules
324 : * are: this relation is linear in the size of the sequences, the
325 : * elements must be comparable with @c <, and
326 : * std::lexicographical_compare() is usually used to make the
327 : * determination.
328 : */
329 : template<typename _Tp, typename _Seq>
330 : inline bool
331 : operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
332 : { return __x.c < __y.c; }
333 :
334 : /// Based on operator==
335 : template<typename _Tp, typename _Seq>
336 : inline bool
337 : operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
338 : { return !(__x == __y); }
339 :
340 : /// Based on operator<
341 : template<typename _Tp, typename _Seq>
342 : inline bool
343 : operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
344 : { return __y < __x; }
345 :
346 : /// Based on operator<
347 : template<typename _Tp, typename _Seq>
348 : inline bool
349 : operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
350 : { return !(__y < __x); }
351 :
352 : /// Based on operator<
353 : template<typename _Tp, typename _Seq>
354 : inline bool
355 : operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
356 : { return !(__x < __y); }
357 :
358 : #if __cplusplus >= 201103L
359 : template<typename _Tp, typename _Seq>
360 : inline
361 : #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
362 : // Constrained free swap overload, see p0185r1
363 : typename enable_if<__is_swappable<_Seq>::value>::type
364 : #else
365 : void
366 : #endif
367 : swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
368 : noexcept(noexcept(__x.swap(__y)))
369 : { __x.swap(__y); }
370 :
371 : template<typename _Tp, typename _Seq, typename _Alloc>
372 : struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
373 : : public uses_allocator<_Seq, _Alloc>::type { };
374 : #endif // __cplusplus >= 201103L
375 :
376 : _GLIBCXX_END_NAMESPACE_VERSION
377 : } // namespace
378 :
379 : #endif /* _STL_STACK_H */
|