当前位置:网站首页>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
边栏推荐
- Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
- Codeforces Round #804 (Div. 2)
- matlab cov函数详解
- Harbor镜像仓库搭建
- -26374 and -26377 errors during coneroller execution
- 2022 Pengcheng cup Web
- Data types ntext and varchar are incompatible in the not equal to operator - 95 small pang
- 9、 Disk management
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- Intelligent metal detector based on openharmony
猜你喜欢
随机推荐
基础篇——基础项目解析
Solve the problem of slow access to foreign public static resources
DDR4硬件原理图设计详解
Process control
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
COMSOL--建立几何模型---二维图形的建立
How to make full-color LED display more energy-saving and environmental protection
【爬虫】wasm遇到的bug
Question and answer 45: application of performance probe monitoring principle node JS probe
2022 mobile crane driver examination question bank and simulation examination
[Oracle] use DataGrid to connect to Oracle Database
Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
分类TAB商品流多目标排序模型的演进
Detailed explanation of DDR4 hardware schematic design
Lombok 同时使⽤@Data和@Builder 的坑,你中招没?
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
COMSOL--三维随便画--扫掠
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
How to close the log window in vray5.2