%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % clrscode4e.sty % See the document "Using the clrscode4e Package in LaTeX 2e" for % examples. % Package for producing pseudocode in the style of Cormen, Leiserson, % Rivest, and Stein, Introduction to Algorithms, Fourth edition. % LIMITATION: This package works only if each procedure has at most 99 % numbered lines of code. % Each pseudocode procedure is typeset within a codebox environment, % \begin{codebox}...\end{codebox}. % If the 'screen' option is specified, there is a colored screen behind % each codebox. If 'noscreen' is specified, there is no screen. The % default is 'noscreen'. % Normally, the first line within the codebox environment is a \Procname % command. The argument of the \Procname command is a math-mode % expression consisting of the procedure name and its parameters. The % name of the procedure itself goes within a \proc command. Example: % \Procname{$\proc{Matrix-Multiply}(A,B)$} % The \Procname command is optional. % To put more than code procedure within a single box, use the % outercodebox and innercodebox environments. outercodebox produces % the screen, if desired, and keeps the innercodeboxes in the same % box. Each procedure should be its own innercodebox. % To typeset the name of a procedure (e.g., Matrix-Multiply) in small % caps, use the \proc command: % \proc{Matrix-Multiply} % To typeset the name of a constant (e.g., red) in small caps, use the % \const command: % \const{red} % There is one built-in constant: \nil. % To typeset the name of an identifier (e.g., rank) in regular italics, % use the \id command: % \id{rank} % To typeset the name of a fixed function (e.g., sin) in roman, use the % \func command: % \func{sin} % (Note that several fixed functions, like sin, are already built into % LaTeX.) % The \proc, \const, \id, and \func commands not only use the correct % font, they also perform the important service of interpreting a dash % as a hyphen, rather than as a minus sign. These commands may be used % either in or out of math mode. % For attributes, use the various forms of the \attrib commands. % Other than the \Procname line, all lines begin with either \li (for a % numbered line) or \zi (for an unnumbered line). The following % commands are provided for typesetting keywords and handling automatic % indentation: % Loops: \For, \To, \By, \Downto, \Do, \While, \Repeat, \Until % Selection: \If, \Then, \Else, \ElseIf, \ElseNoIf % Jumps: \Return, \Error, \Goto % Multithreading: \Spawn, \Sync, \Parfor % Comments: \Comment, \CommentAt, \RComment, \CommentSymbol, \CommentNoSymbol, \CommentNoSymbolAt % Indentation: \Indentmore, \Startalign, \Stopalign % \label commands appearing in or after the first numbered line in a % codebox resolve to the number of the most recent numbered line. % \subarr produces the ":" notation used for subarrays. % Written for general distribution by Thomas H. Cormen, February 2022. % The author grants permission for anyone to use this macro package and % to distribute it unchanged without further restriction. If you choose % to modify this package, you must indicate that you have modified it % prior to your distributing it. I don't want to get bug reports about % changes that *you* have made! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ProvidesPackage{clrscode4e} \RequirePackage{graphics} % needed for \scalebox command \RequirePackage[cmyk]{xcolor} \newif\ifscreen \screenfalse \DeclareOption{screen}{\screentrue} \DeclareOption{noscreen}{\screenfalse} \newif\iffloatingcodebox \floatingcodeboxfalse \DeclareOption{floatingcodebox}{\floatingcodeboxtrue} \DeclareOption{nofloatingcodebox}{\floatingcodeboxfalse} \ProcessOptions \ifscreen \RequirePackage[cmyk]{xcolor} %\definecolor{codebackground}{rgb}{1.0,1.0,0.5} \definecolor{tan}{cmyk}{0,0.1,0.33,0} \definecolor{lighttan}{cmyk}{0,0.05,0.17,0} \colorlet{codebackground}{lighttan} \fi % Commands for typesetting constants, procedure names, identifiers, and % fixed functions. \newcommand{\const}[1]{\textnormal{\scshape#1}} \newcommand{\proc}[1]{\textnormal{\scshape#1}} \newcommand{\text@hyphens}{\mathcode`\-=`\-\relax} \newcommand{\func}[1]{% \ensuremath{\mathop{\text@hyphens\operator@font#1}\nolimits}} \newcommand{\id}[1]{% \ensuremath{\mathit{\text@hyphens#1}}} % Commands for typesetting object attributes. All of these commands % may be used either in or out of math mode. % Definitions: % An i-string is a string that you would use in an \id command, % typically one or more non-Greek letters, numerals, or dashes. % An x-string is a string that you would not use in an \id command, % typically because it has a subscript or one or more Greek letters. % A single non-Greek letter can be either an i-string or an x-string. % Most of the time, we use the \attrib command, which assumes that its % first argument, the object name, is an x-string and its second % argument, the attribute name, is an i-string. Examples of the % \attrib command: \attrib{A}{length}, \attrib{x}{next}, % \attrib{\rho}{next-item}. \attrib is just a direct call to the % \attribxi macro. % The four macros \attribxi, \attribxx, \attribix, and \attribii each % take two arguments. The two letters at the end of the macro name % determine, in order, how each argument is treated. x indicates that % the argument should be an x-string, and i indicates that the % argument should be an i-string. % Because we use a single letter to indicate most objects and a string % of one or more letters to indicate most attribute names, \attribxi % is the most common macro we use. That's why \attrib is just a % direct call to \attribxi. % Use \attribxx when the object name is an x-string and the object % name is also an x-string, for example, when the attribute name has a % subscript: \attribxx{x}{c_i} or \attrib{x}{\id{key}_i}. Another % time you would use \attribxx is when the attribute name is a Greek % letter: \attrib{v}{\pi}. % When the object name has more than one letter, it is usually an % i-string. In this case, use \attribii or \attribix, depending on % the attribute name: \attribii{item}{key}, \attribii{prev-item}{np}, % \attribix{item}{\pi}. % The \attribb, \attribbb, \attribbbb, and \attribbxxi macros are for % cascading attributes. They just call the appropriate \attribxi and % \attribxx commands. For one level of cascading, use \attribb: % \attribb{x}{left}{size}. For two levels, use \attribbb: % \attribbb{y}{p}{left}{size}. For three levels, use \attribbbb (but % no examples in the book use this macro). The \attribbxxi macro is % for one level of cascading where the first attribute given is an % x-string: \attribbxxi{x}{c_i}{n}. % When the object is an edge of a graph, specified by two vertices, % use \attribe or \attribex: \attribe{u}{v}{f}, \attribex{u}{v}{c'}. \newcommand{\attribxi}[2]{\ensuremath{#1.\hspace*{1pt}\id{#2}}} \newcommand{\attribxx}[2]{\ensuremath{#1.\hspace*{1pt}#2}} \newcommand{\attribix}[2]{\ensuremath{\id{#1}\hspace*{1pt}.#2}} \newcommand{\attribii}[2]{\ensuremath{\id{#1}\hspace*{1pt}.\hspace*{1pt}\id{#2}}} \newcommand{\attrib}[2]{\attribxi{#1}{#2}} \newcommand{\attribe}[3]{\attribxi{(#1,#2)}{#3}} \newcommand{\attribex}[3]{\attribxx{(#1,#2)}{#3}} \newcommand{\attribb}[3]{\attribxi{\attribxi{#1}{#2}}{#3}} \newcommand{\attribbb}[4]{\attribxi{\attribb{#1}{#2}{#3}}{#4}} \newcommand{\attribbbb}[5]{\attribxi{\attribbb{#1}{#2}{#3}{#4}}{#5}} \newcommand{\attribbxxi}[3]{\attribxi{\attribxx{#1}{#2}}{#3}} \newcommand{\attribbxxx}[3]{\attribxx{\attribxx{#1}{#2}}{#3}} % Command for typesetting subarray ranges. \newcommand{\twodots}{\typeout{Warning: twodots command used on page \thepage}\mathinner{\ldotp\ldotp}} \newcommand{\subarrsymbol}{:} \newcommand{\subarr}{\nolinebreak\mathinner{\subarrsymbol}\nolinebreak} % The special pointer for no object. \newcommand{\nil}{\const{nil}} % The codelinenumber counter counts the current line number. \newcounter{codelinenumber} % The indent counter keeps track of the current indentation level. \newcounter{indent} % The \iffirstcodeline command tells us whether we are about to % produce the first line other than the procedure declaration. \newif\iffirstcodeline\firstcodelinetrue % The \zeroli command makes it so that we're about to produce the % first line other than the procedure declaration. \newcommand{\zeroli}{\setcounter{codelinenumber}{0}% \setcounter{indent}{0}% \firstcodelinetrue} % \digitwidth gives the width of a single digit. All digits are the % same width. We'll need this amount to do the right thing for line % numbers. \newcommand{\linenumsize}{\small} \newlength{\digitwidth} \settowidth{\digitwidth}{\linenumsize{}0} % The \li command bumps the counter, outputs it, and skips some space % A \label cmd for a given numbered line is allowed to appear after the % \\, as in % \li $x\gets y$ \label{li:assign-x} % But if \li merely set \@currentlabel in the usual way via % \refstepcounter, the value of \@currentlabel does not persist outside % the current cell. Solution: use an additional, global variable % \@lilabel. % THC: This next command is magic to me. I didn't write it. \def\@startline{\global\@curtabmar\@nxttabmar\relax \global\@curtab\@curtabmar\setbox\@curline\hbox {}\@startfield\strut} % \code@init is run at the beginning of a codebox environment. \def\code@init{% \zeroli% producing the first line \setlength{\tabbingsep}{1em}% distance between numbers and code % Initialize \@lilabel to allow a pageref \label cmd at the beginning % of the codebox \global\let\@lilabel\@currentlabel \def\@currentlabel{\@lilabel}% } % When we make a codebox, we save the code part into a box before % printing it. We do not actually print the code until we know how many % line numbers there are. \newsavebox{\savecode} \ifscreen\newsavebox{\screenbox}% \newlength{\screenboxwidth}\setlength{\screenboxwidth}{\linewidth}% \addtolength{\screenboxwidth}{-7pt}% \fi % The \ifprocname command tells us whether this procedure has been % given a name yet. \newif\ifprocname\procnamefalse % Assume that the width of the codebox is the width of the text, minus % the width of 2 digits. We'll correct for that later. \newlength{\codeboxwidth} \setlength{\codeboxwidth}{\linewidth} % Thanks, David Etherington! \addtolength{\codeboxwidth}{-3\digitwidth} % The outercodebox environment takes care of shading and keeping one % or more innercodeboxes together. \newenvironment{outercodebox}{% \ifscreen\noindent\begin{lrbox}{\screenbox}% \begin{minipage}[t]{\screenboxwidth}% \vspace*{-1.6ex}\fi}{% \ifscreen\vspace*{-2.0ex}\fi% \ifscreen\end{minipage}\end{lrbox}% \colorbox{codebackground}{\usebox{\screenbox}}\iffloatingcodebox\else\vspace{2ex}\fi\fi} \newif\ifreducecodebox \reducecodeboxfalse \def\codeboxscalefactor{1.0} \newcommand{\codeboxreduction}[1]{\def\codeboxscalefactor{#1}} % The "innercodebox" environment produces an unbreakable section of code \newenvironment{innercodebox}{% % \ifscreen % \noindent % \begin{lrbox}{\screenbox} % \begin{minipage}[t]{\screenboxwidth}\fi \global\procnamefalse% this proc hasn't been given a name yet \code@init% set up for first line \begin{lrbox}{\savecode}% save the code into a box \begin{minipage}[t]{\codeboxwidth}% it'll be a minipage % Set up the tab stops \def\codeindent{\textbf{else} }% \begin{tabbing}% 99\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\+\kill% }{% % Here's what's run at the end of a codebox environment. Start by % making sure that we have ended at indent level 0. Otherwise, print a % warning. \ifnum\value{indent}=0\else\typeout{Warning: Indentation ends at level \theindent\space in codebox on page \thepage.}\fi% \end{tabbing}\end{minipage}\end{lrbox}% \addtolength{\topsep}{0.5ex}% for the following trivlist \begin{trivlist}\item\parindent=0pt% % If there was a procedure name given, print it now but with a little % space below, and disallow a page break after the procedure name. \@nobreaktrue% % \ifscreen\vspace*{-2.0ex}\fi% \ifprocname% \ifreducecodebox\scalebox{\codeboxscalefactor}{\saveprocname\rule[-1.25ex]{0pt}{0pt}}\else\saveprocname\rule[-1.25ex]{0pt}{0pt}\fi\\ \fi% % Put in the right amount of space, depending on whether we reached % double digits in the line numbers. \ifnum\value{codelinenumber}>9\hspace*{2\digitwidth}\else\hspace*{1\digitwidth}\fi% % Now print the code \ifreducecodebox\scalebox{\codeboxscalefactor}{\usebox{\savecode}\codeboxreduction{1.0}}\else\usebox{\savecode}\fi\end{trivlist}% \addtolength{\topsep}{-0.5ex}\global\procnamefalse% % \ifscreen\end{minipage}\end{lrbox}% % \colorbox{codebackground}{\usebox{\screenbox}}\vspace{2ex}\fi } \newcommand{\indentlinenumbers}{\setcounter{codelinenumber}{10}} %% For a tab stop within a \Comment \newlength{\codeindentwidth} \settowidth{\codeindentwidth}{\textbf{else} } \newcommand{\tabincomment}{\hspace{\codeindentwidth}} % Normally, just use the codebox enviroment, for when there's one % procedure to be put within a box. \newenvironment{codebox}{\iffloatingcodebox\begin{floatingcodebox}\fi\begin{outercodebox}\begin{innercodebox}}{% \end{innercodebox}\end{outercodebox}\iffloatingcodebox\end{floatingcodebox}\fi} % For a codebox that you want to go at the top of a page. \newenvironment{codeboxt}{\iffloatingcodebox\begin{floatingcodeboxt}\fi\begin{outercodebox}\begin{innercodebox}}{% \end{innercodebox}\end{outercodebox}\iffloatingcodebox\end{floatingcodeboxt}\fi} % For a codebox that you want to go at the bottom of a page. \newenvironment{codeboxb}{\iffloatingcodebox\begin{floatingcodeboxb}\fi\begin{outercodebox}\begin{innercodebox}}{% \end{innercodebox}\end{outercodebox}\iffloatingcodebox\end{floatingcodeboxb}\fi} % For a codebox that you want to go HERE. \newenvironment{codeboxh}{\iffloatingcodebox\begin{floatingcodeboxh}\fi\begin{outercodebox}\begin{innercodebox}}{% \end{innercodebox}\end{outercodebox}\iffloatingcodebox\end{floatingcodeboxh}\bigskip\fi} % For a codebox that you want to go on its own page. \newenvironment{codeboxp}{\iffloatingcodebox\begin{floatingcodeboxp}\fi\begin{outercodebox}\begin{innercodebox}}{% \end{innercodebox}\end{outercodebox}\iffloatingcodebox\end{floatingcodeboxp}\fi} % Use the \Procname macro to give the name of the procedure. \newcommand{\Procname}[1]{\global\def\saveprocname{#1}\global\procnametrue} \newcounter{thisindent} % counter for recursive indenting code \newcommand{\Indent}{\setcounter{thisindent}{\value{indent}}\puttabs} % \puttabs is a recursive macro that indents a number of times given % by the counter thisindent. \newcommand{\puttabs}{\ifnum\value{thisindent}>0\>\addtocounter{thisindent}{-1}\puttabs\fi} \newcommand{\tab}[1]{\setcounter{thisindent}{#1}\puttabs\ignorespaces} \newcommand{\tabto}[1]{\setcounter{thisindent}{#1}\addtocounter{thisindent}{-\value{indent}}\puttabs\ignorespaces} % For typesetting any keyword in the main text. \newcommand{\kw}[1]{\textbf{#1}} % Override the 'gets' symbol. \def\gets{\mathrel{\hspace{1pt}=\hspace{1pt}}} \newcommand{\isequal}{\mathrel{\scalebox{0.8}[1]{=}\hspace*{1pt}\scalebox{0.8}[1]{=}}} % All of our favorite keywords. \newcommand{\For}{\textbf{for} } \newcommand{\To}{\ifmmode\ \textrm{\textbf{to}}\ \else\textbf{to}\ \fi} \newcommand{\By}{\ifmmode\ \textrm{\textbf{by}}\ \else\textbf{by}\ \fi} \newcommand{\Downto}{\ifmmode\ \textrm{\textbf{downto}}\ \else\textbf{downto}\ \fi} \newcommand{\While}{\textbf{while} } \newcommand{\Repeat}{\textbf{repeat}\>\addtocounter{indent}{1}} \newcommand{\Until}{\kill\addtocounter{indent}{-1}\liprint\textbf{until} } \newcommand{\If}{\textbf{if} } \newcommand{\Then}{\>\addtocounter{indent}{1}} \newcommand{\Else}{\kill\addtocounter{indent}{-1}\liprint\textbf{else}\>\addtocounter{indent}{1}} \newcommand{\End}{\addtocounter{indent}{-1}} \newcommand{\ElseIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{elseif} } \newcommand{\ElseNoIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{else} \addtocounter{indent}{1}} \newcommand{\Do}{\>\addtocounter{indent}{1}} \newcommand{\Return}{\textbf{return} } \newcommand{\CommentSymbol}{\textcolor{commentcolor}{\texttt{\textbf{/\hspace*{-0.3em}/}}}} \newcommand{\Comment}[1]{\textcolor{commentcolor}{\CommentSymbol\ #1}} \newcommand{\CommentAt}[2]{\tabto{#1}\textcolor{commentcolor}{\CommentSymbol\ #2}} \newcommand{\CommentNoSymbol}[1]{\textcolor{commentcolor}{#1}} \newcommand{\CommentNoSymbolAt}[2]{\tabto{#1}\textcolor{commentcolor}{#2}} \newcommand{\RComment}[1]{\textcolor{commentcolor}{\`\CommentSymbol\ }} \newcommand{\Goto}{\textbf{goto} } \newcommand{\Error}{\textbf{error} } % optionally followed by string argument \newcommand{\EndTest}{\textbf{:}} \newcommand{\Spawn}{\ifmmode\textbf{spawn}\ \else\textbf{spawn} \fi} \newcommand{\Sync}{\textbf{sync}} \newcommand{\Parfor}{\textbf{parallel for} } %\newcommand{\New}{\textbf{new} } \definecolor{commentcolor}{cmyk}{0.2, 1, 1, 0.3} % dark red \definecolor{linenumcolor}{cmyk}{0, 0, 0, 1} % black % Indent the next line one level more \newcommand{\Indentmore}{\addtocounter{indent}{1}} \newif\ifnumberedline \numberedlinetrue % The \li command starts a new numbered line. \newcommand{\li}{\global\numberedlinetrue% \iffirstcodeline\global\firstcodelinefalse\else\\ \fi \stepcounter{codelinenumber}% \liprint} % The \lispace command starts a new numbered line with a little extra % space above, given by the argument. \newcommand{\lispace}[1]{\iffirstcodeline\global\firstcodelinefalse\else\\[#1] \fi \stepcounter{codelinenumber}% \liprint} % \liprint actually prints the line number and sets up the indentation. \newcommand{\liprint}{\protected@xdef\@lilabel{\thecodelinenumber}% \ifnumberedline\textcolor{linenumcolor}{\linenumsize\thecodelinenumber}\fi\'\Indent% } \providecommand{\numref}[1]{% \@ifundefined{r@#1}{000}{% \expandafter\expandafter\expandafter\@firstoftwo \csname r@#1\endcsname }% } % \setlinenumber sets the line number to its argument \newcommand{\setlinenumber}[1]{\setcounter{codelinenumber}{\numref{#1}}% \addtocounter{codelinenumber}{-1}} % \setlinenumberplus sets the line number to its first argument plus its % second argument. \newcommand{\setlinenumberplus}[2]{\setcounter{codelinenumber}{\numref{#1}}% \addtocounter{codelinenumber}{-1}\addtocounter{codelinenumber}{#2}} % The \zi command starts a new unnumbered line. \newcommand{\zi}{\global\numberedlinefalse% \iffirstcodeline\global\firstcodelinefalse\else\\ \fi \liprint} % Temporarily make all lines indented so that they start at the end of % a given text. \newcommand{\Startalign}[1]{\\ \pushtabs\FakeIndent#1\=\kill} \newcommand{\Stopalign}{\poptabs} \newcommand{\FakeIndent}{\setcounter{thisindent}{\value{indent}}\putfakeindents} \newcommand{\putfakeindents}{\ifnum\value{thisindent}>0\textbf{else }\addtocounter{thisindent}{-1}\putfakeindents\fi} \RequirePackage{float} \newfloat{floatingcodebox}{htbp}{codebox} \newfloat{floatingcodeboxb}{b}{codeboxb} \newfloat{floatingcodeboxt}{t}{codeboxt} \newfloat{floatingcodeboxp}{p}{codeboxp} \newif\iffloatingcodeboxh \floatingcodeboxhfalse \newenvironment{floatingcodeboxh}{\floatingcodeboxhtrue}{\floatingcodeboxhfalse} \newif\ifsavescreen \newenvironment{codeboxnoscreenh}{\ifscreen\savescreentrue\else\savescreenfalse\fi\screenfalse\vspace{-0.5ex}\begin{outercodebox}\begin{innercodebox}}{\end{innercodebox}\end{outercodebox}\vspace{-1ex}\ifsavescreen\screentrue\else\screenfalse\fi} \endinput