简体   繁体   中英

What does storage[index][i][0] mean in this function?

This is the code. I'm trying to understand what storage[index][i][0] means in the 'this.add' function. I know storage[index] is referring to the array where the key/value pairs are stored after running through the hash function, but what about [i] and [0]? what do they mean in this context?

If it was just storage[index][i] I would have thought it's referring to the iterator within storage[index] but the 0 (and the 1 in the next line) is throwing me off. Please explain as simply as possible as I am quite new to data structures and coding

 var hash = (string, max) => { var hash = 0; for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); } return hash % max; }; let HashTable = function() { let storage = []; const storageLimit = 14; this.print = function() { console.log(storage) } this.add = function(key, value) { var index = hash(key, storageLimit); if (storage[index] === undefined) { storage[index] = [ [key, value] ]; } else { var inserted = false; for (var i = 0; i < storage[index].length; i++) { if (storage[index][i][0] === key) { storage[index][i][1] = value; inserted = true; } } if (inserted === false) { storage[index].push([key, value]); } } }; this.remove = function(key) { var index = hash(key, storageLimit); if (storage[index].length === 1 && storage[index][0][0] === key) { delete storage[index]; } else { for (var i = 0; i < storage[index].length; i++) { if (storage[index][i][0] === key) { delete storage[index][i]; } } } }; this.lookup = function(key) { var index = hash(key, storageLimit); if (storage[index] === undefined) { return undefined; } else { for (var i = 0; i < storage[index].length; i++) { if (storage[index][i][0] === key) { return storage[index][i][1]; } } } }; }; console.log(hash('quincy', 10)) let ht = new HashTable(); ht.add('beau', 'person'); ht.add('fido', 'dog'); ht.add('rex', 'dinosour'); ht.add('tux', 'penguin') console.log(ht.lookup('tux')) ht.print();

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. If you run you code snippet you will see that ht.print() will print an array (in your example Array(14)) which in turn is storing an array (because this is a hashTable).

So storage[index] will give you an array (name it as array1, and in your example it is something like [Array(2)]) at that index. Now this array (ie array1) is also a hashtable that is storing array as per their indexes.

So now when you do storage[index][i] you will get an array containing you values which you inserted in a hashtable ie as per you example you will get ["tux", "penguin"]. So to print or get the actual value you again have to add the specific index (ie you need to do storage[index][i][0] or storage[index][i][1]) to get "tux" or "penguin".

Storage is defined as an array whose key are the has values computed using the key supplied

When adding they are first checking if there is any value stored for that hash computed, if not they are adding it as follows

 storage[index] = [
    [key, value]
 ];

which can be read as

  1. create an array say obj with two elements, 0th index will have key, 1st index will have value
  2. now store that obj created in step one as 0th element of a new array
  3. which results in ins storage[index][[key,value]

Now, if the index is found in the storage which means you have to add the new key say key2 and value2 as follows

storage[index] = [
   [key, value],
   [key2, value2]
];

which could be read as storage[index][1][0] if i is 1 , essentially pushing a new item to the array of the colliding index of the storage.

Essentially this is a collision avoidance mechanism while adding elements to storage using hash as key when two or more keys end up having the same hash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM