Skip to content

Commit

Permalink
Merge pull request #323 from cicirello/refactor
Browse files Browse the repository at this point in the history
Refactored KendallTauDistance, WeightedKendallTauDistance, CyclicEdgeDistance, CyclicRTypeDistance
  • Loading branch information
cicirello authored Mar 5, 2023
2 parents 3c12922 + 4b7d664 commit dba2610
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - 2023-03-03
## [Unreleased] - 2023-03-05

### Added

### Changed
* Minor code improvements within Permutation and PermutationIterator classes.
* Minor code improvements to Permutation and PermutationIterator classes.
* Minor code improvements to KendallTauDistance and WeightedKendallTauDistance.
* Minor code improvements to CyclicEdgeDistance and CyclicRTypeDistance.

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
*
* <p><a href="https://doi.org/10.21105/joss.00950"><img
* src="http://joss.theoj.org/papers/10.21105/joss.00950/status.svg" alt="DOI:10.21105/joss.00950"
* height="20" width="168"></a> <a href="https://central.sonatype.com/search?namespace=org.cicirello&q=jpt"><img
* height="20" width="168"></a> <a
* href="https://central.sonatype.com/search?namespace=org.cicirello&q=jpt"><img
* src="https://img.shields.io/maven-central/v/org.cicirello/jpt.svg?logo=apachemaven" alt="Maven
* Central" height="20" width="153"></a> <a
* href="https://github.com/cicirello/JavaPermutationTools/releases"><img
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools - A Java library for computation on permutations.
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* JavaPermutationTools is free software: you can
* redistribute it and/or modify it under the terms of the GNU
Expand Down Expand Up @@ -61,12 +61,14 @@ public int distance(Permutation p1, Permutation p2) {
int countNonSharedEdges = 0;
int[] successors2 = new int[p2.length()];
for (int i = 0; i < successors2.length; i++) {
successors2[p2.get(i)] = p2.get((i + 1) % successors2.length);
successors2[p2.get(i)] = p2.get(indexCyclicAdjustment(i + 1, successors2.length));
}

for (int i = 0; i < successors2.length; i++) {
if (p1.get((i + 1) % successors2.length) != successors2[p1.get(i)]
&& p1.get(i) != successors2[p1.get((i + 1) % successors2.length)]) countNonSharedEdges++;
int j = indexCyclicAdjustment(i + 1, successors2.length);
if (p1.get(j) != successors2[p1.get(i)] && p1.get(i) != successors2[p1.get(j)]) {
countNonSharedEdges++;
}
}

return countNonSharedEdges;
Expand All @@ -78,4 +80,8 @@ public int max(int length) {
if (length == 4) return 2;
return length;
}

private int indexCyclicAdjustment(int i, int length) {
return i < length ? i : 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools - A Java library for computation on permutations.
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* JavaPermutationTools is free software: you can
* redistribute it and/or modify it under the terms of the GNU
Expand Down Expand Up @@ -61,11 +61,13 @@ public int distance(Permutation p1, Permutation p2) {
int countNonSharedEdges = 0;
int[] successors2 = new int[p2.length()];
for (int i = 0; i < successors2.length; i++) {
successors2[p2.get(i)] = p2.get((i + 1) % successors2.length);
successors2[p2.get(i)] = p2.get(indexCyclicAdjustment(i + 1, successors2.length));
}

for (int i = 0; i < successors2.length; i++) {
if (p1.get((i + 1) % successors2.length) != successors2[p1.get(i)]) countNonSharedEdges++;
if (p1.get(indexCyclicAdjustment(i + 1, successors2.length)) != successors2[p1.get(i)]) {
countNonSharedEdges++;
}
}
return countNonSharedEdges;
}
Expand All @@ -75,4 +77,8 @@ public int max(int length) {
if (length <= 2) return 0;
return length;
}

private int indexCyclicAdjustment(int i, int length) {
return i < length ? i : 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools - A Java library for computation on permutations.
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* JavaPermutationTools is free software: you can
* redistribute it and/or modify it under the terms of the GNU
Expand Down Expand Up @@ -131,7 +131,7 @@ public double distancef(Permutation p1, Permutation p2) {
if (combined <= changeCost) {
return (length-1)*combined;
}
if (length % 2 == 0) {
if ((length & 1) == 0) {
double m1 = (length-1)*combined;
double m2 = length*changeCost;
double m3 = combined + (length-2)*changeCost;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools - A Java library for computation on permutations.
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* JavaPermutationTools is free software: you can
* redistribute it and/or modify it under the terms of the GNU
Expand Down Expand Up @@ -112,16 +112,8 @@ private int merge(int[] array, int first, int midPlus, int lastPlus) {
k++;
}
}
while (i < left.length) {
array[k] = left[i];
i++;
k++;
}
while (j < right.length) {
array[k] = right[j];
j++;
k++;
}
System.arraycopy(left, i, array, k, left.length - i);
System.arraycopy(right, j, array, k, right.length - j);
return count;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JavaPermutationTools: A Java library for computation on permutations and sequences.
* Copyright (C) 2018-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
* Copyright (C) 2018-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
*
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
*
Expand Down Expand Up @@ -149,16 +149,8 @@ private double merge(int[] array, double[] w, int first, int midPlus, int lastPl
k++;
}
}
while (i < left.length) {
array[k] = left[i];
i++;
k++;
}
while (j < right.length) {
array[k] = right[j];
j++;
k++;
}
System.arraycopy(left, i, array, k, left.length - i);
System.arraycopy(right, j, array, k, right.length - j);
return weightedCount;
}
}

0 comments on commit dba2610

Please sign in to comment.