summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luaset/luaset.sty
blob: 4b2fac6655b2ee2e4511f9fc0dffd749e630f487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
% luaset package
% version 1.1
% Authors: Chetan Shirore and Ajit Kumar
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.


\ProvidesPackage{luaset}[1.1]
\RequirePackage{xkeyval}
\RequirePackage{amsmath}
\RequirePackage{luacode}
\RequirePackage{luamaths}
\begin{luacode*}
sets = {} -- global registry for sets.
Set = {} -- the module.
local mt = {
    __tostring = function(self)
        return "\\{" .. table.concat(table.sortedkeys(self), ", ") .. "\\}"
    end
}
-- Adding functions to the module.
function Set.new(str)
    str = str or ""
    local set = {}
    for v in utilities.parsers.iterator(str) do
        set[v] = true
    end
    return setmetatable(set, mt)
end

function Set.union (a, b)
        local union = {}
        for k in pairs(a) do union[k] = true end
        for k in pairs(b) do union[k] = true end
        return setmetatable(union, mt)
      end

function Set.intersection (a, b)
        local intersection = {}
        for k in pairs(a) do
          intersection[k] = b[k]
            end
        return setmetatable(intersection, mt)
        end

function Set.difference (a, b)
        local difference = {}
        for k in pairs(a) do
        if b[k]~= true then
          difference[k] = true
        end
        end
         setmetatable(difference, mt)
        return difference
      end

function Set.cardinal (a)
        local len = 0
        for k in pairs(a) do
          len = len + 1
          end
        return len
      end

function Set.subseteq(a, b)
        for k in pairs(a) do
          if not b[k] then return false end
        end
        return true
    end

function Set.subset (a, b)
        return Set.subseteq(a, b)  and not (Set.subseteq(b, a) )
    end

    function Set.equal (a, b)
        return Set.subseteq(a, b)  and  (Set.subseteq(b, a) )
    end


function Set.belongsto (x, s)
        if s[x] then
        return true
      else
        return false
        end
    end

\end{luacode*}

\newcommand\luaSetNew[2]{%
    \directlua{%
        sets[\luastringN{#1}] = Set.new(\luastringN{#2})
    }%
}

\newcommand\luaSetUnion[3]{%
    \directlua{%
        sets[\luastringN{#1}] = Set.union(sets[\luastringN{#2}],
                                          sets[\luastringN{#3}])
    }%
}

\newcommand\luaSetIntersection[3]{%
    \directlua{%
        sets[\luastringN{#1}] = Set.intersection(sets[\luastringN{#2}],
                                          sets[\luastringN{#3}])
    }%
}

\newcommand\luaSetDifference[3]{%
    \directlua{%
        sets[\luastringN{#1}] = Set.difference(sets[\luastringN{#2}],
                                          sets[\luastringN{#3}])
    }%
}


\newcommand\luaSetPrint[1]{%
    \directlua{tex.sprint(tostring(sets[\luastringN{#1}]))}%
}

\newcommand\luaSetCardinal[1]{%
    \directlua{tex.sprint(tostring(Set.cardinal(sets[\luastringN{#1}])))}%
}

\newcommand\luaSetSubseteq[2]{%
    \directlua{tex.sprint(tostring(Set.subseteq(sets[\luastringN{#1}],sets[\luastringN{#2}])))}%
}

\newcommand\luaSetSubset[2]{%
    \directlua{tex.sprint(tostring(Set.subset(sets[\luastringN{#1}],sets[\luastringN{#2}])))}%
}

\newcommand\luaSetEqual[2]{%
    \directlua{tex.sprint(tostring(Set.equal(sets[\luastringN{#1}],sets[\luastringN{#2}])))}%
}

\newcommand\luaSetBelongsto[2]{%
    \directlua{tex.sprint(tostring(Set.belongsto("#1",sets[\luastringN{#2}])))}%
}

\endinput