Annotate

util/set.lua @ 917:f12f88b3d4a1

util.set: Rename private items container, optimise set.difference() and add set.intersection()
author Matthew Wild <mwild1@gmail.com>
date Mon, 23 Mar 2009 01:49:22 +0000
parent 905:6169597d5574
child 1028:594a07e753a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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 = {};
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
8 local set = { _items = items };
904
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();
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
53 local items = set._items;
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
55 for item in pairs(set1._items) do
904
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
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
59 for item in pairs(set2._items) do
904
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();
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
68 local items = set._items;
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
70 for item in pairs(set1._items) do
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
71 items[item] = (not set2._items[item]) or nil;
904
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
917
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
74 return set;
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
75 end
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
76
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
77 function intersection(set1, set2)
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
78 local set = new();
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
79 local items = set._items;
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
80
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
81 set1, set2 = set1._items, set2._items;
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
82
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
83 for item in pairs(set1) do
f12f88b3d4a1 util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents: 905
diff changeset
84 items[item] = (not not set2[item]) or nil;
904
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 return set;
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
0205dcd0854a util.set: New util library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return _M;