Skip to content

Commit d476bce

Browse files
authored
Merge pull request #316 from cicirello/optimize
Minor optimizations in Permutation class
2 parents 644aa8c + b408fd5 commit d476bce

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ 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-01-12
7+
## [Unreleased] - 2023-02-26
88

99
### Added
1010

1111
### Changed
12+
* Minor optimizations within Permutation class.
1213

1314
### Deprecated
1415

src/main/java/org/cicirello/permutations/Permutation.java

Lines changed: 8 additions & 6 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
*
@@ -605,9 +605,7 @@ public int get(int i) {
605605
*/
606606
public int[] get(int i, int j) {
607607
if (j < i) throw new IllegalArgumentException("j must not be less than i");
608-
int[] array = new int[j - i + 1];
609-
System.arraycopy(permutation, i, array, 0, array.length);
610-
return array;
608+
return Arrays.copyOfRange(permutation, i, j + 1);
611609
}
612610

613611
/**
@@ -776,11 +774,15 @@ public void reverse(int i, int j) {
776774
public void removeAndInsert(int i, int j) {
777775
if (i < j) {
778776
int n = permutation[i];
779-
System.arraycopy(permutation, i + 1, permutation, i, j - i);
777+
for (int k = i; k < j; k++) {
778+
permutation[k] = permutation[k + 1];
779+
}
780780
permutation[j] = n;
781781
} else if (i > j) {
782782
int n = permutation[i];
783-
System.arraycopy(permutation, j, permutation, j + 1, i - j);
783+
for (int k = i; k > j; k--) {
784+
permutation[k] = permutation[k - 1];
785+
}
784786
permutation[j] = n;
785787
}
786788
}

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

Lines changed: 1 addition & 1 deletion
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
*

src/main/java/org/cicirello/sequences/distance/KendallTauSequenceDistance.java

Lines changed: 1 addition & 1 deletion
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
*

0 commit comments

Comments
 (0)