Return the approximated cardinality of the set based on the harmonic * mean of the registers values. 'hdr' points to the start of the SDS * representing the String object holding the HLL representation. * * If the sparse representation of the HLL object is not valid, the integer * pointed by 'invalid' is set to non-zero, otherwise it is left untouched. * * hllCount() supports a special inte
| 1019 | * This is useful in order to speedup PFCOUNT when called against multiple |
| 1020 | * keys (no need to work with 6-bit integers encoding). */ |
| 1021 | uint64_t hllCount(struct hllhdr *hdr, int *invalid) { |
| 1022 | double m = HLL_REGISTERS; |
| 1023 | double E; |
| 1024 | int j; |
| 1025 | /* Note that reghisto size could be just HLL_Q+2, because HLL_Q+1 is |
| 1026 | * the maximum frequency of the "000...1" sequence the hash function is |
| 1027 | * able to return. However it is slow to check for sanity of the |
| 1028 | * input: instead we history array at a safe size: overflows will |
| 1029 | * just write data to wrong, but correctly allocated, places. */ |
| 1030 | int reghisto[64] = {0}; |
| 1031 | |
| 1032 | /* Compute register histogram */ |
| 1033 | if (hdr->encoding == HLL_DENSE) { |
| 1034 | hllDenseRegHisto(hdr->registers(),reghisto); |
| 1035 | } else if (hdr->encoding == HLL_SPARSE) { |
| 1036 | hllSparseRegHisto(hdr->registers(), |
| 1037 | sdslen((sds)hdr)-HLL_HDR_SIZE,invalid,reghisto); |
| 1038 | } else if (hdr->encoding == HLL_RAW) { |
| 1039 | hllRawRegHisto(hdr->registers(),reghisto); |
| 1040 | } else { |
| 1041 | serverPanic("Unknown HyperLogLog encoding in hllCount()"); |
| 1042 | } |
| 1043 | |
| 1044 | /* Estimate cardinality form register histogram. See: |
| 1045 | * "New cardinality estimation algorithms for HyperLogLog sketches" |
| 1046 | * Otmar Ertl, arXiv:1702.01284 */ |
| 1047 | double z = m * hllTau((m-reghisto[HLL_Q+1])/(double)m); |
| 1048 | for (j = HLL_Q; j >= 1; --j) { |
| 1049 | z += reghisto[j]; |
| 1050 | z *= 0.5; |
| 1051 | } |
| 1052 | z += m * hllSigma(reghisto[0]/(double)m); |
| 1053 | E = llroundl(HLL_ALPHA_INF*m*m/z); |
| 1054 | |
| 1055 | return (uint64_t) E; |
| 1056 | } |
| 1057 | |
| 1058 | /* Call hllDenseAdd() or hllSparseAdd() according to the HLL encoding. */ |
| 1059 | int hllAdd(robj *o, unsigned char *ele, size_t elesize) { |
no test coverage detected