|
26 | 26 | import org.cicirello.util.ArrayMinimumLengthEnforcer; |
27 | 27 |
|
28 | 28 | /** |
29 | | - * SequenceReservoirSampler is a class of utility methods for efficiently generating random samples |
30 | | - * of array elements, without replacement. |
| 29 | + * SequenceReservoirSampler generates random samples of array elements, without replacement. |
31 | 30 | * |
32 | 31 | * <p>The methods of this class use the reservoir sampling algorithm (Algorithm R) from J. Vitter's |
33 | 32 | * 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. |
|
38 | 37 | * @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a |
39 | 38 | * href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a> |
40 | 39 | */ |
41 | | -public final class SequenceReservoirSampler extends AbstractSequenceSampler { |
| 40 | +public final class SequenceReservoirSampler extends AbstractSequenceSampler |
| 41 | + implements SequenceSampler { |
42 | 42 |
|
43 | | - /** Class of static utility methods so prevent instantiation with a private constructor. */ |
44 | | - private SequenceReservoirSampler() {} |
| 43 | + private final RandomGenerator r; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a sampler wrapping a RandomGenerator used as the source of randomness. |
| 47 | + * |
| 48 | + * @param r The source of randomness. |
| 49 | + */ |
| 50 | + public SequenceReservoirSampler(RandomGenerator r) { |
| 51 | + this.r = r; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritDoc} |
| 56 | + * |
| 57 | + * @throws IllegalArgumentException if k > source.length |
| 58 | + * @throws NegativeArraySizeException if k < 0 |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public int[] nextSample(int[] source, int k, int[] target) { |
| 62 | + return sample(source, k, target, r); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritDoc} |
| 67 | + * |
| 68 | + * @throws IllegalArgumentException if k > source.length |
| 69 | + * @throws NegativeArraySizeException if k < 0 |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public long[] nextSample(long[] source, int k, long[] target) { |
| 73 | + return sample(source, k, target, r); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + * |
| 79 | + * @throws IllegalArgumentException if k > source.length |
| 80 | + * @throws NegativeArraySizeException if k < 0 |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public short[] nextSample(short[] source, int k, short[] target) { |
| 84 | + return sample(source, k, target, r); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritDoc} |
| 89 | + * |
| 90 | + * @throws IllegalArgumentException if k > source.length |
| 91 | + * @throws NegativeArraySizeException if k < 0 |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public byte[] nextSample(byte[] source, int k, byte[] target) { |
| 95 | + return sample(source, k, target, r); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritDoc} |
| 100 | + * |
| 101 | + * @throws IllegalArgumentException if k > source.length |
| 102 | + * @throws NegativeArraySizeException if k < 0 |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public double[] nextSample(double[] source, int k, double[] target) { |
| 106 | + return sample(source, k, target, r); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + * |
| 112 | + * @throws IllegalArgumentException if k > source.length |
| 113 | + * @throws NegativeArraySizeException if k < 0 |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public float[] nextSample(float[] source, int k, float[] target) { |
| 117 | + return sample(source, k, target, r); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * {@inheritDoc} |
| 122 | + * |
| 123 | + * @throws IllegalArgumentException if k > source.length |
| 124 | + * @throws NegativeArraySizeException if k < 0 |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public char[] nextSample(char[] source, int k, char[] target) { |
| 128 | + return sample(source, k, target, r); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * {@inheritDoc} |
| 133 | + * |
| 134 | + * @throws IllegalArgumentException if k > source.length() |
| 135 | + * @throws NegativeArraySizeException if k < 0 |
| 136 | + */ |
| 137 | + @Override |
| 138 | + public char[] nextSample(String source, int k, char[] target) { |
| 139 | + return sample(source, k, target, r); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritDoc} |
| 144 | + * |
| 145 | + * @throws IllegalArgumentException if k > source.length |
| 146 | + * @throws NegativeArraySizeException if k < 0 |
| 147 | + */ |
| 148 | + @Override |
| 149 | + public <T> T[] nextSample(T[] source, int k, T[] target) { |
| 150 | + return sample(source, k, target, r); |
| 151 | + } |
45 | 152 |
|
46 | 153 | /** |
47 | 154 | * Generates a random sample of k elements, without replacement, from a given source array. All n |
|
0 commit comments