Skip to content

Commit d2ff7bb

Browse files
authored
Merge pull request #299 from cicirello/reservoir-sampler
Implemented SequenceSampler interface in SequenceReservoirSampler
2 parents 1171ad6 + bb7575a commit d2ff7bb

13 files changed

+258
-268
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased] - 2022-11-18
88

99
### Added
10+
* Added the SequenceSampler interface's methods to SequenceReservoirSampler.
1011

1112
### Changed
1213
* SequenceSampler converted from a utility class of static methods to an interface, retaining the existing static
@@ -15,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1516
### Deprecated
1617

1718
### Removed
19+
* SequenceSampler.sampleReservoir methods, previously deprecated in 4.3.0, replaced by the SequenceReservoirSampler class.
1820

1921
### Fixed
2022

src/main/java/org/cicirello/sequences/AbstractSequenceSampler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ abstract class AbstractSequenceSampler {
3434

3535
/** prevent instantiation with a private constructor. */
3636
AbstractSequenceSampler() {}
37-
;
3837

3938
static void validateK(int k, int sourceLength) {
4039
if (k > sourceLength) {

src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import org.cicirello.util.ArrayMinimumLengthEnforcer;
2727

2828
/**
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.
3130
*
3231
* <p>The methods of this class use the reservoir sampling algorithm (Algorithm R) from J. Vitter's
3332
* 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software.
@@ -38,10 +37,118 @@
3837
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
3938
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4039
*/
41-
public final class SequenceReservoirSampler extends AbstractSequenceSampler {
40+
public final class SequenceReservoirSampler extends AbstractSequenceSampler
41+
implements SequenceSampler {
4242

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 &gt; source.length
58+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
69+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
80+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
91+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
102+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
113+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
124+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length()
135+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
146+
* @throws NegativeArraySizeException if k &lt; 0
147+
*/
148+
@Override
149+
public <T> T[] nextSample(T[] source, int k, T[] target) {
150+
return sample(source, k, target, r);
151+
}
45152

46153
/**
47154
* Generates a random sample of k elements, without replacement, from a given source array. All n

0 commit comments

Comments
 (0)