package umontreal.iro.lecuyer.util;
import java.util.Comparator;
/**
* An implementation of {@link java.util.Comparator} which compares two
* double arrays by comparing their i-th element,
* where i is given in the constructor.
* Method compare(d1, d2) returns -1, 0, or 1 depending on
* whether d1[i] is less than, equal to, or greater than
* d2[i].
*
*/
public class DoubleArrayComparator implements Comparator {
private int i;
/**
* Constructs a comparator, where i is the index
* used for the comparisons.
*
* @param i index used for comparison
*
*/
public DoubleArrayComparator (int i) {
this.i = i;
}
/**
* Returns -1, 0, or 1 depending on
* whether d1[i] is less than, equal
* to, or greater than d2[i].
*
* @param d1 first array
*
* @param d2 second array
*
*
*/
public int compare (double[] d1, double[] d2) {
if (i >= d1.length || i >= d2.length)
throw new IllegalArgumentException("Comparing in a"+
"dimension larger than arary dimension");
return (d1[i]< d2[i] ? -1 : (d1[i] > d2[i] ? 1 : 0));
}
}
Ads help maintain this website.