UNPKG

cvm-lib

Version:

Estimate the number of distinct values in a set using the simple and space-efficient CVM algorithm

3 lines 1.8 kB
/*! @license cvm-lib | MIT | https://github.com/havelessbemore/cvm-lib */ var m=Object.defineProperty;var p=(e,t,r)=>t in e?m(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var a=(e,t,r)=>p(e,typeof t!="symbol"?t+"":t,r);function s(e){return typeof e=="number"&&e>0&&e<1}function n(e){return typeof e=="number"&&e>0}function o(e){return Number.isInteger(e)&&e>0}function c(e,t=.05,r=.01){if(!n(e))throw new RangeError("Invalid n");if(!s(t))throw new RangeError("Invalid epsilon");if(!s(r))throw new RangeError("Invalid delta");return Math.min(e,Math.ceil(Math.log2(e/r)/t**2))}var i=class{constructor(t){a(this,"_capacity");a(this,"_randomFn");a(this,"_rate");a(this,"_sampleRate");a(this,"_samples");if(this._capacity=1,this._rate=1,this._randomFn=Math.random,this._sampleRate=.5,typeof t=="number"){this.capacity=t,this._samples=new Set;return}this.capacity=t.capacity,this.randomFn=t.randomFn??this._randomFn,this.sampleRate=t.sampleRate??this._sampleRate,this._samples=t.storage??new Set}get capacity(){return this._capacity}set capacity(t){if(!o(t))throw new RangeError("Invalid capacity");this._capacity=t}get randomFn(){return this._randomFn}set randomFn(t){this._randomFn=t}get sampleRate(){return this._sampleRate}set sampleRate(t){if(!s(t))throw new RangeError("Invalid sample rate");this._sampleRate=t}get size(){return this._samples.size}add(t){if(this._randomFn()>=this._rate)return this._samples.delete(t),this;for(this._samples.add(t);this._samples.size>=this._capacity;){for(let r of this._samples)this._randomFn()>=this._sampleRate&&this._samples.delete(r);this._rate*=this._sampleRate}return this}clear(){this._rate=1,this._samples.clear()}estimate(){return this._samples.size/this._rate}};export{i as Estimator,c as calculateCapacity}; //# sourceMappingURL=index.js.map