Given a string element to add to the HyperLogLog, returns the length * of the pattern 000..1 of the element hash. As a side effect 'regp' is * set to the register index this element hashes to. */
| 451 | * of the pattern 000..1 of the element hash. As a side effect 'regp' is |
| 452 | * set to the register index this element hashes to. */ |
| 453 | int hllPatLen(unsigned char *ele, size_t elesize, long *regp) { |
| 454 | uint64_t hash, bit, index; |
| 455 | int count; |
| 456 | |
| 457 | /* Count the number of zeroes starting from bit HLL_REGISTERS |
| 458 | * (that is a power of two corresponding to the first bit we don't use |
| 459 | * as index). The max run can be 64-P+1 = Q+1 bits. |
| 460 | * |
| 461 | * Note that the final "1" ending the sequence of zeroes must be |
| 462 | * included in the count, so if we find "001" the count is 3, and |
| 463 | * the smallest count possible is no zeroes at all, just a 1 bit |
| 464 | * at the first position, that is a count of 1. |
| 465 | * |
| 466 | * This may sound like inefficient, but actually in the average case |
| 467 | * there are high probabilities to find a 1 after a few iterations. */ |
| 468 | hash = MurmurHash64A(ele,elesize,0xadc83b19ULL); |
| 469 | index = hash & HLL_P_MASK; /* Register index. */ |
| 470 | hash >>= HLL_P; /* Remove bits used to address the register. */ |
| 471 | hash |= ((uint64_t)1<<HLL_Q); /* Make sure the loop terminates |
| 472 | and count will be <= Q+1. */ |
| 473 | bit = 1; |
| 474 | count = 1; /* Initialized to 1 since we count the "00000...1" pattern. */ |
| 475 | while((hash & bit) == 0) { |
| 476 | count++; |
| 477 | bit <<= 1; |
| 478 | } |
| 479 | *regp = (int) index; |
| 480 | return count; |
| 481 | } |
| 482 | |
| 483 | /* ================== Dense representation implementation ================== */ |
| 484 |
no test coverage detected