SNAP Library , Developer Reference  2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bits.cpp
Go to the documentation of this file.
00001 
00002 // One byte
00003 const int TB1Def::B1Bits=8;
00004 const int TB1Def::MxP2Exp=TB1Def::B1Bits-1;
00005 const TB1Def::TB1 TB1Def::MxB1=0xFF;
00006 
00007 TB1Def::TB1Def(){
00008   B1P2T=new TB1[B1Bits+1]; B1P2T[0]=1;
00009   for (int BitN=1; BitN<B1Bits; BitN++){B1P2T[BitN]=TB1(2*B1P2T[BitN-1]);}
00010   B1P2T[B1Bits]=0;
00011 
00012   B1BitsT=new int[MxB1+1];
00013   for (int B1N=0; B1N<MxB1+1; B1N++){
00014     TB1 B1=(TB1)B1N; B1BitsT[B1]=0;
00015     for (int BitN=0; BitN<B1Bits; BitN++){B1BitsT[B1N]+=B1%2; B1/=(TB1)2;}}
00016 }
00017 
00018 int TB1Def::GetB1Bits(const TB1& B1){
00019   return B1Def.B1BitsT[B1];
00020 }
00021 
00022 uint TB1Def::GetP2(const int& P2Exp){
00023   IAssert((0<=P2Exp)&&(P2Exp<=TB1Def::MxP2Exp));
00024   return B1Def.B1P2T[P2Exp];
00025 }
00026 
00027 int TB1Def::GetL2(const uchar& Val){
00028   int L2=0;
00029   while ((L2<TB1Def::MxP2Exp)&&(Val>=B1Def.B1P2T[L2])){L2++;}
00030   return L2-1;
00031 }
00032 
00033 bool TB1Def::GetBit(const int& BitN, const uchar& Val){
00034   IAssert((0<=BitN)&&(BitN<=TB1Def::MxP2Exp));
00035   return (Val & B1Def.B1P2T[BitN])!=0;
00036 }
00037 
00038 const TB1Def TB1Def::B1Def;
00039 
00041 // Two bytes
00042 const int TB2Def::B2Bits=16;
00043 const int TB2Def::MxP2Exp=TB2Def::B2Bits-1;
00044 const TB2Def::TB2 TB2Def::MxB2=0xFFFF;
00045 
00046 TB2Def::TB2Def(){
00047   B2P2T=new TB2[B2Bits+1]; B2P2T[0]=1;
00048   for (int BitN=1; BitN<B2Bits; BitN++){B2P2T[BitN]=TB2(2*B2P2T[BitN-1]);}
00049   B2P2T[B2Bits]=0;
00050 }
00051 
00052 int TB2Def::GetB2Bits(const TB2& B2){
00053   return
00054    TB1Def::B1Def.B1BitsT[(B2>>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
00055    TB1Def::B1Def.B1BitsT[(B2>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1];
00056 }
00057 
00058 uint TB2Def::GetP2(const int& P2Exp){
00059   IAssert((0<=P2Exp)&&(P2Exp<=TB2Def::MxP2Exp));
00060   return B2Def.B2P2T[P2Exp];
00061 }
00062 
00063 int TB2Def::GetL2(const TB2& Val){
00064   int L2=0;
00065   while ((L2<TB2Def::MxP2Exp)&&(Val>=B2Def.B2P2T[L2])){L2++;}
00066   return L2-1;
00067 }
00068 
00069 const TB2Def TB2Def::B2Def;
00070 
00072 // Four bytes
00073 const int TB4Def::B4Bits=32;
00074 const int TB4Def::MxP2Exp=TB4Def::B4Bits-1;
00075 const TB4Def::TB4 TB4Def::MxB4=0xFFFFFFFF;
00076 
00077 TB4Def::TB4Def(){
00078   B4P2T=new TB4[B4Bits+1]; B4P2T[0]=1;
00079   for (int BitN=1; BitN<B4Bits; BitN++){B4P2T[BitN]=TB4(2*B4P2T[BitN-1]);}
00080   B4P2T[B4Bits]=0;
00081 }
00082 
00083 int TB4Def::GetB4Bits(const TB4& B4){
00084   return
00085    TB1Def::B1Def.B1BitsT[(B4>>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
00086    TB1Def::B1Def.B1BitsT[(B4>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
00087    TB1Def::B1Def.B1BitsT[(B4>>(2*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
00088    TB1Def::B1Def.B1BitsT[(B4>>(3*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1];
00089 }
00090 
00091 uint TB4Def::GetP2(const int& P2Exp){
00092   IAssert((0<=P2Exp)&&(P2Exp<=TB4Def::MxP2Exp));
00093   return B4Def.B4P2T[P2Exp];
00094 }
00095 
00096 int TB4Def::GetL2(const uint& Val){
00097   int L2=0;
00098   while ((L2<TB4Def::MxP2Exp)&&(Val>=B4Def.B4P2T[L2])){L2++;}
00099   return L2-1;
00100 }
00101 
00102 const TB4Def TB4Def::B4Def;
00103 
00105 // Flag-Set
00106 const int TFSet::B4s=4;
00107 const int TFSet::Bits=TFSet::B4s*TB4Def::B4Bits;
00108 
00109 TFSet::TFSet(
00110  const int& FlagN1, const int& FlagN2, const int& FlagN3,
00111  const int& FlagN4, const int& FlagN5, const int& FlagN6,
00112  const int& FlagN7, const int& FlagN8, const int& FlagN9):
00113   B4V(4, 4){
00114   if (FlagN1!=-1){Incl(FlagN1);}
00115   if (FlagN2!=-1){Incl(FlagN2);}
00116   if (FlagN3!=-1){Incl(FlagN3);}
00117   if (FlagN4!=-1){Incl(FlagN4);}
00118   if (FlagN5!=-1){Incl(FlagN5);}
00119   if (FlagN6!=-1){Incl(FlagN6);}
00120   if (FlagN7!=-1){Incl(FlagN7);}
00121   if (FlagN8!=-1){Incl(FlagN8);}
00122   if (FlagN9!=-1){Incl(FlagN9);}
00123 }
00124 
00125 const TFSet TFSet::EmptyFSet;
00126 
00128 // Bit8-Set
00129 const int TB8Set::Bits=8;
00130 
00131 TStr TB8Set::GetStr() const {
00132   TChA ChA;
00133   for (int BitN=0; BitN<Bits; BitN++){
00134     if (In(BitN)){ChA+='1';} else {ChA+='0';}}
00135   return ChA;
00136 }
00137 
00138 void TB8Set::Wr(){
00139   printf("[");
00140   for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
00141   printf("]\n");
00142 }
00143 
00145 // Bit32-Set
00146 const int TB32Set::Bits=32;
00147 
00148 TStr TB32Set::GetStr() const {
00149   TChA ChA;
00150   for (int BitN=0; BitN<Bits; BitN++){
00151     if (In(BitN)){ChA+='1';} else {ChA+='0';}}
00152   return ChA;
00153 }
00154 
00155 void TB32Set::Wr(){
00156   printf("[");
00157   for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
00158   printf("]\n");
00159 }
00160 
00162 // Bit-Set
00163 TBSet::TBSet(const TBSet& BSet):
00164   B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){
00165   Gen(BSet.Bits);
00166   for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=BSet.B4T[B4N];}
00167 }
00168 
00169 TBSet& TBSet::operator=(const TBSet& BSet){
00170   if (this!=&BSet){
00171     if (Bits!=BSet.Bits){Gen(BSet.Bits);}
00172     for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=BSet.B4T[B4N];}}
00173   return *this;
00174 }
00175 
00176 bool TBSet::operator==(const TBSet& BSet) const {
00177   if (Bits!=BSet.Bits){return false;}
00178   for (int B4N=0; B4N<B4s-1; B4N++){if (B4T[B4N]!=BSet.B4T[B4N]){return false;}}
00179   return (B4s>0)&&((B4T[B4s-1]&LastB4Mask)==(BSet.B4T[B4s-1]&BSet.LastB4Mask));
00180 }
00181 
00182 void TBSet::Gen(const int& _Bits){
00183   if (B4T!=NULL){delete[] B4T;}
00184   Bits=_Bits;
00185   B4T=new TB4Def::TB4[B4s=(Bits-1)/TB4Def::B4Def.B4Bits+1];
00186   LastB4Mask=TB4Def::B4Def.MxB4;
00187   for (int BitN=Bits; BitN<B4s*TB4Def::B4Def.B4Bits; BitN++){
00188     LastB4Mask&= ~TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits];}
00189   Clr();
00190 }
00191 
00192 void TBSet::Clr(){
00193   for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=0;}}
00194 
00195 void TBSet::Fill(){
00196   for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=TB4Def::B4Def.MxB4;}}
00197 
00198 int TBSet::Get1s(){
00199   int Ones=0; SetLastB4();
00200   for (int B4N=0; B4N<B4s; B4N++){Ones += TB4Def::B4Def.GetB4Bits(B4T[B4N]);}
00201   return Ones;
00202 }
00203 
00204 void TBSet::Wr(){
00205   for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
00206   //printf("\n");
00207 }
00208