Pagini recente » Cod sursa (job #1366663) | Cod sursa (job #1334405) | Cod sursa (job #1348132) | Cod sursa (job #701112) | Cod sursa (job #745320)
Cod sursa(job #745320)
// Double hashing with templates
// Ursateanu Irina Mihaela, Grupa 135
#include <fstream>
#include <stdio.h>
#define mod1 666013
#define mod2 100021
#define mod3 100007
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
template <class tip>// clasa pentru date de tipul float cu maxim 2 zecimale, char, int, long int etc.
class hash
{
private:
tip H[mod1+10];
int size_of_hash;
int vizitat[mod1+10];// vectorul cu care verificam daca o pozitie este libera
public:
hash(int size)
{
size_of_hash=size;
for(int i=0;i<size;i++)
vizitat[i]=0;
}
int hash_table(tip X,int i);
void insert(tip X);
int find(tip X);
void erase(tip X);
};
template <class tip> // functia de hash pentru tipurile int,float, etc
int hash<tip>::hash_table(tip X,int i)
{
int y,z;
y=(int)X;
z=X-y;
return (((y+100*z)%mod2)+i*(1+(y+100*z)%mod3))%mod1;
}
template<> //functie speciala de hash pentru tipul char
int hash<char *>::hash_table(char *X,int i)
{
unsigned int n=0;
int m;
while(m=*X++)
n=m+(n<<6)+(n<<16)-n;
return ((n%mod2)+i*(1+n&mod3))&mod1;
}
template <class tip>
int hash<tip>::find(tip X) // returneaza pozitia pe care a fost gasit elementul sau ca nu exista in vector
{
int i,aux;
for(i=0;i<mod1;i++)
{
aux=hash_table(X,i);
if(vizitat[aux]==0)
return -1;
if(H[aux]==X&& vizitat[aux]==1)
return aux;
}
return -1;
}
template <class tip>
void hash<tip>::insert(tip X)
{
int i, aux;
if(find(X)==-1)
for(i=0;i<mod1;i++)
{
aux=hash_table(X,i);
if(vizitat[aux]==0) // Daca este liber il insereaza
{
H[aux]=X;
vizitat[aux]=1;
return;
}
}
}
template <class tip>
void hash<tip>::erase(tip X)
{
int aux;
aux=find(X);
if(aux!=-1) //Daca exista elementul atunci il sterge
{
H[aux]=0;
vizitat[aux]=0;
}
}
int main()
{
hash <char *>object(mod1);
int i,op,n;
char *x;
f>>n;
for(i=1;i<=n;i++)
{
f>>op>>x;
if(op==1)
object.insert(x);
if(op==2)
object.erase(x);
if(op==3)
if(object.find(x)==-1)g<<"0\n";
else g<<"1\n";
}
return 0;
}