2 //===---------------------------- stack -----------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
19 template <class T, class Container = deque<T>>
23 typedef Container container_type;
24 typedef typename container_type::value_type value_type;
25 typedef typename container_type::reference reference;
26 typedef typename container_type::const_reference const_reference;
27 typedef typename container_type::size_type size_type;
36 stack(const stack& q) = default;
37 stack(stack&& q) = default;
39 stack& operator=(const stack& q) = default;
40 stack& operator=(stack&& q) = default;
42 explicit stack(const container_type& c);
43 explicit stack(container_type&& c);
44 template <class Alloc> explicit stack(const Alloc& a);
45 template <class Alloc> stack(const container_type& c, const Alloc& a);
46 template <class Alloc> stack(container_type&& c, const Alloc& a);
47 template <class Alloc> stack(const stack& c, const Alloc& a);
48 template <class Alloc> stack(stack&& c, const Alloc& a);
51 size_type size() const;
53 const_reference top() const;
55 void push(const value_type& x);
56 void push(value_type&& x);
57 template <class... Args> reference emplace(Args&&... args); // reference in C++17
60 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
63 template<class Container>
64 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
66 template<class Container, class Allocator>
67 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
69 template <class T, class Container>
70 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
71 template <class T, class Container>
72 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
73 template <class T, class Container>
74 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
75 template <class T, class Container>
76 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
77 template <class T, class Container>
78 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
79 template <class T, class Container>
80 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
82 template <class T, class Container>
83 void swap(stack<T, Container>& x, stack<T, Container>& y)
84 noexcept(noexcept(x.swap(y)));
93 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94 #pragma GCC system_header
97 _LIBCPP_BEGIN_NAMESPACE_STD
99 template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
101 template <class _Tp, class _Container>
102 _LIBCPP_INLINE_VISIBILITY
104 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
106 template <class _Tp, class _Container>
107 _LIBCPP_INLINE_VISIBILITY
109 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
111 template <class _Tp, class _Container /*= deque<_Tp>*/>
112 class _LIBCPP_TEMPLATE_VIS stack
115 typedef _Container container_type;
116 typedef typename container_type::value_type value_type;
117 typedef typename container_type::reference reference;
118 typedef typename container_type::const_reference const_reference;
119 typedef typename container_type::size_type size_type;
120 static_assert((is_same<_Tp, value_type>::value), "" );
126 _LIBCPP_INLINE_VISIBILITY
128 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
131 _LIBCPP_INLINE_VISIBILITY
132 stack(const stack& __q) : c(__q.c) {}
134 _LIBCPP_INLINE_VISIBILITY
135 stack& operator=(const stack& __q) {c = __q.c; return *this;}
138 #ifndef _LIBCPP_CXX03_LANG
139 _LIBCPP_INLINE_VISIBILITY
141 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
142 : c(_VSTD::move(__q.c)) {}
144 _LIBCPP_INLINE_VISIBILITY
145 stack& operator=(stack&& __q)
146 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
147 {c = _VSTD::move(__q.c); return *this;}
149 _LIBCPP_INLINE_VISIBILITY
150 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
151 #endif // _LIBCPP_CXX03_LANG
153 _LIBCPP_INLINE_VISIBILITY
154 explicit stack(const container_type& __c) : c(__c) {}
156 template <class _Alloc>
157 _LIBCPP_INLINE_VISIBILITY
158 explicit stack(const _Alloc& __a,
159 typename enable_if<uses_allocator<container_type,
160 _Alloc>::value>::type* = 0)
162 template <class _Alloc>
163 _LIBCPP_INLINE_VISIBILITY
164 stack(const container_type& __c, const _Alloc& __a,
165 typename enable_if<uses_allocator<container_type,
166 _Alloc>::value>::type* = 0)
168 template <class _Alloc>
169 _LIBCPP_INLINE_VISIBILITY
170 stack(const stack& __s, const _Alloc& __a,
171 typename enable_if<uses_allocator<container_type,
172 _Alloc>::value>::type* = 0)
174 #ifndef _LIBCPP_CXX03_LANG
175 template <class _Alloc>
176 _LIBCPP_INLINE_VISIBILITY
177 stack(container_type&& __c, const _Alloc& __a,
178 typename enable_if<uses_allocator<container_type,
179 _Alloc>::value>::type* = 0)
180 : c(_VSTD::move(__c), __a) {}
181 template <class _Alloc>
182 _LIBCPP_INLINE_VISIBILITY
183 stack(stack&& __s, const _Alloc& __a,
184 typename enable_if<uses_allocator<container_type,
185 _Alloc>::value>::type* = 0)
186 : c(_VSTD::move(__s.c), __a) {}
187 #endif // _LIBCPP_CXX03_LANG
189 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
190 bool empty() const {return c.empty();}
191 _LIBCPP_INLINE_VISIBILITY
192 size_type size() const {return c.size();}
193 _LIBCPP_INLINE_VISIBILITY
194 reference top() {return c.back();}
195 _LIBCPP_INLINE_VISIBILITY
196 const_reference top() const {return c.back();}
198 _LIBCPP_INLINE_VISIBILITY
199 void push(const value_type& __v) {c.push_back(__v);}
200 #ifndef _LIBCPP_CXX03_LANG
201 _LIBCPP_INLINE_VISIBILITY
202 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
204 template <class... _Args>
205 _LIBCPP_INLINE_VISIBILITY
206 #if _LIBCPP_STD_VER > 14
207 decltype(auto) emplace(_Args&&... __args)
208 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
210 void emplace(_Args&&... __args)
211 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
213 #endif // _LIBCPP_CXX03_LANG
215 _LIBCPP_INLINE_VISIBILITY
216 void pop() {c.pop_back();}
218 _LIBCPP_INLINE_VISIBILITY
219 void swap(stack& __s)
220 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
226 template <class T1, class _C1>
229 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
231 template <class T1, class _C1>
234 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
237 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
238 template<class _Container,
239 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
242 -> stack<typename _Container::value_type, _Container>;
244 template<class _Container,
246 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
247 class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
249 stack(_Container, _Alloc)
250 -> stack<typename _Container::value_type, _Container>;
253 template <class _Tp, class _Container>
254 inline _LIBCPP_INLINE_VISIBILITY
256 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
258 return __x.c == __y.c;
261 template <class _Tp, class _Container>
262 inline _LIBCPP_INLINE_VISIBILITY
264 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
266 return __x.c < __y.c;
269 template <class _Tp, class _Container>
270 inline _LIBCPP_INLINE_VISIBILITY
272 operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
274 return !(__x == __y);
277 template <class _Tp, class _Container>
278 inline _LIBCPP_INLINE_VISIBILITY
280 operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
285 template <class _Tp, class _Container>
286 inline _LIBCPP_INLINE_VISIBILITY
288 operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
293 template <class _Tp, class _Container>
294 inline _LIBCPP_INLINE_VISIBILITY
296 operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
301 template <class _Tp, class _Container>
302 inline _LIBCPP_INLINE_VISIBILITY
304 __is_swappable<_Container>::value,
307 swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
308 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
313 template <class _Tp, class _Container, class _Alloc>
314 struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
315 : public uses_allocator<_Container, _Alloc>
319 _LIBCPP_END_NAMESPACE_STD
321 #endif // _LIBCPP_STACK