-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_vec.hpp
59 lines (54 loc) · 1.11 KB
/
bit_vec.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <math.h>
#include <algorithm>
using namespace std;
class bit_vec{
string bins(int a,int len)
{
string maps="01";
string r="";
for (;a!=0;a=a/2)
{
r=maps[a%2]+r;
}
while (r.size()!=len)
{r='0'+r;}
return r;
}
public :
int size;char len;
unsigned char *arr;
bit_vec(int *p,int siz)
{
int h=0;
for (int c(0);c<siz;c++)
{
h=max(h,*(p+c));
}
len=(unsigned char)((int)log2(h)+1);
size=ceil(len*siz/8.0);
arr=new unsigned char [size];
for (int c(0);c<size;c++)arr[c]=0;
int j=0;
for (int c(0);c<siz;c++)
{
string rr=bins(*(p+c),len);
for (int c1(0);c1<rr.size();c1++,j++)
{
arr[j/8]=(arr[j/8]|((rr[c1]-'0')<<(7-j%8)));
}
}
}
int size_in_bytes()
{
return size+5;
}
int ret(int i)
{
int res=0;
for (int c(len*i);c<=len*i+len-1;c++)
{
res=res*2+((arr[c/8]&(1<<(7-c%8)))!=0);
}
return res;
}
};