Skip to content

HyperLogLog module

Francesco Saverio Castellano edited this page Mar 21, 2020 · 5 revisions

This module is based on the cpp-HyperLogLog library which implements the HyperLogLog algorithm.

HyperLogLog.addToNew(integer num, string value)
Creates a new HyperLogLog structure with num bit width and adds value to it.
Returns an array of bytes that represents the state of the registers and must be used for further operations.  
 
HyperLogLog.add(array state, string value)
Restores the HyperLogLog structure from state and adds to it the passed value.
Returns the new state.  
 
HyperLogLog.merge(array state1, array state2)
Creates a new HyperLogLog structure by merging the passed states and returns its state.  
 
HyperLogLog.getCardinality(array state)
Restores the HyperLogLog structure and returns the current estimated cardinality value.
 
The following example shows how to use HyperLogLog to count occurrences of different strings:

var hll = HyperLogLog.addToNew(10, "string #1");
hll = HyperLogLog.add(hll,  "string #2")
hll = HyperLogLog.add(hll,  "string #3")
hll = HyperLogLog.add(hll,  "string #4")
hll = HyperLogLog.add(hll,  "string #5")
hll = HyperLogLog.add(hll,  "string #6")
hll = HyperLogLog.add(hll,  "string #7")
hll = HyperLogLog.add(hll,  "string #8")
hll = HyperLogLog.add(hll,  "string #9")
hll = HyperLogLog.add(hll,  "string #10");

var card = HyperLogLog.getCardinality(hll);

print("the cardinality after adding 10 different string values is: " + card);
Clone this wiki locally