Created
February 1, 2013 03:01
-
-
Save abramsm/4688828 to your computer and use it in GitHub Desktop.
Offering object to HyperLogLog++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean offer(Object o) | |
{ | |
long x = MurmurHash.hash64(o); | |
switch (format) | |
{ | |
case NORMAL: | |
// find first p bits of x | |
final long idx = x >>> (64 - p); | |
//Ignore the first p bits (the idx), and then find the number of leading zeros | |
//Push a 1 to where the bit string would have ended if we didnt just push the idx out of the way | |
//A one is always added to runLength for estimation calculation purposes | |
final int runLength = Long.numberOfLeadingZeros((x << this.p) | (1 << (this.p - 1))) + 1; | |
if (registerSet.get((int) idx) < runLength) | |
{ | |
registerSet.set((int) idx, runLength); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
case SPARSE: | |
//Call the sparse encoding scheme which attempts to stuff as much helpful data into 32 bits as possible | |
int k = encodeHash(x, p, sp); | |
//Put the encoded data into the temp set | |
boolean ret = tmpSet.add(k); | |
if (tmpSet.size() > SORT_THRESHOLD) | |
{ | |
mergeTempList(); | |
if (sparseSet.size() > MAX_THRESHOLD) | |
{ | |
convertToNormal(); | |
} | |
} | |
return ret; | |
default: | |
break; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment