forked from I2P_Developers/i2p.i2p
- Add kad lib, from i2p.zzz.kademlia branch (without the history),
which is a rewrite of the netdb kad - Drop now-unused SHA1Comparator - Efficiency tweak to NodeInfoComparator
This commit is contained in:
27
apps/i2psnark/java/src/net/i2p/kademlia/XORComparator.java
Normal file
27
apps/i2psnark/java/src/net/i2p/kademlia/XORComparator.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package net.i2p.kademlia;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.SimpleDataStructure;
|
||||
|
||||
/**
|
||||
* Help sort Hashes in relation to a base key using the XOR metric
|
||||
*
|
||||
*/
|
||||
class XORComparator<T extends SimpleDataStructure> implements Comparator<T> {
|
||||
private final byte[] _base;
|
||||
|
||||
/**
|
||||
* @param target key to compare distances with
|
||||
*/
|
||||
public XORComparator(T target) {
|
||||
_base = target.getData();
|
||||
}
|
||||
|
||||
public int compare(T lhs, T rhs) {
|
||||
byte lhsDelta[] = DataHelper.xor(lhs.getData(), _base);
|
||||
byte rhsDelta[] = DataHelper.xor(rhs.getData(), _base);
|
||||
return DataHelper.compareTo(lhsDelta, rhsDelta);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user