| 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___CXX03_STACK |
| 11 |
#define _LIBCPP___CXX03_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<container-compatible-range<T> R> stack(from_range_t, R&& rg); // since C++23 |
| 46 |
template <class Alloc> explicit stack(const Alloc& a); |
| 47 |
template <class Alloc> stack(const container_type& c, const Alloc& a); |
| 48 |
template <class Alloc> stack(container_type&& c, const Alloc& a); |
| 49 |
template <class Alloc> stack(const stack& c, const Alloc& a); |
| 50 |
template <class Alloc> stack(stack&& c, const Alloc& a); |
| 51 |
template<class InputIterator, class Alloc> |
| 52 |
stack(InputIterator first, InputIterator last, const Alloc&); // since C++23 |
| 53 |
template<container-compatible-range<T> R, class Alloc> |
| 54 |
stack(from_range_t, R&& rg, const Alloc&); // since C++23 |
| 55 |
|
| 56 |
bool empty() const; |
| 57 |
size_type size() const; |
| 58 |
reference top(); |
| 59 |
const_reference top() const; |
| 60 |
|
| 61 |
void push(const value_type& x); |
| 62 |
void push(value_type&& x); |
| 63 |
template<container-compatible-range<T> R> |
| 64 |
void push_range(R&& rg); // C++23 |
| 65 |
template <class... Args> reference emplace(Args&&... args); // reference in C++17 |
| 66 |
void pop(); |
| 67 |
|
| 68 |
void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>) |
| 69 |
}; |
| 70 |
|
| 71 |
template<class Container> |
| 72 |
stack(Container) -> stack<typename Container::value_type, Container>; // C++17 |
| 73 |
|
| 74 |
template<class InputIterator> |
| 75 |
stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23 |
| 76 |
|
| 77 |
template<ranges::input_range R> |
| 78 |
stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23 |
| 79 |
|
| 80 |
template<class Container, class Allocator> |
| 81 |
stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17 |
| 82 |
|
| 83 |
template<class InputIterator, class Allocator> |
| 84 |
stack(InputIterator, InputIterator, Allocator) |
| 85 |
-> stack<iter-value-type<InputIterator>, |
| 86 |
deque<iter-value-type<InputIterator>, Allocator>>; // since C++23 |
| 87 |
|
| 88 |
template<ranges::input_range R, class Allocator> |
| 89 |
stack(from_range_t, R&&, Allocator) |
| 90 |
-> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23 |
| 91 |
|
| 92 |
template <class T, class Container> |
| 93 |
bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); |
| 94 |
template <class T, class Container> |
| 95 |
bool operator< (const stack<T, Container>& x, const stack<T, Container>& y); |
| 96 |
template <class T, class Container> |
| 97 |
bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 98 |
template <class T, class Container> |
| 99 |
bool operator> (const stack<T, Container>& x, const stack<T, Container>& y); |
| 100 |
template <class T, class Container> |
| 101 |
bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 102 |
template <class T, class Container> |
| 103 |
bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 104 |
template<class T, three_way_comparable Container> |
| 105 |
compare_three_way_result_t<Container> |
| 106 |
operator<=>(const stack<T, Container>& x, const stack<T, Container>& y); // since C++20 |
| 107 |
|
| 108 |
template <class T, class Container> |
| 109 |
void swap(stack<T, Container>& x, stack<T, Container>& y) |
| 110 |
noexcept(noexcept(x.swap(y))); |
| 111 |
|
| 112 |
} // std |
| 113 |
|
| 114 |
*/ |
| 115 |
|
| 116 |
#include <__cxx03/__config> |
| 117 |
#include <__cxx03/__fwd/stack.h> |
| 118 |
#include <__cxx03/__iterator/back_insert_iterator.h> |
| 119 |
#include <__cxx03/__iterator/iterator_traits.h> |
| 120 |
#include <__cxx03/__memory/uses_allocator.h> |
| 121 |
#include <__cxx03/__type_traits/is_same.h> |
| 122 |
#include <__cxx03/__utility/forward.h> |
| 123 |
#include <__cxx03/deque> |
| 124 |
#include <__cxx03/version> |
| 125 |
|
| 126 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 127 |
# pragma GCC system_header |
| 128 |
#endif |
| 129 |
|
| 130 |
_LIBCPP_PUSH_MACROS |
| 131 |
#include <__cxx03/__undef_macros> |
| 132 |
|
| 133 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 134 |
|
| 135 |
template <class _Tp, class _Container> |
| 136 |
_LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 137 |
|
| 138 |
template <class _Tp, class _Container> |
| 139 |
_LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 140 |
|
| 141 |
template <class _Tp, class _Container /*= deque<_Tp>*/> |
| 142 |
class _LIBCPP_TEMPLATE_VIS stack { |
| 143 |
public: |
| 144 |
typedef _Container container_type; |
| 145 |
typedef typename container_type::value_type value_type; |
| 146 |
typedef typename container_type::reference reference; |
| 147 |
typedef typename container_type::const_reference const_reference; |
| 148 |
typedef typename container_type::size_type size_type; |
| 149 |
static_assert(is_same<_Tp, value_type>::value, ""); |
| 150 |
|
| 151 |
protected: |
| 152 |
container_type c; |
| 153 |
|
| 154 |
public: |
| 155 |
_LIBCPP_HIDE_FROM_ABI stack() : c() {} |
| 156 |
|
| 157 |
_LIBCPP_HIDE_FROM_ABI stack(const stack& __q) : c(__q.c) {} |
| 158 |
|
| 159 |
_LIBCPP_HIDE_FROM_ABI stack& operator=(const stack& __q) { |
| 160 |
c = __q.c; |
| 161 |
return *this; |
| 162 |
} |
| 163 |
|
| 164 |
_LIBCPP_HIDE_FROM_ABI explicit stack(const container_type& __c) : c(__c) {} |
| 165 |
|
| 166 |
template <class _Alloc> |
| 167 |
_LIBCPP_HIDE_FROM_ABI explicit stack(const _Alloc& __a, |
| 168 |
__enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 169 |
: c(__a) {} |
| 170 |
template <class _Alloc> |
| 171 |
_LIBCPP_HIDE_FROM_ABI |
| 172 |
stack(const container_type& __c, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 173 |
: c(__c, __a) {} |
| 174 |
template <class _Alloc> |
| 175 |
_LIBCPP_HIDE_FROM_ABI |
| 176 |
stack(const stack& __s, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0) |
| 177 |
: c(__s.c, __a) {} |
| 178 |
|
| 179 |
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); } |
| 180 |
_LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); } |
| 181 |
_LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); } |
| 182 |
_LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); } |
| 183 |
|
| 184 |
_LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); } |
| 185 |
|
| 186 |
_LIBCPP_HIDE_FROM_ABI void pop() { c.pop_back(); } |
| 187 |
|
| 188 |
_LIBCPP_HIDE_FROM_ABI void swap(stack& __s) { |
| 189 |
using std::swap; |
| 190 |
swap(c, __s.c); |
| 191 |
} |
| 192 |
|
| 193 |
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; } |
| 194 |
|
| 195 |
template <class _T1, class _OtherContainer> |
| 196 |
friend bool operator==(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y); |
| 197 |
|
| 198 |
template <class _T1, class _OtherContainer> |
| 199 |
friend bool operator<(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y); |
| 200 |
}; |
| 201 |
|
| 202 |
template <class _Tp, class _Container> |
| 203 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 204 |
return __x.c == __y.c; |
| 205 |
} |
| 206 |
|
| 207 |
template <class _Tp, class _Container> |
| 208 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 209 |
return __x.c < __y.c; |
| 210 |
} |
| 211 |
|
| 212 |
template <class _Tp, class _Container> |
| 213 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 214 |
return !(__x == __y); |
| 215 |
} |
| 216 |
|
| 217 |
template <class _Tp, class _Container> |
| 218 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 219 |
return __y < __x; |
| 220 |
} |
| 221 |
|
| 222 |
template <class _Tp, class _Container> |
| 223 |
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 224 |
return !(__x < __y); |
| 225 |
} |
| 226 |
|
| 227 |
template <class _Tp, class _Container> |
| 228 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) { |
| 229 |
return !(__y < __x); |
| 230 |
} |
| 231 |
|
| 232 |
template <class _Tp, class _Container, __enable_if_t<__is_swappable_v<_Container>, int> = 0> |
| 233 |
inline _LIBCPP_HIDE_FROM_ABI void swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) { |
| 234 |
__x.swap(__y); |
| 235 |
} |
| 236 |
|
| 237 |
template <class _Tp, class _Container, class _Alloc> |
| 238 |
struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc> : public uses_allocator<_Container, _Alloc> { |
| 239 |
}; |
| 240 |
|
| 241 |
_LIBCPP_END_NAMESPACE_STD |
| 242 |
|
| 243 |
_LIBCPP_POP_MACROS |
| 244 |
|
| 245 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) |
| 246 |
# include <__cxx03/functional> |
| 247 |
# include <__cxx03/type_traits> |
| 248 |
#endif |
| 249 |
|
| 250 |
#endif // _LIBCPP___CXX03_STACK |