aboutsummaryrefslogtreecommitdiff
path: root/samples/tinyirc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'samples/tinyirc.lua')
-rw-r--r--samples/tinyirc.lua25
1 files changed, 17 insertions, 8 deletions
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua
index 684e7c0..e3dd517 100644
--- a/samples/tinyirc.lua
+++ b/samples/tinyirc.lua
@@ -24,20 +24,29 @@ io.write("Servers bound\n")
24-- simple set implementation 24-- simple set implementation
25-- the select function doesn't care about what is passed to it as long as 25-- the select function doesn't care about what is passed to it as long as
26-- it behaves like a table 26-- it behaves like a table
27-- creates a new set data structure
27function newset() 28function newset()
28 local reverse = {} 29 local reverse = {}
29 local set = {} 30 local set = {}
30 setmetatable(set, { __index = { 31 return setmetatable(set, {__index = {
31 insert = function(set, value) 32 insert = function(set, value)
32 table.insert(set, value) 33 if not reverse[value] then
33 reverse[value] = table.getn(set) 34 table.insert(set, value)
35 reverse[value] = table.getn(set)
36 end
34 end, 37 end,
35 remove = function(set, value) 38 remove = function(set, value)
36 table.remove(set, reverse[value]) 39 local index = reverse[value]
37 reverse[value] = nil 40 if index then
38 end, 41 reverse[value] = nil
42 local top = table.remove(set)
43 if top ~= value then
44 reverse[top] = index
45 set[index] = top
46 end
47 end
48 end
39 }}) 49 }})
40 return set
41end 50end
42 51
43set = newset() 52set = newset()