Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active November 10, 2022 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/5165ee85b01372389c3998b331d803c2 to your computer and use it in GitHub Desktop.
Save zr-tex8r/5165ee85b01372389c3998b331d803c2 to your computer and use it in GitHub Desktop.
Deque in TeX
% Compile this file with 'pdftex' command!
%% \DequeNew\DEQ : Makes a new deque \DEQ.
%% \DequePushBack\DEQ{<text>} : Pushes-back <text> to \DEQ.
%% \DequePushFront\DEQ{<text>} : Pushes-front <text> to \DEQ.
%% \DequePopBack\DEQ : Pops-back from \DEQ and sets the popped text
% to \DequePopped.
%% \DequePopFront\DEQ : Pops-front from \DEQ and sets the popped text
% to \DequePopped.
\catcode`\@=11 %--------------------------------------------
\newcount\deq@id \deq@id\z@
\def\deq@{\deq@@} \def\deq@@{\deq@@@}
\def\deq@newcount{\csname newcount\endcsname}% avoid \outer
\def\deq@error#1{\errhelp{}\errmessage{deque error: #1}}
\def\deq@dispatch#1#2{\expandafter\deq@disp@a#2\deq@@{#1}}
\def\deq@disp@a#1{\ifx#1\deq@\expandafter\deq@disp@b\fi \deq@disp@c}
\def\deq@disp@b#1#2#3#4\deq@@#5{#5#2#3{deq@/#4}}
\def\deq@disp@c#1\deq@@#2{\deq@error{Not a deque}}
\def\deq@no@empty#1#2#3{\ifnum#2>#3\expandafter\deq@no@empty@a\fi#1#2#3}
\def\deq@no@empty@a#1#2#3{\deq@error{The deque is empty}}
\def\deq@pop#1{\expandafter\let\expandafter\DequePopped\csname#1\endcsname}
\def\DequeNew{\advance\deq@id\@ne
\expandafter\deq@new@a \csname deq@bi/\the\deq@id\expandafter\endcsname
\csname deq@ei/\the\deq@id\expandafter\endcsname \the\deq@id\deq@@}
\def\deq@new@a#1#2#3\deq@@#4{\def#4{\deq@#1#2{#3}}%
\deq@newcount#1#1\@ne \deq@newcount#2#2\z@}
\def\DequePushBack{\deq@dispatch\deq@pushb@a}
\def\deq@pushb@a#1#2#3{\advance#2\@ne
\expandafter\def\csname#3/\the#2\endcsname}
\def\DequePushFront{\deq@dispatch\deq@pushf@a}
\def\deq@pushf@a#1#2#3{\advance#1\m@ne
\expandafter\def\csname#3/\the#1\endcsname}
\def\DequePopBack{\deq@dispatch{\deq@no@empty\deq@popb@a}}
\def\deq@popb@a#1#2#3{\deq@pop{#3/\the#2}%
\advance#2\m@ne}
\def\DequePopFront{\deq@dispatch{\deq@no@empty\deq@popf@a}}
\def\deq@popf@a#1#2#3{\deq@pop{#3/\the#1}%
\advance#1\@ne}
\catcode`\@=12 %--------------------------------------------
%% EXAMPLES
\DequeNew\hoge
\DequePushBack\hoge{foo}
\DequePushBack\hoge{bar}
\DequePushBack\hoge{gee}
\DequePopFront\hoge \DequePopped\par %==> foo
\DequePushFront\hoge{whiz}
% Now the content is: [ whiz bar gee ]
\DequePopBack\hoge \DequePopped\par %==> gee
\DequePopBack\hoge \DequePopped\par %==> bar
\DequePopBack\hoge \DequePopped\par %==> whiz
% Now the deque is empty
\bye
%% \DequeDump\DEQ : Dumps to terminal the content of \DEQ.
\catcode`\@=11 %--------------------------------------------
\def\DequeDump{\deq@dispatch\deq@dump@a}
\def\deq@dump@a#1#2#3{\begingroup
\def\deq@out{}\advance#1\m@ne
\loop\ifnum#1<#2\advance#1\@ne
\edef\deq@out{\deq@out\csname#3/\the#1\endcsname\space}\repeat
\immediate\write16{[ \deq@out]}%
\endgroup}
\catcode`\@=12 %--------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment