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