Annotate

util/set.lua @ 904:0205dcd0854a

util.set: New util library
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Mar 2009 12:13:39 +0000
child 905:6169597d5574
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 module "set"
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 function new(list)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local items = {};
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local set = { items = items };
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 function set:add(set, item)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function set:contains(set, item)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 return items[item]
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function set:items(set)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function set:remove(set, item)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 function set:add_list(set, list)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for _, item in ipairs(list) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 function set:include(set, otherset)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for item in pairs(otherset) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 function set:exclude(set, otherset)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 for item in pairs(otherset) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 function union(set1, set2)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local set = new();
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local items = set.items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 for item in pairs(set1.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 for item in pairs(set2.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 function difference(set1, set2)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local set = new();
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local items = set.items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 for item in pairs(set1.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 for item in pairs(set2.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 return _M;