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