[Tutor] hash value input
Kent Johnson
kent37 at tds.net
Fri Jan 29 17:54:51 CET 2010
On Fri, Jan 29, 2010 at 8:03 AM, spir <denis.spir at free.fr> wrote:
> I recently discovered that Lua uses the data's address (read: id) as input to the hash func. This allows Lua tables (a kind of more versatile associative array) to use _anything_ as key, since the id is guaranteed not to change, per definition.
> [Aside this consideration, hashing addresses ensures a constant type as input (so allows a specific efficient hash method) and the simplest possible one.]
This sounds like a bad idea to me. You generally want key lookup to be
based on value, not identity. For example, if I say
d = dict()
d['key'] = value
and later
print d['key']
I want this to print 'value' regardless of whether I use the same
instance of the string 'key' in both cases.
Maybe Lua has some mechanism for ensuring that this can't happen?
In general, for a hash table to work as expected, two keys that are
equal in value should have the same hash value. Here is an explanation
of why (for Java, but it pretty much applies to any hash table
implementation):
http://www.javaworld.com/javaqa/2002-06/01-qa-0621-hashtable.html
Kent
More information about the Tutor
mailing list