当前位置:网站首页>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
边栏推荐
- R3live series learning (IV) r2live source code reading (2)
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- 跨境电商是啥意思?主要是做什么的?业务模式有哪些?
- 2022 chemical automation control instrument examination questions and online simulation examination
- Lombok 同时使⽤@Data和@Builder 的坑,你中招没?
- [advertising system] parameter server distributed training
- uboot的启动流程:
- Array
- 9、 Disk management
- DDRx寻址原理
猜你喜欢
Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
About the use of Vray 5.2 (self research notes) (II)
COMSOL--三维图形的建立
Lombok 同时使⽤@Data和@Builder 的坑,你中招没?
Ddrx addressing principle
Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
go语言学习笔记-分析第一个程序
Detailed explanation of DDR4 hardware schematic design
Codeforces Round #804 (Div. 2)
DDR4的特性与电气参数
随机推荐
2022 t elevator repair operation certificate examination questions and answers
Technology sharing | common interface protocol analysis
Operators
Paradigm in database: first paradigm, second paradigm, third paradigm
7.2 daily study 4
[first release in the whole network] (tips for big tables) sometimes it takes only 1 minute for 2 hours of SQL operation
LSTM applied to MNIST dataset classification (compared with CNN)
COMSOL--建立几何模型---二维图形的建立
如何通俗理解超级浏览器?可以用于哪些场景?有哪些品牌?
【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
Go language learning notes - analyze the first program
Oneforall installation and use
如何让全彩LED显示屏更加节能环保
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
regular expression
TSQL – identity column, guid, sequence
Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
Question and answer 45: application of performance probe monitoring principle node JS probe
2022 mobile crane driver examination question bank and simulation examination