Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3sort.dtx

raw
1 % \iffalse
2 %
3 %% File l3sort.dtx
4 %
5 % Copyright (C) 2012-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3sort} module\\ Sorting functions^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \section{Controlling sorting}
53 %
54 % \label{sec:l3sort:mech}
55 %
56 % \LaTeX3 comes with a facility to sort list variables (sequences,
57 % token lists, or comma-lists) according to some user-defined
58 % comparison. For instance,
59 % \begin{verbatim}
60 % \clist_set:Nn \l_foo_clist { 3 , 01 , -2 , 5 , +1 }
61 % \clist_sort:Nn \l_foo_clist
62 % {
63 % \int_compare:nNnTF { #1 } > { #2 }
64 % { \sort_return_swapped: }
65 % { \sort_return_same: }
66 % }
67 % \end{verbatim}
68 % results in \cs[no-index]{l_foo_clist} holding the values
69 % |{ -2 , 01 , +1 , 3 , 5 }| sorted in non-decreasing order.
70 %
71 % The code defining the comparison should call
72 % \cs{sort_return_swapped:} if the two items given as |#1|
73 % and |#2| are not in the correct order, and otherwise it
74 % should call \cs{sort_return_same:} to indicate that
75 % the order of this pair of items should not be changed.
76 %
77 % For instance, a \meta{comparison code} consisting only
78 % of \cs{sort_return_same:} with no test yields a trivial
79 % sort: the final order is identical to the original order.
80 % Conversely, using a \meta{comparison code} consisting only
81 % of \cs{sort_return_swapped:} reverses the list (in a fairly
82 % inefficient way).
83 %
84 % \begin{texnote}
85 % The current implementation is limited to sorting approximately
86 % $20000$ items ($40000$ in \LuaTeX{}), depending on what other
87 % packages are loaded.
88 %
89 % Internally, the code from \pkg{l3sort} stores items in \tn{toks}
90 % registers allocated locally. Thus, the \meta{comparison code}
91 % should not call \tn{newtoks} or other commands that allocate new
92 % \tn{toks} registers. On the other hand, altering the value of a
93 % previously allocated \tn{toks} register is not a problem.
94 % \end{texnote}
95 %
96 % \begin{function}[added = 2017-02-06]{\sort_return_same:, \sort_return_swapped:}
97 % \begin{syntax}
98 % \cs{seq_sort:Nn} \meta{seq~var}
99 % ~~|{| \ldots{} \cs{sort_return_same:} or \cs{sort_return_swapped:} \ldots{} |}|
100 % \end{syntax}
101 % Indicates whether to keep the order or swap the order of two items
102 % that are compared in the sorting code. Only one of the
103 % \cs[no-index]{sort_return_\ldots{}} functions should be used by the
104 % code, according to the results of some tests on the items |#1| and
105 % |#2| to be compared.
106 % \end{function}
107 %
108 % \end{documentation}
109 %
110 % \begin{implementation}
111 %
112 % \section{\pkg{l3sort} implementation}
113 %
114 % \begin{macrocode}
115 %<*code>
116 % \end{macrocode}
117 %
118 % \begin{macrocode}
119 %<@@=sort>
120 % \end{macrocode}
121 %
122 % \begin{macro}{\@@_sep:}
123 % \begin{macrocode}
124 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
125 % \end{macrocode}
126 % \end{macro}
127 %
128 % \subsection{Variables}
129 %
130 % \begin{variable}{\g_@@_tmp_seq, \g_@@_tmp_tl}
131 % Sorting happens in a group; the result is stored in those global
132 % variables before being copied outside the group to the proper
133 % places. For seq and tl this is more efficient than using \cs{use:e}
134 % (or some \cs{exp_args:NNNe}) to smuggle the definition outside the
135 % group since \TeX{} does not need to re-read tokens. For clist we
136 % don't gain anything since the result is converted from seq to clist
137 % anyways.
138 % \begin{macrocode}
139 \seq_new:N \g_@@_tmp_seq
140 \tl_new:N \g_@@_tmp_tl
141 % \end{macrocode}
142 % \end{variable}
143 %
144 % \begin{variable}
145 % {
146 % \l_@@_length_int, \l_@@_min_int, \l_@@_top_int, \l_@@_max_int,
147 % \l_@@_true_max_int
148 % }
149 % The sequence has \cs{l_@@_length_int} items and is stored from
150 % \cs{l_@@_min_int} to $\cs{l_@@_top_int}-1$. While reading the
151 % sequence in memory, we check that \cs{l_@@_top_int} remains at most
152 % \cs{l_@@_max_int}, precomputed by \cs{@@_compute_range:}. That
153 % bound is such that the merge sort only uses \tn{toks} registers
154 % less than \cs{l_@@_true_max_int}, namely those that have not been
155 % allocated for use in other code: the user's comparison code could
156 % alter these.
157 % \begin{macrocode}
158 \int_new:N \l_@@_length_int
159 \int_new:N \l_@@_min_int
160 \int_new:N \l_@@_top_int
161 \int_new:N \l_@@_max_int
162 \int_new:N \l_@@_true_max_int
163 % \end{macrocode}
164 % \end{variable}
165 %
166 % \begin{variable}{\l_@@_block_int}
167 % Merge sort is done in several passes. In each pass, blocks of size
168 % \cs{l_@@_block_int} are merged in pairs. The block size starts
169 % at $1$, and, for a length in the range $[2^k+1,2^{k+1}]$, reaches
170 % $2^{k}$ in the last pass.
171 % \begin{macrocode}
172 \int_new:N \l_@@_block_int
173 % \end{macrocode}
174 % \end{variable}
175 %
176 % \begin{variable}{\l_@@_begin_int}
177 % \begin{variable}{\l_@@_end_int}
178 % When merging two blocks, \cs{l_@@_begin_int} marks the lowest
179 % index in the two blocks, and \cs{l_@@_end_int} marks the
180 % highest index, plus $1$.
181 % \begin{macrocode}
182 \int_new:N \l_@@_begin_int
183 \int_new:N \l_@@_end_int
184 % \end{macrocode}
185 % \end{variable}
186 % \end{variable}
187 %
188 % \begin{variable}{\l_@@_A_int}
189 % \begin{variable}{\l_@@_B_int}
190 % \begin{variable}{\l_@@_C_int}
191 % When merging two blocks (whose end-points are \texttt{beg}
192 % and \texttt{end}), $A$ starts from the high end of the low
193 % block, and decreases until reaching \texttt{beg}. The index
194 % $B$ starts from the top of the range and marks the register
195 % in which a sorted item should be put. Finally, $C$ points
196 % to the copy of the high block in the interval of registers
197 % starting at \cs{l_@@_length_int}, upwards. $C$ starts
198 % from the upper limit of that range.
199 % \begin{macrocode}
200 \int_new:N \l_@@_A_int
201 \int_new:N \l_@@_B_int
202 \int_new:N \l_@@_C_int
203 % \end{macrocode}
204 % \end{variable}
205 % \end{variable}
206 % \end{variable}
207 %
208 % \begin{variable}{\s_@@_mark,\s_@@_stop}
209 % Internal scan marks.
210 % \begin{macrocode}
211 \scan_new:N \s_@@_mark
212 \scan_new:N \s_@@_stop
213 % \end{macrocode}
214 % \end{variable}
215 %
216 % \subsection{Finding available \tn{toks} registers}
217 %
218 % \begin{macro}{\@@_shrink_range:}
219 % \begin{macro}{\@@_shrink_range_loop:}
220 % After \cs{@@_compute_range:} (defined below) determines that
221 % \tn{toks} registers between \cs{l_@@_min_int} (included) and
222 % \cs{l_@@_true_max_int} (excluded) have not yet been assigned,
223 % \cs{@@_shrink_range:} computes \cs{l_@@_max_int} to reflect the need
224 % for a buffer when merging blocks in the merge sort. Given
225 % $2^{n}\leq A\leq 2^{n}+2^{n-1}$ registers we can sort $\lfloor
226 % A/2\rfloor+2^{n-2}$ items while if we have $2^{n}+2^{n-1}\leq A\leq
227 % 2^{n+1}$ registers we can sort $A-2^{n-1}$ items. We first find out
228 % a power $2^{n}$ such that $2^{n}\leq A\leq 2^{n+1}$ by repeatedly
229 % halving \cs{l_@@_block_int}, starting at $2^{15}$ or $2^{14}$ namely
230 % half the total number of registers, then we use the formulas and set
231 % \cs{l_@@_max_int}.
232 % \begin{macrocode}
233 \cs_new_protected:Npn \@@_shrink_range:
234 {
235 \int_set:Nn \l_@@_A_int
236 { \l_@@_true_max_int - \l_@@_min_int + 1 }
237 \int_set:Nn \l_@@_block_int { \c_max_register_int / 2 }
238 \@@_shrink_range_loop:
239 \int_set:Nn \l_@@_max_int
240 {
241 \int_compare:nNnTF
242 { \l_@@_block_int * 3 / 2 } > \l_@@_A_int
243 {
244 \l_@@_min_int
245 + ( \l_@@_A_int - 1 ) / 2
246 + \l_@@_block_int / 4
247 - 1
248 }
249 { \l_@@_true_max_int - \l_@@_block_int / 2 }
250 }
251 }
252 \cs_new_protected:Npn \@@_shrink_range_loop:
253 {
254 \if_int_compare:w \l_@@_A_int < \l_@@_block_int
255 \tex_divide:D \l_@@_block_int 2 \exp_stop_f:
256 \exp_after:wN \@@_shrink_range_loop:
257 \fi:
258 }
259 % \end{macrocode}
260 % \end{macro}
261 % \end{macro}
262 %
263 % \begin{macro}{\@@_compute_range:, \@@_redefine_compute_range:}
264 % \begin{variable}{\c_@@_max_length_int}
265 % First find out what \tn{toks} have not yet been assigned. There are
266 % many cases. In \LaTeXe{} with no package, available \tn{toks} range
267 % from $\tn{count}15+1$ to \cs{c_max_register_int} included (this was
268 % not altered despite the 2015 changes). When \tn{loctoks} is
269 % defined, namely in plain (e)\TeX{}, or when the package \pkg{etex}
270 % is loaded in \LaTeXe{}, redefine \cs{@@_compute_range:} to use the
271 % range $\tn{count}265$ to $\tn{count}275-1$. The \pkg{elocalloc}
272 % package also defines \tn{loctoks} but uses yet another number for
273 % the upper bound, namely \cs{e@alloc@top} (minus one). We must check
274 % for \tn{loctoks} every time a sorting function is called, as
275 % \pkg{etex} or \pkg{elocalloc} could be loaded.
276 %
277 % In \ConTeXt{} MkIV the range is from
278 % $|\c_syst_last_allocated_toks|+1$ to \cs{c_max_register_int}, and in
279 % MkII it is from $|\lastallocatedtoks|+1$ to \cs{c_max_register_int}.
280 % In all these cases, call \cs{@@_shrink_range:}.
281 % \begin{macrocode}
282 \cs_new_protected:Npn \@@_compute_range:
283 {
284 \int_set:Nn \l_@@_min_int { \tex_count:D 15 + 1 }
285 \int_set:Nn \l_@@_true_max_int { \c_max_register_int + 1 }
286 \@@_shrink_range:
287 \if_meaning:w \loctoks \tex_undefined:D \else:
288 \if_meaning:w \loctoks \scan_stop: \else:
289 \@@_redefine_compute_range:
290 \@@_compute_range:
291 \fi:
292 \fi:
293 }
294 \cs_new_protected:Npn \@@_redefine_compute_range:
295 {
296 \cs_if_exist:cTF { [email protected] }
297 {
298 \cs_gset_protected:Npn \@@_compute_range:
299 {
300 \int_set:Nn \l_@@_min_int { \tex_count:D 265 }
301 \int_set_eq:NN \l_@@_true_max_int \e@alloc@top
302 \@@_shrink_range:
303 }
304 }
305 {
306 \cs_gset_protected:Npn \@@_compute_range:
307 {
308 \int_set:Nn \l_@@_min_int { \tex_count:D 265 }
309 \int_set:Nn \l_@@_true_max_int { \tex_count:D 275 }
310 \@@_shrink_range:
311 }
312 }
313 }
314 \cs_if_exist:NT \loctoks { \@@_redefine_compute_range: }
315 \tl_map_inline:nn { \lastallocatedtoks \c_syst_last_allocated_toks }
316 {
317 \cs_if_exist:NT #1
318 {
319 \cs_gset_protected:Npn \@@_compute_range:
320 {
321 \int_set:Nn \l_@@_min_int { #1 + 1 }
322 \int_set:Nn \l_@@_true_max_int { \c_max_register_int + 1 }
323 \@@_shrink_range:
324 }
325 }
326 }
327 % \end{macrocode}
328 % \end{variable}
329 % \end{macro}
330 %
331 % \subsection{Protected user commands}
332 %
333 % \begin{macro}{\@@_main:NNNn}
334 % Sorting happens in three steps. First store items in \tn{toks}
335 % registers ranging from \cs{l_@@_min_int} to $\cs{l_@@_top_int}-1$,
336 % while checking that the list is not too long. If we reach the
337 % maximum length, that's an error; exit the group. Secondly, sort the
338 % array of \tn{toks} registers, using the user-defined sorting
339 % function: \cs{@@_level:} calls \cs{@@_compare:nn} as needed.
340 % Finally, unpack the \tn{toks} registers (now sorted) into the target
341 % tl, or into \cs{g_@@_tmp_seq} for seq and clist. This is done
342 % by \cs{@@_seq:NNNNn} and \cs{@@_tl:NNn}.
343 % \begin{macrocode}
344 \cs_new_protected:Npn \@@_main:NNNn #1#2#3#4
345 {
346 \@@_disable_toksdef:
347 \@@_compute_range:
348 \int_set_eq:NN \l_@@_top_int \l_@@_min_int
349 #1 #3
350 {
351 \if_int_compare:w \l_@@_top_int = \l_@@_max_int
352 \@@_too_long_error:NNw #2 #3
353 \fi:
354 \tex_toks:D \l_@@_top_int {##1}
355 \int_incr:N \l_@@_top_int
356 }
357 \int_set:Nn \l_@@_length_int
358 { \l_@@_top_int - \l_@@_min_int }
359 \cs_set:Npn \@@_compare:nn ##1 ##2 {#4}
360 \int_set:Nn \l_@@_block_int { 1 }
361 \@@_level:
362 }
363 % \end{macrocode}
364 % \end{macro}
365 %
366 % \begin{macro}{\tl_sort:Nn, \tl_sort:cn, \tl_gsort:Nn, \tl_gsort:cn}
367 % \begin{macro}{\@@_tl:NNn}
368 % \begin{macro}[EXP]{\@@_tl_toks:w}
369 % Call the main sorting function then unpack \tn{toks} registers
370 % outside the group into the target token list. The unpacking is done
371 % by \cs{@@_tl_toks:w}; registers are numbered from \cs{l_@@_min_int}
372 % to $\cs{l_@@_top_int}-1$. For expansion behavior we need a couple
373 % of primitives. The \cs{tl_gclear:N} reduces memory usage. The
374 % \cs{prg_break_point:} is used by \cs{@@_main:NNNn} when the list is
375 % too long.
376 % \begin{macrocode}
377 \cs_new_protected:Npn \tl_sort:Nn { \@@_tl:NNn \tl_set_eq:NN }
378 \cs_generate_variant:Nn \tl_sort:Nn { c }
379 \cs_new_protected:Npn \tl_gsort:Nn { \@@_tl:NNn \tl_gset_eq:NN }
380 \cs_generate_variant:Nn \tl_gsort:Nn { c }
381 \cs_new_protected:Npn \@@_tl:NNn #1#2#3
382 {
383 \group_begin:
384 \@@_main:NNNn \tl_map_inline:Nn \tl_map_break:n #2 {#3}
385 \__kernel_tl_gset:Nx \g_@@_tmp_tl
386 { \@@_tl_toks:w \l_@@_min_int \@@_sep: }
387 \group_end:
388 #1 #2 \g_@@_tmp_tl
389 \tl_gclear:N \g_@@_tmp_tl
390 \prg_break_point:
391 }
392 \cs_new:Npn \@@_tl_toks:w #1 \@@_sep:
393 {
394 \if_int_compare:w #1 < \l_@@_top_int
395 { \tex_the:D \tex_toks:D #1 }
396 \exp_after:wN \@@_tl_toks:w
397 \int_value:w \int_eval:n { #1 + 1 } \exp_after:wN \@@_sep:
398 \fi:
399 }
400 % \end{macrocode}
401 % \end{macro}
402 % \end{macro}
403 % \end{macro}
404 %
405 % \begin{macro}{\seq_sort:Nn, \seq_sort:cn, \seq_gsort:Nn, \seq_gsort:cn}
406 % \begin{macro}{\clist_sort:Nn, \clist_sort:cn, \clist_gsort:Nn, \clist_gsort:cn}
407 % \begin{macro}{\@@_seq:NNNNn}
408 % Use the same general framework for seq and clist. Apply the general
409 % sorting code, then unpack \tn{toks} into \cs{g_@@_tmp_seq}.
410 % Outside the group copy or convert (for clist) the data to the target
411 % variable. The \cs{seq_gclear:N} reduces memory usage. The
412 % \cs{prg_break_point:} is used by \cs{@@_main:NNNn} when the list is
413 % too long.
414 % \begin{macrocode}
415 \cs_new_protected:Npn \seq_sort:Nn
416 { \@@_seq:NNNNn \seq_map_inline:Nn \seq_map_break:n \seq_set_eq:NN }
417 \cs_generate_variant:Nn \seq_sort:Nn { c }
418 \cs_new_protected:Npn \seq_gsort:Nn
419 { \@@_seq:NNNNn \seq_map_inline:Nn \seq_map_break:n \seq_gset_eq:NN }
420 \cs_generate_variant:Nn \seq_gsort:Nn { c }
421 \cs_new_protected:Npn \clist_sort:Nn
422 {
423 \@@_seq:NNNNn \clist_map_inline:Nn \clist_map_break:n
424 \clist_set_from_seq:NN
425 }
426 \cs_generate_variant:Nn \clist_sort:Nn { c }
427 \cs_new_protected:Npn \clist_gsort:Nn
428 {
429 \@@_seq:NNNNn \clist_map_inline:Nn \clist_map_break:n
430 \clist_gset_from_seq:NN
431 }
432 \cs_generate_variant:Nn \clist_gsort:Nn { c }
433 \cs_new_protected:Npn \@@_seq:NNNNn #1#2#3#4#5
434 {
435 \group_begin:
436 \@@_main:NNNn #1 #2 #4 {#5}
437 \seq_gclear:N \g_@@_tmp_seq
438 \int_step_inline:nnn
439 \l_@@_min_int { \l_@@_top_int - 1 }
440 {
441 \seq_gput_right:Ne \g_@@_tmp_seq
442 { \tex_the:D \tex_toks:D ##1 }
443 }
444 \group_end:
445 #3 #4 \g_@@_tmp_seq
446 \seq_gclear:N \g_@@_tmp_seq
447 \prg_break_point:
448 }
449 % \end{macrocode}
450 % \end{macro}
451 % \end{macro}
452 % \end{macro}
453 %
454 % \subsection{Merge sort}
455 %
456 % \begin{macro}{\@@_level:}
457 % This function is called once blocks of size \cs{l_@@_block_int}
458 % (initially $1$) are each sorted. If the whole list fits in one
459 % block, then we are done (this also takes care of the case of an
460 % empty list or a list with one item). Otherwise, go through pairs
461 % of blocks starting from $0$, then double the block size, and repeat.
462 % \begin{macrocode}
463 \cs_new_protected:Npn \@@_level:
464 {
465 \if_int_compare:w \l_@@_block_int < \l_@@_length_int
466 \l_@@_end_int \l_@@_min_int
467 \@@_merge_blocks:
468 \tex_advance:D \l_@@_block_int \l_@@_block_int
469 \exp_after:wN \@@_level:
470 \fi:
471 }
472 % \end{macrocode}
473 % \end{macro}
474 %
475 % \begin{macro}{\@@_merge_blocks:}
476 % This function is called to merge a pair of blocks, starting at
477 % the last value of \cs{l_@@_end_int} (end-point of the previous
478 % pair of blocks). If shifting by one block to the right we reach
479 % the end of the list, then this pass has ended: the end of the
480 % list is sorted already. Otherwise, store the result of that shift in $A$,
481 % which indexes the first block starting from the top end.
482 % Then locate the end-point (maximum) of the second block: shift
483 % \texttt{end} upwards by one more block, but keeping it
484 % $\leq\texttt{top}$. Copy this upper block of \tn{toks}
485 % registers in registers above \texttt{length}, indexed by $C$:
486 % this is covered by \cs{@@_copy_block:}. Once this is done we
487 % are ready to do the actual merger using \cs{@@_merge_blocks_aux:},
488 % after shifting $A$, $B$ and $C$ so that they point to the largest
489 % index in their respective ranges rather than pointing just beyond
490 % those ranges. Of course, once that pair of blocks is merged,
491 % move on to the next pair.
492 % \begin{macrocode}
493 \cs_new_protected:Npn \@@_merge_blocks:
494 {
495 \l_@@_begin_int \l_@@_end_int
496 \tex_advance:D \l_@@_end_int \l_@@_block_int
497 \if_int_compare:w \l_@@_end_int < \l_@@_top_int
498 \l_@@_A_int \l_@@_end_int
499 \tex_advance:D \l_@@_end_int \l_@@_block_int
500 \if_int_compare:w \l_@@_end_int > \l_@@_top_int
501 \l_@@_end_int \l_@@_top_int
502 \fi:
503 \l_@@_B_int \l_@@_A_int
504 \l_@@_C_int \l_@@_top_int
505 \@@_copy_block:
506 \int_decr:N \l_@@_A_int
507 \int_decr:N \l_@@_B_int
508 \int_decr:N \l_@@_C_int
509 \exp_after:wN \@@_merge_blocks_aux:
510 \exp_after:wN \@@_merge_blocks:
511 \fi:
512 }
513 % \end{macrocode}
514 % \end{macro}
515 %
516 % \begin{macro}{\@@_copy_block:}
517 % We wish to store a copy of the \enquote{upper} block of
518 % \tn{toks} registers, ranging between the initial value of
519 % \cs{l_@@_B_int} (included) and \cs{l_@@_end_int}
520 % (excluded) into a new range starting at the initial value
521 % of \cs{l_@@_C_int}, namely \cs{l_@@_top_int}.
522 % \begin{macrocode}
523 \cs_new_protected:Npn \@@_copy_block:
524 {
525 \tex_toks:D \l_@@_C_int \tex_toks:D \l_@@_B_int
526 \int_incr:N \l_@@_C_int
527 \int_incr:N \l_@@_B_int
528 \if_int_compare:w \l_@@_B_int = \l_@@_end_int
529 \use_i:nn
530 \fi:
531 \@@_copy_block:
532 }
533 % \end{macrocode}
534 % \end{macro}
535 %
536 % \begin{macro}{\@@_merge_blocks_aux:}
537 % At this stage, the first block starts at \cs{l_@@_begin_int},
538 % and ends at \cs{l_@@_A_int}, and the second block starts at
539 % \cs{l_@@_top_int} and ends at \cs{l_@@_C_int}. The result
540 % of the merger is stored at positions indexed by \cs{l_@@_B_int},
541 % which starts at $\cs{l_@@_end_int}-1$ and decreases down to
542 % \cs{l_@@_begin_int}, covering the full range of the two blocks.
543 % In other words, we are building the merger starting with the
544 % largest values.
545 % The comparison function is defined to return either
546 % \texttt{swapped} or \texttt{same}. Of course, this
547 % means the arguments need to be given in the order they
548 % appear originally in the list.
549 % \begin{macrocode}
550 \cs_new_protected:Npn \@@_merge_blocks_aux:
551 {
552 \exp_after:wN \@@_compare:nn \exp_after:wN
553 { \tex_the:D \tex_toks:D \exp_after:wN \l_@@_A_int \exp_after:wN }
554 \exp_after:wN { \tex_the:D \tex_toks:D \l_@@_C_int }
555 \prg_do_nothing:
556 \@@_return_mark:w
557 \@@_return_mark:w
558 \s_@@_mark
559 \@@_return_none_error:
560 }
561 % \end{macrocode}
562 % \end{macro}
563 %
564 % \begin{macro}{\sort_return_same:, \sort_return_swapped:}
565 % \begin{macro}{\@@_return_mark:w}
566 % \begin{macro}{\@@_return_none_error:, \@@_return_two_error:}
567 % Each comparison should call \cs{sort_return_same:} or
568 % \cs{sort_return_swapped:} exactly once. If neither is called,
569 % \cs{@@_return_none_error:} is called, since the \texttt{return_mark}
570 % removes tokens until \cs{s_@@_mark}. If one is called, the
571 % \texttt{return_mark} auxiliary removes everything except
572 % \cs{@@_return_same:w} (or its \texttt{swapped} analogue) followed by
573 % \cs{@@_return_none_error:}. Finally if two or more are called,
574 % \cs{@@_return_two_error:} ends up before any \cs{@@_return_mark:w},
575 % so that it produces an error.
576 % \begin{macrocode}
577 \cs_new_protected:Npn \sort_return_same:
578 #1 \@@_return_mark:w #2 \s_@@_mark
579 {
580 #1
581 #2
582 \@@_return_two_error:
583 \@@_return_mark:w
584 \s_@@_mark
585 \@@_return_same:w
586 }
587 \cs_new_protected:Npn \sort_return_swapped:
588 #1 \@@_return_mark:w #2 \s_@@_mark
589 {
590 #1
591 #2
592 \@@_return_two_error:
593 \@@_return_mark:w
594 \s_@@_mark
595 \@@_return_swapped:w
596 }
597 \cs_new_protected:Npn \@@_return_mark:w #1 \s_@@_mark { }
598 \cs_new_protected:Npn \@@_return_none_error:
599 {
600 \msg_error:nnee { sort } { return-none }
601 { \tex_the:D \tex_toks:D \l_@@_A_int }
602 { \tex_the:D \tex_toks:D \l_@@_C_int }
603 \@@_return_same:w \@@_return_none_error:
604 }
605 \cs_new_protected:Npn \@@_return_two_error:
606 {
607 \msg_error:nnee { sort } { return-two }
608 { \tex_the:D \tex_toks:D \l_@@_A_int }
609 { \tex_the:D \tex_toks:D \l_@@_C_int }
610 }
611 % \end{macrocode}
612 % \end{macro}
613 % \end{macro}
614 % \end{macro}
615 %
616 % \begin{macro}{\@@_return_same:w}
617 % If the comparison function returns \texttt{same},
618 % then the second argument fed to \cs{@@_compare:nn}
619 % should remain to the right of the other one. Since
620 % we build the merger starting from the right, we copy
621 % that \tn{toks} register into the allotted range, then
622 % shift the pointers $B$ and $C$, and go on to do one
623 % more step in the merger, unless the second block has
624 % been exhausted: then the remainder of the first block
625 % is already in the correct registers and we are done
626 % with merging those two blocks.
627 % \begin{macrocode}
628 \cs_new_protected:Npn \@@_return_same:w #1 \@@_return_none_error:
629 {
630 \tex_toks:D \l_@@_B_int \tex_toks:D \l_@@_C_int
631 \int_decr:N \l_@@_B_int
632 \int_decr:N \l_@@_C_int
633 \if_int_compare:w \l_@@_C_int < \l_@@_top_int
634 \use_i:nn
635 \fi:
636 \@@_merge_blocks_aux:
637 }
638 % \end{macrocode}
639 % \end{macro}
640 %
641 % \begin{macro}{\@@_return_swapped:w}
642 % If the comparison function returns \texttt{swapped},
643 % then the next item to add to the merger is the first
644 % argument, contents of the \tn{toks} register $A$.
645 % Then shift the pointers $A$ and $B$ to the left, and
646 % go for one more step for the merger, unless the left
647 % block was exhausted ($A$ goes below the threshold).
648 % In that case, all remaining \tn{toks} registers in
649 % the second block, indexed by $C$, are copied
650 % to the merger by \cs{@@_merge_blocks_end:}.
651 % \begin{macrocode}
652 \cs_new_protected:Npn \@@_return_swapped:w #1 \@@_return_none_error:
653 {
654 \tex_toks:D \l_@@_B_int \tex_toks:D \l_@@_A_int
655 \int_decr:N \l_@@_B_int
656 \int_decr:N \l_@@_A_int
657 \if_int_compare:w \l_@@_A_int < \l_@@_begin_int
658 \@@_merge_blocks_end: \use_i:nn
659 \fi:
660 \@@_merge_blocks_aux:
661 }
662 % \end{macrocode}
663 % \end{macro}
664 %
665 % \begin{macro}{\@@_merge_blocks_end:}
666 % This function's task is to copy the \tn{toks} registers
667 % in the block indexed by $C$ to the merger indexed by $B$.
668 % The end can equally be detected by checking when $B$ reaches
669 % the threshold \texttt{begin}, or when $C$ reaches
670 % \texttt{top}.
671 % \begin{macrocode}
672 \cs_new_protected:Npn \@@_merge_blocks_end:
673 {
674 \tex_toks:D \l_@@_B_int \tex_toks:D \l_@@_C_int
675 \int_decr:N \l_@@_B_int
676 \int_decr:N \l_@@_C_int
677 \if_int_compare:w \l_@@_B_int < \l_@@_begin_int
678 \use_i:nn
679 \fi:
680 \@@_merge_blocks_end:
681 }
682 % \end{macrocode}
683 % \end{macro}
684 %
685 % \subsection{Expandable sorting}
686 %
687 % Sorting expandably is very different from sorting and assigning to a
688 % variable. Since tokens cannot be stored, they must remain in the
689 % input stream, and be read through at every step. It is thus
690 % necessarily much slower (at best $O(n^2\ln n)$) than non-expandable
691 % sorting functions ($O(n\ln n)$).
692 %
693 % A prototypical version of expandable quicksort is as follows. If the
694 % argument has no item, return nothing, otherwise partition, using the
695 % first item as a pivot (argument |#4| of \cs{@@:nnNnn}). The
696 % arguments of \cs{@@:nnNnn} are 1.~items less than |#4|, 2.~items
697 % greater or equal to |#4|, 3.~comparison, 4.~pivot, 5.~next item to
698 % test. If |#5| is the tail of the list, call \cs{tl_sort:nN} on |#1|
699 % and on |#2|, placing |#4| in between; |\use:ff| expands the parts to
700 % make \cs{tl_sort:nN} \texttt{f}-expandable. Otherwise, compare |#4|
701 % and |#5| using |#3|. If they are ordered, place |#5| amongst the
702 % \enquote{greater} items, otherwise amongst the \enquote{lesser} items,
703 % and continue partitioning.
704 % \begin{verbatim}
705 % \cs_new:Npn \tl_sort:nN #1#2
706 % {
707 % \tl_if_blank:nF {#1}
708 % {
709 % \__sort:nnNnn { } { } #2
710 % #1 \q__sort_recursion_tail \q__sort_recursion_stop
711 % }
712 % }
713 % \cs_new:Npn \__sort:nnNnn #1#2#3#4#5
714 % {
715 % \quark_if_recursion_tail_stop_do:nn {#5}
716 % { \use:ff { \tl_sort:nN {#1} #3 {#4} } { \tl_sort:nN {#2} #3 } }
717 % #3 {#4} {#5}
718 % { \__sort:nnNnn {#1} { #2 {#5} } #3 {#4} }
719 % { \__sort:nnNnn { #1 {#5} } {#2} #3 {#4} }
720 % }
721 % \cs_generate_variant:Nn \use:nn { ff }
722 % \end{verbatim}
723 % There are quite a few optimizations available here: the code below is
724 % less legible, but more than twice as fast.
725 %
726 % In the simple version of the code, \cs{@@:nnNnn} is called
727 % \(O(n\ln n)\) times on average (the number of comparisons required by
728 % the quicksort algorithm). Hence most of our focus is on
729 % optimizing that function.
730 %
731 % The first speed up is to avoid testing for the end of the list at
732 % every call to \cs{@@:nnNnn}. For this, the list is prepared by
733 % changing each \meta{item} of the original token list into
734 % \meta{command} \Arg{item}, just like sequences are stored. We arrange
735 % things such that the \meta{command} is the \meta{conditional} provided
736 % by the user: the loop over the \meta{prepared tokens} then looks like
737 % \begin{quote}
738 % \ttfamily
739 % \cs{cs_new:Npn}~\cs{@@_loop:wNn}~\ldots{}~|#6#7|\\
740 % ~~|{|\\
741 % ~~~~|#6|~\Arg{pivot}~|{#7}|~\meta{loop big}~\meta{loop small}\\
742 % ~~~~~~\meta{extra arguments}\\
743 % ~~|}|\\
744 % \cs{@@_loop:wNn}~\ldots{}~\meta{prepared tokens}\\
745 % ~~\meta{end-loop}~|{}|~\cs{s_@@_stop}
746 % \end{quote}
747 % In this example, which matches the structure of
748 % \cs{@@_quick_split_i:NnnnnNn} and a few other functions below, the
749 % \cs{@@_loop:wNn} auxiliary normally receives the user's
750 % \meta{conditional} as~|#6| and an \meta{item} as~|#7|. This is
751 % compared to the \meta{pivot} (the argument~|#5|, not shown here), and
752 % the \meta{conditional} leaves the \meta{loop big} or \meta{loop small}
753 % auxiliary, which both have the same form as \cs{@@_loop:wNn},
754 % receiving the next pair \meta{conditional} \Arg{item} as |#6|
755 % and~|#7|. At the end, |#6| is the \meta{end-loop} function, which
756 % terminates the loop.
757 %
758 % The second speed up is to minimize the duplicated tokens between the
759 % \texttt{true} and \texttt{false} branches of the conditional. For
760 % this, we introduce two versions of \cs{@@:nnNnn}, which receive
761 % the new item as~|#1| and place it either into the list~|#2| of items
762 % less than the pivot~|#4| or into the list~|#3| of items greater or
763 % equal to the pivot.
764 % \begin{verbatim}
765 % \cs_new:Npn \__sort_i:nnnnNn #1#2#3#4#5#6
766 % {
767 % #5 {#4} {#6} \__sort_ii:nnnnNn \__sort_i:nnnnNn
768 % {#6} { #2 {#1} } {#3} {#4}
769 % }
770 % \cs_new:Npn \__sort_ii:nnnnNn #1#2#3#4#5#6
771 % {
772 % #5 {#4} {#6} \__sort_ii:nnnnNn \__sort_i:nnnnNn
773 % {#6} {#2} { #3 {#1} } {#4}
774 % }
775 % \end{verbatim}
776 % Note that the two functions have the form of \cs{@@_loop:wNn} above,
777 % receiving as~|#5| the conditional or a function to end the loop. In
778 % fact, the lists~|#2| and~|#3| must be made of pairs \meta{conditional}
779 % \Arg{item}, so we have to replace~|{#6}| above by |{|~|#5|~|{#6}|~|}|,
780 % and |{#1}|~by~|#1|. The actual functions have one more argument, so
781 % all argument numbers are shifted compared to this code.
782 %
783 % The third speed up is to avoid |\use:ff| using a continuation-passing
784 % style: \cs{@@_quick_split:NnNn} expects a list followed by
785 % \cs{s_@@_mark} \Arg{code}, and expands to \meta{code} \meta{sorted list}.
786 % Sorting the two parts of the list around the pivot is done with
787 % \begin{quote}
788 % \ttfamily
789 % \cs{@@_quick_split:NnNn} |#2| \ldots{} \cs{s_@@_mark}\\
790 % ~~|{|\\
791 % ~~~~\cs{@@_quick_split:NnNn} |#1| \ldots{} \cs{s_@@_mark} \Arg{code}\\
792 % ~~~~\Arg{pivot}\\
793 % ~~|}|
794 % \end{quote}
795 % Items which are larger than the \meta{pivot} are sorted, then placed
796 % after code that sorts the smaller items, and after the (braced)
797 % \meta{pivot}.
798 %
799 % The fourth speed up is avoid the recursive call to \cs{tl_sort:nN}
800 % with an empty first argument. For this, we introduce functions
801 % similar to the \cs{@@_i:nnnnNn} of the last example, but aware of
802 % whether the list of \meta{conditional} \Arg{item} read so far that are
803 % less than the pivot, and the list of those greater or equal, are empty
804 % or not: see \cs{@@_quick_split:NnNn} and functions defined below.
805 % Knowing whether the lists are empty or not is useless if we do not use
806 % distinct ending codes as appropriate. The splitting auxiliaries
807 % communicate to the \meta{end-loop} function (that is initially placed
808 % after the ``prepared'' list) by placing a specific ending function,
809 % ignored when looping, but useful at the end. In fact, the
810 % \meta{end-loop} function does nothing but place the appropriate ending
811 % function in front of all its arguments. The ending functions take
812 % care of sorting non-empty sublists, placing the pivot in between, and
813 % the continuation before.
814 %
815 % The final change in fact slows down the code a little, but is required
816 % to avoid memory issues: schematically, when \TeX{} encounters
817 % \begin{verbatim}
818 % \use:n { \use:n { \use:n { ... } ... } ... }
819 % \end{verbatim}
820 % the argument of the first \cs{use:n} is not completely read by the
821 % second \cs{use:n}, hence must remain in memory; then the argument of
822 % the second \cs{use:n} is not completely read when grabbing the
823 % argument of the third \cs{use:n}, hence must remain in memory, and so
824 % on. The memory consumption grows quadratically with the number of
825 % nested \cs{use:n}. In practice, this means that we must read
826 % everything until a trailing \cs{s_@@_stop} once in a while, otherwise
827 % sorting lists of more than a few thousand items would exhaust a
828 % typical \TeX{}'s memory.
829 %
830 % \begin{macro}[EXP]{\tl_sort:nN}
831 % \begin{macro}[EXP]
832 % {
833 % \@@_quick_prepare:Nnnn,
834 % \@@_quick_prepare_end:NNNnw,
835 % \@@_quick_cleanup:w
836 % }
837 % The code within the \cs{exp_not:f} sorts the list, leaving in most
838 % cases a leading \cs{exp_not:f}, which stops the expansion, letting
839 % the result be return within \cs{exp_not:n}. We filter out the case
840 % of a list with no item, which would otherwise cause problems. Then
841 % prepare the token list~|#1| by inserting the conditional~|#2| before
842 % each item. The \texttt{prepare} auxiliary receives the conditional
843 % as~|#1|, the prepared token list so far as~|#2|, the next prepared
844 % item as~|#3|, and the item after that as~|#4|. The loop ends
845 % when~|#4| contains \cs{prg_break_point:}, then the
846 % \texttt{prepare_end} auxiliary finds the prepared token list
847 % as~|#4|. The scene is then set up for \cs{@@_quick_split:NnNn},
848 % which sorts the prepared list and perform the post action placed
849 % after \cs{s_@@_mark}, namely removing the trailing \cs{s_@@_stop} and
850 % \cs{s_@@_stop} and leaving \cs{exp_stop_f:} to stop
851 % \texttt{f}-expansion.
852 % \begin{macrocode}
853 \cs_new:Npn \tl_sort:nN #1#2
854 {
855 \exp_not:f
856 {
857 \tl_if_blank:nF {#1}
858 {
859 \@@_quick_prepare:Nnnn #2 { } { }
860 #1
861 { \prg_break_point: \@@_quick_prepare_end:NNNnw }
862 \s_@@_stop
863 }
864 }
865 }
866 \cs_new:Npn \@@_quick_prepare:Nnnn #1#2#3#4
867 {
868 \prg_break: #4 \prg_break_point:
869 \@@_quick_prepare:Nnnn #1 { #2 #3 } { #1 {#4} }
870 }
871 \cs_new:Npn \@@_quick_prepare_end:NNNnw #1#2#3#4#5 \s_@@_stop
872 {
873 \@@_quick_split:NnNn #4 \@@_quick_end:nnTFNn { }
874 \s_@@_mark { \@@_quick_cleanup:w \exp_stop_f: }
875 \s_@@_mark \s_@@_stop
876 }
877 \cs_new:Npn \@@_quick_cleanup:w #1 \s_@@_mark \s_@@_stop {#1}
878 % \end{macrocode}
879 % \end{macro}
880 % \end{macro}
881 %
882 % \begin{macro}[EXP]
883 % {
884 % \@@_quick_split:NnNn,
885 % \@@_quick_only_i:NnnnnNn,
886 % \@@_quick_only_ii:NnnnnNn,
887 % \@@_quick_split_i:NnnnnNn,
888 % \@@_quick_split_ii:NnnnnNn
889 % }
890 % The \texttt{only_i}, \texttt{only_ii}, \texttt{split_i} and
891 % \texttt{split_ii} auxiliaries receive a useless first argument, the
892 % new item~|#2| (that they append to either one of the next two
893 % arguments), the list~|#3| of items less than the pivot, bigger
894 % items~|#4|, the pivot~|#5|, a \meta{function}~|#6|, and an
895 % item~|#7|. The \meta{function} is the user's \meta{conditional}
896 % except at the end of the list where it is
897 % \cs{@@_quick_end:nnTFNn}. The comparison is applied to the
898 % \meta{pivot} and the \meta{item}, and calls the \texttt{only_i} or
899 % \texttt{split_i} auxiliaries if the \meta{item} is smaller, and the
900 % \texttt{only_ii} or \texttt{split_ii} auxiliaries otherwise. In
901 % both cases, the next auxiliary goes to work right away, with no
902 % intermediate expansion that would slow down operations. Note that
903 % the argument~|#2| left for the next call has the form
904 % \meta{conditional} \Arg{item}, so that the lists~|#3| and~|#4| keep
905 % the right form to be fed to the next sorting function.
906 % The \texttt{split} auxiliary differs from these in that it is
907 % missing three of the arguments, which would be empty, and its first
908 % argument is always the user's \meta{conditional} rather than an
909 % ending function.
910 % \begin{macrocode}
911 \cs_new:Npn \@@_quick_split:NnNn #1#2#3#4
912 {
913 #3 {#2} {#4} \@@_quick_only_ii:NnnnnNn
914 \@@_quick_only_i:NnnnnNn
915 \@@_quick_single_end:nnnwnw
916 { #3 {#4} } { } { } {#2}
917 }
918 \cs_new:Npn \@@_quick_only_i:NnnnnNn #1#2#3#4#5#6#7
919 {
920 #6 {#5} {#7} \@@_quick_split_ii:NnnnnNn
921 \@@_quick_only_i:NnnnnNn
922 \@@_quick_only_i_end:nnnwnw
923 { #6 {#7} } { #3 #2 } { } {#5}
924 }
925 \cs_new:Npn \@@_quick_only_ii:NnnnnNn #1#2#3#4#5#6#7
926 {
927 #6 {#5} {#7} \@@_quick_only_ii:NnnnnNn
928 \@@_quick_split_i:NnnnnNn
929 \@@_quick_only_ii_end:nnnwnw
930 { #6 {#7} } { } { #4 #2 } {#5}
931 }
932 \cs_new:Npn \@@_quick_split_i:NnnnnNn #1#2#3#4#5#6#7
933 {
934 #6 {#5} {#7} \@@_quick_split_ii:NnnnnNn
935 \@@_quick_split_i:NnnnnNn
936 \@@_quick_split_end:nnnwnw
937 { #6 {#7} } { #3 #2 } {#4} {#5}
938 }
939 \cs_new:Npn \@@_quick_split_ii:NnnnnNn #1#2#3#4#5#6#7
940 {
941 #6 {#5} {#7} \@@_quick_split_ii:NnnnnNn
942 \@@_quick_split_i:NnnnnNn
943 \@@_quick_split_end:nnnwnw
944 { #6 {#7} } {#3} { #4 #2 } {#5}
945 }
946 % \end{macrocode}
947 % \end{macro}
948 %
949 % \begin{macro}[EXP]
950 % {
951 % \@@_quick_end:nnTFNn,
952 % \@@_quick_single_end:nnnwnw,
953 % \@@_quick_only_i_end:nnnwnw,
954 % \@@_quick_only_ii_end:nnnwnw,
955 % \@@_quick_split_end:nnnwnw,
956 % }
957 % The \cs{@@_quick_end:nnTFNn} appears instead of the user's
958 % conditional, and receives as its arguments the pivot~|#1|, a fake
959 % item~|#2|, a \texttt{true} and a \texttt{false} branches |#3|
960 % and~|#4|, followed by an ending function~|#5| (one of the four
961 % auxiliaries here) and another copy~|#6| of the fake item. All those
962 % are discarded except the function~|#5|. This function receives
963 % lists~|#1| and~|#2| of items less than or greater than the
964 % pivot~|#3|, then a continuation code~|#5| just after \cs{s_@@_mark}.
965 % To avoid a memory problem described earlier, all of the ending
966 % functions read~|#6| until \cs{s_@@_stop} and place~|#6| back into the
967 % input stream. When the lists |#1| and~|#2| are empty, the
968 % \texttt{single} auxiliary simply places the continuation~|#5| before
969 % the pivot~|{#3}|. When |#2|~is empty, |#1|~is sorted and placed
970 % before the pivot~|{#3}|, taking care to feed the continuation~|#5|
971 % as a continuation for the function sorting~|#1|. When |#1|~is
972 % empty, |#2|~is sorted, and the continuation argument is used to
973 % place the continuation~|#5| and the pivot~|{#3}| before the sorted
974 % result. Finally, when both lists are non-empty, items larger than
975 % the pivot are sorted, then items less than the pivot, and the
976 % continuations are done in such a way to place the pivot in between.
977 % \begin{macrocode}
978 \cs_new:Npn \@@_quick_end:nnTFNn #1#2#3#4#5#6 {#5}
979 \cs_new:Npn \@@_quick_single_end:nnnwnw #1#2#3#4 \s_@@_mark #5#6 \s_@@_stop
980 { #5 {#3} #6 \s_@@_stop }
981 \cs_new:Npn \@@_quick_only_i_end:nnnwnw #1#2#3#4 \s_@@_mark #5#6 \s_@@_stop
982 {
983 \@@_quick_split:NnNn #1
984 \@@_quick_end:nnTFNn { } \s_@@_mark {#5}
985 {#3}
986 #6 \s_@@_stop
987 }
988 \cs_new:Npn \@@_quick_only_ii_end:nnnwnw #1#2#3#4 \s_@@_mark #5#6 \s_@@_stop
989 {
990 \@@_quick_split:NnNn #2
991 \@@_quick_end:nnTFNn { } \s_@@_mark { #5 {#3} }
992 #6 \s_@@_stop
993 }
994 \cs_new:Npn \@@_quick_split_end:nnnwnw #1#2#3#4 \s_@@_mark #5#6 \s_@@_stop
995 {
996 \@@_quick_split:NnNn #2 \@@_quick_end:nnTFNn { } \s_@@_mark
997 {
998 \@@_quick_split:NnNn #1
999 \@@_quick_end:nnTFNn { } \s_@@_mark {#5}
1000 {#3}
1001 }
1002 #6 \s_@@_stop
1003 }
1004 % \end{macrocode}
1005 % \end{macro}
1006 %
1007 % \subsection{Messages}
1008 %
1009 % \begin{macro}{\@@_error:}
1010 % Bailing out of the sorting code is a bit tricky. It may not be safe
1011 % to use a delimited argument, so instead we redefine many
1012 % \pkg{l3sort} commands to be trivial, with \cs{@@_level:} jumping to
1013 % the break point. This error recovery won't work in a group.
1014 % \begin{macrocode}
1015 \cs_new_protected:Npn \@@_error:
1016 {
1017 \cs_set_eq:NN \@@_merge_blocks_aux: \prg_do_nothing:
1018 \cs_set_eq:NN \@@_merge_blocks: \prg_do_nothing:
1019 \cs_set_protected:Npn \@@_level: { \group_end: \prg_break: }
1020 }
1021 % \end{macrocode}
1022 % \end{macro}
1023 %
1024 % \begin{macro}{\@@_disable_toksdef:, \@@_disabled_toksdef:n}
1025 % While sorting, \tn{toksdef} is locally disabled to prevent users
1026 % from using \tn{newtoks} or similar commands in their comparison
1027 % code: the \tn{toks} registers that would be assigned are in use by
1028 % \pkg{l3sort}. In format mode, none of this is needed since there is
1029 % no \tn{toks} allocator.
1030 % \begin{macrocode}
1031 \cs_new_protected:Npn \@@_disable_toksdef:
1032 { \cs_set_eq:NN \toksdef \@@_disabled_toksdef:n }
1033 \cs_new_protected:Npn \@@_disabled_toksdef:n #1
1034 {
1035 \msg_error:nne { sort } { toksdef }
1036 { \token_to_str:N #1 }
1037 \@@_error:
1038 \tex_toksdef:D #1
1039 }
1040 \msg_new:nnnn { sort } { toksdef }
1041 { Allocation~of~\iow_char:N\\toks~registers~impossible~while~sorting. }
1042 {
1043 The~comparison~code~used~for~sorting~a~list~has~attempted~to~
1044 define~#1~as~a~new~\iow_char:N\\toks~register~using~
1045 \iow_char:N\\newtoks~
1046 or~a~similar~command.~The~list~will~not~be~sorted.
1047 }
1048 % \end{macrocode}
1049 % \end{macro}
1050 %
1051 % \begin{macro}{\@@_too_long_error:NNw}
1052 % When there are too many items in a sequence, this is an error, and
1053 % we clean up properly the mapping over items in the list: break using
1054 % the type-specific breaking function |#1|.
1055 % \begin{macrocode}
1056 \cs_new_protected:Npn \@@_too_long_error:NNw #1#2 \fi:
1057 {
1058 \fi:
1059 \msg_error:nneee { sort } { too-large }
1060 { \token_to_str:N #2 }
1061 { \int_eval:n { \l_@@_true_max_int - \l_@@_min_int } }
1062 { \int_eval:n { \l_@@_top_int - \l_@@_min_int } }
1063 #1 \@@_error:
1064 }
1065 \msg_new:nnnn { sort } { too-large }
1066 { The~list~#1~is~too~long~to~be~sorted~by~TeX. }
1067 {
1068 TeX~has~#2~toks~registers~still~available:~
1069 this~only~allows~to~sort~with~up~to~#3~
1070 items.~The~list~will~not~be~sorted.
1071 }
1072 % \end{macrocode}
1073 % \end{macro}
1074 %
1075 % \begin{macrocode}
1076 \msg_new:nnnn { sort } { return-none }
1077 { The~comparison~code~did~not~return. }
1078 {
1079 When~sorting~a~list,~the~code~to~compare~items~#1~and~#2~
1080 did~not~call~
1081 \iow_char:N\\sort_return_same: ~nor~
1082 \iow_char:N\\sort_return_swapped: .~
1083 Exactly~one~of~these~should~be~called.
1084 }
1085 \msg_new:nnnn { sort } { return-two }
1086 { The~comparison~code~returned~multiple~times. }
1087 {
1088 When~sorting~a~list,~the~code~to~compare~items~#1~and~#2~called~
1089 \iow_char:N\\sort_return_same: ~or~
1090 \iow_char:N\\sort_return_swapped: ~multiple~times.~
1091 Exactly~one~of~these~should~be~called.
1092 }
1093 \prop_gput:Nnn \g_msg_module_name_prop { sort } { LaTeX }
1094 \prop_gput:Nnn \g_msg_module_type_prop { sort } { }
1095 % \end{macrocode}
1096 %
1097 % \begin{macrocode}
1098 %</code>
1099 % \end{macrocode}
1100 %
1101 % \end{implementation}
1102 %
1103 % \PrintIndex