PHP Implementation Hash Table _php Tutorial

Source: Internet
Author: User

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

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