SNAP Library 2.3, Developer Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bits.cpp
Go to the documentation of this file.
1 // One byte
3 const int TB1Def::B1Bits=8;
5 const TB1Def::TB1 TB1Def::MxB1=0xFF;
6 
8  B1P2T=new TB1[B1Bits+1]; B1P2T[0]=1;
9  for (int BitN=1; BitN<B1Bits; BitN++){B1P2T[BitN]=TB1(2*B1P2T[BitN-1]);}
10  B1P2T[B1Bits]=0;
11 
12  B1BitsT=new int[MxB1+1];
13  for (int B1N=0; B1N<MxB1+1; B1N++){
14  TB1 B1=(TB1)B1N; B1BitsT[B1]=0;
15  for (int BitN=0; BitN<B1Bits; BitN++){B1BitsT[B1N]+=B1%2; B1/=(TB1)2;}}
16 }
17 
18 int TB1Def::GetB1Bits(const TB1& B1){
19  return B1Def.B1BitsT[B1];
20 }
21 
22 uint TB1Def::GetP2(const int& P2Exp){
23  IAssert((0<=P2Exp)&&(P2Exp<=TB1Def::MxP2Exp));
24  return B1Def.B1P2T[P2Exp];
25 }
26 
27 int TB1Def::GetL2(const uchar& Val){
28  int L2=0;
29  while ((L2<TB1Def::MxP2Exp)&&(Val>=B1Def.B1P2T[L2])){L2++;}
30  return L2-1;
31 }
32 
33 bool TB1Def::GetBit(const int& BitN, const uchar& Val){
34  IAssert((0<=BitN)&&(BitN<=TB1Def::MxP2Exp));
35  return (Val & B1Def.B1P2T[BitN])!=0;
36 }
37 
38 const TB1Def TB1Def::B1Def;
39 
41 // Two bytes
42 const int TB2Def::B2Bits=16;
44 const TB2Def::TB2 TB2Def::MxB2=0xFFFF;
45 
47  B2P2T=new TB2[B2Bits+1]; B2P2T[0]=1;
48  for (int BitN=1; BitN<B2Bits; BitN++){B2P2T[BitN]=TB2(2*B2P2T[BitN-1]);}
49  B2P2T[B2Bits]=0;
50 }
51 
52 int TB2Def::GetB2Bits(const TB2& B2){
53  return
54  TB1Def::B1Def.B1BitsT[(B2>>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
55  TB1Def::B1Def.B1BitsT[(B2>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1];
56 }
57 
58 uint TB2Def::GetP2(const int& P2Exp){
59  IAssert((0<=P2Exp)&&(P2Exp<=TB2Def::MxP2Exp));
60  return B2Def.B2P2T[P2Exp];
61 }
62 
63 int TB2Def::GetL2(const TB2& Val){
64  int L2=0;
65  while ((L2<TB2Def::MxP2Exp)&&(Val>=B2Def.B2P2T[L2])){L2++;}
66  return L2-1;
67 }
68 
69 const TB2Def TB2Def::B2Def;
70 
72 // Four bytes
73 const int TB4Def::B4Bits=32;
75 const TB4Def::TB4 TB4Def::MxB4=0xFFFFFFFF;
76 
78  B4P2T=new TB4[B4Bits+1]; B4P2T[0]=1;
79  for (int BitN=1; BitN<B4Bits; BitN++){B4P2T[BitN]=TB4(2*B4P2T[BitN-1]);}
80  B4P2T[B4Bits]=0;
81 }
82 
83 int TB4Def::GetB4Bits(const TB4& B4){
84  return
85  TB1Def::B1Def.B1BitsT[(B4>>(0*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
86  TB1Def::B1Def.B1BitsT[(B4>>(1*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
87  TB1Def::B1Def.B1BitsT[(B4>>(2*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1]+
88  TB1Def::B1Def.B1BitsT[(B4>>(3*TB1Def::B1Def.B1Bits))&TB1Def::B1Def.MxB1];
89 }
90 
91 uint TB4Def::GetP2(const int& P2Exp){
92  IAssert((0<=P2Exp)&&(P2Exp<=TB4Def::MxP2Exp));
93  return B4Def.B4P2T[P2Exp];
94 }
95 
96 int TB4Def::GetL2(const uint& Val){
97  int L2=0;
98  while ((L2<TB4Def::MxP2Exp)&&(Val>=B4Def.B4P2T[L2])){L2++;}
99  return L2-1;
100 }
101 
102 const TB4Def TB4Def::B4Def;
103 
105 // Flag-Set
106 const int TFSet::B4s=4;
108 
110  const int& FlagN1, const int& FlagN2, const int& FlagN3,
111  const int& FlagN4, const int& FlagN5, const int& FlagN6,
112  const int& FlagN7, const int& FlagN8, const int& FlagN9):
113  B4V(4, 4){
114  if (FlagN1!=-1){Incl(FlagN1);}
115  if (FlagN2!=-1){Incl(FlagN2);}
116  if (FlagN3!=-1){Incl(FlagN3);}
117  if (FlagN4!=-1){Incl(FlagN4);}
118  if (FlagN5!=-1){Incl(FlagN5);}
119  if (FlagN6!=-1){Incl(FlagN6);}
120  if (FlagN7!=-1){Incl(FlagN7);}
121  if (FlagN8!=-1){Incl(FlagN8);}
122  if (FlagN9!=-1){Incl(FlagN9);}
123 }
124 
125 const TFSet TFSet::EmptyFSet;
126 
128 // Bit8-Set
129 const int TB8Set::Bits=8;
130 
132  TChA ChA;
133  for (int BitN=0; BitN<Bits; BitN++){
134  if (In(BitN)){ChA+='1';} else {ChA+='0';}}
135  return ChA;
136 }
137 
138 void TB8Set::Wr(){
139  printf("[");
140  for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
141  printf("]\n");
142 }
143 
145 // Bit32-Set
146 const int TB32Set::Bits=32;
147 
149  TChA ChA;
150  for (int BitN=0; BitN<Bits; BitN++){
151  if (In(BitN)){ChA+='1';} else {ChA+='0';}}
152  return ChA;
153 }
154 
155 void TB32Set::Wr(){
156  printf("[");
157  for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
158  printf("]\n");
159 }
160 
162 // Bit-Set
163 TBSet::TBSet(const TBSet& BSet):
164  B4s(0), Bits(0), LastB4Mask(0), B4T(NULL){
165  Gen(BSet.Bits);
166  for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=BSet.B4T[B4N];}
167 }
168 
170  if (this!=&BSet){
171  if (Bits!=BSet.Bits){Gen(BSet.Bits);}
172  for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=BSet.B4T[B4N];}}
173  return *this;
174 }
175 
176 bool TBSet::operator==(const TBSet& BSet) const {
177  if (Bits!=BSet.Bits){return false;}
178  for (int B4N=0; B4N<B4s-1; B4N++){if (B4T[B4N]!=BSet.B4T[B4N]){return false;}}
179  return (B4s>0)&&((B4T[B4s-1]&LastB4Mask)==(BSet.B4T[B4s-1]&BSet.LastB4Mask));
180 }
181 
182 void TBSet::Gen(const int& _Bits){
183  if (B4T!=NULL){delete[] B4T;}
184  Bits=_Bits;
185  B4T=new TB4Def::TB4[B4s=(Bits-1)/TB4Def::B4Def.B4Bits+1];
186  LastB4Mask=TB4Def::B4Def.MxB4;
187  for (int BitN=Bits; BitN<B4s*TB4Def::B4Def.B4Bits; BitN++){
188  LastB4Mask&= ~TB4Def::B4Def.B4P2T[BitN%TB4Def::B4Def.B4Bits];}
189  Clr();
190 }
191 
192 void TBSet::Clr(){
193  for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=0;}}
194 
195 void TBSet::Fill(){
196  for (int B4N=0; B4N<B4s; B4N++){B4T[B4N]=TB4Def::B4Def.MxB4;}}
197 
199  int Ones=0; SetLastB4();
200  for (int B4N=0; B4N<B4s; B4N++){Ones += TB4Def::B4Def.GetB4Bits(B4T[B4N]);}
201  return Ones;
202 }
203 
204 void TBSet::Wr(){
205  for (int BitN=0; BitN<Bits; BitN++){printf("%d", GetBit(BitN));}
206  //printf("\n");
207 }
208 
#define IAssert(Cond)
Definition: bd.h:262
static const int Bits
Definition: bits.h:168
void Wr()
Definition: bits.cpp:204
TStr GetStr() const
Definition: bits.cpp:148
static uint GetP2(const int &P2Exp)
Definition: bits.cpp:58
Definition: bits.h:5
Definition: bits.h:313
void Fill()
Definition: bits.cpp:195
void SetLastB4()
Definition: bits.h:318
Definition: bits.h:29
int * B1BitsT
Definition: bits.h:12
static const int MxP2Exp
Definition: bits.h:33
unsigned int uint
Definition: bd.h:11
void Clr()
Definition: bits.cpp:192
unsigned short int TB2
Definition: bits.h:31
Definition: bits.h:119
static const int Bits
Definition: bits.h:241
bool GetBit(const int &BitN) const
Definition: bits.h:273
void Wr()
Definition: bits.cpp:138
void Incl(const int &FlagN)
Definition: bits.h:149
TB1Def()
Definition: bits.cpp:7
static const int MxP2Exp
Definition: bits.h:55
TB4Def()
Definition: bits.cpp:77
static int GetL2(const uint &Val)
Definition: bits.cpp:96
static const int MxP2Exp
Definition: bits.h:9
TFSet()
Definition: bits.h:125
bool In(const int &BitN) const
Definition: bits.h:196
static const int B2Bits
Definition: bits.h:32
static int GetL2(const TB2 &Val)
Definition: bits.cpp:63
static const int B4s
Definition: bits.h:121
int Get1s()
Definition: bits.cpp:198
TB1 * B1P2T
Definition: bits.h:11
TBSet()
Definition: bits.h:320
bool GetBit(const int &BitN) const
Definition: bits.h:358
static uint GetP2(const int &P2Exp)
Definition: bits.cpp:22
static const TB4 MxB4
Definition: bits.h:56
static int GetB1Bits(const TB1 &B1)
Definition: bits.cpp:18
static bool GetBit(const int &BitN, const uchar &Val)
Definition: bits.cpp:33
TB4Def::TB4 LastB4Mask
Definition: bits.h:316
static const TB2 MxB2
Definition: bits.h:34
static int GetL2(const uchar &Val)
Definition: bits.cpp:27
bool In(const int &BitN) const
Definition: bits.h:268
unsigned char uchar
Definition: bd.h:10
void Gen(const int &_Bits)
Definition: bits.cpp:182
bool operator==(const TBSet &BSet) const
Definition: bits.cpp:176
static int GetB4Bits(const TB4 &B4)
Definition: bits.cpp:83
static const TB4Def B4Def
Definition: bits.h:68
int B4s
Definition: bits.h:315
static const TFSet EmptyFSet
Definition: bits.h:161
static int GetB2Bits(const TB2 &B2)
Definition: bits.cpp:52
Definition: dt.h:201
static const TB1 MxB1
Definition: bits.h:10
static const int B1Bits
Definition: bits.h:8
bool GetBit(const int &BitN) const
Definition: bits.h:201
uchar TB1
Definition: bits.h:7
static const TB1Def B1Def
Definition: bits.h:24
Definition: dt.h:412
Definition: bits.h:51
TB2 * B2P2T
Definition: bits.h:35
static const int Bits
Definition: bits.h:122
int Bits
Definition: bits.h:315
TB4Def::TB4 * B4T
Definition: bits.h:317
TBSet & operator=(const TBSet &BSet)
Definition: bits.cpp:169
uint TB4
Definition: bits.h:53
void Wr()
Definition: bits.cpp:155
TStr GetStr() const
Definition: bits.cpp:131
static uint GetP2(const int &P2Exp)
Definition: bits.cpp:91
static const int B4Bits
Definition: bits.h:54
TB2Def()
Definition: bits.cpp:46
TB4 * B4P2T
Definition: bits.h:57
static const TB2Def B2Def
Definition: bits.h:46