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