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,20 +7,24 @@
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); double initial_estimate = 2*(log(1.04)-log(error))/log(2);
int answer = (int) floor(initial_estimate); int answer = (int) floor(initial_estimate);
double k; double k;
do{ do
{
answer++; answer++;
k = pow(2, (answer - initial_estimate)/2); k = pow(2, (answer - initial_estimate)/2);
}while(erf(k/sqrt(2)) < conf); }
while (erf(k/sqrt(2)) < conf);
return answer; return answer;
} }
CardinalityCounter :: CardinalityCounter(uint64_t size){ CardinalityCounter::CardinalityCounter(uint64_t size)
{
m = size; m = size;
buckets = new uint8_t[m]; buckets = new uint8_t[m];
@ -31,15 +37,14 @@ using namespace std;
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){ CardinalityCounter :: CardinalityCounter(double error_margin)
{
int b = optimalB(error_margin); int b = optimalB(error_margin);
m = (uint64_t) pow(2, b); m = (uint64_t) pow(2, b);
buckets = new uint8_t[m]; buckets = new uint8_t[m];
@ -53,81 +58,86 @@ using namespace std;
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(){ CardinalityCounter::~CardinalityCounter()
{
delete [] buckets; delete [] buckets;
delete &m; delete &m;
delete &V; delete &V;
delete &alpha_m; delete &alpha_m;
} }
uint8_t CardinalityCounter :: rank(uint64_t hash_modified){ uint8_t CardinalityCounter::rank(uint64_t hash_modified)
{
uint8_t answer = 0; uint8_t answer = 0;
hash_modified = (uint64_t)(hash_modified/m); hash_modified = (uint64_t)(hash_modified/m);
hash_modified *= 2; hash_modified *= 2;
do{ do
{
hash_modified = (uint64_t) (hash_modified/2); hash_modified = (uint64_t) (hash_modified/2);
answer++; answer++;
}while(hash_modified%2 == 0); }
while (hash_modified%2 == 0);
return answer; return answer;
} }
void CardinalityCounter::addElement(uint64_t hash)
{
void CardinalityCounter::addElement(uint64_t hash){
uint64_t index = hash % m; uint64_t index = hash % m;
hash = hash-index; hash = hash-index;
if(buckets[index] == 0) if(buckets[index] == 0)
V--; V--;
uint8_t temp = rank(hash);
if(temp > buckets[index]){
buckets[index] = temp;
}
}
double CardinalityCounter::size(){ uint8_t temp = rank(hash);
if (temp > buckets[index])
buckets[index] = temp;
}
double CardinalityCounter::size()
{
double answer = 0; double answer = 0;
for(int i = 0; i < m; i++){ for (int i = 0; i < m; i++)
answer += pow(2, -(int)buckets[i]); answer += pow(2, -(int)buckets[i]);
}
answer = 1/answer; answer = 1/answer;
answer = alpha_m*m*m*answer; answer = alpha_m*m*m*answer;
if(answer <= 5*(double)(m/2)){ if (answer <= 5*(double)(m/2))
return m*log((double) m/V); return m*log((double) m/V);
} else if(answer <= pow(2,64)/30)
else if(answer <= pow(2,64)/30){
return answer; return answer;
} else
else{
return -pow(2,64)*log(1-answer/pow(2,64)); return -pow(2,64)*log(1-answer/pow(2,64));
} }
}
void CardinalityCounter::merge(CardinalityCounter* c){ void CardinalityCounter::merge(CardinalityCounter* c)
{
uint8_t* temp = (*c).getBuckets(); uint8_t* temp = (*c).getBuckets();
V = 0; V = 0;
for(int i = 0; i < m; i++){ for (int i = 0; i < m; i++)
if(temp[i] > buckets[i]){ {
if (temp[i] > buckets[i])
buckets[i] = temp[i]; buckets[i] = temp[i];
}
if(buckets[i] == 0){ if (buckets[i] == 0)
V += 1; V += 1;
} }
} }
}
uint8_t* CardinalityCounter::getBuckets(){ uint8_t* CardinalityCounter::getBuckets()
{
return buckets; return buckets;
} }
uint64_t CardinalityCounter::getM(){ uint64_t CardinalityCounter::getM()
{
return m; 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>
/* /*