This implementation varies from the algorithm described in the paper by redefining nonzero
register values by the mapping r -> r + 4*p - 8. Since the maximum register value in the
paper is given by 4*w+3 = 263-4*p with w = 65-p, the maximum register value in
this implementation is given by (263 - 4*p) + 4*p - 8 = 255 which is independent of p.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA distinct count estimator for UltraLogLog. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final UltraLogLog.EstimatorThe default estimator.static final UltraLogLog.EstimatorBias-reduced maximum-likelihood estimator.static final UltraLogLog.EstimatorOptimal further generalized remaining area (FGRA) estimator. -
Method Summary
Modifier and TypeMethodDescriptionadd(long hashValue) Adds a new element represented by a 64-bit hash value to this sketch.add(long hashValue, StateChangeObserver stateChangeObserver) Adds a new element represented by a 64-bit hash value to this sketch and passes, if the internal state has changed, decrements of the state change probability to the givenStateChangeObserver.add(UltraLogLog other) Adds another sketch.addToken(int token) Adds a new element represented by a 32-bit token obtained fromcomputeToken(long).addToken(int token, StateChangeObserver stateChangeObserver) Adds a new element, represented by a 32-bit token obtained fromcomputeToken(long), to this sketch and passes, if the internal state has changed, decrements of the state change probability to the givenStateChangeObserver.static intcomputeToken(long hashValue) Computes a token from a given 64-bit hash value.copy()Creates a copy of this sketch.static UltraLogLogcreate(int p) Creates an emptyUltraLogLogsketch with given precision.downsize(int p) Returns a downsized copy of this sketch with a precision that is not larger than the given precision parameter.doubleReturns an estimate of the number of distinct elements added to this sketch.doublegetDistinctCountEstimate(UltraLogLog.Estimator estimator) Returns an estimate of the number of distinct elements added to this sketch using the given estimator.intgetP()Returns the precision parameter of this sketch.byte[]getState()Returns a reference to the internal state of this sketch.doubleReturns the probability of an internal state change when a new distinct element is added.booleanisEmpty()Returnstrueif the sketch is empty, corresponding to the initial state.static UltraLogLogmerge(UltraLogLog sketch1, UltraLogLog sketch2) Merges twoUltraLogLogsketches into a new sketch.reset()Resets this sketch to its initial state representing an empty set.static UltraLogLogwrap(byte[] state) Returns aUltraLogLogsketch whose state is kept in the given byte array.
-
Field Details
-
MAXIMUM_LIKELIHOOD_ESTIMATOR
Bias-reduced maximum-likelihood estimator. -
OPTIMAL_FGRA_ESTIMATOR
Optimal further generalized remaining area (FGRA) estimator. -
DEFAULT_ESTIMATOR
The default estimator.
-
-
Method Details
-
create
Creates an emptyUltraLogLogsketch with given precision.The precision parameter
pmust be in the range{3, 4, 5, ..., 25, 26}. It also defines the size of the internal state, which is a byte array of length2^p.- Parameters:
p- the precision parameter- Returns:
- the new sketch
- Throws:
IllegalArgumentException- if the precision parameter is invalid
-
wrap
Returns aUltraLogLogsketch whose state is kept in the given byte array.The array must have a length that is a power of two of a valid precision parameter. If the state is not valid (it was not retrieved using
getState()) the behavior will be undefined.- Parameters:
state- the state- Returns:
- the new sketch
- Throws:
NullPointerException- if the passed array is nullIllegalArgumentException- if the passed array has invalid length
-
copy
Creates a copy of this sketch.- Returns:
- the copy
-
downsize
Returns a downsized copy of this sketch with a precision that is not larger than the given precision parameter.- Parameters:
p- the precision parameter used for downsizing- Returns:
- the downsized copy
- Throws:
IllegalArgumentException- if the precision parameter is invalid
-
merge
Merges twoUltraLogLogsketches into a new sketch.The precision of the merged sketch is given by the smaller precision of both sketches.
- Parameters:
sketch1- the first sketchsketch2- the second sketch- Returns:
- the merged sketch
- Throws:
NullPointerException- if one of both arguments is null
-
getState
public byte[] getState()Returns a reference to the internal state of this sketch.The returned state is never
null.- Returns:
- the internal state of this sketch
-
getP
public int getP()Returns the precision parameter of this sketch.- Returns:
- the precision parameter
-
add
Adds a new element represented by a 64-bit hash value to this sketch.In order to get good estimates, it is important that the hash value is calculated using a high-quality hash algorithm.
- Parameters:
hashValue- a 64-bit hash value- Returns:
- this sketch
-
addToken
Adds a new element represented by a 32-bit token obtained fromcomputeToken(long).- Parameters:
token- a 32-bit hash token- Returns:
- this sketch
-
computeToken
public static int computeToken(long hashValue) Computes a token from a given 64-bit hash value.Instead of updating the sketch with the hash value using the
add(long)method, it can alternatively be updated with the corresponding 32-bit token using theaddToken(int)method.addToken(computeToken(hash))is equivalent toadd(hash)Tokens can be temporarily collected using for example an
int[] arrayand added later usingaddToken(int)into the sketch resulting exactly in the same final state. This can be used to realize a sparse mode, where the sketch is created only when there are enough tokens to justify the memory allocation. It is sufficient to store only distinct tokens. Deduplication does not result in any loss of information with respect to distinct count estimation. CompareDistinctCountUtil.deduplicateTokens(int[], int, int).- Parameters:
hashValue- the 64-bit hash value- Returns:
- the 32-bit token
-
add
Adds a new element represented by a 64-bit hash value to this sketch and passes, if the internal state has changed, decrements of the state change probability to the givenStateChangeObserver.In order to get good estimates, it is important that the hash value is calculated using a high-quality hash algorithm.
- Parameters:
hashValue- a 64-bit hash valuestateChangeObserver- a state change observer- Returns:
- this sketch
-
addToken
Adds a new element, represented by a 32-bit token obtained fromcomputeToken(long), to this sketch and passes, if the internal state has changed, decrements of the state change probability to the givenStateChangeObserver.addToken(computeToken(hash), stateChangeObserver)is equivalent toadd(hash, stateChangeObserver)- Parameters:
token- a 32-bit hash tokenstateChangeObserver- a state change observer- Returns:
- this sketch
-
add
Adds another sketch.The precision parameter of the added sketch must not be smaller than the precision parameter of this sketch. Otherwise, an
IllegalArgumentExceptionwill be thrown.- Parameters:
other- the other sketch- Returns:
- this sketch
- Throws:
NullPointerException- if the argument is null
-
getDistinctCountEstimate
public double getDistinctCountEstimate()Returns an estimate of the number of distinct elements added to this sketch.- Returns:
- estimated number of distinct elements
-
getDistinctCountEstimate
Returns an estimate of the number of distinct elements added to this sketch using the given estimator.- Parameters:
estimator- the estimator- Returns:
- estimated number of distinct elements
-
getStateChangeProbability
public double getStateChangeProbability()Returns the probability of an internal state change when a new distinct element is added.- Returns:
- the state change probability
-
isEmpty
public boolean isEmpty()Returnstrueif the sketch is empty, corresponding to the initial state.- Returns:
trueif the sketch is empty
-
reset
Resets this sketch to its initial state representing an empty set.- Returns:
- this sketch
-