Hash Compared & Elfhash Detailed

Source: Internet
Author: User

Label:

  • Partially reproduced from here
Common hash Algorithm Codes & Comparisons
  • Commonly used string hash function and Elfhash,aphash, and so on, are very simple and effective method. These functions use bitwise arithmetic to make each character affect the last function value. There are also hash functions represented by MD5 and SHA1, which are almost impossible to find collisions.
  • Commonly used string hash functions have bkdrhash,aphash,djbhash,jshash,rshash,sdbmhash,pjwhash,elfhash and so on. For the above hash functions, I have a small evaluation of them.
Hashfunction data1Data2Data3Data4Data1Scoring data2Scoring data3Scoring data4Score average score Bkdrhash2   0     4774    481       96.55    -         90.95       82.05       92.64Aphash2   3     4754    493       96.55   88.46        -         51.28       86.28Djbhash2   2     4975    474       96.55   92.31       0            -         83.43Jshash1   4     4761    506        -     84.62       96.83       17.95       81.94Rshash1   0     4861    30s        -      -         51.58       20.51       75.96Sdbmhash3   2     4849    504       93.1    92.31       57.01       23.08       72.41Pjwhash -   -    4878    513       0       0           43.89       0           21.95Elfhash -   -    4878    513       0       0           43.89       0           21.95
  • Where data 1 is the number of random string hash collisions consisting of 100,000 letters and numbers. Data 2 is the number of 100,000 meaningful English sentence hash collisions. Data 3 is the number of conflicts that are stored in a linear table after the hash value of data 1 is modeled with 1000003 (large prime). Data 4 is the number of conflicts that are stored in a linear table after the hash value of data 1 is modeled with 10000019 (greater prime).
  • After comparison, the above average score is obtained. The average is the square average. It can be found that the Bkdrhash effect is the most prominent in both actual and coding implementations. Aphash is also an excellent algorithm. Djbhash,jshash,rshash and Sdbmhash have their own merits. Pjwhash and Elfhash have the worst effect, but the scores are similar and the algorithms are similar in nature.
  • In the information repair competition, in accordance with the principle of easy coding and debugging, personally think that Bkdrhash is the most suitable for memory and use.
  • Byvoid Original, welcome advice, communication, criticism and correction.
  • Attached: C language Program code for various hash functions
SDBM Hash
  • SDBM Hash
unsigned int SDBMHash(char *str){    hash0;    while (*str)    {        hash65599*hash + (*str++);        hash = (*str++) + (hash6) + (hash16hash;    }    return (hash0x7FFFFFFF);}
RS Hash Function
  • RS Hash Function
// RS Hash Functionunsignedint RSHash(char *str){    unsignedint378551;    unsignedint63689;    unsignedint0;    while (*str)    {        hash = hash * a + (*str++);        a *= b;    }    return0x7FFFFFFF);}
JS Hash Function
  • JS Hash Function
// JS Hash Functionint JSHash(char *str){    int1315423911;    while (*str)    {        5) + (*str2));    }    return0x7FFFFFFF);}
P. J. Weinberger Hash Function
  • P. J. Weinberger Hash Function
//P. J. Weinberger Hash Functionunsigned intPjwhash (Char*STR) {unsigned intBitsinunignedint = (unsigned int)(sizeof(unsigned int) *8);unsigned intThreequarters = (unsigned int) ((Bitsinunignedint *3) /4);unsigned intOneeighth = (unsigned int) (Bitsinunignedint/8);unsigned intHighbits = (unsigned int)(0xFFFFFFFF) << (bitsinunignedint-oneeighth);unsigned inthash =0;unsigned intTest =0; while(*STR) {hash = (hash << oneeighth) + (*str++);if(test = hash & highbits)! =0{hash = ((hash ^ (test >> threequarters)) & (~highbits)); }    }return(Hash &0x7FFFFFFF);}
ELF Hash Function
  • ELF Hash Function
//ELF Hash Function  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
  • BKDR Hash Function
//BKDR Hash FunctionUnsignedintBkdrhash (Char*Str) {unsignedintSeed =131;//131 1313 13131 131313 etc..Unsignedinthash =0; while(*Str{hash = hash * seed + (*Str++); }return(Hash &0x7FFFFFFF);}//DJB Hash FunctionUnsignedintDjbhash (Char*Str) {unsignedinthash =5381; while(*Str) {hash + = (hash <<5) + (*Str++); }return(Hash &0x7FFFFFFF);}
AP Hash Function
  • AP Hash Function
//AP Hash FunctionUnsignedintAphash (Char*Str) {unsignedinthash =0;intI for(i=0; *Str; i++) {if((I &1) ==0) {Hash ^= (hash <<7) ^ (*Str+ +) ^ (hash >>3)); }Else{Hash ^= (hash << One) ^ (*Str+ +) ^ (hash >>5))); }    }return(Hash &0x7FFFFFFF);}
Elfhash Detailed analysis
  • Elfhash
//ELF Hash Functionunsigned intElfhash (Char*STR) {unsigned inthash =0;unsigned intx =0; while(*STR) {hash = (hash <<4) + (*str++);//hash Left 4 bits, current character ASCII deposit hash        if((x = hash &0xf0000000L)! =0)        {//If the highest four bit is not 0, then the character is more than 7, if not processed, plus a nineth character, the first character will be moved out, so the following processing.             //This processing, if the string (A-Z or a-Z) will affect only 5-8 bits, otherwise it will affect 5-31 bits, because the C language uses the arithmetic shiftHash ^= (x >> -);//Clear 28-31 bits. Above is actually to delete the high four-bit and low 5-8-bit operations once, and hash = (Hash << 4) + (*str++); Same effectHash &= ~x; }    }//Returns a number with a sign bit of 0, which discards the highest bit, so as not to have an effect outside the function. (We can consider that if there are only characters, the sign bit cannot be negative)    return(Hash &0X7FFFFFFF);}
  • Explain
ELFHashfunction on Unix System V version4In the executable link format (executable and linking format, or ELF), the elf file format is used to store executables and destination files. ELFHashThe function is a hash of the string. It works well for long strings and short strings, and every character in the string has the same effect, and it cleverly calculates the ASCII encoded value of the character, ELFHashfunction for the ability to evenly distribute strings in a hash table. Description: Unsigned intHash=0; unsigned int x =0Define unsigned integers, do not take into account the influence of the sign bit in the bitwise operation, the left shift and the right shift both complement the position0int is +Bits, i.e.00000000  00000000   00000000   00000000Hash= (Hash<<4) + (*str++);//HashMove left4bit, current character ASCII depositHashExample, ifHashFor2When, (Hash<<4) After the operation, zoom in -(2Of4And then add (*str++), (*str++) to8Bit of character, so on4-7To be affected, then four bits are added toHashFour bits left empty.if((x =Hash&0xf0000000l)! =0)0xf0000000l says -- toBit this4Bit is1After -For both0Long Integer (L), the result of the operation is X-savedHashThe high4Bit & Bitwise AND if two corresponding bits are1, the result value of the bit is1, otherwise the0Hash^= (x >> -First, the copy of x is shifted to the right. atBit, and then with theHashTo make a different or action. The value of x after the right shift is00000000 00000000 00000000****0000; * * * forHashThe high four-bit ^ bitwise XOR or if the two bits values of the participating operations are the same0, otherwise the1Hash&= ~x;if((x =Hash&0xf0000000l)! =0), X holds theHashThe high four-bit, although the right move operation, but does not change the value of x, but the operation of the copy. AfterHash&= ~x;HashThe high four bits are emptied. Returns a symbol bit for0, that is, discard the highest bit, so as not to have an effect outside the function. (We can consider that if there are only characters, the sign bit cannot be negative)return(Hash&0X7FFFFFFF);

Hash Compared & Elfhash Detailed

Related Article

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.

Tags Index: