blob: 2b3f8aea19e69f30b240194840c5c1e2f950de91 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- stack -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STACK
12#define _LIBCPP_STACK
13
14/*
15 stack synopsis
16
17namespace std
18{
19
20template <class T, class Container = deque<T>>
21class stack
22{
23public:
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;
29
30protected:
31 container_type c;
32
33public:
Howard Hinnant58cd8232011-06-04 22:09:19 +000034 stack() = default;
35 ~stack() = default;
36
37 stack(const stack& q) = default;
38 stack(stack&& q) = default;
39
40 stack& operator=(const stack& q) = default;
41 stack& operator=(stack&& q) = default;
42
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043 explicit stack(const container_type& c);
44 explicit stack(container_type&& c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045 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);
Howard Hinnant58cd8232011-06-04 22:09:19 +000048 template <class Alloc> stack(const stack& c, const Alloc& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049 template <class Alloc> stack(stack&& c, const Alloc& a);
50
51 bool empty() const;
52 size_type size() const;
53 reference top();
54 const_reference top() const;
55
56 void push(const value_type& x);
57 void push(value_type&& x);
Marshall Clow4e42dc92017-01-24 23:09:12 +000058 template <class... Args> reference emplace(Args&&... args); // reference in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059 void pop();
60
Eric Fiselier8f1e73d2016-04-21 23:38:59 +000061 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062};
63
Marshall Clow0e5d7072018-05-22 01:57:53 +000064template<class Container>
65 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
66
67template<class Container, class Allocator>
68 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
69
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070template <class T, class Container>
71 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
72template <class T, class Container>
73 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
74template <class T, class Container>
75 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
76template <class T, class Container>
77 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
78template <class T, class Container>
79 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
80template <class T, class Container>
81 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
82
83template <class T, class Container>
Howard Hinnant58cd8232011-06-04 22:09:19 +000084 void swap(stack<T, Container>& x, stack<T, Container>& y)
85 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87} // std
88
89*/
90
91#include <__config>
92#include <deque>
93
Howard Hinnant08e17472011-10-17 20:05:10 +000094#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000096#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097
98_LIBCPP_BEGIN_NAMESPACE_STD
99
Eric Fiselierc3589a82017-01-04 23:56:00 +0000100template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101
102template <class _Tp, class _Container>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000103_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104bool
105operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
106
107template <class _Tp, class _Container>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000108_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109bool
110operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
111
Marshall Clowa46f5ce2015-02-18 17:51:56 +0000112template <class _Tp, class _Container /*= deque<_Tp>*/>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000113class _LIBCPP_TEMPLATE_VIS stack
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114{
115public:
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;
Marshall Clowed77ffb2016-03-14 17:58:11 +0000121 static_assert((is_same<_Tp, value_type>::value), "" );
122
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123protected:
124 container_type c;
125
126public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58cd8232011-06-04 22:09:19 +0000128 stack()
129 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
130 : c() {}
131
132 _LIBCPP_INLINE_VISIBILITY
133 stack(const stack& __q) : c(__q.c) {}
134
Eric Fiselier7162dce2017-04-18 21:16:26 +0000135 _LIBCPP_INLINE_VISIBILITY
136 stack& operator=(const stack& __q) {c = __q.c; return *this;}
137
138
139#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant58cd8232011-06-04 22:09:19 +0000140 _LIBCPP_INLINE_VISIBILITY
141 stack(stack&& __q)
142 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000143 : c(_VSTD::move(__q.c)) {}
Howard Hinnant58cd8232011-06-04 22:09:19 +0000144
145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58cd8232011-06-04 22:09:19 +0000146 stack& operator=(stack&& __q)
147 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000148 {c = _VSTD::move(__q.c); return *this;}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000149
150 _LIBCPP_INLINE_VISIBILITY
151 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
152#endif // _LIBCPP_CXX03_LANG
Howard Hinnant58cd8232011-06-04 22:09:19 +0000153
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 explicit stack(const container_type& __c) : c(__c) {}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000156
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 explicit stack(const _Alloc& __a,
160 typename enable_if<uses_allocator<container_type,
161 _Alloc>::value>::type* = 0)
162 : c(__a) {}
163 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165 stack(const container_type& __c, const _Alloc& __a,
166 typename enable_if<uses_allocator<container_type,
167 _Alloc>::value>::type* = 0)
168 : c(__c, __a) {}
169 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171 stack(const stack& __s, const _Alloc& __a,
172 typename enable_if<uses_allocator<container_type,
173 _Alloc>::value>::type* = 0)
174 : c(__s.c, __a) {}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000175#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178 stack(container_type&& __c, const _Alloc& __a,
179 typename enable_if<uses_allocator<container_type,
180 _Alloc>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000181 : c(_VSTD::move(__c), __a) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 stack(stack&& __s, const _Alloc& __a,
185 typename enable_if<uses_allocator<container_type,
186 _Alloc>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000187 : c(_VSTD::move(__s.c), __a) {}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000188#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189
Marshall Clow88626bf2017-11-15 05:51:26 +0000190 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 bool empty() const {return c.empty();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193 size_type size() const {return c.size();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 reference top() {return c.back();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197 const_reference top() const {return c.back();}
198
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200 void push(const value_type& __v) {c.push_back(__v);}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +0000203 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
Eric Fiselier7162dce2017-04-18 21:16:26 +0000204
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000205 template <class... _Args>
206 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4e42dc92017-01-24 23:09:12 +0000207#if _LIBCPP_STD_VER > 14
Marshall Clowcc704882018-01-24 22:42:25 +0000208 decltype(auto) emplace(_Args&&... __args)
Eric Fiselier3816ef92016-07-21 03:20:17 +0000209 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
Marshall Clow4e42dc92017-01-24 23:09:12 +0000210#else
211 void emplace(_Args&&... __args)
212 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
213#endif
Eric Fiselier7162dce2017-04-18 21:16:26 +0000214#endif // _LIBCPP_CXX03_LANG
215
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 void pop() {c.pop_back();}
218
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 void swap(stack& __s)
Howard Hinnant58cd8232011-06-04 22:09:19 +0000221 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000223 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224 swap(c, __s.c);
225 }
226
227 template <class T1, class _C1>
228 friend
229 bool
230 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +0000231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 template <class T1, class _C1>
233 friend
234 bool
235 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
236};
237
Marshall Clow0e5d7072018-05-22 01:57:53 +0000238#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
239template<class _Container,
240 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
241>
242stack(_Container)
243 -> stack<typename _Container::value_type, _Container>;
244
245template<class _Container,
246 class _Alloc,
247 class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
248 class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
249 >
250stack(_Container, _Alloc)
251 -> stack<typename _Container::value_type, _Container>;
252#endif
253
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000255inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256bool
257operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
258{
259 return __x.c == __y.c;
260}
261
262template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264bool
265operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
266{
267 return __x.c < __y.c;
268}
269
270template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272bool
273operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
274{
275 return !(__x == __y);
276}
277
278template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280bool
281operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
282{
283 return __y < __x;
284}
285
286template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288bool
289operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
290{
291 return !(__x < __y);
292}
293
294template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296bool
297operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
298{
299 return !(__y < __x);
300}
301
302template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000303inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000304typename enable_if<
305 __is_swappable<_Container>::value,
306 void
307>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
Howard Hinnant58cd8232011-06-04 22:09:19 +0000309 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310{
311 __x.swap(__y);
312}
313
314template <class _Tp, class _Container, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000315struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 : public uses_allocator<_Container, _Alloc>
317{
318};
319
320_LIBCPP_END_NAMESPACE_STD
321
322#endif // _LIBCPP_STACK