blob: 3cf6cd2332823d78210487c1adf6461d7a59e0ac [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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>
Christopher Di Bella55d7a822021-07-01 09:25:35 -040091#include <__memory/uses_allocator.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +000092#include <__utility/forward.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +000093#include <deque>
Mark de Weverce8f12c2021-12-22 18:14:14 +010094#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +000095
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000096#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000097#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000098#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000099
100_LIBCPP_BEGIN_NAMESPACE_STD
101
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000102template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103
104template <class _Tp, class _Container>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000105_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106bool
107operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
108
109template <class _Tp, class _Container>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000110_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111bool
112operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
113
Marshall Clow9e1b8452015-02-18 17:51:56 +0000114template <class _Tp, class _Container /*= deque<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000115class _LIBCPP_TEMPLATE_VIS stack
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116{
117public:
118 typedef _Container container_type;
119 typedef typename container_type::value_type value_type;
120 typedef typename container_type::reference reference;
121 typedef typename container_type::const_reference const_reference;
122 typedef typename container_type::size_type size_type;
Marshall Clowb8825f02016-03-14 17:58:11 +0000123 static_assert((is_same<_Tp, value_type>::value), "" );
Louis Dionne173f29e2019-05-29 16:01:36 +0000124
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125protected:
126 container_type c;
127
128public:
Howard Hinnant186dca82010-09-23 17:31:07 +0000129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante0da4a12011-06-04 22:09:19 +0000130 stack()
131 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
132 : c() {}
133
134 _LIBCPP_INLINE_VISIBILITY
135 stack(const stack& __q) : c(__q.c) {}
136
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000137 _LIBCPP_INLINE_VISIBILITY
138 stack& operator=(const stack& __q) {c = __q.c; return *this;}
139
140
141#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante0da4a12011-06-04 22:09:19 +0000142 _LIBCPP_INLINE_VISIBILITY
143 stack(stack&& __q)
144 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000145 : c(_VSTD::move(__q.c)) {}
Howard Hinnante0da4a12011-06-04 22:09:19 +0000146
147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante0da4a12011-06-04 22:09:19 +0000148 stack& operator=(stack&& __q)
149 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000150 {c = _VSTD::move(__q.c); return *this;}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000151
152 _LIBCPP_INLINE_VISIBILITY
153 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400154#endif // _LIBCPP_CXX03_LANG
Howard Hinnante0da4a12011-06-04 22:09:19 +0000155
Howard Hinnant186dca82010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 explicit stack(const container_type& __c) : c(__c) {}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000158
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161 explicit stack(const _Alloc& __a,
Louis Dionne9ce598d2021-09-08 09:14:43 -0400162 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 : c(__a) {}
164 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 stack(const container_type& __c, const _Alloc& __a,
Louis Dionne9ce598d2021-09-08 09:14:43 -0400167 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 : c(__c, __a) {}
169 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 stack(const stack& __s, const _Alloc& __a,
Louis Dionne9ce598d2021-09-08 09:14:43 -0400172 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173 : c(__s.c, __a) {}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000174#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 stack(container_type&& __c, const _Alloc& __a,
Louis Dionne9ce598d2021-09-08 09:14:43 -0400178 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000179 : c(_VSTD::move(__c), __a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 template <class _Alloc>
Howard Hinnant186dca82010-09-23 17:31:07 +0000181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182 stack(stack&& __s, const _Alloc& __a,
Louis Dionne9ce598d2021-09-08 09:14:43 -0400183 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000184 : c(_VSTD::move(__s.c), __a) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400185#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Marshall Clow425f5752017-11-15 05:51:26 +0000187 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188 bool empty() const {return c.empty();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190 size_type size() const {return c.size();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192 reference top() {return c.back();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 const_reference top() const {return c.back();}
195
Howard Hinnant186dca82010-09-23 17:31:07 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197 void push(const value_type& __v) {c.push_back(__v);}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000198#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +0000199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000200 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000201
Howard Hinnant186dca82010-09-23 17:31:07 +0000202 template <class... _Args>
203 _LIBCPP_INLINE_VISIBILITY
Marshall Clowea52cc42017-01-24 23:09:12 +0000204#if _LIBCPP_STD_VER > 14
Marshall Clow88746852018-01-24 22:42:25 +0000205 decltype(auto) emplace(_Args&&... __args)
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000206 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
Marshall Clowea52cc42017-01-24 23:09:12 +0000207#else
208 void emplace(_Args&&... __args)
209 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
210#endif
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400211#endif // _LIBCPP_CXX03_LANG
Eric Fiselier33cc2f02017-04-18 21:16:26 +0000212
Howard Hinnant186dca82010-09-23 17:31:07 +0000213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214 void pop() {c.pop_back();}
215
Howard Hinnant186dca82010-09-23 17:31:07 +0000216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 void swap(stack& __s)
Howard Hinnante0da4a12011-06-04 22:09:19 +0000218 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000220 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 swap(c, __s.c);
222 }
223
224 template <class T1, class _C1>
225 friend
226 bool
227 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000228
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 template <class T1, class _C1>
230 friend
231 bool
232 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
233};
234
Louis Dionned59f8a52021-08-17 11:59:07 -0400235#if _LIBCPP_STD_VER >= 17
Marshall Clow592d9952018-05-22 01:57:53 +0000236template<class _Container,
Louis Dionne25547162021-08-17 12:26:09 -0400237 class = enable_if_t<!__is_allocator<_Container>::value>
Marshall Clow592d9952018-05-22 01:57:53 +0000238>
239stack(_Container)
240 -> stack<typename _Container::value_type, _Container>;
Louis Dionne173f29e2019-05-29 16:01:36 +0000241
Marshall Clow592d9952018-05-22 01:57:53 +0000242template<class _Container,
243 class _Alloc,
Louis Dionne25547162021-08-17 12:26:09 -0400244 class = enable_if_t<!__is_allocator<_Container>::value>,
245 class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
Louis Dionne173f29e2019-05-29 16:01:36 +0000246 >
Marshall Clow592d9952018-05-22 01:57:53 +0000247stack(_Container, _Alloc)
248 -> stack<typename _Container::value_type, _Container>;
249#endif
250
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000252inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253bool
254operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
255{
256 return __x.c == __y.c;
257}
258
259template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261bool
262operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
263{
264 return __x.c < __y.c;
265}
266
267template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269bool
270operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
271{
272 return !(__x == __y);
273}
274
275template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277bool
278operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
279{
280 return __y < __x;
281}
282
283template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285bool
286operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
287{
288 return !(__x < __y);
289}
290
291template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293bool
294operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
295{
296 return !(__y < __x);
297}
298
299template <class _Tp, class _Container>
Howard Hinnant186dca82010-09-23 17:31:07 +0000300inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -0400301__enable_if_t<__is_swappable<_Container>::value, void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
Howard Hinnante0da4a12011-06-04 22:09:19 +0000303 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304{
305 __x.swap(__y);
306}
307
308template <class _Tp, class _Container, class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000309struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310 : public uses_allocator<_Container, _Alloc>
311{
312};
313
314_LIBCPP_END_NAMESPACE_STD
315
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400316#endif // _LIBCPP_STACK