Skip to content

Instantly share code, notes, and snippets.

@Adrodoc
Created March 26, 2018 13:56
Show Gist options
  • Save Adrodoc/04240602b32930b6d45bee62688fcde6 to your computer and use it in GitHub Desktop.
Save Adrodoc/04240602b32930b6d45bee62688fcde6 to your computer and use it in GitHub Desktop.
setmultimap.lua
-- adrodoc55/setmultimap.lua
local pkg = {}
function pkg.put(multimap, key, value, hashfunction)
hashfunction = hashfunction or str
local set = multimap[key] or {}
local hash = hashfunction(value)
set[hash] = value
multimap[key] = set
end
function pkg.remove(multimap, key, value)
hashfunction = hashfunction or str
local set = multimap[key]
if set ~= nil then
local hash = hashfunction(value)
set[hash] = nil
if next(set) == nil then
multimap[key] = nil
end
end
end
return pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment