stack revision 1.1 1 1.1 joerg // -*- C++ -*-
2 1.1 joerg //===---------------------------- stack -----------------------------------===//
3 1.1 joerg //
4 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 1.1 joerg // See https://llvm.org/LICENSE.txt for license information.
6 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 1.1 joerg //
8 1.1 joerg //===----------------------------------------------------------------------===//
9 1.1 joerg
10 1.1 joerg #ifndef _LIBCPP_STACK
11 1.1 joerg #define _LIBCPP_STACK
12 1.1 joerg
13 1.1 joerg /*
14 1.1 joerg stack synopsis
15 1.1 joerg
16 1.1 joerg namespace std
17 1.1 joerg {
18 1.1 joerg
19 1.1 joerg template <class T, class Container = deque<T>>
20 1.1 joerg class stack
21 1.1 joerg {
22 1.1 joerg public:
23 1.1 joerg typedef Container container_type;
24 1.1 joerg typedef typename container_type::value_type value_type;
25 1.1 joerg typedef typename container_type::reference reference;
26 1.1 joerg typedef typename container_type::const_reference const_reference;
27 1.1 joerg typedef typename container_type::size_type size_type;
28 1.1 joerg
29 1.1 joerg protected:
30 1.1 joerg container_type c;
31 1.1 joerg
32 1.1 joerg public:
33 1.1 joerg stack() = default;
34 1.1 joerg ~stack() = default;
35 1.1 joerg
36 1.1 joerg stack(const stack& q) = default;
37 1.1 joerg stack(stack&& q) = default;
38 1.1 joerg
39 1.1 joerg stack& operator=(const stack& q) = default;
40 1.1 joerg stack& operator=(stack&& q) = default;
41 1.1 joerg
42 1.1 joerg explicit stack(const container_type& c);
43 1.1 joerg explicit stack(container_type&& c);
44 1.1 joerg template <class Alloc> explicit stack(const Alloc& a);
45 1.1 joerg template <class Alloc> stack(const container_type& c, const Alloc& a);
46 1.1 joerg template <class Alloc> stack(container_type&& c, const Alloc& a);
47 1.1 joerg template <class Alloc> stack(const stack& c, const Alloc& a);
48 1.1 joerg template <class Alloc> stack(stack&& c, const Alloc& a);
49 1.1 joerg
50 1.1 joerg bool empty() const;
51 1.1 joerg size_type size() const;
52 1.1 joerg reference top();
53 1.1 joerg const_reference top() const;
54 1.1 joerg
55 1.1 joerg void push(const value_type& x);
56 1.1 joerg void push(value_type&& x);
57 1.1 joerg template <class... Args> reference emplace(Args&&... args); // reference in C++17
58 1.1 joerg void pop();
59 1.1 joerg
60 1.1 joerg void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
61 1.1 joerg };
62 1.1 joerg
63 1.1 joerg template<class Container>
64 1.1 joerg stack(Container) -> stack<typename Container::value_type, Container>; // C++17
65 1.1 joerg
66 1.1 joerg template<class Container, class Allocator>
67 1.1 joerg stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
68 1.1 joerg
69 1.1 joerg template <class T, class Container>
70 1.1 joerg bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
71 1.1 joerg template <class T, class Container>
72 1.1 joerg bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
73 1.1 joerg template <class T, class Container>
74 1.1 joerg bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
75 1.1 joerg template <class T, class Container>
76 1.1 joerg bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
77 1.1 joerg template <class T, class Container>
78 1.1 joerg bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
79 1.1 joerg template <class T, class Container>
80 1.1 joerg bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
81 1.1 joerg
82 1.1 joerg template <class T, class Container>
83 1.1 joerg void swap(stack<T, Container>& x, stack<T, Container>& y)
84 1.1 joerg noexcept(noexcept(x.swap(y)));
85 1.1 joerg
86 1.1 joerg } // std
87 1.1 joerg
88 1.1 joerg */
89 1.1 joerg
90 1.1 joerg #include <__config>
91 1.1 joerg #include <deque>
92 1.1 joerg
93 1.1 joerg #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94 1.1 joerg #pragma GCC system_header
95 1.1 joerg #endif
96 1.1 joerg
97 1.1 joerg _LIBCPP_BEGIN_NAMESPACE_STD
98 1.1 joerg
99 1.1 joerg template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
100 1.1 joerg
101 1.1 joerg template <class _Tp, class _Container>
102 1.1 joerg _LIBCPP_INLINE_VISIBILITY
103 1.1 joerg bool
104 1.1 joerg operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
105 1.1 joerg
106 1.1 joerg template <class _Tp, class _Container>
107 1.1 joerg _LIBCPP_INLINE_VISIBILITY
108 1.1 joerg bool
109 1.1 joerg operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
110 1.1 joerg
111 1.1 joerg template <class _Tp, class _Container /*= deque<_Tp>*/>
112 1.1 joerg class _LIBCPP_TEMPLATE_VIS stack
113 1.1 joerg {
114 1.1 joerg public:
115 1.1 joerg typedef _Container container_type;
116 1.1 joerg typedef typename container_type::value_type value_type;
117 1.1 joerg typedef typename container_type::reference reference;
118 1.1 joerg typedef typename container_type::const_reference const_reference;
119 1.1 joerg typedef typename container_type::size_type size_type;
120 1.1 joerg static_assert((is_same<_Tp, value_type>::value), "" );
121 1.1 joerg
122 1.1 joerg protected:
123 1.1 joerg container_type c;
124 1.1 joerg
125 1.1 joerg public:
126 1.1 joerg _LIBCPP_INLINE_VISIBILITY
127 1.1 joerg stack()
128 1.1 joerg _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
129 1.1 joerg : c() {}
130 1.1 joerg
131 1.1 joerg _LIBCPP_INLINE_VISIBILITY
132 1.1 joerg stack(const stack& __q) : c(__q.c) {}
133 1.1 joerg
134 1.1 joerg _LIBCPP_INLINE_VISIBILITY
135 1.1 joerg stack& operator=(const stack& __q) {c = __q.c; return *this;}
136 1.1 joerg
137 1.1 joerg
138 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
139 1.1 joerg _LIBCPP_INLINE_VISIBILITY
140 1.1 joerg stack(stack&& __q)
141 1.1 joerg _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
142 1.1 joerg : c(_VSTD::move(__q.c)) {}
143 1.1 joerg
144 1.1 joerg _LIBCPP_INLINE_VISIBILITY
145 1.1 joerg stack& operator=(stack&& __q)
146 1.1 joerg _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
147 1.1 joerg {c = _VSTD::move(__q.c); return *this;}
148 1.1 joerg
149 1.1 joerg _LIBCPP_INLINE_VISIBILITY
150 1.1 joerg explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
151 1.1 joerg #endif // _LIBCPP_CXX03_LANG
152 1.1 joerg
153 1.1 joerg _LIBCPP_INLINE_VISIBILITY
154 1.1 joerg explicit stack(const container_type& __c) : c(__c) {}
155 1.1 joerg
156 1.1 joerg template <class _Alloc>
157 1.1 joerg _LIBCPP_INLINE_VISIBILITY
158 1.1 joerg explicit stack(const _Alloc& __a,
159 1.1 joerg _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
160 1.1 joerg : c(__a) {}
161 1.1 joerg template <class _Alloc>
162 1.1 joerg _LIBCPP_INLINE_VISIBILITY
163 1.1 joerg stack(const container_type& __c, const _Alloc& __a,
164 1.1 joerg _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
165 1.1 joerg : c(__c, __a) {}
166 1.1 joerg template <class _Alloc>
167 1.1 joerg _LIBCPP_INLINE_VISIBILITY
168 1.1 joerg stack(const stack& __s, const _Alloc& __a,
169 1.1 joerg _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
170 1.1 joerg : c(__s.c, __a) {}
171 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
172 1.1 joerg template <class _Alloc>
173 1.1 joerg _LIBCPP_INLINE_VISIBILITY
174 1.1 joerg stack(container_type&& __c, const _Alloc& __a,
175 1.1 joerg _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
176 1.1 joerg : c(_VSTD::move(__c), __a) {}
177 1.1 joerg template <class _Alloc>
178 1.1 joerg _LIBCPP_INLINE_VISIBILITY
179 1.1 joerg stack(stack&& __s, const _Alloc& __a,
180 1.1 joerg _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
181 1.1 joerg : c(_VSTD::move(__s.c), __a) {}
182 1.1 joerg #endif // _LIBCPP_CXX03_LANG
183 1.1 joerg
184 1.1 joerg _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
185 1.1 joerg bool empty() const {return c.empty();}
186 1.1 joerg _LIBCPP_INLINE_VISIBILITY
187 1.1 joerg size_type size() const {return c.size();}
188 1.1 joerg _LIBCPP_INLINE_VISIBILITY
189 1.1 joerg reference top() {return c.back();}
190 1.1 joerg _LIBCPP_INLINE_VISIBILITY
191 1.1 joerg const_reference top() const {return c.back();}
192 1.1 joerg
193 1.1 joerg _LIBCPP_INLINE_VISIBILITY
194 1.1 joerg void push(const value_type& __v) {c.push_back(__v);}
195 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
196 1.1 joerg _LIBCPP_INLINE_VISIBILITY
197 1.1 joerg void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
198 1.1 joerg
199 1.1 joerg template <class... _Args>
200 1.1 joerg _LIBCPP_INLINE_VISIBILITY
201 1.1 joerg #if _LIBCPP_STD_VER > 14
202 1.1 joerg decltype(auto) emplace(_Args&&... __args)
203 1.1 joerg { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
204 1.1 joerg #else
205 1.1 joerg void emplace(_Args&&... __args)
206 1.1 joerg { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
207 1.1 joerg #endif
208 1.1 joerg #endif // _LIBCPP_CXX03_LANG
209 1.1 joerg
210 1.1 joerg _LIBCPP_INLINE_VISIBILITY
211 1.1 joerg void pop() {c.pop_back();}
212 1.1 joerg
213 1.1 joerg _LIBCPP_INLINE_VISIBILITY
214 1.1 joerg void swap(stack& __s)
215 1.1 joerg _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
216 1.1 joerg {
217 1.1 joerg using _VSTD::swap;
218 1.1 joerg swap(c, __s.c);
219 1.1 joerg }
220 1.1 joerg
221 1.1 joerg template <class T1, class _C1>
222 1.1 joerg friend
223 1.1 joerg bool
224 1.1 joerg operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
225 1.1 joerg
226 1.1 joerg template <class T1, class _C1>
227 1.1 joerg friend
228 1.1 joerg bool
229 1.1 joerg operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
230 1.1 joerg };
231 1.1 joerg
232 1.1 joerg #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
233 1.1 joerg template<class _Container,
234 1.1 joerg class = _EnableIf<!__is_allocator<_Container>::value>
235 1.1 joerg >
236 1.1 joerg stack(_Container)
237 1.1 joerg -> stack<typename _Container::value_type, _Container>;
238 1.1 joerg
239 1.1 joerg template<class _Container,
240 1.1 joerg class _Alloc,
241 1.1 joerg class = _EnableIf<!__is_allocator<_Container>::value>,
242 1.1 joerg class = _EnableIf<__is_allocator<_Alloc>::value>
243 1.1 joerg >
244 1.1 joerg stack(_Container, _Alloc)
245 1.1 joerg -> stack<typename _Container::value_type, _Container>;
246 1.1 joerg #endif
247 1.1 joerg
248 1.1 joerg template <class _Tp, class _Container>
249 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
250 1.1 joerg bool
251 1.1 joerg operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
252 1.1 joerg {
253 1.1 joerg return __x.c == __y.c;
254 1.1 joerg }
255 1.1 joerg
256 1.1 joerg template <class _Tp, class _Container>
257 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
258 1.1 joerg bool
259 1.1 joerg operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
260 1.1 joerg {
261 1.1 joerg return __x.c < __y.c;
262 1.1 joerg }
263 1.1 joerg
264 1.1 joerg template <class _Tp, class _Container>
265 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
266 1.1 joerg bool
267 1.1 joerg operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
268 1.1 joerg {
269 1.1 joerg return !(__x == __y);
270 1.1 joerg }
271 1.1 joerg
272 1.1 joerg template <class _Tp, class _Container>
273 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
274 1.1 joerg bool
275 1.1 joerg operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
276 1.1 joerg {
277 1.1 joerg return __y < __x;
278 1.1 joerg }
279 1.1 joerg
280 1.1 joerg template <class _Tp, class _Container>
281 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
282 1.1 joerg bool
283 1.1 joerg operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
284 1.1 joerg {
285 1.1 joerg return !(__x < __y);
286 1.1 joerg }
287 1.1 joerg
288 1.1 joerg template <class _Tp, class _Container>
289 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
290 1.1 joerg bool
291 1.1 joerg operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
292 1.1 joerg {
293 1.1 joerg return !(__y < __x);
294 1.1 joerg }
295 1.1 joerg
296 1.1 joerg template <class _Tp, class _Container>
297 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
298 1.1 joerg _EnableIf<__is_swappable<_Container>::value, void>
299 1.1 joerg swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
300 1.1 joerg _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
301 1.1 joerg {
302 1.1 joerg __x.swap(__y);
303 1.1 joerg }
304 1.1 joerg
305 1.1 joerg template <class _Tp, class _Container, class _Alloc>
306 1.1 joerg struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
307 1.1 joerg : public uses_allocator<_Container, _Alloc>
308 1.1 joerg {
309 1.1 joerg };
310 1.1 joerg
311 1.1 joerg _LIBCPP_END_NAMESPACE_STD
312 1.1 joerg
313 1.1 joerg #endif // _LIBCPP_STACK
314