Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
*/
@NotThreadSafe
public class HyperLogLog {
private final byte[] buckets;
protected final byte[] buckets;

// The current sum of 1 / (1L << buckets[i]). Updated as new items are added and used for
// estimation
private double currentSum;
private int nonZeroBuckets = 0;
protected double currentSum;
protected int nonZeroBuckets = 0;

public HyperLogLog(int numberOfBuckets) {
Preconditions.checkArgument(
Expand Down Expand Up @@ -66,12 +66,13 @@ public HyperLogLog(int[] buckets) {
}
}

public void add(long value) {
/**
* @return true if the buckets are updated
*/
public boolean add(long value) {
BucketAndHash bucketAndHash = BucketAndHash.fromHash(computeHash(value), buckets.length);
int bucket = bucketAndHash.getBucket();

int lowestBitPosition = Long.numberOfTrailingZeros(bucketAndHash.getHash()) + 1;

int previous = buckets[bucket];

if (previous == 0) {
Expand All @@ -83,7 +84,9 @@ public void add(long value) {
currentSum += 1.0 / (1L << lowestBitPosition);

buckets[bucket] = (byte) lowestBitPosition;
return true;
}
return false;
}

public long estimate() {
Expand Down