1 // List implementation (out of line) -*- C++ -*-
 
    3 // Copyright (C) 2001-2014 Free Software Foundation, Inc.
 
    5 // This file is part of the GNU ISO C++ Library.  This library is free
 
    6 // software; you can redistribute it and/or modify it under the
 
    7 // terms of the GNU General Public License as published by the
 
    8 // Free Software Foundation; either version 3, or (at your option)
 
   11 // This library is distributed in the hope that it will be useful,
 
   12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   14 // GNU General Public License for more details.
 
   16 // Under Section 7 of GPL version 3, you are granted additional
 
   17 // permissions described in the GCC Runtime Library Exception, version
 
   18 // 3.1, as published by the Free Software Foundation.
 
   20 // You should have received a copy of the GNU General Public License and
 
   21 // a copy of the GCC Runtime Library Exception along with this program;
 
   22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
   23 // <http://www.gnu.org/licenses/>.
 
   28  * Hewlett-Packard Company
 
   30  * Permission to use, copy, modify, distribute and sell this software
 
   31  * and its documentation for any purpose is hereby granted without fee,
 
   32  * provided that the above copyright notice appear in all copies and
 
   33  * that both that copyright notice and this permission notice appear
 
   34  * in supporting documentation.  Hewlett-Packard Company makes no
 
   35  * representations about the suitability of this software for any
 
   36  * purpose.  It is provided "as is" without express or implied warranty.
 
   39  * Copyright (c) 1996,1997
 
   40  * Silicon Graphics Computer Systems, Inc.
 
   42  * Permission to use, copy, modify, distribute and sell this software
 
   43  * and its documentation for any purpose is hereby granted without fee,
 
   44  * provided that the above copyright notice appear in all copies and
 
   45  * that both that copyright notice and this permission notice appear
 
   46  * in supporting documentation.  Silicon Graphics makes no
 
   47  * representations about the suitability of this software for any
 
   48  * purpose.  It is provided "as is" without express or implied warranty.
 
   51 /** @file bits/list.tcc
 
   52  *  This is an internal header file, included by other library headers.
 
   53  *  Do not attempt to use it directly. @headername{list}
 
   59 namespace std _GLIBCXX_VISIBILITY(default)
 
   61 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
   63   template<typename _Tp, typename _Alloc>
 
   65     _List_base<_Tp, _Alloc>::
 
   66     _M_clear() _GLIBCXX_NOEXCEPT
 
   68       typedef _List_node<_Tp>  _Node;
 
   69       _Node* __cur = static_cast<_Node*>(_M_impl._M_node._M_next);
 
   70       while (__cur != &_M_impl._M_node)
 
   73      __cur = static_cast<_Node*>(__cur->_M_next);
 
   74 #if __cplusplus >= 201103L
 
   75      _M_get_Node_allocator().destroy(__tmp);
 
   77      _M_get_Tp_allocator().destroy(std::__addressof(__tmp->_M_data));
 
   83 #if __cplusplus >= 201103L
 
   84   template<typename _Tp, typename _Alloc>
 
   85     template<typename... _Args>
 
   86       typename list<_Tp, _Alloc>::iterator
 
   88       emplace(const_iterator __position, _Args&&... __args)
 
   90    _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
 
   91    __tmp->_M_hook(__position._M_const_cast()._M_node);
 
   92    return iterator(__tmp);
 
   96   template<typename _Tp, typename _Alloc>
 
   97     typename list<_Tp, _Alloc>::iterator
 
   99 #if __cplusplus >= 201103L
 
  100     insert(const_iterator __position, const value_type& __x)
 
  102     insert(iterator __position, const value_type& __x)
 
  105       _Node* __tmp = _M_create_node(__x);
 
  106       __tmp->_M_hook(__position._M_const_cast()._M_node);
 
  107       return iterator(__tmp);
 
  110 #if __cplusplus >= 201103L
 
  111   template<typename _Tp, typename _Alloc>
 
  112     typename list<_Tp, _Alloc>::iterator
 
  114     insert(const_iterator __position, size_type __n, const value_type& __x)
 
  118      list __tmp(__n, __x, get_allocator());
 
  119      iterator __it = __tmp.begin();
 
  120      splice(__position, __tmp);
 
  123       return __position._M_const_cast();
 
  126   template<typename _Tp, typename _Alloc>
 
  127     template<typename _InputIterator, typename>
 
  128       typename list<_Tp, _Alloc>::iterator
 
  130       insert(const_iterator __position, _InputIterator __first,
 
  131         _InputIterator __last)
 
  133    list __tmp(__first, __last, get_allocator());
 
  136        iterator __it = __tmp.begin();
 
  137        splice(__position, __tmp);
 
  140    return __position._M_const_cast();
 
  144   template<typename _Tp, typename _Alloc>
 
  145     typename list<_Tp, _Alloc>::iterator
 
  147 #if __cplusplus >= 201103L
 
  148     erase(const_iterator __position) noexcept
 
  150     erase(iterator __position)
 
  153       iterator __ret = iterator(__position._M_node->_M_next);
 
  154       _M_erase(__position._M_const_cast());
 
  158 #if __cplusplus >= 201103L
 
  159   template<typename _Tp, typename _Alloc>
 
  162     _M_default_append(size_type __n)
 
  167      for (; __i < __n; ++__i)
 
  174      __throw_exception_again;
 
  178   template<typename _Tp, typename _Alloc>
 
  181     resize(size_type __new_size)
 
  183       iterator __i = begin();
 
  185       for (; __i != end() && __len < __new_size; ++__i, ++__len)
 
  187       if (__len == __new_size)
 
  190    _M_default_append(__new_size - __len);
 
  193   template<typename _Tp, typename _Alloc>
 
  196     resize(size_type __new_size, const value_type& __x)
 
  198       iterator __i = begin();
 
  200       for (; __i != end() && __len < __new_size; ++__i, ++__len)
 
  202       if (__len == __new_size)
 
  205         insert(end(), __new_size - __len, __x);
 
  208   template<typename _Tp, typename _Alloc>
 
  211     resize(size_type __new_size, value_type __x)
 
  213       iterator __i = begin();
 
  215       for (; __i != end() && __len < __new_size; ++__i, ++__len)
 
  217       if (__len == __new_size)
 
  220         insert(end(), __new_size - __len, __x);
 
  224   template<typename _Tp, typename _Alloc>
 
  227     operator=(const list& __x)
 
  231      iterator __first1 = begin();
 
  232      iterator __last1 = end();
 
  233      const_iterator __first2 = __x.begin();
 
  234      const_iterator __last2 = __x.end();
 
  235      for (; __first1 != __last1 && __first2 != __last2;
 
  236           ++__first1, ++__first2)
 
  237        *__first1 = *__first2;
 
  238      if (__first2 == __last2)
 
  239        erase(__first1, __last1);
 
  241        insert(__last1, __first2, __last2);
 
  246   template<typename _Tp, typename _Alloc>
 
  249     _M_fill_assign(size_type __n, const value_type& __val)
 
  251       iterator __i = begin();
 
  252       for (; __i != end() && __n > 0; ++__i, --__n)
 
  255         insert(end(), __n, __val);
 
  260   template<typename _Tp, typename _Alloc>
 
  261     template <typename _InputIterator>
 
  264       _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
 
  267         iterator __first1 = begin();
 
  268         iterator __last1 = end();
 
  269         for (; __first1 != __last1 && __first2 != __last2;
 
  270         ++__first1, ++__first2)
 
  271           *__first1 = *__first2;
 
  272         if (__first2 == __last2)
 
  273           erase(__first1, __last1);
 
  275           insert(__last1, __first2, __last2);
 
  278   template<typename _Tp, typename _Alloc>
 
  281     remove(const value_type& __value)
 
  283       iterator __first = begin();
 
  284       iterator __last = end();
 
  285       iterator __extra = __last;
 
  286       while (__first != __last)
 
  288      iterator __next = __first;
 
  290      if (*__first == __value)
 
  292          // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  293          // 526. Is it undefined if a function in the standard changes
 
  295          if (std::__addressof(*__first) != std::__addressof(__value))
 
  302       if (__extra != __last)
 
  306   template<typename _Tp, typename _Alloc>
 
  311       iterator __first = begin();
 
  312       iterator __last = end();
 
  313       if (__first == __last)
 
  315       iterator __next = __first;
 
  316       while (++__next != __last)
 
  318      if (*__first == *__next)
 
  326   template<typename _Tp, typename _Alloc>
 
  329 #if __cplusplus >= 201103L
 
  335       // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  336       // 300. list::merge() specification incomplete
 
  339      _M_check_equal_allocators(__x); 
 
  341      iterator __first1 = begin();
 
  342      iterator __last1 = end();
 
  343      iterator __first2 = __x.begin();
 
  344      iterator __last2 = __x.end();
 
  345      while (__first1 != __last1 && __first2 != __last2)
 
  346        if (*__first2 < *__first1)
 
  348        iterator __next = __first2;
 
  349        _M_transfer(__first1, __first2, ++__next);
 
  354      if (__first2 != __last2)
 
  355        _M_transfer(__last1, __first2, __last2);
 
  359   template<typename _Tp, typename _Alloc>
 
  360     template <typename _StrictWeakOrdering>
 
  363 #if __cplusplus >= 201103L
 
  364       merge(list&& __x, _StrictWeakOrdering __comp)
 
  366       merge(list& __x, _StrictWeakOrdering __comp)
 
  369    // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  370    // 300. list::merge() specification incomplete
 
  373        _M_check_equal_allocators(__x);
 
  375        iterator __first1 = begin();
 
  376        iterator __last1 = end();
 
  377        iterator __first2 = __x.begin();
 
  378        iterator __last2 = __x.end();
 
  379        while (__first1 != __last1 && __first2 != __last2)
 
  380          if (__comp(*__first2, *__first1))
 
  382          iterator __next = __first2;
 
  383          _M_transfer(__first1, __first2, ++__next);
 
  388        if (__first2 != __last2)
 
  389          _M_transfer(__last1, __first2, __last2);
 
  393   template<typename _Tp, typename _Alloc>
 
  398       // Do nothing if the list has length 0 or 1.
 
  399       if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
 
  400      && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
 
  404         list * __fill = &__tmp[0];
 
  409        __carry.splice(__carry.begin(), *this, begin());
 
  411        for(__counter = &__tmp[0];
 
  412        __counter != __fill && !__counter->empty();
 
  415        __counter->merge(__carry);
 
  416        __carry.swap(*__counter);
 
  418        __carry.swap(*__counter);
 
  419        if (__counter == __fill)
 
  424         for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
 
  425           __counter->merge(*(__counter - 1));
 
  426         swap( *(__fill - 1) );
 
  430   template<typename _Tp, typename _Alloc>
 
  431     template <typename _Predicate>
 
  434       remove_if(_Predicate __pred)
 
  436         iterator __first = begin();
 
  437         iterator __last = end();
 
  438         while (__first != __last)
 
  440        iterator __next = __first;
 
  442        if (__pred(*__first))
 
  448   template<typename _Tp, typename _Alloc>
 
  449     template <typename _BinaryPredicate>
 
  452       unique(_BinaryPredicate __binary_pred)
 
  454         iterator __first = begin();
 
  455         iterator __last = end();
 
  456         if (__first == __last)
 
  458         iterator __next = __first;
 
  459         while (++__next != __last)
 
  461        if (__binary_pred(*__first, *__next))
 
  469   template<typename _Tp, typename _Alloc>
 
  470     template <typename _StrictWeakOrdering>
 
  473       sort(_StrictWeakOrdering __comp)
 
  475    // Do nothing if the list has length 0 or 1.
 
  476    if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
 
  477        && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
 
  481        list * __fill = &__tmp[0];
 
  486        __carry.splice(__carry.begin(), *this, begin());
 
  488        for(__counter = &__tmp[0];
 
  489            __counter != __fill && !__counter->empty();
 
  492            __counter->merge(__carry, __comp);
 
  493            __carry.swap(*__counter);
 
  495        __carry.swap(*__counter);
 
  496        if (__counter == __fill)
 
  501        for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
 
  502          __counter->merge(*(__counter - 1), __comp);
 
  507 _GLIBCXX_END_NAMESPACE_CONTAINER
 
  510 #endif /* _LIST_TCC */