Debian Codegraph @

texmf-dist/tex/generic/pgfplots/liststructure/pgfplotsliststructure.code.tex

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %
3 % This is a helper package with an elementary list datastructure.
4 %
5 % Its implementation is based on Knuth's list macros in "The TeXbook".
6 %
7 % It features a convenient set of list macros, but it is not fast.
8 % In fact, every elementary operation requires time O(N), so working
9 % with this list easily leads to O(N^2) runtime.
10 %
11 % The following macros are supplied:
12 %
13 % \pgfplotslistnewempty
14 % \pgfplotslistnew
15 % \pgfplotslistcopy
16 % \pgfplotslistpopfront
17 % \pgfplotslistfront
18 % \pgfplotslistpushback
19 % \pgfplotslistpushfront
20 % \pgfplotslistsize
21 % \pgfplotslistselect
22 % \pgfplotslistset
23 % \pgfplotslistcheckempty
24 % \pgfplotslistforeach
25 %
26 % @see \pgfplotsapplist a "real" list with O(1) pushback, but limited application.
27 % @see \pgfplotsapplistX a preasymptotical fast list to accumulate elements.
28 % @see \pgfplotsapplistXX an optimized version of \pgfplotsapplistX.
29 %
30 % Copyright 2007/2008 by Christian Feuersänger.
31 %
32 % This program is free software: you can redistribute it and/or modify
33 % it under the terms of the GNU General Public License as published by
34 % the Free Software Foundation, either version 3 of the License, or
35 % (at your option) any later version.
36 %
37 % This program is distributed in the hope that it will be useful,
38 % but WITHOUT ANY WARRANTY; without even the implied warranty of
39 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 % GNU General Public License for more details.
41 %
42 % You should have received a copy of the GNU General Public License
43 % along with this program. If not, see <http://www.gnu.org/licenses/>.
44 %
45 %
46 %
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48
49 \newif\ifpgfplotslistempty
50
51 % Low-level IMPLEMENTATION NOTE
52 % the list is stored in the form
53 % '\pgfpl@@{<first>}\pgfpl@@{<second>}\pgfpl@@{<third>}....\pgfpl@@{<last>}'
54
55 \newif\ifpgfplots@loop@CONTINUE
56 \newif\ifpgfplotslist@is@backslash@terminated
57
58 % Creates a new, empty list.
59 \long\def\pgfplotslistnewempty#1{\let#1=\pgfutil@empty}
60
61 % Creates a new list with an abirtrary number of elements.
62 % Arguments:
63 % #1: the list's name (a macro name)
64 % #2: the elements in the form
65 % first\\second\\third\\ ...\\
66 % like in tabular with one column.
67 % You can also use comma-separated lists
68 % first,second,third
69 %
70 % Example:
71 % 1.
72 % \pgfplotslistnew\foolist{First Element\\Second Element\\Third Element\\}
73 % WARNING: do NOT forget the final '\\'!
74 %
75 % 2.
76 % \pgfplotslistnew\foolist{First Element,Second Element,Third Element}
77 %
78 % Use braces '{}' to use '\\' or ',' as arguments.
79 %
80 \long\def\pgfplotslistnew#1#2{%
81 \pgfkeys@spdef\pgfplotslist@loc@TMPa{#2}%
82 \def\pgfplotslist@loc@TMPb{\pgfplotslistnew@{#1}}%
83 \expandafter\pgfplotslist@loc@TMPb\expandafter{\pgfplotslist@loc@TMPa}%
84 }
85 \long\def\pgfplotslistnew@#1#2{%
86 \pgfplotslist@check@backslash@list #2\\\pgfplotslist@EOI
87 \ifpgfplotslist@is@backslash@terminated
88 \pgfplotslistnew@backslash@{#1}{#2}%
89 \else
90 \pgfplots@foreach@to@list{#2}\to#1%
91 \fi
92 }
93 % Is this here *ever* used!? I guess not.
94 \long\def\pgfplotslistnew@backslash#1#2{%
95 \pgfplotslistnewempty{#1}%
96 \pgfplotslist@check@backslash@list #2\\\pgfplotslist@EOI
97 \ifpgfplotslist@is@backslash@terminated
98 \long\def\pgfplotslistnew@impl@rest{#2\pgfplotslist@EOI\\}%
99 \def\pgfplotslistnew@backslash@loop{\pgfplotslistnew@impl{#1}}%
100 \else
101 \def\pgfplotslistnew@backslash@loop{\pgfplotslistnew@impl@comma{#1}}%
102 \long\def\pgfplotslistnew@impl@rest{#2\pgfplotslist@EOI,}%
103 \fi
104 \expandafter\pgfplotslistnew@backslash@loop\pgfplotslistnew@impl@rest
105 }%
106 \long\def\pgfplotslistnew@backslash@#1#2{%
107 \pgfplotslistnewempty{#1}%
108 \def\pgfplotslistnew@backslash@loop{\pgfplotslistnew@impl{#1}}%
109 \pgfplotslistnew@backslash@loop#2\pgfplotslist@EOI\\%
110 }%
111
112 % helper macro for \pgfplotslistnew
113 \long\def\pgfplotslistnew@impl#1#2\\{%
114 \def\pgfplotslist@loc@TMPa{#2}%
115 \ifx\pgfplotslist@loc@TMPa\pgfplotslist@EOI
116 \else
117 \pgfplotslistpushback{#2}\to#1\relax%
118 \expandafter\pgfplotslistnew@backslash@loop
119 \fi
120 }
121 \long\def\pgfplotslistnew@impl@comma#1#2,{%
122 \def\pgfplotslist@loc@TMPa{#2}%
123 \ifx\pgfplotslist@loc@TMPa\pgfplotslist@EOI
124 \else
125 \pgfplotslistpushback{#2}\to#1\relax%
126 \expandafter\pgfplotslistnew@backslash@loop
127 \fi
128 }
129 \def\pgfplotslist@EOI{\pgfplotslist@EOI}
130 \def\pgfplotslist@backslashsep{\\}
131
132 \long\def\pgfplotslist@check@backslash@list#1\\#2\pgfplotslist@EOI{%
133 \def\pgfplotslist@loc@TMPx{#2}%
134 \ifx\pgfplotslist@loc@TMPx\pgfutil@empty
135 \pgfplotslist@is@backslash@terminatedfalse
136 \let\pgfplotslist@check@backslash@list@next=\relax
137 \else
138 \def\pgfplotslist@loc@TMPy{ }%
139 \ifx\pgfplotslist@loc@TMPx\pgfplotslist@loc@TMPy
140 \pgfplotslist@is@backslash@terminatedfalse
141 \let\pgfplotslist@check@backslash@list@next=\relax
142 \else
143 \ifx\pgfplotslist@loc@TMPx\pgfplotslist@backslashsep
144 % ok. The list is something like 'a\\b\\' so the
145 % complete input is
146 % 'a\\b\\ \\ \EOI'
147 % we have iterated long enough to find only the '\\'
148 % right before the \EOI. It is thus backslash
149 % terminated.
150 \pgfplotslist@is@backslash@terminatedtrue
151 \let\pgfplotslist@check@backslash@list@next=\relax
152 \else
153 % Iterate. We want to check the last list element.
154 \long\def\pgfplotslist@check@backslash@list@next{\pgfplotslist@check@backslash@list#2\pgfplotslist@EOI}%
155 \fi
156 \fi
157 \fi
158 \pgfplotslist@check@backslash@list@next
159 }%
160
161 % Copies list #1 to list #2.
162 \def\pgfplotslistcopy#1\to#2{\let#2=#1}
163
164
165
166
167 % #1: the item to prepend
168 % #2: the list as macro name
169 % Example:
170 % \pgfplotslistpushfront Next first Element\to\foolist
171 \long\def\pgfplotslistpushfront#1\to#2{%
172 \t@pgfplots@toka={\pgfpl@@{#1}}%
173 \t@pgfplots@tokb=\expandafter{#2}%
174 \edef#2{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
175 }
176 \long\def\pgfplotslistpushfrontglobal#1\to#2{%
177 \t@pgfplots@toka={\pgfpl@@{#1}}%
178 \t@pgfplots@tokb=\expandafter{#2}%
179 \xdef#2{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
180 }
181
182 % Assembles a low-level list item representation into the token
183 % register #2.
184 \long\def\pgfplotslist@assembleentry#1\into#2{%
185 #2={\pgfpl@@{#1}}%
186 }
187
188 % #1: the item to append
189 % #2: the list as macro name
190 % Example:
191 % \pgfplotslistpushback Next last element\to\foolist
192 \long\def\pgfplotslistpushback#1\to#2{%
193 \t@pgfplots@toka={\pgfpl@@{#1}}%
194 \ifx#2\pgfutil@empty
195 \t@pgfplots@tokb={}%
196 \else
197 \t@pgfplots@tokb=\expandafter{#2}%
198 \fi
199 \edef#2{\the\t@pgfplots@tokb\the\t@pgfplots@toka}%
200 }
201 % Adds '#1' to the GLOBAL list '#2'.
202 \long\def\pgfplotslistpushbackglobal#1\to#2{%
203 \t@pgfplots@toka={\pgfpl@@{#1}}%
204 \ifx#2\pgfutil@empty
205 \t@pgfplots@tokb={}%
206 \else
207 \t@pgfplots@tokb=\expandafter{#2}%
208 \fi
209 \xdef#2{\the\t@pgfplots@tokb\the\t@pgfplots@toka}%
210 }
211
212 % Concatenates two lists #2 and #3 into #1
213 % Example:
214 % \pgfplotslistconcat\result=\foolist&\bar
215 \long\def\pgfplotslistconcat#1=#2&#3{%
216 \t@pgfplots@toka=\expandafter{#2}%
217 \t@pgfplots@tokb=\expandafter{#3}%
218 \edef#1{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
219 }
220
221 % implements #2 := pop_front(#1)
222 % Example:
223 % \pgfplotslistpopfront\foolist\to\poppedfirstelem
224 \long\def\pgfplotslistpopfront#1\to#2{%
225 \pgfplotslistcheckempty#1\relax
226 \ifpgfplotslistempty
227 \pgfplotsthrow{no such element}{#1}{\string\pgfplotslistpopfront\ from \string#1\ although list is EMPTY}\pgfeov%
228 \else
229 \expandafter\pgfplotslistpopfront@impl#1\pgfplotslistpopfront@macronames#1#2%
230 \fi
231 }
232 \long\def\pgfplotslistfront#1\to#2{%
233 \pgfplotslistcheckempty#1\relax
234 \ifpgfplotslistempty
235 \pgfplotsthrow{no such element}{#2}{\string\pgfplotslistfront\ from \string#1\ although list is EMPTY}\pgfeov%
236 \else
237 \expandafter\pgfplotslistfront@impl#1\pgfplotslistpopfront@macronames#2%
238 \fi
239 }
240
241 % implementation helper for listpopfront
242 \long\def\pgfplotslistpopfront@impl\pgfpl@@#1#2\pgfplotslistpopfront@macronames#3#4{%
243 \def#4{#1}%
244 \def#3{#2}%
245 }
246 \long\def\pgfplotslistfront@impl\pgfpl@@#1#2\pgfplotslistpopfront@macronames#3{%
247 \def#3{#1}%
248 }
249
250 % Counts the number of elements in list #1, storing it into the count
251 % register #2.
252 % Example:
253 % \pgfplotslistsize\foo\to{\count0}%
254 % \the\count0
255 \long\def\pgfplotslistsize#1\to#2{%
256 #2=0%
257 \long\def\pgfpl@@##1{\advance#2 by 1 }%
258 #1%
259 }
260
261 % Returns the #1th element of list #2 into macro #3
262 % Arguments:
263 % #1: a count 0,...,N-1 where N is the list size.
264 % You may specify a number of a count.
265 % #2: a list
266 % #3: a macro name
267 % Example:
268 % Element 0:
269 % \pgfplotslistselect0\of\foo\to\elem
270 % \elem
271 % Element \count1:
272 % \pgfplotslistselect\count1\of\foo\to\elem
273 \long\def\pgfplotslistselect#1\of#2\to#3{%
274 \global\def\pgfplotslistselect@tmp{\pgfplotsthrow{no such element}{#3}{The requested list entry with index #1 of \string#2 is too large; this list has not enough elements.}\pgfeov}%
275 \pgfplotslistselect@{#1}\of{#2}\to{#3}%
276 }
277 \long\def\pgfplotslistselect@#1\of#2\to#3{%
278 \begingroup
279 \count0=#1\relax
280 \long\def\pgfpl@@##1{%
281 \advance\count0 by-1\relax
282 \ifnum\count0=-1\relax
283 \global\def\pgfplotslistselect@tmp{\def#3{##1}}%
284 \fi%
285 }%
286 #2%
287 \endgroup
288 \pgfplotslistselect@tmp
289 }
290
291 % Selects a list entry. If it does not exist, '#' will be empty.
292 \long\def\pgfplotslistselectorempty#1\of#2\to#3{%
293 \global\def\pgfplotslistselect@tmp{\def#3{}}%
294 \pgfplotslistselect@{#1}\of{#2}\to{#3}%
295 }
296
297 % Changes the element at index '#1' of list '#2' to the content '#3'.
298 %
299 % This operation has quadratic (!) time in the worst case.
300 \long\def\pgfplotslistset#1\of#2\to#3{%
301 \ifcase#1\relax
302 % change first:
303 \def\pgfplotslistset@\pgfpl@@##1##2\relax{\def#2{\pgfpl@@{#3}##2}}%
304 \expandafter\pgfplotslistset@#2\relax
305 \or
306 % change second:
307 \def\pgfplotslistset@\pgfpl@@##1\pgfpl@@##2##3\relax{\def#2{\pgfpl@@{##1}\pgfpl@@{#3}##2}}%
308 \expandafter\pgfplotslistset@#2\relax
309 \or
310 \def\pgfplotslistset@\pgfpl@@##1\pgfpl@@##2\pgfpl@@##3##4\relax{\def#2{\pgfpl@@{##1}\pgfpl@@{##2}\pgfpl@@{#3}##3}}%
311 \expandafter\pgfplotslistset@#2\relax
312 \or
313 \def\pgfplotslistset@\pgfpl@@##1\pgfpl@@##2\pgfpl@@##3\pgfpl@@##4##5\relax{\def#2{\pgfpl@@{##1}\pgfpl@@{##2}\pgfpl@@{##3}\pgfpl@@{#3}##4}}%
314 \expandafter\pgfplotslistset@#2\relax
315 \else
316 \begingroup
317 \global\pgfplotslistnewempty\pgfplotslistnewempty\pgfplotslist@glob@TMPa
318 \let\pgfpl@@=\pgfutil@empty % make sure it is not \relax.
319 \count0=#1\relax
320 \count1=0
321 \def\pgfplotslistset@newelem{#3}%
322 \expandafter\pgfplotslistset@loop#2\relax
323 \ifnum\count1 =\count0
324 \else
325 \pgfplotsthrow{no such element}{\pgfplots@loc@TMPa}{\string\pgfplotslistset{\the\count0} failed because there are only \the\count1\space elements in the list. Use pushback to resize it.}\pgfeov%
326 \global\let\pgfplotslist@glob@TMPa=\pgfplots@loc@TMPa
327 \fi
328 \endgroup
329 \let#3=\pgfplotslist@glob@TMPa
330 \global\let\pgfplotslist@glob@TMPa=\relax
331 \fi
332 }%
333
334 \def\pgfplotslistset@loop{%
335 \pgfutil@ifnextchar\relax{%
336 \pgfplotslistset@loop@break
337 }{%
338 \pgfplotslistset@loop@next
339 }%
340 }
341 \def\pgfplotslistset@loop@break#1\relax{%
342 % re-append remaining elements:
343 \expandafter\gdef\expandafter\pgfplotslist@glob@TMPa\expandafter{\pgfplotslist@glob@TMPa#1}
344 }%
345 \def\pgfplotslistset@loop@next\pgfpl@@#1{%
346 \ifnum\count1 =\count0
347 % Ah - we found the element to replace!
348 \expandafter\pgfplotslistpushbackglobal\expandafter{\pgfplotslistset@newelem}\to\pgfplotslist@glob@TMPa
349 \expandafter\pgfplotslistset@loop@break
350 \else
351 \pgfplotslistpushbackglobal{#1}\to\pgfplotslist@glob@TMPa
352 \advance\count1 by1
353 \expandafter\pgfplotslistset@loop
354 \fi
355 }%
356
357 % Sets the boolean \ifpgfplotslistempty depending on whether list #1 is empty
358 % or not.
359 % Example:
360 %
361 % \pgfplotslistcheckempty\foolist
362 % \ifpgfplotslistempty
363 % List foolist is empty!
364 % \else
365 % List is not empty.
366 % \fi
367 \def\pgfplotslistcheckempty#1{%
368 \ifx#1\pgfutil@empty
369 \pgfplotslistemptytrue
370 \else
371 \ifx#1\relax
372 \pgfplots@warning{WARNING: possible logic error in script code: the command \string\pgfplotslistcheckempty{\string#1} encountered an undefined argument.}%
373 \pgfplotslistemptytrue
374 \else
375 \pgfplotslistemptyfalse
376 \fi
377 \fi
378 }
379
380
381 % Iterates through each list element, names it #2 and calls code #3.
382 % Example:
383 % \pgfplotslistnew\foolist{Eins\\Zwei\\Drei\\}%
384 % \pgfplotslistforeach\foolist\as\foo{Element \foo\par}%
385 % results in
386 % Element Eins
387 % Element Zwei
388 % Element Drei
389 % Each single element will be grouped with TeX groups.
390 \long\def\pgfplotslistforeach#1\as#2#3{%
391 \begingroup
392 \long\def\pgfpl@@##1{%
393 \t@pgfplots@tokc={##1}% this allows '#' inside of '##1'
394 \edef#2{\the\t@pgfplots@tokc}%
395 \begingroup #3\endgroup}%
396 #1\relax
397 \endgroup
398 }
399
400 % The same but without groups around #3.
401 %
402 % The list can be nested.
403 \long\def\pgfplotslistforeachungrouped#1\as#2#3{%
404 \t@pgfplots@tokb={{#2}{#3}}%
405 \t@pgfplots@tokc=\expandafter{#1}%
406 \edef\pgfplotslist@loc@TMPa{%
407 \noexpand\pgfplotslistforeachungrouped@
408 \the\t@pgfplots@tokb%
409 \the\t@pgfplots@tokc
410 \noexpand\pgfpl@@\noexpand\pgfplotslistforeachungrouped@EOI
411 }%
412 \pgfplotslist@loc@TMPa
413 }%
414
415 % Technical helper method which performs the loop.
416 % ASSUMPTION:
417 % \pgfplotslistforeachungrouped@
418 % {<macro name>}
419 % {<code to execute>}
420 % \pgfpl@@{<list elem>}\pgfpl@@{<list elem>}\pgfpl@@{<list elem>}\pgfpl@@\pgfplotslistforeachungrouped@EOI
421 %
422 % -> iterate through argument until \pgfplotslistforeachungrouped@EOI
423 % comes.
424 \long\def\pgfplotslistforeachungrouped@#1#2\pgfpl@@#3{%
425 \t@pgfplots@tokc={#3}% this allows '#' inside of '#3'
426 \edef\pgfplotslist@loc@TMPa{\the\t@pgfplots@tokc}%
427 \ifx\pgfplotslist@loc@TMPa\pgfplotslistforeachungrouped@EOI
428 % ok, terminate loop.
429 \let\pgfplotslistforeachungrouped@next=\relax
430 \else
431 % perform loop iteration ...
432 \let#1=\pgfplotslist@loc@TMPa
433 #2\relax
434 % and continue iterating.
435 \def\pgfplotslistforeachungrouped@next{\pgfplotslistforeachungrouped@{#1}{#2}}%
436 \fi
437 \pgfplotslistforeachungrouped@next
438 }%
439 \def\pgfplotslistforeachungrouped@EOI{\pgfplotslistforeachungrouped@EOI}% equals only itself in \ifx
440