Advertisement
Guest User

Untitled

a guest
Feb 6th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1.  
  2. #include <cstdio>
  3.  
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. typedef unsigned char uint8;
  9. typedef unsigned short uint16;
  10. typedef unsigned int uint32;
  11. typedef unsigned long long uint64;
  12.  
  13. void CompressorRAnsFastCmp(uint8* orgBb,int orgSize,uint32* counters,uint8* cmpBb,int& cmpSize){
  14.     const int totFreqBits=12;
  15.     const int totFreq=1<<totFreqBits;
  16.     int cumFreqs[256]={};
  17.     for(int code=0;code<255;code++)
  18.         cumFreqs[code+1]=cumFreqs[code]+counters[code];
  19.  
  20.     cmpSize=0;
  21.     uint64 x=0x100000000;
  22.    
  23.     for(int i=orgSize-1;i>=0;i--){
  24.         int code=orgBb[i];
  25.         uint32 freq=counters[code];
  26.         uint32 cumFreq=cumFreqs[code];
  27.        
  28.         uint64 xNext=(x>>32);
  29.         xNext=xNext/freq*totFreq+xNext%freq+cumFreq;
  30.         if(xNext>=((1L<<32))){
  31.             *((uint32*)(cmpBb+cmpSize))=x;
  32.             cmpSize+=4;
  33.             x=xNext;
  34.         }
  35.         else{
  36.             x=x/freq*totFreq+x%freq+cumFreq;
  37.         }
  38.     }
  39.    
  40.     *((uint64*)(cmpBb+cmpSize))=x;
  41.     cmpSize+=8;
  42. }
  43.  
  44.  
  45. void CompressorRAnsFastDcm(uint8* cmpBb,int cmpSize,int orgSize,uint32* counters,uint8* dcmBb,int& dcmSize){
  46.     const int totFreqBits=12;
  47.  
  48.     uint64_t xFreqToData[(1L<<totFreqBits)]={};
  49.     for(int cumFreqsIndex=0, symb=0, cumFreq=counters[symb]; cumFreqsIndex<(1L<<totFreqBits); )
  50.         if(cumFreqsIndex>=cumFreq){
  51.             cumFreq+=counters[++symb];
  52.         }else{
  53.             xFreqToData[cumFreqsIndex]|=(uint64_t)symb<<16;
  54.             xFreqToData[cumFreqsIndex]|=(uint64_t)(cumFreqsIndex-(cumFreq-counters[symb]))<<32;
  55.             xFreqToData[cumFreqsIndex]|=(uint64_t)counters[symb];
  56.             cumFreqsIndex++;
  57.         }
  58.    
  59.     uint8_t* dcmBbCur=(uint8_t*)dcmBb;
  60.     uint8_t* dcmBbEnd=(uint8_t*)(dcmBb+orgSize);
  61.     uint32_t* cmpBbCur=(uint32_t*)(cmpBb+cmpSize)-1;
  62.     uint64_t x=(((uint64_t)*cmpBbCur--)<<32)|*cmpBbCur--;
  63.    
  64.     while(dcmBbCur<dcmBbEnd){
  65.         if( x<(1L<<32) ) x=(x<<32)+*cmpBbCur--;
  66.         uint64_t d=xFreqToData[x&((1L<<totFreqBits)-1)];
  67.         x=(x>>totFreqBits)*(uint16_t)(d)+(d>>32);
  68.         *dcmBbCur++=d>>16;
  69.     }
  70.    
  71.     dcmSize=orgSize;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement