* XORComparator: Reduce object churn (Ticket #658)

This commit is contained in:
zzz
2012-07-15 15:04:58 +00:00
parent a45688867d
commit 69cdcc8226
2 changed files with 14 additions and 5 deletions

View File

@@ -6,22 +6,28 @@ import net.i2p.data.DataHelper;
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> {
private final byte[] _base;
private final byte[] _lx, _rx;
/**
* @param target key to compare distances with
*/
public XORComparator(Hash target) {
_base = target.getData();
_lx = new byte[Hash.HASH_LENGTH];
_rx = new byte[Hash.HASH_LENGTH];
}
/**
* getData() of args must be non-null
*/
public int compare(Hash lhs, Hash rhs) {
byte lhsDelta[] = DataHelper.xor(lhs.getData(), _base);
byte rhsDelta[] = DataHelper.xor(rhs.getData(), _base);
return DataHelper.compareTo(lhsDelta, rhsDelta);
DataHelper.xor(lhs.getData(), 0, _base, 0, _lx, 0, Hash.HASH_LENGTH);
DataHelper.xor(rhs.getData(), 0, _base, 0, _rx, 0, Hash.HASH_LENGTH);
return DataHelper.compareTo(_lx, _rx);
}
}