libstdc++
|
00001 // <forward_list> -*- C++ -*- 00002 00003 // Copyright (C) 2010-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file debug/forward_list 00026 * This file is a GNU debug extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_DEBUG_FORWARD_LIST 00030 #define _GLIBCXX_DEBUG_FORWARD_LIST 1 00031 00032 #pragma GCC system_header 00033 00034 #include <forward_list> 00035 #include <debug/safe_sequence.h> 00036 #include <debug/safe_container.h> 00037 #include <debug/safe_iterator.h> 00038 00039 // Special validity check for forward_list ranges. 00040 #define __glibcxx_check_valid_fl_range(_First,_Last,_Dist) \ 00041 _GLIBCXX_DEBUG_VERIFY(_First._M_valid_range(_Last, _Dist, false), \ 00042 _M_message(__gnu_debug::__msg_valid_range) \ 00043 ._M_iterator(_First, #_First) \ 00044 ._M_iterator(_Last, #_Last)) 00045 00046 namespace __gnu_debug 00047 { 00048 /// Special iterators swap and invalidation for forward_list because of the 00049 /// before_begin iterator. 00050 template<typename _SafeSequence> 00051 class _Safe_forward_list 00052 : public _Safe_sequence<_SafeSequence> 00053 { 00054 _SafeSequence& 00055 _M_this() noexcept 00056 { return *static_cast<_SafeSequence*>(this); } 00057 00058 static void 00059 _M_swap_aux(_Safe_sequence_base& __lhs, 00060 _Safe_iterator_base*& __lhs_iterators, 00061 _Safe_sequence_base& __rhs, 00062 _Safe_iterator_base*& __rhs_iterators); 00063 00064 void _M_swap_single(_Safe_sequence_base&) noexcept; 00065 00066 protected: 00067 void 00068 _M_invalidate_all() 00069 { 00070 using _Base_const_iterator = __decltype(_M_this()._M_base().cend()); 00071 this->_M_invalidate_if([this](_Base_const_iterator __it) 00072 { 00073 return __it != _M_this()._M_base().cbefore_begin() 00074 && __it != _M_this()._M_base().cend(); }); 00075 } 00076 00077 void _M_swap(_Safe_sequence_base&) noexcept; 00078 }; 00079 00080 template<typename _SafeSequence> 00081 void 00082 _Safe_forward_list<_SafeSequence>:: 00083 _M_swap_aux(_Safe_sequence_base& __lhs, 00084 _Safe_iterator_base*& __lhs_iterators, 00085 _Safe_sequence_base& __rhs, 00086 _Safe_iterator_base*& __rhs_iterators) 00087 { 00088 using const_iterator = typename _SafeSequence::const_iterator; 00089 _Safe_iterator_base* __bbegin_its = 0; 00090 _Safe_iterator_base* __last_bbegin = 0; 00091 _SafeSequence& __rseq = static_cast<_SafeSequence&>(__rhs); 00092 00093 for (_Safe_iterator_base* __iter = __lhs_iterators; __iter;) 00094 { 00095 // Even iterator is cast to const_iterator, not a problem. 00096 _Safe_iterator_base* __victim_base = __iter; 00097 const_iterator* __victim = 00098 static_cast<const_iterator*>(__victim_base); 00099 __iter = __iter->_M_next; 00100 if (__victim->base() == __rseq._M_base().cbefore_begin()) 00101 { 00102 __victim->_M_unlink(); 00103 if (__lhs_iterators == __victim_base) 00104 __lhs_iterators = __victim_base->_M_next; 00105 if (__bbegin_its) 00106 { 00107 __victim_base->_M_next = __bbegin_its; 00108 __bbegin_its->_M_prior = __victim_base; 00109 } 00110 else 00111 __last_bbegin = __victim_base; 00112 __bbegin_its = __victim_base; 00113 } 00114 else 00115 __victim_base->_M_sequence = std::__addressof(__lhs); 00116 } 00117 00118 if (__bbegin_its) 00119 { 00120 if (__rhs_iterators) 00121 { 00122 __rhs_iterators->_M_prior = __last_bbegin; 00123 __last_bbegin->_M_next = __rhs_iterators; 00124 } 00125 __rhs_iterators = __bbegin_its; 00126 } 00127 } 00128 00129 template<typename _SafeSequence> 00130 void 00131 _Safe_forward_list<_SafeSequence>:: 00132 _M_swap_single(_Safe_sequence_base& __other) noexcept 00133 { 00134 std::swap(_M_this()._M_iterators, __other._M_iterators); 00135 std::swap(_M_this()._M_const_iterators, __other._M_const_iterators); 00136 // Useless, always 1 on forward_list 00137 //std::swap(_M_this()_M_version, __other._M_version); 00138 _Safe_iterator_base* __this_its = _M_this()._M_iterators; 00139 _M_swap_aux(__other, __other._M_iterators, 00140 _M_this(), _M_this()._M_iterators); 00141 _Safe_iterator_base* __this_const_its = _M_this()._M_const_iterators; 00142 _M_swap_aux(__other, __other._M_const_iterators, 00143 _M_this(), _M_this()._M_const_iterators); 00144 _M_swap_aux(_M_this(), __this_its, 00145 __other, __other._M_iterators); 00146 _M_swap_aux(_M_this(), __this_const_its, 00147 __other, __other._M_const_iterators); 00148 } 00149 00150 /* Special forward_list _M_swap version that does not swap the 00151 * before-begin ownership.*/ 00152 template<typename _SafeSequence> 00153 void 00154 _Safe_forward_list<_SafeSequence>:: 00155 _M_swap(_Safe_sequence_base& __other) noexcept 00156 { 00157 // We need to lock both sequences to swap 00158 using namespace __gnu_cxx; 00159 __mutex *__this_mutex = &_M_this()._M_get_mutex(); 00160 __mutex *__other_mutex = 00161 &static_cast<_SafeSequence&>(__other)._M_get_mutex(); 00162 if (__this_mutex == __other_mutex) 00163 { 00164 __scoped_lock __lock(*__this_mutex); 00165 _M_swap_single(__other); 00166 } 00167 else 00168 { 00169 __scoped_lock __l1(__this_mutex < __other_mutex 00170 ? *__this_mutex : *__other_mutex); 00171 __scoped_lock __l2(__this_mutex < __other_mutex 00172 ? *__other_mutex : *__this_mutex); 00173 _M_swap_single(__other); 00174 } 00175 } 00176 } 00177 00178 namespace std _GLIBCXX_VISIBILITY(default) 00179 { 00180 namespace __debug 00181 { 00182 /// Class std::forward_list with safety/checking/debug instrumentation. 00183 template<typename _Tp, typename _Alloc = std::allocator<_Tp> > 00184 class forward_list 00185 : public __gnu_debug::_Safe_container< 00186 forward_list<_Tp, _Alloc>, _Alloc, __gnu_debug::_Safe_forward_list>, 00187 public _GLIBCXX_STD_C::forward_list<_Tp, _Alloc> 00188 { 00189 typedef _GLIBCXX_STD_C::forward_list<_Tp, _Alloc> _Base; 00190 typedef __gnu_debug::_Safe_container< 00191 forward_list, _Alloc, __gnu_debug::_Safe_forward_list> _Safe; 00192 00193 typedef typename _Base::iterator _Base_iterator; 00194 typedef typename _Base::const_iterator _Base_const_iterator; 00195 00196 public: 00197 typedef typename _Base::reference reference; 00198 typedef typename _Base::const_reference const_reference; 00199 00200 typedef __gnu_debug::_Safe_iterator< 00201 _Base_iterator, forward_list> iterator; 00202 typedef __gnu_debug::_Safe_iterator< 00203 _Base_const_iterator, forward_list> const_iterator; 00204 00205 typedef typename _Base::size_type size_type; 00206 typedef typename _Base::difference_type difference_type; 00207 00208 typedef _Tp value_type; 00209 typedef typename _Base::allocator_type allocator_type; 00210 typedef typename _Base::pointer pointer; 00211 typedef typename _Base::const_pointer const_pointer; 00212 00213 // 23.2.3.1 construct/copy/destroy: 00214 00215 forward_list() = default; 00216 00217 explicit 00218 forward_list(const allocator_type& __al) noexcept 00219 : _Base(__al) { } 00220 00221 forward_list(const forward_list& __list, const allocator_type& __al) 00222 : _Base(__list, __al) 00223 { } 00224 00225 forward_list(forward_list&& __list, const allocator_type& __al) 00226 : _Safe(std::move(__list._M_safe()), __al), 00227 _Base(std::move(__list._M_base()), __al) 00228 { } 00229 00230 explicit 00231 forward_list(size_type __n, const allocator_type& __al = allocator_type()) 00232 : _Base(__n, __al) 00233 { } 00234 00235 forward_list(size_type __n, const _Tp& __value, 00236 const allocator_type& __al = allocator_type()) 00237 : _Base(__n, __value, __al) 00238 { } 00239 00240 template<typename _InputIterator, 00241 typename = std::_RequireInputIter<_InputIterator>> 00242 forward_list(_InputIterator __first, _InputIterator __last, 00243 const allocator_type& __al = allocator_type()) 00244 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first, 00245 __last)), 00246 __gnu_debug::__base(__last), __al) 00247 { } 00248 00249 forward_list(const forward_list&) = default; 00250 00251 forward_list(forward_list&&) = default; 00252 00253 forward_list(std::initializer_list<_Tp> __il, 00254 const allocator_type& __al = allocator_type()) 00255 : _Base(__il, __al) 00256 { } 00257 00258 ~forward_list() = default; 00259 00260 forward_list& 00261 operator=(const forward_list&) = default; 00262 00263 forward_list& 00264 operator=(forward_list&&) = default; 00265 00266 forward_list& 00267 operator=(std::initializer_list<_Tp> __il) 00268 { 00269 _M_base() = __il; 00270 this->_M_invalidate_all(); 00271 return *this; 00272 } 00273 00274 template<typename _InputIterator, 00275 typename = std::_RequireInputIter<_InputIterator>> 00276 void 00277 assign(_InputIterator __first, _InputIterator __last) 00278 { 00279 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 00280 __glibcxx_check_valid_range2(__first, __last, __dist); 00281 00282 if (__dist.second >= __gnu_debug::__dp_sign) 00283 _Base::assign(__gnu_debug::__unsafe(__first), 00284 __gnu_debug::__unsafe(__last)); 00285 else 00286 _Base::assign(__first, __last); 00287 00288 this->_M_invalidate_all(); 00289 } 00290 00291 void 00292 assign(size_type __n, const _Tp& __val) 00293 { 00294 _Base::assign(__n, __val); 00295 this->_M_invalidate_all(); 00296 } 00297 00298 void 00299 assign(std::initializer_list<_Tp> __il) 00300 { 00301 _Base::assign(__il); 00302 this->_M_invalidate_all(); 00303 } 00304 00305 using _Base::get_allocator; 00306 00307 // iterators: 00308 00309 iterator 00310 before_begin() noexcept 00311 { return iterator(_Base::before_begin(), this); } 00312 00313 const_iterator 00314 before_begin() const noexcept 00315 { return const_iterator(_Base::before_begin(), this); } 00316 00317 iterator 00318 begin() noexcept 00319 { return iterator(_Base::begin(), this); } 00320 00321 const_iterator 00322 begin() const noexcept 00323 { return const_iterator(_Base::begin(), this); } 00324 00325 iterator 00326 end() noexcept 00327 { return iterator(_Base::end(), this); } 00328 00329 const_iterator 00330 end() const noexcept 00331 { return const_iterator(_Base::end(), this); } 00332 00333 const_iterator 00334 cbegin() const noexcept 00335 { return const_iterator(_Base::cbegin(), this); } 00336 00337 const_iterator 00338 cbefore_begin() const noexcept 00339 { return const_iterator(_Base::cbefore_begin(), this); } 00340 00341 const_iterator 00342 cend() const noexcept 00343 { return const_iterator(_Base::cend(), this); } 00344 00345 using _Base::empty; 00346 using _Base::max_size; 00347 00348 // element access: 00349 00350 reference 00351 front() 00352 { 00353 __glibcxx_check_nonempty(); 00354 return _Base::front(); 00355 } 00356 00357 const_reference 00358 front() const 00359 { 00360 __glibcxx_check_nonempty(); 00361 return _Base::front(); 00362 } 00363 00364 // modiļ¬ers: 00365 00366 using _Base::emplace_front; 00367 using _Base::push_front; 00368 00369 void 00370 pop_front() 00371 { 00372 __glibcxx_check_nonempty(); 00373 this->_M_invalidate_if([this](_Base_const_iterator __it) 00374 { return __it == this->_M_base().cbegin(); }); 00375 _Base::pop_front(); 00376 } 00377 00378 template<typename... _Args> 00379 iterator 00380 emplace_after(const_iterator __pos, _Args&&... __args) 00381 { 00382 __glibcxx_check_insert_after(__pos); 00383 return iterator(_Base::emplace_after(__pos.base(), 00384 std::forward<_Args>(__args)...), 00385 this); 00386 } 00387 00388 iterator 00389 insert_after(const_iterator __pos, const _Tp& __val) 00390 { 00391 __glibcxx_check_insert_after(__pos); 00392 return iterator(_Base::insert_after(__pos.base(), __val), this); 00393 } 00394 00395 iterator 00396 insert_after(const_iterator __pos, _Tp&& __val) 00397 { 00398 __glibcxx_check_insert_after(__pos); 00399 return iterator(_Base::insert_after(__pos.base(), std::move(__val)), 00400 this); 00401 } 00402 00403 iterator 00404 insert_after(const_iterator __pos, size_type __n, const _Tp& __val) 00405 { 00406 __glibcxx_check_insert_after(__pos); 00407 return iterator(_Base::insert_after(__pos.base(), __n, __val), 00408 this); 00409 } 00410 00411 template<typename _InputIterator, 00412 typename = std::_RequireInputIter<_InputIterator>> 00413 iterator 00414 insert_after(const_iterator __pos, 00415 _InputIterator __first, _InputIterator __last) 00416 { 00417 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 00418 __glibcxx_check_insert_range_after(__pos, __first, __last, __dist); 00419 00420 if (__dist.second >= __gnu_debug::__dp_sign) 00421 return 00422 { 00423 _Base::insert_after(__pos.base(), 00424 __gnu_debug::__unsafe(__first), 00425 __gnu_debug::__unsafe(__last)), 00426 this 00427 }; 00428 else 00429 return { _Base::insert_after(__pos.base(), __first, __last), this }; 00430 } 00431 00432 iterator 00433 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il) 00434 { 00435 __glibcxx_check_insert_after(__pos); 00436 return iterator(_Base::insert_after(__pos.base(), __il), this); 00437 } 00438 00439 private: 00440 _Base_iterator 00441 _M_erase_after(_Base_const_iterator __pos) 00442 { 00443 _Base_const_iterator __next = std::next(__pos); 00444 this->_M_invalidate_if([__next](_Base_const_iterator __it) 00445 { return __it == __next; }); 00446 return _Base::erase_after(__pos); 00447 } 00448 public: 00449 iterator 00450 erase_after(const_iterator __pos) 00451 { 00452 __glibcxx_check_erase_after(__pos); 00453 return iterator(_M_erase_after(__pos.base()), this); 00454 } 00455 00456 iterator 00457 erase_after(const_iterator __pos, const_iterator __last) 00458 { 00459 __glibcxx_check_erase_range_after(__pos, __last); 00460 for (_Base_const_iterator __victim = std::next(__pos.base()); 00461 __victim != __last.base(); ++__victim) 00462 { 00463 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00464 _M_message(__gnu_debug::__msg_valid_range2) 00465 ._M_sequence(*this, "this") 00466 ._M_iterator(__pos, "pos") 00467 ._M_iterator(__last, "last")); 00468 this->_M_invalidate_if([__victim](_Base_const_iterator __it) 00469 { return __it == __victim; }); 00470 } 00471 return iterator(_Base::erase_after(__pos.base(), __last.base()), this); 00472 } 00473 00474 void 00475 swap(forward_list& __list) 00476 noexcept( noexcept(declval<_Base&>().swap(__list)) ) 00477 { 00478 _Safe::_M_swap(__list); 00479 _Base::swap(__list); 00480 } 00481 00482 void 00483 resize(size_type __sz) 00484 { 00485 this->_M_detach_singular(); 00486 00487 // if __sz < size(), invalidate all iterators in [begin+__sz, end() 00488 _Base_iterator __victim = _Base::begin(); 00489 _Base_iterator __end = _Base::end(); 00490 for (size_type __i = __sz; __victim != __end && __i > 0; --__i) 00491 ++__victim; 00492 00493 for (; __victim != __end; ++__victim) 00494 { 00495 this->_M_invalidate_if([__victim](_Base_const_iterator __it) 00496 { return __it == __victim; }); 00497 } 00498 00499 __try 00500 { 00501 _Base::resize(__sz); 00502 } 00503 __catch(...) 00504 { 00505 this->_M_revalidate_singular(); 00506 __throw_exception_again; 00507 } 00508 } 00509 00510 void 00511 resize(size_type __sz, const value_type& __val) 00512 { 00513 this->_M_detach_singular(); 00514 00515 // if __sz < size(), invalidate all iterators in [begin+__sz, end()) 00516 _Base_iterator __victim = _Base::begin(); 00517 _Base_iterator __end = _Base::end(); 00518 for (size_type __i = __sz; __victim != __end && __i > 0; --__i) 00519 ++__victim; 00520 00521 for (; __victim != __end; ++__victim) 00522 { 00523 this->_M_invalidate_if([__victim](_Base_const_iterator __it) 00524 { return __it == __victim; }); 00525 } 00526 00527 __try 00528 { 00529 _Base::resize(__sz, __val); 00530 } 00531 __catch(...) 00532 { 00533 this->_M_revalidate_singular(); 00534 __throw_exception_again; 00535 } 00536 } 00537 00538 void 00539 clear() noexcept 00540 { 00541 _Base::clear(); 00542 this->_M_invalidate_all(); 00543 } 00544 00545 // 23.2.3.5 forward_list operations: 00546 void 00547 splice_after(const_iterator __pos, forward_list&& __list) 00548 { 00549 __glibcxx_check_insert_after(__pos); 00550 _GLIBCXX_DEBUG_VERIFY(std::__addressof(__list) != this, 00551 _M_message(__gnu_debug::__msg_self_splice) 00552 ._M_sequence(*this, "this")); 00553 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(), 00554 _M_message(__gnu_debug::__msg_splice_alloc) 00555 ._M_sequence(*this) 00556 ._M_sequence(__list, "__list")); 00557 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it) 00558 { 00559 return __it != __list._M_base().cbefore_begin() 00560 && __it != __list._M_base().end(); 00561 }); 00562 _Base::splice_after(__pos.base(), std::move(__list._M_base())); 00563 } 00564 00565 void 00566 splice_after(const_iterator __pos, forward_list& __list) 00567 { splice_after(__pos, std::move(__list)); } 00568 00569 void 00570 splice_after(const_iterator __pos, forward_list&& __list, 00571 const_iterator __i) 00572 { 00573 __glibcxx_check_insert_after(__pos); 00574 _GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(), 00575 _M_message(__gnu_debug::__msg_splice_bad) 00576 ._M_iterator(__i, "__i")); 00577 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__list)), 00578 _M_message(__gnu_debug::__msg_splice_other) 00579 ._M_iterator(__i, "__i") 00580 ._M_sequence(__list, "__list")); 00581 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(), 00582 _M_message(__gnu_debug::__msg_splice_alloc) 00583 ._M_sequence(*this) 00584 ._M_sequence(__list, "__list")); 00585 00586 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00587 // 250. splicing invalidates iterators 00588 _Base_const_iterator __next = std::next(__i.base()); 00589 this->_M_transfer_from_if(__list, [__next](_Base_const_iterator __it) 00590 { return __it == __next; }); 00591 _Base::splice_after(__pos.base(), std::move(__list._M_base()), 00592 __i.base()); 00593 } 00594 00595 void 00596 splice_after(const_iterator __pos, forward_list& __list, 00597 const_iterator __i) 00598 { splice_after(__pos, std::move(__list), __i); } 00599 00600 void 00601 splice_after(const_iterator __pos, forward_list&& __list, 00602 const_iterator __before, const_iterator __last) 00603 { 00604 typename __gnu_debug::_Distance_traits<const_iterator>::__type __dist; 00605 auto __listptr = std::__addressof(__list); 00606 __glibcxx_check_insert_after(__pos); 00607 __glibcxx_check_valid_fl_range(__before, __last, __dist); 00608 _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr), 00609 _M_message(__gnu_debug::__msg_splice_other) 00610 ._M_sequence(__list, "list") 00611 ._M_iterator(__before, "before")); 00612 _GLIBCXX_DEBUG_VERIFY(__before._M_dereferenceable() 00613 || __before._M_is_before_begin(), 00614 _M_message(__gnu_debug::__msg_valid_range2) 00615 ._M_sequence(__list, "list") 00616 ._M_iterator(__before, "before") 00617 ._M_iterator(__last, "last")); 00618 _GLIBCXX_DEBUG_VERIFY(__before != __last, 00619 _M_message(__gnu_debug::__msg_valid_range2) 00620 ._M_sequence(__list, "list") 00621 ._M_iterator(__before, "before") 00622 ._M_iterator(__last, "last")); 00623 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(), 00624 _M_message(__gnu_debug::__msg_splice_alloc) 00625 ._M_sequence(*this) 00626 ._M_sequence(__list, "__list")); 00627 00628 for (_Base_const_iterator __tmp = std::next(__before.base()); 00629 __tmp != __last.base(); ++__tmp) 00630 { 00631 _GLIBCXX_DEBUG_VERIFY(__tmp != __list._M_base().end(), 00632 _M_message(__gnu_debug::__msg_valid_range2) 00633 ._M_sequence(__list, "list") 00634 ._M_iterator(__before, "before") 00635 ._M_iterator(__last, "last")); 00636 _GLIBCXX_DEBUG_VERIFY(__listptr != this || __tmp != __pos.base(), 00637 _M_message(__gnu_debug::__msg_splice_overlap) 00638 ._M_iterator(__tmp, "position") 00639 ._M_iterator(__before, "before") 00640 ._M_iterator(__last, "last")); 00641 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00642 // 250. splicing invalidates iterators 00643 this->_M_transfer_from_if(__list, [__tmp](_Base_const_iterator __it) 00644 { return __it == __tmp; }); 00645 } 00646 00647 _Base::splice_after(__pos.base(), std::move(__list._M_base()), 00648 __before.base(), __last.base()); 00649 } 00650 00651 void 00652 splice_after(const_iterator __pos, forward_list& __list, 00653 const_iterator __before, const_iterator __last) 00654 { splice_after(__pos, std::move(__list), __before, __last); } 00655 00656 void 00657 remove(const _Tp& __val) 00658 { 00659 _Base_iterator __x = _Base::before_begin(); 00660 _Base_iterator __old = __x++; 00661 while (__x != _Base::end()) 00662 { 00663 if (*__x == __val) 00664 __x = _M_erase_after(__old); 00665 else 00666 __old = __x++; 00667 } 00668 } 00669 00670 template<typename _Pred> 00671 void 00672 remove_if(_Pred __pred) 00673 { 00674 _Base_iterator __x = _Base::before_begin(); 00675 _Base_iterator __old = __x++; 00676 while (__x != _Base::end()) 00677 { 00678 if (__pred(*__x)) 00679 __x = _M_erase_after(__old); 00680 else 00681 __old = __x++; 00682 } 00683 } 00684 00685 void 00686 unique() 00687 { 00688 _Base_iterator __first = _Base::begin(); 00689 _Base_iterator __last = _Base::end(); 00690 if (__first == __last) 00691 return; 00692 _Base_iterator __next = std::next(__first); 00693 while (__next != __last) 00694 { 00695 if (*__first == *__next) 00696 __next = _M_erase_after(__first); 00697 else 00698 __first = __next++; 00699 } 00700 } 00701 00702 template<typename _BinPred> 00703 void 00704 unique(_BinPred __binary_pred) 00705 { 00706 _Base_iterator __first = _Base::begin(); 00707 _Base_iterator __last = _Base::end(); 00708 if (__first == __last) 00709 return; 00710 _Base_iterator __next = std::next(__first); 00711 while (__next != __last) 00712 { 00713 if (__binary_pred(*__first, *__next)) 00714 __next = _M_erase_after(__first); 00715 else 00716 __first = __next++; 00717 } 00718 } 00719 00720 void 00721 merge(forward_list&& __list) 00722 { 00723 if (this != std::__addressof(__list)) 00724 { 00725 __glibcxx_check_sorted(_Base::begin(), _Base::end()); 00726 __glibcxx_check_sorted(__list._M_base().begin(), 00727 __list._M_base().end()); 00728 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it) 00729 { 00730 return __it != __list._M_base().cbefore_begin() 00731 && __it != __list._M_base().cend(); 00732 }); 00733 _Base::merge(std::move(__list._M_base())); 00734 } 00735 } 00736 00737 void 00738 merge(forward_list& __list) 00739 { merge(std::move(__list)); } 00740 00741 template<typename _Comp> 00742 void 00743 merge(forward_list&& __list, _Comp __comp) 00744 { 00745 if (this != std::__addressof(__list)) 00746 { 00747 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp); 00748 __glibcxx_check_sorted_pred(__list._M_base().begin(), 00749 __list._M_base().end(), __comp); 00750 this->_M_transfer_from_if(__list, 00751 [&__list](_Base_const_iterator __it) 00752 { 00753 return __it != __list._M_base().cbefore_begin() 00754 && __it != __list._M_base().cend(); 00755 }); 00756 _Base::merge(std::move(__list._M_base()), __comp); 00757 } 00758 } 00759 00760 template<typename _Comp> 00761 void 00762 merge(forward_list& __list, _Comp __comp) 00763 { merge(std::move(__list), __comp); } 00764 00765 using _Base::sort; 00766 using _Base::reverse; 00767 00768 _Base& 00769 _M_base() noexcept { return *this; } 00770 00771 const _Base& 00772 _M_base() const noexcept { return *this; } 00773 }; 00774 00775 template<typename _Tp, typename _Alloc> 00776 bool 00777 operator==(const forward_list<_Tp, _Alloc>& __lx, 00778 const forward_list<_Tp, _Alloc>& __ly) 00779 { return __lx._M_base() == __ly._M_base(); } 00780 00781 template<typename _Tp, typename _Alloc> 00782 inline bool 00783 operator<(const forward_list<_Tp, _Alloc>& __lx, 00784 const forward_list<_Tp, _Alloc>& __ly) 00785 { return __lx._M_base() < __ly._M_base(); } 00786 00787 template<typename _Tp, typename _Alloc> 00788 inline bool 00789 operator!=(const forward_list<_Tp, _Alloc>& __lx, 00790 const forward_list<_Tp, _Alloc>& __ly) 00791 { return !(__lx == __ly); } 00792 00793 /// Based on operator< 00794 template<typename _Tp, typename _Alloc> 00795 inline bool 00796 operator>(const forward_list<_Tp, _Alloc>& __lx, 00797 const forward_list<_Tp, _Alloc>& __ly) 00798 { return (__ly < __lx); } 00799 00800 /// Based on operator< 00801 template<typename _Tp, typename _Alloc> 00802 inline bool 00803 operator>=(const forward_list<_Tp, _Alloc>& __lx, 00804 const forward_list<_Tp, _Alloc>& __ly) 00805 { return !(__lx < __ly); } 00806 00807 /// Based on operator< 00808 template<typename _Tp, typename _Alloc> 00809 inline bool 00810 operator<=(const forward_list<_Tp, _Alloc>& __lx, 00811 const forward_list<_Tp, _Alloc>& __ly) 00812 { return !(__ly < __lx); } 00813 00814 /// See std::forward_list::swap(). 00815 template<typename _Tp, typename _Alloc> 00816 inline void 00817 swap(forward_list<_Tp, _Alloc>& __lx, forward_list<_Tp, _Alloc>& __ly) 00818 noexcept(noexcept(__lx.swap(__ly))) 00819 { __lx.swap(__ly); } 00820 00821 } // namespace __debug 00822 } // namespace std 00823 00824 namespace __gnu_debug 00825 { 00826 template<typename _Tp, typename _Alloc> 00827 struct _BeforeBeginHelper<std::__debug::forward_list<_Tp, _Alloc> > 00828 { 00829 typedef std::__debug::forward_list<_Tp, _Alloc> _Sequence; 00830 00831 template<typename _Iterator> 00832 static bool 00833 _S_Is(const _Safe_iterator<_Iterator, _Sequence>& __it) 00834 { 00835 return 00836 __it.base() == __it._M_get_sequence()->_M_base().before_begin(); 00837 } 00838 00839 template<typename _Iterator> 00840 static bool 00841 _S_Is_Beginnest(const _Safe_iterator<_Iterator, _Sequence>& __it) 00842 { return _S_Is(__it); } 00843 }; 00844 00845 template<typename _Tp, typename _Alloc> 00846 struct _Sequence_traits<std::__debug::forward_list<_Tp, _Alloc> > 00847 { 00848 typedef typename std::__debug::forward_list<_Tp, _Alloc>::iterator _It; 00849 00850 static typename _Distance_traits<_It>::__type 00851 _S_size(const std::__debug::forward_list<_Tp, _Alloc>& __seq) 00852 { 00853 return __seq.empty() 00854 ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_equality); 00855 } 00856 }; 00857 00858 #ifndef _GLIBCXX_DEBUG_PEDANTIC 00859 template<class _Tp, class _Alloc> 00860 struct _Insert_range_from_self_is_safe< 00861 std::__debug::forward_list<_Tp, _Alloc> > 00862 { enum { __value = 1 }; }; 00863 #endif 00864 } 00865 00866 #endif