You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/17 00:30:58 UTC

[commons-numbers] branch master updated (cf5ec33 -> 42a2f65)

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git.


    from cf5ec33  Changed tests to use assertThrows.
     new f6a35da  Use integer division in MultiDimensionalCounter.toMulti
     new bb48af0  Add tests that fails to be detected as a negative index.
     new 42a2f65  Check all indices and the running cumulative are strictly positive.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../numbers/arrays/MultidimensionalCounter.java    | 33 +++++++++++++---------
 .../arrays/MultidimensionalCounterTest.java        |  2 ++
 2 files changed, 21 insertions(+), 14 deletions(-)


[commons-numbers] 03/03: Check all indices and the running cumulative are strictly positive.

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 42a2f65b5b6543f548f951adc7c4b13e5f731a94
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Dec 17 00:30:54 2019 +0000

    Check all indices and the running cumulative are strictly positive.
---
 .../numbers/arrays/MultidimensionalCounter.java        | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
index cd8a47f..c2c94a8 100644
--- a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
@@ -79,14 +79,14 @@ public final class MultidimensionalCounter {
         int tS = 1;
         for (int i = last - 1; i >= 0; i--) {
             final int index = i + 1;
+            checkStrictlyPositive("index size", size[index]);
             tS *= size[index];
+            checkStrictlyPositive("cumulative size", tS);
             uniCounterOffset[i] = tS;
         }
 
         totalSize = tS * size[0];
-        if (totalSize <= 0) {
-            throw new IllegalArgumentException("Negative size: " + totalSize);
-        }
+        checkStrictlyPositive("total size", totalSize);
     }
 
     /**
@@ -190,6 +190,18 @@ public final class MultidimensionalCounter {
     }
 
     /**
+     * Check the size is strictly positive: {@code size > 0}.
+     *
+     * @param name the name of the size
+     * @param size the size
+     */
+    private static void checkStrictlyPositive(String name, int size) {
+        if (size <= 0) {
+            throw new IllegalArgumentException("Not positive " + name + ": " + size);
+        }
+    }
+
+    /**
      * Creates the message for the index out of bounds exception.
      *
      * @param size the size


[commons-numbers] 01/03: Use integer division in MultiDimensionalCounter.toMulti

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit f6a35dabc9a8aab78ddf3dfcb6344e9aa59a75ba
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Dec 17 00:17:46 2019 +0000

    Use integer division in MultiDimensionalCounter.toMulti
---
 .../commons/numbers/arrays/MultidimensionalCounter.java   | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
index 301a0b1..cd8a47f 100644
--- a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
@@ -126,20 +126,13 @@ public final class MultidimensionalCounter {
 
         final int[] indices = new int[dimension];
 
-        int count = 0;
         for (int i = 0; i < last; i++) {
-            int idx = 0;
-            final int offset = uniCounterOffset[i];
-            while (count <= index) {
-                count += offset;
-                ++idx;
-            }
-            --idx;
-            count -= offset;
-            indices[i] = idx;
+            indices[i] = index / uniCounterOffset[i];
+            // index = index % uniCounterOffset[i]
+            index = index - indices[i] * uniCounterOffset[i];
         }
 
-        indices[last] = index - count;
+        indices[last] = index;
 
         return indices;
     }


[commons-numbers] 02/03: Add tests that fails to be detected as a negative index.

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit bb48af087ed0221793ce3e051df3d56b53ec7cd6
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Dec 17 00:20:41 2019 +0000

    Add tests that fails to be detected as a negative index.
    
    (-1,-1) or (Integer.MAX_VALUE, 2, Integer.MAX_VALUE) are not negative as
    a total product.
---
 .../org/apache/commons/numbers/arrays/MultidimensionalCounterTest.java  | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/MultidimensionalCounterTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/MultidimensionalCounterTest.java
index 7294251..88f5815 100644
--- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/MultidimensionalCounterTest.java
+++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/MultidimensionalCounterTest.java
@@ -31,6 +31,8 @@ public class MultidimensionalCounterTest {
         Assertions.assertThrows(IllegalArgumentException.class, () -> MultidimensionalCounter.of(0, 1));
         Assertions.assertThrows(IllegalArgumentException.class, () -> MultidimensionalCounter.of(2, 0));
         Assertions.assertThrows(IllegalArgumentException.class, () -> MultidimensionalCounter.of(-1, 1));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> MultidimensionalCounter.of(-1, -1));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> MultidimensionalCounter.of(Integer.MAX_VALUE, 2, Integer.MAX_VALUE));
 
         final MultidimensionalCounter c = MultidimensionalCounter.of(2, 3);
         Assertions.assertThrows(IllegalArgumentException.class, () -> c.toUni(1, 1, 1));