// Perfect hashing in CLRS, 2nd ed, 245-249 #include #include #include #include int *keyInput; int *keySort,*hTable,hTableA,hTableB; int hTableP; long long int hTableAll; int seed,n; float CPUtime() { struct rusage rusage; getrusage(RUSAGE_SELF,&rusage); return rusage.ru_utime.tv_sec+rusage.ru_utime.tv_usec/1000000.0 + rusage.ru_stime.tv_sec+rusage.ru_stime.tv_usec/1000000.0; } int nextPrime(int x) { // Find a prime number >= x int work,k,remainder,quotient; if (x%2==1) work=x; else work=x+1; while (1) { for (k=3; ;k+=2) { remainder=work%k; if (remainder==0) break; quotient=work/k; if (quotienthTableP) hTableP=keyInput[i]; hTable[i]=0; } hTableP=nextPrime(hTableP+1); hTableAll=hTableA=1+labs(random())%(hTableP-1); hTableB=labs(random())%hTableP; // Counting sort for (i=0;i0) { if (hTable[i]==1) newSize+=2; else newSize+=3+hTable[i]*hTable[i]; } hTable=(int*) realloc((void*) hTable,newSize*sizeof(int)); if (!hTable) { printf("realloc failed %d\n",__LINE__); exit(0); } printf("realloc for %lu more permanent bytes\n",(newSize-n)*sizeof(int)); printf("final structure will use %.3f bytes per key\n", ((float) newSize)*sizeof(int)/n); k=0; //start of next group of keys sub=n; //start of next subarray for (i=0;i=14 keys is %d\n",count[14]); } void verifyPerfectHash() { int i,j,k,sub,slot,length; int a,b; long long int aLL; for (i=0;i