[Patch] java.util.Collections

Michael Koch konqueror@gmx.de
Fri Sep 24 08:47:00 GMT 2004


Hi list,


I just merged the attached patch from GNU classpath to fix
java.util.Collections.


Michael


2004-09-23  Mark Wielaard  <mark@klomp.org>

	* java/util/Collections.java
	(binarySearch(List, Object, Comparator)): Explicitly
	reverse direction in list iterator.
	(rotate): Just return when list is empty.

-------------- next part --------------
Index: java/util/Collections.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/util/Collections.java,v
retrieving revision 1.10
diff -u -r1.10 Collections.java
--- java/util/Collections.java	20 Apr 2004 14:45:06 -0000	1.10
+++ java/util/Collections.java	23 Sep 2004 17:59:11 -0000
@@ -574,14 +574,26 @@
       {
 	ListIterator itr = l.listIterator();
         int i = 0;
+	Object o = itr.next(); // Assumes list is not empty (see isSequential)
+	boolean forward = true;
         while (low <= hi)
           {
             pos = (low + hi) >> 1;
             if (i < pos)
-              for ( ; i != pos; i++, itr.next());
+	      {
+		if (!forward)
+		  itr.next(); // Changing direction first.
+		for ( ; i != pos; i++, o = itr.next());
+		forward = true;
+	      }
             else
-              for ( ; i != pos; i--, itr.previous());
-	    final int d = compare(key, itr.next(), c);
+	      {
+		if (forward)
+		  itr.previous(); // Changing direction first.
+		for ( ; i != pos; i--, o = itr.previous());
+		forward = false;
+	      }
+	    final int d = compare(key, o, c);
 	    if (d == 0)
               return pos;
 	    else if (d < 0)
@@ -1110,6 +1122,8 @@
   public static void rotate(List list, int distance)
   {
     int size = list.size();
+    if (size == 0)
+      return;
     distance %= size;
     if (distance == 0)
       return;


More information about the Java-patches mailing list