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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
* Refactored Permutation.equals() method (non-breaking).
* Refactored target array allocation in the various SequenceSampler implementations (non-breaking).

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
<version>3.15.0</version>
<configuration>
<release>25</release>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
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-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2026 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -222,7 +222,8 @@ public char[] nextSample(String source, int k, char[] target) {
@Override
public <T> T[] nextSample(T[] source, int k, T[] target) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), k, target);
int[] indexes = r.sampleInsertion(source.length, k, null);
for (int i = 0; i < k; i++) {
target[i] = source[indexes[i]];
Expand Down Expand Up @@ -595,7 +596,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), 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
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-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2026 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -247,7 +247,8 @@ public char[] nextSample(String source, int k, char[] target) {
@Override
public <T> T[] nextSample(T[] source, int k, T[] target) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), k, target);
T[] pool = Arrays.copyOf(source, source.length);
int remaining = pool.length;
for (int i = 0; i < k; i++) {
Expand Down Expand Up @@ -653,7 +654,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), k, target);
T[] pool = Arrays.copyOf(source, source.length);
int remaining = pool.length;
for (int i = 0; i < k; i++) {
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-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2026 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -237,7 +237,8 @@ public char[] nextSample(String source, int k, char[] target) {
@Override
public <T> T[] nextSample(T[] source, int k, T[] target) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), k, target);
System.arraycopy(source, 0, target, 0, k);
for (int i = k; i < source.length; i++) {
int j = r.nextInt(i + 1);
Expand Down Expand Up @@ -635,7 +636,8 @@ public static float[] sample(float[] source, int k, float[] target, RandomGenera
*/
public static <T> T[] sample(T[] source, int k, T[] target, RandomGenerator r) {
SequenceSamplerUtils.validateK(k, source.length);
target = SequenceSamplerUtils.allocateIfNecessary(source, k, target);
target =
SequenceSamplerUtils.allocateIfNecessary(source.getClass().getComponentType(), 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
10 changes: 4 additions & 6 deletions src/main/java/org/cicirello/sequences/SequenceSamplerUtils.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-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2026 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -42,11 +42,9 @@ static void validateK(int k, int sourceLength) {
}

@SuppressWarnings("unchecked")
static <T> T[] allocateIfNecessary(T[] source, int k, T[] target) {
if (target == null) {
target = (T[]) Array.newInstance(source.getClass().getComponentType(), k);
} else if (target.length < k) {
target = (T[]) Array.newInstance(target.getClass().getComponentType(), k);
static <T> T[] allocateIfNecessary(Class<?> componentType, int k, T[] target) {
if (target == null || target.length < k) {
return (T[]) Array.newInstance(componentType, k);
}
return target;
}
Expand Down
Loading