libstdc++
forward_list.tcc
Go to the documentation of this file.
00001 // <forward_list.tcc> -*- C++ -*-
00002 
00003 // Copyright (C) 2008, 2009, 2010, 2011 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 bits/forward_list.tcc
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{forward_list}
00028  */
00029 
00030 #ifndef _FORWARD_LIST_TCC
00031 #define _FORWARD_LIST_TCC 1
00032 
00033 namespace std _GLIBCXX_VISIBILITY(default)
00034 {
00035 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00036 
00037   template<typename _Tp, typename _Alloc>
00038     _Fwd_list_base<_Tp, _Alloc>::
00039     _Fwd_list_base(const _Fwd_list_base& __lst, const _Node_alloc_type& __a)
00040     : _M_impl(__a)
00041     {
00042       this->_M_impl._M_head._M_next = 0;
00043       _Fwd_list_node_base* __to = &this->_M_impl._M_head;
00044       _Node* __curr = static_cast<_Node*>(__lst._M_impl._M_head._M_next);
00045 
00046       while (__curr)
00047         {
00048           __to->_M_next = _M_create_node(__curr->_M_value);
00049           __to = __to->_M_next;
00050           __curr = static_cast<_Node*>(__curr->_M_next);
00051         }
00052     }
00053 
00054   template<typename _Tp, typename _Alloc>
00055     template<typename... _Args>
00056       _Fwd_list_node_base*
00057       _Fwd_list_base<_Tp, _Alloc>::
00058       _M_insert_after(const_iterator __pos, _Args&&... __args)
00059       {
00060         _Fwd_list_node_base* __to
00061       = const_cast<_Fwd_list_node_base*>(__pos._M_node);
00062     _Node* __thing = _M_create_node(std::forward<_Args>(__args)...);
00063         __thing->_M_next = __to->_M_next;
00064         __to->_M_next = __thing;
00065         return __to->_M_next;
00066       }
00067 
00068   template<typename _Tp, typename _Alloc>
00069     _Fwd_list_node_base*
00070     _Fwd_list_base<_Tp, _Alloc>::
00071     _M_erase_after(_Fwd_list_node_base* __pos)
00072     {
00073       _Node* __curr = static_cast<_Node*>(__pos->_M_next);
00074       __pos->_M_next = __curr->_M_next;
00075       _M_get_Node_allocator().destroy(__curr);
00076       _M_put_node(__curr);
00077       return __pos->_M_next;
00078     }
00079 
00080   template<typename _Tp, typename _Alloc>
00081     _Fwd_list_node_base*
00082     _Fwd_list_base<_Tp, _Alloc>::
00083     _M_erase_after(_Fwd_list_node_base* __pos, 
00084                    _Fwd_list_node_base* __last)
00085     {
00086       _Node* __curr = static_cast<_Node*>(__pos->_M_next);
00087       while (__curr != __last)
00088         {
00089           _Node* __temp = __curr;
00090           __curr = static_cast<_Node*>(__curr->_M_next);
00091           _M_get_Node_allocator().destroy(__temp);
00092           _M_put_node(__temp);
00093         }
00094       __pos->_M_next = __last;
00095       return __last;
00096     }
00097 
00098   // Called by the range constructor to implement [23.1.1]/9
00099   template<typename _Tp, typename _Alloc>
00100     template<typename _InputIterator>
00101       void
00102       forward_list<_Tp, _Alloc>::
00103       _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
00104                              __false_type)
00105       {
00106         _Node_base* __to = &this->_M_impl._M_head;
00107         for (; __first != __last; ++__first)
00108           {
00109             __to->_M_next = this->_M_create_node(*__first);
00110             __to = __to->_M_next;
00111           }
00112       }
00113 
00114   // Called by forward_list(n,v,a), and the range constructor
00115   // when it turns out to be the same thing.
00116   template<typename _Tp, typename _Alloc>
00117     void
00118     forward_list<_Tp, _Alloc>::
00119     _M_fill_initialize(size_type __n, const value_type& __value)
00120     {
00121       _Node_base* __to = &this->_M_impl._M_head;
00122       for (; __n; --__n)
00123         {
00124           __to->_M_next = this->_M_create_node(__value);
00125           __to = __to->_M_next;
00126         }
00127     }
00128 
00129   template<typename _Tp, typename _Alloc>
00130     void
00131     forward_list<_Tp, _Alloc>::
00132     _M_default_initialize(size_type __n)
00133     {
00134       _Node_base* __to = &this->_M_impl._M_head;
00135       for (; __n; --__n)
00136         {
00137           __to->_M_next = this->_M_create_node();
00138           __to = __to->_M_next;
00139         }
00140     }
00141 
00142   template<typename _Tp, typename _Alloc>
00143     forward_list<_Tp, _Alloc>&
00144     forward_list<_Tp, _Alloc>::
00145     operator=(const forward_list& __list)
00146     {
00147       if (&__list != this)
00148         {
00149           iterator __prev1 = before_begin();
00150           iterator __curr1 = begin();
00151           iterator __last1 = end();
00152           const_iterator __first2 = __list.cbegin();
00153           const_iterator __last2 = __list.cend();
00154           while (__curr1 != __last1 && __first2 != __last2)
00155             {
00156               *__curr1 = *__first2;
00157               ++__prev1;
00158               ++__curr1;
00159               ++__first2;
00160             }
00161           if (__first2 == __last2)
00162             erase_after(__prev1, __last1);
00163           else
00164             insert_after(__prev1, __first2, __last2);
00165         }
00166       return *this;
00167     }
00168 
00169   template<typename _Tp, typename _Alloc>
00170     void
00171     forward_list<_Tp, _Alloc>::
00172     _M_default_insert_after(const_iterator __pos, size_type __n)
00173     {
00174       const_iterator __saved_pos = __pos;
00175       __try
00176     {
00177       for (; __n; --__n)
00178         __pos = emplace_after(__pos);
00179     }
00180       __catch(...)
00181     {
00182       erase_after(__saved_pos, ++__pos);
00183       __throw_exception_again;
00184     }
00185     }
00186 
00187   template<typename _Tp, typename _Alloc>
00188     void
00189     forward_list<_Tp, _Alloc>::
00190     resize(size_type __sz)
00191     {
00192       iterator __k = before_begin();
00193 
00194       size_type __len = 0;
00195       while (__k._M_next() != end() && __len < __sz)
00196         {
00197           ++__k;
00198           ++__len;
00199         }
00200       if (__len == __sz)
00201         erase_after(__k, end());
00202       else
00203     _M_default_insert_after(__k, __sz - __len);
00204     }
00205 
00206   template<typename _Tp, typename _Alloc>
00207     void
00208     forward_list<_Tp, _Alloc>::
00209     resize(size_type __sz, const value_type& __val)
00210     {
00211       iterator __k = before_begin();
00212 
00213       size_type __len = 0;
00214       while (__k._M_next() != end() && __len < __sz)
00215         {
00216           ++__k;
00217           ++__len;
00218         }
00219       if (__len == __sz)
00220         erase_after(__k, end());
00221       else
00222         insert_after(__k, __sz - __len, __val);
00223     }
00224 
00225   template<typename _Tp, typename _Alloc>
00226     typename forward_list<_Tp, _Alloc>::iterator
00227     forward_list<_Tp, _Alloc>::
00228     _M_splice_after(const_iterator __pos,
00229             const_iterator __before, const_iterator __last)
00230     {
00231       _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
00232       _Node_base* __b = const_cast<_Node_base*>(__before._M_node);
00233       _Node_base* __end = __b;
00234 
00235       while (__end && __end->_M_next != __last._M_node)
00236     __end = __end->_M_next;
00237 
00238       if (__b != __end)
00239     return iterator(__tmp->_M_transfer_after(__b, __end));      
00240       else
00241     return iterator(__tmp);
00242     }
00243 
00244   template<typename _Tp, typename _Alloc>
00245     void
00246     forward_list<_Tp, _Alloc>::
00247     splice_after(const_iterator __pos, forward_list&&,
00248          const_iterator __i)
00249     {
00250       const_iterator __j = __i;
00251       ++__j;
00252 
00253       if (__pos == __i || __pos == __j)
00254     return;
00255 
00256       _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
00257       __tmp->_M_transfer_after(const_cast<_Node_base*>(__i._M_node),
00258                    const_cast<_Node_base*>(__j._M_node));
00259     }
00260 
00261   template<typename _Tp, typename _Alloc>
00262     typename forward_list<_Tp, _Alloc>::iterator
00263     forward_list<_Tp, _Alloc>::
00264     insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
00265     {
00266       if (__n)
00267     {
00268       forward_list __tmp(__n, __val, get_allocator());
00269       return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
00270     }
00271       else
00272     return iterator(const_cast<_Node_base*>(__pos._M_node));
00273     }
00274 
00275   template<typename _Tp, typename _Alloc>
00276     template<typename _InputIterator>
00277       typename forward_list<_Tp, _Alloc>::iterator
00278       forward_list<_Tp, _Alloc>::
00279       insert_after(const_iterator __pos,
00280            _InputIterator __first, _InputIterator __last)
00281       {
00282     forward_list __tmp(__first, __last, get_allocator());
00283     if (!__tmp.empty())
00284       return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
00285     else
00286       return iterator(const_cast<_Node_base*>(__pos._M_node));
00287       }
00288 
00289   template<typename _Tp, typename _Alloc>
00290     void
00291     forward_list<_Tp, _Alloc>::
00292     remove(const _Tp& __val)
00293     {
00294       _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
00295       _Node* __extra = 0;
00296 
00297       while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
00298         {
00299           if (__tmp->_M_value == __val)
00300         {
00301           if (std::__addressof(__tmp->_M_value)
00302           != std::__addressof(__val))
00303         {
00304           this->_M_erase_after(__curr);
00305           continue;
00306         }
00307           else
00308         __extra = __curr;
00309         }
00310       __curr = static_cast<_Node*>(__curr->_M_next);
00311         }
00312 
00313       if (__extra)
00314     this->_M_erase_after(__extra);
00315     }
00316 
00317   template<typename _Tp, typename _Alloc>
00318     template<typename _Pred>
00319       void
00320       forward_list<_Tp, _Alloc>::
00321       remove_if(_Pred __pred)
00322       {
00323     _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
00324         while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
00325           {
00326             if (__pred(__tmp->_M_value))
00327               this->_M_erase_after(__curr);
00328             else
00329               __curr = static_cast<_Node*>(__curr->_M_next);
00330           }
00331       }
00332 
00333   template<typename _Tp, typename _Alloc>
00334     template<typename _BinPred>
00335       void
00336       forward_list<_Tp, _Alloc>::
00337       unique(_BinPred __binary_pred)
00338       {
00339         iterator __first = begin();
00340         iterator __last = end();
00341         if (__first == __last)
00342           return;
00343         iterator __next = __first;
00344         while (++__next != __last)
00345         {
00346           if (__binary_pred(*__first, *__next))
00347             erase_after(__first);
00348           else
00349             __first = __next;
00350           __next = __first;
00351         }
00352       }
00353 
00354   template<typename _Tp, typename _Alloc>
00355     template<typename _Comp>
00356       void
00357       forward_list<_Tp, _Alloc>::
00358       merge(forward_list&& __list, _Comp __comp)
00359       {
00360         _Node_base* __node = &this->_M_impl._M_head;
00361         while (__node->_M_next && __list._M_impl._M_head._M_next)
00362           {
00363             if (__comp(static_cast<_Node*>
00364                        (__list._M_impl._M_head._M_next)->_M_value,
00365                        static_cast<_Node*>
00366                        (__node->_M_next)->_M_value))
00367               __node->_M_transfer_after(&__list._M_impl._M_head,
00368                                         __list._M_impl._M_head._M_next);
00369             __node = __node->_M_next;
00370           }
00371         if (__list._M_impl._M_head._M_next)
00372           {
00373             __node->_M_next = __list._M_impl._M_head._M_next;
00374             __list._M_impl._M_head._M_next = 0;
00375           }
00376       }
00377 
00378   template<typename _Tp, typename _Alloc>
00379     bool
00380     operator==(const forward_list<_Tp, _Alloc>& __lx,
00381                const forward_list<_Tp, _Alloc>& __ly)
00382     {
00383       //  We don't have size() so we need to walk through both lists
00384       //  making sure both iterators are valid.
00385       auto __ix = __lx.cbegin();
00386       auto __iy = __ly.cbegin();
00387       while (__ix != __lx.cend() && __iy != __ly.cend())
00388         {
00389           if (*__ix != *__iy)
00390             return false;
00391           ++__ix;
00392           ++__iy;
00393         }
00394       if (__ix == __lx.cend() && __iy == __ly.cend())
00395         return true;
00396       else
00397         return false;
00398     }
00399 
00400   template<typename _Tp, class _Alloc>
00401     template<typename _Comp>
00402       void
00403       forward_list<_Tp, _Alloc>::
00404       sort(_Comp __comp)
00405       {
00406         // If `next' is 0, return immediately.
00407         _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
00408         if (!__list)
00409           return;
00410 
00411         unsigned long __insize = 1;
00412 
00413         while (1)
00414           {
00415             _Node* __p = __list;
00416             __list = 0;
00417             _Node* __tail = 0;
00418 
00419             // Count number of merges we do in this pass.
00420             unsigned long __nmerges = 0;
00421 
00422             while (__p)
00423               {
00424                 ++__nmerges;
00425                 // There exists a merge to be done.
00426                 // Step `insize' places along from p.
00427                 _Node* __q = __p;
00428                 unsigned long __psize = 0;
00429                 for (unsigned long __i = 0; __i < __insize; ++__i)
00430                   {
00431                     ++__psize;
00432                     __q = static_cast<_Node*>(__q->_M_next);
00433                     if (!__q)
00434                       break;
00435                   }
00436 
00437                 // If q hasn't fallen off end, we have two lists to merge.
00438                 unsigned long __qsize = __insize;
00439 
00440                 // Now we have two lists; merge them.
00441                 while (__psize > 0 || (__qsize > 0 && __q))
00442                   {
00443                     // Decide whether next node of merge comes from p or q.
00444                     _Node* __e;
00445                     if (__psize == 0)
00446                       {
00447                         // p is empty; e must come from q.
00448                         __e = __q;
00449                         __q = static_cast<_Node*>(__q->_M_next);
00450                         --__qsize;
00451                       }
00452                     else if (__qsize == 0 || !__q)
00453                       {
00454                         // q is empty; e must come from p.
00455                         __e = __p;
00456                         __p = static_cast<_Node*>(__p->_M_next);
00457                         --__psize;
00458                       }
00459                     else if (__comp(__p->_M_value, __q->_M_value))
00460                       {
00461                         // First node of p is lower; e must come from p.
00462                         __e = __p;
00463                         __p = static_cast<_Node*>(__p->_M_next);
00464                         --__psize;
00465                       }
00466                     else
00467                       {
00468                         // First node of q is lower; e must come from q.
00469                         __e = __q;
00470                         __q = static_cast<_Node*>(__q->_M_next);
00471                         --__qsize;
00472                       }
00473 
00474                     // Add the next node to the merged list.
00475                     if (__tail)
00476                       __tail->_M_next = __e;
00477                     else
00478                       __list = __e;
00479                     __tail = __e;
00480                   }
00481 
00482                 // Now p has stepped `insize' places along, and q has too.
00483                 __p = __q;
00484               }
00485             __tail->_M_next = 0;
00486 
00487             // If we have done only one merge, we're finished.
00488             // Allow for nmerges == 0, the empty list case.
00489             if (__nmerges <= 1)
00490               {
00491                 this->_M_impl._M_head._M_next = __list;
00492                 return;
00493               }
00494 
00495             // Otherwise repeat, merging lists twice the size.
00496             __insize *= 2;
00497           }
00498       }
00499  
00500 _GLIBCXX_END_NAMESPACE_CONTAINER
00501 } // namespace std
00502 
00503 #endif /* _FORWARD_LIST_TCC */
00504