Using PHP to implement hash table function

Source: Internet
Author: User

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)

The code is as follows Copy Code

<?php

Class hashtable{
Private $arr = Array ();
Private $size = 10;
Public Function __construct () {
Splfixedarray creates an array that is more efficient than a generic array () because it is closer to the array of C. Need to specify dimensions when creating
$this->arr = new Splfixedarray ($this->size);
}

   /**
     * Description: Simple hash algorithm. Input key, output the integer after the hash
     * @param $key
     * @return int
      */
    Private Function Simplehash ($key) {
         $len = strlen ($key);
The value of ASCII for each character in the        //key
         $asciiTotal = 0;
        for ($i =0; $i < $len; $i + +) {
             $asciiTotal + + ord ($key [$i]);
       }
        return $asciiTotal% $this->size;
   }

/**
* Description: Assigning value
* @param $key
* @param $value
* @return BOOL
*/
Public function set ($key, $value) {
$hash = $this->simplehash ($key);
$this->arr[$hash] = $value;
return true;
}

/**
* Description: Take value
* @param $key
* @return Mixed
*/
Public function Get ($key) {
$hash = $this->simplehash ($key);
return $this->arr[$hash];
}

Public Function GetList () {
return $this->arr;
}

Public Function Editsize ($size) {
$this->size = $size;
$this->arr->setsize ($size);
}
}
?>

The following is a test of our hashtable.

The code is as follows Copy Code

<?php
//test 1
$arr = new HashTable ()
for ($i =0; $i <15; $i + +) {
    $arr->set (' Key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());
//splfixedarray Object
//(
//    [0] => Value14
//    [1] => value4
//    [2] => value5
//    [3] => VA Lue6
//    [4] => value7
//    [5] => Value8
//    [6] =&G T Value10
//    [7] => value11
//    [8] => value12
//    [ 9] => value13
//)
///The different key may produce the same hash value, then the operation will overwrite the operation before the assignment.

Test 2
$arr->editsize (15);
For ($i =0 $i <15; $i + +) {
$arr->set (' key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());
Splfixedarray Object
//(
[0] => Value14
[1] => Value4
[2] => Value0
[3] => value1
[4] => value2
[5] => Value3
[6] => Value10
[7] => Value11
[8] => Value12
[9] => Value13
[Ten] => value14
[One] => value9
[=>]
[=>]
[=>]
//)
?>

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

The code is as follows Copy Code

&lt;?php


Class hashnode{


Public $key;


Public $value;


Public $nextNode;


Public function __construct ($key, $value, $nextNode =null) {


$this-&gt;key = $key;


$this-&gt;value = $value;


$this-&gt;nextnode = $nextNode;


}


}


Class newhashtable{


Private $arr;


Private $size = 10;


Public Function __construct () {


$this-&gt;arr = new Splfixedarray ($this-&gt;size);


}


Private Function Simplehash ($key) {


$asciiTotal = 0;


$len = strlen ($key);


for ($i =0; $i &lt; $len; $i + +) {


$asciiTotal + + ord ($key [$i]);


}


Return $asciiTotal% $this-&gt;size;


}


Public function set ($key, $value) {


$hash = $this-&gt;simplehash ($key);


if (Isset ($this-&gt;arr[$hash])) {


$newNode = new Hashnode ($key, $value, $this-&gt;arr[$hash]);


}else{


$newNode = new Hashnode ($key, $value, NULL);


}


$this-&gt;arr[$hash] = $newNode;


return true;


}


Public function Get ($key) {


$hash = $this-&gt;simplehash ($key);


$current = $this-&gt;arr[$hash];


while (!empty ($current)) {


if ($current-&gt;key = = $key) {


return $current-&gt;value;


}


$current = $current-&gt;nextnode;


}


return NULL;


}


Public Function GetList () {


return $this-&gt;arr;


}


}


?&gt;

To test our new Hashtable.

The code is as follows Copy Code
&lt;?php


Test 1


$NEWARR = new Newhashtable ();


For ($i =0 $i &lt;30; $i + +) {


$NEWARR-&gt;set (' key '. $i, ' value '. $i);


}


Print_r ($newArr-&gt;getlist ());


Var_dump ($newArr-&gt;get (' Key3 '));


Splfixedarray Object


//(


[0] =&gt; Hashnode Object


//(


[Key] =&gt; key23


[Value] =&gt; value23


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; KEY14


[Value] =&gt; value14


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key3


[Value] =&gt; value3


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[1] =&gt; Hashnode Object


//(


[Key] =&gt; key24


[Value] =&gt; Value24


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; KEY15


[Value] =&gt; VALUE15


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key4


[Value] =&gt; value4


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[2] =&gt; Hashnode Object


//(


[Key] =&gt; key25


[Value] =&gt; value25


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; KEY16


[Value] =&gt; Value16


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key5


[Value] =&gt; value5


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[3] =&gt; Hashnode Object


//(


[Key] =&gt; key26


[Value] =&gt; Value26


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; KEY17


[Value] =&gt; Value17


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key6


[Value] =&gt; value6


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[4] =&gt; Hashnode Object


//(


[Key] =&gt; key27


[Value] =&gt; Value27


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key18


[Value] =&gt; value18


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key7


[Value] =&gt; Value7


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[5] =&gt; Hashnode Object


//(


[Key] =&gt; key28


[Value] =&gt; value28


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; key19


[Value] =&gt; value19


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key8


[Value] =&gt; Value8


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[6] =&gt; Hashnode Object


//(


[Key] =&gt; Key29


[Value] =&gt; value29


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key10


[Value] =&gt; Value10


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key9


[Value] =&gt; value9


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[7] =&gt; Hashnode Object


//(


[Key] =&gt; Key20


[Value] =&gt; VALUE20


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key11


[Value] =&gt; value11


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key0


[Value] =&gt; value0


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[8] =&gt; Hashnode Object


//(


[Key] =&gt; Key21


[Value] =&gt; VALUE21


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key12


[Value] =&gt; value12


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key1


[Value] =&gt; value1


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


[9] =&gt; Hashnode Object


//(


[Key] =&gt; Key22


[Value] =&gt; Value22


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; KEY13


[Value] =&gt; value13


[NextNode] =&gt; Hashnode Object


//(


[Key] =&gt; Key2


[Value] =&gt; value2


[NextNode] =&gt;


//                        )


//


//                )


//


//        )


//


//)


String (6) "Value3"


?&gt;

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.