Implement hash table in PHP
// A simple hash table implementation ....
Size; $ this-> collection = new SplFixedArray ($ bucketsSize);} // generates a hash value, which is used as the location of the stored data. 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 public function set ($ key, $ val) {$ index = $ this-> _ hashAlgorithm ($ key ); $ this-> collection [$ index] = $ val;} // generate a hash value based on the key, and then find the corresponding value public function get ($ key) {$ index = $ this-> _ hashAlgorithm ($ key); return $ this-> collection [$ index] ;}// if a value is deleted, 1 is returned, 0 public function del ($ key) {$ index = $ this-> _ hashAlgorithm ($ key); if (isset ($ this-> collection [$ index]) {unset ($ this-> collection [$ index]); return 1 ;}else {return 0 ;}// you can check whether a value exists. If yes, 1 is returned, 0 public function exist ($ key) {$ index = $ this-> _ hashAlgorithm ($ key); if ($ this-> collection [$ index]) is returned if {return 1 ;}else {return 0 ;}// number of keys returned public function size () {$ size = 0; $ length = count ($ this-> collection); for ($ I = 0; $ I <$ length; $ I ++) {if ($ this-> collection [$ I]) {$ size ++ ;}}return $ size ;}// returns the public function val () sequence of values () {$ size = 0; $ length = count ($ this-> collection); for ($ I = 0; $ I <$ length; $ I ++) {if ($ this-> collection [$ I]) {echo $ this-> collection [$ I]."
";}}// Sort the 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 according to the local character encoding environment case 4: sort ($ temp, SORT_LOCALE_STRING); break;} echo""; Print_r ($ temp);} // The public function rev ($ type = 1) {$ length = count ($ this-> collection) is output in reverse order ); $ 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: // compare rsort ($ temp, SORT_NUMERIC) by number; break; // compare by string case 3: rsort ($ temp, SORT_STRING); break; // compare according to the local character encoding environment case 4: rsort ($ temp, SORT_LOCALE_STRING); break;} echo""; Print_r ($ temp) ;}// simple test $ list = new hashTable (200); $ list-> set (" zero "," zero compare "); $ list-> set ("one", "first test"); $ list-> set ("two", "second test "); $ list-> set ("three", "three test"); $ list-> set ("four", "fouth test "); echo $ list-> val (); echo "after sorted:
"; $ List-> rev (3 );