lua-users home
lua-l archive

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


How a hash calculation would be different and more  efficient than a
string representation? You could store the hash in the table, for
later reuse..

On 11/20/10, Lorenzo Donati <lorenzodonatibz@interfree.it> wrote:
> Hi all!
>
> Recently I needed to use "objects" (tables holding pairs of numbers) as
> indexes in a table, but using value semantics, not object identity.
>
> I searched the wiki and the list archives and I understood (I hope :-) )
> that the usual technique in such cases is to map the object to a unique
> string representation (unique in the sense that every distinct value
> must have a distinct representation and viceversa) and use that
> representation as the index.
>
> However, coming from a Java world, I realized that this could be
> conceptually also be solved introducing a new metamethod (say __hash) to
> "override" default hash calculation.
>
> I wonder why there is no such metamethod. Would it cause problems or
> impact too heavily (code complexity or speed?) on current Lua
> implementation. Or is it just not so useful as I think?
>
> In Java I used to introduce "value classes" almost everywhere (Java
> parlance for classes of objects with value semantics), so I'm curious
> why in Lua, given that there is an __eq metamethod, there is no way to
> override hash calculation (since for value semantics equality and hash
> computation should behave coherently).
>
> Are objects with value semantics in Lua not so heavily used as table
> indexes?
>
> I'm really curious to know how people solve this problem usually.
> Am I missing something, or there are other techniques to achieve the
> same result? Although I find the string representation technique valid
> and simple, there are cases IMHO where it would introduce relevant
> overhead, especially when the internal "object" representation cannot be
> the same as the "string key" representation, so they cannot be "merged"
>
> Thanks!
>
> -- Lorenzo
>
>
>

-- 
Sent from my mobile device