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
This commit is contained in:
mdb
2006-06-05 00:13:00 +00:00
parent 1412f7c5e7
commit 6fc0cf2db0
3 changed files with 8 additions and 8 deletions
@@ -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) {
+2 -2
View File
@@ -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) {
+5 -5
View File
@@ -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;