Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- stack -----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_STACK |
| 12 | #define _LIBCPP_STACK |
| 13 | |
| 14 | /* |
| 15 | stack synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class T, class Container = deque<T>> |
| 21 | class stack |
| 22 | { |
| 23 | public: |
| 24 | typedef Container container_type; |
| 25 | typedef typename container_type::value_type value_type; |
| 26 | typedef typename container_type::reference reference; |
| 27 | typedef typename container_type::const_reference const_reference; |
| 28 | typedef typename container_type::size_type size_type; |
| 29 | |
| 30 | protected: |
| 31 | container_type c; |
| 32 | |
| 33 | public: |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 34 | stack() = default; |
| 35 | ~stack() = default; |
| 36 | |
| 37 | stack(const stack& q) = default; |
| 38 | stack(stack&& q) = default; |
| 39 | |
| 40 | stack& operator=(const stack& q) = default; |
| 41 | stack& operator=(stack&& q) = default; |
| 42 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | explicit stack(const container_type& c); |
| 44 | explicit stack(container_type&& c); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 48 | template <class Alloc> stack(const stack& c, const Alloc& a); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | template <class Alloc> stack(stack&& c, const Alloc& a); |
| 50 | |
| 51 | bool empty() const; |
| 52 | size_type size() const; |
| 53 | reference top(); |
| 54 | const_reference top() const; |
| 55 | |
| 56 | void push(const value_type& x); |
| 57 | void push(value_type&& x); |
| 58 | template <class... Args> void emplace(Args&&... args); |
| 59 | void pop(); |
| 60 | |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 61 | void swap(stack& c) noexcept(noexcept(swap(c, q.c))); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | template <class T, class Container> |
| 65 | bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); |
| 66 | template <class T, class Container> |
| 67 | bool operator< (const stack<T, Container>& x, const stack<T, Container>& y); |
| 68 | template <class T, class Container> |
| 69 | bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 70 | template <class T, class Container> |
| 71 | bool operator> (const stack<T, Container>& x, const stack<T, Container>& y); |
| 72 | template <class T, class Container> |
| 73 | bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 74 | template <class T, class Container> |
| 75 | bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y); |
| 76 | |
| 77 | template <class T, class Container> |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 78 | void swap(stack<T, Container>& x, stack<T, Container>& y) |
| 79 | noexcept(noexcept(x.swap(y))); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 80 | |
| 81 | } // std |
| 82 | |
| 83 | */ |
| 84 | |
| 85 | #include <__config> |
| 86 | #include <deque> |
| 87 | |
| 88 | #pragma GCC system_header |
| 89 | |
| 90 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 91 | |
| 92 | template <class _Tp, class _Container> class stack; |
| 93 | |
| 94 | template <class _Tp, class _Container> |
| 95 | bool |
| 96 | operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 97 | |
| 98 | template <class _Tp, class _Container> |
| 99 | bool |
| 100 | operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y); |
| 101 | |
| 102 | template <class _Tp, class _Container = deque<_Tp> > |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 103 | class _LIBCPP_VISIBLE stack |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | { |
| 105 | public: |
| 106 | typedef _Container container_type; |
| 107 | typedef typename container_type::value_type value_type; |
| 108 | typedef typename container_type::reference reference; |
| 109 | typedef typename container_type::const_reference const_reference; |
| 110 | typedef typename container_type::size_type size_type; |
| 111 | |
| 112 | protected: |
| 113 | container_type c; |
| 114 | |
| 115 | public: |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 116 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 117 | stack() |
| 118 | _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) |
| 119 | : c() {} |
| 120 | |
| 121 | _LIBCPP_INLINE_VISIBILITY |
| 122 | stack(const stack& __q) : c(__q.c) {} |
| 123 | |
| 124 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 125 | _LIBCPP_INLINE_VISIBILITY |
| 126 | stack(stack&& __q) |
| 127 | _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value) |
| 128 | : c(_STD::move(__q.c)) {} |
| 129 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 130 | |
| 131 | _LIBCPP_INLINE_VISIBILITY |
| 132 | stack& operator=(const stack& __q) {c = __q.c; return *this;} |
| 133 | |
| 134 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 135 | _LIBCPP_INLINE_VISIBILITY |
| 136 | stack& operator=(stack&& __q) |
| 137 | _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value) |
| 138 | {c = _STD::move(__q.c); return *this;} |
| 139 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 140 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 141 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 142 | explicit stack(const container_type& __c) : c(__c) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 143 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 144 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 145 | explicit stack(container_type&& __c) : c(_STD::move(__c)) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 146 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | template <class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 148 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | explicit stack(const _Alloc& __a, |
| 150 | typename enable_if<uses_allocator<container_type, |
| 151 | _Alloc>::value>::type* = 0) |
| 152 | : c(__a) {} |
| 153 | template <class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 154 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 155 | stack(const container_type& __c, const _Alloc& __a, |
| 156 | typename enable_if<uses_allocator<container_type, |
| 157 | _Alloc>::value>::type* = 0) |
| 158 | : c(__c, __a) {} |
| 159 | template <class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 160 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 161 | stack(const stack& __s, const _Alloc& __a, |
| 162 | typename enable_if<uses_allocator<container_type, |
| 163 | _Alloc>::value>::type* = 0) |
| 164 | : c(__s.c, __a) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 165 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 166 | template <class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 167 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 168 | stack(container_type&& __c, const _Alloc& __a, |
| 169 | typename enable_if<uses_allocator<container_type, |
| 170 | _Alloc>::value>::type* = 0) |
| 171 | : c(_STD::move(__c), __a) {} |
| 172 | template <class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 174 | stack(stack&& __s, const _Alloc& __a, |
| 175 | typename enable_if<uses_allocator<container_type, |
| 176 | _Alloc>::value>::type* = 0) |
| 177 | : c(_STD::move(__s.c), __a) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 178 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 181 | bool empty() const {return c.empty();} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 182 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 183 | size_type size() const {return c.size();} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 184 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | reference top() {return c.back();} |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 186 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | const_reference top() const {return c.back();} |
| 188 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 189 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 190 | void push(const value_type& __v) {c.push_back(__v);} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 191 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 192 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | void push(value_type&& __v) {c.push_back(_STD::move(__v));} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 194 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 195 | template <class... _Args> |
| 196 | _LIBCPP_INLINE_VISIBILITY |
| 197 | void emplace(_Args&&... __args) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 198 | {c.emplace_back(_STD::forward<_Args>(__args)...);} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 199 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 200 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 201 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 202 | void pop() {c.pop_back();} |
| 203 | |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 204 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | void swap(stack& __s) |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 206 | _NOEXCEPT_(__is_nothrow_swappable<container_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | { |
| 208 | using _STD::swap; |
| 209 | swap(c, __s.c); |
| 210 | } |
| 211 | |
| 212 | template <class T1, class _C1> |
| 213 | friend |
| 214 | bool |
| 215 | operator==(const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 216 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 217 | template <class T1, class _C1> |
| 218 | friend |
| 219 | bool |
| 220 | operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); |
| 221 | }; |
| 222 | |
| 223 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 224 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | bool |
| 226 | operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 227 | { |
| 228 | return __x.c == __y.c; |
| 229 | } |
| 230 | |
| 231 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 232 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | bool |
| 234 | operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 235 | { |
| 236 | return __x.c < __y.c; |
| 237 | } |
| 238 | |
| 239 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 240 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | bool |
| 242 | operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 243 | { |
| 244 | return !(__x == __y); |
| 245 | } |
| 246 | |
| 247 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 248 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | bool |
| 250 | operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 251 | { |
| 252 | return __y < __x; |
| 253 | } |
| 254 | |
| 255 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 256 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | bool |
| 258 | operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 259 | { |
| 260 | return !(__x < __y); |
| 261 | } |
| 262 | |
| 263 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 264 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | bool |
| 266 | operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) |
| 267 | { |
| 268 | return !(__y < __x); |
| 269 | } |
| 270 | |
| 271 | template <class _Tp, class _Container> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 272 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 273 | void |
| 274 | swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) |
Howard Hinnant | 58cd823 | 2011-06-04 22:09:19 +0000 | [diff] [blame] | 275 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 276 | { |
| 277 | __x.swap(__y); |
| 278 | } |
| 279 | |
| 280 | template <class _Tp, class _Container, class _Alloc> |
Howard Hinnant | 8d7a955 | 2010-09-23 17:31:07 +0000 | [diff] [blame] | 281 | struct _LIBCPP_VISIBLE uses_allocator<stack<_Tp, _Container>, _Alloc> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 282 | : public uses_allocator<_Container, _Alloc> |
| 283 | { |
| 284 | }; |
| 285 | |
| 286 | _LIBCPP_END_NAMESPACE_STD |
| 287 | |
| 288 | #endif // _LIBCPP_STACK |