Skip to content

Commit

Permalink
Merge pull request #324 from cicirello/refactor
Browse files Browse the repository at this point in the history
Minor code improvements to the SequenceSamplers and KendallTauSequenceDistance
  • Loading branch information
cicirello authored Mar 7, 2023
2 parents dba2610 + 8614217 commit e456a33
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 139 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - 2023-03-05
## [Unreleased] - 2023-03-07

### Added

### Changed
* Minor code improvements to Permutation and PermutationIterator classes.
* Minor code improvements to KendallTauDistance and WeightedKendallTauDistance.
* Minor code improvements to CyclicEdgeDistance and CyclicRTypeDistance.
* Minor code improvements and/or optimizations within the following classes:
* Permutation
* PermutationIterator
* KendallTauDistance
* WeightedKendallTauDistance.
* CyclicEdgeDistance
* CyclicRTypeDistance.
* The various SequenceSamplers
* KendallTauSequenceDistance

### Deprecated

Expand Down
23 changes: 11 additions & 12 deletions src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools: A Java library for computation on permutations and sequences
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -45,8 +45,7 @@
* @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
implements SequenceSampler {
public final class SequenceInsertionSampler implements SequenceSampler {

private final RandomGenerator r;

Expand Down Expand Up @@ -299,7 +298,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
* @throws NegativeArraySizeException if k &lt; 0
*/
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = RandomSampler.sampleInsertion(source.length, k, target, r);
for (int i = 0; i < k; i++) {
target[i] = source[target[i]];
Expand All @@ -321,7 +320,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
* @throws NegativeArraySizeException if k &lt; 0
*/
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand All @@ -344,7 +343,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand All @@ -367,7 +366,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand All @@ -390,7 +389,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand Down Expand Up @@ -430,7 +429,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand All @@ -453,7 +452,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
* @throws NegativeArraySizeException if k &lt; 0
*/
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
Expand All @@ -477,8 +476,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
validateK(k, source.length);
target = allocateIfNecessary(source, k, target);
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
for (int i = 0; i < k; i++) {
target[i] = source[indexes[i]];
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/cicirello/sequences/SequencePoolSampler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools: A Java library for computation on permutations and sequences
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -41,7 +41,7 @@
* @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 SequencePoolSampler extends AbstractSequenceSampler implements SequenceSampler {
public final class SequencePoolSampler implements SequenceSampler {

private final RandomGenerator r;

Expand Down Expand Up @@ -294,7 +294,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
* @throws NegativeArraySizeException if k &lt; 0
*/
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
int[] pool = source.clone();
int remaining = pool.length;
Expand All @@ -321,7 +321,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
* @throws NegativeArraySizeException if k &lt; 0
*/
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
long[] pool = source.clone();
int remaining = pool.length;
Expand All @@ -348,7 +348,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
short[] pool = source.clone();
int remaining = pool.length;
Expand All @@ -375,7 +375,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
byte[] pool = source.clone();
int remaining = pool.length;
Expand All @@ -402,7 +402,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
char[] pool = source.clone();
int remaining = pool.length;
Expand Down Expand Up @@ -446,7 +446,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
double[] pool = source.clone();
int remaining = pool.length;
Expand All @@ -473,7 +473,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
* @throws NegativeArraySizeException if k &lt; 0
*/
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
float[] pool = source.clone();
int remaining = pool.length;
Expand Down Expand Up @@ -501,8 +501,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
validateK(k, source.length);
target = allocateIfNecessary(source, k, target);
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
T[] pool = Arrays.copyOf(source, source.length);
int remaining = pool.length;
for (int i = 0; i < k; i++) {
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
* @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 SequenceReservoirSampler extends AbstractSequenceSampler
implements SequenceSampler {
public final class SequenceReservoirSampler implements SequenceSampler {

private final RandomGenerator r;

Expand Down Expand Up @@ -292,7 +291,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
* @throws NegativeArraySizeException if k &lt; 0
*/
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -318,7 +317,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
* @throws NegativeArraySizeException if k &lt; 0
*/
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -344,7 +343,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -370,7 +369,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -396,7 +395,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand Down Expand Up @@ -439,7 +438,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
* @throws NegativeArraySizeException if k &lt; 0
*/
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -465,7 +464,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
* @throws NegativeArraySizeException if k &lt; 0
*/
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
validateK(k, source.length);
SequenceSamplerUtils.validateK(k, source.length);
target = ArrayMinimumLengthEnforcer.enforce(target, k);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
Expand All @@ -492,8 +491,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
* @throws NegativeArraySizeException if k &lt; 0
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
validateK(k, source.length);
target = allocateIfNecessary(source, k, target);
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
int j = RandomIndexer.nextInt(i + 1, r);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools: A Java library for computation on permutations and sequences
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand All @@ -24,16 +24,16 @@
import java.lang.reflect.Array;

/**
* AbstractSequenceSampler is an internal abstract base class for the classes of utility methods
* related to efficiently generating random samples of array elements, without replacement.
* SequenceSamplerUtils is an internal utility class with utility methods related to efficiently
* generating random samples of array elements, without replacement.
*
* @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>
*/
abstract class AbstractSequenceSampler {
class SequenceSamplerUtils {

/** prevent instantiation with a private constructor. */
AbstractSequenceSampler() {}
private SequenceSamplerUtils() {}

static void validateK(int k, int sourceLength) {
if (k > sourceLength) {
Expand Down
Loading

0 comments on commit e456a33

Please sign in to comment.