Pagini recente » Cod sursa (job #1449121) | Cod sursa (job #72951) | Cod sursa (job #2573782) | Clasament foxi | Cod sursa (job #913044)
Cod sursa(job #913044)
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class blablaHashing{
public:
blablaHashing(int, char *, char *);
~blablaHashing();
bool insertNumber(int);
bool find(int);
void erase(int);
void start();
private:
int *table1, *table2, random1, random2, sizeHash;
int loopLimit = 10;
fstream in, out;
void swap(int &,int &);
void changeHashingFunction();
int getRandomNumber();
};
int main (){
blablaHashing Hash(1000000, "hashuri.in", "hashuri.out");
Hash.start();
return 0;
}
blablaHashing::blablaHashing(int n, char *fin, char *fout){
sizeHash = n;
in.open(fin, ios::in);
out.open(fout, ios::out);
table1 = new int [n];
table2 = new int [n];
changeHashingFunction();
}
void blablaHashing::start(){
changeHashingFunction();
int M, op, x;
bool ok;
in >> M;
for (; M; M--){
in >> op >> x;
if (op == 1){
ok = insertNumber(x);
if (!ok){
in.seekg(0, ios::beg);
in.seekg(0, ios::beg);
start();
}
}
if (op == 2){
if (find (x)) erase (x);
}
if (op == 3){
if (find(x)) out << 1 << endl;
else out << 0 << endl;
}
}
}
bool blablaHashing::find (int x){
int func1 = ((x + 123456) % random1) % sizeHash;
int func2 = ((x + 654321) % random2) % sizeHash;
if (table1[func1] == x || table2[func2] == x) return true;
else return false ;
}
void blablaHashing::erase (int x){
int func1 = ((x + 123456) % random1) % sizeHash;
int func2 = ((x + 654321) % random2) % sizeHash;
if (table1[func1] == x) table1[func1] = 0;
else table2[func2] = 0;
}
bool blablaHashing::insertNumber(int x){
for (int i = 1; i <= loopLimit; i++){
int tempAux = ((x + 123456) % random1) % sizeHash;
if (table1[tempAux] == x || !table1[tempAux]){
table1[tempAux] = x;
return true;
}
else swap(x, table1[tempAux]);
tempAux = ((x + 654321) % random2) % sizeHash;
if (table2[tempAux] == x || !table2[tempAux]){
table2[tempAux] = x;
return true;
}
else swap(x, table2[tempAux]);
}
return false;
}
blablaHashing::~blablaHashing(){
in.close();
out.close();
}
void blablaHashing::changeHashingFunction(){
random1 = rand();
random2 = rand();
}
void blablaHashing::swap(int &a,int &b){
int tempAux = a;
a = b;
b = tempAux;
}
int blablaHashing::getRandomNumber(){
return 4;
}