#include<stdio.h>
#include<stdlib.h>
#inndef _HashQuad_h
typedef unsigned int Index;
typedef Index Pos;
struct HashTbl;
typedef struct HashTbl *Table;
Table newHash(int);
Pos Find(ElementType,Table);
void Insert(ElementType,Table);
Table ReHash(Table);
#endif
struct HashEntry
{
ElementType ele;
int info;
};
struct HashEntry;
typedef HashEntry Cell;
struct HashTbl
{
int tableSize;
Cell *theCell;
};
Table newHash(int tableSize)
{
Table h;
int i;
if(tableSize<minTableSize)
return NULL;
h=(Table)malloc(sizeof(struct HashTbl));
h->tableSize=nextPrime(tableSize);
h->theCell=(Cell *)malloc(sizeof(Cell)*h->tableSize);
for(i=0;i<h->tableSize;i++)
*(h->theCell+i).info=0;
return h;
}
Pos Find(ElementType x,Table h)
{
Pos currentPos;
int j=0;
currentPos=HashFunction(x,h->tableSize);
while(*(h->theCell+currentPos).info!=0&&fcmp(*(h->theCell+currentPos).ele,x)!=1)
{
currentPos+=(2* ++j)-1;
if(currentPos>=h->tableSize) // 小心边界
currentPos-=h->tableSize;
}
return currentPos;
}
void Insert(ElementType x,Table h)
{
Pos aim;
aim=Find(x,h);
if(h->theCell[aim].info==0)
{
fcpy(h->theCell[aim].ele,x);
h->theCell[aim].info=1;
}
}
Table ReHash(Table oldTbl)
{
Table h;
int oldSize;
oldSize=oldTbl->tableSize;
h=newHash(2*oldSize);
for(int i=0;i<oldSize;i++)
{
if(oldTbl->theCell[i].info==1)
Insert(oldTbl->theCell[i].ele,h);
}
free(oldTbl);
return h;
}