PHP Implementation Hash Table
A simple hash table implementation ....
Size $this->collection = new Splfixedarray ($bucketsSize); }//Generate hash value as the location where the data is stored private function _hashalgorithm ($key) {$length = strlen ($key); $hashValue = 0; for ($i =0; $i < $length; $i + +) {$hashValue + ord ($key [$i]); } return ($hashValue% ($this->size)); }//Store the corresponding value in the appropriate location public function set ($key, $val) {$index = $this->_hashalgorithm ($key); $this->collection[$index] = $val; }//Generates a hash value based on the key, and finds the corresponding value public function get ($key) {$index = $this->_hashalgorithm ($key); return $this->collection[$index]; }//Delete a value, successfully returned 1, failed to return 0 public function del ($key) {$index = $this->_hashalgorithm ($key); if (Isset ($this->collection[$index])) {unset ($this->collection[$index]); return 1; } else {return 0; }}//To determine if a value exists, there is a return of 1, there is no return 0 public function exist ($key) {$index = $tHis->_hashalgorithm ($key); if ($this->collection[$index]) {return 1; } else {return 0; }}//Returns the number of keys public function size () {$size = 0; $length = count ($this->collection); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {$size + +; }} return $size; }//Returns the sequence of value of public function val () {$size = 0; $length = count ($this->collection); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {echo $this->collection[$i]. "
"; }}}//Sort output public function sort ($type =1) {$length = count ($this->collection); $temp = Array (); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {$temp [] = $this->collection[$ I]; }} switch ($type) {Case 1://Normal comparison sort ($temp, sort_regular); Break Case 2://Compare Sort ($temp, sort_numeric) by number; Break Compare by string case 3:sort ($temp, sort_string); Break Compare Case 4:sort ($temp, sort_locale_string) based on the local character encoding environment; Break } echo ""; Print_r ($temp); }//Reverse output Public Function rev ($type =1) {$length = count ($this->collection); $temp = Array (); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {$temp [] = $this->collection[$ I]; }} switch ($type) {Case 1://Normal comparison rsort ($temp, sort_regular); Break Case 2://Rsort ($temp, sort_numeric) according to the numbers; Break Compare by string case 3:rsort ($temp, sort_string); Break Compare Case 4:rsort ($temp, sort_locale_string) based on the local character encoding environment; Break } echo ""; Print_r ($temp);
"; $list->rev (3);
http://www.bkjia.com/PHPjc/1011356.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011356.html techarticle PHP Implementation Hash Table//A simple hash list implementation .... Size $this->collection = new Splfixedarray ($bucketsSize); }//Generate hash value as the location to store data private ...