Skip to content

Commit

Permalink
Add missing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Shcherb authored and alexey-pelykh committed Feb 1, 2013
1 parent 740c1be commit d51ce1a
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 0 deletions.
30 changes: 30 additions & 0 deletions classpath/java/lang/Double.java
Expand Up @@ -96,6 +96,36 @@ public static double parseDouble(String s) {
}
}

public static int compare(double double1, double double2) {
// Non-zero, non-NaN checking.
if (double1 > double2) {
return 1;
}
if (double2 > double1) {
return -1;
}
if (double1 == double2 && 0.0d != double1) {
return 0;
}

// NaNs are equal to other NaNs and larger than any other double
if (isNaN(double1)) {
if (isNaN(double2)) {
return 0;
}
return 1;
} else if (isNaN(double2)) {
return -1;
}

// Deal with +0.0 and -0.0
long d1 = doubleToRawLongBits(double1);
long d2 = doubleToRawLongBits(double2);
// The below expression is equivalent to:
// (d1 == d2) ? 0 : (d1 < d2) ? -1 : 1
return (int) ((d1 >> 63) - (d2 >> 63));
}

public static native int fillBufferWithDouble(double value, byte[] buffer,
int charCount);

Expand Down
30 changes: 30 additions & 0 deletions classpath/java/util/Arrays.java
Expand Up @@ -191,6 +191,30 @@ public static void fill(int[] array, int value) {
}
}

public static void fill(byte[] array, byte value) {
for (int i = 0; i < array.length; i++) {
array[i] = value;
}
}

public static void fill(byte[] array, int start, int end, byte value) {
for (int i = start; i < end; i++) {
array[i] = value;
}
}

public static void fill(long[] array, long value) {
for (int i = 0; i < array.length; i++) {
array[i] = value;
}
}

public static void fill(long[] array, int start, int end, long value) {
for (int i = start; i < end; i++) {
array[i] = value;
}
}

public static void fill(char[] array, char value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
Expand All @@ -203,6 +227,12 @@ public static <T> void fill(T[] array, T value) {
}
}

public static <T> void fill(T[] array, int start, int end, T value) {
for (int i = start; i < end; i++) {
array[i] = value;
}
}

public static void sort(int[] numbers) {
// Check for empty or null array
if (numbers ==null || numbers.length==0){
Expand Down
49 changes: 49 additions & 0 deletions classpath/java/util/Collections.java
Expand Up @@ -567,4 +567,53 @@ public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.set(j, (T)a[j]);
}
}

public static <T>
int binarySearch(List<? extends Comparable<? super T>> list, T key) {
return Collections.indexedBinarySearch(list, key);
}

private static <T>
int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key)
{
int low = 0;
int high = list.size()-1;

while (low <= high) {
int mid = (low + high) >>> 1;
Comparable<? super T> midVal = list.get(mid);
int cmp = midVal.compareTo(key);

if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}

public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
return Collections.indexedBinarySearch(list, key, c);
}

private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
int low = 0;
int high = l.size()-1;

while (low <= high) {
int mid = (low + high) >>> 1;
T midVal = l.get(mid);
int cmp = c.compare(midVal, key);

if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}
}

0 comments on commit d51ce1a

Please sign in to comment.