22-- A SetList is a data structure used for storing only keys, any key is valid (no values!)
33-- It consists of a hash part for fast containment checks
44-- And a list part for fast random generation
5- local function tstr (t ,n )
6- if t == nil then return end
7- local res = tostring (n or " " ) .. ' {'
8- for k ,v in pairs (t ) do res = res .. tostring (k ) .. " =" .. tostring (v ) .. " , " end
9- print ( res .. ' }' )
5+ local function tstr (t , n )
6+ if t == nil then
7+ return
8+ end
9+ local res = tostring (n or " " ) .. " {"
10+ for k , v in pairs (t ) do
11+ res = res .. tostring (k ) .. " =" .. tostring (v ) .. " , "
12+ end
13+ print (res .. " }" )
1014end
1115
1216local mRandom = math.random
@@ -15,39 +19,39 @@ SetListMT.__index = SetListMT
1519function SetListMT :Add (key )
1620 if self .hash [key ] == nil then -- So that we dont add twice
1721 self .count = self .count + 1
18- self .hash [key ] = self .count
22+ self .hash [key ] = self .count
1923 self .list [self .count ] = key
2024 end
2125end
2226function SetListMT :Remove (key )
2327 local popindex = self .hash [key ]
2428 if popindex then
25- if popindex ~= self .count then
29+ if popindex ~= self .count then
2630 -- If not last element, then take the element at the very back, and emplace it at popindex
2731 local popkey = self .list [self .count ]
28- self .list [popindex ] = popkey -- bring it back in list
32+ self .list [popindex ] = popkey -- bring it back in list
2933 self .hash [popkey ] = popindex
3034 end
3135 self .list [self .count ] = nil
32- self .hash [key ] = nil
36+ self .hash [key ] = nil
3337 self .count = self .count - 1
3438 end
35-
3639end
3740function SetListMT :GetRandom ()
38- if self .count > 0 then return self .list [mRandom (1 , self .count )] end
41+ if self .count > 0 then
42+ return self .list [mRandom (1 , self .count )]
43+ end
3944end
4045local function NewSetList ()
4146 local t = {
42- hash = {}, -- Hash table map keys to positions in list
47+ hash = {}, -- Hash table map keys to positions in list
4348 list = {}, -- List table maps positions in list to keys
4449 count = 0 , -- Keeps a tally of how many elements
45- }
50+ }
4651 setmetatable (t , SetListMT )
4752 return t
4853end
4954
50-
5155-- A SetListMin is a data structure used for storing only positive integer keys or strings (no values!)
5256-- It consists of only a hash part. Negative numbers are not allowed
5357local setListMinMT = {}
6266function setListMinMT :Remove (key )
6367 local popindex = self .hash [key ]
6468 if popindex then
65- if popindex ~= self .count then
69+ if popindex ~= self .count then
6670 -- If not last element, then take the element at the very back, and emplace it at popindex
6771 local popkey = self .hash [- 1 * self .count ]
68- self .hash [- 1 * popindex ] = popkey -- bring it back in list
72+ self .hash [- 1 * popindex ] = popkey -- bring it back in list
6973 self .hash [popkey ] = popindex
7074 end
71- self .hash [- 1 * self .count ] = nil
72- self .hash [key ] = nil
75+ self .hash [- 1 * self .count ] = nil
76+ self .hash [key ] = nil
7377 self .count = self .count - 1
7478 end
7579end
7680function setListMinMT :GetRandom ()
77- if self .count > 0 then return self .hash [- 1 * mRandom (1 , self .count )] end
81+ if self .count > 0 then
82+ return self .hash [- 1 * mRandom (1 , self .count )]
83+ end
7884end
7985local function NewSetListMin ()
8086 local t = {
81- hash = {}, -- Hash table map keys to positions in list
87+ hash = {}, -- Hash table map keys to positions in list
8288 count = 0 , -- Keeps a tally of how many elements
83- }
89+ }
8490 setmetatable (t , setListMinMT )
8591 return t
8692end
8793
88-
8994-- A SetListNoTable is a data structure used for storing only positive integer keys, or string keys which arent called 'count'
9095-- It consists of only the table itself. Negative numbers are not allowed
9196
@@ -101,30 +106,32 @@ end
101106function SetListNoTableMT :Remove (key )
102107 local popindex = self [key ]
103108 if popindex then
104- if popindex ~= self .count then
109+ if popindex ~= self .count then
105110 -- If not last element, then take the element at the very back, and emplace it at popindex
106111 local popkey = self [- 1 * self .count ]
107- self [- 1 * popindex ] = popkey -- bring it back in list
112+ self [- 1 * popindex ] = popkey -- bring it back in list
108113 self [popkey ] = popindex
109114 end
110115 self [- 1 * self .count ] = nil
111- self [key ] = nil
116+ self [key ] = nil
112117 self .count = self .count - 1
113118 end
114119end
115120function SetListNoTableMT :GetRandom ()
116- if self .count > 0 then return self [- 1 * mRandom (1 , self .count )] end
121+ if self .count > 0 then
122+ return self [- 1 * mRandom (1 , self .count )]
123+ end
117124end
118125local function NewSetListNoTable ()
119- local t = {count = 0 } -- Keeps a tally of how many elements
126+ local t = { count = 0 } -- Keeps a tally of how many elements
120127 setmetatable (t , SetListNoTableMT )
121128 return t
122129end
123130
124131local SetListUtilities = {
125132 NewSetListNoTable = NewSetListNoTable ,
126- NewSetListMin = NewSetListMin ,
127- NewSetList = NewSetList
133+ NewSetListMin = NewSetListMin ,
134+ NewSetList = NewSetList ,
128135}
129136return SetListUtilities
130137
205212local deltat = os.clock()-t0
206213print ("Time taken to test", "good old vanilla", deltat,"s")
207214
208- ]] --
215+ ]]
216+ --
0 commit comments