Software /
code /
prosody
Comparison
util/set.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 4908:8c5b5ebaacb0 |
child | 5814:5cf1c08805fb |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 local ipairs, pairs, setmetatable, next, tostring = | 9 local ipairs, pairs, setmetatable, next, tostring = |
38 for item in pairs(set1) do | 38 for item in pairs(set1) do |
39 if not set2[item] then | 39 if not set2[item] then |
40 return false; | 40 return false; |
41 end | 41 end |
42 end | 42 end |
43 | 43 |
44 for item in pairs(set2) do | 44 for item in pairs(set2) do |
45 if not set1[item] then | 45 if not set1[item] then |
46 return false; | 46 return false; |
47 end | 47 end |
48 end | 48 end |
49 | 49 |
50 return true; | 50 return true; |
51 end | 51 end |
52 function set_mt.__tostring(set) | 52 function set_mt.__tostring(set) |
53 local s, items = { }, set._items; | 53 local s, items = { }, set._items; |
54 for item in pairs(items) do | 54 for item in pairs(items) do |
63 end | 63 end |
64 | 64 |
65 function new(list) | 65 function new(list) |
66 local items = setmetatable({}, items_mt); | 66 local items = setmetatable({}, items_mt); |
67 local set = { _items = items }; | 67 local set = { _items = items }; |
68 | 68 |
69 function set:add(item) | 69 function set:add(item) |
70 items[item] = true; | 70 items[item] = true; |
71 end | 71 end |
72 | 72 |
73 function set:contains(item) | 73 function set:contains(item) |
74 return items[item]; | 74 return items[item]; |
75 end | 75 end |
76 | 76 |
77 function set:items() | 77 function set:items() |
78 return items; | 78 return items; |
79 end | 79 end |
80 | 80 |
81 function set:remove(item) | 81 function set:remove(item) |
82 items[item] = nil; | 82 items[item] = nil; |
83 end | 83 end |
84 | 84 |
85 function set:add_list(list) | 85 function set:add_list(list) |
86 if list then | 86 if list then |
87 for _, item in ipairs(list) do | 87 for _, item in ipairs(list) do |
88 items[item] = true; | 88 items[item] = true; |
89 end | 89 end |
90 end | 90 end |
91 end | 91 end |
92 | 92 |
93 function set:include(otherset) | 93 function set:include(otherset) |
94 for item in pairs(otherset) do | 94 for item in pairs(otherset) do |
95 items[item] = true; | 95 items[item] = true; |
96 end | 96 end |
97 end | 97 end |
99 function set:exclude(otherset) | 99 function set:exclude(otherset) |
100 for item in pairs(otherset) do | 100 for item in pairs(otherset) do |
101 items[item] = nil; | 101 items[item] = nil; |
102 end | 102 end |
103 end | 103 end |
104 | 104 |
105 function set:empty() | 105 function set:empty() |
106 return not next(items); | 106 return not next(items); |
107 end | 107 end |
108 | 108 |
109 if list then | 109 if list then |
110 set:add_list(list); | 110 set:add_list(list); |
111 end | 111 end |
112 | 112 |
113 return setmetatable(set, set_mt); | 113 return setmetatable(set, set_mt); |
114 end | 114 end |
115 | 115 |
116 function union(set1, set2) | 116 function union(set1, set2) |
117 local set = new(); | 117 local set = new(); |
118 local items = set._items; | 118 local items = set._items; |
119 | 119 |
120 for item in pairs(set1._items) do | 120 for item in pairs(set1._items) do |
121 items[item] = true; | 121 items[item] = true; |
122 end | 122 end |
123 | 123 |
124 for item in pairs(set2._items) do | 124 for item in pairs(set2._items) do |
125 items[item] = true; | 125 items[item] = true; |
126 end | 126 end |
127 | 127 |
128 return set; | 128 return set; |
129 end | 129 end |
130 | 130 |
131 function difference(set1, set2) | 131 function difference(set1, set2) |
132 local set = new(); | 132 local set = new(); |
133 local items = set._items; | 133 local items = set._items; |
134 | 134 |
135 for item in pairs(set1._items) do | 135 for item in pairs(set1._items) do |
136 items[item] = (not set2._items[item]) or nil; | 136 items[item] = (not set2._items[item]) or nil; |
137 end | 137 end |
138 | 138 |
139 return set; | 139 return set; |
140 end | 140 end |
141 | 141 |
142 function intersection(set1, set2) | 142 function intersection(set1, set2) |
143 local set = new(); | 143 local set = new(); |
144 local items = set._items; | 144 local items = set._items; |
145 | 145 |
146 set1, set2 = set1._items, set2._items; | 146 set1, set2 = set1._items, set2._items; |
147 | 147 |
148 for item in pairs(set1) do | 148 for item in pairs(set1) do |
149 items[item] = (not not set2[item]) or nil; | 149 items[item] = (not not set2[item]) or nil; |
150 end | 150 end |
151 | 151 |
152 return set; | 152 return set; |
153 end | 153 end |
154 | 154 |
155 function xor(set1, set2) | 155 function xor(set1, set2) |
156 return union(set1, set2) - intersection(set1, set2); | 156 return union(set1, set2) - intersection(set1, set2); |