当前位置:网站首页>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
边栏推荐
- LSTM applied to MNIST dataset classification (compared with CNN)
- Basic testing process of CSDN Software Testing Introduction
- 【广告系统】增量训练 & 特征准入/特征淘汰
- Summary of thread and thread synchronization under window
- [SWT component] content scrolledcomposite
- 基础篇——基础项目解析
- Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
- 我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
- uboot的启动流程:
- How to introduce devsecops into enterprises?
猜你喜欢

DDR4的特性与电气参数

Detailed explanation of DDR4 hardware schematic design

2022 mobile crane driver examination question bank and simulation examination

Bidirectional RNN and stacked bidirectional RNN

Evolution of multi-objective sorting model for classified tab commodity flow

R3live series learning (IV) r2live source code reading (2)

【广告系统】Parameter Server分布式训练

2022 chemical automation control instrument examination questions and online simulation examination

Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt

Modulenotfounderror: no module named 'scratch' ultimate solution
随机推荐
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
Advanced scaffold development
LSTM applied to MNIST dataset classification (compared with CNN)
基础篇——REST风格开发
百问百答第45期:应用性能探针监测原理-node JS 探针
Startup process of uboot:
9、 Disk management
Manage multiple instagram accounts and share anti Association tips
SLAM 01. Modeling of human recognition Environment & path
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
[office] eight usages of if function in Excel
R3Live系列学习(四)R2Live源码阅读(2)
2022 mobile crane driver examination question bank and simulation examination
About the use of Vray 5.2 (self research notes)
华为设备配置信道切换业务不中断
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Harbor镜像仓库搭建
Solve the problem of slow access to foreign public static resources
Stop saying that microservices can solve all problems!
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things