Skip to content

Commit e456a33

Browse files
authored
Merge pull request #324 from cicirello/refactor
Minor code improvements to the SequenceSamplers and KendallTauSequenceDistance
2 parents dba2610 + 8614217 commit e456a33

File tree

8 files changed

+102
-139
lines changed

8 files changed

+102
-139
lines changed

CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2023-03-05
7+
## [Unreleased] - 2023-03-07
88

99
### Added
1010

1111
### Changed
12-
* Minor code improvements to Permutation and PermutationIterator classes.
13-
* Minor code improvements to KendallTauDistance and WeightedKendallTauDistance.
14-
* Minor code improvements to CyclicEdgeDistance and CyclicRTypeDistance.
12+
* Minor code improvements and/or optimizations within the following classes:
13+
* Permutation
14+
* PermutationIterator
15+
* KendallTauDistance
16+
* WeightedKendallTauDistance.
17+
* CyclicEdgeDistance
18+
* CyclicRTypeDistance.
19+
* The various SequenceSamplers
20+
* KendallTauSequenceDistance
1521

1622
### Deprecated
1723

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools: A Java library for computation on permutations and sequences
3-
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -45,8 +45,7 @@
4545
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
4646
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4747
*/
48-
public final class SequenceInsertionSampler extends AbstractSequenceSampler
49-
implements SequenceSampler {
48+
public final class SequenceInsertionSampler implements SequenceSampler {
5049

5150
private final RandomGenerator r;
5251

@@ -299,7 +298,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
299298
* @throws NegativeArraySizeException if k &lt; 0
300299
*/
301300
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
302-
validateK(k, source.length);
301+
SequenceSamplerUtils.validateK(k, source.length);
303302
target = RandomSampler.sampleInsertion(source.length, k, target, r);
304303
for (int i = 0; i < k; i++) {
305304
target[i] = source[target[i]];
@@ -321,7 +320,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
321320
* @throws NegativeArraySizeException if k &lt; 0
322321
*/
323322
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
324-
validateK(k, source.length);
323+
SequenceSamplerUtils.validateK(k, source.length);
325324
target = ArrayMinimumLengthEnforcer.enforce(target, k);
326325
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
327326
for (int i = 0; i < k; i++) {
@@ -344,7 +343,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
344343
* @throws NegativeArraySizeException if k &lt; 0
345344
*/
346345
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
347-
validateK(k, source.length);
346+
SequenceSamplerUtils.validateK(k, source.length);
348347
target = ArrayMinimumLengthEnforcer.enforce(target, k);
349348
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
350349
for (int i = 0; i < k; i++) {
@@ -367,7 +366,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
367366
* @throws NegativeArraySizeException if k &lt; 0
368367
*/
369368
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
370-
validateK(k, source.length);
369+
SequenceSamplerUtils.validateK(k, source.length);
371370
target = ArrayMinimumLengthEnforcer.enforce(target, k);
372371
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
373372
for (int i = 0; i < k; i++) {
@@ -390,7 +389,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
390389
* @throws NegativeArraySizeException if k &lt; 0
391390
*/
392391
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
393-
validateK(k, source.length);
392+
SequenceSamplerUtils.validateK(k, source.length);
394393
target = ArrayMinimumLengthEnforcer.enforce(target, k);
395394
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
396395
for (int i = 0; i < k; i++) {
@@ -430,7 +429,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
430429
* @throws NegativeArraySizeException if k &lt; 0
431430
*/
432431
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
433-
validateK(k, source.length);
432+
SequenceSamplerUtils.validateK(k, source.length);
434433
target = ArrayMinimumLengthEnforcer.enforce(target, k);
435434
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
436435
for (int i = 0; i < k; i++) {
@@ -453,7 +452,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
453452
* @throws NegativeArraySizeException if k &lt; 0
454453
*/
455454
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
456-
validateK(k, source.length);
455+
SequenceSamplerUtils.validateK(k, source.length);
457456
target = ArrayMinimumLengthEnforcer.enforce(target, k);
458457
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
459458
for (int i = 0; i < k; i++) {
@@ -477,8 +476,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
477476
* @throws NegativeArraySizeException if k &lt; 0
478477
*/
479478
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
480-
validateK(k, source.length);
481-
target = allocateIfNecessary(source, k, target);
479+
SequenceSamplerUtils.validateK(k, source.length);
480+
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
482481
int[] indexes = RandomSampler.sampleInsertion(source.length, k, null, r);
483482
for (int i = 0; i < k; i++) {
484483
target[i] = source[indexes[i]];

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools: A Java library for computation on permutations and sequences
3-
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -41,7 +41,7 @@
4141
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
4242
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4343
*/
44-
public final class SequencePoolSampler extends AbstractSequenceSampler implements SequenceSampler {
44+
public final class SequencePoolSampler implements SequenceSampler {
4545

4646
private final RandomGenerator r;
4747

@@ -294,7 +294,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
294294
* @throws NegativeArraySizeException if k &lt; 0
295295
*/
296296
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
297-
validateK(k, source.length);
297+
SequenceSamplerUtils.validateK(k, source.length);
298298
target = ArrayMinimumLengthEnforcer.enforce(target, k);
299299
int[] pool = source.clone();
300300
int remaining = pool.length;
@@ -321,7 +321,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
321321
* @throws NegativeArraySizeException if k &lt; 0
322322
*/
323323
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
324-
validateK(k, source.length);
324+
SequenceSamplerUtils.validateK(k, source.length);
325325
target = ArrayMinimumLengthEnforcer.enforce(target, k);
326326
long[] pool = source.clone();
327327
int remaining = pool.length;
@@ -348,7 +348,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
348348
* @throws NegativeArraySizeException if k &lt; 0
349349
*/
350350
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
351-
validateK(k, source.length);
351+
SequenceSamplerUtils.validateK(k, source.length);
352352
target = ArrayMinimumLengthEnforcer.enforce(target, k);
353353
short[] pool = source.clone();
354354
int remaining = pool.length;
@@ -375,7 +375,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
375375
* @throws NegativeArraySizeException if k &lt; 0
376376
*/
377377
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
378-
validateK(k, source.length);
378+
SequenceSamplerUtils.validateK(k, source.length);
379379
target = ArrayMinimumLengthEnforcer.enforce(target, k);
380380
byte[] pool = source.clone();
381381
int remaining = pool.length;
@@ -402,7 +402,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
402402
* @throws NegativeArraySizeException if k &lt; 0
403403
*/
404404
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
405-
validateK(k, source.length);
405+
SequenceSamplerUtils.validateK(k, source.length);
406406
target = ArrayMinimumLengthEnforcer.enforce(target, k);
407407
char[] pool = source.clone();
408408
int remaining = pool.length;
@@ -446,7 +446,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
446446
* @throws NegativeArraySizeException if k &lt; 0
447447
*/
448448
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
449-
validateK(k, source.length);
449+
SequenceSamplerUtils.validateK(k, source.length);
450450
target = ArrayMinimumLengthEnforcer.enforce(target, k);
451451
double[] pool = source.clone();
452452
int remaining = pool.length;
@@ -473,7 +473,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
473473
* @throws NegativeArraySizeException if k &lt; 0
474474
*/
475475
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
476-
validateK(k, source.length);
476+
SequenceSamplerUtils.validateK(k, source.length);
477477
target = ArrayMinimumLengthEnforcer.enforce(target, k);
478478
float[] pool = source.clone();
479479
int remaining = pool.length;
@@ -501,8 +501,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
501501
* @throws NegativeArraySizeException if k &lt; 0
502502
*/
503503
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
504-
validateK(k, source.length);
505-
target = allocateIfNecessary(source, k, target);
504+
SequenceSamplerUtils.validateK(k, source.length);
505+
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
506506
T[] pool = Arrays.copyOf(source, source.length);
507507
int remaining = pool.length;
508508
for (int i = 0; i < k; i++) {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
3939
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4040
*/
41-
public final class SequenceReservoirSampler extends AbstractSequenceSampler
42-
implements SequenceSampler {
41+
public final class SequenceReservoirSampler implements SequenceSampler {
4342

4443
private final RandomGenerator r;
4544

@@ -292,7 +291,7 @@ public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
292291
* @throws NegativeArraySizeException if k &lt; 0
293292
*/
294293
public static int[] sample(int[] source, int k, int[] target, RandomGenerator r) {
295-
validateK(k, source.length);
294+
SequenceSamplerUtils.validateK(k, source.length);
296295
target = ArrayMinimumLengthEnforcer.enforce(target, k);
297296
System.arraycopy(source, 0, target, 0, k);
298297
for (int i = k; i < source.length; i++) {
@@ -318,7 +317,7 @@ public static int[] sample(int[] source, int k, int[] target, RandomGenerator r)
318317
* @throws NegativeArraySizeException if k &lt; 0
319318
*/
320319
public static long[] sample(long[] source, int k, long[] target, RandomGenerator r) {
321-
validateK(k, source.length);
320+
SequenceSamplerUtils.validateK(k, source.length);
322321
target = ArrayMinimumLengthEnforcer.enforce(target, k);
323322
System.arraycopy(source, 0, target, 0, k);
324323
for (int i = k; i < source.length; i++) {
@@ -344,7 +343,7 @@ public static long[] sample(long[] source, int k, long[] target, RandomGenerator
344343
* @throws NegativeArraySizeException if k &lt; 0
345344
*/
346345
public static short[] sample(short[] source, int k, short[] target, RandomGenerator r) {
347-
validateK(k, source.length);
346+
SequenceSamplerUtils.validateK(k, source.length);
348347
target = ArrayMinimumLengthEnforcer.enforce(target, k);
349348
System.arraycopy(source, 0, target, 0, k);
350349
for (int i = k; i < source.length; i++) {
@@ -370,7 +369,7 @@ public static short[] sample(short[] source, int k, short[] target, RandomGenera
370369
* @throws NegativeArraySizeException if k &lt; 0
371370
*/
372371
public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator r) {
373-
validateK(k, source.length);
372+
SequenceSamplerUtils.validateK(k, source.length);
374373
target = ArrayMinimumLengthEnforcer.enforce(target, k);
375374
System.arraycopy(source, 0, target, 0, k);
376375
for (int i = k; i < source.length; i++) {
@@ -396,7 +395,7 @@ public static byte[] sample(byte[] source, int k, byte[] target, RandomGenerator
396395
* @throws NegativeArraySizeException if k &lt; 0
397396
*/
398397
public static char[] sample(char[] source, int k, char[] target, RandomGenerator r) {
399-
validateK(k, source.length);
398+
SequenceSamplerUtils.validateK(k, source.length);
400399
target = ArrayMinimumLengthEnforcer.enforce(target, k);
401400
System.arraycopy(source, 0, target, 0, k);
402401
for (int i = k; i < source.length; i++) {
@@ -439,7 +438,7 @@ public static char[] sample(String source, int k, char[] target, RandomGenerator
439438
* @throws NegativeArraySizeException if k &lt; 0
440439
*/
441440
public static double[] sample(double[] source, int k, double[] target, RandomGenerator r) {
442-
validateK(k, source.length);
441+
SequenceSamplerUtils.validateK(k, source.length);
443442
target = ArrayMinimumLengthEnforcer.enforce(target, k);
444443
System.arraycopy(source, 0, target, 0, k);
445444
for (int i = k; i < source.length; i++) {
@@ -465,7 +464,7 @@ public static double[] sample(double[] source, int k, double[] target, RandomGen
465464
* @throws NegativeArraySizeException if k &lt; 0
466465
*/
467466
public static float[] sample(float[] source, int k, float[] target, RandomGenerator r) {
468-
validateK(k, source.length);
467+
SequenceSamplerUtils.validateK(k, source.length);
469468
target = ArrayMinimumLengthEnforcer.enforce(target, k);
470469
System.arraycopy(source, 0, target, 0, k);
471470
for (int i = k; i < source.length; i++) {
@@ -492,8 +491,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
492491
* @throws NegativeArraySizeException if k &lt; 0
493492
*/
494493
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
495-
validateK(k, source.length);
496-
target = allocateIfNecessary(source, k, target);
494+
SequenceSamplerUtils.validateK(k, source.length);
495+
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
497496
System.arraycopy(source, 0, target, 0, k);
498497
for (int i = k; i < source.length; i++) {
499498
int j = RandomIndexer.nextInt(i + 1, r);

src/main/java/org/cicirello/sequences/AbstractSequenceSampler.java renamed to src/main/java/org/cicirello/sequences/SequenceSamplerUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools: A Java library for computation on permutations and sequences
3-
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -24,16 +24,16 @@
2424
import java.lang.reflect.Array;
2525

2626
/**
27-
* AbstractSequenceSampler is an internal abstract base class for the classes of utility methods
28-
* related to efficiently generating random samples of array elements, without replacement.
27+
* SequenceSamplerUtils is an internal utility class with utility methods related to efficiently
28+
* generating random samples of array elements, without replacement.
2929
*
3030
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
3131
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
3232
*/
33-
abstract class AbstractSequenceSampler {
33+
class SequenceSamplerUtils {
3434

3535
/** prevent instantiation with a private constructor. */
36-
AbstractSequenceSampler() {}
36+
private SequenceSamplerUtils() {}
3737

3838
static void validateK(int k, int sourceLength) {
3939
if (k > sourceLength) {

0 commit comments

Comments
 (0)