Skip to content

Commit 7417b25

Browse files
committed
ReversalDistance.max
1 parent 08284e1 commit 7417b25

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public final class ReversalDistance implements NormalizedPermutationDistanceMeas
5252

5353
private byte[] dist;
5454
private final int PERM_LENGTH;
55-
private int maxd;
56-
55+
5756
/**
5857
* Construct the distance measure. Default handles permutations of length n=5.
5958
*/
@@ -71,7 +70,6 @@ public ReversalDistance(int n) {
7170
if (n > 12 || n < 0) throw new IllegalArgumentException("Requires 0 <= n <= 12.");
7271
PERM_LENGTH = n;
7372
int fact = 1;
74-
maxd = 0;
7573
for (int i = 2; i <= n; i++) fact *= i;
7674
dist = new byte[fact];
7775
final byte BYTE_MAX = 0x7f;
@@ -83,7 +81,6 @@ public ReversalDistance(int n) {
8381
int v = p.toInteger();
8482
p.reverse(i,j);
8583
dist[v] = 1;
86-
maxd = 1;
8784
}
8885
}
8986
int visited = n * (n-1) / 2 + 1;
@@ -99,7 +96,7 @@ public ReversalDistance(int n) {
9996
int v = p.toInteger();
10097
p.reverse(i,j);
10198
if (v > 0 && dist[v]==BYTE_MAX) {
102-
maxd = dist[v] = (byte)(d + 1);
99+
dist[v] = (byte)(d + 1);
103100
visited++;
104101
}
105102
}
@@ -129,19 +126,13 @@ public int distance(Permutation p1, Permutation p2) {
129126
return dist[new Permutation(r2).toInteger()];
130127
}
131128

132-
/**
133-
* {@inheritDoc}
134-
*
135-
* @throws IllegalArgumentException if length is not equal to the
136-
* the permutation length for which this was configured at time
137-
* of construction.
138-
*/
139129
@Override
140130
public int max(int length) {
141-
if (PERM_LENGTH != length) {
142-
throw new IllegalArgumentException("This distance measurer was not configured for length: " + length);
131+
if (length > 1) {
132+
return length - 1;
133+
} else {
134+
return 0;
143135
}
144-
return maxd;
145136
}
146137

147138
}

src/test/java/org/cicirello/permutations/distance/PermutationDistanceMaxTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public void testReversalDistance() {
4343
assertEquals(2, d.max(3));
4444
d = new ReversalDistance(4);
4545
assertEquals(3, d.max(4));
46-
47-
final ReversalDistance df = d;
48-
IllegalArgumentException thrown = assertThrows(
49-
IllegalArgumentException.class,
50-
() -> df.max(7)
51-
);
5246
}
5347

5448
@Test

0 commit comments

Comments
 (0)