#include "BooPHF.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <random>
#include <algorithm>
using namespace std;
class Custom_uint64_Hasher
{
public:
uint64_t operator () (uint64_t key, uint64_t seed=0) const
{
key ^= key >> 33;
key *= 0xff51afd7ed558ccd;
key ^= key >> 33;
key *= 0xc4ceb9fe1a85ec53;
key ^= key >> 33;
key ^= seed;
return key;
}
};
typedef boomphf::mphf< u_int64_t, Custom_uint64_Hasher > boophf_t;
int main (int argc, char* argv[]){
u_int64_t nelem = 1000000;
uint nthreads = 1;
if(argc !=3 ){
printf("Usage :\n");
printf("%s <nelem> <nthreads> \n",argv[0]);
return EXIT_FAILURE;
}
if(argc ==3 ){
nelem = strtoul(argv[1], NULL,0);
nthreads = atoi(argv[2]);
}
uint64_t ii, jj;
u_int64_t *data;
uint64_t rab = 100;
static std::mt19937_64 rng;
rng.seed(std::mt19937_64::default_seed);
data = (u_int64_t * ) calloc(nelem+rab,sizeof(u_int64_t));
for (u_int64_t i = 1; i < nelem+rab; i++){
data[i] = rng();
}
printf("de-duplicating items \n");
std::sort(data,data+nelem+rab);
for (ii = 1, jj = 0; ii < nelem+rab; ii++) {
if (data[ii] != data[jj])
data[++jj] = data[ii];
}
printf("found %lli duplicated items \n",nelem+rab-(jj + 1) );
boophf_t * bphf = NULL;
double t_begin,t_end; struct timeval timet;
printf("Construct a BooPHF with %lli elements \n",nelem);
gettimeofday(&timet, NULL); t_begin = timet.tv_sec +(timet.tv_usec/1000000.0);
auto data_iterator = boomphf::range(static_cast<const u_int64_t*>(data), static_cast<const u_int64_t*>(data+nelem));
double gammaFactor = 2.0;
bphf = new boomphf::mphf<u_int64_t,Custom_uint64_Hasher>(nelem,data_iterator,nthreads,gammaFactor);
gettimeofday(&timet, NULL); t_end = timet.tv_sec +(timet.tv_usec/1000000.0);
double elapsed = t_end - t_begin;
printf("BooPHF constructed perfect hash for %llu keys in %.2fs\n", nelem,elapsed);
printf("boophf bits/elem : %f\n",(float) (bphf->totalBitSize())/nelem);
uint64_t idx = bphf->lookup(data[0]);
printf(" example query %lli -----> %llu \n",data[0],idx);
free(data);
delete bphf;
return EXIT_SUCCESS;
}