Software /
code /
prosody
Annotate
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 |
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 | 3 |
4 module "set" | |
5 | |
6 function new(list) | |
7 local items = {}; | |
8 local set = { items = items }; | |
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 | 11 items[item] = true; |
12 end | |
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 | 16 end |
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 | 19 return items; |
20 end | |
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 | 23 items[item] = nil; |
24 end | |
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 | 27 for _, item in ipairs(list) do |
28 items[item] = true; | |
29 end | |
30 end | |
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 | 33 for item in pairs(otherset) do |
34 items[item] = true; | |
35 end | |
36 end | |
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 | 39 for item in pairs(otherset) do |
40 items[item] = nil; | |
41 end | |
42 end | |
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 | 48 return set; |
49 end | |
50 | |
51 function union(set1, set2) | |
52 local set = new(); | |
53 local items = set.items; | |
54 | |
55 for item in pairs(set1.items) do | |
56 items[item] = true; | |
57 end | |
58 | |
59 for item in pairs(set2.items) do | |
60 items[item] = true; | |
61 end | |
62 | |
63 return set; | |
64 end | |
65 | |
66 function difference(set1, set2) | |
67 local set = new(); | |
68 local items = set.items; | |
69 | |
70 for item in pairs(set1.items) do | |
71 items[item] = true; | |
72 end | |
73 | |
74 for item in pairs(set2.items) do | |
75 items[item] = nil; | |
76 end | |
77 | |
78 return set; | |
79 end | |
80 | |
81 return _M; |