lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Luiz Henrique de Figueiredo <lhf <at> tecgraf.puc-rio.br> writes:
> > Languages like Ruby, Python, etc. allow you to define hash functions that
> > apply to your objects.  You define a hash function and a comparison
> > function, and this lets user-defined types be keys in hash tables based on
> > their values.
> 
> In Lua, any value can be used as a table key. But for objects, this means
> that their address is used rather than the contents. Is that the issue?
> If so, can't it be addressed by calling new/put/get functions that compute
> new keys for objects based on whatever is needed by the app? And of course
> use a table as the underlying container.

Using table keys that fully distinguish the object from all other objects is
inefficient, because the key has to contain as much information as the object
itself.  Creating such keys (which are usually strings) feels inelegant, hacky,
and is a poor way of using a hash table, because the string is just going to get
hashed again by Lua.  This is essentially two-pass hashing.

Using table keys that do not fully distinguish the object from all other objects
means that you have to implement your own hash collision algorithm, which is
inefficient and error prone compared to using the algorithms that Lua already
uses internally.

Josh