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