Skip to content

Commit 4b53ecd

Browse files
committed
refactor reverse
1 parent 8d518f5 commit 4b53ecd

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,7 @@ public void swapBlocks(int a, int b, int i, int j) {
760760

761761
/** Reverses the order of the elements in the permutation. */
762762
public void reverse() {
763-
for (int i = 0, j = permutation.length - 1; i < j; i++, j--) {
764-
internalSwap(i, j);
765-
}
763+
internalReverse(0, permutation.length - 1);
766764
hashCodeIsCached = false;
767765
}
768766

@@ -776,13 +774,9 @@ public void reverse() {
776774
*/
777775
public void reverse(int i, int j) {
778776
if (i > j) {
779-
for (; i > j; i--, j++) {
780-
internalSwap(i, j);
781-
}
777+
internalReverse(j, i);
782778
} else {
783-
for (; i < j; i++, j--) {
784-
internalSwap(i, j);
785-
}
779+
internalReverse(i, j);
786780
}
787781
hashCodeIsCached = false;
788782
}
@@ -959,6 +953,12 @@ private boolean validate(int[] p) {
959953
return true;
960954
}
961955

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

0 commit comments

Comments
 (0)