list.tcc

Go to the documentation of this file.
00001 // List implementation (out of line) -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  *
00028  * Copyright (c) 1994
00029  * Hewlett-Packard Company
00030  *
00031  * Permission to use, copy, modify, distribute and sell this software
00032  * and its documentation for any purpose is hereby granted without fee,
00033  * provided that the above copyright notice appear in all copies and
00034  * that both that copyright notice and this permission notice appear
00035  * in supporting documentation.  Hewlett-Packard Company makes no
00036  * representations about the suitability of this software for any
00037  * purpose.  It is provided "as is" without express or implied warranty.
00038  *
00039  *
00040  * Copyright (c) 1996,1997
00041  * Silicon Graphics Computer Systems, Inc.
00042  *
00043  * Permission to use, copy, modify, distribute and sell this software
00044  * and its documentation for any purpose is hereby granted without fee,
00045  * provided that the above copyright notice appear in all copies and
00046  * that both that copyright notice and this permission notice appear
00047  * in supporting documentation.  Silicon Graphics makes no
00048  * representations about the suitability of this software for any
00049  * purpose.  It is provided "as is" without express or implied warranty.
00050  */
00051 
00052 /** @file list.tcc
00053  *  This is an internal header file, included by other library headers.
00054  *  You should not attempt to use it directly.
00055  */
00056 
00057 #ifndef _LIST_TCC
00058 #define _LIST_TCC 1
00059 
00060 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
00061 
00062   template<typename _Tp, typename _Alloc>
00063     void
00064     _List_base<_Tp, _Alloc>::
00065     _M_clear()
00066     {
00067       typedef _List_node<_Tp>  _Node;
00068       _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
00069       while (__cur != &this->_M_impl._M_node)
00070     {
00071       _Node* __tmp = __cur;
00072       __cur = static_cast<_Node*>(__cur->_M_next);
00073 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00074       _M_get_Node_allocator().destroy(__tmp);
00075 #else
00076       _M_get_Tp_allocator().destroy(std::__addressof(__tmp->_M_data));
00077 #endif
00078       _M_put_node(__tmp);
00079     }
00080     }
00081 
00082 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00083   template<typename _Tp, typename _Alloc>
00084     template<typename... _Args>
00085       typename list<_Tp, _Alloc>::iterator
00086       list<_Tp, _Alloc>::
00087       emplace(iterator __position, _Args&&... __args)
00088       {
00089     _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
00090     __tmp->_M_hook(__position._M_node);
00091     return iterator(__tmp);
00092       }
00093 #endif
00094 
00095   template<typename _Tp, typename _Alloc>
00096     typename list<_Tp, _Alloc>::iterator
00097     list<_Tp, _Alloc>::
00098     insert(iterator __position, const value_type& __x)
00099     {
00100       _Node* __tmp = _M_create_node(__x);
00101       __tmp->_M_hook(__position._M_node);
00102       return iterator(__tmp);
00103     }
00104 
00105   template<typename _Tp, typename _Alloc>
00106     typename list<_Tp, _Alloc>::iterator
00107     list<_Tp, _Alloc>::
00108     erase(iterator __position)
00109     {
00110       iterator __ret = iterator(__position._M_node->_M_next);
00111       _M_erase(__position);
00112       return __ret;
00113     }
00114 
00115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00116   template<typename _Tp, typename _Alloc>
00117     void
00118     list<_Tp, _Alloc>::
00119     _M_default_append(size_type __n)
00120     {
00121       size_type __i = 0;
00122       __try
00123     {
00124       for (; __i < __n; ++__i)
00125         emplace_back();
00126     }
00127       __catch(...)
00128     {
00129       for (; __i; --__i)
00130         pop_back();
00131       __throw_exception_again;
00132     }
00133     }
00134 
00135   template<typename _Tp, typename _Alloc>
00136     void
00137     list<_Tp, _Alloc>::
00138     resize(size_type __new_size)
00139     {
00140       iterator __i = begin();
00141       size_type __len = 0;
00142       for (; __i != end() && __len < __new_size; ++__i, ++__len)
00143         ;
00144       if (__len == __new_size)
00145         erase(__i, end());
00146       else                          // __i == end()
00147     _M_default_append(__new_size - __len);
00148     }
00149 
00150   template<typename _Tp, typename _Alloc>
00151     void
00152     list<_Tp, _Alloc>::
00153     resize(size_type __new_size, const value_type& __x)
00154     {
00155       iterator __i = begin();
00156       size_type __len = 0;
00157       for (; __i != end() && __len < __new_size; ++__i, ++__len)
00158         ;
00159       if (__len == __new_size)
00160         erase(__i, end());
00161       else                          // __i == end()
00162         insert(end(), __new_size - __len, __x);
00163     }
00164 #else
00165   template<typename _Tp, typename _Alloc>
00166     void
00167     list<_Tp, _Alloc>::
00168     resize(size_type __new_size, value_type __x)
00169     {
00170       iterator __i = begin();
00171       size_type __len = 0;
00172       for (; __i != end() && __len < __new_size; ++__i, ++__len)
00173         ;
00174       if (__len == __new_size)
00175         erase(__i, end());
00176       else                          // __i == end()
00177         insert(end(), __new_size - __len, __x);
00178     }
00179 #endif
00180 
00181   template<typename _Tp, typename _Alloc>
00182     list<_Tp, _Alloc>&
00183     list<_Tp, _Alloc>::
00184     operator=(const list& __x)
00185     {
00186       if (this != &__x)
00187     {
00188       iterator __first1 = begin();
00189       iterator __last1 = end();
00190       const_iterator __first2 = __x.begin();
00191       const_iterator __last2 = __x.end();
00192       for (; __first1 != __last1 && __first2 != __last2;
00193            ++__first1, ++__first2)
00194         *__first1 = *__first2;
00195       if (__first2 == __last2)
00196         erase(__first1, __last1);
00197       else
00198         insert(__last1, __first2, __last2);
00199     }
00200       return *this;
00201     }
00202 
00203   template<typename _Tp, typename _Alloc>
00204     void
00205     list<_Tp, _Alloc>::
00206     _M_fill_assign(size_type __n, const value_type& __val)
00207     {
00208       iterator __i = begin();
00209       for (; __i != end() && __n > 0; ++__i, --__n)
00210         *__i = __val;
00211       if (__n > 0)
00212         insert(end(), __n, __val);
00213       else
00214         erase(__i, end());
00215     }
00216 
00217   template<typename _Tp, typename _Alloc>
00218     template <typename _InputIterator>
00219       void
00220       list<_Tp, _Alloc>::
00221       _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
00222              __false_type)
00223       {
00224         iterator __first1 = begin();
00225         iterator __last1 = end();
00226         for (; __first1 != __last1 && __first2 != __last2;
00227          ++__first1, ++__first2)
00228           *__first1 = *__first2;
00229         if (__first2 == __last2)
00230           erase(__first1, __last1);
00231         else
00232           insert(__last1, __first2, __last2);
00233       }
00234 
00235   template<typename _Tp, typename _Alloc>
00236     void
00237     list<_Tp, _Alloc>::
00238     remove(const value_type& __value)
00239     {
00240       iterator __first = begin();
00241       iterator __last = end();
00242       iterator __extra = __last;
00243       while (__first != __last)
00244     {
00245       iterator __next = __first;
00246       ++__next;
00247       if (*__first == __value)
00248         {
00249           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00250           // 526. Is it undefined if a function in the standard changes
00251           // in parameters?
00252           if (std::__addressof(*__first) != std::__addressof(__value))
00253         _M_erase(__first);
00254           else
00255         __extra = __first;
00256         }
00257       __first = __next;
00258     }
00259       if (__extra != __last)
00260     _M_erase(__extra);
00261     }
00262 
00263   template<typename _Tp, typename _Alloc>
00264     void
00265     list<_Tp, _Alloc>::
00266     unique()
00267     {
00268       iterator __first = begin();
00269       iterator __last = end();
00270       if (__first == __last)
00271     return;
00272       iterator __next = __first;
00273       while (++__next != __last)
00274     {
00275       if (*__first == *__next)
00276         _M_erase(__next);
00277       else
00278         __first = __next;
00279       __next = __first;
00280     }
00281     }
00282 
00283   template<typename _Tp, typename _Alloc>
00284     void
00285     list<_Tp, _Alloc>::
00286 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00287     merge(list&& __x)
00288 #else
00289     merge(list& __x)
00290 #endif
00291     {
00292       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00293       // 300. list::merge() specification incomplete
00294       if (this != &__x)
00295     {
00296       _M_check_equal_allocators(__x); 
00297 
00298       iterator __first1 = begin();
00299       iterator __last1 = end();
00300       iterator __first2 = __x.begin();
00301       iterator __last2 = __x.end();
00302       while (__first1 != __last1 && __first2 != __last2)
00303         if (*__first2 < *__first1)
00304           {
00305         iterator __next = __first2;
00306         _M_transfer(__first1, __first2, ++__next);
00307         __first2 = __next;
00308           }
00309         else
00310           ++__first1;
00311       if (__first2 != __last2)
00312         _M_transfer(__last1, __first2, __last2);
00313     }
00314     }
00315 
00316   template<typename _Tp, typename _Alloc>
00317     template <typename _StrictWeakOrdering>
00318       void
00319       list<_Tp, _Alloc>::
00320 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00321       merge(list&& __x, _StrictWeakOrdering __comp)
00322 #else
00323       merge(list& __x, _StrictWeakOrdering __comp)
00324 #endif
00325       {
00326     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00327     // 300. list::merge() specification incomplete
00328     if (this != &__x)
00329       {
00330         _M_check_equal_allocators(__x);
00331 
00332         iterator __first1 = begin();
00333         iterator __last1 = end();
00334         iterator __first2 = __x.begin();
00335         iterator __last2 = __x.end();
00336         while (__first1 != __last1 && __first2 != __last2)
00337           if (__comp(*__first2, *__first1))
00338         {
00339           iterator __next = __first2;
00340           _M_transfer(__first1, __first2, ++__next);
00341           __first2 = __next;
00342         }
00343           else
00344         ++__first1;
00345         if (__first2 != __last2)
00346           _M_transfer(__last1, __first2, __last2);
00347       }
00348       }
00349 
00350   template<typename _Tp, typename _Alloc>
00351     void
00352     list<_Tp, _Alloc>::
00353     sort()
00354     {
00355       // Do nothing if the list has length 0 or 1.
00356       if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
00357       && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
00358       {
00359         list __carry;
00360         list __tmp[64];
00361         list * __fill = &__tmp[0];
00362         list * __counter;
00363 
00364         do
00365       {
00366         __carry.splice(__carry.begin(), *this, begin());
00367 
00368         for(__counter = &__tmp[0];
00369         __counter != __fill && !__counter->empty();
00370         ++__counter)
00371           {
00372         __counter->merge(__carry);
00373         __carry.swap(*__counter);
00374           }
00375         __carry.swap(*__counter);
00376         if (__counter == __fill)
00377           ++__fill;
00378       }
00379     while ( !empty() );
00380 
00381         for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
00382           __counter->merge(*(__counter - 1));
00383         swap( *(__fill - 1) );
00384       }
00385     }
00386 
00387   template<typename _Tp, typename _Alloc>
00388     template <typename _Predicate>
00389       void
00390       list<_Tp, _Alloc>::
00391       remove_if(_Predicate __pred)
00392       {
00393         iterator __first = begin();
00394         iterator __last = end();
00395         while (__first != __last)
00396       {
00397         iterator __next = __first;
00398         ++__next;
00399         if (__pred(*__first))
00400           _M_erase(__first);
00401         __first = __next;
00402       }
00403       }
00404 
00405   template<typename _Tp, typename _Alloc>
00406     template <typename _BinaryPredicate>
00407       void
00408       list<_Tp, _Alloc>::
00409       unique(_BinaryPredicate __binary_pred)
00410       {
00411         iterator __first = begin();
00412         iterator __last = end();
00413         if (__first == __last)
00414       return;
00415         iterator __next = __first;
00416         while (++__next != __last)
00417       {
00418         if (__binary_pred(*__first, *__next))
00419           _M_erase(__next);
00420         else
00421           __first = __next;
00422         __next = __first;
00423       }
00424       }
00425 
00426   template<typename _Tp, typename _Alloc>
00427     template <typename _StrictWeakOrdering>
00428       void
00429       list<_Tp, _Alloc>::
00430       sort(_StrictWeakOrdering __comp)
00431       {
00432     // Do nothing if the list has length 0 or 1.
00433     if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
00434         && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
00435       {
00436         list __carry;
00437         list __tmp[64];
00438         list * __fill = &__tmp[0];
00439         list * __counter;
00440 
00441         do
00442           {
00443         __carry.splice(__carry.begin(), *this, begin());
00444 
00445         for(__counter = &__tmp[0];
00446             __counter != __fill && !__counter->empty();
00447             ++__counter)
00448           {
00449             __counter->merge(__carry, __comp);
00450             __carry.swap(*__counter);
00451           }
00452         __carry.swap(*__counter);
00453         if (__counter == __fill)
00454           ++__fill;
00455           }
00456         while ( !empty() );
00457 
00458         for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
00459           __counter->merge(*(__counter - 1), __comp);
00460         swap(*(__fill - 1));
00461       }
00462       }
00463 
00464 _GLIBCXX_END_NESTED_NAMESPACE
00465 
00466 #endif /* _LIST_TCC */
00467