diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6688fd242..63b38eb8f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Improved performance of sort-in-place methods by avoiding boxing. + Avoid overflow in "Fraction" (multiplication or division by an int). diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java index 46a871678..82310ac4f 100644 --- a/src/main/java/org/apache/commons/math3/util/MathArrays.java +++ b/src/main/java/org/apache/commons/math3/util/MathArrays.java @@ -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> list - = new ArrayList>(len); + final List list + = new ArrayList(len); for (int i = 0; i < len; i++) { - list.add(new Pair(x[i], i)); + list.add(new PairDoubleInteger(x[i], i)); } // Create comparators for increasing and decreasing orders. - final Comparator> comp + final Comparator comp = dir == MathArrays.OrderDirection.INCREASING ? - new Comparator>() { - public int compare(Pair o1, - Pair o2) { - return o1.getKey().compareTo(o2.getKey()); + new Comparator() { + public int compare(PairDoubleInteger o1, + PairDoubleInteger o2) { + return Double.compare(o1.getKey(), o2.getKey()); } - } : new Comparator>() { - public int compare(Pair o1, - Pair o2) { - return o2.getKey().compareTo(o1.getKey()); + } : new Comparator() { + 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 e = list.get(i); + final PairDoubleInteger e = list.get(i); x[i] = e.getKey(); indices[i] = e.getValue(); }