Skip to content

Commit 0ebdb1e

Browse files
committed
refactor Permutation
1 parent 1e4e5a2 commit 0ebdb1e

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.ThreadLocalRandom;
2929
import java.util.random.RandomGenerator;
3030
import org.cicirello.math.rand.RandomIndexer;
31+
import org.cicirello.util.ArrayFiller;
3132
import org.cicirello.util.Copyable;
3233

3334
/**
@@ -81,16 +82,11 @@ public Permutation(int n, RandomGenerator r) {
8182
* @param value The integer value of the permutation in the interval: 0..(n!-1).
8283
*/
8384
public Permutation(int n, int value) {
84-
permutation = new int[n];
85-
for (int i = 0; i < n; i++) {
86-
permutation[i] = i;
87-
}
85+
permutation = ArrayFiller.create(n);
8886
for (int i = 0; i < n - 1; i++) {
8987
int j = i + value % (n - i);
9088
int temp = permutation[j];
91-
for (int k = j; k > i; k--) {
92-
permutation[k] = permutation[k - 1];
93-
}
89+
System.arraycopy(permutation, i, permutation, i + 1, j - i);
9490
permutation[i] = temp;
9591
value = value / (n - i);
9692
}
@@ -117,17 +113,12 @@ public Permutation(int n, int value) {
117113
* @param value The integer value of the permutation in the interval: 0..(n!-1).
118114
*/
119115
public Permutation(int n, BigInteger value) {
120-
permutation = new int[n];
121-
for (int i = 0; i < n; i++) {
122-
permutation[i] = i;
123-
}
116+
permutation = ArrayFiller.create(n);
124117
for (int i = 0; i < n - 1; i++) {
125118
BigInteger[] divRem = value.divideAndRemainder(BigInteger.valueOf(n - i));
126119
int j = i + divRem[1].intValue();
127120
int temp = permutation[j];
128-
for (int k = j; k > i; k--) {
129-
permutation[k] = permutation[k - 1];
130-
}
121+
System.arraycopy(permutation, i, permutation, i + 1, j - i);
131122
permutation[i] = temp;
132123
value = divRem[0];
133124
}
@@ -341,18 +332,16 @@ public Permutation copy() {
341332
* @throws UnsupportedOperationException when permutation length is greater than 12.
342333
*/
343334
public int toInteger() {
344-
int N = permutation.length;
345-
if (N > 12)
335+
if (permutation.length > 12)
346336
throw new UnsupportedOperationException(
347337
"Unsupported for permutations of length greater than 12.");
348-
int[] index = new int[N];
349-
for (int i = 0; i < N; i++) index[i] = i;
338+
int[] index = ArrayFiller.create(permutation.length);
350339
int result = 0;
351340
int multiplier = 1;
352-
int factor = N;
353-
for (int i = 0; i < N - 1; i++) {
341+
int factor = permutation.length;
342+
for (int i = 0; i < index.length - 1; i++) {
354343
result += multiplier * index[permutation[i]];
355-
for (int j = permutation[i]; j < N; j++) {
344+
for (int j = permutation[i]; j < index.length; j++) {
356345
index[j]--;
357346
}
358347
multiplier *= factor;
@@ -377,16 +366,14 @@ public int toInteger() {
377366
* @return a mixed radix representation of the permutation
378367
*/
379368
public BigInteger toBigInteger() {
380-
int N = permutation.length;
381-
if (N <= 12) return BigInteger.valueOf(toInteger());
382-
int[] index = new int[N];
383-
for (int i = 0; i < N; i++) index[i] = i;
369+
if (permutation.length <= 12) return BigInteger.valueOf(toInteger());
370+
int[] index = ArrayFiller.create(permutation.length);
384371
BigInteger result = BigInteger.ZERO;
385372
BigInteger multiplier = BigInteger.ONE;
386-
int factor = N;
387-
for (int i = 0; i < N - 1; i++) {
373+
int factor = permutation.length;
374+
for (int i = 0; i < index.length - 1; i++) {
388375
result = result.add(multiplier.multiply(BigInteger.valueOf(index[permutation[i]])));
389-
for (int j = permutation[i]; j < N; j++) {
376+
for (int j = permutation[i]; j < index.length; j++) {
390377
index[j]--;
391378
}
392379
multiplier = multiplier.multiply(BigInteger.valueOf(factor));
@@ -425,8 +412,7 @@ public Permutation getInversePermutation() {
425412
* iff p2.get(j) == i, for all i, j.
426413
*/
427414
public void invert() {
428-
int[] inverse = getInverse();
429-
System.arraycopy(inverse, 0, permutation, 0, inverse.length);
415+
System.arraycopy(getInverse(), 0, permutation, 0, permutation.length);
430416
}
431417

432418
/**
@@ -789,13 +775,16 @@ public void removeAndInsert(int i, int j) {
789775
* @param numPositions Number of positions to rotate.
790776
*/
791777
public void rotate(int numPositions) {
792-
if (numPositions >= permutation.length || numPositions < 0)
778+
if (numPositions >= permutation.length || numPositions < 0) {
793779
numPositions = Math.floorMod(numPositions, permutation.length);
794-
if (numPositions == 0) return;
795-
int[] temp = new int[numPositions];
796-
System.arraycopy(permutation, 0, temp, 0, numPositions);
797-
System.arraycopy(permutation, numPositions, permutation, 0, permutation.length - numPositions);
798-
System.arraycopy(temp, 0, permutation, permutation.length - numPositions, numPositions);
780+
}
781+
if (numPositions > 0) {
782+
int[] temp = new int[numPositions];
783+
System.arraycopy(permutation, 0, temp, 0, numPositions);
784+
System.arraycopy(
785+
permutation, numPositions, permutation, 0, permutation.length - numPositions);
786+
System.arraycopy(temp, 0, permutation, permutation.length - numPositions, numPositions);
787+
}
799788
}
800789

801790
/**
@@ -811,7 +800,7 @@ public void rotate(int numPositions) {
811800
* greater than or equal to length().
812801
*/
813802
public void removeAndInsert(int i, int size, int j) {
814-
if ((size == 0) || (i == j)) {
803+
if ((size <= 0) || (i == j)) {
815804
return;
816805
} else if (size == 1) {
817806
removeAndInsert(i, j);
@@ -865,14 +854,15 @@ public Iterator<Permutation> iterator() {
865854
*/
866855
@Override
867856
public String toString() {
868-
String permS = "";
857+
StringBuilder s = new StringBuilder();
869858
if (permutation.length > 0) {
870-
permS += permutation[0];
859+
s.append(permutation[0]);
871860
for (int i = 1; i < permutation.length; i++) {
872-
permS += " " + permutation[i];
861+
s.append(" ");
862+
s.append(permutation[i]);
873863
}
874864
}
875-
return permS;
865+
return s.toString();
876866
}
877867

878868
/**
@@ -908,11 +898,13 @@ public int hashCode() {
908898
private boolean validate(int[] p) {
909899
boolean[] inP = new boolean[p.length];
910900
for (int e : p) {
911-
if (e < 0 || e >= p.length)
901+
if (e < 0 || e >= p.length) {
912902
throw new IllegalArgumentException(
913903
"Elements of a Permutation must be in interval [0, length())");
914-
if (inP[e])
904+
}
905+
if (inP[e]) {
915906
throw new IllegalArgumentException("Duplicate elements are not allowed in a Permutation.");
907+
}
916908
inP[e] = true;
917909
}
918910
return true;

0 commit comments

Comments
 (0)