//implements perfect hashing
//hashtable.c@standard c library(*SCL*) by ASHISH TIWARI
/*
You may print, store code from this program for your personal,
noncommercial use. By accessing this code, you agree not to reproduce, distribute,
display or transmit information from this code in any form or by any means without
the permission of ASHISH TIWARI, with this exception: You may occasionally reproduce,
distribute, display or transmit an insubstantial portion of the information, for a
noncommercial purpose, to a limited number of individuals. ASHISH TIWARI does not warrant
the accuracy, completeness, timeliness, merchantability or fitness for a particular purpose
of the cose,though every care has been taken in making this code and has been compiled
on standard gcc compiler provided by GNU. ASHISH TIWARI shall not be liable for any
errors or omissions in the information or decisions made or actions taken in reliance
on the code.

"*SCL*" is a registered trademark of ASHISH TIWARI, IITKGP.
All contents of "*SCL*" is copyright ©2000-.... ,ASHISH TIWARI, IITKGP
unless otherwise noted. Reproduction in part or in whole without
permission is expressly prohibited.
*/


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ver_hash_size 7
#define p 119
#define NIL 0
struct tag{
int a;
int b;
int arraysize;
int* newarr;
};
typedef struct tag hasht;

hasht* hasharr;
int a,b;
void init();
void insert(int data);
int hash_1(int k);
int hash_2(int k,int j);
int search(int data,int* i,int* j);
void delete(int data);

main()
{
int* copy,temp;
int item;int j;int i=0,k=0;
srand((unsigned int)time(NULL));
b=rand()%p;

a=((rand()%(p-1))+1);

init();
for(item=11;item<32;item++)
insert(item);
for(item=0;item<ver_hash_size;item++)
{
for(j=0;j<hasharr[item].arraysize;j++)
{
printf("%d ",*((hasharr[item].newarr)+j));
}
if(hasharr[item].newarr!=NULL)
printf("gfgfgf");
printf("\n%d\n",hasharr[item].arraysize);
}
if(search(29,&i,&k))
printf("succcess i=%d j=%d\n",i,k);
delete(29);
for(item=0;item<ver_hash_size;item++)
{
for(j=0;j<hasharr[item].arraysize;j++)
{
printf("%d ",*((hasharr[item].newarr)+j));
}
if(hasharr[item].newarr!=NULL)
printf("gfgfgf");
printf("\n%d\n",hasharr[item].arraysize);
}

}

void init()
{
int i;
hasharr=(hasht*)malloc(ver_hash_size*sizeof(hasht));
for(i=0;i<ver_hash_size;i++)
{
hasharr[i].a=rand()%100;
hasharr[i].b=rand()%100;
hasharr[i].arraysize=0;
hasharr[i].newarr=NULL;
}
}

void insert(int data)
{
int i,size,j,g;

i=hash_1(data);
//temp=hasharr[i].newarr;
size=hasharr[i].arraysize;
size++;
size=(size*size);
if(hasharr[i].arraysize==0)
{
hasharr[i].newarr=(int*)malloc(sizeof(int));
*((hasharr[i].newarr)+0) =NIL;
}
else
{
hasharr[i].newarr=(int*)realloc(hasharr[i].newarr,size*sizeof(int));
g=sqrt(size)-1;
for(j=g*g-1;j<size;j++)
*((hasharr[i].newarr)+j)=NIL;
}
//hasharr[i].n
hasharr[i].arraysize=size;
j=hash_2(data,i);
*((hasharr[i].newarr)+j)=data;
}


int hash_1(int k)
{
return(((a*k+b)%p)%ver_hash_size);
}


int hash_2(int k,int j)
{
int aj;
int bj;
int mj;
aj=hasharr[j].a;
bj=hasharr[j].b;
mj=hasharr[j].arraysize;
return(((aj*k+bj)%p)%mj);
}


int search(int data,int* i,int* j)
{

*i=hash_1(data);
*j=hash_2(data,*i);
if(data==(*((hasharr[*i].newarr)+*j)))
return(1);
else
return(0);
}

void delete(int data)
{
int i=0,j=0;
if(search(data,&i,&j))
*((hasharr[i].newarr)+j)=NIL;
else
printf("element not found so unable to delete\n"),exit(1);
}