mercredi 27 mai 2015

storing vectors in compressed form c++

I wrote the following code in c++ for performing gap compression -- in this a vector vpp containing values (3,4,8) (alternate representation of (3,4,8) in bit vector form is 00110001) is represented as [0]2 2 3 1. That is, starting with first bit value, alternating run lengths of 0 and 1 are recorded.

 bool flag=true;
 unsigned pv=~0u;
 unsigned cv=0;
 for(vector<uint64_t>::iterator is=vpp.begin(), lSub=vpp.end(); is!=lSub; ++is){
     cv=0;
     if(flag)
     {
         if((*is)==0)
             gcvpp.push_back(1);
         else
             gcvpp.push_back(0);
         flag=false;
         pv=(*is);
     }else{
         if((pv+1)!=(*is)) //if we do not get consecutive values, then write the difference
         {
             if(cv==0)
                 gcvpp.push_back(cv);
             gcvpp.push_back((*is)-(pv));
             cv=0;
         } else{ //consecutive value
             cv++;
         }                                              
     }
 }
 if(cv!=0)
     gcvpp.push_back(cv);

Can someone please help me identify as to where am I making a mistake in this algorithm

Aucun commentaire:

Enregistrer un commentaire