The hash algorithm of PHP core technology and best practice

Source: Internet
Author: User
Tags spl ord php class

The hash algorithm of PHP core technology and best practice

The hash table is also called a hash list, which accesses the record by mapping the keyword key to a location in the array to speed up the lookup. This mapping function is called the hash function, and the array that holds the record is called the hash table.

1. Hash function

The function is to transform the input of any length into a fixed length output through the hash algorithm, which is the hash value. This conversion is a compression map, that is, the hash is worth much less space than the input space, not input may be hashed into the same output, and it is not possible to from the hash value to uniquely determine the input value.

A good hash function should satisfy the following conditions: Each keyword can be evenly distributed to any location in the hash table, and it does not conflict with other keywords that have been hashed into the hash table. This is the most difficult hash function to achieve.

2. Hash algorithm

1) Direct Extraction method

The direct method is relatively simple, the key word K divided by the size of the hash table to the remainder, the algorithm is as follows:

H (k) = k mod m

For example: the size of the hash table is m=12, given the keyword is k=100, then h (k) = 4. This algorithm is a residual operation, the speed is relatively fast.

2) Product Rounding Method

The product rounding method first uses the keyword K multiplied by a constant a (0<a<1), and extracts the small part of Ka, then multiplies the hash table size m by this value, then takes the integer part. The algorithm is as follows:

H (k) = Floor (m* (kA mod 1))

Among them, Ka mod1 represents the small number part of KA, floor is the rounding operation.

When the keyword is a string, you cannot use the hash algorithm above. Because strings are made up of characters, you can add the ASCII code of all characters of a string to an integer, and then follow the hash algorithm above to compute it.

The algorithm is as follows:

Function Hash ($key, $m) {

$strlen = strlen ($key);

$hashval = 0;

for ($i =0; $i < $strlen; $i + +) {

$hashval +=ord ($key {$I});

}

return $hashval% $m;

}

3) Classic Hash algorithm Times33

Unsigned int Djbhash (char *str) {

Unsignedint hash = 5381;

while (*STR) {

hash+= (hash <<5) + (*str++);

}

Return (hash &0x7fffffff)

}

The algorithm is constantly multiplied by 33, its efficiency and randomness are very good, widely used in a number of open source projects, such as Apache, Perl and PHP.

3. Hash table

The time complexity of the hash table is O (1), and the hash table structure can be represented by the graph:



To construct a hash table you must create a large enough array to hold the data, and a hash function to map the keyword key to a location in the array.

The implementation of the hash table steps:

1 Create a fixed-size array to store the data.

2) design hash function.

3 through the hash function to map the keyword to a location in the array, and in this location for data access.

4. Use PHP to implement hash table

First, create a Hashtable class with two properties $buckets and $size. $buckets an array to store data $size used to record the size of the $buckets array. The $buckets array is then allocated memory in the constructor. The code is as follows:

? Php

classhashtable{

Private$buckets;

Private$size = 10;

Publicfunction __construct () {

$this-> Buckets =new splfixedarray ($this->size);

}

}

?>

In the constructor, an array with a size of 10 is assigned to the $buckets array. The SPL extended Splfixedarray array is used here, not a generic array (array)

This is because the Splfixedarray array is closer to the C-language array and more efficient. You need to provide an initialization size for the array when it is created.

Note: The SPL extension must be turned on to use the Splfixedarray array. If it is not turned on, you can use a generic array instead.

Then, to specify a hash function for the hash table, the simplest hash algorithm is used here for the sake of simplicity. It is mentioned above to add up all the characters of a string and then take the remainder. The code is as follows:

Public Function Hashfunc ($key) {

$strlen = strlen ($key);

$hashval = 0;

for ($i =0; $i < $strlen; $i + +) {

$hashval +=ord ($key {$I});

}

Return $hashval% $this->size;

}

With the hash function, you can implement the insert and lookup methods. When inserting data, the hash function is used to calculate the location of the hash table where the key is located, and then the data is saved to this location. The code is as follows:

Public Function Insert ($key, $val) {

$index = $this-> hashfunc ($key);

$this-> buckets[$index] = $val;

}

The lookup data method is similar to inserting the data method, first using the hash function to compute the location of the hash table where the keyword is located, and then returning the data at that location. The code is as follows:

Public function Find ($key) {

$index = $this-> hashfunc ($key);

Return$this->buckets[$index];

}

Now that a simple hash table is written, test the hash table below. The code listing is as follows:

? Php

$ht = new HashTable ();

$ht->insert (' Key1 ', ' value1 ');

$ht->insert (' Key2 ', ' value2 ');

Echo$ht->find (' Key1 ');

Echo$ht->find (' Key2 ');

?>

Full code: #hash. php

?
                    PHP Class hashtable{private$buckets;
                    Private $size = 10;
                    Public function__construct () {$this-> buckets =new splfixedarray ($this->size);
                           } publicfunction Hashfunc ($key) {$strlen = strlen ($key);
                           $hashval = 0;
                           For ($i =0 $i < $strlen; $i + +) {$hashval +=ord ($key {$i});
                    } return$hashval% $this->size;
                           Publicfunction Insert ($key, $val) {$index = $this-> hashfunc ($key);
                    $this-> buckets[$index] = $val;
                           Publicfunction Find ($key) {$index = $this-> hashfunc ($key); Return$this->buckets[$index];
             }} $ht = Newhashtable ();
             $ht->insert (' Key1 ', ' value1 ');
             $ht->insert (' Key2 ', ' value2 ');
             Echo $ht->find (' Key1 ');
 
Echo $ht->find (' Key2 '); ?>


refer to this article please note:http://blog.csdn.net/u012675743/article/details/45048369

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.