Skip to content

Commit abbadfa

Browse files
committed
Don't use #define
1 parent 3ded030 commit abbadfa

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/xorfilter.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,21 @@ struct t2val {
8989

9090
typedef struct t2val t2val_t;
9191

92-
#define BLOCK_SHIFT 18
93-
#define BLOCK_LEN (1 << BLOCK_SHIFT)
92+
const int blockShift = 18;
9493

9594
void applyBlock(uint64_t* tmp, int b, int len, t2val_t * t2vals) {
9695
for (int i = 0; i < len; i += 2) {
97-
uint64_t x = tmp[(b << BLOCK_SHIFT) + i];
98-
int index = (int) tmp[(b << BLOCK_SHIFT) + i + 1];
96+
uint64_t x = tmp[(b << blockShift) + i];
97+
int index = (int) tmp[(b << blockShift) + i + 1];
9998
t2vals[index].t2count++;
10099
t2vals[index].t2 ^= x;
101100
}
102101
}
103102

104103
int applyBlock2(uint64_t* tmp, int b, int len, t2val_t * t2vals, int* alone, int alonePos) {
105104
for (int i = 0; i < len; i += 2) {
106-
uint64_t hash = tmp[(b << BLOCK_SHIFT) + i];
107-
int index = (int) tmp[(b << BLOCK_SHIFT) + i + 1];
105+
uint64_t hash = tmp[(b << blockShift) + i];
106+
int index = (int) tmp[(b << blockShift) + i + 1];
108107
int oldCount = t2vals[index].t2count;
109108
if (oldCount >= 1) {
110109
int newCount = oldCount - 1;
@@ -131,20 +130,20 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
131130
t2val_t * t2vals = new t2val_t[m];
132131
while (true) {
133132
memset(t2vals, 0, sizeof(t2val_t[m]));
134-
int blocks = 1 + (3 * blockLength) / BLOCK_LEN;
135-
uint64_t* tmp = new uint64_t[blocks * BLOCK_LEN];
133+
int blocks = 1 + ((3 * blockLength) >> blockShift);
134+
uint64_t* tmp = new uint64_t[blocks << blockShift];
136135
int* tmpc = new int[blocks]();
137136
for(size_t i = start; i < end; i++) {
138137
uint64_t k = keys[i];
139138
uint64_t hash = (*hasher)(k);
140139
for (int hi = 0; hi < 3; hi++) {
141140
int index = getHashFromHash(hash, hi, blockLength);
142-
int b = index >> BLOCK_SHIFT;
141+
int b = index >> blockShift;
143142
int i2 = tmpc[b];
144-
tmp[(b << BLOCK_SHIFT) + i2] = hash;
145-
tmp[(b << BLOCK_SHIFT) + i2 + 1] = index;
143+
tmp[(b << blockShift) + i2] = hash;
144+
tmp[(b << blockShift) + i2 + 1] = index;
146145
tmpc[b] += 2;
147-
if (i2 + 2 == BLOCK_LEN) {
146+
if (i2 + 2 == (1 << blockShift)) {
148147
applyBlock(tmp, b, i2 + 2, t2vals);
149148
tmpc[b] = 0;
150149
}
@@ -165,7 +164,7 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
165164
alone[alonePos++] = i;
166165
}
167166
}
168-
tmp = new uint64_t[blocks * BLOCK_LEN];
167+
tmp = new uint64_t[blocks << blockShift];
169168
tmpc = new int[blocks]();
170169
reverseOrderPos = 0;
171170
int bestBlock = -1;
@@ -184,7 +183,7 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
184183
if (tmpc[b] > best) {
185184
best = tmpc[b];
186185
bestBlock = b;
187-
if (best > BLOCK_LEN / 2) {
186+
if (best > (1 << (blockShift - 1))) {
188187
// sufficiently large: stop
189188
break;
190189
}
@@ -208,7 +207,7 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
208207
break;
209208
}
210209
int i = alone[--alonePos];
211-
int b = i >> BLOCK_SHIFT;
210+
int b = i >> blockShift;
212211
if (tmpc[b] > 0) {
213212
alonePos = applyBlock2(tmp, b, tmpc[b], t2vals, alone, alonePos);
214213
tmpc[b] = 0;
@@ -224,12 +223,12 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
224223
found = (uint8_t) hi;
225224
t2vals[i].t2count = 0;
226225
} else {
227-
int b = h >> BLOCK_SHIFT;
226+
int b = h >> blockShift;
228227
int i2 = tmpc[b];
229-
tmp[(b << BLOCK_SHIFT) + i2] = hash;
230-
tmp[(b << BLOCK_SHIFT) + i2 + 1] = h;
228+
tmp[(b << blockShift) + i2] = hash;
229+
tmp[(b << blockShift) + i2 + 1] = h;
231230
tmpc[b] += 2;
232-
if (tmpc[b] >= BLOCK_LEN) {
231+
if (tmpc[b] >= 1 << blockShift) {
233232
alonePos = applyBlock2(tmp, b, tmpc[b], t2vals, alone, alonePos);
234233
tmpc[b] = 0;
235234
}
@@ -241,9 +240,10 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
241240
}
242241
delete[] tmp;
243242
delete[] tmpc;
244-
delete [] alone;
243+
delete[] alone;
245244

246245
/*
246+
247247
int* alone = new int[arrayLength];
248248
int alonePos = 0;
249249
for (size_t i = 0; i < arrayLength; i++) {
@@ -296,6 +296,7 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
296296
hasher = new HashFamily();
297297

298298
}
299+
299300
for (int i = reverseOrderPos - 1; i >= 0; i--) {
300301
// the hash of the key we insert next
301302
uint64_t hash = reverseOrder[i];
@@ -320,6 +321,7 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
320321
delete [] t2vals;
321322
delete [] reverseOrder;
322323
delete [] reverseH;
324+
323325
return Ok;
324326
}
325327

0 commit comments

Comments
 (0)