1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 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 InputIterator> stack(InputIterator first, InputIterator last); // since C++23 45 template <class Alloc> explicit stack(const Alloc& a); 46 template <class Alloc> stack(const container_type& c, const Alloc& a); 47 template <class Alloc> stack(container_type&& c, const Alloc& a); 48 template <class Alloc> stack(const stack& c, const Alloc& a); 49 template <class Alloc> stack(stack&& c, const Alloc& a); 50 template<class InputIterator, class Alloc> 51 stack(InputIterator first, InputIterator last, const Alloc&); // since C++23 52 53 bool empty() const; 54 size_type size() const; 55 reference top(); 56 const_reference top() const; 57 58 void push(const value_type& x); 59 void push(value_type&& x); 60 template <class... Args> reference emplace(Args&&... args); // reference in C++17 61 void pop(); 62 63 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>) 64}; 65 66template<class Container> 67 stack(Container) -> stack<typename Container::value_type, Container>; // C++17 68 69template<class InputIterator> 70 stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23 71 72template<class Container, class Allocator> 73 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17 74 75template<class InputIterator, class Allocator> 76 stack(InputIterator, InputIterator, Allocator) 77 -> stack<iter-value-type<InputIterator>, 78 deque<iter-value-type<InputIterator>, Allocator>>; // since C++23 79 80template <class T, class Container> 81 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); 82template <class T, class Container> 83 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y); 84template <class T, class Container> 85 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y); 86template <class T, class Container> 87 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y); 88template <class T, class Container> 89 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y); 90template <class T, class Container> 91 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y); 92 93template <class T, class Container> 94 void swap(stack<T, Container>& x, stack<T, Container>& y) 95 noexcept(noexcept(x.swap(y))); 96 97} // std 98 99*/ 100 101#include <__assert> // all public C++ headers provide the assertion handler 102#include <__config> 103#include <__iterator/iterator_traits.h> 104#include <__memory/uses_allocator.h> 105#include <__utility/forward.h> 106#include <deque> 107#include <type_traits> 108#include <version> 109 110#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 111# pragma GCC system_header 112#endif 113 114_LIBCPP_BEGIN_NAMESPACE_STD 115 116template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack; 117 118template <class _Tp, class _Container> 119_LIBCPP_INLINE_VISIBILITY 120bool 121operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); 122 123template <class _Tp, class _Container> 124_LIBCPP_INLINE_VISIBILITY 125bool 126operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); 127 128template <class _Tp, class _Container /*= deque<_Tp>*/> 129class _LIBCPP_TEMPLATE_VIS stack 130{ 131public: 132 typedef _Container container_type; 133 typedef typename container_type::value_type value_type; 134 typedef typename container_type::reference reference; 135 typedef typename container_type::const_reference const_reference; 136 typedef typename container_type::size_type size_type; 137 static_assert((is_same<_Tp, value_type>::value), "" ); 138 139protected: 140 container_type c; 141 142public: 143 _LIBCPP_INLINE_VISIBILITY 144 stack() 145 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) 146 : c() {} 147 148 _LIBCPP_INLINE_VISIBILITY 149 stack(const stack& __q) : c(__q.c) {} 150 151 _LIBCPP_INLINE_VISIBILITY 152 stack& operator=(const stack& __q) {c = __q.c; return *this;} 153 154 155#ifndef _LIBCPP_CXX03_LANG 156 _LIBCPP_INLINE_VISIBILITY 157 stack(stack&& __q) 158 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value) 159 : c(_VSTD::move(__q.c)) {} 160 161 _LIBCPP_INLINE_VISIBILITY 162 stack& operator=(stack&& __q) 163 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value) 164 {c = _VSTD::move(__q.c); return *this;} 165 166 _LIBCPP_INLINE_VISIBILITY 167 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {} 168#endif // _LIBCPP_CXX03_LANG 169 170 _LIBCPP_INLINE_VISIBILITY 171 explicit stack(const container_type& __c) : c(__c) {} 172 173 template <class _Alloc> 174 _LIBCPP_INLINE_VISIBILITY 175 explicit stack(const _Alloc& __a, 176 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) 177 : c(__a) {} 178 template <class _Alloc> 179 _LIBCPP_INLINE_VISIBILITY 180 stack(const container_type& __c, const _Alloc& __a, 181 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) 182 : c(__c, __a) {} 183 template <class _Alloc> 184 _LIBCPP_INLINE_VISIBILITY 185 stack(const stack& __s, const _Alloc& __a, 186 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) 187 : c(__s.c, __a) {} 188#ifndef _LIBCPP_CXX03_LANG 189 template <class _Alloc> 190 _LIBCPP_INLINE_VISIBILITY 191 stack(container_type&& __c, const _Alloc& __a, 192 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) 193 : c(_VSTD::move(__c), __a) {} 194 template <class _Alloc> 195 _LIBCPP_INLINE_VISIBILITY 196 stack(stack&& __s, const _Alloc& __a, 197 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) 198 : c(_VSTD::move(__s.c), __a) {} 199#endif // _LIBCPP_CXX03_LANG 200 201#if _LIBCPP_STD_VER > 20 202 template <class _InputIterator, 203 class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>> 204 _LIBCPP_HIDE_FROM_ABI 205 stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {} 206 207 template <class _InputIterator, 208 class _Alloc, 209 class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, 210 class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>> 211 _LIBCPP_HIDE_FROM_ABI 212 stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {} 213#endif 214 215 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 216 bool empty() const {return c.empty();} 217 _LIBCPP_INLINE_VISIBILITY 218 size_type size() const {return c.size();} 219 _LIBCPP_INLINE_VISIBILITY 220 reference top() {return c.back();} 221 _LIBCPP_INLINE_VISIBILITY 222 const_reference top() const {return c.back();} 223 224 _LIBCPP_INLINE_VISIBILITY 225 void push(const value_type& __v) {c.push_back(__v);} 226#ifndef _LIBCPP_CXX03_LANG 227 _LIBCPP_INLINE_VISIBILITY 228 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));} 229 230 template <class... _Args> 231 _LIBCPP_INLINE_VISIBILITY 232#if _LIBCPP_STD_VER > 14 233 decltype(auto) emplace(_Args&&... __args) 234 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);} 235#else 236 void emplace(_Args&&... __args) 237 { c.emplace_back(_VSTD::forward<_Args>(__args)...);} 238#endif 239#endif // _LIBCPP_CXX03_LANG 240 241 _LIBCPP_INLINE_VISIBILITY 242 void pop() {c.pop_back();} 243 244 _LIBCPP_INLINE_VISIBILITY 245 void swap(stack& __s) 246 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value) 247 { 248 using _VSTD::swap; 249 swap(c, __s.c); 250 } 251 252 template <class T1, class _C1> 253 friend 254 bool 255 operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); 256 257 template <class T1, class _C1> 258 friend 259 bool 260 operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); 261}; 262 263#if _LIBCPP_STD_VER > 14 264template<class _Container, 265 class = enable_if_t<!__is_allocator<_Container>::value> 266> 267stack(_Container) 268 -> stack<typename _Container::value_type, _Container>; 269 270template<class _Container, 271 class _Alloc, 272 class = enable_if_t<!__is_allocator<_Container>::value>, 273 class = enable_if_t<uses_allocator<_Container, _Alloc>::value> 274 > 275stack(_Container, _Alloc) 276 -> stack<typename _Container::value_type, _Container>; 277#endif 278 279#if _LIBCPP_STD_VER > 20 280template<class _InputIterator, 281 class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>> 282stack(_InputIterator, _InputIterator) 283 -> stack<__iter_value_type<_InputIterator>>; 284 285template<class _InputIterator, 286 class _Alloc, 287 class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, 288 class = __enable_if_t<__is_allocator<_Alloc>::value>> 289stack(_InputIterator, _InputIterator, _Alloc) 290 -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>; 291#endif 292 293template <class _Tp, class _Container> 294inline _LIBCPP_INLINE_VISIBILITY 295bool 296operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 297{ 298 return __x.c == __y.c; 299} 300 301template <class _Tp, class _Container> 302inline _LIBCPP_INLINE_VISIBILITY 303bool 304operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 305{ 306 return __x.c < __y.c; 307} 308 309template <class _Tp, class _Container> 310inline _LIBCPP_INLINE_VISIBILITY 311bool 312operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 313{ 314 return !(__x == __y); 315} 316 317template <class _Tp, class _Container> 318inline _LIBCPP_INLINE_VISIBILITY 319bool 320operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 321{ 322 return __y < __x; 323} 324 325template <class _Tp, class _Container> 326inline _LIBCPP_INLINE_VISIBILITY 327bool 328operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 329{ 330 return !(__x < __y); 331} 332 333template <class _Tp, class _Container> 334inline _LIBCPP_INLINE_VISIBILITY 335bool 336operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) 337{ 338 return !(__y < __x); 339} 340 341template <class _Tp, class _Container> 342inline _LIBCPP_INLINE_VISIBILITY 343__enable_if_t<__is_swappable<_Container>::value, void> 344swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) 345 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 346{ 347 __x.swap(__y); 348} 349 350template <class _Tp, class _Container, class _Alloc> 351struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc> 352 : public uses_allocator<_Container, _Alloc> 353{ 354}; 355 356_LIBCPP_END_NAMESPACE_STD 357 358#endif // _LIBCPP_STACK 359