Skip to content

Commit 91ad17b

Browse files
authored
Merge pull request #300 from cicirello/pool-sampler
Implemented SequenceSampler interface in SequencePoolSampler
2 parents d2ff7bb + 38d5083 commit 91ad17b

12 files changed

+133
-276
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased] - 2022-11-18
88

9+
**BREAKING CHANGES:** See the Removed section for details.
10+
911
### Added
1012
* Added the SequenceSampler interface's methods to SequenceReservoirSampler.
13+
* Added the SequenceSampler interface's methods to SequencePoolSampler.
1114

1215
### Changed
1316
* SequenceSampler converted from a utility class of static methods to an interface, retaining the existing static
@@ -17,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1720

1821
### Removed
1922
* SequenceSampler.sampleReservoir methods, previously deprecated in 4.3.0, replaced by the SequenceReservoirSampler class.
23+
* SequenceSampler.samplePool methods, previously deprecated in 4.3.0, replaced by the SequencePoolSampler class.
2024

2125
### Fixed
2226

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

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

2929
/**
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.
3231
*
3332
* <p>The methods of this class implement the algorithm SELECT of S. Goodman and S. Hedetniemi, as
3433
* described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer
@@ -41,10 +40,117 @@
4140
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
4241
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4342
*/
44-
public final class SequencePoolSampler extends AbstractSequenceSampler {
43+
public final class SequencePoolSampler extends AbstractSequenceSampler implements SequenceSampler {
4544

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 &gt; source.length
60+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
71+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
82+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
93+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
104+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
115+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
126+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length()
137+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
148+
* @throws NegativeArraySizeException if k &lt; 0
149+
*/
150+
@Override
151+
public <T> T[] nextSample(T[] source, int k, T[] target) {
152+
return sample(source, k, target, r);
153+
}
48154

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

0 commit comments

Comments
 (0)