xref: /netbsd-src/external/apache2/llvm/dist/libcxx/include/stack (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1*4d6fc14bSjoerg// -*- C++ -*-
2*4d6fc14bSjoerg//===---------------------------- stack -----------------------------------===//
3*4d6fc14bSjoerg//
4*4d6fc14bSjoerg// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4d6fc14bSjoerg// See https://llvm.org/LICENSE.txt for license information.
6*4d6fc14bSjoerg// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4d6fc14bSjoerg//
8*4d6fc14bSjoerg//===----------------------------------------------------------------------===//
9*4d6fc14bSjoerg
10*4d6fc14bSjoerg#ifndef _LIBCPP_STACK
11*4d6fc14bSjoerg#define _LIBCPP_STACK
12*4d6fc14bSjoerg
13*4d6fc14bSjoerg/*
14*4d6fc14bSjoerg    stack synopsis
15*4d6fc14bSjoerg
16*4d6fc14bSjoergnamespace std
17*4d6fc14bSjoerg{
18*4d6fc14bSjoerg
19*4d6fc14bSjoergtemplate <class T, class Container = deque<T>>
20*4d6fc14bSjoergclass stack
21*4d6fc14bSjoerg{
22*4d6fc14bSjoergpublic:
23*4d6fc14bSjoerg    typedef Container                                container_type;
24*4d6fc14bSjoerg    typedef typename container_type::value_type      value_type;
25*4d6fc14bSjoerg    typedef typename container_type::reference       reference;
26*4d6fc14bSjoerg    typedef typename container_type::const_reference const_reference;
27*4d6fc14bSjoerg    typedef typename container_type::size_type       size_type;
28*4d6fc14bSjoerg
29*4d6fc14bSjoergprotected:
30*4d6fc14bSjoerg    container_type c;
31*4d6fc14bSjoerg
32*4d6fc14bSjoergpublic:
33*4d6fc14bSjoerg    stack() = default;
34*4d6fc14bSjoerg    ~stack() = default;
35*4d6fc14bSjoerg
36*4d6fc14bSjoerg    stack(const stack& q) = default;
37*4d6fc14bSjoerg    stack(stack&& q) = default;
38*4d6fc14bSjoerg
39*4d6fc14bSjoerg    stack& operator=(const stack& q) = default;
40*4d6fc14bSjoerg    stack& operator=(stack&& q) = default;
41*4d6fc14bSjoerg
42*4d6fc14bSjoerg    explicit stack(const container_type& c);
43*4d6fc14bSjoerg    explicit stack(container_type&& c);
44*4d6fc14bSjoerg    template <class Alloc> explicit stack(const Alloc& a);
45*4d6fc14bSjoerg    template <class Alloc> stack(const container_type& c, const Alloc& a);
46*4d6fc14bSjoerg    template <class Alloc> stack(container_type&& c, const Alloc& a);
47*4d6fc14bSjoerg    template <class Alloc> stack(const stack& c, const Alloc& a);
48*4d6fc14bSjoerg    template <class Alloc> stack(stack&& c, const Alloc& a);
49*4d6fc14bSjoerg
50*4d6fc14bSjoerg    bool empty() const;
51*4d6fc14bSjoerg    size_type size() const;
52*4d6fc14bSjoerg    reference top();
53*4d6fc14bSjoerg    const_reference top() const;
54*4d6fc14bSjoerg
55*4d6fc14bSjoerg    void push(const value_type& x);
56*4d6fc14bSjoerg    void push(value_type&& x);
57*4d6fc14bSjoerg    template <class... Args> reference emplace(Args&&... args); // reference in C++17
58*4d6fc14bSjoerg    void pop();
59*4d6fc14bSjoerg
60*4d6fc14bSjoerg    void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
61*4d6fc14bSjoerg};
62*4d6fc14bSjoerg
63*4d6fc14bSjoergtemplate<class Container>
64*4d6fc14bSjoerg  stack(Container) -> stack<typename Container::value_type, Container>;  // C++17
65*4d6fc14bSjoerg
66*4d6fc14bSjoergtemplate<class Container, class Allocator>
67*4d6fc14bSjoerg  stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
68*4d6fc14bSjoerg
69*4d6fc14bSjoergtemplate <class T, class Container>
70*4d6fc14bSjoerg  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
71*4d6fc14bSjoergtemplate <class T, class Container>
72*4d6fc14bSjoerg  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
73*4d6fc14bSjoergtemplate <class T, class Container>
74*4d6fc14bSjoerg  bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
75*4d6fc14bSjoergtemplate <class T, class Container>
76*4d6fc14bSjoerg  bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
77*4d6fc14bSjoergtemplate <class T, class Container>
78*4d6fc14bSjoerg  bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
79*4d6fc14bSjoergtemplate <class T, class Container>
80*4d6fc14bSjoerg  bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
81*4d6fc14bSjoerg
82*4d6fc14bSjoergtemplate <class T, class Container>
83*4d6fc14bSjoerg  void swap(stack<T, Container>& x, stack<T, Container>& y)
84*4d6fc14bSjoerg  noexcept(noexcept(x.swap(y)));
85*4d6fc14bSjoerg
86*4d6fc14bSjoerg}  // std
87*4d6fc14bSjoerg
88*4d6fc14bSjoerg*/
89*4d6fc14bSjoerg
90*4d6fc14bSjoerg#include <__config>
91*4d6fc14bSjoerg#include <deque>
92*4d6fc14bSjoerg
93*4d6fc14bSjoerg#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94*4d6fc14bSjoerg#pragma GCC system_header
95*4d6fc14bSjoerg#endif
96*4d6fc14bSjoerg
97*4d6fc14bSjoerg_LIBCPP_BEGIN_NAMESPACE_STD
98*4d6fc14bSjoerg
99*4d6fc14bSjoergtemplate <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
100*4d6fc14bSjoerg
101*4d6fc14bSjoergtemplate <class _Tp, class _Container>
102*4d6fc14bSjoerg_LIBCPP_INLINE_VISIBILITY
103*4d6fc14bSjoergbool
104*4d6fc14bSjoergoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
105*4d6fc14bSjoerg
106*4d6fc14bSjoergtemplate <class _Tp, class _Container>
107*4d6fc14bSjoerg_LIBCPP_INLINE_VISIBILITY
108*4d6fc14bSjoergbool
109*4d6fc14bSjoergoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
110*4d6fc14bSjoerg
111*4d6fc14bSjoergtemplate <class _Tp, class _Container /*= deque<_Tp>*/>
112*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS stack
113*4d6fc14bSjoerg{
114*4d6fc14bSjoergpublic:
115*4d6fc14bSjoerg    typedef _Container                               container_type;
116*4d6fc14bSjoerg    typedef typename container_type::value_type      value_type;
117*4d6fc14bSjoerg    typedef typename container_type::reference       reference;
118*4d6fc14bSjoerg    typedef typename container_type::const_reference const_reference;
119*4d6fc14bSjoerg    typedef typename container_type::size_type       size_type;
120*4d6fc14bSjoerg    static_assert((is_same<_Tp, value_type>::value), "" );
121*4d6fc14bSjoerg
122*4d6fc14bSjoergprotected:
123*4d6fc14bSjoerg    container_type c;
124*4d6fc14bSjoerg
125*4d6fc14bSjoergpublic:
126*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
127*4d6fc14bSjoerg    stack()
128*4d6fc14bSjoerg        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
129*4d6fc14bSjoerg        : c() {}
130*4d6fc14bSjoerg
131*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
132*4d6fc14bSjoerg    stack(const stack& __q) : c(__q.c) {}
133*4d6fc14bSjoerg
134*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
135*4d6fc14bSjoerg    stack& operator=(const stack& __q) {c = __q.c; return *this;}
136*4d6fc14bSjoerg
137*4d6fc14bSjoerg
138*4d6fc14bSjoerg#ifndef _LIBCPP_CXX03_LANG
139*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
140*4d6fc14bSjoerg    stack(stack&& __q)
141*4d6fc14bSjoerg        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
142*4d6fc14bSjoerg        : c(_VSTD::move(__q.c)) {}
143*4d6fc14bSjoerg
144*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
145*4d6fc14bSjoerg    stack& operator=(stack&& __q)
146*4d6fc14bSjoerg        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
147*4d6fc14bSjoerg        {c = _VSTD::move(__q.c); return *this;}
148*4d6fc14bSjoerg
149*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
150*4d6fc14bSjoerg    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
151*4d6fc14bSjoerg#endif // _LIBCPP_CXX03_LANG
152*4d6fc14bSjoerg
153*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
154*4d6fc14bSjoerg    explicit stack(const container_type& __c) : c(__c) {}
155*4d6fc14bSjoerg
156*4d6fc14bSjoerg    template <class _Alloc>
157*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
158*4d6fc14bSjoerg        explicit stack(const _Alloc& __a,
159*4d6fc14bSjoerg                       _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
160*4d6fc14bSjoerg            : c(__a) {}
161*4d6fc14bSjoerg    template <class _Alloc>
162*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
163*4d6fc14bSjoerg        stack(const container_type& __c, const _Alloc& __a,
164*4d6fc14bSjoerg              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
165*4d6fc14bSjoerg            : c(__c, __a) {}
166*4d6fc14bSjoerg    template <class _Alloc>
167*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
168*4d6fc14bSjoerg        stack(const stack& __s, const _Alloc& __a,
169*4d6fc14bSjoerg              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
170*4d6fc14bSjoerg            : c(__s.c, __a) {}
171*4d6fc14bSjoerg#ifndef _LIBCPP_CXX03_LANG
172*4d6fc14bSjoerg    template <class _Alloc>
173*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
174*4d6fc14bSjoerg        stack(container_type&& __c, const _Alloc& __a,
175*4d6fc14bSjoerg              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
176*4d6fc14bSjoerg            : c(_VSTD::move(__c), __a) {}
177*4d6fc14bSjoerg    template <class _Alloc>
178*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
179*4d6fc14bSjoerg        stack(stack&& __s, const _Alloc& __a,
180*4d6fc14bSjoerg              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
181*4d6fc14bSjoerg            : c(_VSTD::move(__s.c), __a) {}
182*4d6fc14bSjoerg#endif // _LIBCPP_CXX03_LANG
183*4d6fc14bSjoerg
184*4d6fc14bSjoerg    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
185*4d6fc14bSjoerg    bool empty()     const      {return c.empty();}
186*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
187*4d6fc14bSjoerg    size_type size() const      {return c.size();}
188*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
189*4d6fc14bSjoerg    reference top()             {return c.back();}
190*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
191*4d6fc14bSjoerg    const_reference top() const {return c.back();}
192*4d6fc14bSjoerg
193*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
194*4d6fc14bSjoerg    void push(const value_type& __v) {c.push_back(__v);}
195*4d6fc14bSjoerg#ifndef _LIBCPP_CXX03_LANG
196*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
197*4d6fc14bSjoerg    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
198*4d6fc14bSjoerg
199*4d6fc14bSjoerg    template <class... _Args>
200*4d6fc14bSjoerg        _LIBCPP_INLINE_VISIBILITY
201*4d6fc14bSjoerg#if _LIBCPP_STD_VER > 14
202*4d6fc14bSjoerg        decltype(auto) emplace(_Args&&... __args)
203*4d6fc14bSjoerg        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
204*4d6fc14bSjoerg#else
205*4d6fc14bSjoerg        void      emplace(_Args&&... __args)
206*4d6fc14bSjoerg        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
207*4d6fc14bSjoerg#endif
208*4d6fc14bSjoerg#endif // _LIBCPP_CXX03_LANG
209*4d6fc14bSjoerg
210*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
211*4d6fc14bSjoerg    void pop() {c.pop_back();}
212*4d6fc14bSjoerg
213*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
214*4d6fc14bSjoerg    void swap(stack& __s)
215*4d6fc14bSjoerg        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
216*4d6fc14bSjoerg    {
217*4d6fc14bSjoerg        using _VSTD::swap;
218*4d6fc14bSjoerg        swap(c, __s.c);
219*4d6fc14bSjoerg    }
220*4d6fc14bSjoerg
221*4d6fc14bSjoerg    template <class T1, class _C1>
222*4d6fc14bSjoerg    friend
223*4d6fc14bSjoerg    bool
224*4d6fc14bSjoerg    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
225*4d6fc14bSjoerg
226*4d6fc14bSjoerg    template <class T1, class _C1>
227*4d6fc14bSjoerg    friend
228*4d6fc14bSjoerg    bool
229*4d6fc14bSjoerg    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
230*4d6fc14bSjoerg};
231*4d6fc14bSjoerg
232*4d6fc14bSjoerg#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
233*4d6fc14bSjoergtemplate<class _Container,
234*4d6fc14bSjoerg         class = _EnableIf<!__is_allocator<_Container>::value>
235*4d6fc14bSjoerg>
236*4d6fc14bSjoergstack(_Container)
237*4d6fc14bSjoerg    -> stack<typename _Container::value_type, _Container>;
238*4d6fc14bSjoerg
239*4d6fc14bSjoergtemplate<class _Container,
240*4d6fc14bSjoerg         class _Alloc,
241*4d6fc14bSjoerg         class = _EnableIf<!__is_allocator<_Container>::value>,
242*4d6fc14bSjoerg         class = _EnableIf<__is_allocator<_Alloc>::value>
243*4d6fc14bSjoerg         >
244*4d6fc14bSjoergstack(_Container, _Alloc)
245*4d6fc14bSjoerg    -> stack<typename _Container::value_type, _Container>;
246*4d6fc14bSjoerg#endif
247*4d6fc14bSjoerg
248*4d6fc14bSjoergtemplate <class _Tp, class _Container>
249*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
250*4d6fc14bSjoergbool
251*4d6fc14bSjoergoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
252*4d6fc14bSjoerg{
253*4d6fc14bSjoerg    return __x.c == __y.c;
254*4d6fc14bSjoerg}
255*4d6fc14bSjoerg
256*4d6fc14bSjoergtemplate <class _Tp, class _Container>
257*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
258*4d6fc14bSjoergbool
259*4d6fc14bSjoergoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
260*4d6fc14bSjoerg{
261*4d6fc14bSjoerg    return __x.c < __y.c;
262*4d6fc14bSjoerg}
263*4d6fc14bSjoerg
264*4d6fc14bSjoergtemplate <class _Tp, class _Container>
265*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
266*4d6fc14bSjoergbool
267*4d6fc14bSjoergoperator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
268*4d6fc14bSjoerg{
269*4d6fc14bSjoerg    return !(__x == __y);
270*4d6fc14bSjoerg}
271*4d6fc14bSjoerg
272*4d6fc14bSjoergtemplate <class _Tp, class _Container>
273*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
274*4d6fc14bSjoergbool
275*4d6fc14bSjoergoperator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
276*4d6fc14bSjoerg{
277*4d6fc14bSjoerg    return __y < __x;
278*4d6fc14bSjoerg}
279*4d6fc14bSjoerg
280*4d6fc14bSjoergtemplate <class _Tp, class _Container>
281*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
282*4d6fc14bSjoergbool
283*4d6fc14bSjoergoperator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
284*4d6fc14bSjoerg{
285*4d6fc14bSjoerg    return !(__x < __y);
286*4d6fc14bSjoerg}
287*4d6fc14bSjoerg
288*4d6fc14bSjoergtemplate <class _Tp, class _Container>
289*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
290*4d6fc14bSjoergbool
291*4d6fc14bSjoergoperator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
292*4d6fc14bSjoerg{
293*4d6fc14bSjoerg    return !(__y < __x);
294*4d6fc14bSjoerg}
295*4d6fc14bSjoerg
296*4d6fc14bSjoergtemplate <class _Tp, class _Container>
297*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
298*4d6fc14bSjoerg_EnableIf<__is_swappable<_Container>::value, void>
299*4d6fc14bSjoergswap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
300*4d6fc14bSjoerg    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
301*4d6fc14bSjoerg{
302*4d6fc14bSjoerg    __x.swap(__y);
303*4d6fc14bSjoerg}
304*4d6fc14bSjoerg
305*4d6fc14bSjoergtemplate <class _Tp, class _Container, class _Alloc>
306*4d6fc14bSjoergstruct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
307*4d6fc14bSjoerg    : public uses_allocator<_Container, _Alloc>
308*4d6fc14bSjoerg{
309*4d6fc14bSjoerg};
310*4d6fc14bSjoerg
311*4d6fc14bSjoerg_LIBCPP_END_NAMESPACE_STD
312*4d6fc14bSjoerg
313*4d6fc14bSjoerg#endif // _LIBCPP_STACK
314