purely aesthetical - make whitespacing fit bro coding style.

Second step will be to change the bifs a bit...
This commit is contained in:
Bernhard Amann 2013-04-02 11:24:03 +02:00
parent b5cdf13469
commit fd51db1c89
2 changed files with 128 additions and 116 deletions

View file

@ -1,3 +1,5 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include "HyperLogLog.h" #include "HyperLogLog.h"
@ -5,129 +7,137 @@
using namespace std; using namespace std;
int CardinalityCounter::optimalB(double error){ int CardinalityCounter::optimalB(double error)
double initial_estimate = 2*(log(1.04)-log(error))/log(2); {
int answer = (int) floor(initial_estimate); double initial_estimate = 2*(log(1.04)-log(error))/log(2);
double k; int answer = (int) floor(initial_estimate);
double k;
do{ do
answer++; {
k = pow(2, (answer - initial_estimate)/2); answer++;
}while(erf(k/sqrt(2)) < conf); k = pow(2, (answer - initial_estimate)/2);
}
while (erf(k/sqrt(2)) < conf);
return answer; return answer;
} }
CardinalityCounter :: CardinalityCounter(uint64_t size){ CardinalityCounter::CardinalityCounter(uint64_t size)
m = size; {
buckets = new uint8_t[m]; m = size;
buckets = new uint8_t[m];
if(m == 16) if(m == 16)
alpha_m = 0.673; alpha_m = 0.673;
else if(m == 32) else if(m == 32)
alpha_m = 0.697; alpha_m = 0.697;
else if(m == 64) else if(m == 64)
alpha_m = 0.709; alpha_m = 0.709;
else else
alpha_m = 0.7213/(1+1.079/m); alpha_m = 0.7213/(1+1.079/m);
for(uint64_t i = 0; i < m; i++){ for (uint64_t i = 0; i < m; i++)
buckets[i] = 0; buckets[i] = 0;
}
V = m; V = m;
}
} CardinalityCounter :: CardinalityCounter(double error_margin)
{
int b = optimalB(error_margin);
m = (uint64_t) pow(2, b);
buckets = new uint8_t[m];
CardinalityCounter :: CardinalityCounter(double error_margin){ if(m == 16)
int b = optimalB(error_margin); alpha_m = 0.673;
m = (uint64_t) pow(2, b); else if(m == 32)
buckets = new uint8_t[m]; alpha_m = 0.697;
else if(m == 64)
alpha_m = 0.709;
else
alpha_m = 0.7213/(1+1.079/m);
if(m == 16) for (uint64_t i = 0; i < m; i++)
alpha_m = 0.673; buckets[i] = 0;
else if(m == 32)
alpha_m = 0.697;
else if(m == 64)
alpha_m = 0.709;
else
alpha_m = 0.7213/(1+1.079/m);
for(uint64_t i = 0; i < m; i++){ V = m;
buckets[i] = 0; }
}
V = m; CardinalityCounter::~CardinalityCounter()
} {
delete [] buckets;
delete &m;
delete &V;
delete &alpha_m;
}
CardinalityCounter :: ~CardinalityCounter(){ uint8_t CardinalityCounter::rank(uint64_t hash_modified)
delete [] buckets; {
delete &m; uint8_t answer = 0;
delete &V; hash_modified = (uint64_t)(hash_modified/m);
delete &alpha_m; hash_modified *= 2;
} do
{
hash_modified = (uint64_t) (hash_modified/2);
answer++;
}
while (hash_modified%2 == 0);
uint8_t CardinalityCounter :: rank(uint64_t hash_modified){ return answer;
uint8_t answer = 0; }
hash_modified = (uint64_t)(hash_modified/m);
hash_modified *= 2;
do{
hash_modified = (uint64_t) (hash_modified/2);
answer++;
}while(hash_modified%2 == 0);
return answer;
}
void CardinalityCounter::addElement(uint64_t hash)
{
uint64_t index = hash % m;
hash = hash-index;
if(buckets[index] == 0)
V--;
void CardinalityCounter::addElement(uint64_t hash){ uint8_t temp = rank(hash);
uint64_t index = hash % m;
hash = hash-index;
if(buckets[index] == 0) if (temp > buckets[index])
V--; buckets[index] = temp;
uint8_t temp = rank(hash); }
if(temp > buckets[index]){
buckets[index] = temp;
}
}
double CardinalityCounter::size(){ double CardinalityCounter::size()
double answer = 0; {
for(int i = 0; i < m; i++){ double answer = 0;
answer += pow(2, -(int)buckets[i]); for (int i = 0; i < m; i++)
} answer += pow(2, -(int)buckets[i]);
answer = 1/answer;
answer = alpha_m*m*m*answer;
if(answer <= 5*(double)(m/2)){ answer = 1/answer;
return m*log((double) m/V); answer = alpha_m*m*m*answer;
}
else if(answer <= pow(2,64)/30){
return answer;
}
else{
return -pow(2,64)*log(1-answer/pow(2,64));
}
}
void CardinalityCounter::merge(CardinalityCounter* c){ if (answer <= 5*(double)(m/2))
uint8_t* temp = (*c).getBuckets(); return m*log((double) m/V);
V = 0; else if(answer <= pow(2,64)/30)
for(int i = 0; i < m; i++){ return answer;
if(temp[i] > buckets[i]){ else
buckets[i] = temp[i]; return -pow(2,64)*log(1-answer/pow(2,64));
} }
if(buckets[i] == 0){
V += 1;
}
}
}
uint8_t* CardinalityCounter::getBuckets(){ void CardinalityCounter::merge(CardinalityCounter* c)
return buckets; {
} uint8_t* temp = (*c).getBuckets();
V = 0;
for (int i = 0; i < m; i++)
{
if (temp[i] > buckets[i])
buckets[i] = temp[i];
uint64_t CardinalityCounter::getM(){ if (buckets[i] == 0)
return m; V += 1;
} }
}
uint8_t* CardinalityCounter::getBuckets()
{
return buckets;
}
uint64_t CardinalityCounter::getM()
{
return m;
}

View file

@ -1,3 +1,5 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include <stdint.h> #include <stdint.h>
/* /*