2 //===---------------------------- stack -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
20 template <class T, class Container = deque<T>>
24 typedef Container container_type;
25 typedef typename container_type::value_type value_type;
26 typedef typename container_type::reference reference;
27 typedef typename container_type::const_reference const_reference;
28 typedef typename container_type::size_type size_type;
37 stack(const stack& q) = default;
38 stack(stack&& q) = default;
40 stack& operator=(const stack& q) = default;
41 stack& operator=(stack&& q) = default;
43 explicit stack(const container_type& c);
44 explicit stack(container_type&& c);
45 template <class Alloc> explicit stack(const Alloc& a);
46 template <class Alloc> stack(const container_type& c, const Alloc& a);
47 template <class Alloc> stack(container_type&& c, const Alloc& a);
48 template <class Alloc> stack(const stack& c, const Alloc& a);
49 template <class Alloc> stack(stack&& c, const Alloc& a);
52 size_type size() const;
54 const_reference top() const;
56 void push(const value_type& x);
57 void push(value_type&& x);
58 template <class... Args> reference emplace(Args&&... args); // reference in C++17
61 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
64 template<class Container>
65 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
67 template<class Container, class Allocator>
68 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
70 template <class T, class Container>
71 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
72 template <class T, class Container>
73 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
74 template <class T, class Container>
75 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
76 template <class T, class Container>
77 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
78 template <class T, class Container>
79 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
80 template <class T, class Container>
81 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
83 template <class T, class Container>
84 void swap(stack<T, Container>& x, stack<T, Container>& y)
85 noexcept(noexcept(x.swap(y)));
94 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
95 #pragma GCC system_header
98 _LIBCPP_BEGIN_NAMESPACE_STD
100 template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
102 template <class _Tp, class _Container>
103 _LIBCPP_INLINE_VISIBILITY
105 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
107 template <class _Tp, class _Container>
108 _LIBCPP_INLINE_VISIBILITY
110 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
112 template <class _Tp, class _Container /*= deque<_Tp>*/>
113 class _LIBCPP_TEMPLATE_VIS stack
116 typedef _Container container_type;
117 typedef typename container_type::value_type value_type;
118 typedef typename container_type::reference reference;
119 typedef typename container_type::const_reference const_reference;
120 typedef typename container_type::size_type size_type;
121 static_assert((is_same<_Tp, value_type>::value), "" );
127 _LIBCPP_INLINE_VISIBILITY
129 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
132 _LIBCPP_INLINE_VISIBILITY
133 stack(const stack& __q) : c(__q.c) {}
135 _LIBCPP_INLINE_VISIBILITY
136 stack& operator=(const stack& __q) {c = __q.c; return *this;}
139 #ifndef _LIBCPP_CXX03_LANG
140 _LIBCPP_INLINE_VISIBILITY
142 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
143 : c(_VSTD::move(__q.c)) {}
145 _LIBCPP_INLINE_VISIBILITY
146 stack& operator=(stack&& __q)
147 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
148 {c = _VSTD::move(__q.c); return *this;}
150 _LIBCPP_INLINE_VISIBILITY
151 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
152 #endif // _LIBCPP_CXX03_LANG
154 _LIBCPP_INLINE_VISIBILITY
155 explicit stack(const container_type& __c) : c(__c) {}
157 template <class _Alloc>
158 _LIBCPP_INLINE_VISIBILITY
159 explicit stack(const _Alloc& __a,
160 typename enable_if<uses_allocator<container_type,
161 _Alloc>::value>::type* = 0)
163 template <class _Alloc>
164 _LIBCPP_INLINE_VISIBILITY
165 stack(const container_type& __c, const _Alloc& __a,
166 typename enable_if<uses_allocator<container_type,
167 _Alloc>::value>::type* = 0)
169 template <class _Alloc>
170 _LIBCPP_INLINE_VISIBILITY
171 stack(const stack& __s, const _Alloc& __a,
172 typename enable_if<uses_allocator<container_type,
173 _Alloc>::value>::type* = 0)
175 #ifndef _LIBCPP_CXX03_LANG
176 template <class _Alloc>
177 _LIBCPP_INLINE_VISIBILITY
178 stack(container_type&& __c, const _Alloc& __a,
179 typename enable_if<uses_allocator<container_type,
180 _Alloc>::value>::type* = 0)
181 : c(_VSTD::move(__c), __a) {}
182 template <class _Alloc>
183 _LIBCPP_INLINE_VISIBILITY
184 stack(stack&& __s, const _Alloc& __a,
185 typename enable_if<uses_allocator<container_type,
186 _Alloc>::value>::type* = 0)
187 : c(_VSTD::move(__s.c), __a) {}
188 #endif // _LIBCPP_CXX03_LANG
190 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
191 bool empty() const {return c.empty();}
192 _LIBCPP_INLINE_VISIBILITY
193 size_type size() const {return c.size();}
194 _LIBCPP_INLINE_VISIBILITY
195 reference top() {return c.back();}
196 _LIBCPP_INLINE_VISIBILITY
197 const_reference top() const {return c.back();}
199 _LIBCPP_INLINE_VISIBILITY
200 void push(const value_type& __v) {c.push_back(__v);}
201 #ifndef _LIBCPP_CXX03_LANG
202 _LIBCPP_INLINE_VISIBILITY
203 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
205 template <class... _Args>
206 _LIBCPP_INLINE_VISIBILITY
207 #if _LIBCPP_STD_VER > 14
208 decltype(auto) emplace(_Args&&... __args)
209 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
211 void emplace(_Args&&... __args)
212 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
214 #endif // _LIBCPP_CXX03_LANG
216 _LIBCPP_INLINE_VISIBILITY
217 void pop() {c.pop_back();}
219 _LIBCPP_INLINE_VISIBILITY
220 void swap(stack& __s)
221 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
227 template <class T1, class _C1>
230 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
232 template <class T1, class _C1>
235 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
238 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
239 template<class _Container,
240 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
243 -> stack<typename _Container::value_type, _Container>;
245 template<class _Container,
247 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
248 class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
250 stack(_Container, _Alloc)
251 -> stack<typename _Container::value_type, _Container>;
254 template <class _Tp, class _Container>
255 inline _LIBCPP_INLINE_VISIBILITY
257 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
259 return __x.c == __y.c;
262 template <class _Tp, class _Container>
263 inline _LIBCPP_INLINE_VISIBILITY
265 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
267 return __x.c < __y.c;
270 template <class _Tp, class _Container>
271 inline _LIBCPP_INLINE_VISIBILITY
273 operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
275 return !(__x == __y);
278 template <class _Tp, class _Container>
279 inline _LIBCPP_INLINE_VISIBILITY
281 operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
286 template <class _Tp, class _Container>
287 inline _LIBCPP_INLINE_VISIBILITY
289 operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
294 template <class _Tp, class _Container>
295 inline _LIBCPP_INLINE_VISIBILITY
297 operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
302 template <class _Tp, class _Container>
303 inline _LIBCPP_INLINE_VISIBILITY
305 __is_swappable<_Container>::value,
308 swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
309 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
314 template <class _Tp, class _Container, class _Alloc>
315 struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
316 : public uses_allocator<_Container, _Alloc>
320 _LIBCPP_END_NAMESPACE_STD
322 #endif // _LIBCPP_STACK