summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/liststructure/pgfplotsliststructure.code.tex
blob: 91caf2a033f766ba03351dcf911d1502b2c74e5c (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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is a helper package with an elementary list datastructure.
%
% Its implementation is based on Knuth's list macros in "The TeXbook".
%
% It features a convenient set of list macros, but it is not fast.
% In fact, every elementary operation requires time O(N), so working 
% with this list easily leads to O(N^2) runtime.
%
% The following macros are supplied:
%
% \pgfplotslistnewempty
% \pgfplotslistnew
% \pgfplotslistcopy
% \pgfplotslistpopfront
% \pgfplotslistfront
% \pgfplotslistpushback
% \pgfplotslistpushfront
% \pgfplotslistsize
% \pgfplotslistselect
% \pgfplotslistcheckempty
% \pgfplotslistforeach
%
% @see \pgfplotsapplist a "real" list with O(1) pushback, but limited application.
% @see \pgfplotsapplistX a preasymptotical fast list to accumulate elements.
% @see \pgfplotsapplistXX an optimized version of \pgfplotsapplistX.
%
% 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/>.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\ifpgfplotslistempty


\newif\ifpgfplots@loop@CONTINUE
\newif\ifpgfplotslist@is@backslash@terminated

% Creates a new, empty list.
\long\def\pgfplotslistnewempty#1{\let#1=\pgfutil@empty}

% Creates a new list with an abirtrary number of elements.
% Arguments:
% #1: the list's name (a macro name)
% #2: the elements in the form 
%     first\\second\\third\\ ...\\
%     like in tabular with one column.
%     You can also use comma-separated lists
%     first,second,third
%
% Example:
% 1.
%  \pgfplotslistnew\foolist{First Element\\Second Element\\Third Element\\}
%   WARNING: do NOT forget the final '\\'!
%
% 2.
% \pgfplotslistnew\foolist{First Element,Second Element,Third Element}
%
% Use braces '{}' to use '\\' or ',' as arguments.
%
\long\def\pgfplotslistnew#1#2{%
	\pgfplotslistnewempty{#1}%
	\long\def\pgfplotslistnew@impl@rest{#2}%
	\pgfplotslist@check@backslash@list #2\\\pgfplotslist@EOI
	\ifpgfplotslist@is@backslash@terminated
	\else
		\expandafter\def\expandafter\pgfplotslistnew@impl@rest\expandafter{\pgfplotslistnew@impl@rest,}%
	\fi
	\pgfutil@loop
	\ifx\pgfplotslistnew@impl@rest\pgfutil@empty
		\pgfplots@loop@CONTINUEfalse
	\else
		\pgfplots@loop@CONTINUEtrue
	\fi
	\ifpgfplots@loop@CONTINUE
		\ifpgfplotslist@is@backslash@terminated
			\expandafter\pgfplotslistnew@impl\pgfplotslistnew@impl@rest\tolist#1\relax
		\else
			\expandafter\pgfplotslistnew@impl@comma\pgfplotslistnew@impl@rest\tolist#1\relax
		\fi
	\pgfutil@repeat
}
\def\pgfplotslist@EOI{\pgfplotslist@EOI}

\def\pgfplotslist@check@backslash@list#1\\#2\pgfplotslist@EOI{%
	\def\pgfplotslist@loc@TMPa{#2}%
	\ifx\pgfplotslist@loc@TMPa\pgfutil@empty
		\pgfplotslist@is@backslash@terminatedfalse
	\else
		\pgfplotslist@is@backslash@terminatedtrue
	\fi
}%

% Copies list #1 to list #2.
\def\pgfplotslistcopy#1\to#2{\let#2=#1}


% helper macro for \pgfplotslistnew
\long\def\pgfplotslistnew@impl#1\\#2\tolist#3{%
	\pgfplotslistpushback#1\to#3\relax%
	\def\pgfplotslistnew@impl@rest{#2}%
}
\long\def\pgfplotslistnew@impl@comma#1,#2\tolist#3{%
	\pgfplotslistpushback#1\to#3\relax%
	\def\pgfplotslistnew@impl@rest{#2}%
}


% #1: the item to prepend
% #2: the list as macro name
% Example:
% \pgfplotslistpushfront Next first Element\to\foolist
\long\def\pgfplotslistpushfront#1\to#2{%
	\t@pgfplots@toka={\pgfpl@@{#1}}%
	\t@pgfplots@tokb=\expandafter{#2}%
  	\edef#2{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
}

% Assembles a low-level list item representation into the token
% register #2.
\def\pgfplotslist@assembleentry#1\into#2{%
	#2={\pgfpl@@{#1}}%
}

% #1: the item to append
% #2: the list as macro name
% Example:
% \pgfplotslistpushback Next last element\to\foolist
\long\def\pgfplotslistpushback#1\to#2{%
	\t@pgfplots@toka={\pgfpl@@{#1}}%
	\ifx#2\pgfutil@empty
		\t@pgfplots@tokb={}%
	\else
		\t@pgfplots@tokb=\expandafter{#2}%
	\fi
 	\edef#2{\the\t@pgfplots@tokb\the\t@pgfplots@toka}%
}
% Adds '#1' to the GLOBAL list '#2'.
\long\def\pgfplotslistpushbackglobal#1\to#2{%
	\t@pgfplots@toka={\pgfpl@@{#1}}%
	\ifx#2\pgfutil@empty
		\t@pgfplots@tokb={}%
	\else
		\t@pgfplots@tokb=\expandafter{#2}%
	\fi
 	\xdef#2{\the\t@pgfplots@tokb\the\t@pgfplots@toka}%
}

% Concatenates two lists #2 and #3 into #1
% Example:
% \pgfplotslistconcat\result=\foolist&\bar
\long\def\pgfplotslistconcat#1=#2&#3{%
	\t@pgfplots@toka=\expandafter{#2}%
	\t@pgfplots@tokb=\expandafter{#3}%
	\edef#1{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
}

% implements #2 := pop_front(#1)
% Example:
% \pgfplotslistpopfront\foolist\to\poppedfirstelem
\long\def\pgfplotslistpopfront#1\to#2{%
	\pgfplotslistcheckempty#1\relax
	\ifpgfplotslistempty
		\pgfplots@error{ERROR: \string\pgfplotslistpopfront\ from \string#1\ although list is EMPTY}%
		\let#2=\pgfutil@empty
	\else
		\expandafter\pgfplotslistpopfront@impl#1\pgfplotslistpopfront@macronames#1#2%
	\fi
}
\long\def\pgfplotslistfront#1\to#2{%
	\pgfplotslistcheckempty#1\relax
	\ifpgfplotslistempty
		\pgfplots@error{ERROR: \string\pgfplotslistfront\ from \string#1\ although list is EMPTY}%
		\let#2=\pgfutil@empty
	\else
		\expandafter\pgfplotslistfront@impl#1\pgfplotslistpopfront@macronames#2%
	\fi
}

% implementation helper for listpopfront
\long\def\pgfplotslistpopfront@impl\pgfpl@@#1#2\pgfplotslistpopfront@macronames#3#4{%
	\def#4{#1}%
	\def#3{#2}%
}
\long\def\pgfplotslistfront@impl\pgfpl@@#1#2\pgfplotslistpopfront@macronames#3{%
	\def#3{#1}%
}

% Counts the number of elements in list #1, storing it into the count
% register #2.
% Example:
% \pgfplotslistsize\foo\to{\count0}%
% \the\count0
\long\def\pgfplotslistsize#1\to#2{%
	#2=0%
	\long\def\pgfpl@@##1{\advance#2 by 1 }%
	#1%
}

% Returns the #1th element of list #2 into macro #3
% Arguments:
% #1: a count 0,...,N-1 where N is the list size.
%     You may specify a number of a count.
% #2: a list
% #3: a macro name
% Example:
%   Element 0:
%   \pgfplotslistselect0\of\foo\to\elem
%   \elem
%   Element \count1:
%   \pgfplotslistselect\count1\of\foo\to\elem
\def\pgfplotslistselect#1\of#2\to#3{%
	\global\def\pgfplotslistselect@tmp{\def#3{}\pgfplots@error{The requested list entry with index #1 of \string#2 is too large; this list has not enough elements.}}%
	\pgfplotslistselect@{#1}\of{#2}\to{#3}%
}
\def\pgfplotslistselect@#1\of#2\to#3{%
	\begingroup
	\count0=#1\relax
 	\long\def\pgfpl@@##1{%
		\advance\count0 by-1\relax
		\ifnum\count0=-1\relax
			\global\def\pgfplotslistselect@tmp{\def#3{##1}}%
		\fi%
	}%
	#2%
	\endgroup
	\pgfplotslistselect@tmp
}
\def\pgfplotslistselectorempty#1\of#2\to#3{%
	\global\def\pgfplotslistselect@tmp{\def#3{}}%
	\pgfplotslistselect@{#1}\of{#2}\to{#3}%
}

% Sets the boolean \ifpgfplotslistempty depending on whether list #1 is empty
% or not.
% Example:
%
% \pgfplotslistcheckempty\foolist
% \ifpgfplotslistempty
%    List foolist is empty!
% \else
%    List is not empty.
% \fi
\def\pgfplotslistcheckempty#1{%
	\ifx#1\pgfutil@empty
		\pgfplotslistemptytrue
	\else
		\ifx#1\relax
			\pgfplots@warning{WARNING: possible logic error in script code: the command \string\pgfplotslistcheckempty{\string#1} encountered an undefined argument.}%
			\pgfplotslistemptytrue
		\else
			\pgfplotslistemptyfalse
		\fi
	\fi
}


% Iterates through each list element, names it #2 and calls code #3.
% Example:
% \pgfplotslistnew\foolist{Eins\\Zwei\\Drei\\}%
% \pgfplotslistforeach\foolist\as\foo{Element \foo\par}%
% results in
%  Element Eins
%  Element Zwei
%  Element Drei
% Each single element will be grouped with TeX groups.
\long\def\pgfplotslistforeach#1\as#2#3{%
	\begingroup
	\long\def\pgfpl@@##1{\def#2{##1}%
		\begingroup #3\endgroup}%
	#1\relax
	\endgroup
}

\newif\ifpgfplotslistforeachungroupedrunning

% The same but without groups around #3.
\long\def\pgfplotslistforeachungrouped#1\as#2#3{%
	\ifpgfplotslistforeachungroupedrunning
		\pgfplots@error{Sorry, you can't nest pgfplotslistforeachungrouped.}%
	\else
		\pgfplotslistforeachungroupedrunningtrue
		\long\def\pgfpl@@##1{\def#2{##1}#3}%
		#1\relax
		\pgfplotslistforeachungroupedrunningfalse
	\fi
}