%
%  $Id: dcsalg.sty 3115 2012-11-22 09:10:07Z sobaniec $
%
%  Algorithm environment of DCS group
%  by Cezary Sobaniec <cezary.sobaniec@cs.put.poznan.pl>
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{dcsalg}[2010/01/14 v1.0 DCS Group algorithms package]

\RequirePackage{algpseudocode}
\RequirePackage{keyval}
\RequirePackage{ifthen}
\RequirePackage{caption}[2009/10/09]
\RequirePackage{translator}
\RequirePackage{xcolor}
\RequirePackage{float}

\usedictionary{translator-float-dictionary}

\definecolor{commentcolor}{rgb}{0.3,0.3,0.3}

\renewcommand{\algorithmiccomment}[1]{\hspace{1em}\textcolor{commentcolor}{::~\textit{#1}}}
\renewcommand{\algorithmicforall}{\textbf{foreach}}

% procedures
\algrenewcommand{\textproc}{\texttt}

% additional commands
\newcommand{\AND}{\ \textbf{and}\ }
\newcommand{\OR}{\ \textbf{or}\ }
\newcommand{\NOT}{\textbf{not}\ }
\newcommand{\algorithmicprocname}{\textbf{Procedure:}}
\newcommand{\ProcName}{\item[\algorithmicprocname]}
\newcommand{\algorithmicparameters}{\textbf{Parameters:}}
\newcommand{\Parameters}{\item[\algorithmicparameters]}
\newcommand{\Statey}[1]{\item[#1]}
\newcommand{\Stateybf}[1]{\item[\textbf{#1}]}
\newcommand{\Stateysf}[1]{\item[\textsf{#1}]}
\newcommand{\Stateytt}[1]{\item[\texttt{#1}]}
\newcommand{\Send}{\textbf{send}\ }
\newcommand{\Broadcast}{\textbf{broadcast}\ }
\newcommand{\Await}{\textbf{await}\ }
\newcommand{\Notify}{\textbf{notify}\ }
\newcommand{\NotifyAll}{\textbf{notify all}\ }
\newcommand{\Var}[1]{\ensuremath{\mathit{#1}}}
\newcommand{\Val}[1]{\ensuremath{\mathrm{#1}}}
\newcommand{\True}{\Val{true}}
\newcommand{\TRUE}{\True}
\newcommand{\False}{\Val{false}}
\newcommand{\FALSE}{\False}

\newcommand{\MSG}[1]{\ensuremath{\left\langle #1 \right\rangle}}
\newcommand{\Msg}[2]{\ensuremath{\mathsf{#1}%
                     \ifthenelse{\equal{#2}{}}{}{\left\langle#2\right\rangle}}}
\newcommand{\MSGN}[2]{\Msg{#1}{#2}}

%
% dcsalg environment
%

% width parameter 
\newlength{\@dcsalgwidth}
\setlength{\@dcsalgwidth}{\textwidth}
\addtolength{\@dcsalgwidth}{-1.18em}
\define@key{dcsalg}{width}{\setlength{\@dcsalgwidth}{#1}}

% font parameter
\newcommand{\@dcsalgfont}{\small}
\define@key{dcsalg}{font}{\renewcommand{\@dcsalgfont}{#1}}

% number parameter
\newboolean{@dcsalgnumber}
\setboolean{@dcsalgnumber}{true}
\define@key{dcsalg}{number}[]{\setboolean{@dcsalgnumber}{true}}
\define@key{dcsalg}{nonumber}[]{\setboolean{@dcsalgnumber}{false}}

% plain|boxed|ruled parameters
\newboolean{@dcsalgruled}
\setboolean{@dcsalgruled}{false}
\newboolean{@dcsalgboxed}
\setboolean{@dcsalgboxed}{true}
\define@key{dcsalg}{plain}[]{\setboolean{@dcsalgruled}{false}
                             \setboolean{@dcsalgboxed}{false}}
\define@key{dcsalg}{ruled}[]{\setboolean{@dcsalgruled}{true}
                             \setboolean{@dcsalgboxed}{true}}
\define@key{dcsalg}{boxed}[]{\setboolean{@dcsalgruled}{false}
                             \setboolean{@dcsalgboxed}{true}}
 
\newenvironment{dcsalg}[1][]%
  {\setkeys{dcsalg}{#1}%
   \if@dcsalgboxed
     \if@dcsalgruled
       \begin{tabular}{c}%
     \else
       \begin{tabular}{|c|}%
     \fi
     \hline%
   \fi
   \begin{minipage}{\@dcsalgwidth}\smallskip%
   \if@dcsalgnumber
     \begin{algorithmic}[1]%
   \else
     \begin{algorithmic}%
   \fi
   \@dcsalgfont}%
  {\end{algorithmic}%
   \smallskip%
   \end{minipage}%
   \if@dcsalgboxed
     \tabularnewline%
     \hline%
     \end{tabular}
   \fi}

\newcommand{\dcsalgset}[1]{\setkeys{dcsalg}{#1}}

% algorithm float using "float" package
\ifthenelse{\isundefined{\chaptermark}}
  {\newfloat{algorithm}{tbhp}{loa}}
  {\newfloat{algorithm}{tbhp}{loa}[chapter]}
\floatname{algorithm}{\translate{Algorithm}}
%% algorithm float using newfloat package
%\DeclareFloatingEnvironment[fileext=loa,placement=tbhp,within=none,name=Alg.]{algorithm}
%\ifthenelse{\isundefined{\chaptermark}}
%  {}
%  {\SetupFloatingEnvironment{algorithm}{within=chapter}}
\captionsetup[algorithm]{position=bottom,skip=\bigskipamount}


\ProcessOptions\relax
