xref: /llvm-project-15.0.7/libcxx/include/stack (revision 8fa2e679)
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 <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);
48    template <class Alloc> stack(const stack& c, const Alloc& a);
49    template <class Alloc> stack(stack&& c, const Alloc& a);
50    template<class InputIterator, class Alloc>
51    stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
52
53    bool empty() const;
54    size_type size() const;
55    reference top();
56    const_reference top() const;
57
58    void push(const value_type& x);
59    void push(value_type&& x);
60    template <class... Args> reference emplace(Args&&... args); // reference in C++17
61    void pop();
62
63    void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
64};
65
66template<class Container>
67  stack(Container) -> stack<typename Container::value_type, Container>;  // C++17
68
69template<class InputIterator>
70  stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
71
72template<class Container, class Allocator>
73  stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
74
75template<class InputIterator, class Allocator>
76  stack(InputIterator, InputIterator, Allocator)
77    -> stack<iter-value-type<InputIterator>,
78             deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
79
80template <class T, class Container>
81  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
82template <class T, class Container>
83  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
84template <class T, class Container>
85  bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
86template <class T, class Container>
87  bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
88template <class T, class Container>
89  bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
90template <class T, class Container>
91  bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
92
93template <class T, class Container>
94  void swap(stack<T, Container>& x, stack<T, Container>& y)
95  noexcept(noexcept(x.swap(y)));
96
97}  // std
98
99*/
100
101#include <__assert> // all public C++ headers provide the assertion handler
102#include <__config>
103#include <__iterator/iterator_traits.h>
104#include <__memory/uses_allocator.h>
105#include <__utility/forward.h>
106#include <deque>
107#include <type_traits>
108#include <version>
109
110// standard-mandated includes
111#include <compare>
112#include <initializer_list>
113
114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115#  pragma GCC system_header
116#endif
117
118_LIBCPP_BEGIN_NAMESPACE_STD
119
120template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
121
122template <class _Tp, class _Container>
123_LIBCPP_INLINE_VISIBILITY
124bool
125operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
126
127template <class _Tp, class _Container>
128_LIBCPP_INLINE_VISIBILITY
129bool
130operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
131
132template <class _Tp, class _Container /*= deque<_Tp>*/>
133class _LIBCPP_TEMPLATE_VIS stack
134{
135public:
136    typedef _Container                               container_type;
137    typedef typename container_type::value_type      value_type;
138    typedef typename container_type::reference       reference;
139    typedef typename container_type::const_reference const_reference;
140    typedef typename container_type::size_type       size_type;
141    static_assert((is_same<_Tp, value_type>::value), "" );
142
143protected:
144    container_type c;
145
146public:
147    _LIBCPP_INLINE_VISIBILITY
148    stack()
149        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
150        : c() {}
151
152    _LIBCPP_INLINE_VISIBILITY
153    stack(const stack& __q) : c(__q.c) {}
154
155    _LIBCPP_INLINE_VISIBILITY
156    stack& operator=(const stack& __q) {c = __q.c; return *this;}
157
158
159#ifndef _LIBCPP_CXX03_LANG
160    _LIBCPP_INLINE_VISIBILITY
161    stack(stack&& __q)
162        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
163        : c(_VSTD::move(__q.c)) {}
164
165    _LIBCPP_INLINE_VISIBILITY
166    stack& operator=(stack&& __q)
167        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
168        {c = _VSTD::move(__q.c); return *this;}
169
170    _LIBCPP_INLINE_VISIBILITY
171    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
172#endif // _LIBCPP_CXX03_LANG
173
174    _LIBCPP_INLINE_VISIBILITY
175    explicit stack(const container_type& __c) : c(__c) {}
176
177    template <class _Alloc>
178        _LIBCPP_INLINE_VISIBILITY
179        explicit stack(const _Alloc& __a,
180                       __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
181            : c(__a) {}
182    template <class _Alloc>
183        _LIBCPP_INLINE_VISIBILITY
184        stack(const container_type& __c, const _Alloc& __a,
185              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
186            : c(__c, __a) {}
187    template <class _Alloc>
188        _LIBCPP_INLINE_VISIBILITY
189        stack(const stack& __s, const _Alloc& __a,
190              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
191            : c(__s.c, __a) {}
192#ifndef _LIBCPP_CXX03_LANG
193    template <class _Alloc>
194        _LIBCPP_INLINE_VISIBILITY
195        stack(container_type&& __c, const _Alloc& __a,
196              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
197            : c(_VSTD::move(__c), __a) {}
198    template <class _Alloc>
199        _LIBCPP_INLINE_VISIBILITY
200        stack(stack&& __s, const _Alloc& __a,
201              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
202            : c(_VSTD::move(__s.c), __a) {}
203#endif // _LIBCPP_CXX03_LANG
204
205#if _LIBCPP_STD_VER > 20
206    template <class _InputIterator,
207              class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
208    _LIBCPP_HIDE_FROM_ABI
209    stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
210
211    template <class _InputIterator,
212              class _Alloc,
213              class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
214              class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
215    _LIBCPP_HIDE_FROM_ABI
216    stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
217#endif
218
219    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
220    bool empty()     const      {return c.empty();}
221    _LIBCPP_INLINE_VISIBILITY
222    size_type size() const      {return c.size();}
223    _LIBCPP_INLINE_VISIBILITY
224    reference top()             {return c.back();}
225    _LIBCPP_INLINE_VISIBILITY
226    const_reference top() const {return c.back();}
227
228    _LIBCPP_INLINE_VISIBILITY
229    void push(const value_type& __v) {c.push_back(__v);}
230#ifndef _LIBCPP_CXX03_LANG
231    _LIBCPP_INLINE_VISIBILITY
232    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
233
234    template <class... _Args>
235        _LIBCPP_INLINE_VISIBILITY
236#if _LIBCPP_STD_VER > 14
237        decltype(auto) emplace(_Args&&... __args)
238        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
239#else
240        void      emplace(_Args&&... __args)
241        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
242#endif
243#endif // _LIBCPP_CXX03_LANG
244
245    _LIBCPP_INLINE_VISIBILITY
246    void pop() {c.pop_back();}
247
248    _LIBCPP_INLINE_VISIBILITY
249    void swap(stack& __s)
250        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
251    {
252        using _VSTD::swap;
253        swap(c, __s.c);
254    }
255
256    template <class T1, class _C1>
257    friend
258    bool
259    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
260
261    template <class T1, class _C1>
262    friend
263    bool
264    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
265};
266
267#if _LIBCPP_STD_VER > 14
268template<class _Container,
269         class = enable_if_t<!__is_allocator<_Container>::value>
270>
271stack(_Container)
272    -> stack<typename _Container::value_type, _Container>;
273
274template<class _Container,
275         class _Alloc,
276         class = enable_if_t<!__is_allocator<_Container>::value>,
277         class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
278         >
279stack(_Container, _Alloc)
280    -> stack<typename _Container::value_type, _Container>;
281#endif
282
283#if _LIBCPP_STD_VER > 20
284template<class _InputIterator,
285         class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
286stack(_InputIterator, _InputIterator)
287    -> stack<__iter_value_type<_InputIterator>>;
288
289template<class _InputIterator,
290         class _Alloc,
291         class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
292         class = __enable_if_t<__is_allocator<_Alloc>::value>>
293stack(_InputIterator, _InputIterator, _Alloc)
294    -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
295#endif
296
297template <class _Tp, class _Container>
298inline _LIBCPP_INLINE_VISIBILITY
299bool
300operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
301{
302    return __x.c == __y.c;
303}
304
305template <class _Tp, class _Container>
306inline _LIBCPP_INLINE_VISIBILITY
307bool
308operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
309{
310    return __x.c < __y.c;
311}
312
313template <class _Tp, class _Container>
314inline _LIBCPP_INLINE_VISIBILITY
315bool
316operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
317{
318    return !(__x == __y);
319}
320
321template <class _Tp, class _Container>
322inline _LIBCPP_INLINE_VISIBILITY
323bool
324operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
325{
326    return __y < __x;
327}
328
329template <class _Tp, class _Container>
330inline _LIBCPP_INLINE_VISIBILITY
331bool
332operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
333{
334    return !(__x < __y);
335}
336
337template <class _Tp, class _Container>
338inline _LIBCPP_INLINE_VISIBILITY
339bool
340operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
341{
342    return !(__y < __x);
343}
344
345template <class _Tp, class _Container>
346inline _LIBCPP_INLINE_VISIBILITY
347__enable_if_t<__is_swappable<_Container>::value, void>
348swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
349    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
350{
351    __x.swap(__y);
352}
353
354template <class _Tp, class _Container, class _Alloc>
355struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
356    : public uses_allocator<_Container, _Alloc>
357{
358};
359
360_LIBCPP_END_NAMESPACE_STD
361
362#endif // _LIBCPP_STACK
363