mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Add Yen’s K-shortest loopless paths with tests [GRAPHS] (#6773)
* Add Yen’s K-shortest loopless paths with tests and index update * style: fix Checkstyle in Yens algorithm and tests * fix: resolve SpotBugs in Yens algorithm * fix (PMD): rename short variables in the code * (pmd): code fixes * fix(bloomfilter): hash arrays by content to satisfy array membership tests * style(pmd): fix EmptyControlStatement in validate() by returning early when src==dst * style(pmd): remove unnecessary return in validate()
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.thealgorithms.datastructures.bloomfilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.BitSet;
|
||||
|
||||
/**
|
||||
@@ -115,7 +116,7 @@ public class BloomFilter<T> {
|
||||
* @return the computed hash value
|
||||
*/
|
||||
public int compute(T key) {
|
||||
return index * asciiString(String.valueOf(key));
|
||||
return index * contentHash(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,5 +136,31 @@ public class BloomFilter<T> {
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a content-based hash for arrays; falls back to ASCII-sum of String value otherwise.
|
||||
*/
|
||||
private int contentHash(Object key) {
|
||||
if (key instanceof int[]) {
|
||||
return Arrays.hashCode((int[]) key);
|
||||
} else if (key instanceof long[]) {
|
||||
return Arrays.hashCode((long[]) key);
|
||||
} else if (key instanceof byte[]) {
|
||||
return Arrays.hashCode((byte[]) key);
|
||||
} else if (key instanceof short[]) {
|
||||
return Arrays.hashCode((short[]) key);
|
||||
} else if (key instanceof char[]) {
|
||||
return Arrays.hashCode((char[]) key);
|
||||
} else if (key instanceof boolean[]) {
|
||||
return Arrays.hashCode((boolean[]) key);
|
||||
} else if (key instanceof float[]) {
|
||||
return Arrays.hashCode((float[]) key);
|
||||
} else if (key instanceof double[]) {
|
||||
return Arrays.hashCode((double[]) key);
|
||||
} else if (key instanceof Object[]) {
|
||||
return Arrays.deepHashCode((Object[]) key);
|
||||
}
|
||||
return asciiString(String.valueOf(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user