% Cross-references for handout numbers.

\newlength{\toppush}
\setlength{\toppush}{2\headheight}
\addtolength{\toppush}{\headsep}

\newcounter{problemsetnum}
\newcommand{\solutions}[1]{\setcounter{problemsetnum}{#1}}

\newcommand{\stationery}[1]{\noindent\vspace*{-\toppush}\newline\parbox{6.5in}
{Yoav Yerushalmi \hfill Massachusetts Institute of Technology \newline
Zulfikar Ramzan \hfill 6.823: Computer System Architecture \newline 
Raj Iyer\hfill Problem Set \arabic{problemsetnum} Solutions\vspace*{-.5ex}\newline
\mbox{}\hrulefill\mbox{}}\vspace*{1ex}\mbox{}\newline
\begin{center}{\Large\bf Question \theproblemnum: #1}\end{center}}
% above command produces ``class stationery'' that is a header as in the
% handout case but without handout number 

\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}
\setlength{\textwidth}{6.5in}
\setlength{\topmargin}{0in}
\setlength{\textheight}{8.5in}

\newcommand{\For}{{\bf for} }
\newcommand{\To}{{\bf to} }
\newcommand{\By}{{\bf by} }
\newcommand{\Downto}{{\bf downto} }
\newcommand{\Case}{{\bf case} }
\newcommand{\While}{{\bf while} }
\newcommand{\Repeat}{\>\>{\bf repeat}\'}
\newcommand{\Until}{\>\>{\bf until}\'}
\newcommand{\If}{{\bf if} }
\newcommand{\Then}{\>\>{\bf then}\>\>}
\newcommand{\Else}{\>{\bf else}\>}
\newcommand{\ElseIf}{{\bf elseif} }
\newcommand{\ElseNoIf}{{\bf else} }
\newcommand{\Do}{\>\>{\bf do}\'}
\newcommand{\Return}{{\bf return} }
\newcommand{\Comment}{$\!\rhd$ }
\newcommand{\Inparallel}{{\bf in parallel} }	%This should be flushed
\newcommand{\Goto}{{\bf goto} }
\newcommand{\Error}{{\bf error} } % optionally followed by string argument

\renewcommand{\cases}[1]{\left\{ \begin{array}{ll}#1\end{array}\right.}
\newcommand{\cif}[1]{\mbox{if $#1$}}
\newcommand{\cwhen}[1]{\mbox{when $#1$}}

\newcounter{problemnum}
\newcommand{\theproblem}{Problem Set \arabic{problemsetnum} Solutions : Question \arabic{problemnum}}
\newenvironment{problems}{
	\begin{list}{}
	{\setlength{\leftmargin}{0em}
	 \setlength{\rightmargin}{0em}
	 \setlength{\labelwidth}{0em}
	 \setlength{\labelsep}{0em}
	 \usecounter{problemnum}}}{\end{list}}
\makeatletter
\newcommand{\problem}[1]{\cleardoublepage \item \stationery{#1}}
%\def\problem{\stepcounter{problemnum}\let\@currentlabel=\theproblem%
%	\cleardoublepage%
%	\@ifstar{\item[\llap{$\star$\quad}\bf\theproblem.\hspace*{0.5em}]}%
%		{\item[\bf\theproblem.\hspace*{0.5em}]}%
%	\stationery{\theproblem} %
%	}

\makeatother

\newcounter{problempartnum}[problemnum]
\newcommand{\problempartlabel}[1]{\textsf{{\bf \arabic{problemnum}.\Alph{problempartnum}} \hfill {#1}}}
\newenvironment{problemparts}{
	\begin{list}{}%
	{\renewcommand{\makelabel}{\problempartlabel}%
	 \setlength{\leftmargin}{2.5em}
	 \setlength{\rightmargin}{2.5em}
	 \setlength{\labelsep}{0.5em}}}{\end{list}}
\newcommand{\problempart}[1]{\addtocounter{problempartnum}{1}\item[#1]\mbox{}\\}

\newcounter{exercisenum}
\newcommand{\theexercise}{Exercise \arabic{problemsetnum}-\arabic{exercisenum}}
\newenvironment{exercises}{
	\begin{list}{{\bf \theexercise. \hspace*{0.5em}}}
	{\setlength{\leftmargin}{0em}
	 \setlength{\rightmargin}{0em}
	 \setlength{\labelwidth}{0em}
	 \setlength{\labelsep}{0em}
	\usecounter{exercisenum}}}{\end{list}}
\makeatletter
\newcommand{\exercise}{\item \let\@currentlabel=\theexercise}
\makeatother

\newcounter{exercisepartnum}[exercisenum]
%\newcommand{\problem}[1]{\medskip\mbox{}\newline\noindent{\bf Problem #1.}\hspace*{1em}}
%\newcommand{\exercise}[1]{\medskip\mbox{}\newline\noindent{\bf Exercise #1.}\hspace*{1em}}

\newenvironment{exerciseparts}{
	\begin{list}{{\bf (\alph{exercisepartnum})}}
	{\setlength{\leftmargin}{2.5em}
	 \setlength{\rightmargin}{2.5em}
	 \setlength{\labelsep}{0.5em}}}{\end{list}}
\newcommand{\exercisepart}{\addtocounter{exercisepartnum}{1}\item}


% Macros to make captions print with small type and 'Figure xx' in bold.
\makeatletter
\def\fnum@figure{{\bf Figure \thefigure}}
\def\fnum@table{{\bf Table \thetable}}
\let\@mycaption\caption
%\long\def\@mycaption#1[#2]#3{\addcontentsline{\csname
%  ext@#1\endcsname}{#1}{\protect\numberline{\csname 
%  the#1\endcsname}{\ignorespaces #2}}\par
%  \begingroup
%    \@parboxrestore
%    \small
%    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
%  \endgroup}
%\def\mycaption{\refstepcounter\@captype \@dblarg{\@mycaption\@captype}}
%\makeatother
\let\mycaption\caption
%\newcommand{\figcaption}[1]{\mycaption[]{#1}}

\newcounter{totalcaptions}
\newcounter{totalart}

\newcommand{\figcaption}[1]{\addtocounter{totalcaptions}{1}\caption[]{#1}}

% \psfigures determines what to do for figures:
%	0 means just leave vertical space
%	1 means put a vertical rule and the figure name
%	2 means insert the PostScript version of the figure
%	3 means put the figure name flush left or right
\newcommand{\psfigures}{0}
\newcommand{\spacefigures}{\renewcommand{\psfigures}{0}}
\newcommand{\rulefigures}{\renewcommand{\psfigures}{1}}
\newcommand{\macfigures}{\renewcommand{\psfigures}{2}}
\newcommand{\namefigures}{\renewcommand{\psfigures}{3}}

\newcommand{\figpart}[1]{{\bf (#1)}\nolinebreak[2]\relax}
\newcommand{\figparts}[2]{{\bf (#1)--(#2)}\nolinebreak[2]\relax}


\macfigures	% STATE

% When calling \figspace, make sure to leave a blank line afterward!!
% \widefigspace is for figures that are more than 28pc wide.
\newlength{\halffigspace} \newlength{\wholefigspace}
\newlength{\figruleheight} \newlength{\figgap}
\newcommand{\setfiglengths}{\ifnum\psfigures=1\setlength{\figruleheight}{\hruleheight}\setlength{\figgap}{1em}\else\setlength{\figruleheight}{0pt}\setlength{\figgap}{0em}\fi}
\newcommand{\figspace}[2]{\ifnum\psfigures=0\leavefigspace{#1}\else%
\setfiglengths%
\setlength{\wholefigspace}{#1}\setlength{\halffigspace}{.5\wholefigspace}%
\rule[-\halffigspace]{\figruleheight}{\wholefigspace}\hspace{\figgap}#2\fi}
\newlength{\widefigspacewidth}
% Make \widefigspace put the figure flush right on the text page.
\newcommand{\widefigspace}[2]{
\ifnum\psfigures=0\leavefigspace{#1}\else%
\setfiglengths%
\setlength{\widefigspacewidth}{28pc}%
\addtolength{\widefigspacewidth}{-\figruleheight}%
\setlength{\wholefigspace}{#1}\setlength{\halffigspace}{.5\wholefigspace}%
\makebox[\widefigspacewidth][r]{#2\hspace{\figgap}}\rule[-\halffigspace]{\figruleheight}{\wholefigspace}\fi}
\newcommand{\leavefigspace}[1]{\setlength{\wholefigspace}{#1}\setlength{\halffigspace}{.5\wholefigspace}\rule[-\halffigspace]{0em}{\wholefigspace}}

% Commands for including figures with macpsfig.
% To use these commands, documentstyle ``macpsfig'' must be specified.
\newlength{\macfigfill}
\makeatother
\newlength{\bbx}
\newlength{\bby}
\newcommand{\macfigure}[5]{\addtocounter{totalart}{1}
\ifnum\psfigures=2%
\setlength{\bbx}{#2}\addtolength{\bbx}{#4}%
\setlength{\bby}{#3}\addtolength{\bby}{#5}%
\begin{flushleft}
\ifdim#4>28pc\setlength{\macfigfill}{#4}\addtolength{\macfigfill}{-28pc}\hspace*{-\macfigfill}\fi%
\mbox{\psfig{figure=./#1.ps,%
bbllx=#2,bblly=#3,bburx=\bbx,bbury=\bby}}
\end{flushleft}%
\else\ifdim#4>28pc\widefigspace{#5}{#1}\else\figspace{#5}{#1}\fi\fi}
\makeatletter

\newlength{\savearraycolsep}
\newcommand{\narrowarray}[1]{\setlength{\savearraycolsep}{\arraycolsep}\setlength{\arraycolsep}{#1\arraycolsep}}
\newcommand{\normalarray}{\setlength{\arraycolsep}{\savearraycolsep}}

\newcommand{\hint}{{\em Hint:\ }}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{xample}{Example}
\newenvironment{example}{\begin{xample}\rm}{\end{xample}}
\def\squarebox#1{\hbox to #1{\hfill\vbox to #1{\vfill}}}
\newcommand{\eqnref}[1]{(\protect\ref{#1})}

\newcommand{\twodots}{\mathinner{\ldotp\ldotp}}

%%% command for putting definitions into boldface
% New style for defined terms, as of 2/23/88, redefined by THC.
\newcommand{\defn}[1]{{\bf #1}}
\newcommand{\defi}[1]{{\bf #1}}

\newcommand{\const}[1]{\ifmmode\mathord{\mathcode`-="702D\sc #1\mathcode`\-="2200}\else$\mathord{\mathcode`-="702D\sc #1\mathcode`\-="2200}$\fi}

%\newcommand{\ckt}[1]{\ifmmode\mathord{\mathcode`-="702D\sc #1\mathcode`\-="2200}\else$\mathord{\mathcode`-="702D\sc #1\mathcode`\-="2200}$\fi}
\newcommand{\ckt}[1]{\ifmmode \sc #1\else$\sc #1$\fi}
\newcommand{\id}[1]{\mbox{\it #1}}
\newcommand{\proc}[1]{\ifmmode\mbox{\sc #1}\else$\mbox{\sc #1}$\fi}
\newcommand{\procdecl}[1]{\proc{#1}\vrule width0pt height0pt depth 7pt \relax}

\newif\ifcodeinbox

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The Code environment %%%

%% The following commands are so we can automatically number and symbolically reference
%% lines.
%% The ``codelinenumber'' counter counts the current line number
\newcounter{codelinenumber}
%% The \zeroli command zeros the line number counter 
\newcommand{\zeroli}{\setcounter{codelinenumber}0}
%% The \li command bumps the counter, outputs it, and skips some space
%% It also GLOBALLY sets the value of \@currentlabel, so that the \label
%% commands on the same line work OK.  The problem otherwise is that Latex
%% uses groups on each field of a line in tabbing mode, and this causes
%% \@currentlabel to get reset as soon as a field ends (e.g. at the \').
%% Now, the label can appear after the \\, as in
%%	\li \>\>$x\gets y$ \\ \label{li:assign-x}
%%

\def\@startline{\global\@curtabmar\@nxttabmar\relax
   \global\@curtab\@curtabmar\setbox\@curline\hbox
    {}\@startfield\global\lifalse\strut}

\newenvironment{code}{\global\codeinboxtrue%
\setbox\strutbox\hbox{\vrule height 9pt depth 4pt width0pt}%
\noindent\begin{tabbing}%
\zeroli\setlength{\tabbingsep}{1em}
\hspace*{1em}\=999\ifdoubledigit9\fi
\=\ {\bf if} \={\bf then} \={\bf if} \={\bf then}
	\={\bf if} \={\bf then} \={\bf if} \={\bf then} \={\bf if} \={\bf then}
	\={\bf if} \=\+\+\kill}{\end{tabbing}\global\codeinboxfalse}

\newif\ifdoubledigit
\newcommand{\codebox}[1]{\setbox0=\vbox{\begin{code}#1\end{code}}
\ifnum\c@codelinenumber>9\global\doubledigittrue\else\doubledigitfalse\fi
\vskip1sp\noindent\hskip-14pt%% 
\parbox{\textwidth}{\begin{code}\protect#1\end{code}}\global\let\@currentlabel=\thechapter}

\newif\ifli
\newcommand{\li}{\global\litrue\stepcounter{codelinenumber}%
\ifdoubledigit%
\hbox to8pt{\hss\thecodelinenumber\hskip5pt}\else%
\hbox to8pt{\hskip-1pt\thecodelinenumber\hss}\fi%
\xdef\@currentlabel{\p@codelinenumber\thecodelinenumber}\'}


\newcommand{\trslabel}[1]{\textit{#1}}
\newcommand{\trsresult}{\setlength{\leftmargin}{30pt}}
\newenvironment{trs-rules}{ %
   \begin{list}{} %
	{\renewcommand{\makelabel}{\trslabel} %
	\setlength{\labelwidth}{35pt}%
	\setlength{\leftmargin}{50pt}%
	\setlength{\leftmargin}{\labelwidth}}}
{\end{list}}