简体   繁体   English

这个 function 中的 storage[index][i][0] 是什么意思?

[英]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.我试图了解 storage[index][i][0] 在“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]?我知道 storage[index] 是指在运行 hash function 之后存储键/值对的数组,但是 [i] 和 [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.如果它只是 storage[index][i] 我会认为它指的是 storage[index] 中的迭代器,但是 0(以及下一行中的 1)让我失望。 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. Hash 表是一种以关联方式存储数据的数据结构。 In a hash table, data is stored in an array format, where each data value has its own unique index value.在 hash 表中,数据以数组格式存储,其中每个数据值都有自己唯一的索引值。 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).如果您运行代码片段,您将看到 ht.print() 将打印一个数组(在您的示例 Array(14) 中),该数组又存储一个数组(因为这是一个哈希表)。

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.因此 storage[index] 会在该索引处为您提供一个数组(将其命名为 array1,在您的示例中,它类似于 [Array(2)])。 Now this array (ie array1) is also a hashtable that is storing array as per their indexes.现在这个数组(即array1)也是一个哈希表,它根据它们的索引存储数组。

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"].因此,现在当您执行 storage[index][i] 时,您将获得一个包含您插入到哈希表中的值的数组,即根据您的示例,您将获得 ["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[index][i][0] 或 storage[index][i][1])以获得“tux”或“企鹅”。

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添加时,他们首先检查是否有为计算的 hash 存储的任何值,如果没有,则按如下方式添加

 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创建一个数组,说obj有两个元素,第 0 个索引将具有键,第 1 个索引将具有值
  2. now store that obj created in step one as 0th element of a new array现在将在第一步中创建的obj存储为新数组的第 0 个元素
  3. which results in ins storage[index][[key,value]这导致 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现在,如果在存储中找到索引,这意味着您必须添加新键,例如key2value2 ,如下所示

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.如果 i 为1 ,则可以将其读取为storage[index][1][0] ,本质上是将新项目推送到存储冲突索引的数组中。

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.本质上,这是一种避免冲突的机制,当两个或多个键最终具有相同的 hash 时,使用 hash 作为键向存储添加元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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