Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1C09D200BD8 for ; Wed, 2 Nov 2016 00:03:19 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 1A997160B0B; Tue, 1 Nov 2016 23:03:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DFEAA160B07 for ; Wed, 2 Nov 2016 00:03:17 +0100 (CET) Received: (qmail 84657 invoked by uid 500); 1 Nov 2016 23:03:16 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 84407 invoked by uid 99); 1 Nov 2016 23:03:16 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Nov 2016 23:03:16 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9482CE3839; Tue, 1 Nov 2016 23:03:16 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: erans@apache.org To: commits@commons.apache.org Date: Tue, 01 Nov 2016 23:03:17 -0000 Message-Id: <6917ef12546f473f813549c72e02c3cc@git.apache.org> In-Reply-To: <52175208aee14ac29183c49ce5b792fc@git.apache.org> References: <52175208aee14ac29183c49ce5b792fc@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/8] commons-rng git commit: Move "fillState" methods from "SeedFactory" to "BaseProvider". archived-at: Tue, 01 Nov 2016 23:03:19 -0000 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 Authored: Tue Nov 1 12:09:56 2016 +0100 Committer: Gilles 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 + *
    + *
  1. + * fill the beginning of {@code state} by copying + * {@code min(seed.length, state.length)} elements from + * {@code seed}, + *
  2. + *
  3. + * set all remaining elements of {@code state} with non-zero + * values (even if {@code seed.length < state.length}). + *
  4. + *
+ * + * @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 + *
    + *
  1. + * fill the beginning of {@code state} by copying + * {@code min(seed.length, state.length)} elements from + * {@code seed}, + *
  2. + *
  3. + * set all remaining elements of {@code state} with non-zero + * values (even if {@code seed.length < state.length}). + *
  4. + *
+ * + * @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 @@ -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 @@ -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 - *
    - *
  1. - * fill the beginning of {@code state} by copying - * {@code min(seed.length, state.length)} elements from - * {@code seed}, - *
  2. - *
  3. - * set all remaining elements of {@code state} with non-zero - * values (even if {@code seed.length < state.length}). - *
  4. - *
- * - * @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 - *
    - *
  1. - * fill the beginning of {@code state} by copying - * {@code min(seed.length, state.length)} elements from - * {@code seed}, - *
  2. - *
  3. - * set all remaining elements of {@code state} with non-zero - * values (even if {@code seed.length < state.length}). - *
  4. - *
- * - * @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 + *
    + *
  • an incomplete implementation
  • + *
  • {@code fillState} methods with "protected" access
  • + *
*/ 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 values = new HashMap();