search
HomeBackend DevelopmentPHP TutorialZipper method solves problems related to Hash node conflicts_PHP tutorial

<?<span>php
</span><span>/*</span><span>
 * hash::拉链法解决hash节点存储冲突问题
 * ::2014-07-02
 * ::Small_Kind
 </span><span>*/</span>
<span>class</span><span> small_hash {
    
  </span><span>private</span> <span>$size</span> = 20;<span>//</span><span>hash节点大小</span>
  <span>private</span> <span>$zone</span> = <span>null</span>;<span>//</span><span>hash空间
    
  //实例化函数,并设置一个初始hash节点大小,如果节点大小为null,则为默认节点大小</span>
  <span>final</span> <span>public</span> <span>function</span> __construct(<span>$size</span> = <span>null</span><span>){
    </span><span>if</span>(!<span>is_null</span>(<span>$size</span>))<span>$this</span>->size = <span>$size</span>;<span>//
</span>    <span>$this</span>->zone = <span>new</span> SplFixedArray(<span>$this</span>->size);<span>//
</span><span>  }
  
  </span><span>//</span><span>times33::计算key的hash值,并进行节点大小的取模工作
  //::实现流程1、计算key长度2、循环长度,并将每个字符转换成asicc码后*33</span>
  <span>final</span> <span>private</span> <span>function</span> hash_times33(<span>$key</span><span>){
      </span><span>if</span>(<span>empty</span>(<span>$key</span>))<span>return</span> <span>false</span>;<span>//</span><span>key==>empty</span>
      <span>$strlen</span> = <span>strlen</span>(<span>$key</span><span>);
      </span><span>$hash_val</span> = 0<span>;
      </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$strlen</span>;<span>$i</span>++<span>){
          </span><span>$hash_val</span> += (<span>$hash_val</span> * 33) + <span>ord</span>(<span>$key</span>{<span>$i</span><span>});
      }
      </span><span>return</span> (<span>$hash_val</span> & 0x7FFFFFFF) % <span>$this</span>-><span>size;
  }
  
  </span><span>//</span><span>set::通过拉链法进行key->value对应的值设置</span>
  <span>final</span> <span>public</span> <span>function</span> set(<span>$key</span>,<span>$value</span><span>){
      </span><span>if</span>(<span>empty</span>(<span>$key</span>) || <span>empty</span>(<span>$value</span>))<span>return</span> <span>false</span>;<span>//</span><span>empty</span>
      <span>$index</span> = <span>$this</span>->hash_times33(<span>$key</span><span>);
      </span><span>//</span><span>如果不存在此节点的数据,则向该节点添加第一组数据</span>
      <span>if</span>(<span>isset</span>(<span>$this</span>->zone[<span>$index</span><span>])){
          </span><span>//</span><span>key、value、拉链对象[当该节点已存在1个或多组数据的时候,将之前的数据赋值给最新的一组data]
          //也就是说在查询时候相当于一种后进先出的原则,不断将最新的数据放在最前面,以此形成一种阶梯形式</span>
          <span>$data</span> = <span>array</span>(<span>$key</span>,<span>serialize</span>(<span>$value</span>),<span>$this</span>->zone[<span>$index</span><span>]);
      }</span><span>else</span><span>{
          </span><span>$data</span> = <span>array</span>(<span>$key</span>,<span>serialize</span>(<span>$value</span>),<span>null</span>);<span>//</span><span>key、value、拉链对象[当该节点是第一次有值的时候,拉链对象为null]</span>
<span>      }
      </span><span>return</span> <span>$this</span>->zone[<span>$index</span>] = <span>$data</span>;<span>//</span><span>最后将完整的data赋值给该节点</span>
<span>  }
  
  </span><span>//</span><span>get::通过key获取对应hash后对应的的节点,循环该对象节点,然后通过key值匹配节点中的key,并将值进行返回</span>
  <span>final</span> <span>public</span> <span>function</span> get(<span>$key</span><span>){
      </span><span>$index</span>  = <span>$this</span>->hash_times33(<span>$key</span><span>);
      </span><span>$handle</span> = <span>new</span> stdClass();<span>//</span><span>初始化一个对象</span>
      <span>$handle</span> = (<span>array</span>)<span>$this</span>->zone[<span>$index</span>];<span>//</span><span>查询该key对应的节点</span>
      <span>return</span> <span>$this</span>->for_match(<span>$key</span>,<span>$handle</span>);<span>//</span><span>将对象数组送入匹配函数进行迭代查询</span>
<span>  }
  
  </span><span>//</span><span>for_match::通过key对handle进行迭代查询,查询到了即返回,没有查询到则返回一个false</span>
  <span>final</span> <span>public</span> <span>function</span> for_match(<span>$key</span>,<span>$handle</span><span>){
      </span><span>if</span>(<span>is_array</span>(<span>$handle</span><span>)){
          </span><span>if</span>(<span>$handle</span>[0] == <span>$key</span><span>){
              </span><span>return</span> <span>unserialize</span>(<span>$handle</span>[1]);<span>//</span><span>如果找到值了,则对值进行返回</span>
          }<span>else</span><span>{
              </span><span>return</span> <span>$this</span>->for_match(<span>$key</span>,<span>$handle</span>[2]);<span>//</span><span>否则继续迭代该方法</span>
<span>          }
      }
  }
}

</span><span>$hand</span> = <span>new</span><span> small_hash();
</span><span>$hand</span>->set('Libin','WWW.BAIDU.COM'<span>);
</span><span>$hand</span>->set('d24150ddd','WWW.PHP.COM'<span>);
</span><span>var_dump</span>(<span>$hand</span>->get('Libin'<span>));
</span><span>var_dump</span>(<span>$hand</span>->get('d24150ddd'<span>));
</span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/821272.htmlTechArticle?php /* * hash::zipper method solves hash node storage conflict problem* ::2014-07-02 * ::Small_Kind */ class small_hash { private $size = 20; // hash node size private $zone = null; // h...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools