SNAP Library 2.0, Developer Reference  2013-05-13 16:33:57
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.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // One byte
00005 class TB1Def{
00006 public:
00007   typedef uchar TB1;
00008   static const int B1Bits;
00009   static const int MxP2Exp;
00010   static const TB1 MxB1;
00011   TB1* B1P2T;
00012   int* B1BitsT;
00013 public:
00014   TB1Def();
00015   ~TB1Def(){delete[] B1P2T; delete[] B1BitsT;}
00016 
00017   TB1Def& operator=(const TB1Def&){Fail; return *this;}
00018 
00019   static int GetB1Bits(const TB1& B1);
00020   static uint GetP2(const int& P2Exp);
00021   static int GetL2(const uchar& Val);
00022   static bool GetBit(const int& BitN, const uchar& Val);
00023 
00024   static const TB1Def B1Def;
00025 };
00026 
00028 // Two bytes
00029 class TB2Def{
00030 public:
00031   typedef unsigned short int TB2;
00032   static const int B2Bits;
00033   static const int MxP2Exp;
00034   static const TB2 MxB2;
00035   TB2* B2P2T;
00036 public:
00037   TB2Def();
00038   ~TB2Def(){delete[] B2P2T;}
00039 
00040   TB2Def& operator=(const TB2Def&){Fail; return *this;}
00041 
00042   static int GetB2Bits(const TB2& B2);
00043   static uint GetP2(const int& P2Exp);
00044   static int GetL2(const TB2& Val);
00045 
00046   static const TB2Def B2Def;
00047 };
00048 
00050 // Four bytes
00051 class TB4Def{
00052 public:
00053   typedef uint TB4;
00054   static const int B4Bits;
00055   static const int MxP2Exp;
00056   static const TB4 MxB4;
00057   TB4* B4P2T;
00058 public:
00059   TB4Def();
00060   ~TB4Def(){delete[] B4P2T;}
00061 
00062   TB4Def& operator=(const TB4Def&){Fail; return *this;}
00063 
00064   static int GetB4Bits(const TB4& B4);
00065   static uint GetP2(const int& P2Exp);
00066   static int GetL2(const uint& Val);
00067 
00068   static const TB4Def B4Def;
00069 };
00070 
00072 // Flag-Set
00073 /*class TFSet{
00074 private:
00075   static const int B4s;
00076   static const int Bits;
00077   TB4Def::TB4 B4T[4];
00078 public:
00079   TFSet(){
00080     B4T[0]=0; B4T[1]=0; B4T[2]=0; B4T[3]=0;}
00081   TFSet(const TFSet& FSet){
00082     B4T[0]=FSet.B4T[0]; B4T[1]=FSet.B4T[1];
00083     B4T[2]=FSet.B4T[2]; B4T[3]=FSet.B4T[3];}
00084   TFSet(const int& FlagN){
00085     B4T[0]=0; B4T[1]=0; B4T[2]=0; B4T[3]=0;
00086     Assert((0<=FlagN)&&(FlagN<Bits));
00087     B4T[FlagN/TB4Def::B4Def.B4Bits]=
00088      TB4Def::B4Def.B4P2T[FlagN%TB4Def::B4Def.B4Bits];}
00089 
00090   TFSet& operator=(const TFSet& FSet){
00091     if (this!=&FSet){
00092       B4T[0]=FSet.B4T[0]; B4T[1]=FSet.B4T[1];
00093       B4T[2]=FSet.B4T[2]; B4T[3]=FSet.B4T[3];}
00094     return *this;}
00095   bool operator==(const TFSet& FSet) const {
00096     return
00097      (B4T[0]==FSet.B4T[0])&&(B4T[1]==FSet.B4T[1])&&
00098      (B4T[2]==FSet.B4T[2])&&(B4T[3]==FSet.B4T[3]);}
00099   TFSet& operator|(const int& FlagN){
00100     Assert((0<=FlagN)&&(FlagN<Bits));
00101     B4T[FlagN/TB4Def::B4Def.B4Bits]|=
00102      TB4Def::B4Def.B4P2T[FlagN%TB4Def::B4Def.B4Bits];
00103     return *this;}
00104   TFSet& operator|(const TFSet& FSet){
00105     B4T[0]|=FSet.B4T[0]; B4T[1]|=FSet.B4T[1];
00106     B4T[2]|=FSet.B4T[2]; B4T[3]|=FSet.B4T[3];
00107     return *this;}
00108 
00109   bool Empty() const {
00110     return (B4T[0]==0)&&(B4T[1]==0)&&(B4T[2]==0)&&(B4T[3]==0);}
00111   bool In(const int& FlagN) const {
00112     Assert((0<=FlagN)&&(FlagN<Bits));
00113     return (B4T[FlagN/TB4Def::B4Def.B4Bits] &
00114      TB4Def::B4Def.B4P2T[FlagN%TB4Def::B4Def.B4Bits])!=0;}
00115 };*/
00116 
00118 // Flag-Set
00119 class TFSet{
00120 private:
00121   static const int B4s;
00122   static const int Bits;
00123   TUIntV B4V;
00124 public:
00125   TFSet(): B4V(4, 4){}
00126   TFSet(const TFSet& FSet): B4V(FSet.B4V){}
00127   TFSet(
00128    const int& FlagN1, const int& FlagN2=-1, const int& FlagN3=-1,
00129    const int& FlagN4=-1, const int& FlagN5=-1, const int& FlagN6=-1,
00130    const int& FlagN7=-1, const int& FlagN8=-1, const int& FlagN9=-1);
00131   TFSet(const TFSet& FSet1, const TFSet& FSet2):
00132     B4V(4, 4){Incl(FSet1); Incl(FSet2);}
00133   ~TFSet(){}
00134   TFSet(TSIn& SIn): B4V(SIn){}
00135   void Save(TSOut& SOut) const {B4V.Save(SOut);}
00136 
00137   TFSet& operator=(const TFSet& FSet){
00138     if (this!=&FSet){B4V=FSet.B4V;} return *this;}
00139   bool operator==(const TFSet& FSet) const {return B4V==FSet.B4V;}
00140   TFSet& operator|(const int& FlagN){Incl(FlagN); return *this;}
00141   TFSet& operator|(const TFSet& FSet){Incl(FSet); return *this;}
00142 
00143   void Clr(){
00144     B4V[0]=0; B4V[1]=0; B4V[2]=0; B4V[3]=0;}
00145   bool Empty() const {
00146     return
00147      (uint(B4V[0])==0)&&(uint(B4V[1])==0)&&
00148      (uint(B4V[2])==0)&&(uint(B4V[3])==0);}
00149   void Incl(const int& FlagN){
00150     Assert((0<=FlagN)&&(FlagN<Bits));
00151     B4V[FlagN/TB4Def::B4Def.B4Bits]|=
00152      TB4Def::B4Def.B4P2T[FlagN%TB4Def::B4Def.B4Bits];}
00153   void Incl(const TFSet& FSet){
00154     B4V[0]|=FSet.B4V[0]; B4V[1]|=FSet.B4V[1];
00155     B4V[2]|=FSet.B4V[2]; B4V[3]|=FSet.B4V[3];}
00156   bool In(const int& FlagN) const {
00157     Assert((0<=FlagN)&&(FlagN<Bits));
00158     return (B4V[FlagN/TB4Def::B4Def.B4Bits] &
00159      TB4Def::B4Def.B4P2T[FlagN%TB4Def::B4Def.B4Bits])!=0;}
00160 
00161   static const TFSet EmptyFSet;
00162 };
00163 
00165 // Bit8-Set
00166 class TB8Set{
00167 private:
00168   static const int Bits;
00169   TB1Def::TB1 B1;
00170 public:
00171   TB8Set(): B1(0){}
00172   TB8Set(const TB8Set& B8Set): B1(B8Set.B1){}
00173   TB8Set(const uchar& _B1): B1(_B1){}
00174   TB8Set(TSIn& SIn){SIn.LoadBf(&B1, sizeof(TB1Def::TB1));}
00175   void Save(TSOut& SOut) const {SOut.SaveBf(&B1, sizeof(TB1Def::TB1));}
00176 
00177   TB8Set& operator=(const TB8Set& BSet){B1=BSet.B1; return *this;}
00178   TB8Set& operator=(const uchar& _B1){B1=_B1; return *this;}
00179   bool operator==(const TB8Set& BSet) const {return B1==BSet.B1;}
00180   bool operator<(const TB8Set& BSet) const {return B1<BSet.B1;}
00181 
00182   bool Empty() const {return B1==0;}
00183   TB8Set& Clr(){B1=0; return *this;}
00184   TB8Set& Fill(){B1=TB1Def::B1Def.MxB1; return *this;}
00185   bool IsPrefix(const TB8Set& BSet, const int& MnBitN) const {
00186     Assert((0<=MnBitN)&&(MnBitN<Bits));
00187     return (B1>>MnBitN)==(BSet.B1>>MnBitN);}
00188   uchar GetUCh() const {return B1;}
00189 
00190   void Incl(const int& BitN){
00191     Assert((0<=BitN)&&(BitN<Bits));
00192     B1|=TB1Def::B1Def.B1P2T[BitN];}
00193   void Excl(const int& BitN){
00194     Assert((0<=BitN)&&(BitN<Bits));
00195     B1&=TB1Def::TB1(~(TB1Def::B1Def.B1P2T[BitN]));}
00196   bool In(const int& BitN) const {
00197     Assert((0<=BitN)&&(BitN<Bits));
00198     return (B1 & TB1Def::B1Def.B1P2T[BitN])!=0;}
00199   void SetBit(const int& BitN, const bool& Bool){
00200     if (Bool) Incl(BitN); else Excl(BitN);}
00201   bool GetBit(const int& BitN) const {
00202     Assert((0<=BitN)&&(BitN<Bits));
00203     return (B1 & TB1Def::B1Def.B1P2T[BitN])!=0;}
00204   int GetBits() const {return Bits;}
00205   int Get1s() const {return TB1Def::B1Def.GetB1Bits(B1);}
00206   int Get0s() const {return Bits-Get1s();}
00207   TStr GetStr() const;
00208   void Wr();
00209 
00210   void PutInt(const int& MnBitN, const int& MxBitN, const int& Val){
00211     Assert((0<=MnBitN)&&(MnBitN<=MxBitN)&&(MxBitN<Bits));
00212     B1 &= TB1Def::TB1((~(TB1Def::B1Def.B1P2T[MxBitN-MnBitN+1]-1)) << MnBitN);
00213     B1 |= TB1Def::TB1((Val & (TB1Def::B1Def.B1P2T[MxBitN-MnBitN+1]-1)) << MnBitN);}
00214   int GetInt(const int& MnBitN, const int& MxBitN) const {
00215     Assert((0<=MnBitN)&&(MnBitN<=MxBitN)&&(MxBitN<Bits));
00216     return (B1>>MnBitN) & (TB1Def::B1Def.B1P2T[MxBitN-MnBitN+1]-1);}
00217 
00218   TB8Set& operator~(){B1=TB1Def::TB1(~B1); return *this;}
00219   TB8Set& operator&=(const TB8Set& BSet){B1&=BSet.B1; return *this;}
00220   TB8Set& operator|=(const TB8Set& BSet){B1|=BSet.B1; return *this;}
00221   TB8Set& operator|=(const int& BitN){Incl(BitN); return *this;}
00222   TB8Set& operator^=(const TB8Set& BSet){B1^=BSet.B1; return *this;}
00223   TB8Set& operator>>=(const int& ShiftBits){B1>>=ShiftBits; return *this;}
00224   TB8Set& operator<<=(const int& ShiftBits){B1<<=ShiftBits; return *this;}
00225 
00226   friend TB8Set operator~(const TB8Set& BSet){
00227     return ~TB8Set(BSet);}
00228   friend TB8Set operator&(const TB8Set& LBSet, const TB8Set& RBSet){
00229     return TB8Set(LBSet)&=RBSet;}
00230   friend TB8Set operator|(const TB8Set& LBSet, const TB8Set& RBSet){
00231     return TB8Set(LBSet)|=RBSet;}
00232   friend TB8Set operator^(const TB8Set& LBSet, const TB8Set& RBSet){
00233     return TB8Set(LBSet)^=RBSet;}
00234 };
00235 typedef TVec<TB8Set> TB8SetV;
00236 
00238 // Bit32-Set
00239 class TB32Set{
00240 private:
00241   static const int Bits;
00242   TB4Def::TB4 B4;
00243 public:
00244   TB32Set(): B4(0){}
00245   TB32Set(const TB32Set& B32Set): B4(B32Set.B4){}
00246   TB32Set(const uint& _B4): B4(_B4){}
00247   TB32Set(TSIn& SIn){SIn.LoadBf(&B4, sizeof(TB4Def::TB4));}
00248   void Save(TSOut& SOut) const {SOut.SaveBf(&B4, sizeof(TB4Def::TB4));}
00249 
00250   TB32Set& operator=(const TB32Set& BSet){B4=BSet.B4; return *this;}
00251   bool operator==(const TB32Set& BSet) const {return B4==BSet.B4;}
00252   bool operator<(const TB32Set& BSet) const {return B4<BSet.B4;}
00253 
00254   bool Empty() const {return B4==0;}
00255   TB32Set& Clr(){B4=0; return *this;}
00256   TB32Set& Fill(){B4=TB4Def::B4Def.MxB4; return *this;}
00257   bool IsPrefix(const TB32Set& BSet, const int& MnBitN) const {
00258     Assert((0<=MnBitN)&&(MnBitN<Bits));
00259     return (B4>>MnBitN)==(BSet.B4>>MnBitN);}
00260   uint GetUInt() const {return B4;}
00261 
00262   void Incl(const int& BitN){
00263     Assert((0<=BitN)&&(BitN<Bits));
00264     B4|=TB4Def::B4Def.B4P2T[BitN];}
00265   void Excl(const int& BitN){
00266     Assert((0<=BitN)&&(BitN<Bits));
00267     B4&=~TB4Def::B4Def.B4P2T[BitN];}
00268   bool In(const int& BitN) const {
00269     Assert((0<=BitN)&&(BitN<Bits));
00270     return (B4 & TB4Def::B4Def.B4P2T[BitN])!=0;}
00271   void SetBit(const int& BitN, const bool& Bool){
00272     if (Bool) Incl(BitN); else Excl(BitN);}
00273   bool GetBit(const int& BitN) const {
00274     Assert((0<=BitN)&&(BitN<Bits));
00275     return (B4 & TB4Def::B4Def.B4P2T[BitN])!=0;}
00276   void SwitchBit(const int& BitN){
00277     SetBit(BitN, !GetBit(BitN));}
00278   int GetBits() const {return Bits;}
00279   int Get1s() const {return TB4Def::B4Def.GetB4Bits(B4);}
00280   int Get0s() const {return Bits-Get1s();}
00281   TStr GetStr() const;
00282   void Wr();
00283 
00284   void PutInt(const int& MnBitN, const int& MxBitN, const int& Val){
00285     Assert((0<=MnBitN)&&(MnBitN<=MxBitN)&&(MxBitN<Bits));
00286     B4 &= (~(TB4Def::B4Def.B4P2T[MxBitN-MnBitN+1]-1)) << MnBitN;
00287     B4 |= (Val & (TB4Def::B4Def.B4P2T[MxBitN-MnBitN+1]-1)) << MnBitN;}
00288   int GetInt(const int& MnBitN, const int& MxBitN) const {
00289     Assert((0<=MnBitN)&&(MnBitN<=MxBitN)&&(MxBitN<Bits));
00290     return (B4>>MnBitN) & (TB4Def::B4Def.B4P2T[MxBitN-MnBitN+1]-1);}
00291 
00292   TB32Set& operator~(){B4=~B4; return *this;}
00293   TB32Set& operator&=(const TB32Set& BSet){B4&=BSet.B4; return *this;}
00294   TB32Set& operator|=(const TB32Set& BSet){B4|=BSet.B4; return *this;}
00295   //TB32Set& operator|=(const int& BitN){Incl(BitN); return *this;}
00296   TB32Set& operator^=(const TB32Set& BSet){B4^=BSet.B4; return *this;}
00297   TB32Set& operator>>=(const int& ShiftBits){B4>>=ShiftBits; return *this;}
00298   TB32Set& operator<<=(const int& ShiftBits){B4<<=ShiftBits; return *this;}
00299 
00300   friend TB32Set operator~(const TB32Set& BSet){
00301     return ~TB32Set(BSet);}
00302   friend TB32Set operator&(const TB32Set& LBSet, const TB32Set& RBSet){
00303     return TB32Set(LBSet)&=RBSet;}
00304   friend TB32Set operator|(const TB32Set& LBSet, const TB32Set& RBSet){
00305     return TB32Set(LBSet)|=RBSet;}
00306   friend TB32Set operator^(const TB32Set& LBSet, const TB32Set& RBSet){
00307     return TB32Set(LBSet)^=RBSet;}
00308 };
00309 typedef TVec<TB32Set> TB32SetV;
00310 
00312 // Bit-Set
00313 ClassTPV(TBSet, PBSet, TBSetV)//{
00314 private:
00315   int B4s, Bits;
00316   TB4Def::TB4 LastB4Mask;
00317   TB4Def::TB4* B4T;
00318   void SetLastB4(){B4T[B4s-1]&=LastB4Mask;}
00319 public:
00320   TBSet(): B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){}
00321   TBSet(const TBSet& BSet);
00322   PBSet Clone() const {return PBSet(new TBSet(*this));}
00323   TBSet(const int& _Bits):
00324     B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){Gen(_Bits);}
00325   static PBSet New(const int& Bits){return PBSet(new TBSet(Bits));}
00326   ~TBSet(){delete[] B4T;}
00327   TBSet(TSIn& SIn){
00328     SIn.Load(B4s); SIn.Load(Bits);
00329     SIn.LoadBf(&LastB4Mask, sizeof(TB4Def::TB4));
00330     B4T=(TB4Def::TB4*)SIn.LoadNewBf(B4s*sizeof(TB4Def::TB4));}
00331   static PBSet Load(TSIn& SIn){return new TBSet(SIn);}
00332   void Save(TSOut& SOut) const {
00333     SOut.Save(B4s); SOut.Save(Bits);
00334     SOut.SaveBf(&LastB4Mask, sizeof(TB4Def::TB4));
00335     SOut.SaveBf(B4T, B4s*sizeof(TB4Def::TB4));}
00336 
00337   TBSet& operator=(const TBSet& BSet);
00338   bool operator==(const TBSet& BSet) const;
00339 
00340   void Gen(const int& _Bits);
00341   void Clr();
00342   void Fill();
00343 
00344   void Incl(const int& BitN){
00345     Assert((0<=BitN)&&(BitN<Bits));
00346     B4T[BitN/TB4Def::B4Def.B4Bits]|=
00347      TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits];}
00348   void Excl(const int& BitN){
00349     Assert((0<=BitN)&&(BitN<Bits));
00350     B4T[BitN/TB4Def::B4Def.B4Bits]&=
00351      ~TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits];}
00352   bool In(const int& BitN) const {
00353     Assert((0<=BitN)&&(BitN<Bits));
00354     return (B4T[BitN/TB4Def::B4Def.B4Bits] &
00355      TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits])!=0;}
00356   void SetBit(const int& BitN, const bool& Bool){
00357     if (Bool){Incl(BitN);} else {Excl(BitN);}}
00358   bool GetBit(const int& BitN) const {
00359     Assert((0<=BitN)&&(BitN<Bits));
00360     return (B4T[BitN/TB4Def::B4Def.B4Bits] &
00361      TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits])!=0;}
00362   void SwitchBit(const int& BitN){
00363     SetBit(BitN, !GetBit(BitN));}
00364   int GetBits() const {return Bits;}
00365   int Get1s();
00366   int Get0s(){return Bits-Get1s();}
00367   uint64 GetUInt64() const {
00368     Assert(Bits>=64); uint64 Val; memcpy(&Val, B4T, 8); return Val;}
00369   void Wr();
00370 
00371   TBSet& operator~(){
00372     for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=~B4T[B4N];} return *this;}
00373   TBSet& operator&=(const TBSet& BSet){
00374     Assert(B4s==BSet.B4s);
00375     for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]&=BSet.B4T[B4N];} return *this;}
00376   TBSet& operator|=(const TBSet& BSet){
00377     Assert(B4s==BSet.B4s);
00378     for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]|=BSet.B4T[B4N];} return *this;}
00379   TBSet& operator|=(const int& BitN){
00380     Incl(BitN); return *this;}
00381   TBSet& operator^=(const TBSet& BSet){
00382     Assert(B4s==BSet.B4s);
00383     for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]^=BSet.B4T[B4N];} return *this;}
00384 
00385   friend TBSet operator~(const TBSet& BSet){
00386     return ~TBSet(BSet);}
00387   friend TBSet operator&(const TBSet& LBSet, const TBSet& RBSet){
00388     return TBSet(LBSet)&=RBSet;}
00389   friend TBSet operator|(const TBSet& LBSet, const TBSet& RBSet){
00390     return TBSet(LBSet)|=RBSet;}
00391   friend TBSet operator^(const TBSet& LBSet, const TBSet& RBSet){
00392     return TBSet(LBSet)^=RBSet;}
00393 
00394   friend TBSet operator&(const TBSet& LBSet, const int& BitN){
00395     return TBSet(LBSet)&=BitN;}
00396   friend TBSet operator|(const TBSet& LBSet, const int& BitN){
00397     return TBSet(LBSet)|=BitN;}
00398   friend TBSet operator^(const TBSet& LBSet, const int& BitN){
00399     return TBSet(LBSet)^=BitN;}
00400 };