]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
2008-10-16 Edward Smith-Rowland <3dw4rd@verizon.net>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Oct 2008 08:08:03 +0000 (08:08 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Oct 2008 08:08:03 +0000 (08:08 +0000)
* include/bits/forward_list.h: Factor list construction to dispatch
routines.
* include/bits/forward_list.tcc: Likewise.
* testsuite/23_containers/forward_list/modifiers/2.cc:

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@141189 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/forward_list.h
libstdc++-v3/include/bits/forward_list.tcc
libstdc++-v3/testsuite/23_containers/forward_list/modifiers/2.cc

index 4b7151e0c35b590cb4e392cd2c5066543c67e1c0..1364419d2996846ff42bef8238cb47ea1b2f4e7f 100644 (file)
@@ -1,3 +1,10 @@
+2008-10-16  Edward Smith-Rowland  <3dw4rd@verizon.net>
+
+       * include/bits/forward_list.h: Factor list construction to dispatch
+       routines.
+       * include/bits/forward_list.tcc: Likewise.
+       * testsuite/23_containers/forward_list/modifiers/2.cc: 
+
 2008-10-16  Paolo Carlini  <paolo.carlini@oracle.com>
 
        * include/bits/forward_list.tcc (operator==): Use auto.
index 88a6017d08d378e7b4f46f5bf926e1f3914b9087..cce8f3d7cc52ed2349c6a4cf8889fb877897b8d3 100644 (file)
@@ -469,7 +469,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  the default value.
        */
       explicit
-      forward_list(size_type __n);
+      forward_list(size_type __n)
+      : _Base(_Alloc())
+      { _M_fill_initialize(__n, value_type()); }
 
       /**
        *  @brief  Creates a %forward_list with copies of an exemplar element.
@@ -481,7 +483,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  value.
        */
       forward_list(size_type __n, const _Tp& __value,
-                   const _Alloc& __al = _Alloc());
+                   const _Alloc& __al = _Alloc())
+      : _Base(__al)
+      { _M_fill_initialize(__n, __value); }
 
       /**
        *  @brief  Builds a %forward_list from a range.
@@ -495,7 +499,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        */
       template<typename _InputIterator>
         forward_list(_InputIterator __first, _InputIterator __last,
-                    const _Alloc& __al = _Alloc());
+                     const _Alloc& __al = _Alloc())
+        : _Base(__al)
+        {
+          // Check whether it's an integral type.  If so, it's not an iterator.
+          typedef typename std::__is_integer<_InputIterator>::__type _Integral;
+          _M_initialize_dispatch(__first, __last, _Integral());
+        }
 
       /**
        *  @brief  The %forward_list copy constructor.
@@ -505,7 +515,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  The newly-created %forward_list uses a copy of the allocation
        *  object used by @a list.
        */
-      forward_list(const forward_list& __list);
+      forward_list(const forward_list& __list)
+      : _Base(__list.get_allocator())
+      { _M_initialize_dispatch(__list.begin(), __list.end(),
+                               __false_type()); }
 
       /**
        *  @brief  The %forward_list move constructor.
@@ -528,7 +541,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  in the initializer_list @a il.  This is linear in il.size().
        */
       forward_list(std::initializer_list<_Tp> __il,
-                   const _Alloc& __al = _Alloc());
+                   const _Alloc& __al = _Alloc())
+      : _Base(__al)
+      { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
 
       /**
        *  @brief  The forward_list dtor.
@@ -871,7 +886,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  does not invalidate iterators and references.
        */
       void
-      insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
+      insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
+      {
+        forward_list<_Tp, _Alloc> __tmp(__n, __val, this->get_allocator());
+        this->splice_after(__pos, std::move(__tmp));
+      }
 
       /**
        *  @brief  Inserts a range into the %forward_list.
@@ -889,7 +908,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       template<typename _InputIterator>
         void
         insert_after(const_iterator __pos,
-                     _InputIterator __first, _InputIterator __last);
+                     _InputIterator __first, _InputIterator __last)
+        {
+          forward_list<_Tp, _Alloc> __tmp(__first, __last, this->get_allocator());
+          this->splice_after(__pos, std::move(__tmp));
+        }
 
       /**
        *  @brief  Inserts the contents of an initializer_list into
@@ -905,7 +928,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  does not invalidate iterators and references.
        */
       void
-      insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
+      insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
+      {
+        forward_list<_Tp, _Alloc> __tmp(__il, this->get_allocator());
+        this->splice_after(__pos, std::move(__tmp));
+      }
 
       /**
        *  @brief  Removes the element pointed to by the iterator following
@@ -1106,7 +1133,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        *  the pointer is the user's responsibility.
        */
       void
-      unique();
+      unique()
+      { this->unique(std::equal_to<_Tp>()); }
 
       /**
        *  @brief  Remove consecutive elements satisfying a predicate.
@@ -1186,6 +1214,23 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        */
       void
       reverse();
+
+    private:
+      template<typename _Integer>
+       void
+       _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
+       { _M_fill_initialize(static_cast<size_type>(__n), __x); }
+
+      // Called by the range constructor to implement [23.1.1]/9
+      template<typename _InputIterator>
+       void
+       _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
+                              __false_type);
+
+      // Called by forward_list(n,v,a), and the range constructor when it turns out
+      // to be the same thing.
+      void
+      _M_fill_initialize(size_type __n, const value_type& __value);
     };
 
   /**
index 46b97d53f34324274cea8db798922f35580f5121..f222f703aac969b9a4e82e011bc0b20065eb5e49 100644 (file)
@@ -206,38 +206,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       return __pos;
     }
   
-  template<typename _Tp, typename _Alloc>
-    forward_list<_Tp, _Alloc>::
-    forward_list(size_type __n)
-    : _Base()
-    {
-      _Fwd_list_node_base* __to = &this->_M_impl._M_head;
-      for (size_type __i = 0; __i < __n; ++__i)
-       {
-         __to->_M_next = this->_M_create_node(_Tp());
-         __to = __to->_M_next;
-       }
-    }
-
-  template<typename _Tp, typename _Alloc>
-    forward_list<_Tp, _Alloc>::
-    forward_list(size_type __n, const _Tp& __value, const _Alloc& __al)
-    : _Base(__al)
-    {
-      _Fwd_list_node_base* __to = &this->_M_impl._M_head;
-      for (size_type __i = 0; __i < __n; ++__i)
-       {
-         __to->_M_next = this->_M_create_node(__value);
-         __to = __to->_M_next;
-       }
-    }
-
+  // Called by the range constructor to implement [23.1.1]/9
   template<typename _Tp, typename _Alloc>
     template<typename _InputIterator>
+      void
       forward_list<_Tp, _Alloc>::
-      forward_list(_InputIterator __first, _InputIterator __last,
-                   const _Alloc& __al)
-      : _Base(__al)
+      _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
+                             __false_type)
       {
         _Fwd_list_node_base* __to = &this->_M_impl._M_head;
         _InputIterator __curr = __first;
@@ -249,34 +224,19 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
           }
       }
 
+  // Called by forward_list(n,v,a), and the range constructor
+  // when it turns out to be the same thing.
   template<typename _Tp, typename _Alloc>
+    void
     forward_list<_Tp, _Alloc>::
-    forward_list(const forward_list& __list)
-    : _Base(__list._M_get_Node_allocator())
-    {
-      const _Fwd_list_node_base* __from = &__list._M_impl._M_head;
-      _Fwd_list_node_base* __to = &this->_M_impl._M_head;
-      while (__from->_M_next != 0)
-       {
-         const _Node* __temp = static_cast<_Node*>(__from->_M_next);
-         __to->_M_next = this->_M_create_node(__temp->_M_value);
-         __from = __from->_M_next;
-         __to = __to->_M_next;
-       }
-    }
-
-  template<typename _Tp, typename _Alloc>
-    forward_list<_Tp, _Alloc>::
-    forward_list(std::initializer_list<_Tp> __il, const _Alloc& __al)
-    : _Base(__al)
+    _M_fill_initialize(size_type __n, const value_type& __value)
     {
       _Fwd_list_node_base* __to = &this->_M_impl._M_head;
-      for (const _Tp* __item = __il.begin();
-          __item != __il.end(); ++__item)
-       {
-         __to->_M_next = this->_M_create_node(*__item);
-         __to = __to->_M_next;
-       }
+      for (; __n > 0; --__n)
+        {
+          __to->_M_next = this->_M_create_node(__value);
+          __to = __to->_M_next;
+        }
     }
 
   template<typename _Tp, typename _Alloc>
@@ -306,61 +266,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       return *this;
     }
 
-  template<typename _Tp, typename _Alloc>
-    void
-    forward_list<_Tp, _Alloc>::
-    insert_after(const_iterator __pos,
-                size_type __n, const _Tp& __val)
-    {
-      _Fwd_list_node_base* __to
-       = const_cast<_Fwd_list_node_base*>(__pos._M_node);
-      _Fwd_list_node_base* __keep = __to->_M_next;
-      for (size_type __i = 0; __i < __n; ++__i)
-       {
-         __to->_M_next = this->_M_create_node(__val);
-         __to = __to->_M_next;
-       }
-      __to->_M_next = __keep;
-    }
-
-  template<typename _Tp, typename _Alloc>
-    template<typename _InputIterator>
-      void
-      forward_list<_Tp, _Alloc>::
-      insert_after(const_iterator __pos,
-                  _InputIterator __first, _InputIterator __last)
-      {
-       _Fwd_list_node_base* __to
-         = const_cast<_Fwd_list_node_base*>(__pos._M_node);
-       _Fwd_list_node_base* __keep = __to->_M_next;
-       _InputIterator __curr = __first;
-       while (__curr != __last)
-         {
-           __to->_M_next = this->_M_create_node(*__curr);
-           __to = __to->_M_next;
-           ++__curr;
-         }
-       __to->_M_next = __keep;
-      }
-
-  template<typename _Tp, typename _Alloc>
-    void
-    forward_list<_Tp, _Alloc>::
-    insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
-    {
-      _Fwd_list_node_base* __to
-       = const_cast<_Fwd_list_node_base*>(__pos._M_node);
-      _Fwd_list_node_base* __keep = __to->_M_next;
-      const _Tp* __item = __il.begin();
-      while (__item != __il.end())
-       {
-         __to->_M_next = this->_M_create_node(*__item);
-         __to = __to->_M_next;
-         ++__item;
-       }
-      __to->_M_next = __keep;
-    }
-
   template<typename _Tp, typename _Alloc>
     void
     forward_list<_Tp, _Alloc>::
@@ -440,26 +345,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          }
       }
 
-  template<typename _Tp, typename _Alloc>
-    void
-    forward_list<_Tp, _Alloc>::
-    unique()
-    {
-      iterator __first = begin();
-      iterator __last = end();
-      if (__first == __last)
-       return;
-      iterator __next = __first;
-      while (++__next != __last)
-       {
-         if (*__first == *__next)
-           erase_after(__first);
-         else
-           __first = __next;
-         __next = __first;
-       }
-    }
-
   template<typename _Tp, typename _Alloc>
     template<typename _BinPred>
       void
index 9d79b22986b2c68d800b9bcdd65cd508ca043191..a6283580586dd579934ab20123e970aea03bda9c 100644 (file)
@@ -51,7 +51,7 @@ test02()
 
   // Note: Calling l.insert_after(pos, 5, 42); without the long five
   // gets resolved to the iterator range version and fails to compile!
-  fl.insert_after(pos, 5L, 42);
+  fl.insert_after(pos, 5, 42);
   VERIFY(*pos == 1);
 
   ++pos;