2021-07-12 05:00:10 +03:00
|
|
|
// This file implements HyperLogLog algorithm, which estimates
|
|
|
|
// the number of unique items in a given multiset.
|
|
|
|
//
|
|
|
|
// For more info, read
|
|
|
|
// https://engineering.fb.com/2018/12/13/data-infrastructure/hyperloglog
|
|
|
|
|
|
|
|
#include "mold.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2021-09-02 12:19:43 +03:00
|
|
|
namespace mold::elf {
|
|
|
|
|
2021-07-12 05:00:10 +03:00
|
|
|
i64 HyperLogLog::get_cardinality() const {
|
|
|
|
double z = 0;
|
|
|
|
for (i64 val : buckets)
|
|
|
|
z += pow(2, -val);
|
|
|
|
return ALPHA * NBUCKETS * NBUCKETS / z;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HyperLogLog::merge(const HyperLogLog &other) {
|
|
|
|
for (i64 i = 0; i < NBUCKETS; i++)
|
|
|
|
merge_one(i, other.buckets[i]);
|
|
|
|
}
|
2021-09-02 12:19:43 +03:00
|
|
|
|
|
|
|
} // namespace mold::elf
|