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