Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sfhashfcn.c

Go to the documentation of this file.
00001 /*
00002      sfhashfcn.c 
00003 
00004      Each hash table must allocate it's own SFGHASH struct, this is because
00005      sfghash_new uses the number of rows in the hash table to modulo the random
00006      values.
00007 
00008 */
00009 
00010 #include "sfhashfcn.h"
00011 
00012 /*
00013  *   Primiitive Prime number test, not very fast nor efficient, but should be ok for 
00014  *   hash table sizes of typical size.  NOT for real-time usage!
00015  */
00016 static int isPrime(int num )
00017 {
00018     int i;
00019     for(i=2;i<num;i++)
00020     {
00021         if( (num % i) == 0 ) break;//oops not prime, should have a remainder
00022     }
00023     if( i == num ) return 1;
00024     return 0;
00025 }
00026 /*
00027  *  Iterate number till we find a prime.
00028  */
00029 static int calcNextPrime(int num )
00030 {
00031     while( !isPrime( num ) ) num++;
00032 
00033     return num;
00034 }
00035 
00036 SFHASHFCN * sfhashfcn_new( int m )
00037 {
00038    SFHASHFCN *p;
00039 
00040    p = (SFHASHFCN*) malloc( sizeof(SFHASHFCN) );
00041    if( !p )
00042        return 0;
00043 
00044    srand( (unsigned) time(0) );
00045 
00046    p->seed       = calcNextPrime( rand() % m );
00047    p->scale      = calcNextPrime( rand() % m );
00048    p->hardener   = ( rand() * rand() ) ^ 0xe0c0b0a0;
00049 
00050    p->hash_fcn   = &sfhashfcn_hash;
00051    p->keycmp_fcn = &memcmp;
00052        
00053    return p;
00054 }
00055 
00056 void sfhashfcn_free( SFHASHFCN * p )
00057 {
00058    if( p )
00059    {
00060        free( p);
00061    }
00062 }
00063 
00064 unsigned sfhashfcn_hash( SFHASHFCN * p, unsigned char *d, int n )
00065 {
00066     unsigned hash = p->seed;
00067     while( n )
00068     {
00069         hash *=  p->scale;
00070         hash += *d++;
00071         n--;
00072     }
00073     return hash ^ p->hardener;
00074 }
00075 
00076 
00077 /** 
00078  * Make sfhashfcn use a separate set of operators for the backend.
00079  *
00080  * @param h sfhashfcn ptr
00081  * @param hash_fcn user specified hash function
00082  * @param keycmp_fcn user specified key comparisoin function
00083  */
00084 int sfhashfcn_set_keyops( SFHASHFCN *h,
00085                           unsigned (*hash_fcn)( SFHASHFCN * p,
00086                                                 unsigned char *d,
00087                                                 int n),
00088                           int (*keycmp_fcn)( const void *s1,
00089                                              const void *s2,
00090                                              size_t n))
00091 {
00092     if(h && hash_fcn && keycmp_fcn)
00093     {
00094         h->hash_fcn   = hash_fcn;
00095         h->keycmp_fcn = keycmp_fcn;
00096 
00097         return 0;
00098     }
00099 
00100     return -1;
00101 }
00102                         

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2