MATH-990: improved in-place sorting by using a helper class instead of
the generic Pair class, in order to avoid boxing and unboxing
This commit is contained in:
parent
0820703df0
commit
32c5f86125
|
@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
|
|||
</properties>
|
||||
<body>
|
||||
<release version="3.6" date="XXXX-XX-XX" description="">
|
||||
<action dev="oertl" type="update" issue="MATH-990">
|
||||
Improved performance of sort-in-place methods by avoiding boxing.
|
||||
</action>
|
||||
<action dev="erans" type="fix" issue="MATH-1261" due-to="Osamu Ikeuchi">
|
||||
Avoid overflow in "Fraction" (multiplication or division by an int).
|
||||
</action>
|
||||
|
|
|
@ -744,6 +744,28 @@ public class MathArrays {
|
|||
return norm;
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper data structure holding a double and an integer value.
|
||||
*/
|
||||
private static class PairDoubleInteger {
|
||||
|
||||
private final double key;
|
||||
private final int value;
|
||||
|
||||
public PairDoubleInteger(double key, int value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public double getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort an array in ascending order in place and perform the same reordering
|
||||
* of entries on other arrays. For example, if
|
||||
|
@ -807,24 +829,24 @@ public class MathArrays {
|
|||
}
|
||||
|
||||
// Associate each abscissa "x[i]" with its index "i".
|
||||
final List<Pair<Double, Integer>> list
|
||||
= new ArrayList<Pair<Double, Integer>>(len);
|
||||
final List<PairDoubleInteger> list
|
||||
= new ArrayList<PairDoubleInteger>(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
list.add(new Pair<Double, Integer>(x[i], i));
|
||||
list.add(new PairDoubleInteger(x[i], i));
|
||||
}
|
||||
|
||||
// Create comparators for increasing and decreasing orders.
|
||||
final Comparator<Pair<Double, Integer>> comp
|
||||
final Comparator<PairDoubleInteger> comp
|
||||
= dir == MathArrays.OrderDirection.INCREASING ?
|
||||
new Comparator<Pair<Double, Integer>>() {
|
||||
public int compare(Pair<Double, Integer> o1,
|
||||
Pair<Double, Integer> o2) {
|
||||
return o1.getKey().compareTo(o2.getKey());
|
||||
new Comparator<PairDoubleInteger>() {
|
||||
public int compare(PairDoubleInteger o1,
|
||||
PairDoubleInteger o2) {
|
||||
return Double.compare(o1.getKey(), o2.getKey());
|
||||
}
|
||||
} : new Comparator<Pair<Double,Integer>>() {
|
||||
public int compare(Pair<Double, Integer> o1,
|
||||
Pair<Double, Integer> o2) {
|
||||
return o2.getKey().compareTo(o1.getKey());
|
||||
} : new Comparator<PairDoubleInteger>() {
|
||||
public int compare(PairDoubleInteger o1,
|
||||
PairDoubleInteger o2) {
|
||||
return Double.compare(o2.getKey(), o1.getKey());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -836,7 +858,7 @@ public class MathArrays {
|
|||
// Retrieve indices of original locations.
|
||||
final int[] indices = new int[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
final Pair<Double, Integer> e = list.get(i);
|
||||
final PairDoubleInteger e = list.get(i);
|
||||
x[i] = e.getKey();
|
||||
indices[i] = e.getValue();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue