Implement hash table in PHP

Source: Internet
Author: User

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 );


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.