当前位置:网站首页>PHP中Array的hash函数实现
PHP中Array的hash函数实现
2022-07-05 11:22:00 【全栈程序员站长】
PHP中使用最多的非Array莫属了,那Array是如何实现的?
在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Array元素的复杂度为O(N),最好则为1.
而其计算字符串hash值的方法如下,将源码摘出来以供查备:
ps:对于以下函数,仍有两点不明:
1. hash = 5381设置的理由?
2. 这种step=8的循环方式是为了效率么?
Php代码
- static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)
- {
- register ulong hash = 5381; //此处初始值的设置有什么玄机么?
- /* variant with the hash unrolled eight times */
- for (; nKeyLength >= 8; nKeyLength -= 8) { //这种step=8的方式是为何?
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++; //比直接*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… */ //此处是将剩余的字符hash
- 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; //返回hash值
- }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109530.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
Question bank and answers of special operation certificate examination for main principals of hazardous chemical business units in 2022
[advertising system] parameter server distributed training
LSTM applied to MNIST dataset classification (compared with CNN)
In the last process before the use of the risk control model, 80% of children's shoes are trampled here
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
Advanced technology management - what is the physical, mental and mental strength of managers
COMSOL--三维随便画--扫掠
[office] eight usages of if function in Excel
Go language learning notes - analyze the first program
Detailed explanation of MATLAB cov function
随机推荐
Sklearn model sorting
msfconsole命令大全,以及使用说明
基础篇——基础项目解析
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
Redis如何实现多可用区?
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
deepfake教程
The art of communication III: Listening between people
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
2022 mobile crane driver examination question bank and simulation examination
Bidirectional RNN and stacked bidirectional RNN
LSTM applied to MNIST dataset classification (compared with CNN)
Pytorch training process was interrupted
About the use of Vray 5.2 (self research notes)
[advertising system] incremental training & feature access / feature elimination
7.2每日学习4
String
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Data type
Bracket matching problem (STL)