commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [2/8] commons-rng git commit: Move "fillState" methods from "SeedFactory" to "BaseProvider".
Date Tue, 01 Nov 2016 23:03:17 GMT
Move "fillState" methods from "SeedFactory" to "BaseProvider".

The methods are only used by implementations that inherit from "BaseProvider".


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

Branch: refs/heads/multimodule
Commit: f2419b7d650e14c08b7cb1a28f86e0e72bacf4d0
Parents: b40e979
Author: Gilles <erans@apache.org>
Authored: Tue Nov 1 12:09:56 2016 +0100
Committer: Gilles <erans@apache.org>
Committed: Tue Nov 1 12:09:56 2016 +0100

----------------------------------------------------------------------
 .../commons/rng/internal/BaseProvider.java      | 95 ++++++++++++++++++++
 .../rng/internal/source32/KISSRandom.java       |  3 +-
 .../internal/source32/MultiplyWithCarry256.java |  3 +-
 .../rng/internal/source64/XorShift1024Star.java |  3 +-
 .../commons/rng/internal/util/SeedFactory.java  | 95 --------------------
 .../commons/rng/internal/BaseProviderTest.java  | 63 +++++++++++--
 .../rng/internal/util/SeedFactoryTest.java      | 36 --------
 7 files changed, 154 insertions(+), 144 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
index 70f1585..b93ae4c 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
@@ -105,6 +105,68 @@ public abstract class BaseProvider
     }
 
     /**
+     * Simple filling procedure.
+     * It will
+     * <ol>
+     *  <li>
+     *   fill the beginning of {@code state} by copying
+     *   {@code min(seed.length, state.length)} elements from
+     *   {@code seed},
+     *  </li>
+     *  <li>
+     *   set all remaining elements of {@code state} with non-zero
+     *   values (even if {@code seed.length < state.length}).
+     *  </li>
+     * </ol>
+     *
+     * @param state State. Must be allocated.
+     * @param seed Seed. Cannot be null.
+     */
+    protected void fillState(int[] state,
+                             int[] seed) {
+        final int stateSize = state.length;
+        final int seedSize = seed.length;
+        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
+
+        if (seedSize < stateSize) {
+            for (int i = seedSize; i < stateSize; i++) {
+                state[i] = (int) (scrambleWell(state[i - seed.length], i) & 0xffffffffL);
+            }
+        }
+    }
+
+    /**
+     * Simple filling procedure.
+     * It will
+     * <ol>
+     *  <li>
+     *   fill the beginning of {@code state} by copying
+     *   {@code min(seed.length, state.length)} elements from
+     *   {@code seed},
+     *  </li>
+     *  <li>
+     *   set all remaining elements of {@code state} with non-zero
+     *   values (even if {@code seed.length < state.length}).
+     *  </li>
+     * </ol>
+     *
+     * @param state State. Must be allocated.
+     * @param seed Seed. Cannot be null.
+     */
+    protected void fillState(long[] state,
+                             long[] seed) {
+        final int stateSize = state.length;
+        final int seedSize = seed.length;
+        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
+
+        if (seedSize < stateSize) {
+            for (int i = seedSize; i < stateSize; i++) {
+                state[i] = scrambleWell(state[i - seed.length], i);
+            }
+        }
+    }
+
+    /**
      * Checks that the {@code state} has the {@code expected} size.
      *
      * @param state State.
@@ -150,4 +212,37 @@ public abstract class BaseProvider
             throw new IllegalArgumentException("Must be strictly positive: " + n);
         }
     }
+
+    /**
+     * Transformation used to scramble the initial state of
+     * a generator.
+     *
+     * @param n Seed element.
+     * @param mult Multiplier.
+     * @param shift Shift.
+     * @param add Offset.
+     * @return the transformed seed element.
+     */
+    private static long scramble(long n,
+                                 long mult,
+                                 int shift,
+                                 int add) {
+        // Code inspired from "AbstractWell" class.
+        return mult * (n ^ (n >> shift)) + add;
+    }
+
+    /**
+     * Transformation used to scramble the initial state of
+     * a generator.
+     *
+     * @param n Seed element.
+     * @param add Offset.
+     * @return the transformed seed element.
+     * @see #scramble(long,long,int,int)
+     */
+    private static long scrambleWell(long n,
+                                     int add) {
+        // Code inspired from "AbstractWell" class.
+        return scramble(n, 1812433253L, 30, add);
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
index 5c0ecf4..7f5e68b 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
@@ -17,7 +17,6 @@
 package org.apache.commons.rng.internal.source32;
 
 import org.apache.commons.rng.internal.util.NumberFactory;
-import org.apache.commons.rng.internal.util.SeedFactory;
 
 /**
  * Port from Marsaglia's <a href="http://www.cse.yorku.ca/~oz/marsaglia-rng.html">
@@ -81,7 +80,7 @@ public class KISSRandom extends IntProvider {
         // Reset the whole state of this RNG (i.e. the 4 state variables).
         // Filling procedure is not part of the reference code.
         final int[] tmp = new int[SEED_SIZE];
-        SeedFactory.fillState(tmp, seed);
+        fillState(tmp, seed);
 
         z = tmp[0];
         w = tmp[1];

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
index 65cf152..9ac5026 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
@@ -18,7 +18,6 @@ package org.apache.commons.rng.internal.source32;
 
 import java.util.Arrays;
 import org.apache.commons.rng.internal.util.NumberFactory;
-import org.apache.commons.rng.internal.util.SeedFactory;
 
 /**
  * Port from Marsaglia's <a href="https://en.wikipedia.org/wiki/Multiply-with-carry">
@@ -90,7 +89,7 @@ public class MultiplyWithCarry256 extends IntProvider {
         // Reset the whole state of this RNG (i.e. "state" and "index").
         // Filling procedure is not part of the reference code.
         final int[] tmp = new int[SEED_SIZE];
-        SeedFactory.fillState(tmp, seed);
+        fillState(tmp, seed);
 
         // First element of the "seed" is the initial "carry".
         final int c = tmp[0];

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source64/XorShift1024Star.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source64/XorShift1024Star.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source64/XorShift1024Star.java
index 0aa2342..940593d 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source64/XorShift1024Star.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/source64/XorShift1024Star.java
@@ -19,7 +19,6 @@ package org.apache.commons.rng.internal.source64;
 
 import java.util.Arrays;
 import org.apache.commons.rng.internal.util.NumberFactory;
-import org.apache.commons.rng.internal.util.SeedFactory;
 
 /**
  * A fast RNG.
@@ -78,7 +77,7 @@ public class XorShift1024Star extends LongProvider {
     private void setSeedInternal(long[] seed) {
         // Reset the whole state of this RNG (i.e. "state" and "index").
         // Filling procedure is not part of the reference code.
-        SeedFactory.fillState(state, seed);
+        fillState(state, seed);
         index = 0;
     }
 

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
index 11fcfb1..fe9e66c 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
@@ -98,68 +98,6 @@ public class SeedFactory {
     }
 
     /**
-     * Simple filling procedure.
-     * It will
-     * <ol>
-     *  <li>
-     *   fill the beginning of {@code state} by copying
-     *   {@code min(seed.length, state.length)} elements from
-     *   {@code seed},
-     *  </li>
-     *  <li>
-     *   set all remaining elements of {@code state} with non-zero
-     *   values (even if {@code seed.length < state.length}).
-     *  </li>
-     * </ol>
-     *
-     * @param state State. Must be allocated.
-     * @param seed Seed. Cannot be null.
-     */
-    public static void fillState(int[] state,
-                                 int[] seed) {
-        final int stateSize = state.length;
-        final int seedSize = seed.length;
-        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
-
-        if (seedSize < stateSize) {
-            for (int i = seedSize; i < stateSize; i++) {
-                state[i] = (int) (scrambleWell(state[i - seed.length], i) & 0xffffffffL);
-            }
-        }
-    }
-
-    /**
-     * Simple filling procedure.
-     * It will
-     * <ol>
-     *  <li>
-     *   fill the beginning of {@code state} by copying
-     *   {@code min(seed.length, state.length)} elements from
-     *   {@code seed},
-     *  </li>
-     *  <li>
-     *   set all remaining elements of {@code state} with non-zero
-     *   values (even if {@code seed.length < state.length}).
-     *  </li>
-     * </ol>
-     *
-     * @param state State. Must be allocated.
-     * @param seed Seed. Cannot be null.
-     */
-    public static void fillState(long[] state,
-                                 long[] seed) {
-        final int stateSize = state.length;
-        final int seedSize = seed.length;
-        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
-
-        if (seedSize < stateSize) {
-            for (int i = seedSize; i < stateSize; i++) {
-                state[i] = scrambleWell(state[i - seed.length], i);
-            }
-        }
-    }
-
-    /**
      * Creates an array of numbers for use as a seed.
      *
      * @param n Size of the array to create.
@@ -321,37 +259,4 @@ public class SeedFactory {
             return source.next() ^ number;
         }
     }
-
-    /**
-     * Transformation used to scramble the initial state of
-     * a generator.
-     *
-     * @param n Seed element.
-     * @param mult Multiplier.
-     * @param shift Shift.
-     * @param add Offset.
-     * @return the transformed seed element.
-     */
-    private static long scramble(long n,
-                                 long mult,
-                                 int shift,
-                                 int add) {
-        // Code inspired from "AbstractWell" class.
-        return mult * (n ^ (n >> shift)) + add;
-    }
-
-    /**
-     * Transformation used to scramble the initial state of
-     * a generator.
-     *
-     * @param n Seed element.
-     * @param add Offset.
-     * @return the transformed seed element.
-     * @see #scramble(long,long,int,int)
-     */
-    private static long scrambleWell(long n,
-                                     int add) {
-        // Code inspired from "AbstractWell" class.
-        return scramble(n, 1812433253L, 30, add);
-    }
 }

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
index 0b73b0d..46956f5 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
@@ -17,8 +17,7 @@
 package org.apache.commons.rng.internal;
 
 import org.junit.Test;
-
-import org.apache.commons.rng.RestorableUniformRandomProvider;
+import org.junit.Assert;
 
 /**
  * Tests for {@link BaseProvider}.
@@ -31,19 +30,57 @@ import org.apache.commons.rng.RestorableUniformRandomProvider;
 public class BaseProviderTest {
     @Test(expected=UnsupportedOperationException.class)
     public void testMissingGetStateInternal() {
-        final RestorableUniformRandomProvider rng = new DummyGenerator();
-        rng.saveState();
+        new DummyGenerator().saveState();
     }
 
     @Test(expected=UnsupportedOperationException.class)
     public void testMissingSetStateInternal() {
-        final RestorableUniformRandomProvider rng = new DummyGenerator();
-        rng.restoreState(new RandomProviderDefaultState(new byte[1]));
+        new DummyGenerator().restoreState(new RandomProviderDefaultState(new byte[1]));
+    }
+
+    @Test
+    public void testFillStateInt() {
+        final int[] state = new int[10];
+        final int[] seed = {1, 2, 3};
+
+        for (int i = 0; i < state.length; i++) {
+            Assert.assertEquals(0, state[i]);
+        }
+
+        new DummyGenerator().fillState(state, seed);
+        for (int i = 0; i < seed.length; i++) {
+            Assert.assertEquals(seed[i], state[i]);
+        }
+        for (int i = seed.length; i < state.length; i++) {
+            Assert.assertNotEquals(0, state[i]);
+        }
+    }
+
+    @Test
+    public void testFillStateLong() {
+        final long[] state = new long[10];
+        final long[] seed = {1, 2, 3};
+
+        for (int i = 0; i < state.length; i++) {
+            Assert.assertEquals(0, state[i]);
+        }
+
+        new DummyGenerator().fillState(state, seed);
+        for (int i = 0; i < seed.length; i++) {
+            Assert.assertEquals(seed[i], state[i]);
+        }
+        for (int i = seed.length; i < state.length; i++) {
+            Assert.assertNotEquals(0, state[i]);
+        }
     }
 }
 
 /**
- * Dummy class for checking the behaviour of an incomplete implementation.
+ * Dummy class for checking the behaviour of
+ * <ul>
+ *  <li>an incomplete implementation</li>
+ *  <li>{@code fillState} methods with "protected" access</li>
+ * </ul>
  */
 class DummyGenerator extends org.apache.commons.rng.internal.source32.IntProvider {
     /** {@inheritDoc} */
@@ -53,4 +90,16 @@ class DummyGenerator extends org.apache.commons.rng.internal.source32.IntProvide
     }
 
     // Missing overrides of "setStateInternal" and "getStateInternal".
+
+    /** {@inheritDoc} */
+    @Override
+    public void fillState(int[] state, int[] seed) {
+        super.fillState(state, seed);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void fillState(long[] state, long[] seed) {
+        super.fillState(state, seed);
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/f2419b7d/commons-rng-core/src/test/java/org/apache/commons/rng/internal/util/SeedFactoryTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/util/SeedFactoryTest.java
b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/util/SeedFactoryTest.java
index 4e6be30..43c754b 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/util/SeedFactoryTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/util/SeedFactoryTest.java
@@ -26,42 +26,6 @@ import org.junit.Test;
  */
 public class SeedFactoryTest {
     @Test
-    public void testFillStateInt() {
-        final int[] state = new int[10];
-        final int[] seed = {1, 2, 3};
-
-        for (int i = 0; i < state.length; i++) {
-            Assert.assertEquals(0, state[i]);
-        }
-
-        SeedFactory.fillState(state, seed);
-        for (int i = 0; i < seed.length; i++) {
-            Assert.assertEquals(seed[i], state[i]);
-        }
-        for (int i = seed.length; i < state.length; i++) {
-            Assert.assertNotEquals(0, state[i]);
-        }
-    }
-
-    @Test
-    public void testFillStateLong() {
-        final long[] state = new long[10];
-        final long[] seed = {1, 2, 3};
-
-        for (int i = 0; i < state.length; i++) {
-            Assert.assertEquals(0, state[i]);
-        }
-
-        SeedFactory.fillState(state, seed);
-        for (int i = 0; i < seed.length; i++) {
-            Assert.assertEquals(seed[i], state[i]);
-        }
-        for (int i = seed.length; i < state.length; i++) {
-            Assert.assertNotEquals(0, state[i]);
-        }
-    }
-
-    @Test
     public void testCreateLong() {
         final Map<Long, Integer> values = new HashMap<Long, Integer>();
 


Mime
View raw message