Pagini recente » Istoria paginii runda/igorj/clasament | Cod sursa (job #1106744) | Cod sursa (job #1008512) | Rating Pocea Antonio (tonipocea) | Cod sursa (job #936832)
Cod sursa(job #936832)
#include <math.h>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
using namespace std;
template <typename type>
class base_hash
{
/*protected:
type *h1,*h2;
int hash_size;
int a,b;
long long prim;*/
public:
//base_hash <type> ();
virtual bool operator +=(type)=0;
virtual void operator -=(type)=0;
virtual int function (type,int,int)=0;
virtual int hash_from_data(string,string)=0;
};
template <typename type>
class cuckoo_hash:public base_hash <type> {
private:
type *h1,*h2;
int hash_size;
int a,b;
long long prim;
public:
cuckoo_hash<type>(int size,int p=1000000009)
{
h1 = new type[size];
h2 = new type[size];
hash_size = size;
fill(h1,h1+size,0);
fill(h2,h2+size,0);
prim=p;
}
~cuckoo_hash()
{
delete [] h1;
delete [] h2;
a=b=hash_size=0;
prim=0;
}
bool operator +=(type value)
{
int cycles=0,sw=1;
type aux=value;
bool found=false;
if (*(this) == value) return true;
while (!found)
{
int poz1 = function(value,a,b);
int poz2 = function(value,b,a);
cycles++;
if (h1[poz1] == 0)
{
h1[poz1] = value;
found = true;
}
else if(h2[poz2] == 0)
{
h2[poz2] = value;
found = true;
}
else
{
if (sw==1)
{
aux = value;
value = h1[poz1];
h1[poz1] = aux;
}
else
{
aux = value;
value = h2[poz2];
h2[poz2] = aux;
}
sw*=-1;
}
if ((cycles > log2(this->hash_size))&&(!found)){return false;} //numarul de incercari nu depaseste log size
}
return true;
}
void operator -=(type value)
{
int poz1 = function(value,a,b);
int poz2 = function(value,b,a);
if (h1[poz1] == value)
{
h1[poz1] = 0;
}
else if (h2[poz2] == value)
{
h2[poz2] = 0;
}
}
bool operator ==(type value)
{
int poz1 = function(value,a,b);
int poz2 = function(value,b,a);
if (h1[poz1] == value || h2[poz2] == value)
{
return true;
}
else
{
return false;
}
}
//define function for each type
int function(int value,int a,int b)
{
return (((long long)a+(long long)value * (long long)b ) % (long long)prim )% (long long)hash_size;
}
int function(float value,int a,int b)
{
value=value*(0.61);
double integral,fractionary;
fractionary=modf(value,&integral);
return (((long long)fractionary+(long long)integral * (long long)b ) % (long long)prim )% (long long)hash_size;
}
int function(double value,int a,int b)
{
value=value*(0.61);
double integral,fractionary;
fractionary=modf(value,&integral);
return (((long long)fractionary+(long long)integral * (long long)b ) % (long long)prim )% (long long)hash_size;
}
int function(char value,int a,int b)
{
return (((long long)a+(long long)(int(value)) * (long long)b ) % (long long)prim )% (long long)hash_size;
}
void make_hash_function()
{
a = rand() % hash_size;
b = rand() % hash_size;
}
int hash_from_data (string a, string b)
{
bool ok = false;
ifstream in(a.c_str());
ofstream out(b.c_str());
while (ok == false)
{
in.clear();
int N;
make_hash_function();
in >> N;
for (int i =0;i<N;i++)
{
type x ;int op;
in >> op >> x;
if (op==1)
if ( (*(this)+=x) == false )
{
ok = false;
break;
}
if (op==2)
*(this)-=x;
if (op==3)
out << (*(this) == x) << "\n";
}
ok = true;
}
return 0;
}
};
int main()
{ base_hash <int> *t;
t=new cuckoo_hash<int>(1000005);
t->hash_from_data("hashuri.in","hashuri.out");
return 0;
}