[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: equals metamethod and hash functions
- From: Dave Dodge <dododge@...>
- Date: Wed, 8 Jun 2005 19:05:41 -0400
On Tue, Jun 07, 2005 at 06:27:35PM -0400, Paul Chiusano wrote:
> In order for this to work, I need equal objects (according to __eq)
> to hash to the same location in the table.
If these objects are strictly tables, tostring() might be sufficient
for this already. tostring(sometable) seems to produce a string of
the form "table: 0x0123456", where I assume the number has to do with
the address where the table is stored. This string might therefore be
unique for each table, and always the same for the same table. I
don't know if Lua makes any such guarantees about those strings, but
it's a start.
Another option if the objects are tables might be to add an extra
field to the tables, with its value being a unique string or number
that you would use as the key when storing the table in the set. Then
the problem changes to one of creating unique strings or numbers; but
since they don't have to be derived from the table contents, they
could just be a simple one-up counter.