Software /
code /
prosody
Annotate
util/set.lua @ 4908:8c5b5ebaacb0
util.set: Change '/' operator from filter to map+filter (nil to drop from set, false is a valid item). No code found which depends on current behaviour.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 24 May 2012 18:51:18 +0100 |
parent | 4544:316e2b09a562 |
child | 5776:bd0ff8ae98a8 |
child | 5813:c888f548876b |
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 |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
1522
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 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
9 local ipairs, pairs, setmetatable, next, tostring = |
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
|
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 |
4908
8c5b5ebaacb0
util.set: Change '/' operator from filter to map+filter (nil to drop from set, false is a valid item). No code found which depends on current behaviour.
Matthew Wild <mwild1@gmail.com>
parents:
4544
diff
changeset
|
29 local new_item = func(item); |
8c5b5ebaacb0
util.set: Change '/' operator from filter to map+filter (nil to drop from set, false is a valid item). No code found which depends on current behaviour.
Matthew Wild <mwild1@gmail.com>
parents:
4544
diff
changeset
|
30 if new_item ~= nil then |
8c5b5ebaacb0
util.set: Change '/' operator from filter to map+filter (nil to drop from set, false is a valid item). No code found which depends on current behaviour.
Matthew Wild <mwild1@gmail.com>
parents:
4544
diff
changeset
|
31 new_items[new_item] = true; |
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
|
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 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
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 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
|
43 |
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 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
|
45 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
|
46 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
|
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 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
|
49 |
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 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 |
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 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
|
61 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
|
62 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
|
63 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
|
64 |
904 | 65 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
|
66 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
|
67 local set = { _items = items }; |
904 | 68 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
69 function set:add(item) |
904 | 70 items[item] = true; |
71 end | |
72 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
73 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
|
74 return items[item]; |
904 | 75 end |
76 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
77 function set:items() |
904 | 78 return items; |
79 end | |
80 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
81 function set:remove(item) |
904 | 82 items[item] = nil; |
83 end | |
84 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
85 function set:add_list(list) |
4544
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
86 if list then |
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
87 for _, item in ipairs(list) do |
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
88 items[item] = true; |
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
89 end |
904 | 90 end |
91 end | |
92 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
93 function set:include(otherset) |
904 | 94 for item in pairs(otherset) do |
95 items[item] = true; | |
96 end | |
97 end | |
98 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
99 function set:exclude(otherset) |
904 | 100 for item in pairs(otherset) do |
101 items[item] = nil; | |
102 end | |
103 end | |
104 | |
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
|
105 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
|
106 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
|
107 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
|
108 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
109 if list then |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
110 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
|
111 end |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
112 |
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
|
113 return setmetatable(set, set_mt); |
904 | 114 end |
115 | |
116 function union(set1, set2) | |
117 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
|
118 local items = set._items; |
904 | 119 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
120 for item in pairs(set1._items) do |
904 | 121 items[item] = true; |
122 end | |
123 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
124 for item in pairs(set2._items) do |
904 | 125 items[item] = true; |
126 end | |
127 | |
128 return set; | |
129 end | |
130 | |
131 function difference(set1, set2) | |
132 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
|
133 local items = set._items; |
904 | 134 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
135 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
|
136 items[item] = (not set2._items[item]) or nil; |
904 | 137 end |
138 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
139 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
|
140 end |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
141 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
146 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
|
147 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
148 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
|
149 items[item] = (not not set2[item]) or nil; |
904 | 150 end |
151 | |
152 return set; | |
153 end | |
154 | |
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
|
155 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
|
156 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
|
157 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
|
158 |
904 | 159 return _M; |