/// /** * Implement this interface to support the use of different hash functions. Note that * your backend will need to be registered using `HasherFactory.register(..)` before * it can be used. */ interface Hasher { /** * Generate hash of the provided string. */ hash(val: string): V; /** * Returns the number of consecutive zeros in the provided hash value. * @param hashVal The hash value as computed by the `hash(..)` method. */ runLength(hashVal: V): number; /** * Determine the register index for a given hash value. * @param hashVal The hash value as computed by the `hash(..)` method. */ regIdx(hashVal: V): number; /** * Returns the length, in bits, of hash values generated by this `Hasher`. */ readonly hashLen: number; /** * The unique name of this `Hasher` implementation. Used to ensure counters * are compatible before merging, as well as to instantiate the proper backend * when deserializing. */ readonly hasherId: string; /** Maximum number of register bits supported by a `Hasher` implementation */ readonly maxPrecision: number; } interface MultiSetCounter { add(val: string): void; count(): number; merge(other: MultiSetCounter): MultiSetCounter; precision(): number; serialize(): Buffer; } type Options = { /** * The name of the hasher implementation to use. */ hasherId?: string; /** * Number of hash bits used to determine register index. This should be * `log2(r)` where `r` is the number of registers. **/ precision?: number; /** * Whether to correct for 'systematic multiplicative bias' resulting from * hash collisions. See the `count()` method for details (enabled by default) */ collisionAdjustment?: boolean; /** * Whether to make adjustments when the observed cardinality is more or less than * expected. See the counter `count()` method for details. (enabled by default) */ boundAdjustments?: boolean; }; /** * A basic implementation of the 'HyperLogLog' datastructure which is useful for * estimating the number of distinct elements in a multiset. */ declare class HyperLogLog implements MultiSetCounter { protected hasher: Hasher; protected registers: Uint8Array; protected opts: Options; /** * @param hasher The hashing backend to use. Any instance implementing the `Hasher` trait may be used. * @param options */ constructor(options?: Options); /** * @returns The number of bits used for the register index (e.g. 8-bits = 256 registers) */ precision(): number; /** * @param val Inserts a value into this set for counting. Duplicate values will only be * counted once in the resulting estimate. */ add(val: string): void; /** * Estimates the cardinality of all elements added to this set. This implementation * performs various adjustments as recommended by the HyperLogLog wikipedia entry: * * * When the estimate is low relative to the number of configured registers, 'Linear * Counting' is used instead of the standard method of estimation. A corresponding * adjustment is made when the cardinality is high relative to the size of the hash space. * These upper & lower bound adjustments may be controlled using the `boundAdjustments` * option (enabled by default). * * An additional correction is performed to compensate for overestimates resulting from hash * collisions. This correction can be controlled by the `collisionAdjustment` option * (enabled by default) */ count(): number; /** * Creates a new counter by merging the state of this with another - useful for distributed scenarios. * @returns A new `HyperLogLog` counter instance whose cardinality estimate reflects the * combined sets of the merged counters. The new counter inherits the `options` * of this instance (lhs). */ merge(other: HyperLogLog): HyperLogLog; /** * @returns A `Buffer` containing the state of this counter. Useful for transmitting * to another host for merging. */ serialize(): Buffer; /** * @returns A newly instantiated counter which mirrors a previously serialized one. */ static deserialize(rawBuf: Buffer): HyperLogLog; } export { Hasher, HyperLogLog, MultiSetCounter, Options };