lua-users home
lua-l archive

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


> From: Xavier Wang <weasley.wx@gmail.com>
> 2011/8/13 Lars Doelle <lars.doelle@on-line.de>:

> maybe you can write you own hash function in lua:
> 
> function hash(tbl) ... end
> local t = {}
> t[hash { a = 1, b = 2 }] = true
> print(t[hash {a = 1, b = 2}]) --> true

Xavier,

i've seriously considered writing a hash function in LUA,
and make the proposed thingy a module, simply promissing
never to change constant tables.

The problem is, that such a hash function cannot be easily
written in LUA itself, because the hash function for strings
is not available and likely are the object names, i.e. pointers
of tables and other objects.

What one could do is to setup an ephemeron table of all
undecomposable constants used in constant tables, along
the line of:

  __newindex =  function (table,key,_) 
                            rawset(table,key,math.random())
                         end

Then, a hash key would have to be calculated for the table
to be made constant, and this key would have to be looked
up in another hash table, but one implemented in LUA itself,
since the hash key would not be unique in general.

On top of this, as their might be collisions, a structural equivalence
(one level only) is then needed to find the right entry.

This basically means to reimplement much of the LUAs infrastructure
itself, but only with a worse performance. I may do so, perhaps,
before digging into LUA's code itself. As anyone, I've quite some use
for composed keys and LUA simply lacks any support for them keeping
the best of hash tables for itself. :(

Being new on the board, i don't know how such proposals are handled.
I'd volunteer to write a patch to the machinery, but only if it is wanted.

-lars