Software /
code /
prosody
Annotate
util/set.lua @ 1812:e32593074602
mod_httpserver: Configurable filesystem path to serve from
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 21 Sep 2009 17:57:28 +0100 |
parent | 1522:569d58d21612 |
child | 2923:b7049746bd29 |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
1 -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1030
diff
changeset
|
8 |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
9 local ipairs, pairs, setmetatable, next, tostring = |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
10 ipairs, pairs, setmetatable, next, tostring; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
11 local t_concat = table.concat; |
904 | 12 |
13 module "set" | |
14 | |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
15 local set_mt = {}; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
16 function set_mt.__call(set, _, k) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
17 return next(set._items, k); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
18 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
19 function set_mt.__add(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
20 return _M.union(set1, set2); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
21 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
22 function set_mt.__sub(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
23 return _M.difference(set1, set2); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
24 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
25 function set_mt.__div(set, func) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
26 local new_set, new_items = _M.new(); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
27 local items, new_items = set._items, new_set._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
28 for item in pairs(items) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
29 if func(item) then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
30 new_items[item] = true; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
31 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
32 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
33 return new_set; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
34 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
35 function set_mt.__eq(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
36 local set1, set2 = set1._items, set2._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
37 for item in pairs(set1) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
38 if not set2[item] then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
39 return false; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
40 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
41 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
42 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
43 for item in pairs(set2) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
44 if not set1[item] then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
45 return false; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
46 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
47 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
48 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
49 return true; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
50 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
51 function set_mt.__tostring(set) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
52 local s, items = { }, set._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
53 for item in pairs(items) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
54 s[#s+1] = tostring(item); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
55 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
56 return t_concat(s, ", "); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
57 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
58 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
59 local items_mt = {}; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
60 function items_mt.__call(items, _, k) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
61 return next(items, k); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
62 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
63 |
904 | 64 function new(list) |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
65 local items = setmetatable({}, items_mt); |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
66 local set = { _items = items }; |
904 | 67 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
68 function set:add(item) |
904 | 69 items[item] = true; |
70 end | |
71 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
72 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
|
73 return items[item]; |
904 | 74 end |
75 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
76 function set:items() |
904 | 77 return items; |
78 end | |
79 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
80 function set:remove(item) |
904 | 81 items[item] = nil; |
82 end | |
83 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
84 function set:add_list(list) |
904 | 85 for _, item in ipairs(list) do |
86 items[item] = true; | |
87 end | |
88 end | |
89 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
90 function set:include(otherset) |
904 | 91 for item in pairs(otherset) do |
92 items[item] = true; | |
93 end | |
94 end | |
95 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
96 function set:exclude(otherset) |
904 | 97 for item in pairs(otherset) do |
98 items[item] = nil; | |
99 end | |
100 end | |
101 | |
1029
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
102 function set:empty() |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
103 return not next(items); |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
104 end |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
105 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
106 if list then |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
107 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
|
108 end |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
109 |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
110 return setmetatable(set, set_mt); |
904 | 111 end |
112 | |
113 function union(set1, set2) | |
114 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
|
115 local items = set._items; |
904 | 116 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
117 for item in pairs(set1._items) do |
904 | 118 items[item] = true; |
119 end | |
120 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
121 for item in pairs(set2._items) do |
904 | 122 items[item] = true; |
123 end | |
124 | |
125 return set; | |
126 end | |
127 | |
128 function difference(set1, set2) | |
129 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
|
130 local items = set._items; |
904 | 131 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
132 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
|
133 items[item] = (not set2._items[item]) or nil; |
904 | 134 end |
135 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
136 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
|
137 end |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
138 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
143 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
|
144 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
145 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
|
146 items[item] = (not not set2[item]) or nil; |
904 | 147 end |
148 | |
149 return set; | |
150 end | |
151 | |
1030
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
152 function xor(set1, set2) |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
153 return union(set1, set2) - intersection(set1, set2); |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
154 end |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
155 |
904 | 156 return _M; |