* Initialize HyperLogLog track state, by bit width * * bwidth is bit width (so register size will be 2 to the power of bwidth). * Must be between 4 and 16 inclusive. */
| 63 | * Must be between 4 and 16 inclusive. |
| 64 | */ |
| 65 | void |
| 66 | initHyperLogLog(hyperLogLogState *cState, uint8 bwidth) |
| 67 | { |
| 68 | double alpha; |
| 69 | |
| 70 | if (bwidth < 4 || bwidth > 16) |
| 71 | elog(ERROR, "bit width must be between 4 and 16 inclusive"); |
| 72 | |
| 73 | cState->registerWidth = bwidth; |
| 74 | cState->nRegisters = (Size) 1 << bwidth; |
| 75 | cState->arrSize = sizeof(uint8) * cState->nRegisters + 1; |
| 76 | |
| 77 | /* |
| 78 | * Initialize hashes array to zero, not negative infinity, per discussion |
| 79 | * of the coupon collector problem in the HyperLogLog paper |
| 80 | */ |
| 81 | cState->hashesArr = palloc0(cState->arrSize); |
| 82 | |
| 83 | /* |
| 84 | * "alpha" is a value that for each possible number of registers (m) is |
| 85 | * used to correct a systematic multiplicative bias present in m ^ 2 Z (Z |
| 86 | * is "the indicator function" through which we finally compute E, |
| 87 | * estimated cardinality). |
| 88 | */ |
| 89 | switch (cState->nRegisters) |
| 90 | { |
| 91 | case 16: |
| 92 | alpha = 0.673; |
| 93 | break; |
| 94 | case 32: |
| 95 | alpha = 0.697; |
| 96 | break; |
| 97 | case 64: |
| 98 | alpha = 0.709; |
| 99 | break; |
| 100 | default: |
| 101 | alpha = 0.7213 / (1.0 + 1.079 / cState->nRegisters); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Precalculate alpha m ^ 2, later used to generate "raw" HyperLogLog |
| 106 | * estimate E |
| 107 | */ |
| 108 | cState->alphaMM = alpha * cState->nRegisters * cState->nRegisters; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Initialize HyperLogLog track state, by error rate |
no test coverage detected