% ALGORITHMIC STYLE -- Released 17 DEC 2003
%    for LaTeX version 2e
%
% Copyright Szász János
% E-mail szaszjanos@users.sourceforge.net
% Based on Peter Williams's algorithmic.sty
%
%
%
%      ***      INITIALISING      ***
%
%
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algorithmicx}
\typeout{Document Style - a greatly improved `algorithmic' style}
%
\RequirePackage{ifthen}
%
\newboolean{ALG@include@pascal}
\setboolean{ALG@include@pascal}{false}
\newboolean{ALG@include@c}
\setboolean{ALG@include@c}{false}
\newboolean{ALG@include@pseudocode}
\setboolean{ALG@include@pseudocode}{false}
%
\DeclareOption{pascal}{\setboolean{ALG@include@pascal}{true}}
\DeclareOption{c}{\setboolean{ALG@include@c}{true}}
\DeclareOption{pseudocode}{\setboolean{ALG@include@pseudocode}{true}}
%
\ProcessOptions
%
\newcounter{ALG@line}
\newcounter{ALG@rem}
\newlength{\ALG@tlm}
\newlength{\algorithmicindent}
\setlength{\algorithmicindent}{1.5em}
\newcounter{ALG@blocknr}% the number of defined blocks
\setcounter{ALG@blocknr}{0}%
\newcounter{ALG@tmpcounter}% only to decrement things
%
%
%
%      ***      ALGORITHMIC      ***
%
%
%
\newenvironment{ALG@g}[1]% a list for the iner loops
   {%
   \begin{list}%
      {\ALG@step}%
      {%
      \itemsep\z@%
      \itemindent\z@%
      \listparindent2em% the indent given to multiline lines...
      \rightmargin\z@%
      \topsep\z@%
      \partopsep\z@%
      \parskip\z@%
      \parsep\z@%
      \leftmargin#1%
      \addtolength{\ALG@tlm}{\leftmargin}%
      \labelsep\ALG@tlm%
      }%
   \global\@newlistfalse% give no error, if the list is empty
   }%
   {\end{list}}%
%
%
%
%      ***      algorithmic      ***
%
%
%
\let\ALG@beginalgorithmic\relax% for user overrides
\let\ALG@endalgorithmic\relax% for user overrides
\newenvironment{algorithmic}[1][0]%
   {%
%     labeling macro -- sorry, badly implemented
   \let\ALG@label\label%
   \renewcommand{\label}%
      {%
      \edef\@currentlabel{\arabic{ALG@line}}%
      \ALG@label%
      }%
   \newcommand{\ALG@step}%
      {%
%      \rightmark{\arabic{ALG@rem}}% ???
      \addtocounter{ALG@line}{1}%
      \addtocounter{ALG@rem}{1}%
      \ifthenelse{\equal{\arabic{ALG@rem}}{#1}}%
         {\setcounter{ALG@rem}{0}}%
         {}%
      \ifthenelse{\equal{\arabic{ALG@rem}}{0}}%
         {{\footnotesize \arabic{ALG@line}:}}%
         {}%
      }%
   %
   %
   %
   \let\@listii\@listi%
   \let\@listiii\@listi%
   \let\@listiv\@listi%
   \let\@listv\@listi%
   \let\@listvi\@listi%
   \let\@listvii\@listi%
   %
   %
   %
   \renewcommand{\\}% for multiline parameters !!! needs fix
      {%
      \item%
      \hskip\ALG@parindent%!!! not yet implemented
%      \hskip-\algorithmicindent%
      }%
   %
   %
   %
   \renewcommand{\@toodeep}{}%
   \setcounter{ALG@line}{0}%
   \setcounter{ALG@rem}{0}%
   %
   \edef\ALG@currentitem{0}%
   \let\ALG@currentvalue\relax%
   %
   %
   %
   \begin{list}%
      {\ALG@step}%
      {%
      \rightmargin\z@%
      \itemsep\z@ \itemindent\z@ \listparindent2em%
      \partopsep\z@ \parskip\z@ \parsep\z@%
      \labelsep 0.5em \topsep 0.0em%\skip 1.2em 
      \ifthenelse{\equal{#1}{0}}%
         {\labelwidth 0.5em}%
         {\labelwidth 1.2em}%
      \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
      \ALG@tlm\labelsep%
      }%
   \ALG@beginalgorithmic%
   }%
   {% end{algorithmic}
%   \global\@newlistfalse% protects against empty algorithms, not needed
% check if all blocks are closed
   \ALG@closeloops%
   \ifnum0=\ALG@currentitem\relax%
   \else%
      \PackageError{algorithmicx}{Some blocks are not closed!!!}{Yes, you have some open blocks.}%
   \fi%
   \ALG@endalgorithmic%
   \end{list}%
   }%
%
%
%
%   ***   ALG@makeentitie   ***
%
%
%
\newcommand\ALG@makeentitie[1]% execute the entitie (#1)
   {%
   \expandafter\ifx\csname ALG@b@#1@0\endcsname\relax%
      \ALG@makenobegin{#1}% this entitie ends or continues blocks
   \else%
      \ALG@makebegin{#1}% this entitie can open blocks
   \fi%
   \csname algorithmic#1\endcsname%
   }%
%
\newcommand\ALG@makebegin[1]% executes an entitie (#1) that can open blocks
   {%
   \ifx\ALG@currentvalue\relax%
      \ALG@openblock{0}{#1}% in infinite block i can open my block
   \else%
      \ifnum0<\ALG@currentvalue\relax%
         \setcounter{ALG@tmpcounter}{\ALG@currentvalue}% the block has 'space' for another included block
         \addtocounter{ALG@tmpcounter}{-1}%
	 \edef\ALG@currentvalue{\arabic{ALG@tmpcounter}}%
         \ALG@openblock{0}{#1}%
      \else% the block needs to be closed
         \expandafter\ifx\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
	    \end{ALG@g}% i can not close this block, continue after it is closed by force
	    \ALG@makebegin{#1}%
	 \else%
            \ALG@changeblock{#1}% close this block
         \fi%
      \fi%
   \fi%
   }%
%
\newcommand\ALG@makenobegin[1]% executes an entitie (#1) that can not open blocks
   {%
   \ifx\ALG@currentvalue\relax%
      \ALG@changeblock{#1}% an infinite block must be broken
   \else%
      \expandafter\ifx\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
         \end{ALG@g}% the block must be ended by force,
	 \ALG@makenobegin{#1}% the command still runs
      \else%
         \ALG@changeblock{#1}% i can continue / end this block, let's do it
      \fi%
   \fi%
   }%
%
\newcommand\ALG@closeblock[1]% #1 - the entitie
   {%
   \end{ALG@g}%
   \expandafter\ifx\csname algorithmic#1\endcsname\relax\else%
      \item%
   \fi%
   }%
%
\newcommand\ALG@openblock[2]% #1 - the number of the closed block, #2 - the entitie
   {%
   \item[]%
   \expandafter\ifx\csname ALG@i@#2@#1\endcsname\relax%
      \begin{ALG@g}{\algorithmicindent}%
   \else
      \begin{ALG@g}{\csname ALG@i@#2@#1\endcsname}%
   \fi
   \edef\ALG@currentitem{\csname ALG@b@#2@#1\endcsname}%
   \expandafter\ifx\csname ALG@c@#2@#1\endcsname\relax%
      \let\ALG@currentvalue\relax%
   \else%
      \edef\ALG@currentvalue{\csname ALG@c@#2@#1\endcsname}%
   \fi%
   \expandafter\ifx\csname algorithmic#2\endcsname\relax\else%
      \item%
      \expandafter\ifx\csname ALG@i@#2@#1\endcsname\relax%
         \hskip-\algorithmicindent% default spacing
      \else
         \hskip-\csname ALG@i@#2@#1\endcsname%
      \fi
   \fi%
   }%
%
\newcommand\ALG@changeblock[1]% #1 - the entitie
   {%
   \expandafter\ifnum0=\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
      \ALG@closeblock{#1}%
   \else%
      \setcounter{ALG@tmpcounter}{\ALG@currentitem}%
      \end{ALG@g}%
      \ALG@openblock{\arabic{ALG@tmpcounter}}{#1}%
   \fi%
   }%
%
\newcommand\ALG@closeloops% closes all finite blocks
   {%
   \ifx\ALG@currentvalue\relax%
   \else% only if it is finite
      \end{ALG@g}% the block must be ended by force,
      \ALG@closeloops% the command still runs
   \fi%
   }%
%
%
%
%   ***   Low level block/entitie commands   ***
%
%
%
\def\ALG@bl@{0}% the BIG block
%
%  Create a block
%
\newcommand\ALG@createblock[1]% create the block #1, if it does not exists
   {%
   \expandafter\ifx\csname ALG@bl@#1\endcsname\relax% needs to be created?
      \addtocounter{ALG@blocknr}{1}% increment the block counter
      \expandafter\edef\csname ALG@bl@#1\endcsname{\arabic{ALG@blocknr}}% set the block number
   \fi%
   }%
%
%  Get the block number
%
\def\ALG@getblocknumber#1{\csname ALG@bl@#1\endcsname}%
%
%  Create an entitie
%
\newcommand\ALG@createentitie[1]% create the block #1, if it does not exists
   {%
   \expandafter\ifx\csname #1\endcsname\relax% needs to be created?
      \expandafter\def\csname #1\endcsname{\ALG@makeentitie{#1}}%
      \expandafter\ifx\csname algorithmic#1\endcsname\relax% \algorithmic#1 exists?
         \expandafter\def\csname algorithmic#1\endcsname{\textbf{#1}}%
      \fi%
   \fi%
   }%
%
%  End and Continue block
%
\newcommand\ALG@endandcontinueblock[5]% #1 = new block; #2 = old block; #3 = entitie; #4 = credits; #5 = indent
   {%
   \ALG@createentitie{#3}% create the entitie
   \ifthenelse{\equal{#2}{}}% no old block
      {}%
      {\ALG@createblock{#2}}% create the old block
   \ifthenelse{\equal{#1}{}}% whe need to open a new block?
      {\expandafter\edef\csname ALG@b@#3@\ALG@getblocknumber{#2}\endcsname{0}}% no, just close the old one
      {% yes,
      \ALG@createblock{#1}% create the block
      \expandafter\edef\csname ALG@b@#3@\ALG@getblocknumber{#2}\endcsname{\ALG@getblocknumber{#1}}% ending the old block opens a new one
      \ifthenelse{\equal{#4}{}}% infinite or finite credits?
         {\expandafter\let\csname ALG@c@#3@\ALG@getblocknumber{#2}\endcsname\relax}% infinite credits
	 {\expandafter\edef\csname ALG@c@#3@\ALG@getblocknumber{#2}\endcsname{#4}}% finite credits
      \ifthenelse{\equal{#5}{}}% default or specified indentation
         {\expandafter\let\csname ALG@i@#3@\ALG@getblocknumber{#2}\endcsname\relax}% default indentation
	 {\expandafter\edef\csname ALG@i@#3@\ALG@getblocknumber{#2}\endcsname{#5}}% indentation is specified
      }%
   }%
%
%
%   ***   User level block/entitie commands   ***
%
%
%
%   algblock
%
\newcommand\algblock[3][]% [block]{start}{stop}
   {%
   \ifthenelse{\equal{#1}{}}%
      {%
      \ifthenelse{\equal{#2}{}}%
         {\PackageError{algorithmicx}{Block or starting entitie must be specified!!!}{}}%
         {\algblock[#2]{#2}{#3}}%
      }%
      {%
      \ifthenelse{\equal{#2}{}}%
         {}%
         {\ALG@endandcontinueblock{#1}{}{#2}{}{}}%
      \ifthenelse{\equal{#3}{}}%
         {}%
         {\ALG@endandcontinueblock{}{#1}{#3}{}{}}%
      }%
   }%
%
%   algcblock
%
\newcommand\algcblock[4][]% [new block]{old block}{start}{stop}
   {%
   \ifthenelse{\equal{#1}{}}%
      {%
      \ifthenelse{\equal{#3}{}}%
         {\PackageError{algorithmicx}{New block or starting entitie must be specified!!!}{}}%
         {\algcblock[#3]{#2}{#3}{#4}}%
      }%
      {%
      \ifthenelse{\equal{#3}{}}%
         {}%
         {\ALG@endandcontinueblock{#1}{#2}{#3}{}{}}%
      \ifthenelse{\equal{#4}{}}%
         {}%
         {\ALG@endandcontinueblock{}{#1}{#4}{}{}}%
      }%
   }%
%
%   algloop
%
\newcommand\algloop[2][]% [loop]{start}
   {%
   \ifthenelse{\equal{#1}{}}%
      {\algloop[#2]{#2}}%
      {\ALG@endandcontinueblock{#1}{}{#2}{1}{}}%
   }%
%
%   algcloop
%
\newcommand\algcloop[3][]% [new loop]{old loop}{start}
   {%
   \ifthenelse{\equal{#1}{}}%
      {\algcloop[#3]{#2}{#3}}%
      {\ALG@endandcontinueblock{#1}{#2}{#3}{1}{}}%
   }%
%
%   algsetblock
%
\newcommand\algsetblock[5][]% [block]{start}{stop}{credits}{indent}
   {%
   \ifthenelse{\equal{#1}{}}%
      {%
      \ifthenelse{\equal{#2}{}}%
         {\PackageError{algorithmicx}{Block or starting entitie must be specified!!!}{}}%
         {\algsetblock[#2]{#2}{#3}{#4}{#5}}%
      }%
      {%
      \ifthenelse{\equal{#2}{}}%
         {}%
         {\ALG@endandcontinueblock{#1}{}{#2}{#4}{#5}}%
      \ifthenelse{\equal{#3}{}}%
         {}%
         {\ALG@endandcontinueblock{}{#1}{#3}{}{}}%
      }%
   }%
%
%   algsetcblock
%
\newcommand\algsetcblock[6][]% [new block]{old block}{start}{stop}{credits}{indent}
   {%
   \ifthenelse{\equal{#1}{}}%
      {%
      \ifthenelse{\equal{#3}{}}%
         {\PackageError{algorithmicx}{New block or starting entitie must be specified!!!}{}}%
         {\algsetcblock[#3]{#2}{#3}{#4}{#5}{#6}}%
      }%
      {%
      \ifthenelse{\equal{#3}{}}%
         {}%
         {\ALG@endandcontinueblock{#1}{#2}{#3}{#5}{#6}}%
      \ifthenelse{\equal{#4}{}}%
         {}%
         {\ALG@endandcontinueblock{}{#1}{#4}{}{}}%
      }%
   }%
%
%   algnotext
%
\newcommand\algnotext[1]%
   {%
   \expandafter\let\csname algorithmic#1\endcsname\relax%
   }%
%
%
%
%   ***   OTHERS   ***
%
%
%
\algsetblock[STATE]{State}{}{0}{}%
\algsetblock[STATE]{STATE}{}{0}{}%
\renewcommand\algorithmicState{}%
\renewcommand\algorithmicSTATE{}%
\newcommand\Statex{\item[]}% an empty line
\let\STATEx\Statex% an empty line
\newcommand\algorithmiccomment[1]{\hfill\(\triangleright\) #1}%
\def\Comment{\algorithmiccomment}%
\let\COMMENT\Comment%
%
%
%
%
%   ***   LANGUAGE SWITCHING   ***
%
%
%
%
%\def\ALG@language{nolanguage}%
%\newcommand{\ALG@setlanguage@nolanguage}{}%
%\newcommand{\ALG@unsetlanguage@nolanguage}{}%
\newcommand{\algsetlanguage}[1]%
   {%
%   \csname ALG@unsetlanguage@\ALG@language\endcsname%
%   \edef\ALG@language{#1}%
   \csname ALG@setlanguage@#1\endcsname%
   }%
%
