Skip to content

Commit 4ed9802

Browse files
committed
refactor RelabelBySorting
1 parent 6c7a7a3 commit 4ed9802

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/main/java/org/cicirello/sequences/distance/RelabelBySorting.java

Lines changed: 45 additions & 5 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 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -22,6 +22,7 @@
2222
package org.cicirello.sequences.distance;
2323

2424
import java.util.Arrays;
25+
import java.util.Iterator;
2526
import java.util.List;
2627

2728
/**
@@ -201,14 +202,53 @@ public int relabel(double[] s1, double[] s2, int[][] relabeling) {
201202
}
202203

203204
@Override
204-
@SuppressWarnings("unchecked")
205205
public int relabel(Object[] s1, Object[] s2, int[][] relabeling) {
206-
return internalRelabel((Comparable[]) s1, (Comparable[]) s2, relabeling);
206+
@SuppressWarnings("unchecked")
207+
Comparable[] comp1 = (Comparable[]) s1;
208+
@SuppressWarnings("unchecked")
209+
Comparable[] comp2 = (Comparable[]) s2;
210+
Comparable[] c1 = comp1.clone();
211+
Arrays.sort(c1);
212+
int[] labels = new int[c1.length];
213+
int current = labels[0] = 0;
214+
for (int i = 1; i < labels.length; i++) {
215+
if (c1[i] != c1[i - 1]) current++;
216+
labels[i] = current;
217+
}
218+
219+
for (int i = 0; i < relabeling.length; i++) {
220+
int j = Arrays.binarySearch(c1, comp1[i]);
221+
relabeling[i][0] = labels[j];
222+
j = Arrays.binarySearch(c1, comp2[i]);
223+
validateElementIndex(j);
224+
relabeling[i][1] = labels[j];
225+
}
226+
return current + 1;
207227
}
208228

209229
@Override
210-
@SuppressWarnings("unchecked")
211230
public <T> int relabel(List<T> s1, List<T> s2, int[][] relabeling) {
212-
return internalRelabel((List<Comparable>) s1, (List<Comparable>) s2, relabeling);
231+
@SuppressWarnings("unchecked")
232+
List<Comparable> comp1 = (List<Comparable>) s1;
233+
@SuppressWarnings("unchecked")
234+
List<Comparable> comp2 = (List<Comparable>) s2;
235+
Comparable[] c1 = comp1.toArray(new Comparable[comp1.size()]);
236+
Arrays.sort(c1);
237+
int[] labels = new int[c1.length];
238+
int current = labels[0] = 0;
239+
for (int i = 1; i < labels.length; i++) {
240+
if (c1[i] != c1[i - 1]) current++;
241+
labels[i] = current;
242+
}
243+
Iterator<Comparable> iter1 = comp1.iterator();
244+
Iterator<Comparable> iter2 = comp2.iterator();
245+
for (int i = 0; i < relabeling.length; i++) {
246+
int j = Arrays.binarySearch(c1, iter1.next());
247+
relabeling[i][0] = labels[j];
248+
j = Arrays.binarySearch(c1, iter2.next());
249+
validateElementIndex(j);
250+
relabeling[i][1] = labels[j];
251+
}
252+
return current + 1;
213253
}
214254
}

0 commit comments

Comments
 (0)