-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitVec.c
167 lines (125 loc) · 3.09 KB
/
BitVec.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BitVec.h"
#include "GaussCoef_BitVec.h"
static int constrain_ext(int n, int min, int max){
if(n<min)
return min;
if(n>max)
return max;
return n;
}
typedef struct BitVec{
uint8_t *arr;
int sz;
} BitVec;
BitVec *new_BitVec(int sz){
BitVec *bvptr = malloc(sizeof(BitVec));
int arrsz = (sz+7)/8;
bvptr->arr = malloc(arrsz*sizeof(uint8_t));
memset(bvptr->arr, 0, arrsz*sizeof(uint8_t));
bvptr->sz = sz;
return bvptr;
}
BitVec *duplicate_BitVec(BitVec *bvptr){
BitVec *dpptr = malloc(sizeof(BitVec));
int arrsz = (bvptr->sz+7)/8;
dpptr->arr = malloc(arrsz*sizeof(uint8_t));
memcpy(dpptr->arr, bvptr->arr, arrsz*sizeof(uint8_t));
dpptr->sz = bvptr->sz;
return dpptr;
}
void copy_BitVec(BitVec *src, BitVec *dest){
int sz = src->sz;
int arrsz = (sz+7)/8;
dest->sz = sz;
dest->arr = realloc(dest->arr, arrsz*sizeof(float));
memcpy(dest->arr, src->arr, arrsz*sizeof(float));
}
void free_BitVec(BitVec *bvptr){
free(bvptr->arr);
free(bvptr);
}
int nSet_BitVec(BitVec *bvptr){
int arrsz = (bvptr->sz+7)/8;
int nSet = 0;
for(int i=0; i<arrsz; i++){
for(int j=1; j<256; j<<=1){
if( (bvptr->arr)[i] & j )
nSet++;
}
}
return nSet;
}
void setBit_BitVec(BitVec *bvptr, int idx, int val){
int arridx = idx/8;
int bitidx = idx%8;
if(val==0)
(bvptr->arr)[arridx] &= ~(1<<bitidx);
else if(val==1)
(bvptr->arr)[arridx] |= 1<<bitidx;
}
void setAll_BitVec(BitVec *bvptr, int val){
int arrsz = (bvptr->sz+7)/8;
if(val==0)
memset(bvptr->arr, 0, arrsz*sizeof(uint8_t));
else if(val==1){
memset(bvptr->arr, 255, arrsz*sizeof(uint8_t));
(bvptr->arr)[arrsz-1] &= 255<<(bvptr->sz%8); // Set bits out of bound to zero
}
}
int getBit_BitVec(BitVec *bvptr, int idx){
int val;
int arridx = idx/8;
int bitidx = idx%8;
val = bvptr->arr[arridx] & 1<<bitidx;
if(val==0)
return 0;
else
return 1;
}
int getSize_BitVec(BitVec *bvptr){
return bvptr->sz;
}
// Check for val at and after iter position
int hasNext_BitVec(BitVec *bvptr, int val, int iter){
for(int i=iter; i<bvptr->sz; i++)
if(getBit_BitVec(bvptr, i)==val)
return 1;
return 0;
}
// Returns idx and len of next contiguous block of vals
void getNext_BitVec(BitVec *bvptr, int val, int *iter, int *idx, int *len){
while(getBit_BitVec(bvptr,*iter)!=val)
(*iter)++;
*idx = *iter;
while(getBit_BitVec(bvptr,*iter)==val && *iter<bvptr->sz)
(*iter)++;
*len = *iter-*idx;
}
void gaussBlur_BitVec(BitVec *bvptr, int rad, float th){
float val;
int sz = bvptr->sz;
BitVec *tbvptr = new_BitVec(sz);
for(int i=0; i<bvptr->sz; i++){
val = 0;
for(int j=i-rad; j<=i+rad; j++)
if(j>-1 && j<sz)
val += getBit_BitVec(bvptr,j)*GaussCoef_BitVec[rad][j-i+rad];
if(val>th)
setBit_BitVec(tbvptr, i, 1);
}
memcpy(bvptr->arr, tbvptr->arr, (bvptr->sz+7)/8);
free_BitVec(tbvptr);
}
void toggleMaxLen(BitVec *bvptr, int val, int maxlen){
int idx, len;
int iter = 0;
while(hasNext_BitVec(bvptr,val,iter)){
getNext_BitVec(bvptr,val, &iter, &idx, &len);
if(len<=maxlen)
for(int i=idx; i<idx+len; i++)
setBit_BitVec(bvptr,i,!val);
}
}