summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/liststructure/pgfplotsdeque.code.tex
blob: 7a524f1ee645c23dfa393b3d1ba93789cc3c6c67 (plain)
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is a helper package with an elementary double-ended-queue
% (deque) datastructure,
% featuring O(1) index access and O(N) creation, deletion, copy.
%
% The following macros are supplied:
%
% \pgfplotsdequenewempty
% \pgfplotsdequecopy
% \pgfplotsdequepushback
% \pgfplotsdequepopfront
% \pgfplotsdequecheckempty
% \pgfplotsdequeforeach
%
% Copyright 2007/2008 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% IMPLEMENTATION NOTES:
% I allocate an array which provides storage capacity and two pointers
% (indices) into that array.
%
% The deque is stored as
% - an array: \csname pgfpldq@<name>\endcsname
% - the pointers:
%   \csname pgfpldq@<name>@beg\endcsname
%   \csname pgfpldq@<name>@end\endcsname (one after the end)
%
% ATTENTION: <name> may NOT be a control sequence! 
%            this is in contrast to my other container structures.


% Allocates a new, empty deque #1.
%
% #1 the name of a deque (NO control sequence!)
% #2 is an integer denoting the maximum capacity. For now, this
% capacity is fixed afterwards and denotes the maximum number of
% entries.
\def\pgfplotsdequenewempty#1{%
	\pgfutil@ifnextchar\capacity{%
		\pgfplotsdequenewempty@{#1}%
	}{%
		\pgfplots@error{Expected \string\capacity\{the capacity\} after \string\pgfplotsdequenewempty...}%
		\pgfplotsdequenewempty@{#1}\capacity{100}%
	}%
}%
\def\pgfplotsdequenewempty@#1\capacity#2{%
	\pgfplotsarrayresize{pgfpldq@#1}{#2}%
	\pgfutil@namedef{pgfpldq@#1@beg}{0}%
	\pgfutil@namedef{pgfpldq@#1@end}{0}%
}%

\def\pgfplotsdequecopy#1\to#2{%
	\pgfutil@namelet{pgfpldq@#1@beg}{pgfpldq@#2@beg}%
	\pgfutil@namelet{pgfpldq@#1@end}{pgfpldq@#2@end}%
	\pgfplotsarraycopy{pgfpldq@#1}\to{pgfpldq@#2}%
}%

\def\pgfplotsdequepushback#1\to#2{%
	\pgfplotsarrayset{\csname pgfpldq@#2@end\endcsname}\of{pgfpldq@#2}\to{#1}%
	\c@pgfplotsarray@tmp=\csname pgfpldq@#2@end\endcsname
	\advance\c@pgfplotsarray@tmp by1
	\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#2}
		\c@pgfplotsarray@tmp=0
	\fi
	\expandafter\edef\csname pgfpldq@#2@end\endcsname{\the\c@pgfplotsarray@tmp}%
	\pgfplotsdequeifempty{#2}{\pgfplots@error{Error: \string\pgfplotsdeque\space capacity has been reached - it was too small. Sorry, I didn't write auto-enlargement...}}{}%
}%

\def\pgfplotsdequepopfront#1\to#2{%
	\pgfplotsarrayselect{\csname pgfpldq@#1@beg\endcsname}\of{pgfpldq@#1}\to{#2}%
	\c@pgfplotsarray@tmp=\csname pgfpldq@#1@beg\endcsname
	\advance\c@pgfplotsarray@tmp by1
	\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#1}
		\c@pgfplotsarray@tmp=0
	\fi
	\expandafter\edef\csname pgfpldq@#1@beg\endcsname{\the\c@pgfplotsarray@tmp}%
}%

% invokes #2 if deque '#1' is empty and '#3' if it is not empty.
\def\pgfplotsdequeifempty#1#2#3{%
	\ifnum\csname pgfpldq@#1@beg\endcsname=\csname pgfpldq@#1@end\endcsname\relax
		#2%
	\else
		#3%
	\fi
}%


\long\def\pgfplotsdequeforeach#1\as#2#3{%
	\c@pgfplotsarray@tmp=\csname pgfpldq@#1@beg\endcsname
	\long\def\pgfplotsdequeforeach@next{\pgfplotsdequeforeach@iter{#1}{#2}{#3}}%
	\pgfplotsdequeforeach@next
}%
\long\def\pgfplotsdequeforeach@iter#1#2#3{%
	\ifnum\c@pgfplotsarray@tmp=\csname pgfpldq@#1@end\endcsname
		\def\pgfplotsdequeforeach@next{}%
	\else
		\pgfplotsarrayselect\c@pgfplotsarray@tmp\of pgfpldq@#1\to#2%
		\edef\pgfplotsdequeforeach@{\the\c@pgfplotsarray@tmp}%
		#3\relax
		\c@pgfplotsarray@tmp=\pgfplotsdequeforeach@
		\advance\c@pgfplotsarray@tmp by1
		\ifnum\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{pgfpldq@#1}
			\c@pgfplotsarray@tmp=0
		\fi
	\fi
	\pgfplotsdequeforeach@next
}%