_php tutorial for arrays in PHP with hash

Source: Internet
Author: User
The most used non-array in PHP is, how is the array implemented? In the PHP internal array is implemented by a Hashtable, which uses the link method to solve the problem of hash conflicts, in the worst case, the complexity of finding the array element is O (N), preferably 1.
And the method of calculating the hash value of the string is as follows, the source code is extracted for inspection:
Copy CodeThe code is as follows:
Static inline ulong Zend_inline_hash_func (const char *arkey, UINT nkeylength)
{
Register ULONG hash = 5381; Is there any mystery about the setting of the initial value here?
/* Variant with the hash unrolled eight times */
for (; nkeylength >= 8; nkeylength-= 8) {//How is this step=8?
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++; Faster than direct *33.
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++;
hash = ((hash << 5) + hash) + *arkey++;
}
Switch (nkeylength) {
Case 7:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... *///Here is a hash of the remaining characters
Case 6:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */
Case 5:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */
Case 4:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */
Case 3:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */
Case 2:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */
Case 1:hash = ((hash << 5) + hash) + *arkey++; Break
Case 0:break;
Empty_switch_default_case ()
}
Return hash;//returns the hash value
}

PS: There are still two unknown points for the following functions:
Why is the hash = 5381 set?
Is this step=8 cycle a form of efficiency?

http://www.bkjia.com/PHPjc/323978.html www.bkjia.com true http://www.bkjia.com/PHPjc/323978.html techarticle The most used non-array in PHP is, how is the array implemented? Inside the PHP array is implemented by a Hashtable, which uses the link method to solve the problem of hash conflict, so the worst ...

  • 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.