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)