Skip to content

Commit 7c672d5

Browse files
committed
Slightly simplify the code
1 parent 3577124 commit 7c672d5

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

src/xorfilter.h

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,8 @@ inline uint32_t reduce(uint32_t hash, uint32_t n) {
3535
}
3636

3737
size_t getHashFromHash(uint64_t hash, int index, int blockLength) {
38-
uint32_t r;
39-
switch(index) {
40-
case 0:
41-
r = (uint32_t) (hash);
42-
break;
43-
case 1:
44-
r = (uint32_t) rotl64(hash, 21);
45-
break;
46-
default:
47-
r = (uint32_t) rotl64(hash, 42);
48-
break;
49-
}
50-
r = reduce(r, blockLength);
51-
r = r + index * blockLength;
52-
return (size_t) r;
38+
uint32_t r = rotl64(hash, index * 21);
39+
return (size_t) reduce(r, blockLength) + index * blockLength;
5340
}
5441

5542
template <typename ItemType, typename FingerprintType,
@@ -167,42 +154,40 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
167154
delete[] tmpc;
168155

169156
reverseOrderPos = 0;
157+
170158
int* alone = new int[arrayLength];
171159
int alonePos = 0;
160+
for (int i = 0; i < arrayLength; i++) {
161+
if (t2vals[i].t2count == 1) {
162+
alone[alonePos++] = i;
163+
}
164+
}
172165
reverseOrderPos = 0;
173-
for(size_t nextAloneCheck = 0; nextAloneCheck < arrayLength;) {
174-
while (nextAloneCheck < arrayLength) {
175-
if (t2vals[nextAloneCheck].t2count == 1) {
176-
alone[alonePos++] = nextAloneCheck;
177-
// break;
178-
}
179-
nextAloneCheck++;
166+
while (alonePos > 0 && reverseOrderPos < size) {
167+
int i = alone[--alonePos];
168+
if (t2vals[i].t2count == 0) {
169+
continue;
180170
}
181-
while (alonePos > 0) {
182-
int i = alone[--alonePos];
183-
if (t2vals[i].t2count == 0) {
184-
continue;
185-
}
186-
long hash = t2vals[i].t2;
187-
uint8_t found = -1;
188-
for (int hi = 0; hi < 3; hi++) {
189-
int h = getHashFromHash(hash, hi, blockLength);
190-
int newCount = -- t2vals[h].t2count;
191-
if (newCount == 0) {
192-
found = (uint8_t) hi;
193-
} else {
194-
if (newCount == 1) {
195-
alone[alonePos++] = h;
196-
}
197-
t2vals[h].t2 ^= hash;
171+
long hash = t2vals[i].t2;
172+
uint8_t found = -1;
173+
for (int hi = 0; hi < 3; hi++) {
174+
int h = getHashFromHash(hash, hi, blockLength);
175+
int newCount = --t2vals[h].t2count;
176+
if (newCount == 0) {
177+
found = (uint8_t) hi;
178+
} else {
179+
if (newCount == 1) {
180+
alone[alonePos++] = h;
198181
}
182+
t2vals[h].t2 ^= hash;
199183
}
200-
reverseOrder[reverseOrderPos] = hash;
201-
reverseH[reverseOrderPos] = found;
202-
reverseOrderPos++;
203184
}
185+
reverseOrder[reverseOrderPos] = hash;
186+
reverseH[reverseOrderPos] = found;
187+
reverseOrderPos++;
204188
}
205189
delete [] alone;
190+
206191
if (reverseOrderPos == size) {
207192
break;
208193
}

0 commit comments

Comments
 (0)