| 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 |
|
| 16 |
namespace std |
| 17 |
{ |
| 18 |
|
| 19 |
template <class T, class Container = deque<T>> |
| 20 |
class stack |
| 21 |
{ |
| 22 |
public: |
| 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 |
|
| 29 |
protected: |
| 30 |
container_type c; |
| 31 |
|
| 32 |
public: |
| 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 |
|
| 66 |
template<class Container> |
| 67 |
stack(Container) -> stack<typename Container::value_type, Container>; // C++17 |
| 68 |
|
| 69 |
template<class InputIterator> |
| 70 |
stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23 |
| 71 |
|
| 72 |
template<class Container, class Allocator> |
| 73 |
stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17 |
| 74 |
|
| 75 |
template<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 |
|
| 80 |
template <class T, class Container> |
| 81 |
bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); |
| 82 |
template <class T, class Container> |
| 83 |
bool operator< (const stack<T, Container>& x, const stack<T, Container>& y); |
| 84 |
template <class T, class Container> |
| 85 |
bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 86 |
template <class T, class Container> |
| 87 |
bool operator> (const stack<T, Container>& x, const stack<T, Container>& y); |
| 88 |
template <class T, class Container> |
| 89 |
bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 90 |
template <class T, class Container> |
| 91 |
bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 92 |
|
| 93 |
template <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 |
// standard-mandated includes |
| 111 |
|
| 112 |
// [stack.syn] |
| 113 |
#include <compare> |
| 114 |
#include <initializer_list> |
| 115 |
|
| 116 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 117 |
# pragma GCC system_header |
| 118 |
#endif |
| 119 |
|
| 120 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 121 |
|
| 122 |
template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack; |
| 123 |
|
| 124 |
template <class _Tp, class _Container> |
| 125 |
_LIBCPP_INLINE_VISIBILITY |
| 126 |
bool |
| 127 |
operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 128 |
|
| 129 |
template <class _Tp, class _Container> |
| 130 |
_LIBCPP_INLINE_VISIBILITY |
| 131 |
bool |
| 132 |
operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 133 |
|
| 134 |
template <class _Tp, class _Container /*= deque<_Tp>*/> |
| 135 |
class _LIBCPP_TEMPLATE_VIS stack |
| 136 |
{ |
| 137 |
public: |
| 138 |
typedef _Container container_type; |
| 139 |
typedef typename container_type::value_type value_type; |
| 140 |
typedef typename container_type::reference reference; |
| 141 |
typedef typename container_type::const_reference const_reference; |
| 142 |
typedef typename container_type::size_type size_type; |
| 143 |
static_assert((is_same<_Tp, value_type>::value), "" ); |
| 144 |
|
| 145 |
protected: |
| 146 |
container_type c; |
| 147 |
|
| 148 |
public: |
| 149 |
_LIBCPP_INLINE_VISIBILITY |
| 150 |
stack() |
| 151 |
_NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) |
| 152 |
: c() {} |
| 153 |
|
| 154 |
_LIBCPP_INLINE_VISIBILITY |
| 155 |
stack(const stack& __q) : c(__q.c) {} |
| 156 |
|
| 157 |
_LIBCPP_INLINE_VISIBILITY |
| 158 |
stack& operator=(const stack& __q) {c = __q.c; return *this;} |
| 159 |
|
| 160 |
|
| 161 |
#ifndef _LIBCPP_CXX03_LANG |
| 162 |
_LIBCPP_INLINE_VISIBILITY |
| 163 |
stack(stack&& __q) |
| 164 |
_NOEXCEPT_(is_nothrow_move_constructible<container_type>::value) |
| 165 |
: c(_VSTD::move(__q.c)) {} |
| 166 |
|
| 167 |
_LIBCPP_INLINE_VISIBILITY |
| 168 |
stack& operator=(stack&& __q) |
| 169 |
_NOEXCEPT_(is_nothrow_move_assignable<container_type>::value) |
| 170 |
{c = _VSTD::move(__q.c); return *this;} |
| 171 |
|
| 172 |
_LIBCPP_INLINE_VISIBILITY |
| 173 |
explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {} |
| 174 |
#endif // _LIBCPP_CXX03_LANG |
| 175 |
|
| 176 |
_LIBCPP_INLINE_VISIBILITY |
| 177 |
explicit stack(const container_type& __c) : c(__c) {} |
| 178 |
|
| 179 |
template <class _Alloc> |
| 180 |
_LIBCPP_INLINE_VISIBILITY |
| 181 |
explicit stack(const _Alloc& __a, |
| 182 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 183 |
: c(__a) {} |
| 184 |
template <class _Alloc> |
| 185 |
_LIBCPP_INLINE_VISIBILITY |
| 186 |
stack(const container_type& __c, const _Alloc& __a, |
| 187 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 188 |
: c(__c, __a) {} |
| 189 |
template <class _Alloc> |
| 190 |
_LIBCPP_INLINE_VISIBILITY |
| 191 |
stack(const stack& __s, const _Alloc& __a, |
| 192 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 193 |
: c(__s.c, __a) {} |
| 194 |
#ifndef _LIBCPP_CXX03_LANG |
| 195 |
template <class _Alloc> |
| 196 |
_LIBCPP_INLINE_VISIBILITY |
| 197 |
stack(container_type&& __c, const _Alloc& __a, |
| 198 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 199 |
: c(_VSTD::move(__c), __a) {} |
| 200 |
template <class _Alloc> |
| 201 |
_LIBCPP_INLINE_VISIBILITY |
| 202 |
stack(stack&& __s, const _Alloc& __a, |
| 203 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 204 |
: c(_VSTD::move(__s.c), __a) {} |
| 205 |
#endif // _LIBCPP_CXX03_LANG |
| 206 |
|
| 207 |
#if _LIBCPP_STD_VER > 20 |
| 208 |
template <class _InputIterator, |
| 209 |
class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>> |
| 210 |
_LIBCPP_HIDE_FROM_ABI |
| 211 |
stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {} |
| 212 |
|
| 213 |
template <class _InputIterator, |
| 214 |
class _Alloc, |
| 215 |
class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, |
| 216 |
class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>> |
| 217 |
_LIBCPP_HIDE_FROM_ABI |
| 218 |
stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {} |
| 219 |
#endif |
| 220 |
|
| 221 |
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 222 |
bool empty() const {return c.empty();} |
| 223 |
_LIBCPP_INLINE_VISIBILITY |
| 224 |
size_type size() const {return c.size();} |
| 225 |
_LIBCPP_INLINE_VISIBILITY |
| 226 |
reference top() {return c.back();} |
| 227 |
_LIBCPP_INLINE_VISIBILITY |
| 228 |
const_reference top() const {return c.back();} |
| 229 |
|
| 230 |
_LIBCPP_INLINE_VISIBILITY |
| 231 |
void push(const value_type& __v) {c.push_back(__v);} |
| 232 |
#ifndef _LIBCPP_CXX03_LANG |
| 233 |
_LIBCPP_INLINE_VISIBILITY |
| 234 |
void push(value_type&& __v) {c.push_back(_VSTD::move(__v));} |
| 235 |
|
| 236 |
template <class... _Args> |
| 237 |
_LIBCPP_INLINE_VISIBILITY |
| 238 |
#if _LIBCPP_STD_VER > 14 |
| 239 |
decltype(auto) emplace(_Args&&... __args) |
| 240 |
{ return c.emplace_back(_VSTD::forward<_Args>(__args)...);} |
| 241 |
#else |
| 242 |
void emplace(_Args&&... __args) |
| 243 |
{ c.emplace_back(_VSTD::forward<_Args>(__args)...);} |
| 244 |
#endif |
| 245 |
#endif // _LIBCPP_CXX03_LANG |
| 246 |
|
| 247 |
_LIBCPP_INLINE_VISIBILITY |
| 248 |
void pop() {c.pop_back();} |
| 249 |
|
| 250 |
_LIBCPP_INLINE_VISIBILITY |
| 251 |
void swap(stack& __s) |
| 252 |
_NOEXCEPT_(__is_nothrow_swappable<container_type>::value) |
| 253 |
{ |
| 254 |
using _VSTD::swap; |
| 255 |
swap(c, __s.c); |
| 256 |
} |
| 257 |
|
| 258 |
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; } |
| 259 |
|
| 260 |
template <class T1, class _C1> |
| 261 |
friend |
| 262 |
bool |
| 263 |
operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
| 264 |
|
| 265 |
template <class T1, class _C1> |
| 266 |
friend |
| 267 |
bool |
| 268 |
operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
| 269 |
}; |
| 270 |
|
| 271 |
#if _LIBCPP_STD_VER > 14 |
| 272 |
template<class _Container, |
| 273 |
class = enable_if_t<!__is_allocator<_Container>::value> |
| 274 |
> |
| 275 |
stack(_Container) |
| 276 |
-> stack<typename _Container::value_type, _Container>; |
| 277 |
|
| 278 |
template<class _Container, |
| 279 |
class _Alloc, |
| 280 |
class = enable_if_t<!__is_allocator<_Container>::value>, |
| 281 |
class = enable_if_t<uses_allocator<_Container, _Alloc>::value> |
| 282 |
> |
| 283 |
stack(_Container, _Alloc) |
| 284 |
-> stack<typename _Container::value_type, _Container>; |
| 285 |
#endif |
| 286 |
|
| 287 |
#if _LIBCPP_STD_VER > 20 |
| 288 |
template<class _InputIterator, |
| 289 |
class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>> |
| 290 |
stack(_InputIterator, _InputIterator) |
| 291 |
-> stack<__iter_value_type<_InputIterator>>; |
| 292 |
|
| 293 |
template<class _InputIterator, |
| 294 |
class _Alloc, |
| 295 |
class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, |
| 296 |
class = __enable_if_t<__is_allocator<_Alloc>::value>> |
| 297 |
stack(_InputIterator, _InputIterator, _Alloc) |
| 298 |
-> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>; |
| 299 |
#endif |
| 300 |
|
| 301 |
template <class _Tp, class _Container> |
| 302 |
inline _LIBCPP_INLINE_VISIBILITY |
| 303 |
bool |
| 304 |
operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 305 |
{ |
| 306 |
return __x.c == __y.c; |
| 307 |
} |
| 308 |
|
| 309 |
template <class _Tp, class _Container> |
| 310 |
inline _LIBCPP_INLINE_VISIBILITY |
| 311 |
bool |
| 312 |
operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 313 |
{ |
| 314 |
return __x.c < __y.c; |
| 315 |
} |
| 316 |
|
| 317 |
template <class _Tp, class _Container> |
| 318 |
inline _LIBCPP_INLINE_VISIBILITY |
| 319 |
bool |
| 320 |
operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 321 |
{ |
| 322 |
return !(__x == __y); |
| 323 |
} |
| 324 |
|
| 325 |
template <class _Tp, class _Container> |
| 326 |
inline _LIBCPP_INLINE_VISIBILITY |
| 327 |
bool |
| 328 |
operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 329 |
{ |
| 330 |
return __y < __x; |
| 331 |
} |
| 332 |
|
| 333 |
template <class _Tp, class _Container> |
| 334 |
inline _LIBCPP_INLINE_VISIBILITY |
| 335 |
bool |
| 336 |
operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 337 |
{ |
| 338 |
return !(__x < __y); |
| 339 |
} |
| 340 |
|
| 341 |
template <class _Tp, class _Container> |
| 342 |
inline _LIBCPP_INLINE_VISIBILITY |
| 343 |
bool |
| 344 |
operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 345 |
{ |
| 346 |
return !(__y < __x); |
| 347 |
} |
| 348 |
|
| 349 |
template <class _Tp, class _Container> |
| 350 |
inline _LIBCPP_INLINE_VISIBILITY |
| 351 |
__enable_if_t<__is_swappable<_Container>::value, void> |
| 352 |
swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) |
| 353 |
_NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
| 354 |
{ |
| 355 |
__x.swap(__y); |
| 356 |
} |
| 357 |
|
| 358 |
template <class _Tp, class _Container, class _Alloc> |
| 359 |
struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc> |
| 360 |
: public uses_allocator<_Container, _Alloc> |
| 361 |
{ |
| 362 |
}; |
| 363 |
|
| 364 |
_LIBCPP_END_NAMESPACE_STD |
| 365 |
|
| 366 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
| 367 |
# include <concepts> |
| 368 |
# include <functional> |
| 369 |
#endif |
| 370 |
|
| 371 |
#endif // _LIBCPP_STACK |