-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjs-hll-1.0.0.min.js
17 lines (17 loc) · 7.69 KB
/
js-hll-1.0.0.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* Copyright 2013 Aggregate Knowledge, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if(hll===undefined){var hll={version:"1.0.0"}}(function(){var c=5,m=13;var b=0,g=1;hll.HLL=function(C,x){var D=this;if(arguments.length<1){C=m}if(arguments.length<2){x=c}if((C<4)||(C>24)){throw new Error("Register width must be between 4 and 24 inclusive (log2m = "+C+").")}if((x<1)||(x>5)){throw new Error("Register width must be between 1 and 5 inclusive (registerWidth = "+x+").")}D.log2m=C;D.m=1<<C;D.registerWidth=x;D.registers=[];for(var t=D.m-1;t>=0;t--){D.registers.push(0)}var B=((1<<D.registerWidth)-1)>>>0;var s=((1<<C)-1)>>>0;var w=B-1,y=w+C,r=Math.pow(2,y);var u=(r/30),v=5*D.registers.length/2;var z=e(D.m);function A(F){var E=hll.util.leastSignificantBit(F[b])+1;return Math.min(B,E)}D.addRaw=function(G){var E=G[b]&s;var F=A(hll.util.shiftRightUnsignedLong(G,C));D.registers[E]=Math.max(D.registers[E],F)};D.algorithmCardinality=function(){var E=D.m;var I=0;var J=0;var F=D.registers;for(var G=E-1;G>=0;G--){var H=F[G];I+=1/((1<<H)>>>0);if(H==0){J++}}var K=z/I;if((J!=0)&&(K<v)){return E*Math.log(E/J)}else{if(K<=u){return K}else{return(-1*r)*Math.log(1-(K/r))}}};D.cardinality=function(){return Math.ceil(D.algorithmCardinality())};D.cardinalityError=function(){return 1.04/Math.sqrt(1<<C)};D.union=function(I){if((D.log2m!=I.log2m)||(D.registerWidth!=I.registerWidth)){throw new Error("Union of sets with different 'log2m' "+((D.log2m==I.log2m)?"":"("+D.log2m+" != "+I.log2m+") ")+"or 'registerWidth'"+((D.registerWidth==I.registerWidth)?"":" ("+D.registerWidth+" != "+I.registerWidth+")")+".")}var E=D.m,H=I.m;var G=Math.min(E,H);var K=D.registers,F=I.registers;for(var J=G-1;J>=0;J--){K[J]=Math.max(K[J],F[J])}return D};D.fold=function(F){if(F==D.log2m){return D.clone()}if((F<1)||(F>D.log2m)){throw new Error("'log2m' cannot be less than 1 or greater than the current value.")}var J=new hll.HLL(F,D.registerWidth);var I=J.registers,H=D.registers;var K=D.log2m-F,E=J.m;for(var G=E-1;G>=0;G--){I[G]=Math.min(H[G]+K,B)}return J};D.clone=function(){var H=new hll.HLL(D.log2m,D.registerWidth);var G=D.registers,E=H.registers;for(var F=D.m-1;F>=0;F--){E[F]=G[F]}return H};D.clear=function(){var E=D.registers;for(var F=D.m-1;F>=0;F--){E[F]=0}return D};D.toHexString=function(){var H=new hll.util.ByteWriter();H.addBits(20,8);H.addBits(x-1,3);H.addBits(C,5);H.addBits(0,8);var E=D.m;var F=D.registers;for(var G=0;G<E;G++){H.addBits(F[G],x)}return hll.util.hexfromByteArray(H.getBytes())}};function e(r){switch(r){case 1:case 2:case 4:case 8:throw new Error("'m' cannot be less than 16 ("+r+" < 16).");case 16:return 0.673*r*r;case 32:return 0.697*r*r;case 64:return 0.709*r*r;default:return(0.7213/(1+1.079/r))*r*r}}var l=8;var d=1;hll.algorithm={EMPTY:"Empty",EXPLICIT:"Explicit",SPARSE:"Sparse",FULL:"Full",UNDEFINED:"Undefined"};var a={UNDEFINED:0,EMPTY:1,EXPLICIT:2,SPARSE:3,FULL:4};hll.fromHexString=function(r){var s=hll.util.hexToArrayBuffer(r);return q(s)};var q=function(u){var s=new Uint8Array(u);var r=hll.util.upperNibble(s[0]),t=hll.util.lowerNibble(s[0]);if(r==d){switch(t){case a.UNDEFINED:return{hllSet:k(u),version:r,algorithm:hll.algorithm.UNDEFINED};case a.EMPTY:return{hllSet:k(u),version:r,algorithm:hll.algorithm.EMPTY};case a.EXPLICIT:return{hllSet:i(u),version:r,algorithm:hll.algorithm.EXPLICIT};case a.SPARSE:return{hllSet:h(u),version:r,algorithm:hll.algorithm.SPARSE};case a.FULL:return{hllSet:f(u),version:r,algorithm:hll.algorithm.FULL};default:throw new Error("Unknown schema version 1 algorithm (index): "+t)}}else{throw new Error("Unknown schema version: "+r)}};function p(r){return{registerWidth:hll.util.getBitSequenceFromByte(r,0,3)+1,log2m:hll.util.getBitSequenceFromByte(r,3,5)}}function f(w){var A=new Uint8Array(w);var y=p(A[1]);var x=y.registerWidth,z=y.log2m,t=1<<z;var v=[];var s=3*l;for(var u=t-1;u>=0;u--){v.push(hll.util.getBitSequenceValueFromByteArray(A,s,x));s+=x}var r=new hll.HLL(z,x);r.registers=v;return r}function h(s){var r=new Uint8Array(s);return j(s,3,p(r[1]))}function k(s){var r=new Uint8Array(s);return o(p(r[1]))}function i(s){var r=new Uint8Array(s);return n(s,3,p(r[1]))}function o(r){return new hll.HLL(r.log2m,r.registerWidth)}function n(t,x,u){var w=new hll.HLL(u.log2m,u.registerWidth);var r=new Uint8Array(t);var s=x;while(s<r.length){var v=hll.util.extractLong(r,s);w.addRaw(v);s+=8}return w}function j(x,u,B){var y=B.registerWidth,C=B.log2m,t=1<<C;var w=[];for(var v=t-1;v>=0;v--){w.push(0)}var D=u*l;D+=(t*y)%l;var E=new Uint8Array(x);var r=Math.floor(((E.length-u)*l)/(C+y));for(var v=r-1;v>=0;v--){var A=hll.util.getBitSequenceValueFromByteArray(E,D,C);D+=C;var z=hll.util.getBitSequenceValueFromByteArray(E,D,y);D+=y;w[A]=z}var s=new hll.HLL(C,y);s.registers=w;return s}})();
if(hll===undefined){var hll={version:"1.0.0"}}if(hll.util===undefined){hll.util={version:"1.0.0"}}(function(){var d=8;var a=0,b=1;hll.util.shiftRightLong=function(h,e){e&=63;if(e==0){return h}var g=h[b];if(e<32){var f=h[a];return[((f>>>e)|(g<<(32-e))),(g>>e)]}else{return[(g>>(e-32)),(g>=0?0:-1)]}};hll.util.shiftRightUnsignedLong=function(h,e){e&=63;if(e==0){return h}var g=h[b];if(e<32){var f=h[a];return[((f>>>e)|(g<<(32-e))),(g>>>e)]}else{if(numBits==32){return[g,0]}else{return[(g>>>(e-32)),0]}}};hll.util.extractLong=function(e,h){var f=0,g=0;g|=e[h]<<24;g|=e[h+1]<<16;g|=e[h+2]<<8;g|=e[h+3];f|=e[h+4]<<24;f|=e[h+5]<<16;f|=e[h+6]<<8;f|=e[h+7];return[f>>>0,g>>>0]};hll.util.upperNibble=function(e){return e>>4};hll.util.lowerNibble=function(e){return e&15};hll.util.hexToArrayBuffer=function(j){j=j.substring(2);var h=j.length/2;var g=new ArrayBuffer(h);var f=new Uint8Array(g);for(var e=0;e<h;e++){var k=j[e*2]+j[e*2+1];f[e]=parseInt(k,16)}return g};hll.util.hexfromByteArray=function(e){var g="/x";for(var f=0;f<e.length;f++){var h=e[f];g+=(h<16?"0":"")+h.toString(16)}return g};hll.util.getBitSequenceFromByte=function(f,g,e){return(f>>>(d-(g+e)))&((1<<e)-1)};hll.util.getBitSequenceValueFromByteArray=function(o,e,g){var h=e>>>3;var l=(e+g)>>>3;var m=e&7;if(h==l){return hll.util.getBitSequenceFromByte(o[l],m,g)}var n=(e+g)&7;var p=0;var f=0;for(var k=l;k>=h;k--){var j;if(k==h){j=hll.util.getBitSequenceFromByte(o[k],m,d-m)}else{if(k==l){j=hll.util.getBitSequenceFromByte(o[k],0,n)}else{j=o[k]}}p|=j<<f;if(k==l){f+=n}else{f+=d}}return p};var c=[-1,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0];hll.util.leastSignificantBit=function(e){if(e==0){return -1}if((e&255)!=0){return c[((e>>>0)&255)]+0}if((e&65535)!=0){return c[((e>>>8)&255)]+8}if((e&16777215)!=0){return c[((e>>>16)&255)]+16}return c[((e>>>24)&255)]+24};hll.util.ByteWriter=function(){var f=this;var h=0;var e=[];var g=d;f.addBits=function(m,k){var l=k;while(l>0){var i=Math.min(l,g);var j=((m>>>(l-i))&((1<<i)-1))>>>0;h|=(j<<(g-i));l-=i;g-=i;if(g<=0){e.push(h);h=0;g=d}}};f.getBytes=function(){if(g<d){var i=e.slice(0);i.push(h);return i}else{return e}}}})();