Advertisement
IWBH_01

PMCA Pattern Matching Compression Algorithm

Oct 2nd, 2021 (edited)
1,511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //PMCA Patter Matching Compression Algorithm
  2.  
  3. /*
  4. //currently a work in progress
  5.  
  6. //use something like ani-prime numbers, factor and compress the factors
  7.  
  8. var fact=[37,41,17, 23],
  9.  
  10. nom=37*41*17*23;
  11.  
  12. */
  13.  
  14. //huffman coding partial
  15. arr2="a,100,g,35,c,34,d,9,m,2,u,2".split(",").reverse();
  16. arr=[]
  17. i=0; L=arr2.length;
  18. while(i!=L){ arr.push([arr2[i]*1,arr2[i+1]]); i+=2;}
  19.  
  20. f0=arr.splice(0,2);
  21. arr.unshift([f0[0][0]+f0[1][0],f0]);
  22. arr.sort(function(a,b){ return a[0]>b[0]; });
  23. arr.length;
  24.  
  25.  
  26.  
  27.  
  28. //make patterns table at beginning like PNG & DNS domain format, have different sections for different length matches, with section 0 being 8 bit (smallest searched match)
  29.  
  30. "1 being 9 bits, 2=10, 3=11, 4=12, 5=13, and so on (num+=8)";
  31.  
  32. //sections begin with 2 bytes telling how many matches there are, round up length to the next byte to make parsing and saving this compression format easier.
  33.  
  34. //sections for exact match, factors, XOR match
  35.  
  36. //I feel like Math.log may come in handy somewhere
  37.  
  38.  
  39.  
  40.  
  41. DataView.prototype.getString=function(bi,L){
  42.  var i=0,s="";
  43.  while(i!=L){
  44.   s+=String.fromCharCode(this.getUint8(bi+i));
  45.   i++;
  46.  }
  47.  return s;
  48. };
  49.  
  50.  
  51. DataView.prototype.setString=function(bi,s){
  52.  var i=0,L=s.length;
  53.  while(i!=L){
  54.   this.setUint8(bi+i,s.charCodeAt(i));
  55.   i++;
  56.  }
  57. };
  58.  
  59.  
  60.  
  61. self.strNui8=function strNui8(s,ui8,u8i){
  62.  var L=s.length,i=L;
  63.   while(i--) ui8[u8i+i]=s.charCodeAt(i);
  64. };
  65.  
  66.  
  67. self.ui8Gstr=function Ui8Gstr(ui8,i,L){
  68.  var s="";
  69.   while(i!=L){ s+=String.fromCharCode(ui8[i]); i++; }
  70.   return s;
  71. };
  72.  
  73.  
  74.  
  75.  
  76. self.test_str="This is a very loooong test string for compression method, must use bitwise NOR? denoted by ^ in javascript to find bit similarities and difference which can be eventual after many iterations translated to 'patterns' for the PMCA Pattern Matching Compression Algorithm. I guess this is the end for now. END";
  77.  
  78.  
  79. lel=test_str.length;
  80.  
  81. self.ui8_1=new Uint8Array(lel);
  82.  
  83. strNui8(test_str,ui8_1,0);
  84.  
  85.  
  86.  
  87.  
  88.  
  89. //new
  90. tak=function(ui16,ui16_2){
  91.  var L=ui16.length,i=0,n,z=0;
  92.   while(i!=L){
  93.    ui16_2[i]=(n=ui16[i])^(z+(n>>1));
  94.    z=(n&1)<<15;
  95.    i++;
  96.   }
  97. };
  98. tuk=function(ui16,ui16_2){
  99.  var L=ui16.length,i=0,n,z=0;
  100.   while(i!=L){
  101.    ui16_2[i]=(n=ui16[i])^(z+(n<<1));
  102.    z=n>>15;
  103.    i++;
  104.   }
  105. };
  106.  
  107. rtak=function(ui16,ui16_2){
  108.  var L=ui16.length,i=L,n,z=ui16[L-2]&1;
  109.   while(i--){
  110.    ui16_2[i]=(n=ui16[i])^(z+(n>>1));
  111.    z=(n&1)<<15;
  112.   }
  113. };
  114. rtuk=function(ui16,ui16_2){
  115.  var L=ui16.length,i=L,n,z=ui16[L-2]>>15;
  116.   while(i--){
  117.    ui16_2[i]=(n=ui16[i])^(z+(n<<1));
  118.    z=n>>15;
  119.   }
  120. };
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. //counting repetitions, gaps
  128. self.pat1=function pattern1(ui8_){
  129. var map=[],n=16, Lim=[],cc,w=0,i=0, L=ui8_.length;
  130. while(n--) map.push([]); na=map[0];
  131. n=16; while(n--)Lim.push(0);
  132. while(i!=L){
  133.   cc=ui8_[i];
  134.   map[n=cc>>4].push(w-Lim[n]);  Lim[n]=w;
  135.   w++;
  136.   map[n=cc&15].push(w-Lim[n]);  Lim[n]=w;
  137.   i++; w++;
  138.  }
  139.  
  140.  return map;
  141. };
  142.  
  143. //counting repetitions of gaps in repetitions, counting gaps in present numbers;
  144. self.UltMp=function ultraMap(zz){ //zz=map[n];
  145. var i=zz.length,z2=[],z3=[],z4=[],L,Lb;
  146. while(i--) if(z2.indexOf(zz[i])==-1) z2.push(zz[i]);
  147.  
  148. z2.sort(function(a,b){return a>b;});
  149.  
  150. i=0; L=z2.length;
  151. Lb=0;
  152. while(i!=L){ z3[i]=z2[i]-Lb; Lb=z2[i]; i++;}
  153.  
  154. i=0; L=zz.length;
  155. while(i!=L){ z4[i]=z2.indexOf(zz[i]); i++;}
  156.  
  157. return [z2,z3,z4];
  158. };
  159.  
  160.  
  161. self.map1=pat1(ui8_1);
  162.  
  163.  
  164. self.reMap=function(a){ var mp2=UltMp(a); return ([mp2[1].length]).concat(mp2[1],mp2[2]); };
  165. //reMap is awesome! :)
  166.  
  167. mp1=reMap(map1[0]);
  168.  
  169. self.uncraam=function(ia){ //reverse the mp2[1] part of reMap to get the mp2[0] part
  170.  var i=0,L=ia.length,Lst=0,ra=[];
  171.  while(i!=L){ ra[i]=ia[i]+Lst; Lst=ra[i]; i++;}
  172.  return ra;
  173. };
  174.  
  175.  
  176. //begin BITmp;   after reMap(s), array size is 1.5ish what it was, but after BITmp, it will be much smaller
  177. self.BITmp=function BIT_MAP(a){
  178.  //step 1, get largest number, get its bit length:
  179.  var Ln=0,L=a.length,i=L; while(i--) if(a[i]>Ln)Ln=a[i];
  180.  var bL=Ln.toString(2).length; //bitLength, number of bytes needed for seq
  181.  var cv,c2=0,c3=0,Lv=-1,ri=1, MV=(1<<bL)-1;
  182.  ra=[bL]; //byte length (max) is first,  treat ra as uint_bL array,
  183.  //seq, number to repeat & number repetition count
  184.  i=0;
  185.     //WUT (doesn't work)
  186.  while(i!=L){ cv=a[i];
  187.  if(cv==Lv){ c2++; if(c3){ra[] } c3=0;}
  188.  else{ c3++; if(c2>3) ra[]; c2=0;}
  189.  Lv=cv; i++;}
  190.  return ra;
  191. };
  192.  
  193.  
  194. //make 2, 3, 4, 5, 6, 7 diminsional "'arrays'" of the data and find (straight?) paths of matching bits?
  195. //2d = (width*y_pos)+x_pos; 3d = (width*height*z_pos)+(width*y_pos)+x_pos; 4d=(width*height*depth*v_pos)+(width*height*z_pos)+(width*y_pos)+x_pos;
  196.  
  197. //make size match 8 length cube? bits not bytes so do nothing to length is already 2 diminsions, divide by 8 once is 3 diminsions, twice is 4 dimensions and so on
  198.  
  199. self.dimensionator=function(Len){
  200.  var din=2; if((!Len)||typeof Len!="number") return 0;
  201.  while((Len>>=3)>3) din++;  //whis is >3 ? I know >>=3, but why >3 ?
  202. //args = coords = x,y,z,a,b,c,d,e,f; etc. aka dimensions 1,2,3,4,5,6,7,8,9 etc.
  203.  var rez=new Uint16Array(din),i=0,t3=0;
  204.  while(i!=din){ rez[i]=t3; i++; t3+=3; }
  205.  return [din,rez];
  206. };
  207.  
  208.  //because we're using >>=3, aka /=8, then each value in cor "should?" never be biger than 7, so we only need 3 bits per element
  209.  
  210.  
  211. self.getBitAtCoord=function(cor,sps,ui8){ //sps = shift amount array
  212. //cor = (Array)coords = x,y,z,a,b,c,d,e,f; etc. aka dimensions 1,2,3,4,5,6,7,8,9 etc.; use only the diminsions that exist
  213.  
  214.  var L=cor.length,rr=0,i=1,t3=0, x_c=cor[0];
  215.  //test weather using sps: cor[i]<<sps[i]; or just multiplying i by 3 is faster: cor[i]<<(3*i); or use t3 again: t3=0; while() cor[i]<<t3; i++;t3+=3;
  216.  while(i!=L){ rr+=(cor[i]<<t3); i++; t3+=3; }
  217.  
  218.  if(!ui8) return (rr<<3)+x_c;
  219.  //cor[0], aka "x" is the simplest, so it is only the bit index inside the byte, so skip it
  220.  return (ui8[rr]&(1<<(7-x_c)))&&1;
  221. };
  222.  
  223.  
  224. self.rres=function(n,d){ var r=n%d; return [(n-r)/d,r];}; //(division with remainder, probly need later)
  225.  
  226.  
  227.  
  228. self.scrubit=function(u8){ //(pat1 v2) (doesn't work yet)
  229.  var le=[],lm=[],i=16,L=u8.length,w=0,
  230.  bep=u8[0]<<4,b0p=4,n;
  231.  while(i--){le[i]=[];lm[i]=0;}
  232.  
  233.  while(i<L){
  234.   if(b0p==8){i++; b0p=0; bep|=u8[i]; }
  235.   le[n=(bep>>8)].push(w-lm[n]); lm[n]=w; n=4095&(bep<<1);bep=n; w++; b0p++;
  236.  }
  237.  return le;
  238. };
  239.  
  240.  
  241. //end of newest part
  242.  
  243.  
  244.  
  245. //something like this:
  246. dv1=new DataView(ab); L=dv1.byteLength;
  247. self.c1=0; self.haz=[]; self.c2=0; i=0;
  248. while(i<L){ c1=dv1.getUint8(i); if(haz[c1]) haz[c1].push(i); else haz[c1]=[i];  i++; }
  249.  
  250. (auto sorts all 1 byte matches by order they appear, cool)
  251.  
  252.  
  253. test=haz[45]; //using byte 45 as a test for bytes in general
  254.  
  255. L2=test.length;
  256.  
  257. tst2=new Array(L2);
  258.  
  259. i=1; tst2[0]=test[0];
  260. while(i!=L2){
  261.  tst2[i]=test[i]^(test[i-1]);
  262.  i++;
  263. }
  264.  
  265.  
  266. /*
  267. start with byte 0, count only the lengths of each group of bytes equal to 0,
  268. like dns domain format, but use length indicator of any number of bytes with bit 0 being reserved for indicating weather there are more bytes in the length indicator, if the length is greater than 128, then the first bit in the first byte would be set to 1, with the length number made of 14 bits across the two bytes excluding the fist bit of the 2nd byte also,
  269. */
  270.  
  271.  
  272. //saaaasss function finds one factor at a time of any integer that can be accurately stored in javascript, returns false on failure or if input number (bign) is prime:
  273.  
  274. function old_sass(bign,lim){
  275.  var e=Math.log(bign),e2; lim=lim||9001;
  276.  while(lim--){
  277.  e2=bign/e; if(!(e2%1)) break;
  278.  e=Math.ceil(e2);
  279.  }
  280.  console.log("sass level is :"+lim);
  281.  return lim?[e,e2]:false;
  282. }
  283.  
  284.  
  285. function sass(bign,lim){
  286.  var L0=Math.log(bign),e=Math.ceil(L0),e2,flo; lim=lim||9001;
  287.  while(lim--){
  288.  e2=bign%e;
  289.  if(e2==1&&(!flo)){flo=!0; e2=Math.floor(L0); }else
  290.  if(!e2) break;
  291.  e=e2;
  292.  }
  293.  console.log("sass level is :"+lim);
  294.  return lim?e:false;
  295. }
  296.  
  297.  
  298. function sass2(bign,lim){
  299.  var L0=Math.log2(bign),e=Math.ceil(L0),e2,flo; lim=lim||9001;
  300.  while(lim--){
  301.  e2=bign%e;
  302.  if(e2==1&&(!flo)){flo=!0; e2=Math.floor(L0); }else
  303.  if(!e2) break;
  304.  e=e2;
  305.  }
  306.  console.log("sass level is :"+lim);
  307.  return lim?e:false;
  308. }
  309.  
  310.  
  311.  
  312. function factorize(bign,lim){
  313.  var L0,e=1,e2=0,flo,f=[]; lim=lim||9001;
  314.  while(lim--){
  315.  if(e2===0){ f.push(e); bign/=e; L0=Math.log2(bign); e=Math.ceil(L0); flo=!1; }
  316.  
  317.  e2=bign%e;
  318.  if(e2==1){ if(!flo){flo=!0; e2=Math.floor(L0); }else{ break; } }
  319.   if(e2!=0) e=e2;
  320.  
  321.  }
  322.  
  323.  return lim?f:false;
  324. }
  325.  
  326.  
  327.  
  328. cant=function(s){ if(typeof s!="string")s=""; var bg=BigInt(0),i=0,L=s.length,e8=BigInt(8); while(i!=L){ bg=(bg<<e8)+BigInt(s.charCodeAt(i)); i++; } return bg; }
  329. //decimal places  Math.ceil(Math.log10())
  330.  
  331.  
  332. //breakout (taken out)
  333.  
  334.  
  335. self.ui161=new Uint16Array(ui8_1.buffer);
  336.  
  337. self.ui8_2=new Uint8Array(16);
  338.  
  339. self.i1=0; self.L=16;
  340.  
  341. while(i1<L){ ui8_2[i1]=ui8_1[i1]^ui8_1[16+i1]; i1++;}
  342.  
  343.  
  344.  
  345.  
  346. self.i1=0;
  347. self.xorL=0; self.nL1=0; self.xorL2=0;
  348.  self.chc=new Uint16Array(153);  //change of change
  349. while(i1!=L){
  350.   nL0=xorL;   xorL^=ui161[i1];
  351.   nL1=xorL2; xorL2=xorL^nL0;
  352.   chc[i1]^=xorL2;
  353.   i1++; }
  354.  
  355. */
  356.  
  357. self.chamt=function ChangeAmount(xor){
  358.   var _0=1,_1=2,_2=4,_3=8,_4=16,_5=32,_6=64,_7=128,_8=256,_9=512,_A=1024,_B=2048,_C=4096,_D=8192,_E=16384,_F=32768;
  359.  
  360.   return ((xor&_0)&&1)+((xor&_1)&&1)+((xor&_2)&&1)+((xor&_3)&&1)+((xor&_4)&&1)+((xor&_5)&&1)+((xor&_6)&&1)+((xor&_7)&&1)+((xor&_8)&&1)+((xor&_9)&&1)+((xor&_A)&&1)+((xor&_B)&&1)+((xor&_C)&&1)+((xor&_D)&&1)+((xor&_E)&&1)+((xor&_F)&&1);
  361. };
  362.  
  363.  
  364. self.cocam=function ChangeOfChangeAmount(xor){
  365.   var _1=1,i=0,L=16,rez=0,lst=0,cur;
  366.   while(i!=L){ cur=(xor&_1)&&1; rez+=cur&lst; lst=cur; _1<<=1; i++; }
  367.   return rez;
  368. };
  369.  
  370.  
  371.  
  372. /*
  373. L=16; i=0;
  374.  
  375. ui163=new Uint16Array(16);
  376.  
  377. while(i!=L){ ui162[i]=chamt(ui161[i+0]^ui161[i+1+0]); ui163[i]=cocam(ui161[i+0]^ui161[i+1+0]);  i++; }
  378. */
  379.  
  380. function Btrring(ui16,bufr){
  381. L=bufr.length; i=0; st=0; a=0; b=0; bi=0;
  382.  
  383. a=ui16[0]; b=ui16[1];
  384.  
  385. while(i!=L){
  386.  bufr[bi+si]=ui16[i]^(((a<<st)&65535)+(b>>(16-st)));
  387.  st++;
  388.  if(st==16){i++; st=0; a=b; b=ui16[(i+1)%L]; }
  389. }
  390.  
  391. }
  392.  
  393.  
  394.  
  395. function binAnTest(){
  396.  
  397. ctx.moveTo(0,250);
  398. var val=250; bi=0; bit=0;
  399. aam=128; chr=ui8_1[0]; i=0;
  400.  
  401. while(i<ui8_1.length){
  402.  bit=(aam&chr)&&1;
  403.  if(bit==lal){ if(bit==1) val--; else val++; }
  404.  ctx.lineTo(bi,val);
  405.  lal=bit;
  406.  aam>>=1; bi++; if(aam==0){ aam=128; i++;  chr=ui8_1[i]; }
  407. }
  408.  
  409. ctx.stroke();
  410.  
  411. }
  412.  
  413.  
  414.  
  415. //redo?
  416. var i=16, a16=new Uint16Array(16), s=1; while(i--){ a16[i]=s; s<<=1; }
  417. //look for patterns in bits, ring theory, winding?
  418.  
  419. //we're skip counting (the cumulative index of the NEXT BIT) now, by function: +1, +2, +3, (up to +16);  fib: 1,1,2,3,5,8,13,21,34,55;  fib: 1,3,4,7,11,18,29,47; etc; mult: *2, *3, *4, *5, *6 *7, etc; multi_fib: 2,3,6,18,108,1944,etc;etc; pow: ^2, ^3, ^4, ^5, ^6, ^7, ^8, ^9, ^10; other functions ?
  420.  
  421.  
  422. //aka put the current BIT index into the function currently being used to scan for matches
  423.  
  424. //BIGfactor: factor the entire number and compress by counting repeated factors, might use modulus also,
  425.  
  426. //subtract number modulusing in multiples of two to save proccesor cycles? aka %7 as rep -(7*1024) = -(7<<10), then count down -(7<<9), -(7<<8) etc.
  427.  
  428. //limit to 16 megabyte length blocks for easier indexing? (3 byte index)
  429.  
  430. //find prime
  431. function sheez(lol,upL){ lol=lol||33; upL=upL||(1<<24);
  432.  var prm=[3,5,7,11,13,17,19,23,29,31],pL=10,pi=pL,g0=1;
  433.   if(lol<33)lol=33; if(upL<33) return prm;
  434.   while(lol<upL){
  435.    pi--;
  436.    g0=((lol%prm[pi])!=0)&&g0;
  437.    if(pi==0){ if(g0) prm.push(lol); lol+=2;  pi=prm.length; g0=1; }
  438.   }
  439.   prm.unshift(2);
  440.   return prm;
  441. }
  442.  
  443.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement