PHP core technology and best practice hash Algorithm _php Tutorial

Source: Internet
Author: User
Tags spl

The hash algorithm of PHP core technology and best practice


The hash algorithm of PHP core technology and best practice

The hash table is also called the 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 convert any length input into a fixed-length output through a hash algorithm, which is the hash value. This conversion is a compression mapping, that is, the hash is usually much smaller than the input space, not input may be hashed into the same output, but not from the hash value to uniquely determine the input value.

A good hash function should satisfy the following criteria: Each keyword can be evenly distributed to any position in the hash table, and there is no conflict with other keywords that have been hashed into the hash table. This is the most difficult to implement the hash function.

2. Hash algorithm

1) Direct method of taking the remainder

Direct take-off method is relatively simple, directly using the key word K divided by the size of the hash table m to take the remainder, the algorithm is as follows:

H (k) = k mod m

For example: the size of the hash table is m=12, the given keyword is k=100, then h (k) = 4. This algorithm is a redundancy 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

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

Among them, Ka Mod1 represents the small part of Ka, floor is the rounding operation.

When the keyword is a string, you cannot use the hash algorithm above. Because the string is composed of characters, you can add the ASCII code of all the characters of the string to get an integer, and then follow the above hash algorithm to calculate.

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 many 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 graphs:


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 steps of the hash table:

1) Create a fixed-size array to hold the data.

2) Design the hash function.

3) The keyword is mapped to a location in the array by a hash function, and the data is accessed at this location.

4. Using PHP to implement a hash table

First, create a Hashtable class with two properties $buckets and $size. $buckets an array for storing 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:

classhashtable{

Private$buckets;

Private$size = 10;

Publicfunction __construct () {

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

}

}

?>

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

This is because the Splfixedarray array is closer to the C language array and is more efficient. You need to provide an initialization size for an 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 specify a hash function for the hash table, and for the sake of simplicity, the simplest hash algorithm is used. It is mentioned above to add up all the characters of the 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 find methods. When inserting data, the hash function is the first to calculate the location of the hash table of the keyword, and then save the data 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 the Insert data method, the hash function is used to calculate the location of the hash table of the keyword, and then return the data at this location. The code is as follows:

Public function Find ($key) {

$index = $this-Hashfunc ($key);

Return$this->buckets[$index];

}

At this point, a simple hash table is written to complete, the following test the hash table. The code listing is as follows:

$ht = new HashTable ();

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

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

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

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

?>

Complete code: #hash. php

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


http://www.bkjia.com/PHPjc/984757.html www.bkjia.com true http://www.bkjia.com/PHPjc/984757.html techarticle PHP Core technology and best practice hash algorithm PHP core technology and best practice hash algorithm hash table is also known as a hash list, by mapping the keyword key to a location in the array to access the record ...

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