Php implements the Hash table function. a Hash table is also called a Hash table as one of the most important data structures. Use PHP to implement the Hash table function. PHP can simulate addition, deletion, modification, and query of Hash tables. By ing the key to a location in the array. The ing function is called the Hash function, and the array storing records is called the Hash table. The Hash function converts keys of any length and type into fixed length output. No
Php implements the Hash table function
As one of the most important data structures, a Hash table is also called a Hash table. Use PHP to implement the Hash table function. PHP can simulate addition, deletion, modification, and query of Hash tables. By ing the key to a location in the array. The ing function is called the Hash function, and the array storing records is called the Hash table.
The Hash function converts keys of any length and type into fixed length output. Different keys may have the same hash.
The time complexity of the Hash table is O (1)
Arr = new SplFixedArray ($ this-> size);}/*** Description: Simple hash algorithm. Enter the key and output the hash integer * @ param $ key * @ return int */private function simpleHash ($ key) {$ len = strlen ($ key ); // The ASCII value of each character in the key $ asciiTotal = 0; for ($ I = 0; $ I <$ len; $ I ++) {$ asciiTotal + = ord ($ key [$ I]);} return $ asciiTotal % $ this-> size;}/*** Description: assign a value * @ param $ key * @ param $ value * @ return bool */public function set ($ key, $ value) {$ hash = $ this-> simpleHash ($ key); $ this-> arr [$ hash] = $ value ; Return true;}/*** Description: value * @ param $ key * @ return mixed */public function get ($ key) {$ hash = $ this-> simpleHash ($ key); return $ this-> arr [$ hash];} public function getList () {return $ this-> arr ;} public function editSize ($ size) {$ this-> size = $ size; $ this-> arr-> setSize ($ size) ;}}?>
Next we will test HashTable.
Set ('key '. $ I, 'value '. $ I);} print_r ($ arr-> getList (); // Test 2 $ arr-> editSize (15); for ($ I = 0; $ I <15; $ I ++) {$ arr-> set ('key '. $ I, 'value '. $ I);} print_r ($ arr-> getList ();?>
After the value is changed, more elements can be stored. However, different keys may generate the same hash value, so the operation will overwrite the previous operation when assigning values. We use the zipper method to solve this conflict.
Solve conflicts by zipping. The zipper method solves the conflict by placing all keys with the same Hash value in a linked list. for example, key3 and key14 are all 0 After hash, store the two values where the key of the array is 0, in the form of a linked list. If you cannot understand my text, take a look at the following example and take a look at the printed information. What is the zipper method is a linked list.
Create a HashNode class to store key and value values and store another element with the same hash. On the same link, it takes more time to search for elements. The time complexity is O (n ).
key = $key; $this->value = $value; $this->nextNode = $nextNode; }}class NewHashTable{ private $arr; private $size = 10; public function __construct(){ $this->arr = new SplFixedArray($this->size); } private function simpleHash($key){ $asciiTotal = 0; $len = strlen($key); for($i=0; $i<$len; $i++){ $asciiTotal += ord($key[$i]); } return $asciiTotal % $this->size; } public function set($key, $value){ $hash = $this->simpleHash($key); if(isset($this->arr[$hash])){ $newNode = new HashNode($key, $value, $this->arr[$hash]); }else{ $newNode = new HashNode($key, $value, null); } $this->arr[$hash] = $newNode; return true; } public function get($key){ $hash = $this->simpleHash($key); $current = $this->arr[$hash]; while(!empty($current)){ if($current->key == $key){ return $current->value; } $current = $current->nextNode; } return NULL; } public function getList(){ return $this->arr; }}?>
Test our new HashTable.
Set ('key '. $ I, 'value '. $ I);} print_r ($ newArr-> getList (); var_dump ($ newArr-> get ('key3');?>
The above is the details about how php implements the Hash table function instance. For more information, see PHP Chinese network (www.php1.cn )!