| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% This is a helper package with an elementary array datastructure, |
| 4 |
% featuring O(1) index access and O(N) creation, deletion, copy. |
| 5 |
% |
| 6 |
% The following macros are supplied: |
| 7 |
% |
| 8 |
% \pgfplotsarraynewempty |
| 9 |
% \pgfplotsarraynew |
| 10 |
% \pgfplotsarrayresize |
| 11 |
% \pgfplotsarraycopy |
| 12 |
% \pgfplotsarraypushback |
| 13 |
% \pgfplotsarraysize |
| 14 |
% \pgfplotsarrayselect |
| 15 |
% \pgfplotsarrayset |
| 16 |
% \pgfplotsarrayletentry |
| 17 |
% \pgfplotsarraycheckempty |
| 18 |
% \pgfplotsarrayforeach |
| 19 |
% \pgfplotsarraysort |
| 20 |
% \pgfplotsarraybinarysearch |
| 21 |
% |
| 22 |
% and a subset also for global arrays: |
| 23 |
% \pgfplotsarraynewemptyglobal |
| 24 |
% \pgfplotsarrayresizeglobal |
| 25 |
% \pgfplotsarraysetglobal |
| 26 |
% \pgfplotsarrayletentryglobal |
| 27 |
% |
| 28 |
% Copyright 2007/2008 by Christian Feuersänger. |
| 29 |
% |
| 30 |
% This program is free software: you can redistribute it and/or modify |
| 31 |
% it under the terms of the GNU General Public License as published by |
| 32 |
% the Free Software Foundation, either version 3 of the License, or |
| 33 |
% (at your option) any later version. |
| 34 |
% |
| 35 |
% This program is distributed in the hope that it will be useful, |
| 36 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 |
% GNU General Public License for more details. |
| 39 |
% |
| 40 |
% You should have received a copy of the GNU General Public License |
| 41 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 42 |
% |
| 43 |
% |
| 44 |
% |
| 45 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 46 |
|
| 47 |
\newcount\c@pgfplotsarray@tmp |
| 48 |
\newif\ifpgfplotsarrayempty |
| 49 |
|
| 50 |
% Creates a new, empty array. |
| 51 |
\def\pgfplotsarraynewempty#1{% |
| 52 |
\pgfplotsarray@@def{#1@size}{0}% |
| 53 |
} |
| 54 |
\def\pgfplotsarraynewemptyglobal#1{% |
| 55 |
\expandafter\gdef\csname\string#1@size\endcsname{0}% |
| 56 |
} |
| 57 |
|
| 58 |
% resizes (truncates) array #1 to size #2 |
| 59 |
% |
| 60 |
% the elements won't be initialised. Use 'set' for each element. |
| 61 |
\def\pgfplotsarrayresize#1#2{% |
| 62 |
\c@pgfplotsarray@tmp=#2 |
| 63 |
\pgfplotsarray@@edef{#1@size}{\the\c@pgfplotsarray@tmp}% |
| 64 |
} |
| 65 |
\def\pgfplotsarrayresizeglobal#1#2{% |
| 66 |
\c@pgfplotsarray@tmp=#2 |
| 67 |
\expandafter\xdef\csname\string#1@size\endcsname{\the\c@pgfplotsarray@tmp}% |
| 68 |
} |
| 69 |
|
| 70 |
% Invokes code '#2' if the array named '#1' exists and '#3' if it does |
| 71 |
% not exist. |
| 72 |
\def\pgfplotsarrayifdefined#1#2#3{% |
| 73 |
\pgfutil@ifundefined{#1@size}{#3}{#2}% |
| 74 |
}% |
| 75 |
|
| 76 |
% Creates a new array with an abirtrary number of elements. |
| 77 |
% Arguments: |
| 78 |
% #1: the array's name (a macro name) |
| 79 |
% #2: the elements in one of the following forms |
| 80 |
% a) a '\\' terminated list: |
| 81 |
% first\\second\\third\\ ...\\ |
| 82 |
% like in tabular with one column. The final '\\' necessary! |
| 83 |
% b) a comma-separated list: |
| 84 |
% first,second,third,5,10,last |
| 85 |
% ATTENTION: arrays do not support the '...' syntax of tikz! |
| 86 |
% |
| 87 |
% Example: |
| 88 |
% \pgfplotsarraynew\fooarray{First Element\\Second Element\\Third Element\\} |
| 89 |
% \pgfplotsarraynew\fooarray{First Element,Second Element,Third Element} |
| 90 |
% \pgfplotsarraynew\fooarray{0,1,2,...,10} |
| 91 |
% |
| 92 |
% |
| 93 |
\long\def\pgfplotsarraynew#1#2{% |
| 94 |
\pgfplots@check@backwards@compatible@list@format #2\\\pgfplots@EOI |
| 95 |
\ifpgfplots@is@old@list@format |
| 96 |
\pgfplotsarraynew@backslash{#1}{#2}% |
| 97 |
\else |
| 98 |
\pgfplotsarraynew@commasep{#1}{#2}% |
| 99 |
\fi |
| 100 |
} |
| 101 |
|
| 102 |
% #1 the array's name (a macro name |
| 103 |
% #2 a list created by \pgfplotslistnew |
| 104 |
\def\pgfplotsarrayfrompgfplotslist#1#2{% |
| 105 |
\pgfplotsarraynewempty#1% |
| 106 |
\pgfplotslistforeachungrouped#2\as\pgfplotsarray@TMPa{% |
| 107 |
\expandafter\pgfplotsarraypushback\expandafter{\pgfplotsarray@TMPa}\to#1% |
| 108 |
}% |
| 109 |
}% |
| 110 |
|
| 111 |
\def\pgfplotsarraynew@backslash#1#2{% |
| 112 |
\pgfplotsarraynewempty{#1}% |
| 113 |
\long\def\pgfplotsarraynew@impl@rest{#2}% |
| 114 |
\pgfutil@loop |
| 115 |
\ifx\pgfplotsarraynew@impl@rest\pgfutil@empty |
| 116 |
\pgfplots@loop@CONTINUEfalse |
| 117 |
\else |
| 118 |
\pgfplots@loop@CONTINUEtrue |
| 119 |
\fi |
| 120 |
\ifpgfplots@loop@CONTINUE |
| 121 |
\expandafter\pgfplotsarraynew@impl\pgfplotsarraynew@impl@rest\toarray#1\relax |
| 122 |
\pgfutil@repeat |
| 123 |
}% |
| 124 |
|
| 125 |
% converts a comma-separated list (PGF foreach) to my internal list |
| 126 |
% structure. |
| 127 |
\long\def\pgfplotsarraynew@commasep#1#2{% |
| 128 |
\pgfplotsarraynewempty{#1}% |
| 129 |
\pgfplotsutilforeachcommasep{#2}\as\pgfplotsarraynew@elem{% |
| 130 |
\expandafter\pgfplotsarraypushback\expandafter{\pgfplotsarraynew@elem}\to#1% |
| 131 |
}% |
| 132 |
} |
| 133 |
% helper macro for \pgfplotsarraynew |
| 134 |
\long\def\pgfplotsarraynew@impl#1\\#2\toarray#3{% |
| 135 |
\pgfplotsarraypushback#1\to#3\relax% |
| 136 |
\def\pgfplotsarraynew@impl@rest{#2}% |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
\def\pgfplotsarray@@let#1=#2{% |
| 141 |
\def\pgfplotsarray@TMP@@{\expandafter\let\csname\string#1\endcsname}% |
| 142 |
\expandafter\pgfplotsarray@TMP@@\csname\string#2\endcsname |
| 143 |
}% |
| 144 |
|
| 145 |
\def\pgfplotsarray@@#1#2{% |
| 146 |
\expandafter#1\csname\string#2\endcsname |
| 147 |
}% |
| 148 |
\def\pgfplotsarray@@def#1{% |
| 149 |
\expandafter\def\csname\string#1\endcsname |
| 150 |
}% |
| 151 |
\def\pgfplotsarray@@edef#1{% |
| 152 |
\expandafter\edef\csname\string#1\endcsname |
| 153 |
}% |
| 154 |
|
| 155 |
\def\pgfplotsarray@@getsizeto#1#2{% |
| 156 |
\pgfplotsarraysize{#1}\to\c@pgfplotsarray@tmp |
| 157 |
\edef#2{\the\c@pgfplotsarray@tmp}% |
| 158 |
}% |
| 159 |
|
| 160 |
% Copies array #1 to array #2. |
| 161 |
\def\pgfplotsarraycopy#1\to#2{% |
| 162 |
\pgfplotsarray@@let{#2@size}={#1@size}% |
| 163 |
\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}% |
| 164 |
\c@pgfplotsarray@tmp=0 |
| 165 |
\pgfutil@loop |
| 166 |
\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP |
| 167 |
\pgfplotsarray@@let{#2@\the\c@pgfplotsarray@tmp}={#1@\the\c@pgfplotsarray@tmp}% |
| 168 |
\advance\c@pgfplotsarray@tmp by1 |
| 169 |
\pgfutil@repeat |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
% #1: the item to append |
| 175 |
% #2: the array as macro name |
| 176 |
% Example: |
| 177 |
% \pgfplotsarraypushback Next last element\to\fooarray |
| 178 |
\long\def\pgfplotsarraypushback#1\to#2{% |
| 179 |
\pgfplotsarraysize{#2}\to\c@pgfplotsarray@tmp |
| 180 |
\pgfplotsarray@@def{#2@\the\c@pgfplotsarray@tmp}{#1}% |
| 181 |
\advance\c@pgfplotsarray@tmp by1 |
| 182 |
\pgfplotsarray@@edef{#2@size}{\the\c@pgfplotsarray@tmp}% |
| 183 |
} |
| 184 |
\long\def\pgfplotsarraypushbackglobal#1\to#2{% |
| 185 |
\pgfplotsarraysize{#2}\to\c@pgfplotsarray@tmp |
| 186 |
\pgfplotsarray@@\gdef{#2@\the\c@pgfplotsarray@tmp}{#1}% |
| 187 |
\advance\c@pgfplotsarray@tmp by1 |
| 188 |
\pgfplotsarray@@\xdef{#2@size}{\the\c@pgfplotsarray@tmp}% |
| 189 |
} |
| 190 |
|
| 191 |
% Counts the number of elements in array #1, storing it into the count |
| 192 |
% register #2. |
| 193 |
% Example: |
| 194 |
% \pgfplotsarraysize\foo\to{\count0}% |
| 195 |
% \the\count0 |
| 196 |
\long\def\pgfplotsarraysize#1\to#2{% |
| 197 |
#2=\csname\string#1@size\endcsname\relax |
| 198 |
} |
| 199 |
\long\def\pgfplotsarraysizetomacro#1\to#2{% |
| 200 |
\expandafter\let\expandafter#2\csname\string#1@size\endcsname |
| 201 |
} |
| 202 |
|
| 203 |
% expands to the size of array #1. |
| 204 |
\def\pgfplotsarraysizeof#1{\csname\string#1@size\endcsname}% |
| 205 |
|
| 206 |
|
| 207 |
% Returns the #1th element of array #2 into macro #3 |
| 208 |
% Arguments: |
| 209 |
% #1: a count 0,...,N-1 where N is the array size. |
| 210 |
% You may specify a number of a count. |
| 211 |
% #2: a array |
| 212 |
% #3: a macro name |
| 213 |
% Example: |
| 214 |
% Element 0: |
| 215 |
% \pgfplotsarrayselect0\of\foo\to\elem |
| 216 |
% \elem |
| 217 |
% Element \count1: |
| 218 |
% \pgfplotsarrayselect\count1\of\foo\to\elem |
| 219 |
\def\pgfplotsarrayselect#1\of#2\to#3{% |
| 220 |
\c@pgfplotsarray@tmp=#1\relax |
| 221 |
\expandafter\let\expandafter#3\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname% |
| 222 |
\ifx#3\relax |
| 223 |
\pgfplotsthrow{no such element}{#1}{No such element: \string\pgfplotsarrayselect\the\c@pgfplotsarray@tmp\string\of{\string#2}}\pgfeov% |
| 224 |
\fi |
| 225 |
} |
| 226 |
|
| 227 |
% Expands to the value at index #1 of array #2. |
| 228 |
% #1: a number (not a register) |
| 229 |
% #2: an array |
| 230 |
\def\pgfplotsarrayvalueofelem#1\of#2{\csname\string#2@#1\endcsname}% |
| 231 |
|
| 232 |
% Sets element '#1' of array '#2' to '#3'. |
| 233 |
\def\pgfplotsarrayset#1\of#2\to#3{% |
| 234 |
\c@pgfplotsarray@tmp=#1\relax |
| 235 |
\pgfutil@namedef{\string#2@\the\c@pgfplotsarray@tmp}{#3}% |
| 236 |
} |
| 237 |
\def\pgfplotsarraysetglobal#1\of#2\to#3{% |
| 238 |
\c@pgfplotsarray@tmp=#1\relax |
| 239 |
\expandafter\gdef\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname{#3}% |
| 240 |
} |
| 241 |
\long\def\pgfplotsarrayletentry#1\of#2=#3{% |
| 242 |
\c@pgfplotsarray@tmp=#1\relax |
| 243 |
\expandafter\let\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname=#3\relax |
| 244 |
} |
| 245 |
\long\def\pgfplotsarrayletentryglobal#1\of#2=#3{% |
| 246 |
\c@pgfplotsarray@tmp=#1\relax |
| 247 |
\expandafter\global\expandafter\let\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname=#3\relax |
| 248 |
} |
| 249 |
|
| 250 |
% Defines \pgfplotsretval to be a text-representation of the array. |
| 251 |
% It will contain \t to separate cells. |
| 252 |
% |
| 253 |
\def\pgfplotsarraytotext#1{% |
| 254 |
\begingroup |
| 255 |
\pgfplotsapplistXnewempty\pgfplotsretval@ |
| 256 |
\pgfplotsarrayforeachungrouped#1\as\entry{% |
| 257 |
\pgfmathfloatparsenumber\entry |
| 258 |
\pgfmathfloattosci\pgfmathresult |
| 259 |
\edef\entry{\pgfmathresult\noexpand\t}% |
| 260 |
\expandafter\pgfplotsapplistXpushback\entry\to\pgfplotsretval@ |
| 261 |
}% |
| 262 |
\pgfplotsapplistXlet\pgfplotsretval=\pgfplotsretval@ |
| 263 |
\pgfmath@smuggleone\pgfplotsretval |
| 264 |
\endgroup |
| 265 |
} |
| 266 |
|
| 267 |
% Sets the boolean \ifpgfplotsarrayempty depending on whether array #1 is empty |
| 268 |
% or not. |
| 269 |
% Example: |
| 270 |
% |
| 271 |
% \pgfplotsarraycheckempty\fooarray |
| 272 |
% \ifpgfplotsarrayempty |
| 273 |
% List fooarray is empty! |
| 274 |
% \else |
| 275 |
% List is not empty. |
| 276 |
% \fi |
| 277 |
\def\pgfplotsarraycheckempty#1{% |
| 278 |
\ifnum\csname\string#1@size\endcsname=0 |
| 279 |
\pgfplotsarrayemptytrue |
| 280 |
\else |
| 281 |
\pgfplotsarrayemptyfalse |
| 282 |
\fi |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
% Iterates through each array element, names it #2 and calls code #3. |
| 287 |
% Example: |
| 288 |
% \pgfplotsarraynew\fooarray{Eins\\Zwei\\Drei\\}% |
| 289 |
% \pgfplotsarrayforeach\fooarray\as\foo{Element \foo\par}% |
| 290 |
% results in |
| 291 |
% Element Eins |
| 292 |
% Element Zwei |
| 293 |
% Element Drei |
| 294 |
% Each single element will be grouped with TeX groups. |
| 295 |
% |
| 296 |
% During the loop, \pgfplotsarrayforeachindex expands to the current index. |
| 297 |
\long\def\pgfplotsarrayforeach#1\as#2#3{% |
| 298 |
% FIXME : add support for \breakforeach |
| 299 |
\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}% |
| 300 |
\c@pgfplotsarray@tmp=0\relax |
| 301 |
\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}% |
| 302 |
\long\def\pgfplotsarray@TMPb{#3}% |
| 303 |
\pgfutil@loop |
| 304 |
\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP\relax |
| 305 |
\begingroup |
| 306 |
\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname% |
| 307 |
\pgfplotsarray@TMPb |
| 308 |
\endgroup |
| 309 |
\advance\c@pgfplotsarray@tmp by1 |
| 310 |
\pgfutil@repeat |
| 311 |
\let\pgfplotsarrayforeachindex=\relax% |
| 312 |
} |
| 313 |
|
| 314 |
% The same but without groups around #3. |
| 315 |
\long\def\pgfplotsarrayforeachungrouped#1\as#2#3{% |
| 316 |
\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}% |
| 317 |
\c@pgfplotsarray@tmp=0\relax |
| 318 |
\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}% |
| 319 |
\pgfutil@loop |
| 320 |
\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP\relax |
| 321 |
\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname% |
| 322 |
\begingroup |
| 323 |
\toks0={#3}% |
| 324 |
\xdef\pgfplotsarray@glob@TMP{% |
| 325 |
\the\toks0 % |
| 326 |
% restore loop state: |
| 327 |
\c@pgfplotsarray@tmp=\the\c@pgfplotsarray@tmp\space |
| 328 |
\noexpand\def\noexpand\pgfplotsarray@TMP{\pgfplotsarray@TMP}% |
| 329 |
}% |
| 330 |
\endgroup |
| 331 |
\pgfplotsarray@glob@TMP |
| 332 |
\advance\c@pgfplotsarray@tmp by1 |
| 333 |
\pgfutil@repeat |
| 334 |
\let\pgfplotsarrayforeachindex=\relax% |
| 335 |
} |
| 336 |
|
| 337 |
\long\def\pgfplotsarrayforeachreversed#1\as#2#3{% |
| 338 |
\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}% |
| 339 |
\c@pgfplotsarray@tmp=\pgfplotsarray@TMP\relax |
| 340 |
\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}% |
| 341 |
\long\def\pgfplotsarray@TMPb{#3}% |
| 342 |
\pgfutil@loop |
| 343 |
\ifnum\c@pgfplotsarray@tmp>0 % |
| 344 |
\advance\c@pgfplotsarray@tmp by-1 |
| 345 |
\begingroup |
| 346 |
\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname% |
| 347 |
\pgfplotsarray@TMPb |
| 348 |
\endgroup |
| 349 |
\pgfutil@repeat |
| 350 |
\let\pgfplotsarrayforeachindex=\relax% |
| 351 |
} |
| 352 |
|
| 353 |
\long\def\pgfplotsarrayforeachreversedungrouped#1\as#2#3{% |
| 354 |
\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}% |
| 355 |
\c@pgfplotsarray@tmp=\pgfplotsarray@TMP\relax |
| 356 |
\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}% |
| 357 |
\long\def\pgfplotsarray@TMPb{#3}% |
| 358 |
\pgfutil@loop |
| 359 |
\ifnum\c@pgfplotsarray@tmp>0 % |
| 360 |
\advance\c@pgfplotsarray@tmp by-1 |
| 361 |
\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname% |
| 362 |
\pgfplotsarray@TMPb |
| 363 |
\pgfutil@repeat |
| 364 |
\let\pgfplotsarrayforeachindex=\relax% |
| 365 |
} |
| 366 |
|
| 367 |
\pgfkeys{% |
| 368 |
/pgfplots/array/unscope pre/.code={% |
| 369 |
% That's more or less efficient (although some runtime factors |
| 370 |
% could be saved). |
| 371 |
\c@pgfplotsarraysort@i=0 |
| 372 |
\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{#1}\relax |
| 373 |
\pgfutil@loop |
| 374 |
\ifnum\c@pgfplotsarraysort@i<\c@pgfplotsarray@tmp |
| 375 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@i}\of{#1}\to\pgfplots@loc@TMPa |
| 376 |
\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa |
| 377 |
\advance\c@pgfplotsarraysort@i by1 |
| 378 |
\pgfutil@repeat |
| 379 |
},% |
| 380 |
/pgfplots/array/unscope post/.code={% |
| 381 |
% copy from global temp -> #1 |
| 382 |
\c@pgfplotsarray@tmp=0 |
| 383 |
\pgfplotsarraysizetomacro{#1}\to\pgfplots@loc@TMPb |
| 384 |
\pgfutil@loop |
| 385 |
\ifnum\c@pgfplotsarray@tmp<\pgfplots@loc@TMPb |
| 386 |
\pgfplotsarrayselect{\c@pgfplotsarray@tmp}\of{pgfparraytmp}\to\pgfplots@loc@TMPa |
| 387 |
\pgfplotsarrayletentry\c@pgfplotsarray@tmp\of{#1}=\pgfplots@loc@TMPa |
| 388 |
\advance\c@pgfplotsarray@tmp by1 |
| 389 |
\pgfutil@repeat |
| 390 |
},% |
| 391 |
}% |
| 392 |
|
| 393 |
% Sorts array '#1' using N log N time. |
| 394 |
% |
| 395 |
% The sort key is provided as argument in |
| 396 |
% /pgfplots/iflessthan/.code args={#1#2#3#4}, see docs in |
| 397 |
% pgfplotsutil.code.tex. |
| 398 |
% |
| 399 |
% Remarks: |
| 400 |
% - the sorting algorithm is a sub-optimal mergesort currently - |
| 401 |
% but it has runtime N log N. |
| 402 |
% - As usual, it is very difficult to move the final result out of the |
| 403 |
% current scope. The initial configuration allocates a *global* copy of #1 |
| 404 |
% to carry results out of one TeX group. This means that memory will NEVER be freed. |
| 405 |
% I don't see a general N log N alternative for doing so (short of discarding |
| 406 |
% the scopes for local groups). |
| 407 |
% BUT: |
| 408 |
% you can redefine the code key pair |
| 409 |
% |/pgfplots/array/unscope pre| |
| 410 |
% and |
| 411 |
% |/pgfplots/array/unscope post| |
| 412 |
% to define your own routine which may benefit from special needs of |
| 413 |
% your application. The |... pre| command is invoked JUST before the |
| 414 |
% \endgroup and the |... post| JUST after the |\endgroup|. |
| 415 |
% |
| 416 |
% |
| 417 |
% EXAMPLE: |
| 418 |
%-------------------------------------------------- |
| 419 |
% \pgfplotsarraynewempty\testarray |
| 420 |
% \pgfplotsarraypushback503\to\testarray |
| 421 |
% \pgfplotsarraypushback087\to\testarray |
| 422 |
% \pgfplotsarraypushback512\to\testarray |
| 423 |
% \pgfplotsarraypushback061\to\testarray |
| 424 |
% \pgfplotsarraypushback908\to\testarray |
| 425 |
% \pgfplotsarraypushback170\to\testarray |
| 426 |
% \pgfplotsarraypushback897\to\testarray |
| 427 |
% \pgfplotsarraypushback275\to\testarray |
| 428 |
% \pgfplotsarraypushback653\to\testarray |
| 429 |
% \pgfplotsarraypushback426\to\testarray |
| 430 |
% \pgfplotsarraypushback154\to\testarray |
| 431 |
% \pgfplotsarraypushback509\to\testarray |
| 432 |
% \pgfplotsarraypushback612\to\testarray |
| 433 |
% \pgfplotsarraypushback677\to\testarray |
| 434 |
% \pgfplotsarraypushback765\to\testarray |
| 435 |
% \pgfplotsarraypushback703\to\testarray |
| 436 |
% |
| 437 |
% Unsorted: |
| 438 |
% |
| 439 |
% [\pgfplotsarrayforeach\testarray\as\elem{\elem\space}] |
| 440 |
% |
| 441 |
% \pgfplotsarraysort\testarray |
| 442 |
% |
| 443 |
% sorted: |
| 444 |
% |
| 445 |
% [\pgfplotsarrayforeach\testarray\as\elem{\elem\space}] |
| 446 |
% |
| 447 |
%-------------------------------------------------- |
| 448 |
% |
| 449 |
% @see \pgfplotsutilsortthree |
| 450 |
% @see \pgfplotsutilsortfour |
| 451 |
\def\pgfplotsarraysort{% |
| 452 |
\pgfutil@ifnextchar[{\pgfplotsarraysort@opt}{\pgfplotsarraysort@opt[]}% |
| 453 |
}% |
| 454 |
\def\pgfplotsarraysort@opt[#1]#2{% |
| 455 |
% I admit, the implementation is more-or less copy-paste of an |
| 456 |
% experiment I had done at university. It does its job, but it is |
| 457 |
% certainly not optimal (that means: it is slow). |
| 458 |
\begingroup |
| 459 |
\pgfqkeys{/pgfplots/array}{#1}% |
| 460 |
\pgfkeysifdefined{/pgfplots/array/iflessthan/.@cmd}{% |
| 461 |
\pgfplots@warning{Warning: /pgfplots/array/iflessthan/.@cmd is deprecated. Please use /pgfplots/iflessthan/.@cmd instead.}% |
| 462 |
\pgfkeysgetvalue{/pgfplots/array/iflessthan/.@cmd}\pgfplotsarraysort@iflt |
| 463 |
}{% |
| 464 |
\pgfkeysgetvalue{/pgfplots/iflessthan/.@cmd}\pgfplotsarraysort@iflt |
| 465 |
}% |
| 466 |
\pgfplotsarraysize{#2}\to\c@pgfplotsarray@tmp |
| 467 |
\pgfplotsarrayresizeglobal{pgfparraytmp}{\the\c@pgfplotsarray@tmp}% |
| 468 |
\countdef\c@pgfplotsarraysort@m=0 |
| 469 |
\countdef\c@pgfplotsarraysort@k=1 |
| 470 |
\countdef\c@pgfplotsarraysort@i=2 |
| 471 |
\countdef\c@pgfplotsarraysort@l=3 |
| 472 |
\countdef\c@pgfplotsarraysort@j=4 |
| 473 |
\countdef\c@pgfplotsarraysort@q=5 |
| 474 |
\def\pgfplotsarray@mergesort@{\pgfplotsarray@mergesort{#2}}% |
| 475 |
\def\pgfplotsarray@mergesort@recurse@{\pgfplotsarray@mergesort@recurse{#2}}% |
| 476 |
\edef\pgfplotsarray@mergesort@@{{0}{\the\c@pgfplotsarray@tmp}}% |
| 477 |
\expandafter\pgfplotsarray@mergesort@\pgfplotsarray@mergesort@@ |
| 478 |
% |
| 479 |
% copy the complete, sorted result outside of the current scope. |
| 480 |
\pgfkeysvalueof{/pgfplots/array/unscope pre/.@cmd}#2\pgfeov |
| 481 |
\endgroup |
| 482 |
\pgfkeysvalueof{/pgfplots/array/unscope post/.@cmd}#2\pgfeov |
| 483 |
}% |
| 484 |
|
| 485 |
% #1: array name, |
| 486 |
% #2: start offset (as string number) |
| 487 |
% #3: number of elems (as string number) |
| 488 |
\def\pgfplotsarray@mergesort#1#2#3{% |
| 489 |
\ifnum#3<2 |
| 490 |
% ready! |
| 491 |
\else |
| 492 |
\c@pgfplotsarraysort@m=#3\relax |
| 493 |
\divide\c@pgfplotsarraysort@m by2 |
| 494 |
\c@pgfplotsarraysort@q=#3\relax |
| 495 |
\advance\c@pgfplotsarraysort@q by-\c@pgfplotsarraysort@m |
| 496 |
\c@pgfplotsarraysort@i=\c@pgfplotsarraysort@m |
| 497 |
\advance\c@pgfplotsarraysort@i by#2\relax |
| 498 |
\edef\pgfplotsarray@mergesort@@{% |
| 499 |
{#2}{\the\c@pgfplotsarraysort@m}% start1, N_1 |
| 500 |
{\the\c@pgfplotsarraysort@i}{\the\c@pgfplotsarraysort@q}% start2, N_2 |
| 501 |
}% |
| 502 |
\expandafter\pgfplotsarray@mergesort@recurse@\pgfplotsarray@mergesort@@ |
| 503 |
% |
| 504 |
% Merge: |
| 505 |
% restore 'm': |
| 506 |
\c@pgfplotsarraysort@m=#3\relax |
| 507 |
\divide\c@pgfplotsarraysort@m by2 |
| 508 |
\c@pgfplotsarraysort@q=#3 |
| 509 |
\advance\c@pgfplotsarraysort@q by-\c@pgfplotsarraysort@m |
| 510 |
\c@pgfplotsarraysort@k=0 |
| 511 |
\c@pgfplotsarraysort@l=0 |
| 512 |
\c@pgfplotsarraysort@i=0 |
| 513 |
% |
| 514 |
% direct comparisons for merge: |
| 515 |
\pgfutil@loop |
| 516 |
\pgfplots@loop@CONTINUEtrue |
| 517 |
\ifnum\c@pgfplotsarraysort@k<\c@pgfplotsarraysort@m |
| 518 |
\ifnum\c@pgfplotsarraysort@l<\c@pgfplotsarraysort@q |
| 519 |
\else |
| 520 |
\pgfplots@loop@CONTINUEfalse |
| 521 |
\fi |
| 522 |
\else |
| 523 |
\pgfplots@loop@CONTINUEfalse |
| 524 |
\fi |
| 525 |
\ifpgfplots@loop@CONTINUE |
| 526 |
\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@m |
| 527 |
\advance\c@pgfplotsarraysort@j by\c@pgfplotsarraysort@l |
| 528 |
\advance\c@pgfplotsarraysort@j by#2 |
| 529 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplotsarray@x@mpl |
| 530 |
\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@k |
| 531 |
\advance\c@pgfplotsarraysort@j by#2 |
| 532 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplotsarray@x@k |
| 533 |
\begingroup |
| 534 |
\pgfplotsarraysort@iflt |
| 535 |
{\pgfplotsarray@x@mpl}% |
| 536 |
{\pgfplotsarray@x@k}% |
| 537 |
{% x[m+l] < x[k] |
| 538 |
\aftergroup\pgfplots@loc@tmptrue |
| 539 |
}% |
| 540 |
{% x[k] <= x[m+l] |
| 541 |
\aftergroup\pgfplots@loc@tmpfalse |
| 542 |
}% |
| 543 |
\pgfeov |
| 544 |
\endgroup |
| 545 |
\ifpgfplots@loc@tmp |
| 546 |
\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplotsarray@x@mpl |
| 547 |
\advance\c@pgfplotsarraysort@i by1 |
| 548 |
\advance\c@pgfplotsarraysort@l by1 |
| 549 |
\else |
| 550 |
\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplotsarray@x@k |
| 551 |
\advance\c@pgfplotsarraysort@i by1 |
| 552 |
\advance\c@pgfplotsarraysort@k by1 |
| 553 |
\fi |
| 554 |
\pgfutil@repeat |
| 555 |
% |
| 556 |
% append: |
| 557 |
\pgfutil@loop |
| 558 |
\ifnum\c@pgfplotsarraysort@k<\c@pgfplotsarraysort@m |
| 559 |
\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@k |
| 560 |
\advance\c@pgfplotsarraysort@j by#2 |
| 561 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplots@loc@TMPa |
| 562 |
\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa |
| 563 |
\advance\c@pgfplotsarraysort@i by1 |
| 564 |
\advance\c@pgfplotsarraysort@k by1 |
| 565 |
\pgfutil@repeat |
| 566 |
\pgfutil@loop |
| 567 |
\ifnum\c@pgfplotsarraysort@l<\c@pgfplotsarraysort@q |
| 568 |
\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@m |
| 569 |
\advance\c@pgfplotsarraysort@j by\c@pgfplotsarraysort@l |
| 570 |
\advance\c@pgfplotsarraysort@j by#2 |
| 571 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplots@loc@TMPa |
| 572 |
\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa |
| 573 |
\advance\c@pgfplotsarraysort@i by1 |
| 574 |
\advance\c@pgfplotsarraysort@l by1 |
| 575 |
\pgfutil@repeat |
| 576 |
% |
| 577 |
% copy back from temporary to '#1' |
| 578 |
\c@pgfplotsarraysort@i=0 |
| 579 |
\c@pgfplotsarraysort@k=#3 |
| 580 |
\pgfutil@loop |
| 581 |
\ifnum\c@pgfplotsarraysort@i<\c@pgfplotsarraysort@k |
| 582 |
\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@i |
| 583 |
\advance\c@pgfplotsarraysort@j by#2 |
| 584 |
\pgfplotsarrayselect{\c@pgfplotsarraysort@i}\of{pgfparraytmp}\to\pgfplots@loc@TMPa |
| 585 |
\pgfplotsarrayletentry\c@pgfplotsarraysort@j\of{#1}=\pgfplots@loc@TMPa |
| 586 |
\advance\c@pgfplotsarraysort@i by1 |
| 587 |
\pgfutil@repeat |
| 588 |
%\pgfplotsarraysort@DEBUGTEST{#1}{after mergesort(start=#2,n=#3):}% |
| 589 |
\fi |
| 590 |
}% |
| 591 |
\long\def\pgfplotsarraysort@DEBUGTEST#1#2{% |
| 592 |
\par#2\par |
| 593 |
[\pgfplotsarrayforeach{#1}\as\elem{\elem\space}]\par% |
| 594 |
} |
| 595 |
% #1: array name |
| 596 |
% #2: start1 |
| 597 |
% #3: N1 |
| 598 |
% #4: start2 |
| 599 |
% #5: N2 |
| 600 |
\def\pgfplotsarray@mergesort@recurse#1#2#3#4#5{% |
| 601 |
\pgfplotsarray@mergesort{#1}{#2}{#3}% |
| 602 |
\pgfplotsarray@mergesort{#1}{#4}{#5}% |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
% A simple, ungrouped insert sort algorithm for small arrays. |
| 607 |
% |
| 608 |
% for i := 1; i < N; ++i do |
| 609 |
% begin |
| 610 |
% value := A[i]; |
| 611 |
% j := i - 1; |
| 612 |
% done := false; |
| 613 |
% repeat |
| 614 |
% if A[j] > value then |
| 615 |
% begin |
| 616 |
% A[j + 1] := A[j]; |
| 617 |
% j := j - 1; |
| 618 |
% if j < 0 then |
| 619 |
% done := true; |
| 620 |
% end |
| 621 |
% else |
| 622 |
% done := true; |
| 623 |
% until done; |
| 624 |
% A[j + 1] := value; |
| 625 |
% end; |
| 626 |
\def\pgfplotsarrayinsertionsort#1{% |
| 627 |
\pgfkeysgetvalue{/pgfplots/iflessthan/.@cmd}\pgfplotsarraysort@iflt |
| 628 |
\pgfplotsarraysizetomacro#1\to\pgfplotsarrayinsertionsort@N |
| 629 |
\c@pgf@countd=1 |
| 630 |
\pgfplotsarrayinsertionsort@{#1}% |
| 631 |
}% |
| 632 |
\def\pgfplotsarrayinsertionsort@#1{% |
| 633 |
\ifnum\c@pgf@countd<\pgfplotsarrayinsertionsort@N\relax |
| 634 |
\pgfplotsarrayselect\c@pgf@countd\of#1\to\pgfplotsarrayinsertionsort@v |
| 635 |
% |
| 636 |
\c@pgf@countb=\c@pgf@countd |
| 637 |
\advance\c@pgf@countb by-1 |
| 638 |
% |
| 639 |
\pgfplotsarrayinsertionsort@@{#1}% |
| 640 |
\advance\c@pgf@countb by1 |
| 641 |
\pgfplotsarrayletentry\c@pgf@countb\of#1=\pgfplotsarrayinsertionsort@v |
| 642 |
% |
| 643 |
\advance\c@pgf@countd by1 |
| 644 |
\expandafter\pgfplotsarrayinsertionsort@\expandafter#1% |
| 645 |
\fi |
| 646 |
} |
| 647 |
\def\pgfplotsarrayinsertionsort@@#1{% |
| 648 |
\pgfplotsarrayselect\c@pgf@countb\of#1\to\pgfplotsarrayinsertionsort@j |
| 649 |
\pgfplotsarraysort@iflt{\pgfplotsarrayinsertionsort@v}{\pgfplotsarrayinsertionsort@j} |
| 650 |
{% |
| 651 |
\advance\c@pgf@countb by1 |
| 652 |
\pgfplotsarrayletentry\c@pgf@countb\of#1=\pgfplotsarrayinsertionsort@j |
| 653 |
\advance\c@pgf@countb by-2 |
| 654 |
\ifnum\c@pgf@countb<0 |
| 655 |
\let\pgfplotsarrayinsertionsort@@next\relax% |
| 656 |
\else |
| 657 |
\def\pgfplotsarrayinsertionsort@@next{\pgfplotsarrayinsertionsort@@#1}% |
| 658 |
\fi |
| 659 |
}{% |
| 660 |
\let\pgfplotsarrayinsertionsort@@next\relax% |
| 661 |
}\pgfeov% |
| 662 |
\pgfplotsarrayinsertionsort@@next |
| 663 |
}% |
| 664 |
|
| 665 |
% applies a binary search for value '#2' on array '#1', starting with |
| 666 |
% index '#3' (inclusive) and ending in index '#4' (exclusive). |
| 667 |
% |
| 668 |
% Returns: \pgfplotsretval, the index of the search key, if it is |
| 669 |
% contained in the array; otherwise, (-(insertion point) - 1). The |
| 670 |
% insertion point is defined as the point at which the key would be |
| 671 |
% inserted into the array: the index of the first element greater than |
| 672 |
% the key, or a.length if all elements in the array are less than the |
| 673 |
% specified key. Note that this guarantees that the return value will |
| 674 |
% be >= 0 if and only if the key is found. |
| 675 |
% |
| 676 |
\def\pgfplotsarraybinarysearch#1#2#3#4{% |
| 677 |
\begingroup |
| 678 |
\edef\targetvalue{#2}% |
| 679 |
\pgfkeysgetvalue{/pgfplots/iflessthan/.@cmd}\pgfplotsarray@iflt |
| 680 |
\let\mid=\c@pgf@counta |
| 681 |
\let\left=\c@pgf@countb |
| 682 |
\let\right=\c@pgf@countc |
| 683 |
\left=#3\relax |
| 684 |
\right=#4\relax |
| 685 |
\advance\right by-1 % make it inclusive |
| 686 |
\let\pgfplotsretval\pgfutil@empty |
| 687 |
% |
| 688 |
\pgfutil@loop |
| 689 |
\ifnum\left>\right |
| 690 |
\pgfplots@loop@CONTINUEfalse |
| 691 |
\else |
| 692 |
\pgfplots@loop@CONTINUEtrue |
| 693 |
\fi |
| 694 |
\ifpgfplots@loop@CONTINUE |
| 695 |
\mid=\right |
| 696 |
\advance\mid by -\left |
| 697 |
\divide\mid by2 % |
| 698 |
\advance\mid by\left |
| 699 |
% |
| 700 |
\pgfplotsarrayselect\mid\of#1\to\midvalue |
| 701 |
\pgfplotsarray@iflt{\targetvalue}{\midvalue}{% |
| 702 |
\right=\mid |
| 703 |
\advance\right by-1 % |
| 704 |
}{% |
| 705 |
\pgfplotsarray@iflt{\midvalue}{\targetvalue}{% |
| 706 |
\left=\mid |
| 707 |
\advance\left by1 % |
| 708 |
}{% |
| 709 |
% found it! abort loop! |
| 710 |
\left=\right |
| 711 |
\advance\left by1 % |
| 712 |
\edef\pgfplotsretval{\the\mid}% |
| 713 |
}% |
| 714 |
\pgfeov% |
| 715 |
}% |
| 716 |
\pgfeov% |
| 717 |
\pgfutil@repeat |
| 718 |
% |
| 719 |
\ifx\pgfplotsretval\pgfutil@empty |
| 720 |
\advance\left by1 % |
| 721 |
\left=-\left |
| 722 |
\edef\pgfplotsretval{\the\left}% |
| 723 |
\fi |
| 724 |
\pgfmath@smuggleone\pgfplotsretval |
| 725 |
\endgroup |
| 726 |
}% |