Is there an easy way to create a dictionary-like collection, i.e.
- Tables can be used as keys
- Tables with the same content are considered equivalent (instead of the default pointer comparison)
e.g. after
t = createCustomTable()
k1 = {'a','b','c'}
k2 = {'a','b','c'}
t[k1] = true
t[k2] should evaluate to true.
Also t itself should be usable as a key in the same way.
Is there any way to do this without
- Re-implementing hash tables
- Converting
k1andk2to strings? (this is what I am currently doing.)
Serializing the two tables into strings is the solution Roberto Ierusalimschy (chief architect of Lua) recommends for indexing by content in Programming in Lua 2nd Edition.
If all of your key tables are arrays of strings (with no embedded nulls), this can be done quickly with
table.concat(t,'\0'). (Obviously, your table will need to be sorted if you want index-independent identity.)