Classic string Hash Function

Source: Internet
Author: User

// 1. String hash function in PHP
    
Static unsigned long hashpjw (char * arkey, unsigned int nkeylength)
{
Unsigned long h = 0, G;
Char * Arend = arkey + nkeylength;
    
While (arkey <Arend)
{
H = (H <4) + * arkey ++;
If (G = (H & 0xf0000000 )))
{
H = H ^ (G> 24 );
H = H ^ g;
}
}

Return h;
}
    
// 2. String hash function in OpenSSL
    
Unsigned long lh_strhash (char * Str)
{
Int I, L;
Unsigned long ret = 0;
Unsigned short * s;
    
If (STR = NULL) return 0;
L = (strlen (STR) + 1)/2;
S = (unsigned short *) STR;
For (I = 0; I ret ^ = (s [I] <(I & 0x0f ));

Return ret;
}
/*
* The following hash seems to work very well on normal text strings
* Distributes on % 2 ^ n quite well, not as good as MD5, but still good.
*/
Unsigned long lh_strhash (const char * C)
{
Unsigned long ret = 0;
Long N;
Unsigned long V;
Int R;
    
If (C = NULL) | (* c = '\ 0') Return (RET );
/*
Unsigned char B [16];
MD5 (C, strlen (C), B );
Return (B [0] | (B [1] <8) | (B [2] <16) | (B [3] <24 ));
*/
N = 0x100;
While (* C)
{
V = n | (* C );
N + = 0x100;
R = (INT) (V> 2) ^ V) & 0x0f;
Ret = (Ret (32-r ));
RET & = 0 xffffffffl;
RET ^ = V * V;
C ++;
}

Return (Ret> 16) ^ RET );
}
    
    
// 3. String hash function in MySQL (the string hash function in MySQL is case sensitive)
    
# Ifndef new_hash_function
    
/* Calc hashvalue for a key */
Static uint calc_hashnr (const byte * Key, uint length)
{
Register uint Nr = 1, Nr2 = 4;

While (length --)
{
NR ^ = (NR & 63) + Nr2) * (uint) (uchar) * Key ++) + (NR <8 );
Nr2 + = 3;
}

Return (uint) nr );
}
    
/* Calc hashvalue for a key, Case indepenently */
Static uint calc_hashnr_caseup (const byte * Key, uint length)
{
Register uint Nr = 1, Nr2 = 4;

While (length --)
{
NR ^ = (NR & 63) + Nr2) * (uint) (uchar) toupper (* Key ++) + (NR <8 );
Nr2 + = 3;
}

Return (uint) nr );
}
    
# Else
    
/*
* Fowler/Noll/Vo hash
*
* The basis of the hash algorithm was taken from an idea sent by email to
* Ieee posix p1003.2 mailing list from Phong VO (kpv@research.att.com) and
* Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com)
* Later improved on their algorithm.
*
* The magic is in the interesting relationship between the special prime
* 16777619 (2 ^ 24 + 403) and 2 ^ 32 and 2 ^ 8.
*
* This hash produces the fewest collisions of any function that we 've seen so
* Far, and works well on both numbers and strings.
*/
    
Uint calc_hashnr (const byte * Key, uint Len)
{
Const byte * end = Key + Len;
Uint hash;

For (hash = 0; key <end; key ++)
{
Hash * = 16777619;
Hash ^ = (uint) * (uchar *) Key;
}

Return (hash );
}
    
Uint calc_hashnr_caseup (const byte * Key, uint Len)
{
Const byte * end = Key + Len;
Uint hash;

For (hash = 0; key <end; key ++)
{
Hash * = 16777619;
Hash ^ = (uint) (uchar) toupper (* Key );
}

Return (hash );
}
    
# Endif
    
    
// 4. Source Code implementation of an open-source hash table
Struct entry
{
Void * k, * V;
Unsigned int h;
Struct entry * next;
};
Struct hashtable {
Unsigned int tablelength;
Struct entry ** table;
Unsigned int entrycount;
Unsigned int loadlimit;
Unsigned int primeindex;
Unsigned int (* hashfn) (void * k );
INT (* eqfn) (void * K1, void * K2 );
};
  
Static const unsigned int primes [] = {
53, 97,193,389,
769,154 3, 3079,615 1,
12289,245 93, 49157,983 17,
196613,393 241, 786433,157 2869,
3145739,629 1469, 12582917,251 65843,
50331653,100 663319, 201326611,402 653189,
805306457,161 062.1641
};

Const unsigned int prime_table_length = sizeof (primes)/sizeof (primes [0]);
Const float max_load_factors = 0.65;
  
Struct hashtable * create_hashtable (unsigned int minsize, unsigned int (* hashf) (void *),
INT (* EQF) (void *, void *))
{
Struct hashtable * h;
Unsigned int pindex, size = primes [0];
/* Check requested hashtable isn't too large */
If (minsize> (1u <30) return NULL;
/* Enforce size as prime */
For (pindex = 0; pindex <prime_table_length; pindex ++)
{
If (primes [pindex]> minsize)
{
Size = prime [pindex];
Break;
}
}
H = (struct hashtable *) malloc (sizeof (struct hashtable ));
If (null = h) return NULL;/* OOM */
H-> table = (struct entry **) malloc (sizeof (struct entry *) * size );
If (null = H-> table)
{
Free (h );
Return NULL;
}/* OOM */
Memset (H-> table, 0, size * sizeof (struct entry *));
H-> tablelength = size;
H-> primeindex = pindex;
H-> entrycount = 0;
H-> hashfn = hashf;
H-> eqfn = EQF;
H-> loadlimit = (unsigned INT) Ceil (size * max_load_factor );

Return h;
}
  
Unsigned int Hash (struct hashtable * H, void * K)
{
/* Aim to protect against poor hash functions by adding logic here-logic taken from Java 1.4 hashtable source */
Unsigned int I = H-> hashfn (k );

I + = ~ (I <9 );
I ^ = (I> 14) | (I <18);/*> */
I + = (I <4 );
I ^ = (I> 10) | (I <22);/*> */

Return I;
}
  
  
5. Other string Hash Functions
    
Unsigned int Hash (char * Str)
{
Register unsigned int h;
Register unsigned char * P;
    
For (H = 0, P = (unsigned char *) STR; * P; P ++)
H = 31 * H + * P;
    
Return h;
}
    
Unsigned int hash_func (char * STR, int Len)
{
Register unsigned int sum = 0;
Register unsigned int h = 0;
Register char * P = STR;
    
While (p-S <Len)
{
Register unsigned short a = * (p ++ );
Sum ^ = A * (p-Str );
H ^ = A/(p-Str );
}

Return (sum <16) | h) % max_prime_less_than_hash_len;
}
    
// Rs Hash Function
Unsigned int rshash (char * Str)
{
Unsigned int B = 378551;
Unsigned int A = 63689;
Unsigned int hash = 0;
    
While (* Str)
{
Hash = hash * A + (* STR ++ );
A * = B;
}

Return hash & 0x7fffffff;
}
    
// JS Hash Function
Unsigned int jshash (char * Str)
{
Unsigned int hash = 1315423911;
    
While (* Str)
Hash ^ = (hash <5) + (* STR ++) + (hash> 2 ));

Return (hash & 0x7fffffff );
}
    
// P. J. Weinberger Hash Function
Unsigned int pjwhash (char * Str)
{
Unsigned int bitsinunignedint = (unsigned INT) (sizeof (unsigned INT) * 8 );
Unsigned int threequarters = (unsigned INT) (bitsinunignedint * 3)/4 );
Unsigned int oneeighth = (unsigned INT) (bitsinunignedint/8 );
Unsigned int highbits = (unsigned INT) (0 xffffffff) <(bitsinunignedint-oneeighth );
Unsigned int hash = 0;
Unsigned int test = 0;
    
While (* Str)
{
Hash = (hash <oneeighth) + (* STR ++ );
If (test = hash & highbits )! = 0)
Hash = (hash ^ (test> threequarters ))&(~ Highbits ));
}

Return (hash & 0x7fffffff );
}
    
// Elf hash function, the mostly frequntly used one
Unsigned int elfhash (char * Str)
{
Unsigned int hash = 0;
Unsigned int x = 0;
    
While (* Str)
{
Hash = (hash <4) + (* STR ++ );
If (x = hash & 0xf0000000l )! = 0)
{
Hash ^ = (x> 24 );
Hash & = ~ X;
}
}

Return (hash & 0x7fffffff );
}
    
// Bkdr Hash Function
Unsigned int bkdrhash (char * Str)
{
Unsigned int seed = 131; // 31 131 1313 13131 131313 Etc ..
Unsigned int hash = 0;
    
While (* Str)
Hash = hash * seed + (* STR ++ );

Return (hash & 0x7fffffff );
}
    
// Sdbm Hash Function
Unsigned int sdbmhash (char * Str)
{
Unsigned int hash = 0;
    
While (* Str)
Hash = (* STR ++) + (hash <6) + (hash <16)-Hash;

Return (hash & 0x7fffffff );
}
    
// Djb Hash Function
Unsigned int djbhash (char * Str)
{
Unsigned int hash = 5381;
    
While (* Str)
Hash ++ (hash <5) + (* STR ++ );

Return (hash & 0x7fffffff );
}
    
// AP Hash Function
Unsigned nt aphash (char * Str)
{
Unsigned int hash = 0;
Int I;
    
For (I = 0; * STR; I ++)
{
If (I & 1) = 0)
Hash ^ = (hash <7) ^ (* STR ++) ^ (hash> 3 ));
Else
Hash ^ = (~ (Hash <11) ^ (* STR ++) ^ (hash> 5 )));
}

Return (hash & 0x7fffffff );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.