forward_list.tcc

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