Comparison

util/set.lua @ 905:6169597d5574

util.set: Fix to make constructor work, and functions defined correctly
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Mar 2009 12:37:56 +0000
parent 904:0205dcd0854a
child 917:f12f88b3d4a1
comparison
equal deleted inserted replaced
904:0205dcd0854a 905:6169597d5574
1 local ipairs, pairs =
2 ipairs, pairs;
1 3
2 module "set" 4 module "set"
3 5
4 function new(list) 6 function new(list)
5 local items = {}; 7 local items = {};
6 local set = { items = items }; 8 local set = { items = items };
7 9
8 function set:add(set, item) 10 function set:add(item)
9 items[item] = true; 11 items[item] = true;
10 end 12 end
11 13
12 function set:contains(set, item) 14 function set:contains(item)
13 return items[item] 15 return items[item];
14 end 16 end
15 17
16 function set:items(set) 18 function set:items()
17 return items; 19 return items;
18 end 20 end
19 21
20 function set:remove(set, item) 22 function set:remove(item)
21 items[item] = nil; 23 items[item] = nil;
22 end 24 end
23 25
24 function set:add_list(set, list) 26 function set:add_list(list)
25 for _, item in ipairs(list) do 27 for _, item in ipairs(list) do
26 items[item] = true; 28 items[item] = true;
27 end 29 end
28 end 30 end
29 31
30 function set:include(set, otherset) 32 function set:include(otherset)
31 for item in pairs(otherset) do 33 for item in pairs(otherset) do
32 items[item] = true; 34 items[item] = true;
33 end 35 end
34 end 36 end
35 37
36 function set:exclude(set, otherset) 38 function set:exclude(otherset)
37 for item in pairs(otherset) do 39 for item in pairs(otherset) do
38 items[item] = nil; 40 items[item] = nil;
39 end 41 end
42 end
43
44 if list then
45 set:add_list(list);
40 end 46 end
41 47
42 return set; 48 return set;
43 end 49 end
44 50