blob: a45b979dea9c51987aa66de6a95c0f20f2ec562b [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- stack -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STACK
11#define _LIBCPP_STACK
12
13/*
14 stack synopsis
15
16namespace std
17{
18
19template <class T, class Container = deque<T>>
20class stack
21{
22public:
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;
28
29protected:
30 container_type c;
31
32public:
Howard Hinnante0da4a12011-06-04 22:09:19 +000033 stack() = default;
34 ~stack() = default;
35
36 stack(const stack& q) = default;
37 stack(stack&& q) = default;
38
39 stack& operator=(const stack& q) = default;
40 stack& operator=(stack&& q) = default;
41
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 explicit stack(const container_type& c);
43 explicit stack(container_type&& c);
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 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);
Howard Hinnante0da4a12011-06-04 22:09:19 +000047 template <class Alloc> stack(const stack& c, const Alloc& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 template <class Alloc> stack(stack&& c, const Alloc& a);
49
50 bool empty() const;
51 size_type size() const;
52 reference top();
53 const_reference top() const;
54
55 void push(const value_type& x);
56 void push(value_type&& x);
Marshall Clowea52cc42017-01-24 23:09:12 +000057 template <class... Args> reference emplace(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000058 void pop();
59
Eric Fiselier6bfed252016-04-21 23:38:59 +000060 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
Howard Hinnantc51e1022010-05-11 19:42:16 +000061};
62
Marshall Clow592d9952018-05-22 01:57:53 +000063template<class Container>
64 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
Louis Dionne173f29e2019-05-29 16:01:36 +000065
66template<class Container, class Allocator>
Marshall Clow592d9952018-05-22 01:57:53 +000067 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
68
Howard Hinnantc51e1022010-05-11 19:42:16 +000069template <class T, class Container>
70 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
71template <class T, class Container>
72 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
73template <class T, class Container>
74 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
75template <class T, class Container>
76 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
77template <class T, class Container>
78 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
79template <class T, class Container>
80 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
81
82template <class T, class Container>
Howard Hinnante0da4a12011-06-04 22:09:19 +000083 void swap(stack<T, Container>& x, stack<T, Container>& y)
84 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
86} // std
87
88*/
89
90#include <__config>
91#include <deque>
92
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000093#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000094#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000095#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97_LIBCPP_BEGIN_NAMESPACE_STD
98
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000099template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100
101template <class _Tp, class _Container>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000102_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103bool
104operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
105
106template <class _Tp, class _Container>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000107_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108bool
109operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
110
Marshall Clow9e1b8452015-02-18 17:51:56 +0000111template <class _Tp, class _Container /*= deque<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000112class _LIBCPP_TEMPLATE_VIS stack
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113{
114public:
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;
Marshall Clowb8825f02016-03-14 17:58:11 +0000120 static_assert((is_same<_Tp, value_type>::value), "" );
Louis Dionne173f29e2019-05-29 16:01:36 +0000121
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122protected:
123 container_type c;
124
125public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante0da4a12011-06-04 22:09:19 +0000127 stack()
128 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
129 : c() {}
130
131 _LIBCPP_INLINE_VISIBILITY
132 stack(const stack& __q) : c(__q.c) {}
133
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000134 _LIBCPP_INLINE_VISIBILITY
135 stack& operator=(const stack& __q) {c = __q.c; return *this;}
136
137
138#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante0da4a12011-06-04 22:09:19 +0000139 _LIBCPP_INLINE_VISIBILITY
140 stack(stack&& __q)
141 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000142 : c(_VSTD::move(__q.c)) {}
Howard Hinnante0da4a12011-06-04 22:09:19 +0000143
144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante0da4a12011-06-04 22:09:19 +0000145 stack& operator=(stack&& __q)
146 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000147 {c = _VSTD::move(__q.c); return *this;}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000148
149 _LIBCPP_INLINE_VISIBILITY
150 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400151#endif // _LIBCPP_CXX03_LANG
Howard Hinnante0da4a12011-06-04 22:09:19 +0000152
Howard Hinnant186dca82010-09-23 17:31:07 +0000153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 explicit stack(const container_type& __c) : c(__c) {}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000155
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 explicit stack(const _Alloc& __a,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500159 _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 : c(__a) {}
161 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 stack(const container_type& __c, const _Alloc& __a,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500164 _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165 : c(__c, __a) {}
166 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 stack(const stack& __s, const _Alloc& __a,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500169 _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 : c(__s.c, __a) {}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000171#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 stack(container_type&& __c, const _Alloc& __a,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500175 _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000176 : c(_VSTD::move(__c), __a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 stack(stack&& __s, const _Alloc& __a,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500180 _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000181 : c(_VSTD::move(__s.c), __a) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400182#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Marshall Clow425f5752017-11-15 05:51:26 +0000184 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 bool empty() const {return c.empty();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 size_type size() const {return c.size();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 reference top() {return c.back();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 const_reference top() const {return c.back();}
192
Howard Hinnant186dca82010-09-23 17:31:07 +0000193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 void push(const value_type& __v) {c.push_back(__v);}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000195#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000197 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000198
Howard Hinnant186dca82010-09-23 17:31:07 +0000199 template <class... _Args>
200 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000201#if _LIBCPP_STD_VER > 14
Marshall Clow88746852018-01-24 22:42:25 +0000202 decltype(auto) emplace(_Args&&... __args)
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000203 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
Marshall Clowea52cc42017-01-24 23:09:12 +0000204#else
205 void emplace(_Args&&... __args)
206 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
207#endif
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400208#endif // _LIBCPP_CXX03_LANG
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000209
Howard Hinnant186dca82010-09-23 17:31:07 +0000210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 void pop() {c.pop_back();}
212
Howard Hinnant186dca82010-09-23 17:31:07 +0000213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214 void swap(stack& __s)
Howard Hinnante0da4a12011-06-04 22:09:19 +0000215 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000217 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 swap(c, __s.c);
219 }
220
221 template <class T1, class _C1>
222 friend
223 bool
224 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000225
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 template <class T1, class _C1>
227 friend
228 bool
229 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
230};
231
Marshall Clow592d9952018-05-22 01:57:53 +0000232#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
233template<class _Container,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500234 class = _EnableIf<!__is_allocator<_Container>::value>
Marshall Clow592d9952018-05-22 01:57:53 +0000235>
236stack(_Container)
237 -> stack<typename _Container::value_type, _Container>;
Louis Dionne173f29e2019-05-29 16:01:36 +0000238
Marshall Clow592d9952018-05-22 01:57:53 +0000239template<class _Container,
240 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500241 class = _EnableIf<!__is_allocator<_Container>::value>,
242 class = _EnableIf<__is_allocator<_Alloc>::value>
Louis Dionne173f29e2019-05-29 16:01:36 +0000243 >
Marshall Clow592d9952018-05-22 01:57:53 +0000244stack(_Container, _Alloc)
245 -> stack<typename _Container::value_type, _Container>;
246#endif
247
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250bool
251operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
252{
253 return __x.c == __y.c;
254}
255
256template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000257inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258bool
259operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
260{
261 return __x.c < __y.c;
262}
263
264template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266bool
267operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
268{
269 return !(__x == __y);
270}
271
272template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274bool
275operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
276{
277 return __y < __x;
278}
279
280template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282bool
283operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
284{
285 return !(__x < __y);
286}
287
288template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290bool
291operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
292{
293 return !(__y < __x);
294}
295
296template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000297inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer56226762021-03-03 23:02:20 -0500298_EnableIf<__is_swappable<_Container>::value, void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
Howard Hinnante0da4a12011-06-04 22:09:19 +0000300 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301{
302 __x.swap(__y);
303}
304
305template <class _Tp, class _Container, class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000306struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 : public uses_allocator<_Container, _Alloc>
308{
309};
310
311_LIBCPP_END_NAMESPACE_STD
312
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400313#endif // _LIBCPP_STACK