src/java.base/share/classes/java/util/random/AbstractArbitrarilyJumpableRNG.java
author jlaskey
Thu, 27 Jun 2019 18:30:27 -0300
branchJDK-8193209-branch
changeset 57437 f02ffcb61dce
parent 57436 b0c958c0e6c6
permissions -rw-r--r--
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     1
/*
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     4
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    10
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    16
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    20
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    23
 * questions.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    24
 */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    25
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    26
package java.util.random;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    27
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    28
import java.util.Spliterator;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    29
import java.util.function.Consumer;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    30
import java.util.function.DoubleConsumer;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    31
import java.util.function.IntConsumer;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    32
import java.util.function.LongConsumer;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    33
import java.util.stream.Stream;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    34
import java.util.stream.StreamSupport;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    35
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    36
/**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    37
 * This class provides much of the implementation of the
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    38
 * {@link ArbitrarilyJumpableRNG} interface, to minimize the effort
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    39
 * required to implement that interface.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    40
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    41
 * To implement a pseudorandom number generator, the programmer needs
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    42
 * only to extend this class and provide implementations for the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    43
 * methods {@code nextInt()}, {@code nextLong()}, {@code copy()},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    44
 * {@code jump(distance)}, {@code jumpPowerOfTwo(distance)},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    45
 * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    46
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    47
 * (If the pseudorandom number generator also has the ability to split,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    48
 * then the programmer may wish to consider instead extending
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    49
 * {@link AbstractSplittableRNG}.)
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    50
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    51
 * The programmer should generally provide at least three constructors:
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    52
 * one that takes no arguments, one that accepts a {@code long}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    53
 * seed value, and one that accepts an array of seed {@code byte} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    54
 * This class provides a public {@code initialSeed()} method that may
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    55
 * be useful in initializing some static state from which to derive
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    56
 * defaults seeds for use by the no-argument constructor.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    57
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    58
 * For the stream methods (such as {@code ints()} and {@code splits()}),
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    59
 * this class provides {@link Spliterator}-based implementations that
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    60
 * allow parallel execution when appropriate.  In this respect
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    61
 * {@link ArbitrarilyJumpableRNG} differs from {@link JumpableRNG},
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    62
 * which provides very simple implementations that produce
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    63
 * sequential streams only.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    64
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    65
 * <p>An implementation of the {@link AbstractArbitrarilyJumpableRNG} class
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    66
 * must provide concrete definitions for the methods {@code nextInt()},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    67
 * {@code nextLong}, {@code period()}, {@code copy()}, {@code jump(double)},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    68
 * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    69
 * Default implementations are provided for all other methods.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    70
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    71
 * The documentation for each non-abstract method in this class
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    72
 * describes its implementation in detail. Each of these methods may
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    73
 * be overridden if the pseudorandom number generator being
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    74
 * implemented admits a more efficient implementation.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    75
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    76
 * @since 14
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    77
 */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    78
public abstract class AbstractArbitrarilyJumpableRNG
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    79
    extends AbstractSpliteratorRNG implements ArbitrarilyJumpableRNG {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    80
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    81
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    82
     * Implementation Overview.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    83
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    84
     * This class provides most of the "user API" methods needed to satisfy
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    85
     * the interface ArbitrarilyJumpableRNG.  Most of these methods
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    86
     * are in turn inherited from AbstractRNG and the non-public class
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    87
     * AbstractSpliteratorRNG; this file implements four versions of the
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    88
     * jumps method and defines the spliterators necessary to support them.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    89
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    90
     * File organization: First the non-public methods needed by the class
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    91
     * AbstractSpliteratorRNG, then the main public methods, followed by some
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
     * custom spliterator classes needed for stream methods.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    94
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    95
    // IllegalArgumentException messages
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
    static final String BadLogDistance  = "logDistance must be non-negative";
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
    98
    // Methods required by class AbstractSpliteratorRNG
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
    Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   100
        return new RandomIntsSpliterator(this, index, fence, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   101
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   102
    Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   103
        return new RandomLongsSpliterator(this, index, fence, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   104
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   105
    Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   106
        return new RandomDoublesSpliterator(this, index, fence, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   107
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
    // Similar methods used by this class
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   110
    Spliterator<RandomNumberGenerator> makeJumpsSpliterator(long index, long fence, double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   111
        return new RandomJumpsSpliterator(this, index, fence, distance);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
    }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   113
    Spliterator<JumpableRNG> makeLeapsSpliterator(long index, long fence, double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   114
        return new RandomLeapsSpliterator(this, index, fence, distance);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
    }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   116
    Spliterator<ArbitrarilyJumpableRNG> makeArbitraryJumpsSpliterator(long index, long fence, double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   117
        return new RandomArbitraryJumpsSpliterator(this, index, fence, distance);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   119
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   120
    /* ---------------- public methods ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   121
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   122
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   123
     * Returns a new generator whose internal state is an exact copy
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   124
     * of this generator (therefore their future behavior should be
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
     * identical if subjected to the same series of operations).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   127
     * @return a new object that is a copy of this generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   129
    public abstract AbstractArbitrarilyJumpableRNG copy();
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
    // Stream methods for jumping
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   133
    private static <T> Stream<T> stream(Spliterator<T> srng) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   134
        return StreamSupport.stream(srng, false);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   135
    }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   136
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   137
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   138
     * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   139
     * implements the {@link RandomNumberGenerator} interface, produced by jumping copies of this
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   140
     * generator by different integer multiples of the default jump distance.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   141
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   142
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   144
     * @implNote This method is implemented to be equivalent to {@code
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   145
     *         jumps(Long.MAX_VALUE)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   146
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   147
    public Stream<RandomNumberGenerator> jumps() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   148
        return stream(makeJumpsSpliterator(0L, Long.MAX_VALUE, defaultJumpDistance()));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   149
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   150
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   151
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
     * new pseudorandom number generators, each of which implements the
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   154
     * {@link RandomNumberGenerator} interface, produced by jumping copies of this generator
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   155
     * by different integer multiples of the default jump distance.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   156
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
     * @param streamSize the number of generators to generate
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   158
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   159
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   160
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   161
     * @throws IllegalArgumentException if {@code streamSize} is less than zero
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   163
    public Stream<RandomNumberGenerator> jumps(long streamSize) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   164
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   165
        return stream(makeJumpsSpliterator(0L, streamSize, defaultJumpDistance()));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   166
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   167
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   169
     * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   170
     * implements the {@link RandomNumberGenerator} interface, produced by jumping copies of this
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   171
     * generator by different integer multiples of the specified jump distance.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   172
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   173
     * @param distance a distance to jump forward within the state cycle
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   174
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   175
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   176
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   177
     * @implNote This method is implemented to be equivalent to {@code
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   178
     *         jumps(Long.MAX_VALUE)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   180
    public Stream<ArbitrarilyJumpableRNG> jumps(double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   181
        return stream(makeArbitraryJumpsSpliterator(0L, Long.MAX_VALUE, distance));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   182
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   183
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   185
     * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   186
     * generators, each of which implements the {@link RandomNumberGenerator} interface, produced by
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   187
     * jumping copies of this generator by different integer multiples of the specified jump
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   188
     * distance.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   189
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   190
     * @param streamSize the number of generators to generate
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   191
     * @param distance   a distance to jump forward within the state cycle
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   192
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   193
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   194
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   195
     * @throws IllegalArgumentException if {@code streamSize} is less than zero
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   196
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   197
    public Stream<ArbitrarilyJumpableRNG> jumps(long streamSize, double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   198
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   199
        return stream(makeArbitraryJumpsSpliterator(0L, streamSize, distance));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   200
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   201
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
     * Alter the state of this pseudorandom number generator so as to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   204
     * jump forward a very large, fixed distance (typically 2<sup>128</sup>
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   205
     * or more) within its state cycle.  The distance used is that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   206
     * returned by method {@code defaultLeapDistance()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   207
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   208
    public void leap() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   209
        jump(defaultLeapDistance());
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   210
    }
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   211
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   212
    // Stream methods for leaping
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   213
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   214
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   215
     * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   216
     * implements the {@link RandomNumberGenerator} interface, produced by jumping copies of this
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   217
     * generator by different integer multiples of the default leap distance.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   218
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   219
     * @implNote This method is implemented to be equivalent to {@code leaps(Long.MAX_VALUE)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   221
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   222
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   223
    public Stream<JumpableRNG> leaps() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   224
        return stream(makeLeapsSpliterator(0L, Long.MAX_VALUE, defaultLeapDistance()));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   225
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   226
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   227
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   228
     * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   229
     * generators, each of which implements the {@link RandomNumberGenerator} interface, produced by
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   230
     * jumping copies of this generator by different integer multiples of the default leap
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   231
     * distance.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   233
     * @param streamSize the number of generators to generate
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   234
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   235
     * @return a stream of objects that implement the {@link RandomNumberGenerator} interface
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   236
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   237
     * @throws IllegalArgumentException if {@code streamSize} is less than zero
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   239
    public Stream<JumpableRNG> leaps(long streamSize) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   240
        return stream(makeLeapsSpliterator(0L, streamSize, defaultLeapDistance()));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   241
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   242
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   243
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   245
     * Spliterator for int streams.  We multiplex the four int versions into one class by treating a
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   246
     * bound less than origin as unbounded, and also by treating "infinite" as equivalent to
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   247
     * {@code Long.MAX_VALUE}. For splits, we choose to override the method {@code trySplit()} to
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   248
     * try to optimize execution speed: instead of dividing a range in half, it breaks off the
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   249
     * largest possible chunk whose size is a power of two such that the remaining chunk is not
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   250
     * empty. In this way, the necessary jump distances will tend to be powers of two.  The long
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   251
     * and double versions of this class are identical except for types.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   252
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   253
    static class RandomIntsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfInt {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   254
        final ArbitrarilyJumpableRNG generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   255
        final int origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   256
        final int bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   257
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   258
        RandomIntsSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, int origin, int bound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   259
            super(index, fence);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   260
            this.origin = origin; this.bound = bound;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   261
            this.generatingRNG = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   262
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   263
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   264
        public Spliterator.OfInt trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   265
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   266
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   267
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   268
            ArbitrarilyJumpableRNG r = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   269
            return new RandomIntsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   270
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   271
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   272
        public boolean tryAdvance(IntConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   273
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   274
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   275
            if (i < f) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   276
                consumer.accept(RNGSupport.boundedNextInt(generatingRNG, origin, bound));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   277
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   278
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   279
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   280
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   281
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   282
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   283
        public void forEachRemaining(IntConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   284
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   285
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   286
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   287
                index = f;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   288
                ArbitrarilyJumpableRNG r = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   289
                int o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   290
                do {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   291
                    consumer.accept(RNGSupport.boundedNextInt(r, o, b));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   292
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   293
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   294
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   295
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   296
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   297
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   298
     * Spliterator for long streams.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   299
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   300
    static class RandomLongsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfLong {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   301
        final ArbitrarilyJumpableRNG generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   302
        final long origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   303
        final long bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   304
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   305
        RandomLongsSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, long origin, long bound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   306
            super(index, fence);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   307
            this.generatingRNG = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   308
            this.origin = origin; this.bound = bound;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   309
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   310
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   311
        public Spliterator.OfLong trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   312
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   313
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   314
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   315
            ArbitrarilyJumpableRNG r = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   316
            return new RandomLongsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   317
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   318
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   319
        public boolean tryAdvance(LongConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   320
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   321
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   322
            if (i < f) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   323
                consumer.accept(RNGSupport.boundedNextLong(generatingRNG, origin, bound));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   324
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   325
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   326
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   327
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   328
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   329
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   330
        public void forEachRemaining(LongConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   331
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   332
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   333
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   334
                index = f;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   335
                ArbitrarilyJumpableRNG r = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   336
                long o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   337
                do {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   338
                    consumer.accept(RNGSupport.boundedNextLong(r, o, b));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   339
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   340
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   341
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   342
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   343
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   344
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   345
     * Spliterator for double streams.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   346
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   347
    static class RandomDoublesSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfDouble {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   348
        final ArbitrarilyJumpableRNG generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   349
        final double origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   350
        final double bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   351
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   352
        RandomDoublesSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, double origin, double bound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   353
            super(index, fence);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   354
            this.generatingRNG = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   355
            this.origin = origin; this.bound = bound;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   356
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   357
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   358
        public Spliterator.OfDouble trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   359
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   360
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   361
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   362
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   363
            ArbitrarilyJumpableRNG r = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   364
            return new RandomDoublesSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   365
        }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   366
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   367
        public boolean tryAdvance(DoubleConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   368
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   369
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   370
            if (i < f) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   371
                consumer.accept(RNGSupport.boundedNextDouble(generatingRNG, origin, bound));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   372
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   373
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   374
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   375
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   376
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   377
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   378
        public void forEachRemaining(DoubleConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   379
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   380
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   381
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   382
                index = f;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   383
                ArbitrarilyJumpableRNG r = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   384
                double o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   385
                do {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   386
                    consumer.accept(RNGSupport.boundedNextDouble(r, o, b));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   387
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   388
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   389
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   390
    }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   391
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   392
    // Spliterators for producing new generators by jumping or leaping.  The
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   393
    // complete implementation of each of these spliterators is right here.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   394
    // In the same manner as for the preceding spliterators, the method trySplit() is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   395
    // coded to optimize execution speed: instead of dividing a range
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   396
    // in half, it breaks off the largest possible chunk whose
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   397
    // size is a power of two such that the remaining chunk is not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   398
    // empty.  In this way, the necessary jump distances will tend to be
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   399
    // powers of two.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   400
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   401
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   402
     * Spliterator for stream of generators of type RandomNumberGenerator produced by jumps.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   403
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   404
    static class RandomJumpsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator<RandomNumberGenerator> {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   405
        ArbitrarilyJumpableRNG generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   406
        final double distance;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   407
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   408
        RandomJumpsSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, double distance) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   409
            super(index, fence);
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   410
            this.generatingRNG = generatingRNG; this.distance = distance;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   411
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   412
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   413
        public Spliterator<RandomNumberGenerator> trySplit() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   414
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   415
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   416
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   417
            ArbitrarilyJumpableRNG r = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   418
            // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   419
            return new RandomJumpsSpliterator(r.copyAndJump(distance * (double)delta), i, m, distance);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   420
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   421
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   422
        public boolean tryAdvance(Consumer<? super RandomNumberGenerator> consumer) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   423
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   424
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   425
            if (i < f) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   426
                consumer.accept(generatingRNG.copyAndJump(distance));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   427
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   428
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   429
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   430
            return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   431
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   432
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   433
        public void forEachRemaining(Consumer<? super RandomNumberGenerator> consumer) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   434
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   435
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   436
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   437
                index = f;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   438
                ArbitrarilyJumpableRNG r = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   439
                do {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   440
                    consumer.accept(r.copyAndJump(distance));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   441
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   442
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   443
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   444
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   445
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   446
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   447
     * Spliterator for stream of generators of type RandomNumberGenerator produced by leaps.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   448
     */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   449
    static class RandomLeapsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator<JumpableRNG> {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   450
        ArbitrarilyJumpableRNG generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   451
        final double distance;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   452
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   453
        RandomLeapsSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, double distance) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   454
            super(index, fence);
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   455
            this.generatingRNG = generatingRNG; this.distance = distance;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   456
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   457
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   458
        public Spliterator<JumpableRNG> trySplit() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   459
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   460
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   461
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   462
            // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   463
            return new RandomLeapsSpliterator(generatingRNG.copyAndJump(distance * (double)delta), i, m, distance);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   464
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   465
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   466
        public boolean tryAdvance(Consumer<? super JumpableRNG> consumer) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   467
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   468
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   469
            if (i < f) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   470
                consumer.accept(generatingRNG.copyAndJump(distance));
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   471
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   472
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   473
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   474
            return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   475
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   476
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   477
        public void forEachRemaining(Consumer<? super JumpableRNG> consumer) {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   478
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   479
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   480
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   481
                index = f;
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   482
                ArbitrarilyJumpableRNG r = generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   483
                do {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   484
                    consumer.accept(r.copyAndJump(distance));
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   485
                } while (++i < f);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   486
            }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   487
        }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   488
    }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   489
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   490
    /**
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   491
     * Spliterator for stream of generators of type RandomNumberGenerator produced by arbitrary jumps.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   492
     */
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   493
    static class RandomArbitraryJumpsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator<ArbitrarilyJumpableRNG> {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   494
        ArbitrarilyJumpableRNG generatingRNG;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   495
        final double distance;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   496
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   497
        RandomArbitraryJumpsSpliterator(ArbitrarilyJumpableRNG generatingRNG, long index, long fence, double distance) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   498
            super(index, fence);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   499
            this.generatingRNG = generatingRNG; this.distance = distance;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   500
        }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   501
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   502
        public Spliterator<ArbitrarilyJumpableRNG> trySplit() {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   503
            long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   504
            if (m <= i) return null;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   505
            index = m;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   506
            // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   507
            return new RandomArbitraryJumpsSpliterator(generatingRNG.copyAndJump(distance * (double)delta), i, m, distance);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   508
        }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   509
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   510
        public boolean tryAdvance(Consumer<? super ArbitrarilyJumpableRNG> consumer) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   511
            if (consumer == null) throw new NullPointerException();
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   512
            long i = index, f = fence;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   513
            if (i < f) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   514
                consumer.accept(generatingRNG.copyAndJump(distance));
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   515
                index = i + 1;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   516
                return true;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   517
            }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   518
            return false;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   519
        }
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   520
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   521
        public void forEachRemaining(Consumer<? super ArbitrarilyJumpableRNG> consumer) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   522
            if (consumer == null) throw new NullPointerException();
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   523
            long i = index, f = fence;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   524
            if (i < f) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   525
                index = f;
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57436
diff changeset
   526
                ArbitrarilyJumpableRNG r = generatingRNG;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   527
                do {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   528
                    consumer.accept(r.copyAndJump(distance));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   529
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   530
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   531
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   532
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   533
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   534
}