@@ -32,21 +32,8 @@ inline uint32_t reduce(uint32_t hash, uint32_t n) {
3232}
3333
3434size_t getHashFromHash (uint64_t hash, int index, int blockLength) {
35- uint32_t r;
36- switch (index) {
37- case 0 :
38- r = (uint32_t ) (hash);
39- break ;
40- case 1 :
41- r = (uint32_t ) rotl64 (hash, 21 );
42- break ;
43- default :
44- r = (uint32_t ) rotl64 (hash, 42 );
45- break ;
46- }
47- r = reduce (r, blockLength);
48- r = r + index * blockLength;
49- return (size_t ) r;
35+ uint32_t r = rotl64 (hash, index * 21 );
36+ return (size_t ) reduce (r, blockLength) + index * blockLength;
5037}
5138
5239template <typename ItemType, typename FingerprintType,
@@ -68,7 +55,7 @@ class XorFilter {
6855 explicit XorFilter (const size_t size) {
6956 hasher = new HashFamily ();
7057 this ->size = size;
71- this ->arrayLength = 3 + 1.23 * size;
58+ this ->arrayLength = 32 + 1.23 * size;
7259 this ->blockLength = arrayLength / 3 ;
7360 fingerprints = new FingerprintType[arrayLength]();
7461 std::fill_n (fingerprints, arrayLength, 0 );
@@ -154,41 +141,40 @@ Status XorFilter<ItemType, FingerprintType, HashFamily>::AddAll(
154141 delete[] tmpc;
155142
156143 reverseOrderPos = 0 ;
144+
157145 int * alone = new int [arrayLength];
158146 int alonePos = 0 ;
147+ for (int i = 0 ; i < arrayLength; i++) {
148+ if (t2vals[i].t2count == 1 ) {
149+ alone[alonePos++] = i;
150+ }
151+ }
159152 reverseOrderPos = 0 ;
160- for (size_t nextAloneCheck = 0 ; nextAloneCheck < arrayLength;) {
161- while (nextAloneCheck < arrayLength) {
162- if (t2vals[nextAloneCheck].t2count == 1 ) {
163- alone[alonePos++] = nextAloneCheck;
164- }
165- nextAloneCheck++;
153+ while (alonePos > 0 && reverseOrderPos < size) {
154+ int i = alone[--alonePos];
155+ if (t2vals[i].t2count == 0 ) {
156+ continue ;
166157 }
167- while (alonePos > 0 ) {
168- int i = alone[--alonePos];
169- if (t2vals[i].t2count == 0 ) {
170- continue ;
171- }
172- long hash = t2vals[i].t2 ;
173- uint8_t found = -1 ;
174- for (int hi = 0 ; hi < 3 ; hi++) {
175- int h = getHashFromHash (hash, hi, blockLength);
176- int newCount = -- t2vals[h].t2count ;
177- if (newCount == 0 ) {
178- found = (uint8_t ) hi;
179- } else {
180- if (newCount == 1 ) {
181- alone[alonePos++] = h;
182- }
183- t2vals[h].t2 ^= hash;
158+ long hash = t2vals[i].t2 ;
159+ uint8_t found = -1 ;
160+ for (int hi = 0 ; hi < 3 ; hi++) {
161+ int h = getHashFromHash (hash, hi, blockLength);
162+ int newCount = --t2vals[h].t2count ;
163+ if (newCount == 0 ) {
164+ found = (uint8_t ) hi;
165+ } else {
166+ if (newCount == 1 ) {
167+ alone[alonePos++] = h;
184168 }
169+ t2vals[h].t2 ^= hash;
185170 }
186- reverseOrder[reverseOrderPos] = hash;
187- reverseH[reverseOrderPos] = found;
188- reverseOrderPos++;
189171 }
172+ reverseOrder[reverseOrderPos] = hash;
173+ reverseH[reverseOrderPos] = found;
174+ reverseOrderPos++;
190175 }
191176 delete [] alone;
177+
192178 if (reverseOrderPos == size) {
193179 break ;
194180 }
0 commit comments