Hi,

Andrew John Hughes wrote:
This adds the new range-based binarySearch methods.

Unfortunately this breaks the binarySearch for empty arrays. I've just committed some mauve tests for this case.

I've attached a patch that fixes it; let me know if you'd like me to commit, or if you'll approach this another way.

Cheers,
Francis

### Eclipse Workspace Patch 1.0
#P classpath
Index: java/util/Arrays.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Arrays.java,v
retrieving revision 1.33
diff -u -r1.33 Arrays.java
--- java/util/Arrays.java	28 Dec 2006 23:30:53 -0000	1.33
+++ java/util/Arrays.java	3 Jan 2007 20:49:08 -0000
@@ -92,7 +92,10 @@
    */
   public static int binarySearch(byte[] a, byte key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -154,7 +157,10 @@
    */
   public static int binarySearch(char[] a, char key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -216,7 +222,10 @@
    */
   public static int binarySearch(short[] a, short key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -278,7 +287,10 @@
    */
   public static int binarySearch(int[] a, int key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -340,7 +352,10 @@
    */
   public static int binarySearch(long[] a, long key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -402,7 +417,10 @@
    */
   public static int binarySearch(float[] a, float key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -465,7 +483,10 @@
    */
   public static int binarySearch(double[] a, double key)
   {
-    return binarySearch(a, 0, a.length - 1, key);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key);
   }
 
   /**
@@ -581,7 +602,10 @@
    */
   public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
   {
-    return binarySearch(a, 0, a.length - 1, key, c);
+    if (a.length == 0)
+      return -1;
+    else
+      return binarySearch(a, 0, a.length - 1, key, c);
   }
 
   /**

Reply via email to