public:
IntC index;
IntC nbits;
IntC size;
IntC csize;
vector
unsigned char temp[8];//temprory for storing data
IntC tempsize;
void BitHolderInit(IntC dimension, IntC ind)
{
index=ind;
nbits=dimension;
IntC i; if(dimension%8) i=dimension/8; else i=dimension/8 + 1;
size = i; tempsize = 0; csize = 0;
}
int BitHolderWrite()
{
if (tempsize != 8) return -1;
//cerr<<"\n Csize : "<< csize <<" "<< size<<" "<< nbits<<" "<< tempsize;
bits.push_back( ((temp[0] & 0x1)<<7) | ((temp[1] & 0x1)<<6) | ((temp[2] & 0x1)
<< 5) | ((temp[3] & 0x1)<< 4) | ((temp[4] & 0x1)<< 3) | ((temp[5] & 0x1)<< 2) | ((temp[6] & 0x1)<< 1) | (temp[7] & 0x1) );
// bits.push_back(((temp[0] & 0x1)<<7) | ((temp[1] & 0x1)<<6 & 0x) | ((temp[
2] & 0x1)<<5) | ((temp[3] & 0x1)<<4) | ((temp[4] & 0x1)<<3) | ((temp[5] & 0x1)<<2)
| ((temp[6] & 0x1)<<1) | ((temp[7] & 0x1)));
tempsize=0; csize++;
temp[0]=temp[1]=temp[2]=temp[3]=temp[4]=temp[5]=temp[6]=temp[7]='0';
return 1;
}
int AddBit(IntC b)
{
//cerr<<" ADDING "<< b<<" "<< tempsize;
if(b==0) temp[tempsize]='0'; else temp[tempsize]='1';
tempsize++;
if(tempsize==8) BitHolderWrite();
}
int GetBit(IntC pos)
{
//cerr<<" Position : "<< pos<<" "<< pos/8<<" "<< pos%8<<" "<< bits.size();;
if(pos>nbits) {cerr<<" ERROR ";return -1;}
if(((pos-1)/8)< bits.size())
{
if (((bits[(pos-1)/8] >> (7-(pos-1)%8)) & 0x1) == 0x1)
return 1;
else
return 0;
}
pos=(pos)%8;
pos--;
//cerr<<" "<< pos<<" "<< tempsize<<" "<< temp[pos];
if(temp[pos]=='1') return 1; else return 0;
}
int PrintBits()
{
cerr<<" Printing Bits Index:"<< index<<" nBits:"<< nbits<<" ";
for (IntC i = 0; i < bits.size(); i++)
{
cerr<<(bits[i]>>7 & 0x1)<<(bits[i]>>6 & 0x1)<<(bits[i]>>5 & 0x1)<<(bits[i]
>>4 & 0x1)<<(bits[i]>>3 & 0x1)<<(bits[i]>>2 & 0x1)<<(bits[i]>>1 & 0x1)<<(bits[i] &
0x1);
}
for (IntC i = 0; i < tempsize; i++)
cerr<< temp[i];
return 1;
}
} BitHolder,*PBitHolder;
bool operator<(const BitHolder &a, const BitHolder &b)
{
//cerr<<"\n SORTING "<< a.index<<" "<< b.index;
IntC i,j;
for(i=0;i< a.bits.size();i++)
{
for(j=7;j>=0;j--)
{
//cerr<<"| i:"<< i<<" j:"<< j<<" "<<(a.bits[i] >> j & 0x1)<<" "<< (b.bits[
i] >> j & 0x1);
if(((a.bits[i] >> j & 0x1)==0x1)&&((b.bits[i] >> j & 0x1)==0x0))
return false;
if(((a.bits[i] >> j & 0x1)==0x0)&&((b.bits[i] >> j & 0x1)==0x1))
return true;
}
}
//cerr<<" Tempsize : "<< a.tempsize;
for(i=0;i< a.tempsize;i++)
{
if((a.temp[i]=='1')&&(b.temp[i]=='0'))
return false;
if((a.temp[i]=='0')&&(b.temp[i]=='1'))
return true;
}
return false;
}
bool operator==(const BitHolder &a, const BitHolder &b)
{if(((a< b)==false)&&((b< a)==false)) return true; else false;}
bool operator>(const BitHolder &a, const BitHolder &b)
{if (b < a) return true; else false;}
No comments:
Post a Comment