Skip to content

Commit dba2610

Browse files
authored
Merge pull request #323 from cicirello/refactor
Refactored KendallTauDistance, WeightedKendallTauDistance, CyclicEdgeDistance, CyclicRTypeDistance
2 parents 3c12922 + 4b7d664 commit dba2610

File tree

7 files changed

+33
-34
lines changed

7 files changed

+33
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2023-03-03
7+
## [Unreleased] - 2023-03-05
88

99
### Added
1010

1111
### Changed
12-
* Minor code improvements within Permutation and PermutationIterator classes.
12+
* Minor code improvements to Permutation and PermutationIterator classes.
13+
* Minor code improvements to KendallTauDistance and WeightedKendallTauDistance.
14+
* Minor code improvements to CyclicEdgeDistance and CyclicRTypeDistance.
1315

1416
### Deprecated
1517

src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
*
3030
* <p><a href="https://doi.org/10.21105/joss.00950"><img
3131
* src="http://joss.theoj.org/papers/10.21105/joss.00950/status.svg" alt="DOI:10.21105/joss.00950"
32-
* height="20" width="168"></a> <a href="https://central.sonatype.com/search?namespace=org.cicirello&q=jpt"><img
32+
* height="20" width="168"></a> <a
33+
* href="https://central.sonatype.com/search?namespace=org.cicirello&q=jpt"><img
3334
* src="https://img.shields.io/maven-central/v/org.cicirello/jpt.svg?logo=apachemaven" alt="Maven
3435
* Central" height="20" width="153"></a> <a
3536
* href="https://github.com/cicirello/JavaPermutationTools/releases"><img

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
}

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

Lines changed: 2 additions & 2 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
@@ -131,7 +131,7 @@ public double distancef(Permutation p1, Permutation p2) {
131131
if (combined <= changeCost) {
132132
return (length-1)*combined;
133133
}
134-
if (length % 2 == 0) {
134+
if ((length & 1) == 0) {
135135
double m1 = (length-1)*combined;
136136
double m2 = length*changeCost;
137137
double m3 = combined + (length-2)*changeCost;

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

Lines changed: 3 additions & 11 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
@@ -112,16 +112,8 @@ private int merge(int[] array, int first, int midPlus, int lastPlus) {
112112
k++;
113113
}
114114
}
115-
while (i < left.length) {
116-
array[k] = left[i];
117-
i++;
118-
k++;
119-
}
120-
while (j < right.length) {
121-
array[k] = right[j];
122-
j++;
123-
k++;
124-
}
115+
System.arraycopy(left, i, array, k, left.length - i);
116+
System.arraycopy(right, j, array, k, right.length - j);
125117
return count;
126118
}
127119
}

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

Lines changed: 3 additions & 11 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 (C) 2018-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright (C) 2018-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -149,16 +149,8 @@ private double merge(int[] array, double[] w, int first, int midPlus, int lastPl
149149
k++;
150150
}
151151
}
152-
while (i < left.length) {
153-
array[k] = left[i];
154-
i++;
155-
k++;
156-
}
157-
while (j < right.length) {
158-
array[k] = right[j];
159-
j++;
160-
k++;
161-
}
152+
System.arraycopy(left, i, array, k, left.length - i);
153+
System.arraycopy(right, j, array, k, right.length - j);
162154
return weightedCount;
163155
}
164156
}

0 commit comments

Comments
 (0)