summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/liststructure/pgfplotsarray.code.tex
blob: 7464dc43876b1c2a0a7775bddb63f02fe5d0d753 (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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is a helper package with an elementary array datastructure,
% featuring O(1) index access and O(N) creation, deletion, copy.
%
% The following macros are supplied:
%
% \pgfplotsarraynewempty
% \pgfplotsarraynew
% \pgfplotsarrayresize
% \pgfplotsarraycopy
% \pgfplotsarraypushback
% \pgfplotsarraysize
% \pgfplotsarrayselect
% \pgfplotsarrayset
% \pgfplotsarrayletentry
% \pgfplotsarraycheckempty
% \pgfplotsarrayforeach
% \pgfplotsarraysort
%
% and a subset also for global arrays:
% \pgfplotsarraynewemptyglobal
% \pgfplotsarrayresizeglobal
% \pgfplotsarraysetglobal
% \pgfplotsarrayletentryglobal
%
% 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/>.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcount\c@pgfplotsarray@tmp
\newif\ifpgfplotsarrayempty

% Creates a new, empty array.
\def\pgfplotsarraynewempty#1{%
	\pgfplotsarray@@def{#1@size}{0}%
}
\def\pgfplotsarraynewemptyglobal#1{%
	\expandafter\gdef\csname\string#1@size\endcsname{0}%
}

% resizes (truncates) array #1 to size #2
%
% the elements won't be initialised. Use 'set' for each element.
\def\pgfplotsarrayresize#1#2{%
	\pgfplotsarray@@edef{#1@size}{#2}%
}
\def\pgfplotsarrayresizeglobal#1#2{%
	\expandafter\xdef\csname\string#1@size\endcsname{#2}%
}

% Invokes code '#2' if the array named '#1' exists and '#3' if it does
% not exist.
\def\pgfplotsarrayifdefined#1#2#3{%
	\pgfutil@ifundefined{#1@size}{#3}{#2}%
}%

% Creates a new array with an abirtrary number of elements.
% Arguments:
% #1: the array's name (a macro name)
% #2: the elements in one of the following forms
% 	a) a '\\' terminated list:
%     first\\second\\third\\ ...\\
%     like in tabular with one column. The final '\\' necessary!
%   b) a comma-separated list:
%     first,second,third,5,10,last
%     ATTENTION: arrays do not support the '...' syntax of tikz!
%
% Example:
% \pgfplotsarraynew\fooarray{First Element\\Second Element\\Third Element\\}
% \pgfplotsarraynew\fooarray{First Element,Second Element,Third Element}
% \pgfplotsarraynew\fooarray{0,1,2,...,10}
%
%
\long\def\pgfplotsarraynew#1#2{%
	\pgfplots@check@backwards@compatible@list@format #2\\\pgfplots@EOI
	\ifpgfplots@is@old@list@format
		\pgfplotsarraynew@backslash{#1}{#2}%
	\else
		\pgfplotsarraynew@commasep{#1}{#2}%
	\fi
}

\def\pgfplotsarraynew@backslash#1#2{%
	\pgfplotsarraynewempty{#1}%
	\long\def\pgfplotsarraynew@impl@rest{#2}%
	\pgfutil@loop
	\ifx\pgfplotsarraynew@impl@rest\pgfutil@empty
		\pgfplots@loop@CONTINUEfalse
	\else
		\pgfplots@loop@CONTINUEtrue
	\fi
	\ifpgfplots@loop@CONTINUE
		\expandafter\pgfplotsarraynew@impl\pgfplotsarraynew@impl@rest\toarray#1\relax
	\pgfutil@repeat
}%

% converts a comma-separated list (PGF foreach)  to my internal list
% structure.
\long\def\pgfplotsarraynew@commasep#1#2{%
	\pgfplotsarraynewempty{#1}%
	\pgfplotsutilforeachcommasep{#2}\as\pgfplotsarraynew@elem{%
		\expandafter\pgfplotsarraypushback\expandafter{\pgfplotsarraynew@elem}\to#1%
	}%
}
% helper macro for \pgfplotsarraynew
\long\def\pgfplotsarraynew@impl#1\\#2\toarray#3{%
	\pgfplotsarraypushback#1\to#3\relax%
	\def\pgfplotsarraynew@impl@rest{#2}%
}


\def\pgfplotsarray@@let#1=#2{%
	\def\pgfplotsarray@TMP@@{\expandafter\let\csname\string#1\endcsname}%
	\expandafter\pgfplotsarray@TMP@@\expandafter=\csname\string#2\endcsname
}%

\def\pgfplotsarray@@def#1{%
	\expandafter\def\csname\string#1\endcsname
}%
\def\pgfplotsarray@@edef#1{%
	\expandafter\edef\csname\string#1\endcsname
}%

\def\pgfplotsarray@@getsizeto#1#2{%
	\pgfplotsarraysize{#1}\to\c@pgfplotsarray@tmp
	\edef#2{\the\c@pgfplotsarray@tmp}%
}%

% Copies array #1 to array #2.
\def\pgfplotsarraycopy#1\to#2{%
	\pgfplotsarray@@let{#2@size}={#1@size}%
	\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}%
	\c@pgfplotsarray@tmp=0
	\pgfutil@loop
	\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP
	\pgfplotsarray@@let{#2@\the\c@pgfplotsarray@tmp}={#1@\the\c@pgfplotsarray@tmp}%
	\advance\c@pgfplotsarray@tmp by1
	\pgfutil@repeat
}



% #1: the item to append
% #2: the array as macro name
% Example:
% \pgfplotsarraypushback Next last element\to\fooarray
\long\def\pgfplotsarraypushback#1\to#2{%
	\pgfplotsarraysize{#2}\to\c@pgfplotsarray@tmp
	\pgfplotsarray@@def{#2@\the\c@pgfplotsarray@tmp}{#1}%
	\advance\c@pgfplotsarray@tmp by1
	\pgfplotsarray@@edef{#2@size}{\the\c@pgfplotsarray@tmp}%
}


% Counts the number of elements in array #1, storing it into the count
% register #2.
% Example:
% \pgfplotsarraysize\foo\to{\count0}%
% \the\count0
\long\def\pgfplotsarraysize#1\to#2{%
	#2=\csname\string#1@size\endcsname\relax
}
\long\def\pgfplotsarraysizetomacro#1\to#2{%
	\expandafter\let\expandafter#2\csname\string#1@size\endcsname
}

% expands to the size of array #1.
\def\pgfplotsarraysizeof#1{\csname\string#1@size\endcsname}%


% Returns the #1th element of array #2 into macro #3
% Arguments:
% #1: a count 0,...,N-1 where N is the array size.
%     You may specify a number of a count.
% #2: a array
% #3: a macro name
% Example:
%   Element 0:
%   \pgfplotsarrayselect0\of\foo\to\elem
%   \elem
%   Element \count1:
%   \pgfplotsarrayselect\count1\of\foo\to\elem
\def\pgfplotsarrayselect#1\of#2\to#3{%
	\c@pgfplotsarray@tmp=#1\relax
	\expandafter\let\expandafter#3\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname%
}

% Expands to the value at index #1 of array #2.
% #1: a number (not a register)
% #2: an array
\def\pgfplotsarrayvalueofelem#1\of#2{\csname\string#2@#1\endcsname}%

% Sets element '#1' of array '#2' to '#3'.
\def\pgfplotsarrayset#1\of#2\to#3{%
	\c@pgfplotsarray@tmp=#1\relax
	\pgfutil@namedef{\string#2@\the\c@pgfplotsarray@tmp}{#3}%
}
\def\pgfplotsarraysetglobal#1\of#2\to#3{%
	\c@pgfplotsarray@tmp=#1\relax
	\expandafter\gdef\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname{#3}%
}
\long\def\pgfplotsarrayletentry#1\of#2=#3{%
	\c@pgfplotsarray@tmp=#1\relax
	\expandafter\let\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname=#3\relax
}
\long\def\pgfplotsarrayletentryglobal#1\of#2=#3{%
	\c@pgfplotsarray@tmp=#1\relax
	\expandafter\global\expandafter\let\csname\string#2@\the\c@pgfplotsarray@tmp\endcsname=#3\relax
}

% Sets the boolean \ifpgfplotsarrayempty depending on whether array #1 is empty
% or not.
% Example:
%
% \pgfplotsarraycheckempty\fooarray
% \ifpgfplotsarrayempty
%    List fooarray is empty!
% \else
%    List is not empty.
% \fi
\def\pgfplotsarraycheckempty#1{%
	\ifnum\csname\string#1@size\endcsname=0
		\pgfplotsarrayemptytrue
	\else
		\pgfplotsarrayemptyfalse
	\fi
}


% Iterates through each array element, names it #2 and calls code #3.
% Example:
% \pgfplotsarraynew\fooarray{Eins\\Zwei\\Drei\\}%
% \pgfplotsarrayforeach\fooarray\as\foo{Element \foo\par}%
% results in
%  Element Eins
%  Element Zwei
%  Element Drei
% Each single element will be grouped with TeX groups.
%
% During the loop, \pgfplotsarrayforeachindex expands to the current index.
\long\def\pgfplotsarrayforeach#1\as#2#3{%
	\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}%
	\c@pgfplotsarray@tmp=0\relax
	\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}%
	\long\def\pgfplotsarray@TMPb{#3}%
	\pgfutil@loop
		\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP\relax
		\begingroup
		\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname%
		\pgfplotsarray@TMPb
		\endgroup
		\advance\c@pgfplotsarray@tmp by1
	\pgfutil@repeat
	\let\pgfplotsarrayforeachindex=\relax%
}

% The same but without groups around #3.
\long\def\pgfplotsarrayforeachungrouped#1\as#2#3{%
	\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}%
	\c@pgfplotsarray@tmp=0\relax
	\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}%
	\long\def\pgfplotsarray@TMPb{#3}%
	\pgfutil@loop
		\ifnum\c@pgfplotsarray@tmp<\pgfplotsarray@TMP\relax
		\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname%
		\pgfplotsarray@TMPb
		\advance\c@pgfplotsarray@tmp by1
	\pgfutil@repeat
	\let\pgfplotsarrayforeachindex=\relax%
}

\long\def\pgfplotsarrayforeachreversed#1\as#2#3{%
	\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}%
	\c@pgfplotsarray@tmp=\pgfplotsarray@TMP\relax
	\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}%
	\long\def\pgfplotsarray@TMPb{#3}%
	\pgfutil@loop
		\ifnum\c@pgfplotsarray@tmp>0 %
		\advance\c@pgfplotsarray@tmp by-1
		\begingroup
		\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname%
		\pgfplotsarray@TMPb
		\endgroup
	\pgfutil@repeat
	\let\pgfplotsarrayforeachindex=\relax%
}

\long\def\pgfplotsarrayforeachreversedungrouped#1\as#2#3{%
	\pgfplotsarray@@getsizeto{#1}{\pgfplotsarray@TMP}%
	\c@pgfplotsarray@tmp=\pgfplotsarray@TMP\relax
	\def\pgfplotsarrayforeachindex{\the\c@pgfplotsarray@tmp}%
	\long\def\pgfplotsarray@TMPb{#3}%
	\pgfutil@loop
		\ifnum\c@pgfplotsarray@tmp>0 %
		\advance\c@pgfplotsarray@tmp by-1
		\expandafter\let\expandafter#2\expandafter=\csname\string#1@\the\c@pgfplotsarray@tmp\endcsname%
		\pgfplotsarray@TMPb
	\pgfutil@repeat
	\let\pgfplotsarrayforeachindex=\relax%
}

% #1: arg1 (possibly not fully expanded)
% #2: arg2 (possibly not fully expanded)
% #3: code to invoke in case arg1<arg2
% #4: code to invoke in case arg1>=arg2
\pgfkeys{%
	/pgfplots/array/iflessthan/.code args={#1#2#3#4}{\ifnum#1<#2\relax#3\else#4\fi},%
	/pgfplots/array/unscope pre/.code={%
		% That's more or less efficient (although some runtime factors
		% could be saved).
		\c@pgfplotsarraysort@i=0
		\c@pgfplotsarray@tmp=\pgfplotsarraysizeof{#1}\relax
		\pgfutil@loop
		\ifnum\c@pgfplotsarraysort@i<\c@pgfplotsarray@tmp
			\pgfplotsarrayselect{\c@pgfplotsarraysort@i}\of{#1}\to\pgfplots@loc@TMPa
			\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa
			\advance\c@pgfplotsarraysort@i by1
		\pgfutil@repeat
	},%
	/pgfplots/array/unscope post/.code={%
		% copy from global temp -> #1
		\c@pgfplotsarray@tmp=0
		\pgfplotsarraysizetomacro{#1}\to\pgfplots@loc@TMPb
		\pgfutil@loop
		\ifnum\c@pgfplotsarray@tmp<\pgfplots@loc@TMPb
			\pgfplotsarrayselect{\c@pgfplotsarray@tmp}\of{pgfparraytmp}\to\pgfplots@loc@TMPa
			\pgfplotsarrayletentry\c@pgfplotsarray@tmp\of{#1}=\pgfplots@loc@TMPa
			\advance\c@pgfplotsarray@tmp by1
		\pgfutil@repeat
	},%
}%

% Sorts array '#1' using N log N time.
% 
% The sort key is provided as argument in
% /pgfplots/array/iflessthan/.code args={#1#2#3#4}, see above.
%
% Remarks:
% - the sorting algorithm is a sub-optimal mergesort currently -
% but it has runtime N log N. 
% - As usual, it is very difficult to move the final result out of the
%   current scope. The initial configuration allocates a *global* copy of #1 
%   to carry results out of one TeX group. This means that memory will NEVER be freed.
%   I don't see a general N log N alternative for doing so (short of discarding
%   the scopes for local groups).
%   BUT:
%   you can redefine the code key pair 
%     |/pgfplots/array/unscope pre|
%   and
%     |/pgfplots/array/unscope post|
%   to define your own routine which may benefit from special needs of
%   your application. The |... pre| command is invoked JUST before the
%   \endgroup and the |... post| JUST after the |\endgroup|.
%
%
% EXAMPLE:
%--------------------------------------------------
% \pgfplotsarraynewempty\testarray
% \pgfplotsarraypushback503\to\testarray
% \pgfplotsarraypushback087\to\testarray
% \pgfplotsarraypushback512\to\testarray
% \pgfplotsarraypushback061\to\testarray
% \pgfplotsarraypushback908\to\testarray
% \pgfplotsarraypushback170\to\testarray
% \pgfplotsarraypushback897\to\testarray
% \pgfplotsarraypushback275\to\testarray
% \pgfplotsarraypushback653\to\testarray
% \pgfplotsarraypushback426\to\testarray
% \pgfplotsarraypushback154\to\testarray
% \pgfplotsarraypushback509\to\testarray
% \pgfplotsarraypushback612\to\testarray
% \pgfplotsarraypushback677\to\testarray
% \pgfplotsarraypushback765\to\testarray
% \pgfplotsarraypushback703\to\testarray
% 
% Unsorted: 
% 
% [\pgfplotsarrayforeach\testarray\as\elem{\elem\space}]
% 
% \pgfplotsarraysort\testarray
% 
% sorted: 
% 
% [\pgfplotsarrayforeach\testarray\as\elem{\elem\space}]
% 
%-------------------------------------------------- 
\def\pgfplotsarraysort#1{%
	% I admit, the implementation is more-or less copy-paste of an
	% experiment I had done at university. It does its job, but it is
	% certainly not optimal.
	\begingroup
	\pgfkeysgetvalue{/pgfplots/array/iflessthan/.@cmd}\pgfplotsarraysort@iflt
	\pgfplotsarraysize{#1}\to\c@pgfplotsarray@tmp
	\pgfplotsarrayresizeglobal{pgfparraytmp}{\the\c@pgfplotsarray@tmp}%
	\countdef\c@pgfplotsarraysort@m=0
	\countdef\c@pgfplotsarraysort@k=1
	\countdef\c@pgfplotsarraysort@i=2
	\countdef\c@pgfplotsarraysort@l=3
	\countdef\c@pgfplotsarraysort@j=4
	\countdef\c@pgfplotsarraysort@q=5
	\def\pgfplotsarray@mergesort@{\pgfplotsarray@mergesort{#1}}%
	\def\pgfplotsarray@mergesort@recurse@{\pgfplotsarray@mergesort@recurse{#1}}%
	\edef\pgfplotsarray@mergesort@@{{0}{\the\c@pgfplotsarray@tmp}}%
	\expandafter\pgfplotsarray@mergesort@\pgfplotsarray@mergesort@@
	%
	% copy the complete, sorted result outside of the current scope.
	\pgfkeysvalueof{/pgfplots/array/unscope pre/.@cmd}#1\pgfeov
	\endgroup
	\pgfkeysvalueof{/pgfplots/array/unscope post/.@cmd}#1\pgfeov
}%

% #1: array name,
% #2: start offset (as string number)
% #3: number of elems (as string number)
\def\pgfplotsarray@mergesort#1#2#3{%
	\ifnum#3<2
		% ready!
	\else
		\c@pgfplotsarraysort@m=#3\relax
		\divide\c@pgfplotsarraysort@m by2
		\c@pgfplotsarraysort@q=#3\relax
		\advance\c@pgfplotsarraysort@q by-\c@pgfplotsarraysort@m
		\c@pgfplotsarraysort@i=\c@pgfplotsarraysort@m
		\advance\c@pgfplotsarraysort@i by#2\relax
		\edef\pgfplotsarray@mergesort@@{%
			{#2}{\the\c@pgfplotsarraysort@m}% start1, N_1
			{\the\c@pgfplotsarraysort@i}{\the\c@pgfplotsarraysort@q}% start2, N_2
		}%
		\expandafter\pgfplotsarray@mergesort@recurse@\pgfplotsarray@mergesort@@
		%
		% Merge:
		% restore 'm':
		\c@pgfplotsarraysort@m=#3\relax
		\divide\c@pgfplotsarraysort@m by2
		\c@pgfplotsarraysort@q=#3
		\advance\c@pgfplotsarraysort@q by-\c@pgfplotsarraysort@m
		\c@pgfplotsarraysort@k=0
		\c@pgfplotsarraysort@l=0
		\c@pgfplotsarraysort@i=0
		%
		% direct comparisons for merge:
		\pgfutil@loop
		\pgfplots@loop@CONTINUEtrue
		\ifnum\c@pgfplotsarraysort@k<\c@pgfplotsarraysort@m
			\ifnum\c@pgfplotsarraysort@l<\c@pgfplotsarraysort@q
			\else
				\pgfplots@loop@CONTINUEfalse
			\fi
		\else
			\pgfplots@loop@CONTINUEfalse
		\fi
		\ifpgfplots@loop@CONTINUE
			\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@m
			\advance\c@pgfplotsarraysort@j by\c@pgfplotsarraysort@l
			\advance\c@pgfplotsarraysort@j by#2
			\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplotsarray@x@mpl
			\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@k
			\advance\c@pgfplotsarraysort@j by#2
			\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplotsarray@x@k
			\pgfplotsarraysort@iflt
				{\pgfplotsarray@x@mpl}%
				{\pgfplotsarray@x@k}%
				{% x[m+l] < x[k]
					\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplotsarray@x@mpl
					\advance\c@pgfplotsarraysort@i by1
					\advance\c@pgfplotsarraysort@l by1
				}%
				{% x[k] <= x[m+l]
					\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplotsarray@x@k
					\advance\c@pgfplotsarraysort@i by1
					\advance\c@pgfplotsarraysort@k by1
				}%
				\pgfeov
		\pgfutil@repeat
		%
		% append:
		\pgfutil@loop
		\ifnum\c@pgfplotsarraysort@k<\c@pgfplotsarraysort@m
			\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@k
			\advance\c@pgfplotsarraysort@j by#2
			\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplots@loc@TMPa
			\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa
			\advance\c@pgfplotsarraysort@i by1
			\advance\c@pgfplotsarraysort@k by1
		\pgfutil@repeat
		\pgfutil@loop
		\ifnum\c@pgfplotsarraysort@l<\c@pgfplotsarraysort@q
			\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@m
			\advance\c@pgfplotsarraysort@j by\c@pgfplotsarraysort@l
			\advance\c@pgfplotsarraysort@j by#2
			\pgfplotsarrayselect{\c@pgfplotsarraysort@j}\of{#1}\to\pgfplots@loc@TMPa
			\pgfplotsarrayletentryglobal\c@pgfplotsarraysort@i\of{pgfparraytmp}=\pgfplots@loc@TMPa
			\advance\c@pgfplotsarraysort@i by1
			\advance\c@pgfplotsarraysort@l by1
		\pgfutil@repeat
		%
		% copy back from temporary to '#1'
		\c@pgfplotsarraysort@i=0
		\c@pgfplotsarraysort@k=#3
		\pgfutil@loop
		\ifnum\c@pgfplotsarraysort@i<\c@pgfplotsarraysort@k
			\c@pgfplotsarraysort@j=\c@pgfplotsarraysort@i
			\advance\c@pgfplotsarraysort@j by#2
			\pgfplotsarrayselect{\c@pgfplotsarraysort@i}\of{pgfparraytmp}\to\pgfplots@loc@TMPa
			\pgfplotsarrayletentry\c@pgfplotsarraysort@j\of{#1}=\pgfplots@loc@TMPa
			\advance\c@pgfplotsarraysort@i by1
		\pgfutil@repeat
%\pgfplotsarraysort@DEBUGTEST{#1}{after mergesort(start=#2,n=#3):}%
	\fi
}%
\long\def\pgfplotsarraysort@DEBUGTEST#1#2{%
	\par#2\par
	[\pgfplotsarrayforeach{#1}\as\elem{\elem\space}]\par%
}
% #1: array name
% #2: start1
% #3: N1
% #4: start2
% #5: N2
\def\pgfplotsarray@mergesort@recurse#1#2#3#4#5{%
	\pgfplotsarray@mergesort{#1}{#2}{#3}%
	\pgfplotsarray@mergesort{#1}{#4}{#5}%
}