Skip to content

Commit b7277c9

Browse files
committed
minor optimizations
1 parent e01aeec commit b7277c9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

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
}

0 commit comments

Comments
 (0)