当前位置:网站首页>Implementation of array hash function in PHP
Implementation of array hash function in PHP
2022-07-05 11:27:00 【Full stack programmer webmaster】
PHP The most used non Array It must be , that Array How is it realized ?
stay PHP Inside Array Through one hashtable To achieve , Among them, the link method is used to solve hash The question of conflict , In the worst case , lookup Array The complexity of the element is O(N), The best is 1.
And its calculation string hash The method of calculating the value is as follows , Pick out the source code for reference :
ps: For the following functions , Two points remain unclear :
1. hash = 5381 The reason for setting ?
2. such step=8 Is the circulation mode of for efficiency ?
Php Code
- static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)
- {
- register ulong hash = 5381; // Is there any mystery about the setting of the initial value here ?
- /* variant with the hash unrolled eight times */
- for (; nKeyLength >= 8; nKeyLength -= 8) { // such step=8 The way is ?
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++;
- hash = ((hash << 5) + hash) + *arKey++; // Than directly *33 Be quick
- 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… */ // Here are the remaining characters 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; // return hash value
- }
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109530.html Link to the original text :https://javaforall.cn
边栏推荐
- 数据库三大范式
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
- Question and answer 45: application of performance probe monitoring principle node JS probe
- Paradigm in database: first paradigm, second paradigm, third paradigm
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- 购买小间距LED显示屏的三个建议
- 不要再说微服务可以解决一切问题了!
- Cdga | six principles that data governance has to adhere to
- 紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
猜你喜欢

DDR4的特性与电气参数

7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow

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

pytorch训练进程被中断了

【Office】Excel中IF函数的8种用法

龙蜥社区第九次运营委员会会议顺利召开

Three paradigms of database

How did the situation that NFT trading market mainly uses eth standard for trading come into being?

数据库三大范式

DDRx寻址原理
随机推荐
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
管理多个Instagram帐户防关联小技巧大分享
[crawler] bugs encountered by wasm
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
Detailed explanation of DDR4 hardware schematic design
DDoS attack principle, the phenomenon of being attacked by DDoS
如何让全彩LED显示屏更加节能环保
[LeetCode] Wildcard Matching 外卡匹配
Leetcode 185 All employees with the top three highest wages in the Department (July 4, 2022)
R3Live系列学习(四)R2Live源码阅读(2)
COMSOL -- 3D casual painting -- sweeping
如何将 DevSecOps 引入企业?
Golang application topic - channel
Manage multiple instagram accounts and share anti Association tips
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
华为设备配置信道切换业务不中断
Spark Tuning (I): from HQL to code
C#实现WinForm DataGridView控件支持叠加数据绑定