Skip to content

Commit 74407e7

Browse files
committed
refactor cyclic edge and cyclic rtype distances
1 parent e9da29e commit 74407e7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/main/java/org/cicirello/permutations/distance/CyclicEdgeDistance.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools - A Java library for computation on permutations.
3-
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* JavaPermutationTools is free software: you can
66
* redistribute it and/or modify it under the terms of the GNU
@@ -61,12 +61,14 @@ public int distance(Permutation p1, Permutation p2) {
6161
int countNonSharedEdges = 0;
6262
int[] successors2 = new int[p2.length()];
6363
for (int i = 0; i < successors2.length; i++) {
64-
successors2[p2.get(i)] = p2.get((i + 1) % successors2.length);
64+
successors2[p2.get(i)] = p2.get(indexCyclicAdjustment(i + 1, successors2.length));
6565
}
6666

6767
for (int i = 0; i < successors2.length; i++) {
68-
if (p1.get((i + 1) % successors2.length) != successors2[p1.get(i)]
69-
&& p1.get(i) != successors2[p1.get((i + 1) % successors2.length)]) countNonSharedEdges++;
68+
int j = indexCyclicAdjustment(i + 1, successors2.length);
69+
if (p1.get(j) != successors2[p1.get(i)] && p1.get(i) != successors2[p1.get(j)]) {
70+
countNonSharedEdges++;
71+
}
7072
}
7173

7274
return countNonSharedEdges;
@@ -78,4 +80,8 @@ public int max(int length) {
7880
if (length == 4) return 2;
7981
return length;
8082
}
83+
84+
private int indexCyclicAdjustment(int i, int length) {
85+
return i < length ? i : 0;
86+
}
8187
}

src/main/java/org/cicirello/permutations/distance/CyclicRTypeDistance.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools - A Java library for computation on permutations.
3-
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* JavaPermutationTools is free software: you can
66
* redistribute it and/or modify it under the terms of the GNU
@@ -61,11 +61,13 @@ public int distance(Permutation p1, Permutation p2) {
6161
int countNonSharedEdges = 0;
6262
int[] successors2 = new int[p2.length()];
6363
for (int i = 0; i < successors2.length; i++) {
64-
successors2[p2.get(i)] = p2.get((i + 1) % successors2.length);
64+
successors2[p2.get(i)] = p2.get(indexCyclicAdjustment(i + 1, successors2.length));
6565
}
6666

6767
for (int i = 0; i < successors2.length; i++) {
68-
if (p1.get((i + 1) % successors2.length) != successors2[p1.get(i)]) countNonSharedEdges++;
68+
if (p1.get(indexCyclicAdjustment(i + 1, successors2.length)) != successors2[p1.get(i)]) {
69+
countNonSharedEdges++;
70+
}
6971
}
7072
return countNonSharedEdges;
7173
}
@@ -75,4 +77,8 @@ public int max(int length) {
7577
if (length <= 2) return 0;
7678
return length;
7779
}
80+
81+
private int indexCyclicAdjustment(int i, int length) {
82+
return i < length ? i : 0;
83+
}
7884
}

0 commit comments

Comments
 (0)