当前位置:网站首页>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
边栏推荐
- Three suggestions for purchasing small spacing LED display
- shell脚本文件遍历 str转数组 字符串拼接
- Codeforces Round #804 (Div. 2)
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
- 管理多个Instagram帐户防关联小技巧大分享
- COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
- DDoS attack principle, the phenomenon of being attacked by DDoS
- 技术管理进阶——什么是管理者之体力、脑力、心力
- 【广告系统】Parameter Server分布式训练
- 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
[office] eight usages of if function in Excel
[advertising system] incremental training & feature access / feature elimination
The ninth Operation Committee meeting of dragon lizard community was successfully held
Cdga | six principles that data governance has to adhere to
Huawei equipment configures channel switching services without interruption
go语言学习笔记-分析第一个程序
AutoCAD -- mask command, how to use CAD to locally enlarge drawings
[crawler] Charles unknown error
COMSOL--三维图形的建立
随机推荐
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
sklearn模型整理
Codeforces Round #804 (Div. 2)
SLAM 01. Modeling of human recognition Environment & path
Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
POJ 3176-Cow Bowling(DP||记忆化搜索)
An error is reported in the process of using gbase 8C database: 80000305, host IPS long to different cluster. How to solve it?
Unity Xlua MonoProxy Mono代理类
我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
COMSOL--建立几何模型---二维图形的建立
使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
Basic part - basic project analysis
CDGA|数据治理不得不坚持的六个原则
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Three suggestions for purchasing small spacing LED display
spark调优(一):从hql转向代码
TSQL – identity column, guid, sequence
The ninth Operation Committee meeting of dragon lizard community was successfully held
【爬虫】wasm遇到的bug
高校毕业求职难?“百日千万”网络招聘活动解决你的难题