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->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->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 ...