JavaScript hash Table (HashTable)

Source: Internet
Author: User
Tags map class

Typescript way to realize the source code

//Features://the hash algorithm functions as quickly as possible to find a value in the data structure. In the previous chapters, you already know that if//to get a value in the data structure (using the Get method), you need to traverse the entire data structure to find it. If you use a hash//function, you know where the value is, so you can quickly retrieve that value. The function of the hash function is to give a key value, and then//returns the address of a value in a table//put (key,value): Add a new item to the hash table (can also update the hash table)//Remove (key): Remove a value from the hash table based on the key value//? Get (Key): Returns the specific value retrieved from the key value//? Loselosehashcode (Key): hash function//? put (key):
1 /**2 * Hash List3 * @desc similar to the Set class, ECMAScript 6 also contains the implementation of a map class, which is what we call a dictionary4  */5 classHashTable {6     PrivateTable = [];7      Publicput (key, value) {8Let position =Hashtable.loselosehashcode (key);9Console.log ('position'+'-'+key);Ten          This. table[position] =value; One     } A      PublicRemove (key) { -          This. Table[hashtable.loselosehashcode (key)] =undefined; -     } the      Public Get(key) { -         return  This. Table[hashtable.loselosehashcode (key)]; -     } -     Private StaticLoselosehashcode (key) { +Let hash =0; -          for(Let i =0; i < key.length; i++) { +Hash + =key.charcodeat (i); A         } at         returnHash%Panax Notoginseng; -     } -}
Hash Table HashTable

JavaScript way to realize the source code

1 /**2 * Hash List3 * @desc similar to the Set class, ECMAScript 6 also contains the implementation of a map class, which is what we call a dictionary4  */5 varHashTable =(function () {6 function HashTable () {7          This. Table = [];8     }9HashTable.prototype.put =function (key, value) {Ten         varPosition =Hashtable.loselosehashcode (key); OneConsole.log ('position'+'-'+key); A          This. table[position] =value; -     }; -HashTable.prototype.remove =function (key) { the          This. Table[hashtable.loselosehashcode (key)] =undefined; -     }; -Hashtable.prototype.Get=function (key) { -         return  This. Table[hashtable.loselosehashcode (key)]; +     }; -Hashtable.loselosehashcode =function (key) { +         varhash =0; A          for(varI_1 =0; I_1 < Key.length; i_1++) { atHash + =key.charcodeat (i_1); -         } -         returnHash%Panax Notoginseng; -     }; -     returnHashTable; -}());
Hash Table HashTable

JavaScript hash Table (HashTable)

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.