Software / code / prosody
Annotate
util/set.lua @ 985:2ecd38c73b50
componentmanager: Use core_route_stanza to reply in the default component
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 11 Apr 2009 23:15:04 +0100 |
| parent | 917:f12f88b3d4a1 |
| child | 1028:594a07e753a0 |
| 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 = {}; | |
|
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 | 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(); | |
|
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 | 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 | 56 items[item] = true; |
| 57 end | |
| 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 | 60 items[item] = true; |
| 61 end | |
| 62 | |
| 63 return set; | |
| 64 end | |
| 65 | |
| 66 function difference(set1, set2) | |
| 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 | 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 | 72 end |
| 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 | 85 end |
| 86 | |
| 87 return set; | |
| 88 end | |
| 89 | |
| 90 return _M; |