xref: /llvm-project-15.0.7/libcxx/include/stack (revision bd6e6846)
13e519524SHoward Hinnant// -*- C++ -*-
2eb8650a7SLouis Dionne//===----------------------------------------------------------------------===//
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>
91050b064fSChristopher Di Bella#include <__memory/uses_allocator.h>
926adbc83eSChristopher Di Bella#include <__utility/forward.h>
933e519524SHoward Hinnant#include <deque>
94*bd6e6846SMark de Wever#include <version>
953e519524SHoward Hinnant
96073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
973e519524SHoward Hinnant#pragma GCC system_header
98073458b1SHoward Hinnant#endif
993e519524SHoward Hinnant
1003e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
1013e519524SHoward Hinnant
102e2f2d1edSEric Fiseliertemplate <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
1033e519524SHoward Hinnant
1043e519524SHoward Hinnanttemplate <class _Tp, class _Container>
105aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY
1063e519524SHoward Hinnantbool
1073e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1083e519524SHoward Hinnant
1093e519524SHoward Hinnanttemplate <class _Tp, class _Container>
110aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY
1113e519524SHoward Hinnantbool
1123e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1133e519524SHoward Hinnant
1143afa22a3SMarshall Clowtemplate <class _Tp, class _Container /*= deque<_Tp>*/>
115e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS stack
1163e519524SHoward Hinnant{
1173e519524SHoward Hinnantpublic:
1183e519524SHoward Hinnant    typedef _Container                               container_type;
1193e519524SHoward Hinnant    typedef typename container_type::value_type      value_type;
1203e519524SHoward Hinnant    typedef typename container_type::reference       reference;
1213e519524SHoward Hinnant    typedef typename container_type::const_reference const_reference;
1223e519524SHoward Hinnant    typedef typename container_type::size_type       size_type;
123c1fe2c43SMarshall Clow    static_assert((is_same<_Tp, value_type>::value), "" );
1243e519524SHoward Hinnant
1253e519524SHoward Hinnantprotected:
1263e519524SHoward Hinnant    container_type c;
1273e519524SHoward Hinnant
1283e519524SHoward Hinnantpublic:
129e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
130bd0c1600SHoward Hinnant    stack()
131bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
132bd0c1600SHoward Hinnant        : c() {}
133bd0c1600SHoward Hinnant
134bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
135bd0c1600SHoward Hinnant    stack(const stack& __q) : c(__q.c) {}
136bd0c1600SHoward Hinnant
1377196ee31SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
1387196ee31SEric Fiselier    stack& operator=(const stack& __q) {c = __q.c; return *this;}
1397196ee31SEric Fiselier
1407196ee31SEric Fiselier
1417196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
142bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
143bd0c1600SHoward Hinnant    stack(stack&& __q)
144bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
145ce48a113SHoward Hinnant        : c(_VSTD::move(__q.c)) {}
146bd0c1600SHoward Hinnant
147bd0c1600SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
148bd0c1600SHoward Hinnant    stack& operator=(stack&& __q)
149bd0c1600SHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
150ce48a113SHoward Hinnant        {c = _VSTD::move(__q.c); return *this;}
1517196ee31SEric Fiselier
1527196ee31SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
1537196ee31SEric Fiselier    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
1547196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG
155bd0c1600SHoward Hinnant
156e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1573e519524SHoward Hinnant    explicit stack(const container_type& __c) : c(__c) {}
1587196ee31SEric Fiselier
1593e519524SHoward Hinnant    template <class _Alloc>
160e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1613e519524SHoward Hinnant        explicit stack(const _Alloc& __a,
162b4e88d4dSLouis Dionne                       __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1633e519524SHoward Hinnant            : c(__a) {}
1643e519524SHoward Hinnant    template <class _Alloc>
165e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1663e519524SHoward Hinnant        stack(const container_type& __c, const _Alloc& __a,
167b4e88d4dSLouis Dionne              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1683e519524SHoward Hinnant            : c(__c, __a) {}
1693e519524SHoward Hinnant    template <class _Alloc>
170e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1713e519524SHoward Hinnant        stack(const stack& __s, const _Alloc& __a,
172b4e88d4dSLouis Dionne              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1733e519524SHoward Hinnant            : c(__s.c, __a) {}
1747196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
1753e519524SHoward Hinnant    template <class _Alloc>
176e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1773e519524SHoward Hinnant        stack(container_type&& __c, const _Alloc& __a,
178b4e88d4dSLouis Dionne              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
179ce48a113SHoward Hinnant            : c(_VSTD::move(__c), __a) {}
1803e519524SHoward Hinnant    template <class _Alloc>
181e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1823e519524SHoward Hinnant        stack(stack&& __s, const _Alloc& __a,
183b4e88d4dSLouis Dionne              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
184ce48a113SHoward Hinnant            : c(_VSTD::move(__s.c), __a) {}
1857196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG
1863e519524SHoward Hinnant
18772c8fad4SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1883e519524SHoward Hinnant    bool empty()     const      {return c.empty();}
189e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1903e519524SHoward Hinnant    size_type size() const      {return c.size();}
191e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1923e519524SHoward Hinnant    reference top()             {return c.back();}
193e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1943e519524SHoward Hinnant    const_reference top() const {return c.back();}
1953e519524SHoward Hinnant
196e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1973e519524SHoward Hinnant    void push(const value_type& __v) {c.push_back(__v);}
1987196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
199e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
200ce48a113SHoward Hinnant    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
2017196ee31SEric Fiselier
202e0601335SHoward Hinnant    template <class... _Args>
203e0601335SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
20463b560beSMarshall Clow#if _LIBCPP_STD_VER > 14
205e34f5ffeSMarshall Clow        decltype(auto) emplace(_Args&&... __args)
2060e411641SEric Fiselier        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
20763b560beSMarshall Clow#else
20863b560beSMarshall Clow        void      emplace(_Args&&... __args)
20963b560beSMarshall Clow        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
21063b560beSMarshall Clow#endif
2117196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG
2127196ee31SEric Fiselier
213e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2143e519524SHoward Hinnant    void pop() {c.pop_back();}
2153e519524SHoward Hinnant
216e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2173e519524SHoward Hinnant    void swap(stack& __s)
218bd0c1600SHoward Hinnant        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
2193e519524SHoward Hinnant    {
220ce48a113SHoward Hinnant        using _VSTD::swap;
2213e519524SHoward Hinnant        swap(c, __s.c);
2223e519524SHoward Hinnant    }
2233e519524SHoward Hinnant
2243e519524SHoward Hinnant    template <class T1, class _C1>
2253e519524SHoward Hinnant    friend
2263e519524SHoward Hinnant    bool
2273e519524SHoward Hinnant    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2283e519524SHoward Hinnant
2293e519524SHoward Hinnant    template <class T1, class _C1>
2303e519524SHoward Hinnant    friend
2313e519524SHoward Hinnant    bool
2323e519524SHoward Hinnant    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2333e519524SHoward Hinnant};
2343e519524SHoward Hinnant
23501666904SLouis Dionne#if _LIBCPP_STD_VER >= 17
2365b8b8b5dSMarshall Clowtemplate<class _Container,
2374e0ea2cfSLouis Dionne         class = enable_if_t<!__is_allocator<_Container>::value>
2385b8b8b5dSMarshall Clow>
2395b8b8b5dSMarshall Clowstack(_Container)
2405b8b8b5dSMarshall Clow    -> stack<typename _Container::value_type, _Container>;
2415b8b8b5dSMarshall Clow
2425b8b8b5dSMarshall Clowtemplate<class _Container,
2435b8b8b5dSMarshall Clow         class _Alloc,
2444e0ea2cfSLouis Dionne         class = enable_if_t<!__is_allocator<_Container>::value>,
2454e0ea2cfSLouis Dionne         class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
2465b8b8b5dSMarshall Clow         >
2475b8b8b5dSMarshall Clowstack(_Container, _Alloc)
2485b8b8b5dSMarshall Clow    -> stack<typename _Container::value_type, _Container>;
2495b8b8b5dSMarshall Clow#endif
2505b8b8b5dSMarshall Clow
2513e519524SHoward Hinnanttemplate <class _Tp, class _Container>
252e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2533e519524SHoward Hinnantbool
2543e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2553e519524SHoward Hinnant{
2563e519524SHoward Hinnant    return __x.c == __y.c;
2573e519524SHoward Hinnant}
2583e519524SHoward Hinnant
2593e519524SHoward Hinnanttemplate <class _Tp, class _Container>
260e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2613e519524SHoward Hinnantbool
2623e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2633e519524SHoward Hinnant{
2643e519524SHoward Hinnant    return __x.c < __y.c;
2653e519524SHoward Hinnant}
2663e519524SHoward Hinnant
2673e519524SHoward Hinnanttemplate <class _Tp, class _Container>
268e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2693e519524SHoward Hinnantbool
2703e519524SHoward Hinnantoperator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2713e519524SHoward Hinnant{
2723e519524SHoward Hinnant    return !(__x == __y);
2733e519524SHoward Hinnant}
2743e519524SHoward Hinnant
2753e519524SHoward Hinnanttemplate <class _Tp, class _Container>
276e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2773e519524SHoward Hinnantbool
2783e519524SHoward Hinnantoperator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2793e519524SHoward Hinnant{
2803e519524SHoward Hinnant    return __y < __x;
2813e519524SHoward Hinnant}
2823e519524SHoward Hinnant
2833e519524SHoward Hinnanttemplate <class _Tp, class _Container>
284e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2853e519524SHoward Hinnantbool
2863e519524SHoward Hinnantoperator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2873e519524SHoward Hinnant{
2883e519524SHoward Hinnant    return !(__x < __y);
2893e519524SHoward Hinnant}
2903e519524SHoward Hinnant
2913e519524SHoward Hinnanttemplate <class _Tp, class _Container>
292e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2933e519524SHoward Hinnantbool
2943e519524SHoward Hinnantoperator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2953e519524SHoward Hinnant{
2963e519524SHoward Hinnant    return !(__y < __x);
2973e519524SHoward Hinnant}
2983e519524SHoward Hinnant
2993e519524SHoward Hinnanttemplate <class _Tp, class _Container>
300e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
301b4e88d4dSLouis Dionne__enable_if_t<__is_swappable<_Container>::value, void>
3023e519524SHoward Hinnantswap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
303bd0c1600SHoward Hinnant    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
3043e519524SHoward Hinnant{
3053e519524SHoward Hinnant    __x.swap(__y);
3063e519524SHoward Hinnant}
3073e519524SHoward Hinnant
3083e519524SHoward Hinnanttemplate <class _Tp, class _Container, class _Alloc>
309e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
3103e519524SHoward Hinnant    : public uses_allocator<_Container, _Alloc>
3113e519524SHoward Hinnant{
3123e519524SHoward Hinnant};
3133e519524SHoward Hinnant
3143e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
3153e519524SHoward Hinnant
3163e519524SHoward Hinnant#endif // _LIBCPP_STACK
317