_php tutorial on solving the problem of hash node conflict by zipper method

Source: Internet
Author: User
 Php/** Hash:: Zipper method to solve the problem of hash node storage conflict *:: 2014-07-02 *:: Small_kind*/classSmall_hash {Private $size= 20;//Hash Node Size  Private $zone=NULL;//hash Space//instantiate the function and set an initial hash node size, or the default node size if the node size is null  Final  Public function__construct ($size=NULL){    if(!Is_null($size))$this->size =$size;//    $this->zone =NewSplfixedarray ($this->size);//  }    //times33:: Calculate the hash value of the key, and the node size of the work//:: Implementation process 1, calculate key length 2, cycle length, and convert each character to ASICC code after *33  Final Private functionHASH_TIMES33 ($key){      if(Empty($key))return false;//Key==>empty      $strlen=strlen($key); $hash _val= 0;  for($i= 0;$i<$strlen;$i++){          $hash _val+= ($hash _val* 33) +Ord($key{$i}); }      return($hash _val& 0x7FFFFFFF)%$this-size; }    //Set :: Key->value corresponding value setting by Zipper method  Final  Public functionSet$key,$value){      if(Empty($key) ||Empty($value))return false;//Empty      $index=$this-&GT;HASH_TIMES33 ($key); //if the data for this node does not exist, the first set of data is added to the node      if(isset($this->zone[$index])){          //key, value, zipper object [when the node already has 1 or more sets of data, the previous data is assigned to the latest set of information]//That is, when the query is equivalent to a last-in-first-out principle, constantly put the latest data on the front, so as to form a ladder form          $data=Array($key,Serialize($value),$this->zone[$index]); }Else{          $data=Array($key,Serialize($value),NULL);//key, value, zipper object [The Zipper object is null when the node is the first time to have a value]      }      return $this->zone[$index] =$data;//Finally, the full data is assigned to the node  }    //Get :: Through the key to obtain the corresponding hash node, loop the object node, and then through the key value to match the key in the node, and return the value  Final  Public functionGet$key){      $index=$this-&GT;HASH_TIMES33 ($key); $handle=NewStdClass ();//Initialize an object      $handle= (Array)$this->zone[$index];//Query the node corresponding to the key      return $this->for_match ($key,$handle);//iterating through an array of objects into a matching function  }    //For_match:: Through key to handle iterative query, query to return, no query to return a false  Final  Public functionFor_match ($key,$handle){      if(Is_array($handle)){          if($handle[0] = =$key){              return unserialize($handle[1]);//If a value is found, the value is returned}Else{              return $this->for_match ($key,$handle[2]);//Otherwise, continue iterating over the method          }      }  }}$hand=NewSmall_hash ();$hand->set (' Libin ', ' WWW.BAIDU.COM ');$hand->set (' d24150ddd ', ' WWW.PHP.COM ');Var_dump($hand->get (' Libin '));Var_dump($hand->get (' d24150ddd '));?>

http://www.bkjia.com/PHPjc/821272.html www.bkjia.com true http://www.bkjia.com/PHPjc/821272.html techarticle PHP/* * Hash:: Zipper method to solve the problem of hash node storage conflict *:: 2014-07-02 *:: Small_kind */class Small_hash {private $size =;//hash node size Private $zone = null; H ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.