From 6fc0cf2db0e9686ef6adc3608b43853fb01c3327 Mon Sep 17 00:00:00 2001 From: mdb Date: Mon, 5 Jun 2006 00:13:00 +0000 Subject: [PATCH] Cope with very large numbers. Consider that: int low = Integer.MAX_VALUE - 2; // = 2147483645 int high = Integer.MAX_VALUE; // = 2147483647 int mid1 = (low + high) / 2; // = -2 int mid2 = low + (high - low) / 2; // = 2147483646 int mid3 = (low + high) >> 1; // = -2 int mid4 = (low + high) >>> 1; // = 2147483646 So we'll use the last one (non-sign-propagating shift) as it is likely to be fastest and clearest. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1850 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/util/ProximityTracker.java | 2 +- src/java/com/samskivert/util/ArrayUtil.java | 4 ++-- src/java/com/samskivert/util/QuickSort.java | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/java/com/samskivert/swing/util/ProximityTracker.java b/src/java/com/samskivert/swing/util/ProximityTracker.java index d4dea4b3..b6b50eb6 100644 --- a/src/java/com/samskivert/swing/util/ProximityTracker.java +++ b/src/java/com/samskivert/swing/util/ProximityTracker.java @@ -240,7 +240,7 @@ public class ProximityTracker int high = _size-1; while (low <= high) { - int mid = (low + high) >> 1; + int mid = (low + high) >>> 1; int cmp = (_records[mid].x - x); if (cmp < 0) { diff --git a/src/java/com/samskivert/util/ArrayUtil.java b/src/java/com/samskivert/util/ArrayUtil.java index d081f6b7..f8d10f20 100644 --- a/src/java/com/samskivert/util/ArrayUtil.java +++ b/src/java/com/samskivert/util/ArrayUtil.java @@ -325,7 +325,7 @@ public class ArrayUtil { int low = offset, high = offset+length-1; while (low <= high) { - int mid = (low + high) >> 1; + int mid = (low + high) >>> 1; T midVal = array[mid]; int cmp = midVal.compareTo(key); if (cmp < 0) { @@ -361,7 +361,7 @@ public class ArrayUtil { int low = offset, high = offset+length-1; while (low <= high) { - int mid = (low + high) >> 1; + int mid = (low + high) >>> 1; T midVal = array[mid]; int cmp = comp.compare(midVal, key); if (cmp < 0) { diff --git a/src/java/com/samskivert/util/QuickSort.java b/src/java/com/samskivert/util/QuickSort.java index 40a1da01..6202a598 100644 --- a/src/java/com/samskivert/util/QuickSort.java +++ b/src/java/com/samskivert/util/QuickSort.java @@ -96,7 +96,7 @@ public class QuickSort } // the middle element in the array is our partitioning element - T mid = a[(lo0 + hi0)/2]; + T mid = a[(lo0 + hi0) >>> 1]; // set up our partitioning boundaries int lo = lo0-1, hi = hi0+1; @@ -164,7 +164,7 @@ public class QuickSort } // the middle element in the array is our partitioning element - T mid = a[(lo0 + hi0)/2]; + T mid = a[(lo0 + hi0) >>> 1]; // set up our partitioning boundaries int lo = lo0-1, hi = hi0+1; @@ -230,7 +230,7 @@ public class QuickSort } // the middle element in the array is our partitioning element - T mid = a[(lo0 + hi0)/2]; + T mid = a[(lo0 + hi0) >>> 1]; // set up our partitioning boundaries int lo = lo0-1, hi = hi0+1; @@ -296,7 +296,7 @@ public class QuickSort } // the middle element in the array is our partitioning element - T mid = a[(lo0 + hi0)/2]; + T mid = a[(lo0 + hi0) >>> 1]; // set up our partitioning boundaries int lo = lo0-1, hi = hi0+1; @@ -421,7 +421,7 @@ public class QuickSort } // the middle element in the array is our partitioning element - T mid = a.get((lo0 + hi0)/2); + T mid = a.get((lo0 + hi0) >>> 1); // set up our partitioning boundaries int lo = lo0-1, hi = hi0+1;