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_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
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, const 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       _Node* __extra = 0;
00290 
00291       while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
00292         {
00293           if (__tmp->_M_value == __val)
00294         {
00295           if (std::__addressof(__tmp->_M_value)
00296           != std::__addressof(__val))
00297         {
00298           this->_M_erase_after(__curr);
00299           continue;
00300         }
00301           else
00302         __extra = __curr;
00303         }
00304       __curr = static_cast<_Node*>(__curr->_M_next);
00305         }
00306 
00307       if (__extra)
00308     this->_M_erase_after(__extra);
00309     }
00310 
00311   template<typename _Tp, typename _Alloc>
00312     template<typename _Pred>
00313       void
00314       forward_list<_Tp, _Alloc>::
00315       remove_if(_Pred __pred)
00316       {
00317     _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
00318         while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
00319           {
00320             if (__pred(__tmp->_M_value))
00321               this->_M_erase_after(__curr);
00322             else
00323               __curr = static_cast<_Node*>(__curr->_M_next);
00324           }
00325       }
00326 
00327   template<typename _Tp, typename _Alloc>
00328     template<typename _BinPred>
00329       void
00330       forward_list<_Tp, _Alloc>::
00331       unique(_BinPred __binary_pred)
00332       {
00333         iterator __first = begin();
00334         iterator __last = end();
00335         if (__first == __last)
00336           return;
00337         iterator __next = __first;
00338         while (++__next != __last)
00339         {
00340           if (__binary_pred(*__first, *__next))
00341             erase_after(__first);
00342           else
00343             __first = __next;
00344           __next = __first;
00345         }
00346       }
00347 
00348   template<typename _Tp, typename _Alloc>
00349     template<typename _Comp>
00350       void
00351       forward_list<_Tp, _Alloc>::
00352       merge(forward_list&& __list, _Comp __comp)
00353       {
00354         _Node_base* __node = &this->_M_impl._M_head;
00355         while (__node->_M_next && __list._M_impl._M_head._M_next)
00356           {
00357             if (__comp(static_cast<_Node*>
00358                        (__list._M_impl._M_head._M_next)->_M_value,
00359                        static_cast<_Node*>
00360                        (__node->_M_next)->_M_value))
00361               __node->_M_transfer_after(&__list._M_impl._M_head,
00362                                         __list._M_impl._M_head._M_next);
00363             __node = __node->_M_next;
00364           }
00365         if (__list._M_impl._M_head._M_next)
00366           {
00367             __node->_M_next = __list._M_impl._M_head._M_next;
00368             __list._M_impl._M_head._M_next = 0;
00369           }
00370       }
00371 
00372   template<typename _Tp, typename _Alloc>
00373     bool
00374     operator==(const forward_list<_Tp, _Alloc>& __lx,
00375                const forward_list<_Tp, _Alloc>& __ly)
00376     {
00377       //  We don't have size() so we need to walk through both lists
00378       //  making sure both iterators are valid.
00379       auto __ix = __lx.cbegin();
00380       auto __iy = __ly.cbegin();
00381       while (__ix != __lx.cend() && __iy != __ly.cend())
00382         {
00383           if (*__ix != *__iy)
00384             return false;
00385           ++__ix;
00386           ++__iy;
00387         }
00388       if (__ix == __lx.cend() && __iy == __ly.cend())
00389         return true;
00390       else
00391         return false;
00392     }
00393 
00394   template<typename _Tp, class _Alloc>
00395     template<typename _Comp>
00396       void
00397       forward_list<_Tp, _Alloc>::
00398       sort(_Comp __comp)
00399       {
00400         // If `next' is 0, return immediately.
00401         _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
00402         if (!__list)
00403           return;
00404 
00405         unsigned long __insize = 1;
00406 
00407         while (1)
00408           {
00409             _Node* __p = __list;
00410             __list = 0;
00411             _Node* __tail = 0;
00412 
00413             // Count number of merges we do in this pass.
00414             unsigned long __nmerges = 0;
00415 
00416             while (__p)
00417               {
00418                 ++__nmerges;
00419                 // There exists a merge to be done.
00420                 // Step `insize' places along from p.
00421                 _Node* __q = __p;
00422                 unsigned long __psize = 0;
00423                 for (unsigned long __i = 0; __i < __insize; ++__i)
00424                   {
00425                     ++__psize;
00426                     __q = static_cast<_Node*>(__q->_M_next);
00427                     if (!__q)
00428                       break;
00429                   }
00430 
00431                 // If q hasn't fallen off end, we have two lists to merge.
00432                 unsigned long __qsize = __insize;
00433 
00434                 // Now we have two lists; merge them.
00435                 while (__psize > 0 || (__qsize > 0 && __q))
00436                   {
00437                     // Decide whether next node of merge comes from p or q.
00438                     _Node* __e;
00439                     if (__psize == 0)
00440                       {
00441                         // p is empty; e must come from q.
00442                         __e = __q;
00443                         __q = static_cast<_Node*>(__q->_M_next);
00444                         --__qsize;
00445                       }
00446                     else if (__qsize == 0 || !__q)
00447                       {
00448                         // q is empty; e must come from p.
00449                         __e = __p;
00450                         __p = static_cast<_Node*>(__p->_M_next);
00451                         --__psize;
00452                       }
00453                     else if (__comp(__p->_M_value, __q->_M_value))
00454                       {
00455                         // First node of p is lower; e must come from p.
00456                         __e = __p;
00457                         __p = static_cast<_Node*>(__p->_M_next);
00458                         --__psize;
00459                       }
00460                     else
00461                       {
00462                         // First node of q is lower; e must come from q.
00463                         __e = __q;
00464                         __q = static_cast<_Node*>(__q->_M_next);
00465                         --__qsize;
00466                       }
00467 
00468                     // Add the next node to the merged list.
00469                     if (__tail)
00470                       __tail->_M_next = __e;
00471                     else
00472                       __list = __e;
00473                     __tail = __e;
00474                   }
00475 
00476                 // Now p has stepped `insize' places along, and q has too.
00477                 __p = __q;
00478               }
00479             __tail->_M_next = 0;
00480 
00481             // If we have done only one merge, we're finished.
00482             // Allow for nmerges == 0, the empty list case.
00483             if (__nmerges <= 1)
00484               {
00485                 this->_M_impl._M_head._M_next = __list;
00486                 return;
00487               }
00488 
00489             // Otherwise repeat, merging lists twice the size.
00490             __insize *= 2;
00491           }
00492       }
00493  
00494 _GLIBCXX_END_NESTED_NAMESPACE // namespace std
00495 
00496 #endif /* _FORWARD_LIST_TCC */
00497