Skip to content

Commit d34cc0c

Browse files
authored
Merge pull request #301 from cicirello/insertion-sampler
Implemented SequenceSampler interface in SequenceInsertionSampler
2 parents d7b60ba + 54d0cc2 commit d34cc0c

12 files changed

+133
-321
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Added
1212
* Added the SequenceSampler interface's methods to SequenceReservoirSampler.
1313
* Added the SequenceSampler interface's methods to SequencePoolSampler.
14+
* Added the SequenceSampler interface's methods to SequenceInsertionSampler.
1415

1516
### Changed
1617
* SequenceSampler converted from a utility class of static methods to an interface, retaining the existing static
@@ -21,10 +22,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2122
### Removed
2223
* SequenceSampler.sampleReservoir methods, previously deprecated in 4.3.0, replaced by the SequenceReservoirSampler class.
2324
* SequenceSampler.samplePool methods, previously deprecated in 4.3.0, replaced by the SequencePoolSampler class.
25+
* SequenceSampler.sampleInsertion methods, previously deprecated in 4.3.0, replaced by the SequenceInsertionSampler class.
2426

2527
### Fixed
2628

2729
### Dependencies
30+
* Bump rho-mu from 2.5.0 to 3.0.1
2831

2932
### CI/CD
3033

src/main/java/org/cicirello/sequences/SequenceInsertionSampler.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-
* 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.
3130
*
3231
* <p>The methods of this class implement the insertion sampling algorithm described in:
3332
*
@@ -45,10 +44,118 @@
4544
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
4645
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4746
*/
48-
public final class SequenceInsertionSampler extends AbstractSequenceSampler {
47+
public final class SequenceInsertionSampler extends AbstractSequenceSampler
48+
implements SequenceSampler {
4949

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 &gt; source.length
65+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
76+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
87+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
98+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
109+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
120+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
131+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length()
142+
* @throws NegativeArraySizeException if k &lt; 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 &gt; source.length
153+
* @throws NegativeArraySizeException if k &lt; 0
154+
*/
155+
@Override
156+
public <T> T[] nextSample(T[] source, int k, T[] target) {
157+
return sample(source, k, target, r);
158+
}
52159

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

0 commit comments

Comments
 (0)