国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php hash算法實現(xiàn)數(shù)組的方法

2024-05-04 21:48:48
字體:
供稿:網(wǎng)友

PHP中使用最多的非Array莫屬了,那Array是如何實現(xiàn)的?在PHP內(nèi)部Array通過一個hashtable來實現(xiàn),其中使用鏈接法解決hash沖突的問題,這樣最壞情況下,查找Array元素的復(fù)雜度為O(N),最好則為1.

  1. static inline ulong zend_inline_hash_func(const char *arKey, uint nKeyLength)  
  2. {  
  3. register ulong hash = 5381; //此處初始值的設(shè)置有什么玄機(jī)么?  
  4. /* variant with the hash unrolled eight times */  
  5. for (; nKeyLength >= 8; nKeyLength -= 8) { //這種step=8的方式是為何?  
  6. hash = ((hash << 5) + hash) + *arKey++;  
  7. hash = ((hash << 5) + hash) + *arKey++;  
  8. hash = ((hash << 5) + hash) + *arKey++;  
  9. hash = ((hash << 5) + hash) + *arKey++; //比直接*33要快  
  10. hash = ((hash << 5) + hash) + *arKey++;  
  11. hash = ((hash << 5) + hash) + *arKey++;  
  12. hash = ((hash << 5) + hash) + *arKey++;  
  13. hash = ((hash << 5) + hash) + *arKey++;  
  14. }  
  15. switch (nKeyLength) {  
  16. case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ //此處是將剩余的字符hash //開源代碼Vevb.com 
  17. case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */  
  18. case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */  
  19. case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */  
  20. case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */  
  21. case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */  
  22. case 1: hash = ((hash << 5) + hash) + *arKey++; break;  
  23. case 0: break;  
  24. EMPTY_SWITCH_DEFAULT_CASE()  
  25. }  
  26. return hash;//返回hash值  
  27. }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 衡山县| 泸西县| 运城市| 鄂伦春自治旗| 浮山县| 洛浦县| 钦州市| 兴国县| 湖南省| 射阳县| 福清市| 峨眉山市| 开封市| 自治县| 大渡口区| 靖宇县| 同德县| 犍为县| 平南县| 葵青区| 临澧县| 沧州市| 兴宁市| 阿巴嘎旗| 天峨县| 高雄县| 乌审旗| 阳东县| 南汇区| 潢川县| 奉贤区| 湖南省| 两当县| 信宜市| 兴业县| 宽城| 墨玉县| 西藏| 长丰县| 布拖县| 本溪|