1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
|
% \changes{1.0}{2014/03/24}{Initial version}
%
% \iffalse
%<*never>
\documentclass{ltxdoc}
\usepackage{fifo-stack}
\usepackage{hyperref}
\GetFileInfo{fifo-stack.sty}
\CodelineIndex
\EnableCrossrefs
\RecordChanges
\begin{document}
\CheckSum{12}
\DocInput{fifo-stack.dtx}
\section{Copyright and \LaTeX\ Project Public License}
\begin{verbatim}
%txsBeginComment
Copyright (C) 2014 Stephen Gaito (PerceptiSys Ltd)
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3 of this license or (at
your option) any later version.
The latest version of this license is in
http://www.latex-project.org/lppl.txt and version 1.3 or later is part of
all distributions of LaTeX version 2005/12/01 or later.
This work has the LPPL maintenance status `maintained'.
The Current Maintainer of this work is Stephen Gaito.
The released source can be found at:
https://github.com/diSimplex/latexFifoStack/tree/master/texStyle
The development source can be found at:
https://github.com/stephengaito/latexFifoStack/tree/master/texStyle
This work consists of the files fifo-stack.dtx, and fifo-stack.ins.
The command:
pdflatex fifo-stack.ins
followed by:
pdflatex fifo-stack.dtx
will produce the derived files: fifo-stack.sty, fifo-stack-test.tex,
and fifo-stack.pdf.
The command:
pdflatex fifo-stack-test.tex
will regression test the fifo-stack package. Output will be found in
fifo-stack-test.lgout. If pdflatex completes with no errors, then all
regression tests passed.
%txsEndComment
\end{verbatim}
\PrintIndex
\PrintChanges
\end{document}
%</never>
% \fi
%
% \DoNotIndex{\edef,\advance,\else,\csname,\endcsname,\expandafter}
% \DoNotIndex{\ifx,\fi,\input,\ifnum,\relax,\newcount,\romannumeral}
% \DoNotIndex{\the,\typeout,\xdef,\global,\newcommand,\RequirePackage}
% \DoNotIndex{\PackageError,\ProvidesPackage,\gdef}
% \DoNotIndex{\IncludeTests,\LogClose,\LogTests,\Expect,\ExpectIfThen}
% \DoNotIndex{\isnamedefined,\isundefined}
% \DoNotIndex{\stop,\makeatletter,\makeatother,\begin,\end,\def,\let}
% \DoNotIndex{\testFSDestroy@fifo@stack@count@top}
% \DoNotIndex{\testFSDestroy@fifo@stack@count@bottom}
% \DoNotIndex{\testFSDestroy@fifo@stack@count@size}
% \DoNotIndex{\fifo@stack@top@testFSDestroy}
% \DoNotIndex{\fifo@stack@bottom@testFSDestroy}
%
% \title{A \LaTeX\ FIFO/Stack implementation for Package writers}
% \author{Stephen \textsc{Gaito}}
% \date{\filedate}
% \maketitle
% \begin{abstract}
% This package provides a \LaTeX\ implementation of a FIFO/Stack system
% independent from the \TeX\ stack itself. It is based on Benjamin Bayart's
% excellent \href{http://www.ctan.org/pkg/stack}{stack} package from
% \href{http://www.ctan.org}{CTAN}.
%
% This package by itself is of no use to anyone other than macro-developers.
%
% Note that to simplify the computations, this package makes essential use of
% $\varepsilon$-\TeX's \verb|\numexpr| macro. Fortunately most modern \TeX,
% \LaTeX\ and pdf\LaTeX's are actually $\varepsilon$-\TeX.
% \end{abstract}
% \tableofcontents
%
% \section{Using the FIFO/stack package}
%
% All of the macros provided by this package are meant to be used by package
% developers (there is no meaningful use of a FIFO or stack in a final
% document, they will only to be used in a package), so, all of those macros
% have capital letters in their names, following old guidelines for \LaTeX3
% project.
%
% To ensure these macros do not clash with other packages, the names of all
% macros begin with ``FS''.
%
% \subsection{FIFO/Stack creation and destruction commands}
%
% To create a new FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSCreate}\verb|\FSCreate{fifoStackName}{defaultValue}|
% \end{quote}
% This macro simply initializes all the required data for the FIFO/stack named by
% the value of the first argument, \verb|fifoStackName|. The second argument,
% \verb|defaultValue|, is the value of the top of the FIFO/stack whenever the
% FIFO/stack is empty.
%
% To clear an existing FIFO/stack of all of its current elements:
% \begin{quote}
% \DescribeMacro{\FSClear}\verb|\FSClear{fifoStackName}|
% \end{quote}
% This macro empties the FIFO/stack named \verb|fifoStackName|, but the
% FIFO/stack can still be used to \verb|\FSPush| or \verb|\FSUnshift| new
% elements.
%
% To destroy an existing FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSDestroy}\verb|\FSDestroy{fifoStackName}|
% \end{quote}
% This macro destroys the FIFO/stack named \verb|fifoStackName|.
%
% \subsection{Stack based commands}
%
% To push a new value on \emph{top} of a FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSPush}\verb|\FSPush{fifoStackName}{value}|\newline
% \end{quote}
% Note that you \emph{can} use \verb|\FSTop| in the \verb|value| argument of the
% \verb|\FSPush| macro. See the qstest unit test for \verb|\FSPush| (below) for
% an example.
%
% To remove the value from the \emph{top} of a FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSPop}\verb|\FSPop{fifoStackName}|
% \end{quote}
%
% To get the value from the \emph{top} of the FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSTop}\verb|\FSTop{fifoStackName}|
% \end{quote}
%
% For use when debugging your use of the FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSShowTop}\verb|\FSShowTop{fifoStackName}|
% \end{quote}
% will \verb|\typeout| the current value on the \emph{top} of the FIFO/stack.
%
% \subsection{FIFO based commands}
%
% To add a new value to the \emph{bottom} of a FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSUnshift}\verb|\FSUnshift{fifoStackName}{value}|
% \end{quote}
% Note that you \emph{can} use \verb|\FSBottom| in the \verb|value| argument of
% the \verb|\FSUnshift| macro. See the qstest unit test for \verb|\FSUnshift|
% (below) for an example.
%
% To remove the value on the \emph{bottom} of a FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSShift}\verb|\FSShift{fifoStackName}|
% \end{quote}
%
% To get the value on the \emph{bottom} of a FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSBottom}\verb|\FSBottom{fifoStackName}|
% \end{quote}
%
% For use when debugging your use of the FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSShowBottom}\verb|\FSShowBottom{fifoStackName}|
% \end{quote}
% will \verb|\typeout| the current value on the \emph{bottom} of the FIFO/stack.
%
% \subsection{FIFO/stack size commands}
%
% To get the current size of the FIFO/stack:
% \begin{quote}
% \DescribeMacro{\FSSize}\verb|\FSSize{fifoStackName}|
% \end{quote}
%
% \subsection{TeXStudio CWL file}
%
% As a bonus for users of TeXStudio, the \verb|fifo-stack.zip| file also
% contains a \verb|fifo-stack.cwl| file which provides TeXStudio command
% expansion descriptions for each of the `public' \verb|fifo-stack| macros. To
% make use of these command expansions, simply place the \verb|fifo-stack.cwl|
% file into TeXStudio's local configuration directory (on most Linux
% distributions this can be found in \verb|~/.config/texstudio|).
%
% This \verb|fifo-stack.cwl| file is automatically generated from the macrocode
% lines which begin with the characters \verb|%txs|. This automatic generation
% is done by the \verb|cook texStyle| command defined by the sister project
% \href{https://github.com/diSimplex/diSimplexLaTeX}{diSimplexLaTeX}'s (ruby)
% cookbook.
%
% \subsection{\TeX\ and \LaTeX\ Dependencies}
%
% The \verb|fifo-stack| package depends upon both $\varepsilon$-\TeX, as well as
% the \verb|ifthen| package. $\varepsilon$-\TeX\ is required only for the two
% uses of \verb|\numexpr|. The \verb|ifthen| package is only required for the
% one use of \verb|\whiledo|. All three uses are listed in the index.
%
% The production of this documentation assumes you have the \verb|hyperref|
% package installed.
%
% The regression tests associated with this package depend upon the
% \verb|qstest|, \verb|ifthen|, and \verb|xifthen| packages.
%
% \section{Integration tests and example usage}
%
% We use the \href{http://www.ctan.org/pkg/qstest}{QSTest} package from
% \href{http://www.ctan.org}{CTAN} to provide both integration and unit tests of
% our FIFO/stack package.
%
% In this section we walk through a number of integration tests which also
% provide example uses of FIFO/Stacks. We provide unit tests of specific
% invariants in the code section below. These unit tests are associated
% with the section of code which implements a particular invariant.
%
% We begin by setting up the \LaTeX\ QSTest package to test the \verb|fifo-stack|
% package, and we will log everything. Note that we do not use a document class
% or begin/end a document, this is because there should not be any \emph{normal}
% output created. All \emph{results} are listed in the associated
% \verb|fifo-stack-test.lgout| file.
%
% \begin{macrocode}
%<*qstest>
\RequirePackage{qstest}
\RequirePackage{xifthen}
\IncludeTests{*}
\LogTests{lgout}{*}{*}
\RequirePackage{fifo-stack}
%</qstest>
% \end{macrocode}
%
% \subsection{Stack based examples}
%
% We now provide an example of standard use of just the stack based
% functionality. To do this we create a stack, push three things onto the stack
% and then pop all three things back off, all in a nice linear fashion.
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{Simple stack integration test}
{\FSCreate, \FSPush, \FSPop, \FSTop}
\FSCreate{testStack}{defaultValue}
\Expect*{\FSTop{testStack}}{defaultValue}
\FSPush{testStack}{firstValue}
\Expect*{\FSTop{testStack}}{firstValue}
\FSPush{testStack}{secondValue}
\Expect*{\FSTop{testStack}}{secondValue}
\FSPush{testStack}{thirdValue}
\Expect*{\FSTop{testStack}}{thirdValue}
\FSPop{testStack}
\Expect*{\FSTop{testStack}}{secondValue}
\FSPop{testStack}
\Expect*{\FSTop{testStack}}{firstValue}
\FSPop{testStack}
\Expect*{\FSTop{testStack}}{defaultValue}
\FSPop{testStack}
\Expect*{\FSTop{testStack}}{defaultValue}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% We now look at using the stack in a non-linear order pushing, popping, then
% pushing and popping again.
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{Up down up down stack integration test}
{\FSCreate, \FSPush, \FSPop, \FSTop}
\FSCreate{testStackUDUD}{defaultValue}
\Expect*{\FSTop{testStackUDUD}}{defaultValue}
\FSPush{testStackUDUD}{firstValue}
\Expect*{\FSTop{testStackUDUD}}{firstValue}
\FSPush{testStackUDUD}{secondValue}
\Expect*{\FSTop{testStackUDUD}}{secondValue}
\FSPop{testStackUDUD}
\Expect*{\FSTop{testStackUDUD}}{firstValue}
\FSPush{testStackUDUD}{newSecondValue}
\Expect*{\FSTop{testStackUDUD}}{newSecondValue}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% \subsection{FIFO/Stack based examples}
%
% This time we make use of \verb|\FSPush| to put items onto the FIFO and
% \verb|\FSShift| to take them off in a \emph{first in first off} order. At the
% end we shift past the top of the FIFO/Stack, and so we should get the default
% value again.
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{Simple FIFO integration test}
{\FSCreate, \FSPush, \FSShift, \FSTop, \FSBottom}
\FSCreate{testFifo}{defaultValue}
\Expect*{\FSTop{testFifo}}{defaultValue}
\Expect*{\FSBottom{testFifo}}{defaultValue}
\FSPush{testFifo}{firstValue}
\Expect*{\FSTop{testFifo}}{firstValue}
\Expect*{\FSBottom{testFifo}}{firstValue}
\FSPush{testFifo}{secondValue}
\Expect*{\FSTop{testFifo}}{secondValue}
\Expect*{\FSBottom{testFifo}}{firstValue}
\FSShift{testFifo}
\Expect*{\FSTop{testFifo}}{secondValue}
\Expect*{\FSBottom{testFifo}}{secondValue}
\FSPush{testFifo}{thirdValue}
\Expect*{\FSTop{testFifo}}{thirdValue}
\Expect*{\FSBottom{testFifo}}{secondValue}
\FSShift{testFifo}
\Expect*{\FSTop{testFifo}}{thirdValue}
\Expect*{\FSBottom{testFifo}}{thirdValue}
\FSShift{testFifo}
\Expect*{\FSTop{testFifo}}{defaultValue}
\Expect*{\FSBottom{testFifo}}{defaultValue}
\FSShift{testFifo}
\Expect*{\FSTop{testFifo}}{defaultValue}
\Expect*{\FSBottom{testFifo}}{defaultValue}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% We now provide an example use of \verb|\FSUnshift| and \verb|\FSShift|.
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{Simple linear use of unshift/shift}
{\FSUnshift, \FSShift}
\FSCreate{testRStack}{defaultValue}
\Expect*{\FSBottom{testRStack}}{defaultValue}
\FSUnshift{testRStack}{firstValue}
\Expect*{\FSBottom{testRStack}}{firstValue}
\FSUnshift{testRStack}{secondValue}
\Expect*{\FSBottom{testRStack}}{secondValue}
\FSUnshift{testRStack}{thirdValue}
\Expect*{\FSBottom{testRStack}}{thirdValue}
\FSShift{testRStack}
\Expect*{\FSBottom{testRStack}}{secondValue}
\FSShift{testRStack}
\Expect*{\FSBottom{testRStack}}{firstValue}
\FSShift{testRStack}
\Expect*{\FSBottom{testRStack}}{defaultValue}
\FSShift{testRStack}
\Expect*{\FSBottom{testRStack}}{defaultValue}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% Now we provide an example (non-linear) use of all of the \verb|\FSPush|,
% \verb|\FSPop|, \verb|\FSUnshift|, and \verb|\FSShift| macros
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{Full non-linear FIFO/stack integration test}
{\FSCreate, \FSPush, \FSPop, \FSUnshift, \FSShift, \FSTop, \FSBottom}
\FSCreate{testFS}{defaultValue}
\Expect*{\FSTop{testFS}}{defaultValue}
\Expect*{\FSBottom{testFS}}{defaultValue}
\FSPush{testFS}{value1}
\Expect*{\FSTop{testFS}}{value1}
\Expect*{\FSBottom{testFS}}{value1}
\FSUnshift{testFS}{value-1}
\Expect*{\FSTop{testFS}}{value1}
\Expect*{\FSBottom{testFS}}{value-1}
\FSShift{testFS}
\Expect*{\FSTop{testFS}}{value1}
\Expect*{\FSBottom{testFS}}{value1}
\FSPush{testFS}{value2}
\Expect*{\FSTop{testFS}}{value2}
\Expect*{\FSBottom{testFS}}{value1}
\FSUnshift{testFS}{value-1again}
\Expect*{\FSTop{testFS}}{value2}
\Expect*{\FSBottom{testFS}}{value-1again}
\FSUnshift{testFS}{value-2}
\Expect*{\FSTop{testFS}}{value2}
\Expect*{\FSBottom{testFS}}{value-2}
\FSPop{testFS}
\Expect*{\FSTop{testFS}}{value1}
\Expect*{\FSBottom{testFS}}{value-2}
\FSPop{testFS}
\Expect*{\FSTop{testFS}}{value-1again}
\Expect*{\FSBottom{testFS}}{value-2}
\FSShift{testFS}
\Expect*{\FSTop{testFS}}{value-1again}
\Expect*{\FSBottom{testFS}}{value-1again}
\FSPop{testFS}
\Expect*{\FSTop{testFS}}{defaultValue}
\Expect*{\FSBottom{testFS}}{defaultValue}
\FSPop{testFS}
\Expect*{\FSTop{testFS}}{defaultValue}
\Expect*{\FSBottom{testFS}}{defaultValue}
\FSShift{testFS}
\Expect*{\FSTop{testFS}}{defaultValue}
\Expect*{\FSBottom{testFS}}{defaultValue}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% Finally we add an integration test of the full suite, including
% \verb|\FSClear|, \verb|\FSSize|, and \verb|\FSDestroy| macros
%
% \begin{macrocode}
%<*qstest>
\begin{qstest}{FSClear, FSSize, and FSDestroy test}
{\FSCreate, \FSPush, \FSPop, \FSUnshift, \FSShift, \FSTop,
\FSBottom, \FSClear, \FSDestroy, \FSSize}
\FSCreate{testFSC}{defaultValue}
\Expect*{\FSSize{testFSC}}{0}
\Expect*{\FSTop{testFSC}}{defaultValue}
\Expect*{\FSBottom{testFSC}}{defaultValue}
\FSPush{testFSC}{value1}
\Expect*{\FSSize{testFSC}}{1}
\Expect*{\FSTop{testFSC}}{value1}
\Expect*{\FSBottom{testFSC}}{value1}
\FSUnshift{testFSC}{value-1}
\Expect*{\FSSize{testFSC}}{2}
\Expect*{\FSTop{testFSC}}{value1}
\Expect*{\FSBottom{testFSC}}{value-1}
\FSPush{testFSC}{value2}
\Expect*{\FSSize{testFSC}}{3}
\Expect*{\FSTop{testFSC}}{value2}
\Expect*{\FSBottom{testFSC}}{value-1}
\FSUnshift{testFSC}{value-2}
\Expect*{\FSSize{testFSC}}{4}
\Expect*{\FSTop{testFSC}}{value2}
\Expect*{\FSBottom{testFSC}}{value-2}
\FSClear{testFSC}
\Expect*{\FSSize{testFSC}}{0}
\Expect*{\FSTop{testFSC}}{defaultValue}
\Expect*{\FSBottom{testFSC}}{defaultValue}
\FSPush{testFSC}{value1}
\Expect*{\FSSize{testFSC}}{1}
\Expect*{\FSTop{testFSC}}{value1}
\Expect*{\FSBottom{testFSC}}{value1}
\FSUnshift{testFSC}{value-1}
\Expect*{\FSSize{testFSC}}{2}
\Expect*{\FSTop{testFSC}}{value1}
\Expect*{\FSBottom{testFSC}}{value-1}
\FSPush{testFSC}{value2}
\Expect*{\FSSize{testFSC}}{3}
\Expect*{\FSTop{testFSC}}{value2}
\Expect*{\FSBottom{testFSC}}{value-1}
\FSUnshift{testFSC}{value-2}
\Expect*{\FSSize{testFSC}}{4}
\Expect*{\FSTop{testFSC}}{value2}
\Expect*{\FSBottom{testFSC}}{value-2}
\FSDestroy{testFSC}
\end{qstest}
%</qstest>
% \end{macrocode}
%
% \section{Code for the FIFO/stack package}
%
% \subsection{Identification}
%
% \begin{macrocode}
%<*package>
\ProvidesPackage{fifo-stack}[2014/03/24 v1.0 Multi-FIFO/stack system]
\RequirePackage{ifthen}
% \end{macrocode}
%
% \subsection{FIFO/stack creation and destruction macro definitions}
%
% In the following code, we use only three counters (in the \TeX\ meaning) we
% store the various values in macros, and use only the top, bottom or size
% counters when we need to do computations.
%
% \begin{macro}{\tmp@fifo@stack@top}
% \begin{macrocode}
\newcount\tmp@fifo@stack@top
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tmp@fifo@stack@bottom}
% \begin{macrocode}
\newcount\tmp@fifo@stack@bottom
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tmp@fifo@stack@size}
% \begin{macrocode}
\newcount\tmp@fifo@stack@size
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fifo@stack@pointer}
%
% All items in a given FIFO/stack named \verb|fifoStackName| are stored in
% dynamically defined macros whose names consist of the \verb|fifoStackName|
% followed by \verb|@| followed by the value of the \verb|\fifo@stack@pointer|
% macro with either \verb|\tmp@fifo@stack@top| or \verb|\tmp@fifo@stack@bottom|
% counters as argument.
%
% We start by providing a unit test of the expected behaviour of the\\
% \verb|\fifo@stack@pointer| macro.
%
% \begin{macrocode}
%</package>
%<*qstest>
\begin{qstest}{Unit test of \fifo@stack@pointer}{\fifo@stack@pointer}
\makeatletter
\Expect*{\fifo@stack@pointer{1}}{ii}
\Expect*{\fifo@stack@pointer{0}}{i}
\Expect*{\fifo@stack@pointer{-1}}{@i}
\makeatother
\end{qstest}
%</qstest>
%<*package>
% \end{macrocode}
%
% Now the macrocode itself. Note the only two uses of the $\varepsilon$-\TeX\
% \verb|\numexpr| macro.
%
% \begin{macrocode}
\global\def\fifo@stack@pointer#1{%
\ifnum#1<0
@\expandafter\romannumeral\numexpr -1*#1 \relax
\else
\expandafter\romannumeral\numexpr #1+1 \relax
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSCreate}
%
% Creating a new FIFO/stack is, essentially, creating new counter-like top,
% bottom and size macros, initializing them to 0, 1 and 0 respectively, as well
% as creating macros for stack-top and stack-bottom evaluation (see next two
% macros below).
%
% \begin{macrocode}
%txs\FSCreate{fifoStackName}{defaultValue}#
\newcommand\FSCreate[2]{%
\expandafter\gdef\csname #1@fifo@stack@count@top\endcsname{0}%
\expandafter\gdef\csname #1@fifo@stack@count@bottom\endcsname{1}%
\expandafter\gdef\csname #1@fifo@stack@count@size\endcsname{0}%
\expandafter\do@fifo@stack@top@macro%
\csname #1@fifo@stack@count@top\endcsname{#1}{#2}%
\expandafter\do@fifo@stack@bottom@macro%
\csname #1@fifo@stack@count@bottom\endcsname{#1}{#2}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\do@fifo@stack@top@macro}
%
% This stack-top evaluation macro is required due to an ugly trick. During the
% push, we ``evaluate'' the value to push with an \verb|\edef|, because one
% might want to push something which contains the previous top of the stack (see
% the example package provided). If the \verb|\FSTop| or \verb|FSBottom| macros
% compute the real value on the top or bottom of the stack, then, the expanded
% definition in \verb|\FSPush| will contain more or less the \emph{content} of
% \verb|\FSTop| and not the value.
%
% Another way would be to have a \verb|\csname...\endcsname| pair within
% another one in an \verb|\ifx| condition, which doesn't work.
%
% \begin{macrocode}
\newcommand\do@fifo@stack@top@macro[3]{%
\expandafter\newcommand\expandafter*%
\csname fifo@stack@top@#2\endcsname{%
\expandafter\ifx\csname #2@\fifo@stack@pointer#1\endcsname\relax
#3%
\else
\csname #2@\fifo@stack@pointer#1\endcsname
\fi
}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\do@fifo@stack@bottom@macro}
%
% This is the stack-bottom macro defined for the same reasons as
% \verb|\do@fifo@stack@top@macro| (see above).
%
% \begin{macrocode}
\newcommand\do@fifo@stack@bottom@macro[3]{%
\expandafter\newcommand\expandafter*%
\csname fifo@stack@bottom@#2\endcsname{%
\expandafter\ifx\csname #2@\fifo@stack@pointer#1\endcsname\relax
#3%
\else
\csname #2@\fifo@stack@pointer#1\endcsname
\fi
}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSClear}
%
% While the size of the FIFO/stack is greater than zero, pop the FIFO/stack.
% Note the only use of the \verb|ifthen| package's \verb|\whiledo| macro.
%
% \begin{macrocode}
%txs\FSClear{fifoStackName}#
\newcommand{\FSClear}[1]{%
\whiledo{0 < \FSSize{#1}}{\FSPop{#1}}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSDestroy}
%
% Clear the FIFO/stack and then undefine all of the supporting macros.
%
% We provide a unit test of the expected behaviour of the \verb|\FSDestroy|
% macro.
%
% \begin{macrocode}
%</package>
%<*qstest>
\begin{qstest}{Unit test of \FSdestroy}
{\FSCreate, \FSDestroy}
\FSCreate{testFSDestroy}{defaultValue}
\makeatletter
\ExpectIfThen{\isnamedefined{testFSDestroy@fifo@stack@count@top}}
\ExpectIfThen{\isnamedefined{testFSDestroy@fifo@stack@count@bottom}}
\ExpectIfThen{\isnamedefined{testFSDestroy@fifo@stack@count@size}}
\ExpectIfThen{\isnamedefined{fifo@stack@top@testFSDestroy}}
\ExpectIfThen{\isnamedefined{fifo@stack@bottom@testFSDestroy}}
\makeatother
\FSDestroy{testFSDestroy}
\makeatletter
\ExpectIfThen{\isundefined\testFSDestroy@fifo@stack@count@top}
\ExpectIfThen{\isundefined\testFSDestroy@fifo@stack@count@bottom}
\ExpectIfThen{\isundefined\testFSDestroy@fifo@stack@count@size}
\ExpectIfThen{\isundefined\fifo@stack@top@testFSDestroy}
\ExpectIfThen{\isundefined\fifo@stack@bottom@testFSDestroy}
\makeatother
\end{qstest}
%</qstest>
%<*package>
% \end{macrocode}
%
% And now the macrocode for \verb|\FSDestroy|.
%
% \begin{macrocode}
%txs\FSDestroy{fifoStackName}#
\newcommand{\FSDestroy}[1]{%
\FSClear{#1}
\expandafter\global\expandafter\let
\csname #1@fifo@stack@count@top\endcsname=
\fifo@stack@never@defined\relax
\expandafter\global\expandafter\let
\csname #1@fifo@stack@count@bottom\endcsname=
\fifo@stack@never@defined\relax
\expandafter\global\expandafter\let
\csname #1@fifo@stack@count@size\endcsname=
\fifo@stack@never@defined\relax
\expandafter\global\expandafter\let
\csname fifo@stack@top@#1\endcsname=
\fifo@stack@never@defined\relax
\expandafter\global\expandafter\let
\csname fifo@stack@bottom@#1\endcsname=
\fifo@stack@never@defined\relax
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Stack based macro definitions}
%
% \begin{macro}{\FSTop}
% I'd rather remove the ``check'' code, but, well, it would suppose
% the end-user of the macro is a fair developer, which is not
% realistic.
%
% If the stack has been properly created, we call the top-stack evaluation
% macro.
%
% \begin{macrocode}
%txs\FSTop{fifoStackName}#
\newcommand\FSTop[1]{%
\ifx\csname #1@fifo@stack@count@top\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
\csname fifo@stack@top@#1\endcsname
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSPush}
% The \verb|\FSPush| macro is one of the most complex ones.
%
% We provide a unit test of the ability to use \verb|\FSTop| inside the value
% argument to a \verb|\FSPush| macro (which is the reason for the complexity of
% this macro).
%
% \begin{macrocode}
%</package>
%<*qstest>
\begin{qstest}{Unit test of \FSPush and \FSTop}
{\FSCreate, \FSPush, \FSTop}
\FSCreate{testFSPush}{defaultValue}
\Expect*{\FSTop{testFSPush}}{defaultValue}
\FSPush{testFSPush}{\FSTop{testFSPush}-with-additional-text}
\Expect*{\FSTop{testFSPush}}{defaultValue-with-additional-text}
\end{qstest}
%</qstest>
%<*package>
% \end{macrocode}
%
% And now the macrocode for \verb|\FSPush|. First, we check if the stack is
% properly defined.
%
% \begin{macrocode}
%txs\FSPush{fifoStackName}{value}#
\newcommand\FSPush[2]{%
\ifx\csname #1@fifo@stack@count@top\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
% \end{macrocode}
%
% We store the new top-value in a macro, just because one might do
% \begin{quote}
% \verb|\FSPush{stack}{\FSTop{stack}.ext}|
% \end{quote}
% in which case the expansion of
% the value to push is something rather tricky.
%
% Then, we step the top of the stack (put the top in the scratch
% counter, advance this counter, have the value back in the macro).
%
% We separately adjust the size macro. We do this since actually computing the
% value in the \verb|\FSSize| macro proved difficult.
%
% Only after that, the value to be pushed is stored in the corresponding
% slot.
% \begin{macrocode}
\edef\fifo@stack@newtop{#2}%
\tmp@fifo@stack@top\csname #1@fifo@stack@count@top\endcsname\relax
\global\advance\tmp@fifo@stack@top by 1\relax
\expandafter\xdef\csname #1@fifo@stack@count@top\endcsname
{\the\tmp@fifo@stack@top}%
\tmp@fifo@stack@size\csname #1@fifo@stack@count@size\endcsname\relax
\global\advance\tmp@fifo@stack@size by 1\relax
\expandafter\xdef\csname #1@fifo@stack@count@size\endcsname
{\the\tmp@fifo@stack@size}%
\expandafter\xdef\csname #1@\fifo@stack@pointer\tmp@fifo@stack@top\endcsname
{\fifo@stack@newtop}%
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSPop}
% The \verb|\FSPop| macro is simpler: if the stack is properly defined and
% the stack-top is greater than or equal to the stack-bottom, then we undefine
% the existing `top' macro and down-step the stack-top and stack-size.
% \begin{macrocode}
%txs\FSPop{fifoStackName}#
\newcommand\FSPop[1]{%
\ifx\csname #1@fifo@stack@count@top\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
\tmp@fifo@stack@top\csname #1@fifo@stack@count@top\endcsname\relax
\tmp@fifo@stack@bottom\csname #1@fifo@stack@count@bottom\endcsname\relax
\ifnum\tmp@fifo@stack@top<\tmp@fifo@stack@bottom\relax\else
\expandafter\global\expandafter\let
\csname #1@\fifo@stack@pointer\tmp@fifo@stack@top\endcsname=
\fifo@stack@never@defined\relax
\advance\tmp@fifo@stack@top by -1\relax
\expandafter\xdef\csname #1@fifo@stack@count@top\endcsname
{\the\tmp@fifo@stack@top}%
\tmp@fifo@stack@size\csname #1@fifo@stack@count@size\endcsname\relax
\global\advance\tmp@fifo@stack@size by -1\relax
\expandafter\xdef\csname #1@fifo@stack@count@size\endcsname
{\the\tmp@fifo@stack@size}%
\fi
\fi
}
% \end{macrocode}
% \end{macro}
%
% Now, a debug only macro.
% \begin{macro}{\FSShowTop}
% \begin{macrocode}
%txs\FSShowTop{fifoStackName}#
\newcommand\FSShowTop[1]{%
{\edef\fifo@stack@top{\FSTop{#1}}%
\typeout{The top of #1: \fifo@stack@top}}}
% \end{macrocode}
% \end{macro}
%
% \subsection{FIFO based macro definitions}
%
% \begin{macro}{\FSBottom}
% \begin{macrocode}
%txs\FSBottom{fifoStackName}#
\newcommand\FSBottom[1]{%
\ifx\csname #1@fifo@stack@count@bottom\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
\csname fifo@stack@bottom@#1\endcsname
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSUnshift}
% The \verb|\FSUnshift| macro, like the corresponding \verb|\FSPush| macro, is
% one of the most complex ones.
%
% % We provide a unit test of the ability to use \verb|\FSBottom| inside the value
% argument to a \verb|\FSUnshift| macro (which is the reason for the complexity of
% this macro).
%
% \begin{macrocode}
%</package>
%<*qstest>
\begin{qstest}{Unit test of \FSUnshift and \FSBottom}
{\FSCreate, \FSUnshift, \FSBottom}
\FSCreate{testFSUnshift}{defaultValue}
\Expect*{\FSBottom{testFSUnshift}}{defaultValue}
\FSUnshift{testFSUnshift}{\FSBottom{testFSUnshift}-with-additional-text}
\Expect*{\FSBottom{testFSUnshift}}{defaultValue-with-additional-text}
\end{qstest}
%</qstest>
%<*package>
% \end{macrocode}
%
% And now the macrocode for \verb|\FSUnshift|. First, we check if the stack is
% properly defined.
%
% \begin{macrocode}
%txs\FSUnshift{fifoStackName}{value}#
\newcommand\FSUnshift[2]{%
\ifx\csname #1@fifo@stack@count@bottom\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
% \end{macrocode}
%
% We store the new bottom-value in a macro, just because one might do
% \begin{quote}
% \verb|\FSUnshift{stack}{\FSBottom{stack}.ext}|
% \end{quote}
% in which case the expansion of
% the value to push is something rather tricky.
%
% Then, we step the bottom of the stack (put the bottom in the scratch
% counter, decrement this counter, have the value back in the macro).
%
% We separately adjust the size macro. We do this since actually computing the
% value in the \verb|\FSSize| macro proved difficult.
%
% Only after that, the value to be unshifted is stored in the corresponding
% slot.
% \begin{macrocode}
\edef\fifo@stack@newBottom{#2}%
\tmp@fifo@stack@bottom\csname #1@fifo@stack@count@bottom\endcsname\relax
\global\advance\tmp@fifo@stack@bottom by -1\relax
\expandafter\xdef\csname #1@fifo@stack@count@bottom\endcsname
{\the\tmp@fifo@stack@bottom}%
\tmp@fifo@stack@size\csname #1@fifo@stack@count@size\endcsname\relax
\global\advance\tmp@fifo@stack@size by 1\relax
\expandafter\xdef\csname #1@fifo@stack@count@size\endcsname
{\the\tmp@fifo@stack@size}%
\expandafter\xdef\csname #1@\fifo@stack@pointer\tmp@fifo@stack@bottom\endcsname
{\fifo@stack@newBottom}%
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\FSShift}
% The \verb|\FSShift| macro is fairly simple: if the stack is properly defined
% and the stack-top is greater than or equal to the stack-bottom, then we undefine
% the `bottom' macro and up-step the stack-bottom and adjust the stack-size.
% \begin{macrocode}
%txs\FSShift{fifoStackName}#
\newcommand\FSShift[1]{%
\ifx\csname #1@fifo@stack@count@bottom\endcsname\relax
\PackageError{fifo-stack}{Undefined FIFO/stack #1}%
{You should first create the FIFO/stack with \FSCreate}%
\else
\tmp@fifo@stack@top\csname #1@fifo@stack@count@top\endcsname\relax
\tmp@fifo@stack@bottom\csname #1@fifo@stack@count@bottom\endcsname\relax
\ifnum\tmp@fifo@stack@top<\tmp@fifo@stack@bottom\relax\else
\expandafter\global\expandafter\let
\csname #1@\fifo@stack@pointer\tmp@fifo@stack@bottom\endcsname=
\fifo@stack@never@defined\relax
\advance\tmp@fifo@stack@bottom by 1\relax
\expandafter\xdef\csname #1@fifo@stack@count@bottom\endcsname
{\the\tmp@fifo@stack@bottom}%
\tmp@fifo@stack@size\csname #1@fifo@stack@count@size\endcsname\relax
\global\advance\tmp@fifo@stack@size by -1\relax
\expandafter\xdef\csname #1@fifo@stack@count@size\endcsname
{\the\tmp@fifo@stack@size}%
\fi
\fi
}
% \end{macrocode}
% \end{macro}
%
% Again, a debug only macro for the FIFO usage.
% \begin{macro}{\FSShowBottom}
% \begin{macrocode}
%txs\FSShowBottom{fifoStackName}#
\newcommand\FSShowBottom[1]{%
{\edef\fifo@stack@bottom{\FSBottom{#1}}%
\typeout{The bottom of #1: \fifo@stack@bottom}}}
% \end{macrocode}
% \end{macro}
%
% \subsection{FIFO/stack length macro definition}
%
% \begin{macro}{\FSSize}
%
% We keep track of the changes to the stack-size in each of the \verb|\FSPush|,
% \verb|\FSPop|, \verb|\FSUnshift|, and \verb|\FSShift| macros, since actually
% computing the value in the \verb|\FSSize| macro (below) was returning spurious
% tokens.
%
% \begin{macrocode}
%txs\FSSize{fifoStackName}#
\newcommand\FSSize[1]{\csname #1@fifo@stack@count@size\endcsname}
% \end{macrocode}
% \end{macro}
%
% \subsection{Finishing off}
%
% \begin{macrocode}
%</package>
%<*qstest>
\LogClose{lgout}
\stop
%</qstest>
% \end{macrocode}
%
|