PHP Realization Hash Table Function Example detailed _php skill

Source: Internet
Author: User
Tags data structures hash ord php class strlen

PHP Implementation hash Table function

As one of the most important data structures, hash table is also called hash list. Use PHP to implement the hash table functionality. PHP can simulate the implementation of hash table additions and deletions to check. Accessed by mapping the key to a location in the array. A mapping function is called a hash function, and an array of records is called a hash table.

The hash function converts any length and type key to a fixed length output. The different key may have the same hash.
The time complexity of the hash table is O (1)

 <?php class hashtable{Private $arr = Array ();
  Private $size = 10; The Public Function __construct () {//splfixedarray creates an array that is more efficient than a generic array () because it is closer to the array of C.
  You need to specify dimensions $this->arr = new Splfixedarray ($this->size) when you create it. /** * Description: Simple hash algorithm.
    Input key, output hash after the integer * @param $key * @return int/private Function Simplehash ($key) {$len = strlen ($key);
    The value of ASCII corresponding to each character in the key $asciiTotal = 0;
    For ($i =0 $i < $len; $i + +) {$asciiTotal + + ord ($key [$i]);
  Return $asciiTotal% $this->size; /** * Description: Assignment * @param $key * @param $value * @return bool/Public function set ($key, $valu
    E) {$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;
    The Public Function editsize ($size) {$this->size = $size;
  $this->arr->setsize ($size); }}?> 

The following is a test of our hashtable.

<?php
//test 1
$arr = new HashTable ();
For ($i =0 $i <15; $i + +) {
  $arr->set (' key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());

Test 2
$arr->editsize ();
For ($i =0 $i <15; $i + +) {
  $arr->set (' key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());
>

After you change the value, you can store more elements. But there are still different keys that may produce the same hash value, then the operation will overwrite the problem before the assignment. The problem of this kind of conflict we use zipper method to solve.

The Zipper method solves the conflict. The method of zipper resolution is to put all the same hash value of the key in a linked list, such as Key3 and KEY14 after the hash is 0, then the array of key to 0 store the two values, in the form of the linked list. If you can't understand my text, take a look at the example below and see the print information. Zipper method is what, is linked list.

Creates a Hashnode class that stores the value of key and value, and stores another element of the same hash. On the same chain, the more time-consuming the lookup element is. Time complexity is O (n).

 <?php class hashnode{public $key;
  Public $value;
  Public $nextNode;
    Public function __construct ($key, $value, $nextNode =null) {$this->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;
    The 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;
  The 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;
  The Public Function getlist () {return $this->arr; }}?> 

To test our new Hashtable.

<?php
//test 1
$newArr = new newhashtable ();
For ($i =0 $i <30; $i + +) {
  $newArr->set (' key '. $i, ' value '. $i);
}
Print_r ($newArr->getlist ());
Var_dump ($newArr->get (' Key3 '));
? >

Thank you for reading, I hope to help you, thank you for your support for this site!

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.