| 1 |
\section{The package sets}\label{sets} |
| 2 |
|
| 3 |
As described in the introduction, sets was designed to support set operations. The elements of a set are normally simple Text, but you can insert commands in a set, too. |
| 4 |
These commands will---except when printing the set---not expanded. |
| 5 |
The usage of braces (`\{' and `\}') unfortunately doesn't work. In this case you have to define a shortcut that doesn't need braces. However, Parameters without braces work. |
| 6 |
``\verb|H"agar|'' is therefore a valid element of a set. ``\verb|\endset|'' and ``\verb|\empty|'' can not be part of a set. |
| 7 |
|
| 8 |
As a document has only a few authors, not much effort was put in improving efficiency. |
| 9 |
Sets should therefore be relatively small. If you nevertheless try to create a set with hundreds or thousands of items, \TeX's stack might overflow. |
| 10 |
|
| 11 |
In most cases the sequence of items doesn't matter. This is also the case in most commands here. Exceptions will be marked. |
| 12 |
|
| 13 |
The package sets needs \LaTeXe. |
| 14 |
|
| 15 |
\subsection{Usage}\label{sets-usage} |
| 16 |
|
| 17 |
In this subsection the usage of the package sets is described. In the description, some example sets will be used: |
| 18 |
\begin{eqnarray*} |
| 19 |
A &=& \{Alice, Bob, Charly\}\\ |
| 20 |
B &=& \{Alice, Bob\}\\ |
| 21 |
C &=& \{Bob, Dean\}\\ |
| 22 |
D &=& \{Dean\}\\ |
| 23 |
L &=& \emptyset |
| 24 |
\end{eqnarray*} |
| 25 |
|
| 26 |
%----------------------------------------------------------------------------- |
| 27 |
\subsubsection{Constructors}\label{constructors} |
| 28 |
To create a set, the commands\\ |
| 29 |
\mbox{}\hspace{2em}\verb$\newset{<set>}{<content>}$\\ |
| 30 |
and\\ |
| 31 |
\mbox{}\hspace{2em}\verb$\newsetsimple{<set>}{<content>}$\\ |
| 32 |
can be used. \texttt{<set>} is a command name to access the set from now on. |
| 33 |
The items of a set are separated with \texttt{|}. The set $A$ could therefore be defined with:\\ |
| 34 |
\mbox{}\hspace{2em}\verb$\newset{\sA}{Alice|Bob|Charly}$\\ |
| 35 |
The set $L$ is defined with:\\ |
| 36 |
\mbox{}\hspace{2em}\verb$\newset{\sL}{}$\\ |
| 37 |
|
| 38 |
\verb$\newset$ creates a new set. This set will be sorted in alphabetical order and duplicates will be removed. So it would be no matter if in the definition of $A$ after ``Charly'' a second ``Alice'' was inserted. |
| 39 |
|
| 40 |
The effort for sorting and duplicate deleting is unnecessary at this point. If you want to skip these expensive steps, you can create a set with the command |
| 41 |
\verb$\newsetsimple$, too. |
| 42 |
|
| 43 |
Because they are needed later, we will create all sets mentioned above:\\ |
| 44 |
\mbox{}\hspace{2em}\verb$\newsetsimple{\sA}{Alice|Bob|Charly}$ \newsetsimple{\sA}{Alice|Bob|Charly}\\ |
| 45 |
\mbox{}\hspace{2em}\verb$\newsetsimple{\sD}{Alice|Bob}$ \newsetsimple{\sB}{Alice|Bob}\\ |
| 46 |
\mbox{}\hspace{2em}\verb$\newsetsimple{\sC}{Bob|Dean}$ \newsetsimple{\sC}{Bob|Dean}\\ |
| 47 |
\mbox{}\hspace{2em}\verb$\newsetsimple{\sD}{Dean}$ \newsetsimple{\sD}{Dean}\\ |
| 48 |
\mbox{}\hspace{2em}\verb$\newsetsimple{\sL}{}$ \newsetsimple{\sL}{} |
| 49 |
%----------------------------------------------------------------------------- |
| 50 |
\subsubsection{Inspectors}\label{inspectors} |
| 51 |
Inspectores help you to retrieve informations about sets and to print sets. |
| 52 |
|
| 53 |
\paragraph{Printing:}A set can be printed using the command\\ |
| 54 |
\mbox{}\hspace{2em}\verb$\listset$.\\ |
| 55 |
The elements will be put in the sequence they are in the set. A comma is used as separator.\\ |
| 56 |
\mbox{}\hspace{2em}\verb$\listset{\sA}$ therefore leads to the following output:\\ |
| 57 |
\centerline{\listset{\sA}} |
| 58 |
|
| 59 |
Sometimes you might want to separate the items in a different way, for example with a \texttt{\&} to put them in a table. In this case a (temporary) redefinition of\\ |
| 60 |
\mbox{}\hspace{2em}\verb$\setseparator$\\ |
| 61 |
helps you. Normally this command expands to `\verb*$,\ $'. |
| 62 |
|
| 63 |
\paragraph{Determining the size of a set:} The next inspector has the syntax\\ |
| 64 |
\mbox{}\hspace{2em}\verb$\sizeofset{$$S$\verb$}\is{<counter>}$,\\ |
| 65 |
where \texttt{<counter>} is the name of a \LaTeX\ counter, which afterwards will contain the number of elements in set $S$. The sequence\\ |
| 66 |
\mbox{}\hspace{2em}\verb$\newcounter{mycounter}$\newcounter{mycounter}\\ |
| 67 |
\mbox{}\hspace{2em}\verb$\sizeofset{\sB}\is{mycounter}$\sizeofset{\sB}\is{mycounter}\\ |
| 68 |
\mbox{}\hspace{2em}\verb$\arabic{mycounter}$\\ |
| 69 |
leads to the output: ``\arabic{mycounter}'' |
| 70 |
If you determine the size of set $L$, the result\sizeofset{\sL}\is{mycounter} (as you might have expected) is ``\arabic{mycounter}''. |
| 71 |
|
| 72 |
\paragraph{Testing for membership:}By using the command\\ |
| 73 |
\mbox{}\hspace{2em}\verb$\iselementofset{$$e$\verb$}{$$S$\verb$}$\\ |
| 74 |
you can check, whether $e \in S$ is true. The effort is $O(1)$, because all work is done by the pattern matching of \TeX. The sequence\\ |
| 75 |
\mbox{}\hspace{2em}\verb$\if \iselementofset{Bob}{\sC}Yes\else No\fi$\\ |
| 76 |
would result in the output ``\if \iselementofset{Bob}{\sC}Yes\else No\fi'', the same test with set $D$ in ``\if \iselementofset{Bob}{\sD}Yes\else No\fi''. |
| 77 |
|
| 78 |
%----------------------------------------------------------------------------- |
| 79 |
\subsubsection{Modificators}\label{modificators} |
| 80 |
|
| 81 |
\paragraph{Union of sets:}The operation $R := S_1 \cup S_2$ is realized in the command\\ |
| 82 |
\mbox{}\hspace{2em}\verb|\unionsets{|$S_1$\verb|}{|$S_2$\verb|}\to{|$R$\verb|}|.\\ |
| 83 |
Table \ref{tab:ops} contains some examples. The result of the operation is a sorted set without duplicates containing the items of sets $S_1$ and $S_2$. |
| 84 |
|
| 85 |
\paragraph{Difference of sets:}The operation $R := S_1 - S_2$ (also written as $R := S_1 \backslash S_2$) can be carried out with\\ |
| 86 |
\mbox{}\hspace{2em}\verb|\minussets{|$S_1$\verb|}\minus{|$S_2$\verb|}\to{|$R$\verb|}|.\\ |
| 87 |
If $S_1$ is a sorted set, $R$ will be sorted, too. If $S_1$ contains duplicates, $R$ might also contains these duplicate elements. Table \ref{tab:ops} contains several examples for the usage of this command. |
| 88 |
|
| 89 |
Colloquially you can formulate the operation as follows: Check for every element $e$ in $S_1$ if $e \in S_2$ is true. If not, insert $e$ into $R$. And that's exactly the way it has been implemented! |
| 90 |
|
| 91 |
\paragraph{Intersection of sets:} The operation $R := S_1 \cap S_2$ is made possible with the command\\ |
| 92 |
\mbox{}\hspace{2em}\verb|\intersectsets{|$S_1$\verb|}{|$S_2$\verb|}\to{|$R$\verb|}|.\\ |
| 93 |
As above: If $S_1$ is a sorted set, $R$ will be sorted, too. If $S_1$ contains duplicates, $R$ might also contains these duplicate elements. Table \ref{tab:ops} contains several examples for the usage of this command, too. |
| 94 |
|
| 95 |
This operation can colloquially be written down as: Check for every element $e$ in $S_1$ if $e \in S_2$ is true. If yes, insert $e$ into $R$. If you compare this with the formulation above, one can recognize that the only difference is the small word ``yes''. |
| 96 |
In the source code, this expresses in a missing \verb|\else|. Acutally amazingly simple if you remember the formal relation $S_1 \cap S_2 \equiv S_1 \backslash (S_1\backslash S_2)$, which lets one expect a much higher complexity. |
| 97 |
|
| 98 |
\begin{table}%[htb] |
| 99 |
\begin{center} |
| 100 |
\begin{tabular}{ll}\hline |
| 101 |
\textbf{Operation} & \textbf{Result} \\ \hline |
| 102 |
\unionsets{\sA}{\sC}\to{\sR}\global\let\sR\sR |
| 103 |
\verb$\unionsets{\sA}{\sC}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 104 |
\unionsets{\sB}{\sD}\to{\sR}\global\let\sR\sR |
| 105 |
\verb$\unionsets{\sB}{\sD}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 106 |
\unionsets{\sL}{\sC}\to{\sR}\global\let\sR\sR |
| 107 |
\verb$\unionsets{\sL}{\sC}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 108 |
\unionsets{\sL}{\sL}\to{\sR}\global\let\sR\sR |
| 109 |
\verb$\unionsets{\sL}{\sL}\to{\sR}$ & ``\listset{\sR}'' \\\hline |
| 110 |
% |
| 111 |
\minussets{\sA}\minus{\sC}\to{\sR}\global\let\sR\sR |
| 112 |
\verb$\minussets{\sA}\minus{\sC}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 113 |
\minussets{\sD}\minus{\sC}\to{\sR}\global\let\sR\sR |
| 114 |
\verb$\minussets{\sD}\minus{\sC}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 115 |
\minussets{\sD}\minus{\sB}\to{\sR}\global\let\sR\sR |
| 116 |
\verb$\minussets{\sD}\minus{\sB}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 117 |
\minussets{\sA}\minus{\sL}\to{\sR}\global\let\sR\sR |
| 118 |
\verb$\minussets{\sA}\minus{\sL}\to{\sR}$ & ``\listset{\sR}'' \\\hline |
| 119 |
% |
| 120 |
\intersectsets{\sA}{\sB}\to{\sR}\global\let\sR\sR |
| 121 |
\verb$\intersectsets{\sA}{\sB}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 122 |
\intersectsets{\sC}{\sB}\to{\sR}\global\let\sR\sR |
| 123 |
\verb$\intersectsets{\sC}{\sB}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 124 |
\intersectsets{\sB}{\sD}\to{\sR}\global\let\sR\sR |
| 125 |
\verb$\intersectsets{\sB}{\sD}\to{\sR}$ & ``\listset{\sR}'' \\ |
| 126 |
\intersectsets{\sA}{\sL}\to{\sR}\global\let\sR\sR |
| 127 |
\verb$\intersectsets{\sA}{\sL}\to{\sR}$ & ``\listset{\sR}'' \\\hline |
| 128 |
\end{tabular} |
| 129 |
\caption{Set operations, examples} |
| 130 |
\label{tab:ops} |
| 131 |
\end{center} |
| 132 |
\end{table} |
| 133 |
|
| 134 |
\paragraph{Sorting:} A set $S$ can be sorted alphabetically by using the command\\ |
| 135 |
\mbox{}\hspace{2em}\verb|\sortset{|$S$\verb|}{|$R$\verb|}|.\\ |
| 136 |
After execution of the command, $R$ contains the sorted set. The sorting is done by the bubblesort algorithm, an algorithm, that can be implemented in \TeX\ without having to perform many contortions. |
| 137 |
|
| 138 |
At sorting, the elements are compared as they are, i.\,e. possibly contained macros are not expanded but compared by their name (including the backslash). |
| 139 |
|
| 140 |
\paragraph{Removing duplicates:} The operations \emph{only} works on sorted sets! You actually will not need it very often, because the creation of a set using \verb|\newset| does the work automatically (by using this macro). However, I decided to make this macro available to public; probably sometimes someone really needs it. |
| 141 |
Duplicate removal is called with:\\ |
| 142 |
\mbox{}\hspace{2em}\verb|\deleteduplicates{|$S$\verb|}{|$R$\verb|}|,\\ |
| 143 |
with $R$ being the result set and $S$ being the sorted set, whose duplicates shall be removed. |
| 144 |
|
| 145 |
%------------------------------------------------------------------------------ |
| 146 |
\subsection{Effort estimations} |
| 147 |
Table \ref{tab:complexity} lists the complexity of the operations in O-notation. The following Assumptions are made: |
| 148 |
\begin{itemize} |
| 149 |
\item Let the length of an element of a set be $m$. |
| 150 |
\item Let the number of elements in a set be $n$. If an operation need two sets, $n_1$ is the cardinality of the first set and $n_2$ the cardinality of the second one. |
| 151 |
\item For simplicity reasons the effort for pattern matching in parameter processing is assumed to be constant. |
| 152 |
\end{itemize} |
| 153 |
|
| 154 |
The given complexity classes can help you to arrange a set of operations in the optimal sequence. For example when using the commands \verb@\intersectsets@ or \verb@\minussets@ it is better to let the smaller set be the first parameter. |
| 155 |
|
| 156 |
\begin{table}[htb] |
| 157 |
\begin{center} |
| 158 |
\begin{tabular}{lc}\hline |
| 159 |
\textbf{Operation} & \textbf{Operation}\\ \hline |
| 160 |
Compare elements & $m$\\ |
| 161 |
\verb@\sizeofset@ & $n$\\ |
| 162 |
\verb@\listset@ & $n$\\ |
| 163 |
\verb@\iselementofset@ & $1$\\%\hline |
| 164 |
\verb@\sortset@ & $m \cdot n^2$\\ |
| 165 |
\verb@\deleteduplicates@ & $n$\\%\hline |
| 166 |
\verb@\newset@ & $m \cdot n^2$\\ |
| 167 |
\verb@\newsetsimple@ & $1$\\%\hline |
| 168 |
\verb@\unionsets@ & $m \cdot (n_1 + n_2)^2$\\ |
| 169 |
\verb@\intersectsets@ & $n_1$\\ |
| 170 |
\verb@\minussets@ & $n_1$\\ \hline |
| 171 |
\end{tabular} |
| 172 |
\caption{Complexity classes of set operations}% |
| 173 |
\label{tab:complexity}% |
| 174 |
\end{center} |
| 175 |
\end{table} |