1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// 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
7//
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:
33 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
42 explicit stack(const container_type& c);
43 explicit stack(container_type&& c);
44 template <class InputIterator> stack(InputIterator first, InputIterator last); // since C++23
45 template<container-compatible-range<T> R> stack(from_range_t, R&& rg); // since C++23
46 template <class Alloc> explicit stack(const Alloc& a);
47 template <class Alloc> stack(const container_type& c, const Alloc& a);
48 template <class Alloc> stack(container_type&& c, const Alloc& a);
49 template <class Alloc> stack(const stack& c, const Alloc& a);
50 template <class Alloc> stack(stack&& c, const Alloc& a);
51 template<class InputIterator, class Alloc>
52 stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
53 template<container-compatible-range<T> R, class Alloc>
54 stack(from_range_t, R&& rg, const Alloc&); // since C++23
55
56 bool empty() const;
57 size_type size() const;
58 reference top();
59 const_reference top() const;
60
61 void push(const value_type& x);
62 void push(value_type&& x);
63 template<container-compatible-range<T> R>
64 void push_range(R&& rg); // C++23
65 template <class... Args> reference emplace(Args&&... args); // reference in C++17
66 void pop();
67
68 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
69};
70
71template<class Container>
72 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
73
74template<class InputIterator>
75 stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
76
77template<ranges::input_range R>
78 stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23
79
80template<class Container, class Allocator>
81 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
82
83template<class InputIterator, class Allocator>
84 stack(InputIterator, InputIterator, Allocator)
85 -> stack<iter-value-type<InputIterator>,
86 deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
87
88template<ranges::input_range R, class Allocator>
89 stack(from_range_t, R&&, Allocator)
90 -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
91
92template <class T, class Container>
93 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
94template <class T, class Container>
95 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
96template <class T, class Container>
97 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
98template <class T, class Container>
99 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
100template <class T, class Container>
101 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
102template <class T, class Container>
103 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
104template<class T, three_way_comparable Container>
105 compare_three_way_result_t<Container>
106 operator<=>(const stack<T, Container>& x, const stack<T, Container>& y); // since C++20
107
108template <class T, class Container>
109 void swap(stack<T, Container>& x, stack<T, Container>& y)
110 noexcept(noexcept(x.swap(y)));
111
112} // std
113
114*/
115
116#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
117# include <__cxx03/stack>
118#else
119# include <__algorithm/ranges_copy.h>
120# include <__config>
121# include <__fwd/stack.h>
122# include <__iterator/back_insert_iterator.h>
123# include <__iterator/iterator_traits.h>
124# include <__memory/uses_allocator.h>
125# include <__ranges/access.h>
126# include <__ranges/concepts.h>
127# include <__ranges/container_compatible_range.h>
128# include <__ranges/from_range.h>
129# include <__type_traits/is_same.h>
130# include <__utility/forward.h>
131# include <deque>
132# include <version>
133
134// standard-mandated includes
135
136// [stack.syn]
137# include <compare>
138# include <initializer_list>
139
140# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
141# pragma GCC system_header
142# endif
143
144_LIBCPP_PUSH_MACROS
145# include <__undef_macros>
146
147_LIBCPP_BEGIN_NAMESPACE_STD
148
149template <class _Tp, class _Container>
150_LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
151
152template <class _Tp, class _Container>
153_LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
154
155template <class _Tp, class _Container /*= deque<_Tp>*/>
156class stack {
157public:
158 typedef _Container container_type;
159 typedef typename container_type::value_type value_type;
160 typedef typename container_type::reference reference;
161 typedef typename container_type::const_reference const_reference;
162 typedef typename container_type::size_type size_type;
163 static_assert(is_same<_Tp, value_type>::value,
164 "The stored elements type must match the underlying container's value_type.");
165
166protected:
167 container_type c;
168
169public:
170 _LIBCPP_HIDE_FROM_ABI stack() _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) : c() {}
171
172 _LIBCPP_HIDE_FROM_ABI stack(const stack& __q) : c(__q.c) {}
173
174 _LIBCPP_HIDE_FROM_ABI stack& operator=(const stack& __q) {
175 c = __q.c;
176 return *this;
177 }
178
179# ifndef _LIBCPP_CXX03_LANG
180 _LIBCPP_HIDE_FROM_ABI stack(stack&& __q) noexcept(is_nothrow_move_constructible<container_type>::value)
181 : c(std::move(__q.c)) {}
182
183 _LIBCPP_HIDE_FROM_ABI stack& operator=(stack&& __q) noexcept(is_nothrow_move_assignable<container_type>::value) {
184 c = std::move(__q.c);
185 return *this;
186 }
187
188 _LIBCPP_HIDE_FROM_ABI explicit stack(container_type&& __c) : c(std::move(__c)) {}
189# endif // _LIBCPP_CXX03_LANG
190
191 _LIBCPP_HIDE_FROM_ABI explicit stack(const container_type& __c) : c(__c) {}
192
193 template <class _Alloc>
194 _LIBCPP_HIDE_FROM_ABI explicit stack(const _Alloc& __a,
195 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
196 : c(__a) {}
197 template <class _Alloc>
198 _LIBCPP_HIDE_FROM_ABI
199 stack(const container_type& __c, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
200 : c(__c, __a) {}
201 template <class _Alloc>
202 _LIBCPP_HIDE_FROM_ABI
203 stack(const stack& __s, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
204 : c(__s.c, __a) {}
205# ifndef _LIBCPP_CXX03_LANG
206 template <class _Alloc>
207 _LIBCPP_HIDE_FROM_ABI
208 stack(container_type&& __c, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
209 : c(std::move(__c), __a) {}
210 template <class _Alloc>
211 _LIBCPP_HIDE_FROM_ABI
212 stack(stack&& __s, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
213 : c(std::move(__s.c), __a) {}
214# endif // _LIBCPP_CXX03_LANG
215
216# if _LIBCPP_STD_VER >= 23
217 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
218 _LIBCPP_HIDE_FROM_ABI stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
219
220 template <_ContainerCompatibleRange<_Tp> _Range>
221 _LIBCPP_HIDE_FROM_ABI stack(from_range_t, _Range&& __range) : c(from_range, std::forward<_Range>(__range)) {}
222
223 template <class _InputIterator,
224 class _Alloc,
225 __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0,
226 __enable_if_t<uses_allocator<container_type, _Alloc>::value, int> = 0>
227 _LIBCPP_HIDE_FROM_ABI stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc)
228 : c(__first, __last, __alloc) {}
229
230 template <_ContainerCompatibleRange<_Tp> _Range,
231 class _Alloc,
232 __enable_if_t<uses_allocator<container_type, _Alloc>::value, int> = 0>
233 _LIBCPP_HIDE_FROM_ABI stack(from_range_t, _Range&& __range, const _Alloc& __alloc)
234 : c(from_range, std::forward<_Range>(__range), __alloc) {}
235
236# endif
237
238 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
239 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
240 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
241 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
242
243 _LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); }
244# ifndef _LIBCPP_CXX03_LANG
245 _LIBCPP_HIDE_FROM_ABI void push(value_type&& __v) { c.push_back(std::move(__v)); }
246
247# if _LIBCPP_STD_VER >= 23
248 template <_ContainerCompatibleRange<_Tp> _Range>
249 _LIBCPP_HIDE_FROM_ABI void push_range(_Range&& __range) {
250 if constexpr (requires(container_type& __c) { __c.append_range(std::forward<_Range>(__range)); }) {
251 c.append_range(std::forward<_Range>(__range));
252 } else {
253 ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
254 }
255 }
256# endif
257
258 template <class... _Args>
259 _LIBCPP_HIDE_FROM_ABI
260# if _LIBCPP_STD_VER >= 17
261 decltype(auto)
262 emplace(_Args&&... __args) {
263 return c.emplace_back(std::forward<_Args>(__args)...);
264 }
265# else
266 void
267 emplace(_Args&&... __args) {
268 c.emplace_back(std::forward<_Args>(__args)...);
269 }
270# endif
271# endif // _LIBCPP_CXX03_LANG
272
273 _LIBCPP_HIDE_FROM_ABI void pop() { c.pop_back(); }
274
275 _LIBCPP_HIDE_FROM_ABI void swap(stack& __s) _NOEXCEPT_(__is_nothrow_swappable_v<container_type>) {
276 using std::swap;
277 swap(c, __s.c);
278 }
279
280 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
281
282 template <class _T1, class _OtherContainer>
283 friend _LIBCPP_HIDE_FROM_ABI bool
284 operator==(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
285
286 template <class _T1, class _OtherContainer>
287 friend _LIBCPP_HIDE_FROM_ABI bool
288 operator<(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
289
290# if _LIBCPP_STD_VER >= 20
291 template <class _T1, three_way_comparable _OtherContainer>
292 friend _LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_OtherContainer>
293 operator<=>(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
294# endif
295};
296
297# if _LIBCPP_STD_VER >= 17
298template <class _Container, class = enable_if_t<!__is_allocator_v<_Container>>>
299stack(_Container) -> stack<typename _Container::value_type, _Container>;
300
301template <class _Container,
302 class _Alloc,
303 class = enable_if_t<!__is_allocator_v<_Container>>,
304 class = enable_if_t<uses_allocator<_Container, _Alloc>::value> >
305stack(_Container, _Alloc) -> stack<typename _Container::value_type, _Container>;
306# endif
307
308# if _LIBCPP_STD_VER >= 23
309template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
310stack(_InputIterator, _InputIterator) -> stack<__iterator_value_type<_InputIterator>>;
311
312template <ranges::input_range _Range>
313stack(from_range_t, _Range&&) -> stack<ranges::range_value_t<_Range>>;
314
315template <class _InputIterator,
316 class _Alloc,
317 __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0,
318 __enable_if_t<__is_allocator_v<_Alloc>, int> = 0>
319stack(_InputIterator, _InputIterator, _Alloc)
320 -> stack<__iterator_value_type<_InputIterator>, deque<__iterator_value_type<_InputIterator>, _Alloc>>;
321
322template <ranges::input_range _Range, class _Alloc, __enable_if_t<__is_allocator_v<_Alloc>, int> = 0>
323stack(from_range_t, _Range&&, _Alloc)
324 -> stack<ranges::range_value_t<_Range>, deque<ranges::range_value_t<_Range>, _Alloc>>;
325
326# endif
327
328template <class _Tp, class _Container>
329inline _LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
330 return __x.c == __y.c;
331}
332
333template <class _Tp, class _Container>
334inline _LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
335 return __x.c < __y.c;
336}
337
338template <class _Tp, class _Container>
339inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
340 return !(__x == __y);
341}
342
343template <class _Tp, class _Container>
344inline _LIBCPP_HIDE_FROM_ABI bool operator>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
345 return __y < __x;
346}
347
348template <class _Tp, class _Container>
349inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
350 return !(__x < __y);
351}
352
353template <class _Tp, class _Container>
354inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
355 return !(__y < __x);
356}
357
358# if _LIBCPP_STD_VER >= 20
359
360template <class _Tp, three_way_comparable _Container>
361_LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_Container>
362operator<=>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
363 return __x.c <=> __y.c;
364}
365
366# endif
367
368template <class _Tp, class _Container, __enable_if_t<__is_swappable_v<_Container>, int> = 0>
369inline _LIBCPP_HIDE_FROM_ABI void swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
370 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
371 __x.swap(__y);
372}
373
374template <class _Tp, class _Container, class _Alloc>
375struct uses_allocator<stack<_Tp, _Container>, _Alloc> : public uses_allocator<_Container, _Alloc> {};
376
377_LIBCPP_END_NAMESPACE_STD
378
379_LIBCPP_POP_MACROS
380
381#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
382
383#endif // _LIBCPP_STACK
384