Repository: commons-math Updated Branches: refs/heads/feature-MATH-1372 0ca1b7449 -> 423181be3
MATH-1372 Reduce code duplication. Thanks to Artem Barger. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/423181be Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/423181be Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/423181be Branch: refs/heads/feature-MATH-1372 Commit: 423181be37313de63602d551146c313be3cbe2b3 Parents: 0ca1b74 Author: Gilles <[email protected]> Authored: Wed Jun 1 16:21:16 2016 +0200 Committer: Gilles <[email protected]> Committed: Wed Jun 1 16:21:16 2016 +0200 ---------------------------------------------------------------------- .../apache/commons/math4/util/MathArrays.java | 26 +++++++------------- 1 file changed, 9 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/423181be/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 cb9e39a..2b03102 100644 --- a/src/main/java/org/apache/commons/math4/util/MathArrays.java +++ b/src/main/java/org/apache/commons/math4/util/MathArrays.java @@ -1977,24 +1977,16 @@ public class MathArrays { return -1; } + final int direction = isMin ? 1 : -1; + int index = 0; - double selected = data[0]; - - if (isMin) { - for (int i = 1; i < data.length; i++) { - final double current = data[i]; - if (current < selected) { - index = i; - selected = current; - } - } - } else { - for (int i = 1; i < data.length; i++) { - final double current = data[i]; - if (current > selected) { - index = i; - selected = current; - } + double selected = direction * data[0]; + + for (int i = 1; i < data.length; i++) { + final double current = direction * data[i]; + if (current < selected) { + index = i; + selected = current; } }
