| 1 |
% luaset package |
| 2 |
% version 1.1 |
| 3 |
% Authors: Chetan Shirore and Ajit Kumar |
| 4 |
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt. |
| 5 |
|
| 6 |
|
| 7 |
\ProvidesPackage{luaset}[1.1] |
| 8 |
\RequirePackage{xkeyval} |
| 9 |
\RequirePackage{amsmath} |
| 10 |
\RequirePackage{luacode} |
| 11 |
\RequirePackage{luamaths} |
| 12 |
\begin{luacode*} |
| 13 |
sets = {} -- global registry for sets. |
| 14 |
Set = {} -- the module. |
| 15 |
local mt = { |
| 16 |
__tostring = function(self) |
| 17 |
return "\\{" .. table.concat(table.sortedkeys(self), ", ") .. "\\}" |
| 18 |
end |
| 19 |
} |
| 20 |
-- Adding functions to the module. |
| 21 |
function Set.new(str) |
| 22 |
str = str or "" |
| 23 |
local set = {} |
| 24 |
for v in utilities.parsers.iterator(str) do |
| 25 |
set[v] = true |
| 26 |
end |
| 27 |
return setmetatable(set, mt) |
| 28 |
end |
| 29 |
|
| 30 |
function Set.union (a, b) |
| 31 |
local union = {} |
| 32 |
for k in pairs(a) do union[k] = true end |
| 33 |
for k in pairs(b) do union[k] = true end |
| 34 |
return setmetatable(union, mt) |
| 35 |
end |
| 36 |
|
| 37 |
function Set.intersection (a, b) |
| 38 |
local intersection = {} |
| 39 |
for k in pairs(a) do |
| 40 |
intersection[k] = b[k] |
| 41 |
end |
| 42 |
return setmetatable(intersection, mt) |
| 43 |
end |
| 44 |
|
| 45 |
function Set.difference (a, b) |
| 46 |
local difference = {} |
| 47 |
for k in pairs(a) do |
| 48 |
if b[k]~= true then |
| 49 |
difference[k] = true |
| 50 |
end |
| 51 |
end |
| 52 |
setmetatable(difference, mt) |
| 53 |
return difference |
| 54 |
end |
| 55 |
|
| 56 |
function Set.cardinal (a) |
| 57 |
local len = 0 |
| 58 |
for k in pairs(a) do |
| 59 |
len = len + 1 |
| 60 |
end |
| 61 |
return len |
| 62 |
end |
| 63 |
|
| 64 |
function Set.subseteq(a, b) |
| 65 |
for k in pairs(a) do |
| 66 |
if not b[k] then return false end |
| 67 |
end |
| 68 |
return true |
| 69 |
end |
| 70 |
|
| 71 |
function Set.subset (a, b) |
| 72 |
return Set.subseteq(a, b) and not (Set.subseteq(b, a) ) |
| 73 |
end |
| 74 |
|
| 75 |
function Set.equal (a, b) |
| 76 |
return Set.subseteq(a, b) and (Set.subseteq(b, a) ) |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
function Set.belongsto (x, s) |
| 81 |
if s[x] then |
| 82 |
return true |
| 83 |
else |
| 84 |
return false |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
\end{luacode*} |
| 89 |
|
| 90 |
\newcommand\luaSetNew[2]{% |
| 91 |
\directlua{% |
| 92 |
sets[\luastringN{#1}] = Set.new(\luastringN{#2}) |
| 93 |
}% |
| 94 |
} |
| 95 |
|
| 96 |
\newcommand\luaSetUnion[3]{% |
| 97 |
\directlua{% |
| 98 |
sets[\luastringN{#1}] = Set.union(sets[\luastringN{#2}], |
| 99 |
sets[\luastringN{#3}]) |
| 100 |
}% |
| 101 |
} |
| 102 |
|
| 103 |
\newcommand\luaSetIntersection[3]{% |
| 104 |
\directlua{% |
| 105 |
sets[\luastringN{#1}] = Set.intersection(sets[\luastringN{#2}], |
| 106 |
sets[\luastringN{#3}]) |
| 107 |
}% |
| 108 |
} |
| 109 |
|
| 110 |
\newcommand\luaSetDifference[3]{% |
| 111 |
\directlua{% |
| 112 |
sets[\luastringN{#1}] = Set.difference(sets[\luastringN{#2}], |
| 113 |
sets[\luastringN{#3}]) |
| 114 |
}% |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
\newcommand\luaSetPrint[1]{% |
| 119 |
\directlua{tex.sprint(tostring(sets[\luastringN{#1}]))}% |
| 120 |
} |
| 121 |
|
| 122 |
\newcommand\luaSetCardinal[1]{% |
| 123 |
\directlua{tex.sprint(tostring(Set.cardinal(sets[\luastringN{#1}])))}% |
| 124 |
} |
| 125 |
|
| 126 |
\newcommand\luaSetSubseteq[2]{% |
| 127 |
\directlua{tex.sprint(tostring(Set.subseteq(sets[\luastringN{#1}],sets[\luastringN{#2}])))}% |
| 128 |
} |
| 129 |
|
| 130 |
\newcommand\luaSetSubset[2]{% |
| 131 |
\directlua{tex.sprint(tostring(Set.subset(sets[\luastringN{#1}],sets[\luastringN{#2}])))}% |
| 132 |
} |
| 133 |
|
| 134 |
\newcommand\luaSetEqual[2]{% |
| 135 |
\directlua{tex.sprint(tostring(Set.equal(sets[\luastringN{#1}],sets[\luastringN{#2}])))}% |
| 136 |
} |
| 137 |
|
| 138 |
\newcommand\luaSetBelongsto[2]{% |
| 139 |
\directlua{tex.sprint(tostring(Set.belongsto("#1",sets[\luastringN{#2}])))}% |
| 140 |
} |
| 141 |
|
| 142 |
\endinput |