Minor HLL fixes

This commit is contained in:
Luca Deri
2020-07-22 14:40:15 +02:00
parent f83d0b18c6
commit 36af97a14c
+50 -43
View File
@@ -82,21 +82,26 @@ int hll_init(struct ndpi_hll *hll, u_int8_t bits) {
}
void hll_destroy(struct ndpi_hll *hll) {
ndpi_free(hll->registers);
hll->registers = NULL;
if(hll->registers) {
ndpi_free(hll->registers);
hll->registers = NULL;
}
}
void hll_reset(struct ndpi_hll *hll) {
memset(hll->registers, 0, hll->size);
if(hll->registers)
memset(hll->registers, 0, hll->size);
}
static __inline void _hll_add_hash(struct ndpi_hll *hll, u_int32_t hash) {
u_int32_t index = hash >> (32 - hll->bits);
u_int8_t rank = _hll_rank(hash, hll->bits);
if(rank > hll->registers[index]) {
hll->registers[index] = rank;
if(hll->registers) {
u_int32_t index = hash >> (32 - hll->bits);
u_int8_t rank = _hll_rank(hash, hll->bits);
if(rank > hll->registers[index]) {
hll->registers[index] = rank;
}
}
}
@@ -107,46 +112,48 @@ void hll_add(struct ndpi_hll *hll, const void *buf, size_t size) {
}
double hll_count(const struct ndpi_hll *hll) {
double alpha_mm;
u_int32_t i;
if(hll->registers) {
double alpha_mm, sum, estimate;
u_int32_t i;
switch (hll->bits) {
case 4:
alpha_mm = 0.673;
break;
case 5:
alpha_mm = 0.697;
break;
case 6:
alpha_mm = 0.709;
break;
default:
alpha_mm = 0.7213 / (1.0 + 1.079 / (double)hll->size);
break;
}
switch(hll->bits) {
case 4:
alpha_mm = 0.673;
break;
case 5:
alpha_mm = 0.697;
break;
case 6:
alpha_mm = 0.709;
break;
default:
alpha_mm = 0.7213 / (1.0 + 1.079 / (double)hll->size);
break;
}
alpha_mm *= ((double)hll->size * (double)hll->size);
double sum = 0;
for(i = 0; i < hll->size; i++) {
sum += 1.0 / (1 << hll->registers[i]);
}
double estimate = alpha_mm / sum;
if (estimate <= 5.0 / 2.0 * (double)hll->size) {
int zeros = 0;
alpha_mm *= ((double)hll->size * (double)hll->size);
sum = 0;
for(i = 0; i < hll->size; i++)
zeros += (hll->registers[i] == 0);
sum += 1.0 / (1 << hll->registers[i]);
if(zeros)
estimate = (double)hll->size * log((double)hll->size / zeros);
estimate = alpha_mm / sum;
} else if (estimate > (1.0 / 30.0) * 4294967296.0) {
estimate = -4294967296.0 * log(1.0 - (estimate / 4294967296.0));
}
if(estimate <= (5.0 / 2.0 * (double)hll->size)) {
int zeros = 0;
return estimate;
for(i = 0; i < hll->size; i++)
zeros += (hll->registers[i] == 0);
if(zeros)
estimate = (double)hll->size * log((double)hll->size / zeros);
} else if(estimate > ((1.0 / 30.0) * 4294967296.0)) {
estimate = -4294967296.0 * log(1.0 - (estimate / 4294967296.0));
}
return estimate;
} else
return(0.);
}