Software /
code /
prosody
Annotate
util/set.lua @ 12914:2b4661bd39e2
mod_tokenauth: Add some sanity checking of the new optional parameters
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 01 Mar 2023 13:02:51 +0000 |
parent | 11788:1ceee8becb1a |
child | 12986:895a82c5d8d4 |
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 |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
4 -- |
1522
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; |
10594
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
11 local getmetatable = getmetatable; |
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
|
12 local t_concat = table.concat; |
904 | 13 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
14 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8522
diff
changeset
|
15 -- luacheck: std none |
904 | 16 |
8522
073e517a1487
util.set: Add a __name field to metatable
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
17 local set_mt = { __name = "set" }; |
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
|
18 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
|
19 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
|
20 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
|
21 |
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 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
|
23 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
|
24 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
|
25 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
|
26 |
9488
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
27 function set_mt:__freeze() |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
28 local a, i = {}, 1; |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
29 for item in self._items do |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
30 a[i], i = item, i+1; |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
31 end |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
32 return a; |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
33 end |
a96a2fbcc6c0
util.set: Add freeze metamethod
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
34 |
11542
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
35 local function is_set(o) |
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
36 local mt = getmetatable(o); |
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
37 return mt == set_mt; |
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
38 end |
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
39 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
40 local 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
|
41 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
|
42 local set = { _items = items }; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
43 |
6674
a0ce52e11122
util.set: Add luacheck annotation for methods not referencing 'self'
Matthew Wild <mwild1@gmail.com>
parents:
6673
diff
changeset
|
44 -- We access the set through an upvalue in these methods, so ignore 'self' being unused |
a0ce52e11122
util.set: Add luacheck annotation for methods not referencing 'self'
Matthew Wild <mwild1@gmail.com>
parents:
6673
diff
changeset
|
45 --luacheck: ignore 212/self |
a0ce52e11122
util.set: Add luacheck annotation for methods not referencing 'self'
Matthew Wild <mwild1@gmail.com>
parents:
6673
diff
changeset
|
46 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
47 function set:add(item) |
904 | 48 items[item] = true; |
49 end | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
50 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
51 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
|
52 return items[item]; |
904 | 53 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
54 |
11788
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
55 function set:contains_set(other_set) |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
56 for item in other_set do |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
57 if not self:contains(item) then |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
58 return false; |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
59 end |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
60 end |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
61 return true; |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
62 end |
1ceee8becb1a
util.set: Add :contains_set() method
Matthew Wild <mwild1@gmail.com>
parents:
11560
diff
changeset
|
63 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
64 function set:items() |
5817
6e087f3b8f3b
util.set: :items() now returns an iterator instead of the underlying table. This is much more efficient than 'for item in set' (which still works for now). Current access to _items is generally done directly, this may change.
Matthew Wild <mwild1@gmail.com>
parents:
5814
diff
changeset
|
65 return next, items; |
904 | 66 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
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:remove(item) |
904 | 69 items[item] = nil; |
70 end | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
71 |
6675
cb5b56ddabfd
util.set: Rename method argument to avoid name clash [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
6674
diff
changeset
|
72 function set:add_list(item_list) |
cb5b56ddabfd
util.set: Rename method argument to avoid name clash [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
6674
diff
changeset
|
73 if item_list then |
cb5b56ddabfd
util.set: Rename method argument to avoid name clash [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
6674
diff
changeset
|
74 for _, item in ipairs(item_list) do |
4544
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
75 items[item] = true; |
316e2b09a562
util.set: Accept nil to add_list()
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
76 end |
904 | 77 end |
78 end | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
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:include(otherset) |
5813
c888f548876b
util.set: Fix :include() and :exclude() methods to iterate the input set correctly
Matthew Wild <mwild1@gmail.com>
parents:
4908
diff
changeset
|
81 for item in otherset do |
904 | 82 items[item] = true; |
83 end | |
84 end | |
85 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
86 function set:exclude(otherset) |
5813
c888f548876b
util.set: Fix :include() and :exclude() methods to iterate the input set correctly
Matthew Wild <mwild1@gmail.com>
parents:
4908
diff
changeset
|
87 for item in otherset do |
904 | 88 items[item] = nil; |
89 end | |
90 end | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
91 |
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
|
92 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
|
93 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
|
94 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
95 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
96 if list then |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
97 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
|
98 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
99 |
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
|
100 return setmetatable(set, set_mt); |
904 | 101 end |
102 | |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
103 local function union(set1, set2) |
904 | 104 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
|
105 local items = set._items; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
106 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
107 for item in pairs(set1._items) do |
904 | 108 items[item] = true; |
109 end | |
110 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
111 for item in pairs(set2._items) do |
904 | 112 items[item] = true; |
113 end | |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
114 |
904 | 115 return set; |
116 end | |
117 | |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
118 local function difference(set1, set2) |
904 | 119 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
|
120 local items = set._items; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
121 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
122 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
|
123 items[item] = (not set2._items[item]) or nil; |
904 | 124 end |
125 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
126 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
|
127 end |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
128 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
129 local function intersection(set1, set2) |
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 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
|
131 local items = set._items; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
132 |
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 set1, set2 = set1._items, set2._items; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
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) 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 not set2[item]) or nil; |
904 | 137 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4908
diff
changeset
|
138 |
904 | 139 return set; |
140 end | |
141 | |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
142 local function xor(set1, set2) |
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
|
143 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
|
144 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
|
145 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
146 function set_mt.__add(set1, set2) |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
147 return union(set1, set2); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
148 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
149 function set_mt.__sub(set1, set2) |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
150 return difference(set1, set2); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
151 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
152 function set_mt.__div(set, func) |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
153 local new_set = new(); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
154 local items, new_items = set._items, new_set._items; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
155 for item in pairs(items) do |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
156 local new_item = func(item); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
157 if new_item ~= nil then |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
158 new_items[new_item] = true; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
159 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
160 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
161 return new_set; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
162 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
163 function set_mt.__eq(set1, set2) |
10594
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
164 if getmetatable(set1) ~= set_mt or getmetatable(set2) ~= set_mt then |
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
165 -- Lua 5.3+ calls this if both operands are tables, even if metatables differ |
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
166 return false; |
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
167 end |
13d5fb74648f
util.set: Fix equality metamethod in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
9488
diff
changeset
|
168 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
169 set1, set2 = set1._items, set2._items; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
170 for item in pairs(set1) do |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
171 if not set2[item] then |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
172 return false; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
173 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
174 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
175 |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
176 for item in pairs(set2) do |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
177 if not set1[item] then |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
178 return false; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
179 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
180 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
181 |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
182 return true; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
183 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
184 function set_mt.__tostring(set) |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
185 local s, items = { }, set._items; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
186 for item in pairs(items) do |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
187 s[#s+1] = tostring(item); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
188 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
189 return t_concat(s, ", "); |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
190 end |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
191 |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
192 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
193 new = new; |
11542
c358537c0878
util.set: Add is_set() to test if an object is a set
Matthew Wild <mwild1@gmail.com>
parents:
9488
diff
changeset
|
194 is_set = is_set; |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
195 union = union; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
196 difference = difference; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
197 intersection = intersection; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
198 xor = xor; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6675
diff
changeset
|
199 }; |