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> 91050b064fSChristopher Di Bella#include <__memory/uses_allocator.h> 926adbc83eSChristopher Di Bella#include <__utility/forward.h> 933e519524SHoward Hinnant#include <deque> 943e519524SHoward Hinnant 95073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 963e519524SHoward Hinnant#pragma GCC system_header 97073458b1SHoward Hinnant#endif 983e519524SHoward Hinnant 993e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD 1003e519524SHoward Hinnant 101e2f2d1edSEric Fiseliertemplate <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack; 1023e519524SHoward Hinnant 1033e519524SHoward Hinnanttemplate <class _Tp, class _Container> 104aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY 1053e519524SHoward Hinnantbool 1063e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); 1073e519524SHoward Hinnant 1083e519524SHoward Hinnanttemplate <class _Tp, class _Container> 109aeb85680SHoward Hinnant_LIBCPP_INLINE_VISIBILITY 1103e519524SHoward Hinnantbool 1113e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); 1123e519524SHoward Hinnant 1133afa22a3SMarshall Clowtemplate <class _Tp, class _Container /*= deque<_Tp>*/> 114e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS stack 1153e519524SHoward Hinnant{ 1163e519524SHoward Hinnantpublic: 1173e519524SHoward Hinnant typedef _Container container_type; 1183e519524SHoward Hinnant typedef typename container_type::value_type value_type; 1193e519524SHoward Hinnant typedef typename container_type::reference reference; 1203e519524SHoward Hinnant typedef typename container_type::const_reference const_reference; 1213e519524SHoward Hinnant typedef typename container_type::size_type size_type; 122c1fe2c43SMarshall Clow static_assert((is_same<_Tp, value_type>::value), "" ); 1233e519524SHoward Hinnant 1243e519524SHoward Hinnantprotected: 1253e519524SHoward Hinnant container_type c; 1263e519524SHoward Hinnant 1273e519524SHoward Hinnantpublic: 128e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 129bd0c1600SHoward Hinnant stack() 130bd0c1600SHoward Hinnant _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) 131bd0c1600SHoward Hinnant : c() {} 132bd0c1600SHoward Hinnant 133bd0c1600SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 134bd0c1600SHoward Hinnant stack(const stack& __q) : c(__q.c) {} 135bd0c1600SHoward Hinnant 1367196ee31SEric Fiselier _LIBCPP_INLINE_VISIBILITY 1377196ee31SEric Fiselier stack& operator=(const stack& __q) {c = __q.c; return *this;} 1387196ee31SEric Fiselier 1397196ee31SEric Fiselier 1407196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG 141bd0c1600SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 142bd0c1600SHoward Hinnant stack(stack&& __q) 143bd0c1600SHoward Hinnant _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value) 144ce48a113SHoward Hinnant : c(_VSTD::move(__q.c)) {} 145bd0c1600SHoward Hinnant 146bd0c1600SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 147bd0c1600SHoward Hinnant stack& operator=(stack&& __q) 148bd0c1600SHoward Hinnant _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value) 149ce48a113SHoward Hinnant {c = _VSTD::move(__q.c); return *this;} 1507196ee31SEric Fiselier 1517196ee31SEric Fiselier _LIBCPP_INLINE_VISIBILITY 1527196ee31SEric Fiselier explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {} 1537196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG 154bd0c1600SHoward Hinnant 155e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1563e519524SHoward Hinnant explicit stack(const container_type& __c) : c(__c) {} 1577196ee31SEric Fiselier 1583e519524SHoward Hinnant template <class _Alloc> 159e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1603e519524SHoward Hinnant explicit stack(const _Alloc& __a, 161199d2ebeSArthur O'Dwyer _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0) 1623e519524SHoward Hinnant : c(__a) {} 1633e519524SHoward Hinnant template <class _Alloc> 164e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1653e519524SHoward Hinnant stack(const container_type& __c, const _Alloc& __a, 166199d2ebeSArthur O'Dwyer _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0) 1673e519524SHoward Hinnant : c(__c, __a) {} 1683e519524SHoward Hinnant template <class _Alloc> 169e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1703e519524SHoward Hinnant stack(const stack& __s, const _Alloc& __a, 171199d2ebeSArthur O'Dwyer _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0) 1723e519524SHoward Hinnant : c(__s.c, __a) {} 1737196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG 1743e519524SHoward Hinnant template <class _Alloc> 175e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1763e519524SHoward Hinnant stack(container_type&& __c, const _Alloc& __a, 177199d2ebeSArthur O'Dwyer _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0) 178ce48a113SHoward Hinnant : c(_VSTD::move(__c), __a) {} 1793e519524SHoward Hinnant template <class _Alloc> 180e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1813e519524SHoward Hinnant stack(stack&& __s, const _Alloc& __a, 182199d2ebeSArthur O'Dwyer _EnableIf<uses_allocator<container_type, _Alloc>::value>* = 0) 183ce48a113SHoward Hinnant : c(_VSTD::move(__s.c), __a) {} 1847196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG 1853e519524SHoward Hinnant 18672c8fad4SMarshall Clow _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 1873e519524SHoward Hinnant bool empty() const {return c.empty();} 188e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1893e519524SHoward Hinnant size_type size() const {return c.size();} 190e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1913e519524SHoward Hinnant reference top() {return c.back();} 192e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1933e519524SHoward Hinnant const_reference top() const {return c.back();} 1943e519524SHoward Hinnant 195e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1963e519524SHoward Hinnant void push(const value_type& __v) {c.push_back(__v);} 1977196ee31SEric Fiselier#ifndef _LIBCPP_CXX03_LANG 198e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 199ce48a113SHoward Hinnant void push(value_type&& __v) {c.push_back(_VSTD::move(__v));} 2007196ee31SEric Fiselier 201e0601335SHoward Hinnant template <class... _Args> 202e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 20363b560beSMarshall Clow#if _LIBCPP_STD_VER > 14 204e34f5ffeSMarshall Clow decltype(auto) emplace(_Args&&... __args) 2050e411641SEric Fiselier { return c.emplace_back(_VSTD::forward<_Args>(__args)...);} 20663b560beSMarshall Clow#else 20763b560beSMarshall Clow void emplace(_Args&&... __args) 20863b560beSMarshall Clow { c.emplace_back(_VSTD::forward<_Args>(__args)...);} 20963b560beSMarshall Clow#endif 2107196ee31SEric Fiselier#endif // _LIBCPP_CXX03_LANG 2117196ee31SEric Fiselier 212e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 2133e519524SHoward Hinnant void pop() {c.pop_back();} 2143e519524SHoward Hinnant 215e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 2163e519524SHoward Hinnant void swap(stack& __s) 217bd0c1600SHoward Hinnant _NOEXCEPT_(__is_nothrow_swappable<container_type>::value) 2183e519524SHoward Hinnant { 219ce48a113SHoward Hinnant using _VSTD::swap; 2203e519524SHoward Hinnant swap(c, __s.c); 2213e519524SHoward Hinnant } 2223e519524SHoward Hinnant 2233e519524SHoward Hinnant template <class T1, class _C1> 2243e519524SHoward Hinnant friend 2253e519524SHoward Hinnant bool 2263e519524SHoward Hinnant operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); 2273e519524SHoward Hinnant 2283e519524SHoward Hinnant template <class T1, class _C1> 2293e519524SHoward Hinnant friend 2303e519524SHoward Hinnant bool 2313e519524SHoward Hinnant operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); 2323e519524SHoward Hinnant}; 2333e519524SHoward Hinnant 23401666904SLouis Dionne#if _LIBCPP_STD_VER >= 17 2355b8b8b5dSMarshall Clowtemplate<class _Container, 236*4e0ea2cfSLouis Dionne class = enable_if_t<!__is_allocator<_Container>::value> 2375b8b8b5dSMarshall Clow> 2385b8b8b5dSMarshall Clowstack(_Container) 2395b8b8b5dSMarshall Clow -> stack<typename _Container::value_type, _Container>; 2405b8b8b5dSMarshall Clow 2415b8b8b5dSMarshall Clowtemplate<class _Container, 2425b8b8b5dSMarshall Clow class _Alloc, 243*4e0ea2cfSLouis Dionne class = enable_if_t<!__is_allocator<_Container>::value>, 244*4e0ea2cfSLouis Dionne class = enable_if_t<uses_allocator<_Container, _Alloc>::value> 2455b8b8b5dSMarshall Clow > 2465b8b8b5dSMarshall Clowstack(_Container, _Alloc) 2475b8b8b5dSMarshall Clow -> stack<typename _Container::value_type, _Container>; 2485b8b8b5dSMarshall Clow#endif 2495b8b8b5dSMarshall Clow 2503e519524SHoward Hinnanttemplate <class _Tp, class _Container> 251e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2523e519524SHoward Hinnantbool 2533e519524SHoward Hinnantoperator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2543e519524SHoward Hinnant{ 2553e519524SHoward Hinnant return __x.c == __y.c; 2563e519524SHoward Hinnant} 2573e519524SHoward Hinnant 2583e519524SHoward Hinnanttemplate <class _Tp, class _Container> 259e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2603e519524SHoward Hinnantbool 2613e519524SHoward Hinnantoperator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2623e519524SHoward Hinnant{ 2633e519524SHoward Hinnant return __x.c < __y.c; 2643e519524SHoward Hinnant} 2653e519524SHoward Hinnant 2663e519524SHoward Hinnanttemplate <class _Tp, class _Container> 267e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2683e519524SHoward Hinnantbool 2693e519524SHoward Hinnantoperator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2703e519524SHoward Hinnant{ 2713e519524SHoward Hinnant return !(__x == __y); 2723e519524SHoward Hinnant} 2733e519524SHoward Hinnant 2743e519524SHoward Hinnanttemplate <class _Tp, class _Container> 275e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2763e519524SHoward Hinnantbool 2773e519524SHoward Hinnantoperator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2783e519524SHoward Hinnant{ 2793e519524SHoward Hinnant return __y < __x; 2803e519524SHoward Hinnant} 2813e519524SHoward Hinnant 2823e519524SHoward Hinnanttemplate <class _Tp, class _Container> 283e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2843e519524SHoward Hinnantbool 2853e519524SHoward Hinnantoperator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2863e519524SHoward Hinnant{ 2873e519524SHoward Hinnant return !(__x < __y); 2883e519524SHoward Hinnant} 2893e519524SHoward Hinnant 2903e519524SHoward Hinnanttemplate <class _Tp, class _Container> 291e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2923e519524SHoward Hinnantbool 2933e519524SHoward Hinnantoperator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 2943e519524SHoward Hinnant{ 2953e519524SHoward Hinnant return !(__y < __x); 2963e519524SHoward Hinnant} 2973e519524SHoward Hinnant 2983e519524SHoward Hinnanttemplate <class _Tp, class _Container> 299e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 300199d2ebeSArthur O'Dwyer_EnableIf<__is_swappable<_Container>::value, void> 3013e519524SHoward Hinnantswap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) 302bd0c1600SHoward Hinnant _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 3033e519524SHoward Hinnant{ 3043e519524SHoward Hinnant __x.swap(__y); 3053e519524SHoward Hinnant} 3063e519524SHoward Hinnant 3073e519524SHoward Hinnanttemplate <class _Tp, class _Container, class _Alloc> 308e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc> 3093e519524SHoward Hinnant : public uses_allocator<_Container, _Alloc> 3103e519524SHoward Hinnant{ 3113e519524SHoward Hinnant}; 3123e519524SHoward Hinnant 3133e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD 3143e519524SHoward Hinnant 3153e519524SHoward Hinnant#endif // _LIBCPP_STACK 316