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