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