MATH-990
Reduced amount of copying. Moved consistency checks out of the double loop. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1489871 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d8aca35b7a
commit
b23c8567c3
|
@ -675,59 +675,69 @@ public class MathArrays {
|
||||||
public static void sortInPlace(double[] x,
|
public static void sortInPlace(double[] x,
|
||||||
final OrderDirection dir,
|
final OrderDirection dir,
|
||||||
double[] ... yList)
|
double[] ... yList)
|
||||||
throws NullArgumentException, DimensionMismatchException {
|
throws NullArgumentException,
|
||||||
|
DimensionMismatchException {
|
||||||
|
final int yListLen = yList.length;
|
||||||
|
final int len = x.length;
|
||||||
|
|
||||||
|
// Consistency checks.
|
||||||
if (x == null) {
|
if (x == null) {
|
||||||
throw new NullArgumentException();
|
throw new NullArgumentException();
|
||||||
}
|
}
|
||||||
|
for (int j = 0; j < yListLen; j++) {
|
||||||
final int len = x.length;
|
final double[] y = yList[j];
|
||||||
final List<Pair<Double, double[]>> list
|
if (y == null) {
|
||||||
= new ArrayList<Pair<Double, double[]>>(len);
|
throw new NullArgumentException();
|
||||||
|
}
|
||||||
final int yListLen = yList.length;
|
if (y.length != len) {
|
||||||
for (int i = 0; i < len; i++) {
|
throw new DimensionMismatchException(y.length, len);
|
||||||
final double[] yValues = new double[yListLen];
|
|
||||||
for (int j = 0; j < yListLen; j++) {
|
|
||||||
double[] y = yList[j];
|
|
||||||
if (y == null) {
|
|
||||||
throw new NullArgumentException();
|
|
||||||
}
|
|
||||||
if (y.length != len) {
|
|
||||||
throw new DimensionMismatchException(y.length, len);
|
|
||||||
}
|
|
||||||
yValues[j] = y[i];
|
|
||||||
}
|
}
|
||||||
list.add(new Pair<Double, double[]>(x[i], yValues));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Comparator<Pair<Double, double[]>> comp
|
// Associate each abscissa "x[i]" with its index "i".
|
||||||
= new Comparator<Pair<Double, double[]>>() {
|
final List<Pair<Double, Integer>> list
|
||||||
public int compare(Pair<Double, double[]> o1,
|
= new ArrayList<Pair<Double, Integer>>(len);
|
||||||
Pair<Double, double[]> o2) {
|
for (int i = 0; i < len; i++) {
|
||||||
int val;
|
list.add(new Pair<Double, Integer>(x[i], i));
|
||||||
switch (dir) {
|
}
|
||||||
case INCREASING:
|
|
||||||
val = o1.getKey().compareTo(o2.getKey());
|
// Create comparators for increasing and decreasing orders.
|
||||||
break;
|
final Comparator<Pair<Double, Integer>> comp
|
||||||
case DECREASING:
|
= dir == MathArrays.OrderDirection.INCREASING ?
|
||||||
val = o2.getKey().compareTo(o1.getKey());
|
new Comparator<Pair<Double, Integer>>() {
|
||||||
break;
|
public int compare(Pair<Double, Integer> o1,
|
||||||
default:
|
Pair<Double, Integer> o2) {
|
||||||
// Should never happen.
|
return o1.getKey().compareTo(o2.getKey());
|
||||||
throw new MathInternalError();
|
}
|
||||||
}
|
} : new Comparator<Pair<Double,Integer>>() {
|
||||||
return val;
|
public int compare(Pair<Double, Integer> o1,
|
||||||
|
Pair<Double, Integer> o2) {
|
||||||
|
return o2.getKey().compareTo(o1.getKey());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Sort.
|
||||||
Collections.sort(list, comp);
|
Collections.sort(list, comp);
|
||||||
|
|
||||||
|
// Modify the original array so that its elements are in
|
||||||
|
// the prescribed order.
|
||||||
|
// Retrieve indices of original locations.
|
||||||
|
final int[] indices = new int[len];
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
final Pair<Double, double[]> e = list.get(i);
|
final Pair<Double, Integer> e = list.get(i);
|
||||||
x[i] = e.getKey();
|
x[i] = e.getKey();
|
||||||
final double[] yValues = e.getValue();
|
indices[i] = e.getValue();
|
||||||
for (int j = 0; j < yListLen; j++) {
|
}
|
||||||
yList[j][i] = yValues[j];
|
|
||||||
|
// In each of the associated arrays, move the
|
||||||
|
// elements to their new location.
|
||||||
|
for (int j = 0; j < yListLen; j++) {
|
||||||
|
// Input array will be modified in place.
|
||||||
|
final double[] yInPlace = yList[j];
|
||||||
|
final double[] yOrig = yInPlace.clone();
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
yInPlace[i] = yOrig[indices[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue