blob: 30909c1ee3bd1b19118c6cac95c1d4977020cfe9 [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);
58 template <class... Args> void emplace(Args&&... args);
59 void pop();
60
Howard Hinnant58cd8232011-06-04 22:09:19 +000061 void swap(stack& c) noexcept(noexcept(swap(c, q.c)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062};
63
64template <class T, class Container>
65 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
66template <class T, class Container>
67 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
68template <class T, class Container>
69 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
70template <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);
76
77template <class T, class Container>
Howard Hinnant58cd8232011-06-04 22:09:19 +000078 void swap(stack<T, Container>& x, stack<T, Container>& y)
79 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080
81} // std
82
83*/
84
85#include <__config>
86#include <deque>
87
Howard Hinnant08e17472011-10-17 20:05:10 +000088#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000090#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091
92_LIBCPP_BEGIN_NAMESPACE_STD
93
Howard Hinnant0f678bd2013-08-12 18:38:34 +000094template <class _Tp, class _Container> class _LIBCPP_TYPE_VIS_ONLY stack;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095
96template <class _Tp, class _Container>
Howard Hinnant33be35e2012-09-14 00:39:16 +000097_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098bool
99operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
100
101template <class _Tp, class _Container>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000102_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103bool
104operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
105
106template <class _Tp, class _Container = deque<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000107class _LIBCPP_TYPE_VIS_ONLY stack
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108{
109public:
110 typedef _Container container_type;
111 typedef typename container_type::value_type value_type;
112 typedef typename container_type::reference reference;
113 typedef typename container_type::const_reference const_reference;
114 typedef typename container_type::size_type size_type;
115
116protected:
117 container_type c;
118
119public:
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58cd8232011-06-04 22:09:19 +0000121 stack()
122 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
123 : c() {}
124
125 _LIBCPP_INLINE_VISIBILITY
126 stack(const stack& __q) : c(__q.c) {}
127
128#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
129 _LIBCPP_INLINE_VISIBILITY
130 stack(stack&& __q)
131 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000132 : c(_VSTD::move(__q.c)) {}
Howard Hinnant58cd8232011-06-04 22:09:19 +0000133#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
134
135 _LIBCPP_INLINE_VISIBILITY
136 stack& operator=(const stack& __q) {c = __q.c; return *this;}
137
138#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
139 _LIBCPP_INLINE_VISIBILITY
140 stack& operator=(stack&& __q)
141 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000142 {c = _VSTD::move(__q.c); return *this;}
Howard Hinnant58cd8232011-06-04 22:09:19 +0000143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
144
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146 explicit stack(const container_type& __c) : c(__c) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000147#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +0000149 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000150#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153 explicit stack(const _Alloc& __a,
154 typename enable_if<uses_allocator<container_type,
155 _Alloc>::value>::type* = 0)
156 : c(__a) {}
157 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 stack(const container_type& __c, const _Alloc& __a,
160 typename enable_if<uses_allocator<container_type,
161 _Alloc>::value>::type* = 0)
162 : c(__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 stack& __s, const _Alloc& __a,
166 typename enable_if<uses_allocator<container_type,
167 _Alloc>::value>::type* = 0)
168 : c(__s.c, __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000169#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 template <class _Alloc>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 stack(container_type&& __c, const _Alloc& __a,
173 typename enable_if<uses_allocator<container_type,
174 _Alloc>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000175 : c(_VSTD::move(__c), __a) {}
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(stack&& __s, 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(__s.c), __a) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000182#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000183
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185 bool empty() const {return c.empty();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187 size_type size() const {return c.size();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189 reference top() {return c.back();}
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 const_reference top() const {return c.back();}
192
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194 void push(const value_type& __v) {c.push_back(__v);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000195#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +0000197 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000198#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000199 template <class... _Args>
200 _LIBCPP_INLINE_VISIBILITY
201 void emplace(_Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000202 {c.emplace_back(_VSTD::forward<_Args>(__args)...);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000203#endif // _LIBCPP_HAS_NO_VARIADICS
204#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206 void pop() {c.pop_back();}
207
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 void swap(stack& __s)
Howard Hinnant58cd8232011-06-04 22:09:19 +0000210 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000212 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 swap(c, __s.c);
214 }
215
216 template <class T1, class _C1>
217 friend
218 bool
219 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +0000220
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221 template <class T1, class _C1>
222 friend
223 bool
224 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
225};
226
227template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229bool
230operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
231{
232 return __x.c == __y.c;
233}
234
235template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237bool
238operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
239{
240 return __x.c < __y.c;
241}
242
243template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245bool
246operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
247{
248 return !(__x == __y);
249}
250
251template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000252inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253bool
254operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
255{
256 return __y < __x;
257}
258
259template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261bool
262operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
263{
264 return !(__x < __y);
265}
266
267template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269bool
270operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
271{
272 return !(__y < __x);
273}
274
275template <class _Tp, class _Container>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277void
278swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
Howard Hinnant58cd8232011-06-04 22:09:19 +0000279 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280{
281 __x.swap(__y);
282}
283
284template <class _Tp, class _Container, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000285struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286 : public uses_allocator<_Container, _Alloc>
287{
288};
289
290_LIBCPP_END_NAMESPACE_STD
291
292#endif // _LIBCPP_STACK