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}
0 commit comments