3 ms·
This is a really nice writeup! Very clear and easy to understand. I did notice one conceptual error near the beginning: > Computers can pull this trick off to
by Stratoscope 10y ago
This is a really nice writeup! Very clear and easy to understand.
I did notice one conceptual error near the beginning:
> Computers can pull this trick off too: they take a value, and store it at a location in memory. Then, given a key, they somehow use that key to figure out the address of that memory location, go to that location, and return that value.
> Figuring out the address is called hashing, and maps that work like this under the hood are called hash tables.
That definition isn't right. Looking up a key to get a value isn't called "hashing". Hashing and hash tables are just one out of a number of ways that a program could do this.
For example, given the data objects in the first illustration that have Key and Value properties, you could simply use a linear array of these objects. Then you loop through the array looking for the matching Key, and there you have your Value. Hashing is not involved here.
Of course, as the array gets longer, this simple loop gets slower and slower. That's where you start looking for faster algorithms and data structures such as hash tables. But the hash table isn't fundamental to doing a name/value lookup, it's just one way to do it.
Another example is a trie, very different from a hash table but could be used for the same purpose. Tries are often used where you may want to get a list of values that match a prefix (like an autocomplete) but they can also be used for a full match just like a hash table or a linear array.
- AkshatM 10y agoThank you! And all true, but I wanted to be able to motivate why we generate a hash at all for a hashtable. Disambiguating the purpose of hashing outside of its narrow use case here fell outside the scope of my writeup, and would have involved a needless digression - that is why I inserted a hyperlink for anyone to follow if they wanted to learn more. :)