util/set.lua

Sun, 22 Mar 2009 12:37:56 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 22 Mar 2009 12:37:56 +0000
changeset 905
6169597d5574
parent 904
0205dcd0854a
child 917
f12f88b3d4a1
permissions
-rw-r--r--

util.set: Fix to make constructor work, and functions defined correctly

905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
1 local ipairs, pairs =
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
2 ipairs, pairs;
904
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 module "set"
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function new(list)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local items = {};
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local set = { items = items };
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
10 function set:add(item)
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
14 function set:contains(item)
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
15 return items[item];
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
18 function set:items()
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
22 function set:remove(item)
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
26 function set:add_list(list)
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 for _, item in ipairs(list) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
32 function set:include(otherset)
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 for item in pairs(otherset) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
38 function set:exclude(otherset)
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 for item in pairs(otherset) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
905
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
44 if list then
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
45 set:add_list(list);
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
46 end
6169597d5574 util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents: 904
diff changeset
47
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function union(set1, set2)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local set = new();
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local items = set.items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 for item in pairs(set1.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 for item in pairs(set2.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function difference(set1, set2)
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local set = new();
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local items = set.items;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 for item in pairs(set1.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 items[item] = true;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 for item in pairs(set2.items) do
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 items[item] = nil;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 return _M;

mercurial