%  MACROS FOR ALGORITHMS OR PROGRAMS     REQUIRES genmacs.tex

\newcounter{proglevel}
\newcounter{progline}
\newlength{\linenosep}
\newlength{\subprogindent}
\newlength{\progitemhangindent}
\newlength{\addprogitemsep}

\newenvironment{program}[1]%
{\setcounter{proglevel}{0}\setcounter{progline}{0}
 \settowidth{\linenosep}{\quad}
 \settowidth{\subprogindent}{\qquad}\settowidth{\progitemhangindent}{\quad}
 \setlength{\addprogitemsep}{0pt}
\newcommand{\setsubprogtabs}{#1\=\hspace*{\linenosep}
			     \hspace*{\value{proglevel}\subprogindent}\=
			     \hspace*{\progitemhangindent}\=\+\+\+\kill}
 \begin{tabpackage}\setlength{\tabbingsep}{0pt}\setsubprogtabs}%
{\end{tabpackage}}

% NOT TO BE USED AS AN ENVIRONMENT; USE ONLY \subprogram AND \endsubprogram
\newenvironment{subprogram}%
{\stepcounter{proglevel}\pushtabs\<\<\<\-\-\-\setsubprogtabs}%
{\poptabs\addtocounter{proglevel}{-1}}

\newif\ifprogitemarg
\makeatletter
\def\progitem{\@ifnextchar [{\progitemargtrue\progitemwarg}%
{\progitemargfalse\progitemwarg[\proglinenum]}}
\makeatother

\newlength{\progitemstrutheight}
\setlength{\progitemstrutheight}{0pt} % really start at {\baselineskip} ??
\addtolength{\progitemstrutheight}{\addprogitemsep}

\def\progitemwarg[#1]{\<\< #1 \'\>\rule{0pt}{\progitemstrutheight}%
\ifprogitemarg\else%
\addtocounter{progline}{-1}\refstepcounter{progline}\ignorespaces\fi}

\newcommand{\proglinenum}{\refstepcounter{progline}\theprogline}

\newcommand{\proglabel}[1]{\label{#1}\ignorespaces}

\newcommand{\progcom}[1]{[{\it #1}]}

\newcommand{\ifthenelse}[3]	% OLD COMMAND; MAY BE BOGUS IN CURRENT LATEX
    {{\it if} #1 \begin{description}
				  \item[\hfill {\it then}] #2
		                  \item[\hfill {\it else}] #3
		              \end{description}}

\newcommand{\Begin}{{\bf begin\ }}
\newcommand{\End}{{\bf end\ }}
\newcommand{\Do}{{\bf do\ }}
\newcommand{\Od}{{\bf od}}
\newcommand{\Enddo}{{\bf enddo}}
\newcommand{\Loop}{{\bf loop\ }}
\newcommand{\Endloop}{{\bf endloop}}
\newcommand{\For}{{\bf for\ }}
\newcommand{\Endfor}{{\bf endfor}}
\newcommand{\Foreach}{{\bf foreach\ }}
\newcommand{\To}{{\bf to\ }}
\newcommand{\If}{{\bf if\ }}
\newcommand{\Then}{{\bf then\ }}
\newcommand{\Else}{{\bf else\ }}
\newcommand{\Elif}{{\bf elif\ }}
\newcommand{\Fi}{{\bf fi}}
\newcommand{\Endif}{{\bf endif}}
\newcommand{\While}{{\bf while\ }}
\newcommand{\Endwhile}{{\bf endwhile}}
\newcommand{\Repeat}{{\bf repeat\ }}
\newcommand{\Until}{{\bf until\ }}
\newcommand{\Case}{{\bf case\ }}
\newcommand{\Of}{{\bf of\ }}

\newcommand{\Function}{{\bf function\ }}
\newcommand{\Return}{{\bf return\ }}
\newcommand{\Procedure}{{\bf procedure\ }}
\newcommand{\Call}{{\bf call\ }}
\newcommand{\With}{{\bf with\ }}
\newcommand{\Imports}{{\bf imports\ }}
\newcommand{\Var}{{\bf var\ }}
\newcommand{\Type}{{\bf type\ }}
\newcommand{\Assert}{{\bf assert\ }}
\newcommand{\True}{{\rm true}}
\newcommand{\False}{{\rm false}}

% PROTOCOLS and ALGOs

\newcounter{protoline}
\newlength{\boxwidth}

\setlength{\boxwidth}{\textwidth}
\addtolength{\boxwidth}{-0.6cm}

\newtheorem{proto_body}{Protocol}[section]
\newtheorem{algo_body}{Algorithm}[section]

\newenvironment{protocol}[1]{ 
\setcounter{protoline}{0}
\begin{minipage}{\boxwidth}
\newcommand{\step}[1]
	{\addtocounter{protoline}{1} {\item [\ \ \ \ \arabic{protoline}:]{\small##1 } }}
\begin{proto_body}[ #1 ]
\ \\ 
\begin{description} }{\end{description}\end{proto_body}\end{minipage}}
%
\newenvironment{algorithm}[1]{ 
\setcounter{protoline}{0}
\begin{minipage}{\boxwidth}
\newcommand{\step}[1]
	{\addtocounter{protoline}{1} {\item [\ \ \ \ \arabic{protoline}:]{\small##1 } }}
\begin{algo_body}[ #1 ]
\ \\ 
\begin{description} }{\end{description}\end{algo_body}\end{minipage}}

\newcommand{\proto}[2]{\ \\ \fbox{ \begin{protocol}{#1}#2 \end{protocol}}\ \\}
\newcommand{\algo}[2]{\ \\ \fbox{ \begin{algorithm}{#1}#2 \end{algorithm}}\ \\}

\newenvironment{protocol_cont}[0]{ 
\begin{minipage}{\boxwidth}
\addtocounter{proto_body}{-1}
\newcommand{\step}[1]
	{\addtocounter{protoline}{1} {\item [\ \ \ \ \arabic{protoline}:]{\small##1 } }}
\begin{proto_body}
\ \\ 
\begin{description} }{\end{description}\end{proto_body}\end{minipage}}

\newcommand{\cont}[1]{\ \\ \fbox{ \begin{protocol_cont}#1 \end{protocol_cont}}\ \\}

\newcommand{\dist}[2]{
\newcommand{\alt}[2] {##1 & \mbox{\rm : } ##2}
$ #1 \inr \left\{
\begin{array}{ll}
#2
\end{array}
\right.$
}

% REDUCTION

\newcounter{reduline}
\newlength{\boxwidthr}

\setlength{\boxwidthr}{\textwidth}
\addtolength{\boxwidthr}{-0.6cm}

\newtheorem{redu_body}{Reduction}[section]
\newenvironment{reduction}[2]{ 
\setcounter{reduline}{0}
\begin{minipage}{\boxwidthr}
\newcommand{\step}[1]
	{\addtocounter{reduline}{1} {\item [\ \ \ \ \arabic{reduline}:]{\small##1 } }}
\begin{redu_body}[ #1 from #2 ]
\ \\ 
\begin{description} }{\end{description}\end{redu_body}\end{minipage}}

\newcommand{\redu}[3]{\ \\ \fbox{ \begin{reduction}{#1}{#2}#3 \end{reduction}}\ \\}

\newenvironment{redu_cont}[0]{ 
\begin{minipage}{\boxwidthr}
\addtocounter{redu_body}{-1}
\newcommand{\step}[1]
	{\addtocounter{reduline}{1} {\item [\ \ \ \ \arabic{reduline}:]{\small##1 } }}
\begin{redu_body}
\ \\ 
\begin{description} }{\end{description}\end{redu_body}\end{minipage}}

\newcommand{\rcont}[1]{\ \\ \fbox{ \begin{redu_cont}#1 \end{redu_cont}}\ \\}
