| 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 | #ifndef _FORWARD_LIST_TCC |
| 31 | #define _FORWARD_LIST_TCC 1 |
| 32 | |
| 33 | namespace std _GLIBCXX_VISIBILITY(default) |
| 34 | { |
| 35 | _GLIBCXX_BEGIN_NAMESPACE_CONTAINER |
| 36 | |
| 37 | template<typename _Tp, typename _Alloc> |
| 38 | _Fwd_list_base<_Tp, _Alloc>:: |
| 39 | _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a) |
| 40 | : _M_impl(std::move(__a)) |
| 41 | { |
| 42 | if (__lst._M_get_Node_allocator() == _M_get_Node_allocator()) |
| 43 | { |
| 44 | this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next; |
| 45 | __lst._M_impl._M_head._M_next = 0; |
| 46 | } |
| 47 | else |
| 48 | this->_M_impl._M_head._M_next = 0; |
| 49 | } |
| 50 | |
| 51 | template<typename _Tp, typename _Alloc> |
| 52 | template<typename... _Args> |
| 53 | _Fwd_list_node_base* |
| 54 | _Fwd_list_base<_Tp, _Alloc>:: |
| 55 | _M_insert_after(const_iterator __pos, _Args&&... __args) |
| 56 | { |
| 57 | _Fwd_list_node_base* __to |
| 58 | = const_cast<_Fwd_list_node_base*>(__pos._M_node); |
| 59 | _Node* __thing = _M_create_node(std::forward<_Args>(__args)...); |
| 60 | __thing->_M_next = __to->_M_next; |
| 61 | __to->_M_next = __thing; |
| 62 | return __to->_M_next; |
| 63 | } |
| 64 | |
| 65 | template<typename _Tp, typename _Alloc> |
| 66 | _Fwd_list_node_base* |
| 67 | _Fwd_list_base<_Tp, _Alloc>:: |
| 68 | _M_erase_after(_Fwd_list_node_base* __pos) |
| 69 | { |
| 70 | _Node* __curr = static_cast<_Node*>(__pos->_M_next); |
| 71 | __pos->_M_next = __curr->_M_next; |
| 72 | _Node_alloc_traits::destroy(_M_get_Node_allocator(), |
| 73 | __curr->_M_valptr()); |
| 74 | __curr->~_Node(); |
| 75 | _M_put_node(__curr); |
| 76 | return __pos->_M_next; |
| 77 | } |
| 78 | |
| 79 | template<typename _Tp, typename _Alloc> |
| 80 | _Fwd_list_node_base* |
| 81 | _Fwd_list_base<_Tp, _Alloc>:: |
| 82 | _M_erase_after(_Fwd_list_node_base* __pos, |
| 83 | _Fwd_list_node_base* __last) |
| 84 | { |
| 85 | _Node* __curr = static_cast<_Node*>(__pos->_M_next); |
| 86 | while (__curr != __last) |
| 87 | { |
| 88 | _Node* __temp = __curr; |
| 89 | __curr = static_cast<_Node*>(__curr->_M_next); |
| 90 | _Node_alloc_traits::destroy(_M_get_Node_allocator(), |
| 91 | __temp->_M_valptr()); |
| 92 | __temp->~_Node(); |
| 93 | _M_put_node(__temp); |
| 94 | } |
| 95 | __pos->_M_next = __last; |
| 96 | return __last; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | template<typename _Tp, typename _Alloc> |
| 101 | template<typename _InputIterator> |
| 102 | void |
| 103 | forward_list<_Tp, _Alloc>:: |
| 104 | _M_range_initialize(_InputIterator __first, _InputIterator __last) |
| 105 | { |
| 106 | _Node_base* __to = &this->_M_impl._M_head; |
| 107 | for (; __first != __last; ++__first) |
| 108 | { |
| 109 | __to->_M_next = this->_M_create_node(*__first); |
| 110 | __to = __to->_M_next; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | template<typename _Tp, typename _Alloc> |
| 116 | void |
| 117 | forward_list<_Tp, _Alloc>:: |
| 118 | _M_fill_initialize(size_type __n, const value_type& __value) |
| 119 | { |
| 120 | _Node_base* __to = &this->_M_impl._M_head; |
| 121 | for (; __n; --__n) |
| 122 | { |
| 123 | __to->_M_next = this->_M_create_node(__value); |
| 124 | __to = __to->_M_next; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | template<typename _Tp, typename _Alloc> |
| 129 | void |
| 130 | forward_list<_Tp, _Alloc>:: |
| 131 | _M_default_initialize(size_type __n) |
| 132 | { |
| 133 | _Node_base* __to = &this->_M_impl._M_head; |
| 134 | for (; __n; --__n) |
| 135 | { |
| 136 | __to->_M_next = this->_M_create_node(); |
| 137 | __to = __to->_M_next; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | template<typename _Tp, typename _Alloc> |
| 142 | forward_list<_Tp, _Alloc>& |
| 143 | forward_list<_Tp, _Alloc>:: |
| 144 | operator=(const forward_list& __list) |
| 145 | { |
| 146 | if (std::__addressof(__list) != this) |
| 147 | { |
| 148 | if (_Node_alloc_traits::_S_propagate_on_copy_assign()) |
| 149 | { |
| 150 | auto& __this_alloc = this->_M_get_Node_allocator(); |
| 151 | auto& __that_alloc = __list._M_get_Node_allocator(); |
| 152 | if (!_Node_alloc_traits::_S_always_equal() |
| 153 | && __this_alloc != __that_alloc) |
| 154 | { |
| 155 | |
| 156 | clear(); |
| 157 | } |
| 158 | std::__alloc_on_copy(__this_alloc, __that_alloc); |
| 159 | } |
| 160 | assign(__list.cbegin(), __list.cend()); |
| 161 | } |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | template<typename _Tp, typename _Alloc> |
| 166 | void |
| 167 | forward_list<_Tp, _Alloc>:: |
| 168 | _M_default_insert_after(const_iterator __pos, size_type __n) |
| 169 | { |
| 170 | const_iterator __saved_pos = __pos; |
| 171 | __try |
| 172 | { |
| 173 | for (; __n; --__n) |
| 174 | __pos = emplace_after(__pos); |
| 175 | } |
| 176 | __catch(...) |
| 177 | { |
| 178 | erase_after(__saved_pos, ++__pos); |
| 179 | __throw_exception_again; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | template<typename _Tp, typename _Alloc> |
| 184 | void |
| 185 | forward_list<_Tp, _Alloc>:: |
| 186 | resize(size_type __sz) |
| 187 | { |
| 188 | iterator __k = before_begin(); |
| 189 | |
| 190 | size_type __len = 0; |
| 191 | while (__k._M_next() != end() && __len < __sz) |
| 192 | { |
| 193 | ++__k; |
| 194 | ++__len; |
| 195 | } |
| 196 | if (__len == __sz) |
| 197 | erase_after(__k, end()); |
| 198 | else |
| 199 | _M_default_insert_after(__k, __sz - __len); |
| 200 | } |
| 201 | |
| 202 | template<typename _Tp, typename _Alloc> |
| 203 | void |
| 204 | forward_list<_Tp, _Alloc>:: |
| 205 | resize(size_type __sz, const value_type& __val) |
| 206 | { |
| 207 | iterator __k = before_begin(); |
| 208 | |
| 209 | size_type __len = 0; |
| 210 | while (__k._M_next() != end() && __len < __sz) |
| 211 | { |
| 212 | ++__k; |
| 213 | ++__len; |
| 214 | } |
| 215 | if (__len == __sz) |
| 216 | erase_after(__k, end()); |
| 217 | else |
| 218 | insert_after(__k, __sz - __len, __val); |
| 219 | } |
| 220 | |
| 221 | template<typename _Tp, typename _Alloc> |
| 222 | typename forward_list<_Tp, _Alloc>::iterator |
| 223 | forward_list<_Tp, _Alloc>:: |
| 224 | _M_splice_after(const_iterator __pos, |
| 225 | const_iterator __before, const_iterator __last) |
| 226 | { |
| 227 | _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node); |
| 228 | _Node_base* __b = const_cast<_Node_base*>(__before._M_node); |
| 229 | _Node_base* __end = __b; |
| 230 | |
| 231 | while (__end && __end->_M_next != __last._M_node) |
| 232 | __end = __end->_M_next; |
| 233 | |
| 234 | if (__b != __end) |
| 235 | return iterator(__tmp->_M_transfer_after(__b, __end)); |
| 236 | else |
| 237 | return iterator(__tmp); |
| 238 | } |
| 239 | |
| 240 | template<typename _Tp, typename _Alloc> |
| 241 | void |
| 242 | forward_list<_Tp, _Alloc>:: |
| 243 | splice_after(const_iterator __pos, forward_list&&, |
| 244 | const_iterator __i) noexcept |
| 245 | { |
| 246 | const_iterator __j = __i; |
| 247 | ++__j; |
| 248 | |
| 249 | if (__pos == __i || __pos == __j) |
| 250 | return; |
| 251 | |
| 252 | _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node); |
| 253 | __tmp->_M_transfer_after(const_cast<_Node_base*>(__i._M_node), |
| 254 | const_cast<_Node_base*>(__j._M_node)); |
| 255 | } |
| 256 | |
| 257 | template<typename _Tp, typename _Alloc> |
| 258 | typename forward_list<_Tp, _Alloc>::iterator |
| 259 | forward_list<_Tp, _Alloc>:: |
| 260 | insert_after(const_iterator __pos, size_type __n, const _Tp& __val) |
| 261 | { |
| 262 | if (__n) |
| 263 | { |
| 264 | forward_list __tmp(__n, __val, get_allocator()); |
| 265 | return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end()); |
| 266 | } |
| 267 | else |
| 268 | return iterator(const_cast<_Node_base*>(__pos._M_node)); |
| 269 | } |
| 270 | |
| 271 | template<typename _Tp, typename _Alloc> |
| 272 | template<typename _InputIterator, typename> |
| 273 | typename forward_list<_Tp, _Alloc>::iterator |
| 274 | forward_list<_Tp, _Alloc>:: |
| 275 | insert_after(const_iterator __pos, |
| 276 | _InputIterator __first, _InputIterator __last) |
| 277 | { |
| 278 | forward_list __tmp(__first, __last, get_allocator()); |
| 279 | if (!__tmp.empty()) |
| 280 | return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end()); |
| 281 | else |
| 282 | return iterator(const_cast<_Node_base*>(__pos._M_node)); |
| 283 | } |
| 284 | |
| 285 | template<typename _Tp, typename _Alloc> |
| 286 | void |
| 287 | forward_list<_Tp, _Alloc>:: |
| 288 | remove(const _Tp& __val) |
| 289 | { |
| 290 | _Node_base* __curr = &this->_M_impl._M_head; |
| 291 | _Node_base* = nullptr; |
| 292 | |
| 293 | while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next)) |
| 294 | { |
| 295 | if (*__tmp->_M_valptr() == __val) |
| 296 | { |
| 297 | if (__tmp->_M_valptr() != std::__addressof(__val)) |
| 298 | { |
| 299 | this->_M_erase_after(__curr); |
| 300 | continue; |
| 301 | } |
| 302 | else |
| 303 | __extra = __curr; |
| 304 | } |
| 305 | __curr = __curr->_M_next; |
| 306 | } |
| 307 | |
| 308 | if (__extra) |
| 309 | this->_M_erase_after(__extra); |
| 310 | } |
| 311 | |
| 312 | template<typename _Tp, typename _Alloc> |
| 313 | template<typename _Pred> |
| 314 | void |
| 315 | forward_list<_Tp, _Alloc>:: |
| 316 | remove_if(_Pred __pred) |
| 317 | { |
| 318 | _Node_base* __curr = &this->_M_impl._M_head; |
| 319 | while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next)) |
| 320 | { |
| 321 | if (__pred(*__tmp->_M_valptr())) |
| 322 | this->_M_erase_after(__curr); |
| 323 | else |
| 324 | __curr = __curr->_M_next; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | template<typename _Tp, typename _Alloc> |
| 329 | template<typename _BinPred> |
| 330 | void |
| 331 | forward_list<_Tp, _Alloc>:: |
| 332 | unique(_BinPred __binary_pred) |
| 333 | { |
| 334 | iterator __first = begin(); |
| 335 | iterator __last = end(); |
| 336 | if (__first == __last) |
| 337 | return; |
| 338 | iterator __next = __first; |
| 339 | while (++__next != __last) |
| 340 | { |
| 341 | if (__binary_pred(*__first, *__next)) |
| 342 | erase_after(__first); |
| 343 | else |
| 344 | __first = __next; |
| 345 | __next = __first; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | template<typename _Tp, typename _Alloc> |
| 350 | template<typename _Comp> |
| 351 | void |
| 352 | forward_list<_Tp, _Alloc>:: |
| 353 | merge(forward_list&& __list, _Comp __comp) |
| 354 | { |
| 355 | _Node_base* __node = &this->_M_impl._M_head; |
| 356 | while (__node->_M_next && __list._M_impl._M_head._M_next) |
| 357 | { |
| 358 | if (__comp(*static_cast<_Node*> |
| 359 | (__list._M_impl._M_head._M_next)->_M_valptr(), |
| 360 | *static_cast<_Node*> |
| 361 | (__node->_M_next)->_M_valptr())) |
| 362 | __node->_M_transfer_after(&__list._M_impl._M_head, |
| 363 | __list._M_impl._M_head._M_next); |
| 364 | __node = __node->_M_next; |
| 365 | } |
| 366 | if (__list._M_impl._M_head._M_next) |
| 367 | { |
| 368 | __node->_M_next = __list._M_impl._M_head._M_next; |
| 369 | __list._M_impl._M_head._M_next = 0; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | template<typename _Tp, typename _Alloc> |
| 374 | bool |
| 375 | operator==(const forward_list<_Tp, _Alloc>& __lx, |
| 376 | const forward_list<_Tp, _Alloc>& __ly) |
| 377 | { |
| 378 | |
| 379 | |
| 380 | auto __ix = __lx.cbegin(); |
| 381 | auto __iy = __ly.cbegin(); |
| 382 | while (__ix != __lx.cend() && __iy != __ly.cend()) |
| 383 | { |
| 384 | if (!(*__ix == *__iy)) |
| 385 | return false; |
| 386 | ++__ix; |
| 387 | ++__iy; |
| 388 | } |
| 389 | if (__ix == __lx.cend() && __iy == __ly.cend()) |
| 390 | return true; |
| 391 | else |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | template<typename _Tp, class _Alloc> |
| 396 | template<typename _Comp> |
| 397 | void |
| 398 | forward_list<_Tp, _Alloc>:: |
| 399 | sort(_Comp __comp) |
| 400 | { |
| 401 | |
| 402 | _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next); |
| 403 | if (!__list) |
| 404 | return; |
| 405 | |
| 406 | unsigned long __insize = 1; |
| 407 | |
| 408 | while (1) |
| 409 | { |
| 410 | _Node* __p = __list; |
| 411 | __list = 0; |
| 412 | _Node* __tail = 0; |
| 413 | |
| 414 | |
| 415 | unsigned long __nmerges = 0; |
| 416 | |
| 417 | while (__p) |
| 418 | { |
| 419 | ++__nmerges; |
| 420 | |
| 421 | |
| 422 | _Node* __q = __p; |
| 423 | unsigned long __psize = 0; |
| 424 | for (unsigned long __i = 0; __i < __insize; ++__i) |
| 425 | { |
| 426 | ++__psize; |
| 427 | __q = static_cast<_Node*>(__q->_M_next); |
| 428 | if (!__q) |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | unsigned long __qsize = __insize; |
| 434 | |
| 435 | |
| 436 | while (__psize > 0 || (__qsize > 0 && __q)) |
| 437 | { |
| 438 | |
| 439 | _Node* __e; |
| 440 | if (__psize == 0) |
| 441 | { |
| 442 | |
| 443 | __e = __q; |
| 444 | __q = static_cast<_Node*>(__q->_M_next); |
| 445 | --__qsize; |
| 446 | } |
| 447 | else if (__qsize == 0 || !__q) |
| 448 | { |
| 449 | |
| 450 | __e = __p; |
| 451 | __p = static_cast<_Node*>(__p->_M_next); |
| 452 | --__psize; |
| 453 | } |
| 454 | else if (!__comp(*__q->_M_valptr(), *__p->_M_valptr())) |
| 455 | { |
| 456 | |
| 457 | __e = __p; |
| 458 | __p = static_cast<_Node*>(__p->_M_next); |
| 459 | --__psize; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | |
| 464 | __e = __q; |
| 465 | __q = static_cast<_Node*>(__q->_M_next); |
| 466 | --__qsize; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | if (__tail) |
| 471 | __tail->_M_next = __e; |
| 472 | else |
| 473 | __list = __e; |
| 474 | __tail = __e; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | __p = __q; |
| 479 | } |
| 480 | __tail->_M_next = 0; |
| 481 | |
| 482 | |
| 483 | |
| 484 | if (__nmerges <= 1) |
| 485 | { |
| 486 | this->_M_impl._M_head._M_next = __list; |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | __insize *= 2; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | _GLIBCXX_END_NAMESPACE_CONTAINER |
| 496 | } |
| 497 | |
| 498 | #endif |
| 499 | |
| 500 | |