|
1 | 1 | /* |
2 | 2 | * 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/>. |
4 | 4 | * |
5 | 5 | * This file is part of JavaPermutationTools (https://jpt.cicirello.org/). |
6 | 6 | * |
|
22 | 22 | package org.cicirello.sequences.distance; |
23 | 23 |
|
24 | 24 | import java.util.Arrays; |
| 25 | +import java.util.Iterator; |
25 | 26 | import java.util.List; |
26 | 27 |
|
27 | 28 | /** |
@@ -201,14 +202,53 @@ public int relabel(double[] s1, double[] s2, int[][] relabeling) { |
201 | 202 | } |
202 | 203 |
|
203 | 204 | @Override |
204 | | - @SuppressWarnings("unchecked") |
205 | 205 | 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; |
207 | 227 | } |
208 | 228 |
|
209 | 229 | @Override |
210 | | - @SuppressWarnings("unchecked") |
211 | 230 | 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; |
213 | 253 | } |
214 | 254 | } |
0 commit comments