xref: /freebsd/contrib/llvm-project/libcxx/include/stack (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric// -*- C++ -*-
2*349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_STACK
110b57cec5SDimitry Andric#define _LIBCPP_STACK
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    stack synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class T, class Container = deque<T>>
200b57cec5SDimitry Andricclass stack
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric    typedef Container                                container_type;
240b57cec5SDimitry Andric    typedef typename container_type::value_type      value_type;
250b57cec5SDimitry Andric    typedef typename container_type::reference       reference;
260b57cec5SDimitry Andric    typedef typename container_type::const_reference const_reference;
270b57cec5SDimitry Andric    typedef typename container_type::size_type       size_type;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andricprotected:
300b57cec5SDimitry Andric    container_type c;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andricpublic:
330b57cec5SDimitry Andric    stack() = default;
340b57cec5SDimitry Andric    ~stack() = default;
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric    stack(const stack& q) = default;
370b57cec5SDimitry Andric    stack(stack&& q) = default;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric    stack& operator=(const stack& q) = default;
400b57cec5SDimitry Andric    stack& operator=(stack&& q) = default;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric    explicit stack(const container_type& c);
430b57cec5SDimitry Andric    explicit stack(container_type&& c);
440b57cec5SDimitry Andric    template <class Alloc> explicit stack(const Alloc& a);
450b57cec5SDimitry Andric    template <class Alloc> stack(const container_type& c, const Alloc& a);
460b57cec5SDimitry Andric    template <class Alloc> stack(container_type&& c, const Alloc& a);
470b57cec5SDimitry Andric    template <class Alloc> stack(const stack& c, const Alloc& a);
480b57cec5SDimitry Andric    template <class Alloc> stack(stack&& c, const Alloc& a);
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric    bool empty() const;
510b57cec5SDimitry Andric    size_type size() const;
520b57cec5SDimitry Andric    reference top();
530b57cec5SDimitry Andric    const_reference top() const;
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric    void push(const value_type& x);
560b57cec5SDimitry Andric    void push(value_type&& x);
570b57cec5SDimitry Andric    template <class... Args> reference emplace(Args&&... args); // reference in C++17
580b57cec5SDimitry Andric    void pop();
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric    void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
610b57cec5SDimitry Andric};
620b57cec5SDimitry Andric
630b57cec5SDimitry Andrictemplate<class Container>
640b57cec5SDimitry Andric  stack(Container) -> stack<typename Container::value_type, Container>;  // C++17
650b57cec5SDimitry Andric
660b57cec5SDimitry Andrictemplate<class Container, class Allocator>
670b57cec5SDimitry Andric  stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
680b57cec5SDimitry Andric
690b57cec5SDimitry Andrictemplate <class T, class Container>
700b57cec5SDimitry Andric  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
710b57cec5SDimitry Andrictemplate <class T, class Container>
720b57cec5SDimitry Andric  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
730b57cec5SDimitry Andrictemplate <class T, class Container>
740b57cec5SDimitry Andric  bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
750b57cec5SDimitry Andrictemplate <class T, class Container>
760b57cec5SDimitry Andric  bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
770b57cec5SDimitry Andrictemplate <class T, class Container>
780b57cec5SDimitry Andric  bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
790b57cec5SDimitry Andrictemplate <class T, class Container>
800b57cec5SDimitry Andric  bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
810b57cec5SDimitry Andric
820b57cec5SDimitry Andrictemplate <class T, class Container>
830b57cec5SDimitry Andric  void swap(stack<T, Container>& x, stack<T, Container>& y)
840b57cec5SDimitry Andric  noexcept(noexcept(x.swap(y)));
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric}  // std
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric*/
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric#include <__config>
91fe6060f1SDimitry Andric#include <__memory/uses_allocator.h>
92fe6060f1SDimitry Andric#include <__utility/forward.h>
930b57cec5SDimitry Andric#include <deque>
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
960b57cec5SDimitry Andric#pragma GCC system_header
970b57cec5SDimitry Andric#endif
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andrictemplate <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
1040b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
1050b57cec5SDimitry Andricbool
1060b57cec5SDimitry Andricoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
1090b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
1100b57cec5SDimitry Andricbool
1110b57cec5SDimitry Andricoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andrictemplate <class _Tp, class _Container /*= deque<_Tp>*/>
1140b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS stack
1150b57cec5SDimitry Andric{
1160b57cec5SDimitry Andricpublic:
1170b57cec5SDimitry Andric    typedef _Container                               container_type;
1180b57cec5SDimitry Andric    typedef typename container_type::value_type      value_type;
1190b57cec5SDimitry Andric    typedef typename container_type::reference       reference;
1200b57cec5SDimitry Andric    typedef typename container_type::const_reference const_reference;
1210b57cec5SDimitry Andric    typedef typename container_type::size_type       size_type;
1220b57cec5SDimitry Andric    static_assert((is_same<_Tp, value_type>::value), "" );
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andricprotected:
1250b57cec5SDimitry Andric    container_type c;
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andricpublic:
1280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1290b57cec5SDimitry Andric    stack()
1300b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
1310b57cec5SDimitry Andric        : c() {}
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1340b57cec5SDimitry Andric    stack(const stack& __q) : c(__q.c) {}
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1370b57cec5SDimitry Andric    stack& operator=(const stack& __q) {c = __q.c; return *this;}
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1420b57cec5SDimitry Andric    stack(stack&& __q)
1430b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
1440b57cec5SDimitry Andric        : c(_VSTD::move(__q.c)) {}
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1470b57cec5SDimitry Andric    stack& operator=(stack&& __q)
1480b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
1490b57cec5SDimitry Andric        {c = _VSTD::move(__q.c); return *this;}
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1520b57cec5SDimitry Andric    explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
1530b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1560b57cec5SDimitry Andric    explicit stack(const container_type& __c) : c(__c) {}
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric    template <class _Alloc>
1590b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1600b57cec5SDimitry Andric        explicit stack(const _Alloc& __a,
161*349cc55cSDimitry Andric                       __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1620b57cec5SDimitry Andric            : c(__a) {}
1630b57cec5SDimitry Andric    template <class _Alloc>
1640b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1650b57cec5SDimitry Andric        stack(const container_type& __c, const _Alloc& __a,
166*349cc55cSDimitry Andric              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1670b57cec5SDimitry Andric            : c(__c, __a) {}
1680b57cec5SDimitry Andric    template <class _Alloc>
1690b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1700b57cec5SDimitry Andric        stack(const stack& __s, const _Alloc& __a,
171*349cc55cSDimitry Andric              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1720b57cec5SDimitry Andric            : c(__s.c, __a) {}
1730b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1740b57cec5SDimitry Andric    template <class _Alloc>
1750b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1760b57cec5SDimitry Andric        stack(container_type&& __c, const _Alloc& __a,
177*349cc55cSDimitry Andric              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1780b57cec5SDimitry Andric            : c(_VSTD::move(__c), __a) {}
1790b57cec5SDimitry Andric    template <class _Alloc>
1800b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1810b57cec5SDimitry Andric        stack(stack&& __s, const _Alloc& __a,
182*349cc55cSDimitry Andric              __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
1830b57cec5SDimitry Andric            : c(_VSTD::move(__s.c), __a) {}
1840b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1870b57cec5SDimitry Andric    bool empty()     const      {return c.empty();}
1880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1890b57cec5SDimitry Andric    size_type size() const      {return c.size();}
1900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1910b57cec5SDimitry Andric    reference top()             {return c.back();}
1920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1930b57cec5SDimitry Andric    const_reference top() const {return c.back();}
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1960b57cec5SDimitry Andric    void push(const value_type& __v) {c.push_back(__v);}
1970b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1980b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1990b57cec5SDimitry Andric    void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric    template <class... _Args>
2020b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
2030b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
2040b57cec5SDimitry Andric        decltype(auto) emplace(_Args&&... __args)
2050b57cec5SDimitry Andric        { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
2060b57cec5SDimitry Andric#else
2070b57cec5SDimitry Andric        void      emplace(_Args&&... __args)
2080b57cec5SDimitry Andric        {        c.emplace_back(_VSTD::forward<_Args>(__args)...);}
2090b57cec5SDimitry Andric#endif
2100b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
2110b57cec5SDimitry Andric
2120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2130b57cec5SDimitry Andric    void pop() {c.pop_back();}
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2160b57cec5SDimitry Andric    void swap(stack& __s)
2170b57cec5SDimitry Andric        _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
2180b57cec5SDimitry Andric    {
2190b57cec5SDimitry Andric        using _VSTD::swap;
2200b57cec5SDimitry Andric        swap(c, __s.c);
2210b57cec5SDimitry Andric    }
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric    template <class T1, class _C1>
2240b57cec5SDimitry Andric    friend
2250b57cec5SDimitry Andric    bool
2260b57cec5SDimitry Andric    operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric    template <class T1, class _C1>
2290b57cec5SDimitry Andric    friend
2300b57cec5SDimitry Andric    bool
2310b57cec5SDimitry Andric    operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
2320b57cec5SDimitry Andric};
2330b57cec5SDimitry Andric
234*349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
2350b57cec5SDimitry Andrictemplate<class _Container,
236*349cc55cSDimitry Andric         class = enable_if_t<!__is_allocator<_Container>::value>
2370b57cec5SDimitry Andric>
2380b57cec5SDimitry Andricstack(_Container)
2390b57cec5SDimitry Andric    -> stack<typename _Container::value_type, _Container>;
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andrictemplate<class _Container,
2420b57cec5SDimitry Andric         class _Alloc,
243*349cc55cSDimitry Andric         class = enable_if_t<!__is_allocator<_Container>::value>,
244*349cc55cSDimitry Andric         class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
2450b57cec5SDimitry Andric         >
2460b57cec5SDimitry Andricstack(_Container, _Alloc)
2470b57cec5SDimitry Andric    -> stack<typename _Container::value_type, _Container>;
2480b57cec5SDimitry Andric#endif
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2510b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2520b57cec5SDimitry Andricbool
2530b57cec5SDimitry Andricoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2540b57cec5SDimitry Andric{
2550b57cec5SDimitry Andric    return __x.c == __y.c;
2560b57cec5SDimitry Andric}
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2590b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2600b57cec5SDimitry Andricbool
2610b57cec5SDimitry Andricoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2620b57cec5SDimitry Andric{
2630b57cec5SDimitry Andric    return __x.c < __y.c;
2640b57cec5SDimitry Andric}
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2670b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2680b57cec5SDimitry Andricbool
2690b57cec5SDimitry Andricoperator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2700b57cec5SDimitry Andric{
2710b57cec5SDimitry Andric    return !(__x == __y);
2720b57cec5SDimitry Andric}
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2750b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2760b57cec5SDimitry Andricbool
2770b57cec5SDimitry Andricoperator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2780b57cec5SDimitry Andric{
2790b57cec5SDimitry Andric    return __y < __x;
2800b57cec5SDimitry Andric}
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2830b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2840b57cec5SDimitry Andricbool
2850b57cec5SDimitry Andricoperator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2860b57cec5SDimitry Andric{
2870b57cec5SDimitry Andric    return !(__x < __y);
2880b57cec5SDimitry Andric}
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2910b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2920b57cec5SDimitry Andricbool
2930b57cec5SDimitry Andricoperator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
2940b57cec5SDimitry Andric{
2950b57cec5SDimitry Andric    return !(__y < __x);
2960b57cec5SDimitry Andric}
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andrictemplate <class _Tp, class _Container>
2990b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
300*349cc55cSDimitry Andric__enable_if_t<__is_swappable<_Container>::value, void>
3010b57cec5SDimitry Andricswap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
3020b57cec5SDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
3030b57cec5SDimitry Andric{
3040b57cec5SDimitry Andric    __x.swap(__y);
3050b57cec5SDimitry Andric}
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andrictemplate <class _Tp, class _Container, class _Alloc>
3080b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
3090b57cec5SDimitry Andric    : public uses_allocator<_Container, _Alloc>
3100b57cec5SDimitry Andric{
3110b57cec5SDimitry Andric};
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andric#endif // _LIBCPP_STACK
316