当前位置:网站首页>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
边栏推荐
- How to understand super browser? What scenarios can it be used in? What brands are there?
- 中非 钻石副石怎么镶嵌,才能既安全又好看?
- 解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
- 基于Lucene3.5.0怎样从TokenStream获得Token
- 【爬虫】wasm遇到的bug
- 居家办公那些事|社区征文
- Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
- R3Live系列学习(四)R2Live源码阅读(2)
- 技术分享 | 常见接口协议解析
- Web API配置自定义路由
猜你喜欢
随机推荐
C # to obtain the filtered or sorted data of the GridView table in devaexpress
MySQL 巨坑:update 更新慎用影响行数做判断!!!
解决grpc连接问题Dial成功状态为TransientFailure
Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
ZCMU--1390: 队列问题(1)
Advanced technology management - what is the physical, mental and mental strength of managers
Huawei equipment configures channel switching services without interruption
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
Bracket matching problem (STL)
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Repair animation 1K to 8K
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
CDGA|数据治理不得不坚持的六个原则
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
如何让全彩LED显示屏更加节能环保
Dspic33ep clock initialization program
力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
不要再说微服务可以解决一切问题了!
【全网首发】(大表小技巧)有时候 2 小时的 SQL 操作,可能只要 1 分钟








