| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | #ifndef _STL_STACK_H |
| 57 | #define _STL_STACK_H 1 |
| 58 | |
| 59 | #include <bits/concept_check.h> |
| 60 | #include <debug/debug.h> |
| 61 | #if __cplusplus >= 201103L |
| 62 | # include <bits/uses_allocator.h> |
| 63 | #endif |
| 64 | |
| 65 | namespace std _GLIBCXX_VISIBILITY(default) |
| 66 | { |
| 67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | template<typename _Tp, typename _Sequence = deque<_Tp> > |
| 99 | class stack |
| 100 | { |
| 101 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
| 102 | |
| 103 | typedef typename _Sequence::value_type _Sequence_value_type; |
| 104 | # if __cplusplus < 201103L |
| 105 | __glibcxx_class_requires(_Tp, _SGIAssignableConcept) |
| 106 | __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) |
| 107 | # endif |
| 108 | __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) |
| 109 | #endif |
| 110 | |
| 111 | template<typename _Tp1, typename _Seq1> |
| 112 | friend bool |
| 113 | operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); |
| 114 | |
| 115 | template<typename _Tp1, typename _Seq1> |
| 116 | friend bool |
| 117 | operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); |
| 118 | |
| 119 | #if __cplusplus >= 201103L |
| 120 | template<typename _Alloc> |
| 121 | using _Uses = typename |
| 122 | enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; |
| 123 | #endif |
| 124 | |
| 125 | public: |
| 126 | typedef typename _Sequence::value_type value_type; |
| 127 | typedef typename _Sequence::reference reference; |
| 128 | typedef typename _Sequence::const_reference const_reference; |
| 129 | typedef typename _Sequence::size_type size_type; |
| 130 | typedef _Sequence container_type; |
| 131 | |
| 132 | protected: |
| 133 | |
| 134 | _Sequence c; |
| 135 | |
| 136 | public: |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #if __cplusplus < 201103L |
| 142 | explicit |
| 143 | stack(const _Sequence& __c = _Sequence()) |
| 144 | : c(__c) { } |
| 145 | #else |
| 146 | template<typename _Seq = _Sequence, typename _Requires = typename |
| 147 | enable_if<is_default_constructible<_Seq>::value>::type> |
| 148 | stack() |
| 149 | : c() { } |
| 150 | |
| 151 | explicit |
| 152 | stack(const _Sequence& __c) |
| 153 | : c(__c) { } |
| 154 | |
| 155 | explicit |
| 156 | stack(_Sequence&& __c) |
| 157 | : c(std::move(__c)) { } |
| 158 | |
| 159 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
| 160 | explicit |
| 161 | stack(const _Alloc& __a) |
| 162 | : c(__a) { } |
| 163 | |
| 164 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
| 165 | stack(const _Sequence& __c, const _Alloc& __a) |
| 166 | : c(__c, __a) { } |
| 167 | |
| 168 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
| 169 | stack(_Sequence&& __c, const _Alloc& __a) |
| 170 | : c(std::move(__c), __a) { } |
| 171 | |
| 172 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
| 173 | stack(const stack& __q, const _Alloc& __a) |
| 174 | : c(__q.c, __a) { } |
| 175 | |
| 176 | template<typename _Alloc, typename _Requires = _Uses<_Alloc>> |
| 177 | stack(stack&& __q, const _Alloc& __a) |
| 178 | : c(std::move(__q.c), __a) { } |
| 179 | #endif |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | bool |
| 185 | empty() const |
| 186 | { return c.empty(); } |
| 187 | |
| 188 | |
| 189 | size_type |
| 190 | size() const |
| 191 | { return c.size(); } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | reference |
| 198 | top() |
| 199 | { |
| 200 | __glibcxx_requires_nonempty(); |
| 201 | return c.back(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | const_reference |
| 209 | top() const |
| 210 | { |
| 211 | __glibcxx_requires_nonempty(); |
| 212 | return c.back(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | void |
| 225 | push(const value_type& __x) |
| 226 | { c.push_back(__x); } |
| 227 | |
| 228 | #if __cplusplus >= 201103L |
| 229 | void |
| 230 | push(value_type&& __x) |
| 231 | { c.push_back(std::move(__x)); } |
| 232 | |
| 233 | #if __cplusplus > 201402L |
| 234 | template<typename... _Args> |
| 235 | decltype(auto) |
| 236 | emplace(_Args&&... __args) |
| 237 | { return c.emplace_back(std::forward<_Args>(__args)...); } |
| 238 | #else |
| 239 | template<typename... _Args> |
| 240 | void |
| 241 | emplace(_Args&&... __args) |
| 242 | { c.emplace_back(std::forward<_Args>(__args)...); } |
| 243 | #endif |
| 244 | #endif |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | void |
| 258 | pop() |
| 259 | { |
| 260 | __glibcxx_requires_nonempty(); |
| 261 | c.pop_back(); |
| 262 | } |
| 263 | |
| 264 | #if __cplusplus >= 201103L |
| 265 | void |
| 266 | swap(stack& __s) |
| 267 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
| 268 | noexcept(__is_nothrow_swappable<_Sequence>::value) |
| 269 | #else |
| 270 | noexcept(__is_nothrow_swappable<_Tp>::value) |
| 271 | #endif |
| 272 | { |
| 273 | using std::swap; |
| 274 | swap(c, __s.c); |
| 275 | } |
| 276 | #endif |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | template<typename _Tp, typename _Seq> |
| 292 | inline bool |
| 293 | operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 294 | { return __x.c == __y.c; } |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | template<typename _Tp, typename _Seq> |
| 310 | inline bool |
| 311 | operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 312 | { return __x.c < __y.c; } |
| 313 | |
| 314 | |
| 315 | template<typename _Tp, typename _Seq> |
| 316 | inline bool |
| 317 | operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 318 | { return !(__x == __y); } |
| 319 | |
| 320 | |
| 321 | template<typename _Tp, typename _Seq> |
| 322 | inline bool |
| 323 | operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 324 | { return __y < __x; } |
| 325 | |
| 326 | |
| 327 | template<typename _Tp, typename _Seq> |
| 328 | inline bool |
| 329 | operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 330 | { return !(__y < __x); } |
| 331 | |
| 332 | |
| 333 | template<typename _Tp, typename _Seq> |
| 334 | inline bool |
| 335 | operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y) |
| 336 | { return !(__x < __y); } |
| 337 | |
| 338 | #if __cplusplus >= 201103L |
| 339 | template<typename _Tp, typename _Seq> |
| 340 | inline |
| 341 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
| 342 | |
| 343 | typename enable_if<__is_swappable<_Seq>::value>::type |
| 344 | #else |
| 345 | void |
| 346 | #endif |
| 347 | swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y) |
| 348 | noexcept(noexcept(__x.swap(__y))) |
| 349 | { __x.swap(__y); } |
| 350 | |
| 351 | template<typename _Tp, typename _Seq, typename _Alloc> |
| 352 | struct uses_allocator<stack<_Tp, _Seq>, _Alloc> |
| 353 | : public uses_allocator<_Seq, _Alloc>::type { }; |
| 354 | #endif |
| 355 | |
| 356 | _GLIBCXX_END_NAMESPACE_VERSION |
| 357 | } |
| 358 | |
| 359 | #endif |
| 360 | |