Pagini recente » Cod sursa (job #108028) | Cod sursa (job #1270982) | Cod sursa (job #654631) | Cod sursa (job #1780432) | Cod sursa (job #912925)
Cod sursa(job #912925)
#include<fstream>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
using namespace std;
class hash_function {
private:
int a, b, prime, size;
public:
void generate(int, int);
int calculate(int);
int calculate(float);
int calculate(double);
int calculate(string);
};
void hash_function::generate(int dim, int p) {
prime = p;
size = dim;
a = rand() % size;
b = rand() % size;
}
int hash_function::calculate(int val) {
return(((long long)a * (long long)val + (long long)b) % (long long)prime) % (long long)size;
}
int hash_function::calculate(float val) {
int key;
float number, fractpart, intpart, mult_magic = 0.618034;
number = val*mult_magic;
fractpart = modf(number, &intpart);
key = (((long long)a * (long long)fractpart + (long long)b * (long long)intpart) % (long long)prime) % (long long)size;
return key;
}
int hash_function::calculate(double val) {
int key;
double number, fractpart, intpart, mult_magic = 0.618034;
number = val*mult_magic;
fractpart = modf(number, &intpart);
key = (((long long)a * (long long)fractpart + (long long)b * (long long)intpart) % (long long)prime) % (long long)size;
return key;
}
int hash_function::calculate(string str) {
unsigned InitialFNV = 2166136261U, key;
int FNVMultiple = 16777619, dim, i;
dim = str.length();
key = InitialFNV;
for(i = 0; i<dim; i++) {
key = key ^ (str[i]);
key = key * FNVMultiple;
}
key = (((long long)a * (long long)key + (long long)b) % (long long)prime) % (long long)size;
return key;
}
template<class T>
class cockooHash {
private:
ifstream f;
ofstream g;
T *H1, *H2;
int prime, size, maxsteps;
bool *viz1, *viz2;
string input, output;
hash_function func1, func2;
public:
cockooHash<T>(int, string, string);
bool remake_hash();
bool find(T);
bool erase(T);
bool insert(T);
};
int main() {
cockooHash<string> myHash(500000, "hashuri.in", "hashuri.out");
srand(time(0));
bool ok = false;
while(!ok)
ok = myHash.remake_hash();
return 0;
}
string convert(int val) {
ostringstream stream;
stream<<val;
return stream.str();
}
template<class T>
bool cockooHash<T>::remake_hash() {
int Q, op, number;
bool ok = true;
string val;
f.open(input.c_str());
g.open(output.c_str());
f>>Q;
while(Q--) {
f>>op>>number;
val = convert(number);
if(op == 1) {
if(!find(val)) {
ok = insert(val);
if(!ok) {
f.close(); g.close();
return false;
}
}
}
if(op == 2) {
if(find(val))
erase(val);
}
if(op == 3) {
if(find(val) == false)
g<<0<<"\n";
else
g<<1<<"\n";
}
}
f.close();
g.close();
return true;
}
template<class T>
cockooHash<T>::cockooHash(int dim, string infile, string outfile) {
size = dim;
maxsteps = (int)log2(size);
prime = 1000000009;
input = infile;
output = outfile;
H1 = new T[size];
H2 = new T[size];
viz1 = new bool[size];
viz2 = new bool[size];
memset(viz1, 0, sizeof(viz1));
memset(viz2, 0, sizeof(viz1));
func1.generate(size, prime);
func2.generate(size, prime);
}
template<class T>
bool cockooHash<T>::find(T val) {
int poz1 = func1.calculate(val);
int poz2 = func2.calculate(val);
//cout<<val<<" "<<poz1<<" "<<poz2<<" "<<size<<" "<<prime<<"\n\n";
if(H1[poz1] == val && viz1[poz1] == true)
return true;
if(H2[poz2] == val && viz2[poz2] == true)
return true;
return false;
}
template<class T>
bool cockooHash<T>::erase(T val) {
int poz1 = func1.calculate(val);
int poz2 = func2.calculate(val);
if(H1[poz1] == val && viz1[poz1] == true) {
viz1[poz1] = false;
return true;
}
if(H2[poz2] == val && viz2[poz2] == true) {
viz2[poz2] = false;
return true;
}
return false;
}
template<class T>
bool cockooHash<T>::insert(T val) {
int i, poz1, poz2;
T x;
x = val;
poz1 = func1.calculate(x);
poz2 = func2.calculate(x);
//cout<<val<<" "<<poz1<<" "<<poz2<<" "<<size<<" "<<prime<<"\n\n";
if(viz1[poz1] == false) {
H1[poz1] = x;
viz1[poz1] = true;
return true;
}
for(i=0; i<maxsteps; i+=2) {
if(viz2[poz2] == false) {
H2[poz2] = x;
viz2[poz2] = true;
return true;
}
swap(x, H2[poz2]);
poz1 = func1.calculate(x);
poz2 = func2.calculate(x);
if(viz1[poz1] == false) {
H1[poz1] = x;
viz1[poz1] = true;
return true;
}
swap(x, H1[poz1]);
poz1 = func1.calculate(x);
poz2 = func2.calculate(x);
}
//e nevoie de rehash daca ajungem aici
return false;
}