Minor HLL fixes
This commit is contained in:
Vendored
+50
-43
@@ -82,21 +82,26 @@ int hll_init(struct ndpi_hll *hll, u_int8_t bits) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void hll_destroy(struct ndpi_hll *hll) {
|
void hll_destroy(struct ndpi_hll *hll) {
|
||||||
ndpi_free(hll->registers);
|
if(hll->registers) {
|
||||||
|
ndpi_free(hll->registers);
|
||||||
hll->registers = NULL;
|
|
||||||
|
hll->registers = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hll_reset(struct ndpi_hll *hll) {
|
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) {
|
static __inline void _hll_add_hash(struct ndpi_hll *hll, u_int32_t hash) {
|
||||||
u_int32_t index = hash >> (32 - hll->bits);
|
if(hll->registers) {
|
||||||
u_int8_t rank = _hll_rank(hash, hll->bits);
|
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(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 hll_count(const struct ndpi_hll *hll) {
|
||||||
double alpha_mm;
|
if(hll->registers) {
|
||||||
u_int32_t i;
|
double alpha_mm, sum, estimate;
|
||||||
|
u_int32_t i;
|
||||||
|
|
||||||
switch (hll->bits) {
|
switch(hll->bits) {
|
||||||
case 4:
|
case 4:
|
||||||
alpha_mm = 0.673;
|
alpha_mm = 0.673;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
alpha_mm = 0.697;
|
alpha_mm = 0.697;
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
alpha_mm = 0.709;
|
alpha_mm = 0.709;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
alpha_mm = 0.7213 / (1.0 + 1.079 / (double)hll->size);
|
alpha_mm = 0.7213 / (1.0 + 1.079 / (double)hll->size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
alpha_mm *= ((double)hll->size * (double)hll->size);
|
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;
|
|
||||||
|
|
||||||
|
sum = 0;
|
||||||
for(i = 0; i < hll->size; i++)
|
for(i = 0; i < hll->size; i++)
|
||||||
zeros += (hll->registers[i] == 0);
|
sum += 1.0 / (1 << hll->registers[i]);
|
||||||
|
|
||||||
if(zeros)
|
estimate = alpha_mm / sum;
|
||||||
estimate = (double)hll->size * log((double)hll->size / zeros);
|
|
||||||
|
|
||||||
} else if (estimate > (1.0 / 30.0) * 4294967296.0) {
|
if(estimate <= (5.0 / 2.0 * (double)hll->size)) {
|
||||||
estimate = -4294967296.0 * log(1.0 - (estimate / 4294967296.0));
|
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.);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user