Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* Added the SequenceSampler interface's methods to SequenceReservoirSampler.
* Added the SequenceSampler interface's methods to SequencePoolSampler.
* Added the SequenceSampler interface's methods to SequenceInsertionSampler.

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

### Fixed

### Dependencies
* Bump rho-mu from 2.5.0 to 3.0.1

### CI/CD

Expand Down
117 changes: 112 additions & 5 deletions src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import org.cicirello.util.ArrayMinimumLengthEnforcer;

/**
* SequenceInsertionSampler is a class of utility methods for efficiently generating random samples
* of array elements, without replacement.
* SequenceInsertionSampler generates random samples of array elements, without replacement.
*
* <p>The methods of this class implement the insertion sampling algorithm described in:
*
Expand All @@ -45,10 +44,118 @@
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
*/
public final class SequenceInsertionSampler extends AbstractSequenceSampler {
public final class SequenceInsertionSampler extends AbstractSequenceSampler
implements SequenceSampler {

/** Class of static utility methods so prevent instantiation with a private constructor. */
private SequenceInsertionSampler() {}
private final RandomGenerator r;

/**
* Constructs a sampler wrapping a RandomGenerator used as the source of randomness.
*
* @param r The source of randomness.
*/
public SequenceInsertionSampler(RandomGenerator r) {
this.r = r;
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public int[] nextSample(int[] source, int k, int[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public long[] nextSample(long[] source, int k, long[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public short[] nextSample(short[] source, int k, short[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public byte[] nextSample(byte[] source, int k, byte[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public double[] nextSample(double[] source, int k, double[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public float[] nextSample(float[] source, int k, float[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public char[] nextSample(char[] source, int k, char[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length()
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public char[] nextSample(String source, int k, char[] target) {
return sample(source, k, target, r);
}

/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if k &gt; source.length
* @throws NegativeArraySizeException if k &lt; 0
*/
@Override
public <T> T[] nextSample(T[] source, int k, T[] target) {
return sample(source, k, target, r);
}

/**
* Generates a random sample of k elements, without replacement, from a given source array. All n
Expand Down
Loading