Simulating common data structures using JavaScript (iii)

Source: Internet
Author: User
Tags map class

Vi. dictionaries and hash lists

We already know that a collection represents a set of distinct elements (not repeating elements). In the dictionary, a key-value pair is stored, where the key value is used to query for a particular element. Dictionaries and collections are very similar, and collections are stored in the form of [value, value], while dictionaries are stored in the form of [key, value]. Dictionaries are also known as mappings.

First, we use a function to create a dictionary, and a new map class in Es6, which represents the data structure of the dictionary.

  

function Dictionary () {     var items = {};      }

Well, then, in fact, there are some ways to manipulate this dictionary, including Set,remove,has,get,clear and so on.

 This function (key) {     return in items;}
 This function (key,value) {    = value      }
 This function (key) {    if(this. have (key)) {        delete  items[key];         return true     }    returnnull    }
 This function () {    returnthis. Has[key]?  item[key]:undefined;}

Well, basically the above methods, in fact, is simply an object ....

The following is mainly a hash table, called Hashtable or HashMap, the hash table can significantly improve the dictionary search speed, in short, it does not need to go through the key. For not a key name, it uses an algorithm to encode, the encoded value records the value of the address, well, like this picture below.

  

Well, the above picture is based on the Ascⅱ code of each letter of the key name, and the actual situation is more complicated than this.

Then we can do the corresponding key names and hash values according to the loselose algorithm:

  

var function (key) {        var hash = 0;           for (var i = 0; i < key.length; i++) {         + = key.charcodeat (i)         ;        } return hash% Notoginseng;};

Well, that's what it looks like.

  

Well, the length of the required array is much smaller than the above, and if we need to find a key, it is easy to find its corresponding value directly through the algorithm above, instead of traversing the entire dictionary.

In addition, in the hash table you can easily think of, if the key name generated the same hash value, such as Tyrion and Aaron will generate the same hash value 16, so that the input key value will overwrite the previous key value, in order to solve this problem, we can use the separated list to solve. Such as:

  

  

In this way, we store a list of every value in a hash table, and that is not the case, which is called the separation chain list.

Simulating common data structures using JavaScript (iii)

Related Article

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.