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]

[Patch] Fix to random_access overload of std::reverse


This is a (hopefully) simple patch the overload of std::reverse in the random-access case.

Firstly the comment on the function is wrong because it refers to bidirectional iterators.

Secondly, the existing version has two main problems. It needlessly uses a post-increment, and also when the given vector is of odd length it swaps the middle element with itself, which I suspect violates the standard on this issue. This patch fixes both these problems.


2004-09-09  Christopher Jefferson <caj@cs.york.ac.uk>

	* include/bits/stl_algo.h __reverse(,,random_access_iterator):
	Fix comment, stop swapping center element of odd length
	inputs with itself.
--- stl_algo.h	2004-09-09 17:54:01.000000000 +0100
+++ stl_algo.h.backup	2004-09-09 17:43:15.000000000 +0100
@@ -1325,7 +1325,7 @@ __result, __binary_pred, _IterType());
   /**
    *  @if maint
    *  This is an uglified reverse(_BidirectionalIter, _BidirectionalIter)
-   *  overloaded for random access iterators.
+   *  overloaded for bidirectional iterators.
    *  @endif
   */
   template<typename _RandomAccessIter>
@@ -1333,13 +1333,8 @@ __result, __binary_pred, _IterType());
     __reverse(_RandomAccessIter __first, _RandomAccessIter __last,
 			  random_access_iterator_tag)
     {
-      if(__first==__last) return;
-      --__last;
-      while(__first<__last) {
-        iter_swap(__first,__last);
-        ++__first;
-        --__last;
-      }
+	  while (__first < __last)
+	    iter_swap(__first++, --__last);
     }
 
   /**

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