当前位置:网站首页>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
边栏推荐
- AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
- MFC pet store information management system
- Sklearn model sorting
- Question and answer 45: application of performance probe monitoring principle node JS probe
- Basics - rest style development
- Stop saying that microservices can solve all problems!
- Advanced technology management - what is the physical, mental and mental strength of managers
- 2022 mobile crane driver examination question bank and simulation examination
- uniapp
- Intelligent metal detector based on openharmony
猜你喜欢
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
高校毕业求职难?“百日千万”网络招聘活动解决你的难题
无密码身份验证如何保障用户隐私安全?
龙蜥社区第九次运营委员会会议顺利召开
[office] eight usages of if function in Excel
Codeforces Round #804 (Div. 2)
Basics - rest style development
How did the situation that NFT trading market mainly uses eth standard for trading come into being?
Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
Intelligent metal detector based on openharmony
随机推荐
7.2每日学习4
Modulenotfounderror: no module named 'scratch' ultimate solution
I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
deepfake教程
Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
R3live series learning (IV) r2live source code reading (2)
Question and answer 45: application of performance probe monitoring principle node JS probe
How can edge computing be combined with the Internet of things?
spark调优(一):从hql转向代码
Bidirectional RNN and stacked bidirectional RNN
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
Beego cross domain problem solution - successful trial
Summary of thread and thread synchronization under window
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Sklearn model sorting
Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
OneForAll安装使用
Leetcode 185 All employees with the top three highest wages in the Department (July 4, 2022)
解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..