forked from I2P_Developers/i2p.i2p
speed up XORComparators
This commit is contained in:
@ -2,7 +2,6 @@ package net.i2p.kademlia;
|
|||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import net.i2p.data.DataHelper;
|
|
||||||
import net.i2p.data.SimpleDataStructure;
|
import net.i2p.data.SimpleDataStructure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,8 +20,20 @@ class XORComparator<T extends SimpleDataStructure> implements Comparator<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int compare(T lhs, T rhs) {
|
public int compare(T lhs, T rhs) {
|
||||||
byte lhsDelta[] = DataHelper.xor(lhs.getData(), _base);
|
// same as the following but byte-by-byte for efficiency
|
||||||
byte rhsDelta[] = DataHelper.xor(rhs.getData(), _base);
|
//byte lhsDelta[] = DataHelper.xor(lhs.getData(), _base);
|
||||||
return DataHelper.compareTo(lhsDelta, rhsDelta);
|
//byte rhsDelta[] = DataHelper.xor(rhs.getData(), _base);
|
||||||
|
//return DataHelper.compareTo(lhsDelta, rhsDelta);
|
||||||
|
byte lhsb[] = lhs.getData();
|
||||||
|
byte rhsb[] = rhs.getData();
|
||||||
|
for (int i = 0; i < _base.length; i++) {
|
||||||
|
int ld = (lhsb[i] ^ _base[i]) & 0xff;
|
||||||
|
int rd = (rhsb[i] ^ _base[i]) & 0xff;
|
||||||
|
if (ld < rd)
|
||||||
|
return -1;
|
||||||
|
if (ld > rd)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,32 +2,35 @@ package net.i2p.router.networkdb.kademlia;
|
|||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import net.i2p.data.DataHelper;
|
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Help sort Hashes in relation to a base key using the XOR metric.
|
* Help sort Hashes in relation to a base key using the XOR metric.
|
||||||
* Warning - not thread safe.
|
|
||||||
*/
|
*/
|
||||||
class XORComparator implements Comparator<Hash> {
|
class XORComparator implements Comparator<Hash> {
|
||||||
private final byte[] _base;
|
private final byte[] _base;
|
||||||
private final byte[] _lx, _rx;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param target key to compare distances with
|
* @param target key to compare distances with
|
||||||
*/
|
*/
|
||||||
public XORComparator(Hash target) {
|
public XORComparator(Hash target) {
|
||||||
_base = target.getData();
|
_base = target.getData();
|
||||||
_lx = new byte[Hash.HASH_LENGTH];
|
|
||||||
_rx = new byte[Hash.HASH_LENGTH];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getData() of args must be non-null
|
* getData() of args must be non-null
|
||||||
*/
|
*/
|
||||||
public int compare(Hash lhs, Hash rhs) {
|
public int compare(Hash lhs, Hash rhs) {
|
||||||
DataHelper.xor(lhs.getData(), 0, _base, 0, _lx, 0, Hash.HASH_LENGTH);
|
byte lhsb[] = lhs.getData();
|
||||||
DataHelper.xor(rhs.getData(), 0, _base, 0, _rx, 0, Hash.HASH_LENGTH);
|
byte rhsb[] = rhs.getData();
|
||||||
return DataHelper.compareTo(_lx, _rx);
|
for (int i = 0; i < _base.length; i++) {
|
||||||
|
int ld = (lhsb[i] ^ _base[i]) & 0xff;
|
||||||
|
int rd = (rhsb[i] ^ _base[i]) & 0xff;
|
||||||
|
if (ld < rd)
|
||||||
|
return -1;
|
||||||
|
if (ld > rd)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user