You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oe...@apache.org on 2015/08/22 11:53:36 UTC

[2/2] [math] MATH-990: improved in-place sorting by using a helper class instead of the generic Pair class, in order to avoid boxing and unboxing

MATH-990: improved in-place sorting by using a helper class instead of
the generic Pair class, in  order to avoid boxing and unboxing

Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/72a46bab
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/72a46bab
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/72a46bab

Branch: refs/heads/master
Commit: 72a46babeb0da02071e47425ebd55da915b98178
Parents: 0b5bd38
Author: Otmar Ertl <ot...@gmail.com>
Authored: Sat Aug 22 11:23:54 2015 +0200
Committer: Otmar Ertl <ot...@gmail.com>
Committed: Sat Aug 22 11:47:09 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 ++
 .../apache/commons/math4/util/MathArrays.java   | 48 ++++++++++++++------
 2 files changed, 38 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/72a46bab/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d209db5..afdaa0c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" 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"> <!-- backported to 3.6 -->
         Avoid overflow in "Fraction" (multiplication or division by an int).
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/72a46bab/src/main/java/org/apache/commons/math4/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/MathArrays.java b/src/main/java/org/apache/commons/math4/util/MathArrays.java
index 86f67fd..3d8330f 100644
--- a/src/main/java/org/apache/commons/math4/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math4/util/MathArrays.java
@@ -766,6 +766,28 @@ public class MathArrays {
     }
 
     /**
+     * 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 place and perform the same reordering of entries on
      * other arrays.  This method works the same as the other
      * {@link #sortInPlace(double[], double[][]) sortInPlace} method, but
@@ -807,26 +829,26 @@ 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>>() {
+            new Comparator<PairDoubleInteger>() {
             @Override
-            public int compare(Pair<Double, Integer> o1,
-                               Pair<Double, Integer> o2) {
-                return o1.getKey().compareTo(o2.getKey());
+            public int compare(PairDoubleInteger o1,
+                               PairDoubleInteger o2) {
+                return Double.compare(o1.getKey(), o2.getKey());
             }
-        } : new Comparator<Pair<Double,Integer>>() {
+        } : new Comparator<PairDoubleInteger>() {
             @Override
-            public int compare(Pair<Double, Integer> o1,
-                               Pair<Double, Integer> o2) {
-                return o2.getKey().compareTo(o1.getKey());
+            public int compare(PairDoubleInteger o1,
+                               PairDoubleInteger o2) {
+                return Double.compare(o2.getKey(), o1.getKey());
             }
         };
 
@@ -838,7 +860,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();
         }