src/java.base/share/classes/java/util/random/AbstractSharedRNG.java
author jlaskey
Thu, 27 Jun 2019 18:02:51 -0300
branchJDK-8193209-branch
changeset 57436 b0c958c0e6c6
parent 57388 src/java.base/share/classes/java/util/AbstractSharedRng.java@b1e6bc96af3d
child 57437 f02ffcb61dce
permissions -rw-r--r--
Move random number generators to new folder.
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
/*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
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
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    25
package java.util;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    26
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    27
import java.util.Spliterator;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    28
import java.util.function.Consumer;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    29
import java.util.function.IntConsumer;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    30
import java.util.function.LongConsumer;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    31
import java.util.function.DoubleConsumer;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    32
import java.util.stream.StreamSupport;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    33
import java.util.stream.Stream;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    34
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
 * This class provides much of the implementation of the {@code Rng}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    37
 * interface, to minimize the effort required to implement that interface.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    38
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    39
 * To implement a pseudorandom number generator, the programmer needs
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    40
 * only to extend this class and provide implementations for the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    41
 * {@code nextInt()} and {@code nextLong()} methods.  In order for
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    42
 * the implementations of other methods in this class to operate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    43
 * correctly, it must be safe for multiple threads to call these
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    44
 * methods on that same object.  The principal purpose of this class
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    45
 * is to support the implementations of {@code java.util.Random}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    46
 * and {@code java.util.concurrent.ThreadLocalRandom}, but it could
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    47
 * in principle be used to implement others as well.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    48
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    49
 * (If the pseudorandom number generator has the ability to split or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    50
 * jump, then the programmer may wish to consider instead extending
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    51
 * another abstract class, such as {@code AbstractSplittableRng},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    52
 * {@code AbstractJumpableRng}, {@code AbstractArbitrarilyJumpableRng},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    53
 * {@code AbstractSplittableJumpableRng}, or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    54
 * {@code AbstractSplittableArbitrarilyJumpableRng}.)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    55
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    56
 * The programmer should generally provide at least three constructors:
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    57
 * one that takes no arguments, one that accepts a {@code long}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    58
 * 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
    59
 * This class provides a public {@code initialSeed()} method that may
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    60
 * be useful in initializing some static state from which to derive
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    61
 * defaults seeds for use by the no-argument constructor.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    62
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    63
 * For the stream methods (such as {@code ints()} and {@code splits()}),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    64
 * this class provides {@code Spliterator}-based implementations that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    65
 * allow parallel execution when appropriate.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    66
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    67
 * The documentation for each non-abstract method in this class
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    68
 * describes its implementation in detail. Each of these methods may
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    69
 * be overridden if the pseudorandom number generator being
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    70
 * implemented admits a more efficient implementation.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    71
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    72
 * @author  Guy Steele
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    73
 * @author  Doug Lea
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    74
 * @since   1.9
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    75
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    76
public abstract class AbstractSharedRng extends AbstractSpliteratorRng {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    77
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    78
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    79
     * Implementation Overview.
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
     * This class provides most of the "user API" methods needed to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    82
     * satisfy the interface java.util.Rng.  Most of these methods
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    83
     * are in turn inherited from AbstractRng and the non-public class
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    84
     * AbstractSpliteratorRng; this file implements methods and spliterators
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    85
     * necessary to support the latter.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    86
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    87
     * File organization: First some non-public methods, followed by
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    88
     * some custom spliterator classes needed for stream methods.
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
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    91
    // Methods required by class AbstractSpliteratorRng
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
    Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
	return new RandomIntsSpliterator(this, index, fence, origin, bound);
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
    Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
	return new RandomLongsSpliterator(this, index, fence, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    98
    Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
	return new RandomDoublesSpliterator(this, index, fence, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   100
    }
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
    // Spliterators for producing streams. These are based on abstract
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   103
    // spliterator classes provided by class AbstractSpliteratorRng.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   104
    // Each one needs to define only a constructor and two methods.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   105
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   106
    static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   107
	final AbstractSharedRng generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
        final int origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
        final int bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   110
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   111
        RandomIntsSpliterator(AbstractSharedRng generatingRng, long index, long fence, int origin, int bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
	    super(index, fence);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   113
	    this.generatingRng = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   114
            this.origin = origin; this.bound = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   116
	
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   117
        public Spliterator.OfInt trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
            long i = index, m = (i + fence) >>> 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   119
	    if (m <= i) return null;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   120
	    index = m;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   121
	    // The same generatingRng is used, with no splitting or copying.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   122
	    return new RandomIntsSpliterator(generatingRng, i, m, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   123
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   124
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
        public boolean tryAdvance(IntConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   127
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   129
                consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   133
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   134
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   135
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   136
        public void forEachRemaining(IntConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   137
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   138
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   139
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   140
                index = f;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   141
                Rng r = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   142
                int o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
                do {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   144
                    consumer.accept(RngSupport.boundedNextInt(r, o, b));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   145
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   146
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   147
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   148
    }
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
     * Spliterator for long streams.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
    static class RandomLongsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfLong {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   154
	final AbstractSharedRng generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   155
        final long origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   156
        final long bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   158
        RandomLongsSpliterator(AbstractSharedRng generatingRng, long index, long fence, long origin, long bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   159
	    super(index, fence);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   160
	    this.generatingRng = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   161
            this.origin = origin; this.bound = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   163
	
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   164
        public Spliterator.OfLong trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   165
            long i = index, m = (i + fence) >>> 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   166
	    if (m <= i) return null;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   167
	    index = m;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
	    // The same generatingRng is used, with no splitting or copying.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   169
	    return new RandomLongsSpliterator(generatingRng, i, m, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   170
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   171
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   172
        public boolean tryAdvance(LongConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   173
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   174
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   175
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   176
                consumer.accept(RngSupport.boundedNextLong(generatingRng, origin, bound));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   177
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   178
                return true;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   180
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   181
        }
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
        public void forEachRemaining(LongConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   185
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   186
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   187
                index = f;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   188
                Rng r = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   189
                long o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   190
                do {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   191
                    consumer.accept(RngSupport.boundedNextLong(r, o, b));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   192
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   193
            }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   194
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   195
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   196
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   197
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   198
     * Spliterator for double streams.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   199
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   200
    static class RandomDoublesSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfDouble {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   201
	final AbstractSharedRng generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
        final double origin;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
        final double bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   204
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   205
        RandomDoublesSpliterator(AbstractSharedRng generatingRng, long index, long fence, double origin, double bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   206
	    super(index, fence);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   207
	    this.generatingRng = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   208
            this.origin = origin; this.bound = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   209
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   210
	
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   211
        public Spliterator.OfDouble trySplit() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   212
            long i = index, m = (i + fence) >>> 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   213
	    if (m <= i) return null;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   214
	    index = m;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   215
	    // The same generatingRng is used, with no splitting or copying.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   216
	    return new RandomDoublesSpliterator(generatingRng, i, m, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   217
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   218
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   219
        public boolean tryAdvance(DoubleConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   221
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   222
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   223
                consumer.accept(RngSupport.boundedNextDouble(generatingRng, origin, bound));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   224
                index = i + 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   225
                return true;
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
            else return false;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   228
        }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   229
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   230
        public void forEachRemaining(DoubleConsumer consumer) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   231
            if (consumer == null) throw new NullPointerException();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
            long i = index, f = fence;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   233
            if (i < f) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   234
                index = f;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   235
                Rng r = generatingRng;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   236
                double o = origin, b = bound;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   237
                do {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
                    consumer.accept(RngSupport.boundedNextDouble(r, o, b));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   239
                } while (++i < f);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   240
            }
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
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   243
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
}