Skip to content

Commit d112dcb

Browse files
authored
Merge pull request #332 from cicirello/optimize
Refactored reverse, scramble, and swapBlocks methods of Permutation
2 parents 1ec5eec + dead060 commit d112dcb

File tree

5 files changed

+798
-27
lines changed

5 files changed

+798
-27
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-04-13
7+
## [Unreleased] - 2023-04-14
88

99
### Added
1010

1111
### Changed
12+
* Optimized or otherwise refactored for code quality the scramble, reverse, and swapBlocks methods of the Permutation class.
1213

1314
### Deprecated
1415

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,12 @@ public void scramble(int i, int j, RandomGenerator r) {
539539
if (i == j) {
540540
return;
541541
}
542-
int k = j;
542+
int k;
543543
if (i > j) {
544544
k = i;
545545
i = j;
546+
} else {
547+
k = j;
546548
}
547549
boolean changed = false;
548550
for (; k > i + 1; k--) {
@@ -745,22 +747,20 @@ public void swapBlocks(int a, int b, int i, int j) {
745747
// blocks are adjacent
746748
removeAndInsert(i, j - i + 1, a);
747749
} else {
748-
int[] temp = new int[j - a + 1];
750+
int[] temp = new int[j - b];
749751
int k = j - i + 1;
750752
System.arraycopy(permutation, i, temp, 0, k);
751753
int m = i - b - 1;
752754
System.arraycopy(permutation, b + 1, temp, k, m);
753-
System.arraycopy(permutation, a, temp, k + m, b - a + 1);
755+
System.arraycopy(permutation, a, permutation, a + temp.length, b - a + 1);
754756
System.arraycopy(temp, 0, permutation, a, temp.length);
755757
hashCodeIsCached = false;
756758
}
757759
}
758760

759761
/** Reverses the order of the elements in the permutation. */
760762
public void reverse() {
761-
for (int i = 0, j = permutation.length - 1; i < j; i++, j--) {
762-
internalSwap(i, j);
763-
}
763+
internalReverse(0, permutation.length - 1);
764764
hashCodeIsCached = false;
765765
}
766766

@@ -774,13 +774,9 @@ public void reverse() {
774774
*/
775775
public void reverse(int i, int j) {
776776
if (i > j) {
777-
for (; i > j; i--, j++) {
778-
internalSwap(i, j);
779-
}
777+
internalReverse(j, i);
780778
} else {
781-
for (; i < j; i++, j--) {
782-
internalSwap(i, j);
783-
}
779+
internalReverse(i, j);
784780
}
785781
hashCodeIsCached = false;
786782
}
@@ -957,6 +953,12 @@ private boolean validate(int[] p) {
957953
return true;
958954
}
959955

956+
private void internalReverse(int i, int j) {
957+
for (; i < j; i++, j--) {
958+
internalSwap(i, j);
959+
}
960+
}
961+
960962
/*
961963
* Use internally, such as from reverse, etc to avoid
962964
* repeatedly invalidating hashCode cache (as well as from

0 commit comments

Comments
 (0)