% \iffalse meta-comment
%%% File: algo-addons.sty
%% Copyright (C) 2017 Michael N\"usken.
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3a
% of this license or (at your option) any later version.
% The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% -----------------------------------------------------------
% 
%<*chartab>
%\fi
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
% \iffalse meta-comment
%</chartab>
% \fi
%
% \CheckSum{0}
%
%
%  \GetFileInfo{algo-addons.sty}
%  \CodelinePrefix{A}
%
% \section{Source of \filename (\fileprefix)}
%
%  This file (\file{\filename}) has version number \fileversion, last
%  revised \filedate.
%    \begin{macrocode}
%<*algolists>
%    \end{macrocode}
%
%  Announce the package and its version:
%    \begin{macrocode}
% \iffalse
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algo-addons}[2017/04/21 v0.94 %
Lists for algorithms. MNuesken]
% \fi
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption{override-cc}{%
  \PassOptionsToPackage{override-cc}{algo-lists}
  \PassOptionsToPackage{override-cc}{algo-addons}
  \PassOptionsToPackage{override-cc}{algo-switch}  
  \let\algoprettify\@undefined
  \let\ifblock\@undefined
  \let\endifblock\@undefined
  \let\elifblock\@undefined
  \let\endelifblock\@undefined
  \let\elseblock\@undefined
  \let\endelseblock\@undefined
  \let\forblock\@undefined
  \let\endforblock\@undefined
  \let\whileblock\@undefined
  \let\endwhileblock\@undefined
  \let\blockwhile\@undefined
  \let\endblockwhile\@undefined
  \let\untilblock\@undefined
  \let\enduntilblock\@undefined
  \let\blockuntil\@undefined
  \let\endblockuntil\@undefined
  \let\ifblock\@undefined
  \let\endifblock\@undefined
}
\ProcessOptions
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{algo-lists}
%    \end{macrocode}
%  
%  \subsection{Some high level things for algorithms}
%
% \begin{macro}{\algolistdefaultkeywords}
%  Keywords in algorithms are typeset by the command
%  |\algolistkeyword{|\meta{keyword}|}|.
%    \begin{macrocode}
\newcommand{\algolistkeyword}[1]{\textsf{#1} \ignorespaces}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\algolistdefaultkeywords}
%  Some keywords are predefined.  All these things are only accessible
%  inside algorithm like environments.
%    \begin{macrocode}
\newcommand{\algolistdefaultkeywords}{%
  \relax\let\reserved@a\@undefined
  \def\FOR{\algolistkeyword{For}}%
  \def\DO{\algolistkeyword{do}}%
  \def\endFOR{}%
  \def\WHILE{\algolistkeyword{While}}%
  \def\endWHILE{}%
  \def\REPEAT{\algolistkeyword{Repeat}}%
  \def\UNTIL{\algolistkeyword{Until}}%
  \def\IF{\algolistkeyword{If}}%
  \def\THEN{\algolistkeyword{then}}%
  \def\ELIF{\algolistkeyword{Else if}}%
  \def\ELSE{\algolistkeyword{Else}}%
  \def\endIF{}%
  \def\CALL{\algolistkeyword{Call}}%
  \def\RETURN{\algolistkeyword{Return}}%
  %%% \def\@tempa{nohyphenation}\ifx\languagename\@tempa\else%
  %%% \expandafter\selectlanguage\expandafter{\languagename}\fi%
  \def\COMMENT##1{$\{$ ##1 $\}$}%
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{algosteplist}
%    This environment uses \Lenv{algoitemize} with keywords and
%    access to all nice of block stuff.  Feel free to redefine it
%    after loading this package so that it suits your needs.
%    \begin{macrocode}
\newtoks\everyalgosteplist
\everyalgosteplist{%
  \def\Recall#1{#1}\def\RECALL#1{#1}%
  \scriptsize
}
\newenvironment{algosteplist}{%
  \par
  \algolistdefaultkeywords
  \the\everyalgosteplist
  \algoitemize
}{%
  \endalgoitemize
}
%   \end{macrocode}
% \end{environment}
% \begin{environment}{steplist}
%    This is barely the previous but in an untitled figure.
%    \begin{macrocode}
\newenvironment{steplist}[1][ht!]{%
  \figure[#1]\relax
  \algosteplist
}{\endalgosteplist\endfigure}
%    \end{macrocode}
% \end{environment}
% \end{environment}
%
%  \begin{macro}{\algolistoptitem}
%    This is a helper for the following sophisticated environments.
%    It issues an |\item| if necessary, and omits it otherwise.
%    \begin{macrocode}
\newcommand{\algolistoptitem}[1]{%
  \def\@tempa{#1}\ifx\@tempa\@empty%
  \if@inlabel\let\next\par\else\let\next\item\fi
  \else
  \def\next{\item[#1]}%
  \fi\next%
  }
%    \end{macrocode}
%  \end{macro}
%  
% \begin{macro}{\algolistshort}
%   This macro acts like \cs{short} from \Lpackage{relabel} provided
%   it is defined.  Otherwise it does nothinig.  Only use right before
%   \cs{ref}.
%    \begin{macrocode}
\def\algolistshort{%
  \@ifundefined{short}{}{\short}}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\algoprettify}
%    This is a helper for the following sophisticated environments.
%    It formats a 3-term command like \cs{IF} \meta{condition}
%    \cs{THEN} in a line or a multiline block depending on what is
%    nicer.
%    \begin{macrocode}
\newcommand{\algoprettify}[3]{%
  \setbox2\hbox{#1}%
  \setbox4\hbox{#2}%
  \setbox6\hbox{#3}%
  \setbox0\hbox{\unhcopy2\unhcopy4{ }\unhcopy6}%
  \ifdim\wd0<\linewidth
  \unhbox0
  \else
  \dimen0=\linewidth \advance\dimen0 by -\wd2
  \unhbox2\parbox[t]{\dimen0}{#2\strut}\par\algolist@list@parshape\unhbox6
  \fi
  }
%    \end{macrocode}
%  \end{macro}
%  
%  \begin{macro}{\algolistvisualmarker}
%    This command is meant for visually highlighting particularly
%    important steps in an algorithm.
%    \begin{macrocode}
\newcommand{\algolistvisualmarker}[1]{%
  \leavevmode\rlap{%
    \hbox to\linewidth{%
      \hfill
      \colorbox{green}{%
        \hbox{\strut\hfil\quad\color{black}#1\quad\hfil}%
      }%
    }%
  }%
}
%   \end{macrocode}
% \end{macro}
%  \begin{environment}{ifblock}
%    This produces |\IF| block.
%    \begin{macrocode}
\newenvironment{ifblock}[2][]
%    \end{macrocode}
%    What do we do at the |\begin|.
%    \begin{macrocode}
{%
%    \end{macrocode}
%    First, add |\item| if there is no |\item| immediately before the
%    \Lenv{ifblock}.
%    \begin{macrocode}
  \algolistoptitem{#1}%
%    \end{macrocode}
%    Insert the text `|\IF|~\meta{condition}~|\THEN|'.
%    |\algoprettify| takes care of a long \meta{condition}.
%    \begin{macrocode}
  \algoprettify{\IF}{#2}{\THEN}
%    \end{macrocode}
%    Begin the indention.
%    \begin{macrocode}
  \indention
}
%    \end{macrocode}
%    What do we do at the |\end|.
%    \begin{macrocode}
{%
%    \end{macrocode}
%    End the indention.
%    \begin{macrocode}
  \endindention
}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{elifblock}
%    This produces an |\ELIF| block in the same manner as the |\IF| block.
%    \begin{macrocode}
\newenvironment{elifblock}[2][]{%
  \algolistoptitem{#1}%
  \algoprettify{\ELIF}{#2}{\THEN}
  \indention
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{elseblock}
%    The |\ELSE| block differs from the last two only in the fact that
%    there is no condition.
%    \begin{macrocode}
\newenvironment{elseblock}[1][]{%
  \algolistoptitem{#1}%
  \ELSE%
  \indention
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{doblock}
%    The |\ELSE| block differs from the last two only in the fact that
%    there is no condition.
%    \begin{macrocode}
\newenvironment{doblock}[1][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \DO \algolistshort\ref{@@@-body\currentblock}%
  \indention
  \label{@@@-body\currentblock}%
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{forblock}
%    A |\FOR| block typesets a loop `for \meta{control} do \dots'.
%    Other than the previous environments we use \Lenv{block} rather
%    than only \Lenv{indention} since the range of steps in the
%    indented part is printed after the `do' (unless it is only one step).
%    \begin{macrocode}
\newenvironment{forblock}[2][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \algoprettify{\FOR}{#2}{%
    \DO \algolistshort\ref{@@@\currentblock-body}}%
  \indention
  \label{@@@\currentblock-body}
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{whileblock}
%    The |\WHILE| block is similar to the |\FOR| block: `while
%    \meta{condition} do \dots'.
%    \begin{macrocode}
\newenvironment{whileblock}[2][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \algoprettify{\WHILE}{#2}{%
    \DO \algolistshort\ref{@@@-body\currentblock}}%
  \indention
  \label{@@@-body\currentblock}%
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{untilblock}
%    The |\UNTIL| block is similar to the |\FOR| block: `until
%    \meta{condition} do \dots'.
%    \begin{macrocode}
\newenvironment{untilblock}[2][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \algoprettify{\UNTIL}{#2}{%
    \DO \algolistshort\ref{@@@-body\currentblock}}%
  \indention
  \label{@@@-body\currentblock}%
}{%
  \endindention}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{blockuntil}
%    The reverse |\UNTIL| block is similar to the |\UNTIL| block but
%    the condition is checked at the end rather than at the beginning
%    of the environment.
%    \begin{macrocode}
\newenvironment{blockuntil}[2][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \def\repeat@cond{#2}%
  \REPEAT \algolistshort\ref{@@@-body\currentblock}%
  \indention
  \label{@@@-body\currentblock}%
}{%
  \endindention
\item
  \algoprettify{\UNTIL}{\repeat@cond}{}}
%    \end{macrocode}
%  \end{environment}
%  
%  \begin{environment}{blockwhile}
%    The reverse |\WHILE| block is similar to the |\WHILE| block but
%    the condition is checked at the end rather than at the beginning
%    of the environment.
%    \begin{macrocode}
\newenvironment{blockwhile}[2][]{%
  \algolistnextblock
  \algolistoptitem{#1}%
  \def\repeat@cond{#2}%
  \REPEAT \algolistshort\ref{@@@-body\currentblock}%
  \indention
  \label{@@@-body\currentblock}%
}{%
  \endindention
\item
  \algoprettify{\WHILE}{\repeat@cond}{}}
%    \end{macrocode}
%  \end{environment}
%
%
% \subsection{Gamehopsituation for Game Hopping Lemma}
%
%    \begin{macrocode}
\def\gamehopsituationA#1#2{%
  \gamehopsituationx{\ref{base:#1}}{\ref{game:#1}}{\ref{#2}}{\ref{red:#1}}%
}%
\def\gamehopsituationaautored#1#2#3{%
  \gamehopsituationx{\ref{#1}}{\ref{#2}}{\ref{#3}}{\reduction{#1}{#2}}%
}%
\def\gamehopsituation#1#2#3#4{%
  \gamehopsituationx{\ref{#1}}{\ref{#2}}{\ref{#3}}{\ref{#4}}%
}%
\def\gamehopsituationx#1#2#3#4{%
  \par\begin{minipage}{1.0\linewidth}
    \begin{itemize}
      \let\raggedright\centering
    \item[] \def\switchrule##1~{\kern.4pt~}% hack
      \switchc{}{}{#3\\$\updownarrow$}{}{}%
    \item[] \def\switchrule##1~{\vrule~}% hack
      \switchc%
      {#1}%
      {}{#4}%
      {}{#2}
    \end{itemize}
    \vspace{.3\baselineskip}
  \end{minipage}\par
}
%    \end{macrocode}
%  
%
%    \begin{macrocode}
%</algolists>
%    \end{macrocode}
