Skip to content

Instantly share code, notes, and snippets.

@andry1
Created May 5, 2011 16:19
Show Gist options
  • Save andry1/957349 to your computer and use it in GitHub Desktop.
Save andry1/957349 to your computer and use it in GitHub Desktop.
#!/usr/bin/env lua
Set = {}
Set.mt = {}
function Set.new (t)
local set = {}
setmetatable(set, Set.mt)
for _, l in ipairs(t) do set[l] = true end
return set
end
function Set.union (a,b)
local res = Set.new{}
for k in pairs(a) do res[k] = true end
for k in pairs(b) do res[k] = true end
return res
end
function Set.intersection (a,b)
local res = Set.new{}
for k in pairs(a) do
res[k] = b[k]
end
return res
end
function Set.tostring (set)
local s = "{"
local sep = ""
for e in pairs(set) do
s = s .. sep .. e
sep = ", "
end
return s .. "}"
end
function Set.print (s)
print(Set.tostring(s))
end
Set.mt.__mul = Set.intersection
s1 = Set.new{}
s2 = Set.new{}
math.randomseed(os.clock())
generate_start = os.clock()
for i = 0,9999999 do
-- local key = string.format("%08X", math.random(1,1000000))
-- local key = math.random(1,9999999)
key = i
s1[key] = true
-- key = string.format("%08X", math.random(1,1000000))
-- key = math.random(1,9999999)
s2[key] = true
end
generate_end = os.clock()
print(string.format("Set generation took %d seconds", (generate_end-generate_start)))
intersect_start = os.clock()
intersection = (s1 * s2)
intersect_end = os.clock()
print(string.format("Intersection completed in %d seconds",
(intersect_end - intersect_start)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment