xref: /llvm-project-15.0.7/libcxx/include/stack (revision 199d2ebe)
13e519524SHoward Hinnant// -*- C++ -*-
23e519524SHoward Hinnant//===---------------------------- stack -----------------------------------===//
33e519524SHoward Hinnant//
457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
73e519524SHoward Hinnant//
83e519524SHoward Hinnant//===----------------------------------------------------------------------===//
93e519524SHoward Hinnant
103e519524SHoward Hinnant#ifndef _LIBCPP_STACK
113e519524SHoward Hinnant#define _LIBCPP_STACK
123e519524SHoward Hinnant
133e519524SHoward Hinnant/*
143e519524SHoward Hinnant    stack synopsis
153e519524SHoward Hinnant
163e519524SHoward Hinnantnamespace std
173e519524SHoward Hinnant{
183e519524SHoward Hinnant
193e519524SHoward Hinnanttemplate <class T, class Container = deque<T>>
203e519524SHoward Hinnantclass stack
213e519524SHoward Hinnant{
223e519524SHoward Hinnantpublic:
233e519524SHoward Hinnant    typedef Container                                container_type;
243e519524SHoward Hinnant    typedef typename container_type::value_type      value_type;
253e519524SHoward Hinnant    typedef typename container_type::reference       reference;
263e519524SHoward Hinnant    typedef typename container_type::const_reference const_reference;
273e519524SHoward Hinnant    typedef typename container_type::size_type       size_type;
283e519524SHoward Hinnant
293e519524SHoward Hinnantprotected:
303e519524SHoward Hinnant    container_type c;
313e519524SHoward Hinnant
323e519524SHoward Hinnantpublic:
33bd0c1600SHoward Hinnant    stack() = default;
34bd0c1600SHoward Hinnant    ~stack() = default;
35bd0c1600SHoward Hinnant
36bd0c1600SHoward Hinnant    stack(const stack& q) = default;
37bd0c1600SHoward Hinnant    stack(stack&& q) = default;
38bd0c1600SHoward Hinnant
39bd0c1600SHoward Hinnant    stack& operator=(const stack& q) = default;
40bd0c1600SHoward Hinnant    stack& operator=(stack&& q) = default;
41bd0c1600SHoward Hinnant
423e519524SHoward Hinnant    explicit stack(const container_type& c);
433e519524SHoward Hinnant    explicit stack(container_type&& c);
443e519524SHoward Hinnant    template <class Alloc> explicit stack(const Alloc& a);
453e519524SHoward Hinnant    template <class Alloc> stack(const container_type& c, const Alloc& a);
463e519524SHoward Hinnant    template <class Alloc> stack(container_type&& c, const Alloc& a);
47bd0c1600SHoward Hinnant    template <class Alloc> stack(const stack& c, const Alloc& a);
483e519524SHoward Hinnant    template <class Alloc> stack(stack&& c, const Alloc& a);
493e519524SHoward Hinnant
503e519524SHoward Hinnant    bool empty() const;
513e519524SHoward Hinnant    size_type size() const;
523e519524SHoward Hinnant    reference top();
533e519524SHoward Hinnant    const_reference top() const;
543e519524SHoward Hinnant
553e519524SHoward Hinnant    void push(const value_type& x);
563e519524SHoward Hinnant    void push(value_type&& x);
5763b560beSMarshall Clow    template <class... Args> reference emplace(Args&&... args); // reference in C++17
583e519524SHoward Hinnant    void pop();
593e519524SHoward Hinnant
60f07dd8d0SEric Fiselier    void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
613e519524SHoward Hinnant};
623e519524SHoward Hinnant
635b8b8b5dSMarshall Clowtemplate<class Container>
645b8b8b5dSMarshall Clow  stack(Container) -> stack<typename Container::value_type, Container>;  // C++17
655b8b8b5dSMarshall Clow
665b8b8b5dSMarshall Clowtemplate<class Container, class Allocator>
675b8b8b5dSMarshall Clow  stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
685b8b8b5dSMarshall Clow
693e519524SHoward Hinnanttemplate <class T, class Container>
703e519524SHoward Hinnant  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
713e519524SHoward Hinnanttemplate <class T, class Container>
723e519524SHoward Hinnant  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
733e519524SHoward Hinnanttemplate <class T, class Container>
743e519524SHoward Hinnant  bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
753e519524SHoward Hinnanttemplate <class T, class Container>
763e519524SHoward Hinnant  bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
773e519524SHoward Hinnanttemplate <class T, class Container>
783e519524SHoward Hinnant  bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
793e519524SHoward Hinnanttemplate <class T, class Container>
803e519524SHoward Hinnant  bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
813e519524SHoward Hinnant
823e519524SHoward Hinnanttemplate <class T, class Container>
83bd0c1600SHoward Hinnant  void swap(stack<T, Container>& x, stack<T, Container>& y)
84bd0c1600SHoward Hinnant  noexcept(noexcept(x.swap(y)));
853e519524SHoward Hinnant
863e519524SHoward Hinnant}  // std
873e519524SHoward Hinnant
883e519524SHoward Hinnant*/
893e519524SHoward Hinnant
903e519524SHoward Hinnant#include <__config>
913e519524SHoward Hinnant#include <deque>
923e519524SHoward Hinnant
93073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
943e519524SHoward Hinnant#pragma GCC system_header
95073458b1SHoward Hinnant#endif
963e519524SHoward Hinnant
973e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
983e519524SHoward Hinnant
99e2f2d1edSEric Fiseliertemplate <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
1003e519524SHoward Hinnant
1013e519524SHoward Hinnanttemplate <class _Tp, class _Container>
102aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY
1033e519524SHoward Hinnantbool
1043e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1053e519524SHoward Hinnant
1063e519524SHoward Hinnanttemplate <class _Tp, class _Container>
107aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY
1083e519524SHoward Hinnantbool
1093e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1103e519524SHoward Hinnant
1113afa22a3SMarshall Clowtemplate <class _Tp, class _Container /*= deque<_Tp>*/>
112e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS stack
1133e519524SHoward Hinnant{
1143e519524SHoward Hinnantpublic:
1153e519524SHoward Hinnant    typedef _Container                               container_type;
1163e519524SHoward Hinnant    typedef typename container_type::value_type      value_type;
1173e519524SHoward Hinnant    typedef typename container_type::reference       reference;
1183e519524SHoward Hinnant    typedef typename container_type::const_reference const_reference;
1193e519524SHoward Hinnant    typedef typename container_type::size_type       size_type;
120c1fe2c43SMarshall Clow    static_assert((is_same<_Tp, value_type>::value), "" );
1213e519524SHoward Hinnant
1223e519524SHoward Hinnantprotected:
1233e519524SHoward Hinnant    container_type c;
1243e519524SHoward Hinnant
1253e519524SHoward Hinnantpublic:
126e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
127bd0c1600SHoward Hinnant    stack()
128bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
129bd0c1600SHoward Hinnant        : c() {}
130bd0c1600SHoward Hinnant
131bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
132bd0c1600SHoward Hinnant    stack(const stack& __q) : c(__q.c) {}
133bd0c1600SHoward Hinnant
1347196ee31SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
1357196ee31SEric Fiselier    stack& operator=(const stack& __q) {c = __q.c; return *this;}
1367196ee31SEric Fiselier
1377196ee31SEric Fiselier
1387196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
139bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
140bd0c1600SHoward Hinnant    stack(stack&& __q)
141bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
142ce48a113SHoward Hinnant        : c(_VSTD::move(__q.c)) {}
143bd0c1600SHoward Hinnant
144bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
145bd0c1600SHoward Hinnant    stack& operator=(stack&& __q)
146bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
147ce48a113SHoward Hinnant        {c = _VSTD::move(__q.c); return *this;}
1487196ee31SEric Fiselier
1497196ee31SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
1507196ee31SEric Fiselier    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
1517196ee31SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
152bd0c1600SHoward Hinnant
153e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1543e519524SHoward Hinnant    explicit stack(const container_type& __c) : c(__c) {}
1557196ee31SEric Fiselier
1563e519524SHoward Hinnant    template <class _Alloc>
157e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1583e519524SHoward Hinnant        explicit stack(const _Alloc& __a,
159*199d2ebeSArthur O'Dwyer                       _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
1603e519524SHoward Hinnant            : c(__a) {}
1613e519524SHoward Hinnant    template <class _Alloc>
162e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1633e519524SHoward Hinnant        stack(const container_type& __c, const _Alloc& __a,
164*199d2ebeSArthur O'Dwyer              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
1653e519524SHoward Hinnant            : c(__c, __a) {}
1663e519524SHoward Hinnant    template <class _Alloc>
167e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1683e519524SHoward Hinnant        stack(const stack& __s, const _Alloc& __a,
169*199d2ebeSArthur O'Dwyer              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
1703e519524SHoward Hinnant            : c(__s.c, __a) {}
1717196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
1723e519524SHoward Hinnant    template <class _Alloc>
173e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1743e519524SHoward Hinnant        stack(container_type&& __c, const _Alloc& __a,
175*199d2ebeSArthur O'Dwyer              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
176ce48a113SHoward Hinnant            : c(_VSTD::move(__c), __a) {}
1773e519524SHoward Hinnant    template <class _Alloc>
178e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1793e519524SHoward Hinnant        stack(stack&& __s, const _Alloc& __a,
180*199d2ebeSArthur O'Dwyer              _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0)
181ce48a113SHoward Hinnant            : c(_VSTD::move(__s.c), __a) {}
1827196ee31SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
1833e519524SHoward Hinnant
18472c8fad4SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1853e519524SHoward Hinnant    bool empty()     const      {return c.empty();}
186e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1873e519524SHoward Hinnant    size_type size() const      {return c.size();}
188e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1893e519524SHoward Hinnant    reference top()             {return c.back();}
190e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1913e519524SHoward Hinnant    const_reference top() const {return c.back();}
1923e519524SHoward Hinnant
193e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1943e519524SHoward Hinnant    void push(const value_type& __v) {c.push_back(__v);}
1957196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
196e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
197ce48a113SHoward Hinnant    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
1987196ee31SEric Fiselier
199e0601335SHoward Hinnant    template <class... _Args>
200e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
20163b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
202e34f5ffeSMarshall Clow        decltype(auto) emplace(_Args&&... __args)
2030e411641SEric Fiselier        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
20463b560beSMarshall Clow#else
20563b560beSMarshall Clow        void      emplace(_Args&&... __args)
20663b560beSMarshall Clow        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
20763b560beSMarshall Clow#endif
2087196ee31SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
2097196ee31SEric Fiselier
210e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2113e519524SHoward Hinnant    void pop() {c.pop_back();}
2123e519524SHoward Hinnant
213e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2143e519524SHoward Hinnant    void swap(stack& __s)
215bd0c1600SHoward Hinnant        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
2163e519524SHoward Hinnant    {
217ce48a113SHoward Hinnant        using _VSTD::swap;
2183e519524SHoward Hinnant        swap(c, __s.c);
2193e519524SHoward Hinnant    }
2203e519524SHoward Hinnant
2213e519524SHoward Hinnant    template <class T1, class _C1>
2223e519524SHoward Hinnant    friend
2233e519524SHoward Hinnant    bool
2243e519524SHoward Hinnant    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2253e519524SHoward Hinnant
2263e519524SHoward Hinnant    template <class T1, class _C1>
2273e519524SHoward Hinnant    friend
2283e519524SHoward Hinnant    bool
2293e519524SHoward Hinnant    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2303e519524SHoward Hinnant};
2313e519524SHoward Hinnant
2325b8b8b5dSMarshall Clow#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2335b8b8b5dSMarshall Clowtemplate<class _Container,
234*199d2ebeSArthur O'Dwyer         class = _EnableIf<!__is_allocator<_Container>::value>
2355b8b8b5dSMarshall Clow>
2365b8b8b5dSMarshall Clowstack(_Container)
2375b8b8b5dSMarshall Clow    -> stack<typename _Container::value_type, _Container>;
2385b8b8b5dSMarshall Clow
2395b8b8b5dSMarshall Clowtemplate<class _Container,
2405b8b8b5dSMarshall Clow         class _Alloc,
241*199d2ebeSArthur O'Dwyer         class = _EnableIf<!__is_allocator<_Container>::value>,
242*199d2ebeSArthur O'Dwyer         class = _EnableIf<__is_allocator<_Alloc>::value>
2435b8b8b5dSMarshall Clow         >
2445b8b8b5dSMarshall Clowstack(_Container, _Alloc)
2455b8b8b5dSMarshall Clow    -> stack<typename _Container::value_type, _Container>;
2465b8b8b5dSMarshall Clow#endif
2475b8b8b5dSMarshall Clow
2483e519524SHoward Hinnanttemplate <class _Tp, class _Container>
249e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2503e519524SHoward Hinnantbool
2513e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2523e519524SHoward Hinnant{
2533e519524SHoward Hinnant    return __x.c == __y.c;
2543e519524SHoward Hinnant}
2553e519524SHoward Hinnant
2563e519524SHoward Hinnanttemplate <class _Tp, class _Container>
257e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2583e519524SHoward Hinnantbool
2593e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2603e519524SHoward Hinnant{
2613e519524SHoward Hinnant    return __x.c < __y.c;
2623e519524SHoward Hinnant}
2633e519524SHoward Hinnant
2643e519524SHoward Hinnanttemplate <class _Tp, class _Container>
265e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2663e519524SHoward Hinnantbool
2673e519524SHoward Hinnantoperator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2683e519524SHoward Hinnant{
2693e519524SHoward Hinnant    return !(__x == __y);
2703e519524SHoward Hinnant}
2713e519524SHoward Hinnant
2723e519524SHoward Hinnanttemplate <class _Tp, class _Container>
273e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2743e519524SHoward Hinnantbool
2753e519524SHoward Hinnantoperator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2763e519524SHoward Hinnant{
2773e519524SHoward Hinnant    return __y < __x;
2783e519524SHoward Hinnant}
2793e519524SHoward Hinnant
2803e519524SHoward Hinnanttemplate <class _Tp, class _Container>
281e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2823e519524SHoward Hinnantbool
2833e519524SHoward Hinnantoperator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2843e519524SHoward Hinnant{
2853e519524SHoward Hinnant    return !(__x < __y);
2863e519524SHoward Hinnant}
2873e519524SHoward Hinnant
2883e519524SHoward Hinnanttemplate <class _Tp, class _Container>
289e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2903e519524SHoward Hinnantbool
2913e519524SHoward Hinnantoperator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2923e519524SHoward Hinnant{
2933e519524SHoward Hinnant    return !(__y < __x);
2943e519524SHoward Hinnant}
2953e519524SHoward Hinnant
2963e519524SHoward Hinnanttemplate <class _Tp, class _Container>
297e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
298*199d2ebeSArthur O'Dwyer_EnableIf<__is_swappable<_Container>::value, void>
2993e519524SHoward Hinnantswap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
300bd0c1600SHoward Hinnant    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
3013e519524SHoward Hinnant{
3023e519524SHoward Hinnant    __x.swap(__y);
3033e519524SHoward Hinnant}
3043e519524SHoward Hinnant
3053e519524SHoward Hinnanttemplate <class _Tp, class _Container, class _Alloc>
306e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
3073e519524SHoward Hinnant    : public uses_allocator<_Container, _Alloc>
3083e519524SHoward Hinnant{
3093e519524SHoward Hinnant};
3103e519524SHoward Hinnant
3113e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
3123e519524SHoward Hinnant
3133e519524SHoward Hinnant#endif  // _LIBCPP_STACK
314