2012-06-26 12 views
9

क्या आप किसी ने हैशिंग फ़ंक्शन/एल्गोरिदम पर कुछ प्रकाश डाला हो सकता है पर्ल एक स्ट्रिंग को किसी इंडेक्स में मैप करने के लिए उपयोग करता है? कोई प्रासंगिक पढ़ना?पर्ल का उपयोग क्या हैशिंग फ़ंक्शन/एल्गोरिदम है?

+0

तुम क्या करने कोशिश कर रहे हैं? क्या आप कुछ कोड का उदाहरण प्रदान कर सकते हैं जो काम नहीं कर रहा है? –

+0

कोई भी कुंजी जो टक्कर पैदा करेगी :) – Jean

+0

कोई भी दो कुंजी नहीं है जो हमेशा टकराएगी। आवश्यक होने पर हैशिंग को यादृच्छिक रूप से परेशान किया जाता है। – ikegami

उत्तर

15

PERL_HASH_INTERNAL_, hv.h में परिभाषित है, नीचे की नकल की:

/* hash a key */ 
/* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins 
* from requirements by Colin Plumb. 
* (http://burtleburtle.net/bob/hash/doobs.html) */ 
/* The use of a temporary pointer and the casting games 
* is needed to serve the dual purposes of 
* (a) the hashed data being interpreted as "unsigned char" (new since 5.8, 
*  a "char" can be either signed or unsigned, depending on the compiler) 
* (b) catering for old code that uses a "char" 
* 
* The "hash seed" feature was added in Perl 5.8.1 to perturb the results 
* to avoid "algorithmic complexity attacks". 
* 
* If USE_HASH_SEED is defined, hash randomisation is done by default 
* If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done 
* only if the environment variable PERL_HASH_SEED is set. 
* For maximal control, one can define PERL_HASH_SEED. 
* (see also perl.c:perl_parse()). 
*/ 

#define PERL_HASH_INTERNAL_(hash,str,len,internal) \ 
    STMT_START { \ 
     register const char * const s_PeRlHaSh_tmp = str; \ 
     register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \ 
     register I32 i_PeRlHaSh = len; \ 
     register U32 hash_PeRlHaSh = (internal ? PL_rehash_seed : PERL_HASH_SEED); \ 
     while (i_PeRlHaSh--) { \ 
      hash_PeRlHaSh += *s_PeRlHaSh++; \ 
      hash_PeRlHaSh += (hash_PeRlHaSh << 10); \ 
      hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \ 
     } \ 
     hash_PeRlHaSh += (hash_PeRlHaSh << 3); \ 
     hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \ 
     (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \ 
    } STMT_END 
+0

क्या कुछ सामान्य एल्गोरिदम का कार्यान्वयन है? – Jean

+3

@alertjean हां, कोड से टिप्पणियां कहती हैं कि यह http://burtleburtle.net/bob/hash/doobs.html का एक संस्करण है – Schwern

संबंधित मुद्दे