UNPKG

cvm-lib

Version:

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

3 lines 2.23 kB
/*! @license cvm-lib | MIT | https://github.com/havelessbemore/cvm-lib */ "use strict";var n=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var d=(e,t,r)=>t in e?n(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var f=(e,t)=>{for(var r in t)n(e,r,{get:t[r],enumerable:!0})},y=(e,t,r,m)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of l(t))!_.call(e,s)&&s!==r&&n(e,s,{get:()=>t[s],enumerable:!(m=u(t,s))||m.enumerable});return e};var b=e=>y(n({},"__esModule",{value:!0}),e);var a=(e,t,r)=>d(e,typeof t!="symbol"?t+"":t,r);var w={};f(w,{Estimator:()=>o,calculateCapacity:()=>h});module.exports=b(w);function i(e){return typeof e=="number"&&e>0&&e<1}function p(e){return typeof e=="number"&&e>0}function c(e){return Number.isInteger(e)&&e>0}function h(e,t=.05,r=.01){if(!p(e))throw new RangeError("Invalid n");if(!i(t))throw new RangeError("Invalid epsilon");if(!i(r))throw new RangeError("Invalid delta");return Math.min(e,Math.ceil(Math.log2(e/r)/t**2))}var o=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(!c(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(!i(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}}; //# sourceMappingURL=index.cjs.map