Comparison By Value

lua-users home
wiki

Showing revision 3
The function hashed below adds a metatable to the given table t, so that any write or read in the table will actually use a hash of the key instead of the key itself. This allows simulating the comparison of keys by values. To achieve that, make sure that two objects (table or userdata), with the same value will have the same hash.

local function gethash(k)
	local hash = k
	local mt = getmetatable(k)
	local __hash = mt and mt.__hash
	if type(__hash)=='function' then
		hash = __hash(k)
	elseif type(__hash)~='nil' then
		hash = __hash
	end
	return hash
end

function hashed(t)
	return setmetatable(t, {
		__index = function(t, k)
			return t[gethash(k)]
		end,
		__newindex = function(t, k, v)
			rawset(t, gethash(k), v)
		end,
	})
end

local t1 = setmetatable({}, {__hash = 42})
local t2 = setmetatable({}, {__hash = 42})

local t = {}

t[t1] = "foo"

assert(t[t2]==nil)

t = hashed({})

t[t1] = "foo"

assert(t[t2]=="foo")
t[t2] = "bar"
assert(t[t1]=="bar")

RecentChanges · preferences
edit · history · current revision
Edited September 2, 2008 6:15 pm GMT (diff)