This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Getting improved iter_swap into 4.0?



Any particolar reason for not using the same pattern /consistently/ used in stl_algobase.h:

None at all, and yes that does seem much clearer. Therefore I have used it in this latest patch..


Chris
--- stl_algobase.h.backup	2004-09-10 20:45:29.000000000 +0100
+++ stl_algobase.h	2004-09-13 18:23:44.000000000 +0100
@@ -77,6 +77,53 @@
 
 namespace std
 {
+
+  /**
+   *  @brief Swaps two values.
+   *  @param  a  A thing of arbitrary type.
+   *  @param  b  Another thing of arbitrary type.
+   *  @return   Nothing.
+   *
+   *  This is the simple classic generic implementation.  It will work on
+   *  any type which has a copy constructor and an assignment operator.
+  */
+  template<typename _Tp>
+    inline void
+    swap(_Tp& __a, _Tp& __b)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
+
+      const _Tp __tmp = __a;
+      __a = __b;
+      __b = __tmp;
+    }
+
+  template<int __iters_have_same_value_type>
+    struct __iter_swap
+    {
+      template<typename _ForwardIterator1, typename _ForwardIterator2>
+        static void
+        iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
+        {
+          typedef typename iterator_traits<_ForwardIterator1>::value_type
+            _ValueType1;
+          const _ValueType1 __tmp = *__a;
+          *__a = *__b;
+          *__b = __tmp; 
+      }
+    };
+
+  template<>
+    struct __iter_swap<1>
+    {
+      template<typename _ForwardIterator1, typename _ForwardIterator2>
+        static void 
+        iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
+        {
+          swap(*__a, *__b);
+        }
+    };
   /**
    *  @brief Swaps the contents of two iterators.
    *  @param  a  An iterator.
@@ -104,31 +151,8 @@ namespace std
 				  _ValueType2>)
       __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
 				  _ValueType1>)
-
-      const _ValueType1 __tmp = *__a;
-      *__a = *__b;
-      *__b = __tmp;
-    }
-
-  /**
-   *  @brief Swaps two values.
-   *  @param  a  A thing of arbitrary type.
-   *  @param  b  Another thing of arbitrary type.
-   *  @return   Nothing.
-   *
-   *  This is the simple classic generic implementation.  It will work on
-   *  any type which has a copy constructor and an assignment operator.
-  */
-  template<typename _Tp>
-    inline void
-    swap(_Tp& __a, _Tp& __b)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
-
-      const _Tp __tmp = __a;
-      __a = __b;
-      __b = __tmp;
+      __iter_swap<__are_same<_ValueType1, _ValueType2>::_M_type>
+        ::iter_swap(__a,__b);
     }
 
   #undef min

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]