lua-users home
lua-l archive

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

On 31 May 2011 15:35, Xingzhi Pan <> wrote:

>> The pointer for some Lua values (lightuserdata,userdata,table,function,thread)
>> are used in the hash value when they are used as keys in a table.  Either you
>> will have to change how those values are hashed, or you will have to re-hash
>> all values that you move.
> I don't quite understand "The pointer...are used in the hash value
> when they are used as keys in a table”.  So you're saying for example
> when a function is used as a key in a table, its pointer is used "in
> the hash value"?  What does "in the hash value" mean here?  I suppose
> this function, as a key, can be used to retrieve a value (e.g., a
> string) from the table.  According to you, how is this value connected
> to this function's pointer?

There’s a good explanation of hash tables at . This helped me understand
the ‘magic’ of hash tables. The hash value is a way of converting an
arbitrary value (number, string, pointer), that is a table key, into
an n-bit bucket value, via the chosen hash function. For a table,
function etc to be the key value in a table, Lua uses its pointer
because it will be a unique value. If its pointer changes, its bucket
number (via a hash of the pointer) will change and Lua will not be
able to retrieve its corresponding value. To rehash a table is to
recalculate all its keys into new buckets. This happens when a table
is resized.

Hope this helps,