Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3intarray.dtx
4 %
5 % Copyright (C) 2017-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 %
34 % \title{^^A
35 % The \pkg{l3intarray} module\\ Fast global integer arrays^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-15}
48 %
49 % \maketitle
50 %
51 % \begin{documentation}
52 %
53 % For applications requiring heavy use of integers, this module provides
54 % arrays which can be accessed in constant time (contrast \pkg{l3seq},
55 % where access time is linear). These arrays have several important
56 % features
57 % \begin{itemize}
58 % \item The size of the array is fixed and must be given at
59 % point of initialization.
60 % \item The absolute value of each entry has maximum $2^{30}-1$
61 % (i.e., one power lower than the usual \cs{c_max_int},
62 % ceiling of $2^{31}-1$).
63 % This maximum absolute value is available as \cs{c_max_intarray_int},
64 % documented in the \pkg{l3int} module.
65 % \end{itemize}
66 % The use of \texttt{intarray} data is therefore recommended for cases where
67 % the need for fast access is of paramount importance.
68 %
69 % \section{Creating and initializing integer array variables}
70 %
71 % \begin{function}[added = 2018-03-29]{\intarray_new:Nn, \intarray_new:cn}
72 % \begin{syntax}
73 % \cs{intarray_new:Nn} \meta{intarray~var} \Arg{size}
74 % \end{syntax}
75 % Evaluates the integer expression \meta{size} and allocates an
76 % \meta{integer array variable} with that number of (zero) entries.
77 % The variable name should start with |\g_| because assignments are
78 % always global.
79 % \end{function}
80 %
81 % \begin{function}[added = 2018-05-04]
82 % {\intarray_const_from_clist:Nn, \intarray_const_from_clist:cn}
83 % \begin{syntax}
84 % \cs{intarray_const_from_clist:Nn} \meta{intarray~var} \Arg{int expr clist}
85 % \end{syntax}
86 % Creates a new constant \meta{integer array variable} or raises an
87 % error if the name is already taken. The \meta{integer array
88 % variable} is set (globally) to contain as its items the results of
89 % evaluating each \meta{integer expression} in the \meta{comma list}.
90 % \end{function}
91 %
92 % \begin{function}[added = 2018-05-04]{\intarray_gzero:N, \intarray_gzero:c}
93 % \begin{syntax}
94 % \cs{intarray_gzero:N} \meta{intarray~var}
95 % \end{syntax}
96 % Sets all entries of the \meta{integer array variable} to zero.
97 % Assignments are always global.
98 % \end{function}
99 %
100 % \section{Adding data to integer arrays}
101 %
102 % \begin{function}[added = 2018-03-29]{\intarray_gset:Nnn, \intarray_gset:cnn}
103 % \begin{syntax}
104 % \cs{intarray_gset:Nnn} \meta{intarray~var} \Arg{position} \Arg{value}
105 % \end{syntax}
106 % Stores the result of evaluating the integer expression \meta{value}
107 % into the \meta{integer array variable} at the (integer expression)
108 % \meta{position}. If the \meta{position} is not between $1$ and the
109 % \cs{intarray_count:N}, or the \meta{value}'s absolute value is
110 % bigger than $2^{30}-1$, an error occurs. Assignments are always
111 % global.
112 % \end{function}
113 %
114 % \section{Counting entries in integer arrays}
115 %
116 % \begin{function}[EXP, added = 2018-03-29]{\intarray_count:N, \intarray_count:c}
117 % \begin{syntax}
118 % \cs{intarray_count:N} \meta{intarray~var}
119 % \end{syntax}
120 % Expands to the number of entries in the \meta{integer array variable}.
121 % Contrarily to \cs{seq_count:N} this is performed in constant time.
122 % \end{function}
123 %
124 % \section{Using a single entry}
125 %
126 % \begin{function}[EXP, added = 2018-03-29]{\intarray_item:Nn, \intarray_item:cn}
127 % \begin{syntax}
128 % \cs{intarray_item:Nn} \meta{intarray~var} \Arg{position}
129 % \end{syntax}
130 % Expands to the integer entry stored at the (integer expression)
131 % \meta{position} in the \meta{integer array variable}. If the
132 % \meta{position} is not between $1$ and the \cs{intarray_count:N}, an
133 % error occurs.
134 % \end{function}
135 %
136 % \begin{function}[EXP, added = 2018-05-05]
137 % {\intarray_rand_item:N, \intarray_rand_item:c}
138 % \begin{syntax}
139 % \cs{intarray_rand_item:N} \meta{intarray~var}
140 % \end{syntax}
141 % Selects a pseudo-random item of the \meta{integer array}. If the
142 % \meta{integer array} is empty, produce an error.
143 % \end{function}
144 %
145 % \section{Integer array conditional}
146 %
147 % \begin{function}[pTF, added = 2024-03-31]
148 % {\intarray_if_exist:N, \intarray_if_exist:c}
149 % \begin{syntax}
150 % \cs{intarray_if_exist_p:N} \meta{intarray~var}
151 % \cs{intarray_if_exist:NTF} \meta{intarray~var} \Arg{true code} \Arg{false code}
152 % \end{syntax}
153 % Tests whether the \meta{intarray~var} is currently defined. This
154 % does not check that the \meta{intarray~var} really is an integer
155 % array variable.
156 % \end{function}
157 %
158 % \section{Viewing integer arrays}
159 %
160 % \begin{function}[added = 2018-05-04]
161 % {\intarray_show:N, \intarray_show:c, \intarray_log:N, \intarray_log:c}
162 % \begin{syntax}
163 % \cs{intarray_show:N} \meta{intarray~var}
164 % \cs{intarray_log:N} \meta{intarray~var}
165 % \end{syntax}
166 % Displays the items in the \meta{integer array variable} in the
167 % terminal or writes them in the log file.
168 % \end{function}
169 %
170 % \section{Implementation notes}
171 %
172 % It is a wrapper around the \tn{fontdimen} primitive, used to store
173 % arrays of integers (with a restricted range: absolute value at most
174 % $2^{30}-1$). In contrast to \pkg{l3seq} sequences the access to
175 % individual entries is done in constant time rather than linear time,
176 % but only integers can be stored. More precisely, the primitive
177 % \tn{fontdimen} stores dimensions but the \pkg{l3intarray} module
178 % transparently converts these from/to integers. Assignments are always
179 % global.
180 %
181 % While \LuaTeX{}'s memory is extensible, other engines can
182 % \enquote{only} deal with a bit less than $4\times 10^6$ entries in all
183 % \tn{fontdimen} arrays combined (with default \TeX{} Live settings).
184 %
185 % \end{documentation}
186 %
187 % \begin{implementation}
188 %
189 % \section{\pkg{l3intarray} implementation}
190 %
191 % \begin{macrocode}
192 %<@@=intarray>
193 % \end{macrocode}
194 %
195 % \begin{macrocode}
196 %<*code>
197 % \end{macrocode}
198 %
199 % There are two implementations for this module: One \cs{fontdimen} based one
200 % for more traditional \TeX\ engines and a Lua based one for engines with Lua support.
201 %
202 % Both versions do not allow negative array sizes.
203 % \begin{macrocode}
204 \msg_new:nnn { kernel } { negative-array-size }
205 { Size~of~array~may~not~be~negative:~#1 }
206 % \end{macrocode}
207 %
208 % \begin{macro}{\@@_sep:}
209 % \begin{macrocode}
210 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
211 % \end{macrocode}
212 % \end{macro}
213 %
214 % \begin{variable}{\l_@@_loop_int}
215 % A loop index.
216 % \begin{macrocode}
217 \int_new:N \l_@@_loop_int
218 % \end{macrocode}
219 % \end{variable}
220 %
221 % \subsection{Lua implementation}
222 % First, let's look at the Lua variant:
223 %
224 % We select the Lua version if the Lua helpers were defined. This can be detected by
225 % the presence of \cs{@@_gset_count:Nw}.
226 %
227 % \begin{macrocode}
228 \cs_if_exist:NTF \@@_gset_count:Nw
229 {
230 % \end{macrocode}
231 %
232 % \subsubsection{Allocating arrays}
233 %
234 % \begin{variable}{\g_@@_table_int, \l_@@_bad_index_int}
235 % Used to differentiate intarrays in Lua and to record an invalid index.
236 % \begin{macrocode}
237 \int_new:N \g_@@_table_int
238 \int_new:N \l_@@_bad_index_int
239 %</code>
240 % \end{macrocode}
241 % \end{variable}
242 %
243 % \begin{macro}{\@@:w}
244 % Used as marker for intarrays in Lua. Followed by an unbraced number
245 % identifying the array and a single space. This format is used to make it
246 % easy to scan from Lua.
247 % \begin{macrocode}
248 %<*lua>
249 luacmd('@@:w', function()
250 scan_int()
251 tex.error'LaTeX Error: Isolated intarray ignored'
252 end, 'protected', 'global')
253 %</lua>
254 % \end{macrocode}
255 % \end{macro}
256 %
257 % \begin{macro}{\intarray_new:Nn, \intarray_new:cn}
258 % \begin{macro}{\@@_new:N}
259 % Declare |#1| as a tokenlist with the scanmark and a unique number.
260 % Pass the array's size to the Lua helper.
261 % Every \texttt{intarray} must be global; it's enough to run this
262 % check in \cs{intarray_new:Nn}.
263 % \begin{macrocode}
264 %<*code>
265 \cs_new_protected:Npn \@@_new:N #1
266 {
267 \__kernel_chk_if_free_cs:N #1
268 \int_gincr:N \g_@@_table_int
269 \cs_gset_nopar:Npe #1 { \@@:w \int_use:N \g_@@_table_int \c_space_tl }
270 }
271 \cs_new_protected:Npn \intarray_new:Nn #1#2
272 {
273 \@@_new:N #1
274 \@@_gset_count:Nw #1 \int_eval:n {#2} \scan_stop:
275 \int_compare:nNnT { \intarray_count:N #1 } < 0
276 {
277 \msg_error:nne { kernel } { negative-array-size }
278 { \intarray_count:N #1 }
279 }
280 }
281 \cs_generate_variant:Nn \intarray_new:Nn { c }
282 %</code>
283 % \end{macrocode}
284 % \end{macro}
285 % \end{macro}
286 %
287 % Before we get to the first command implemented in Lua, we first need some
288 % definitions. Since \texttt{token.create} only works correctly if \TeX{}
289 % has seen the tokens before, we first run a short \TeX{} sequence to ensure
290 % that all relevant control sequences are known.
291 % \begin{macrocode}
292 %<*lua>
293
294 local scan_token = token.scan_token
295 local put_next = token.put_next
296 local intarray_marker = token_create_safe'@@:w'
297 local use_none = token_create_safe'use_none:n'
298 local use_i = token_create_safe'use:n'
299 local expand_after_scan_stop = {token_create_safe'exp_after:wN',
300 token_create_safe'scan_stop:'}
301 local comma = token_create(string.byte',')
302 % \end{macrocode}
303 %
304 % \begin{macro}{@@_table}
305 % Internal helper to scan an intarray token, extract the associated
306 % Lua table and return an error if the input is invalid.
307 %
308 % \begin{macrocode}
309 local @@_table do
310 local tables = get_luadata and get_luadata'@@' or {[0] = {}}
311 function @@_table()
312 local t = scan_token()
313 if t ~= intarray_marker then
314 put_next(t)
315 tex.error'LaTeX Error: intarray expected'
316 return tables[0]
317 end
318 local i = scan_int()
319 local current_table = tables[i]
320 if current_table then return current_table end
321 current_table = {}
322 tables[i] = current_table
323 return current_table
324 end
325 % \end{macrocode}
326 % Since in \LaTeX{} this is loaded in the format, we want to preserve any intarrays
327 % which are created while format building for the actual run.
328 %
329 % To do this, we use the \texttt{register_luadata} mechanism from \pkg{l3luatex}:
330 % Directly before the format get dumped, the following function gets invoked and serializes
331 % all existing tables into a string. This string gets compiled and dumped into the format and
332 % is made available at the beginning of regular runs as \texttt{get_luadata'@@'}.
333 % \begin{macrocode}
334 if register_luadata then
335 register_luadata('@@', function()
336 local t = "{[0]={},"
337 for i=1, #tables do
338 t = string.format("%s{%s},", t, table.concat(tables[i], ','))
339 end
340 return t .. "}"
341 end)
342 end
343 end
344 % \end{macrocode}
345 % \end{macro}
346 %
347 % \begin{macro}[EXP]{\intarray_count:N, \intarray_count:c}
348 % \begin{macro}[EXP]{\@@_gset_count:Nw}
349 % Set and get the size of an array. ``Setting the size'' means in this context that
350 % we add zeros until we reach the desired size.
351 % \begin{macrocode}
352
353 local sprint = tex.sprint
354
355 luacmd('@@_gset_count:Nw', function()
356 local t = @@_table()
357 local n = scan_int()
358 for i=#t+1, n do t[i] = 0 end
359 end, 'protected', 'global')
360
361 luacmd('intarray_count:N', function()
362 sprint(-2, #@@_table())
363 end, 'global')
364 %</lua>
365 % \end{macrocode}
366 %
367 % \begin{macrocode}
368 %<*code>
369 \cs_generate_variant:Nn \intarray_count:N { c }
370 %</code>
371 % \end{macrocode}
372 % \end{macro}
373 % \end{macro}
374 %
375 % \subsubsection{Array items}
376 %
377 % \begin{macro}{\@@_gset:wF, \@@_gset:w}
378 % The setter provided by Lua. The argument order somewhat emulates the |\fontdimen|:
379 % First the array index, followed by the intarray and then the new value.
380 % This has been chosen over a more conventional order to provide a delimiter for the numbers.
381 % \begin{macrocode}
382 %<*lua>
383 luacmd('@@_gset:wF', function()
384 local i = scan_int()
385 local t = @@_table()
386 if t[i] then
387 t[i] = scan_int()
388 put_next(use_none)
389 else
390 tex.count.l_@@_bad_index_int = i
391 scan_int()
392 put_next(use_i)
393 end
394 end, 'protected', 'global')
395
396 luacmd('@@_gset:w', function()
397 local i = scan_int()
398 local t = @@_table()
399 t[i] = scan_int()
400 end, 'protected', 'global')
401 %</lua>
402 % \end{macrocode}
403 % \end{macro}
404 %
405 % \begin{macro}{\intarray_gset:Nnn, \intarray_gset:cnn, \__kernel_intarray_gset:Nnn}
406 % The \cs{__kernel_intarray_gset:Nnn} function does not use
407 % \cs{int_eval:n}, namely its arguments must be suitable for
408 % \cs{int_value:w}. The user version checks the position and value
409 % are within bounds.
410 % \begin{macrocode}
411 %<*code>
412 \cs_new_protected:Npn \__kernel_intarray_gset:Nnn #1#2#3
413 { \@@_gset:w #2 #1 #3 \scan_stop: }
414 \cs_new_protected:Npn \intarray_gset:Nnn #1#2#3
415 {
416 \@@_gset:wF \int_eval:n {#2} #1 \int_eval:n{#3}
417 {
418 \msg_error:nneee { kernel } { out-of-bounds }
419 { \token_to_str:N #1 } { \int_use:N \l_@@_bad_index_int } { \intarray_count:N #1 }
420 }
421 }
422 \cs_generate_variant:Nn \intarray_gset:Nnn { c }
423 %</code>
424 % \end{macrocode}
425 % \end{macro}
426 %
427 % \begin{macro}{\intarray_gzero:N, \intarray_gzero:c}
428 % Set the appropriate array entry to zero. No bound checking
429 % needed.
430 % \begin{macrocode}
431 %<*lua>
432 luacmd('intarray_gzero:N', function()
433 local t = @@_table()
434 for i=1, #t do
435 t[i] = 0
436 end
437 end, 'global', 'protected')
438 %</lua>
439 %<*code>
440 \cs_generate_variant:Nn \intarray_gzero:N { c }
441 %</code>
442 % \end{macrocode}
443 % \end{macro}
444 %
445 % \begin{macro}[EXP]{\intarray_item:Nn, \intarray_item:cn, \__kernel_intarray_item:Nn}
446 % \begin{macro}{\@@_item:wF,\@@_item:w}
447 % Get the appropriate entry and perform bound checks. The
448 % \cs{__kernel_intarray_item:Nn} function omits bound checks and omits
449 % \cs{int_eval:n}, namely its argument must be a \TeX{} integer
450 % suitable for \cs{int_value:w}.
451 % \begin{macrocode}
452 %<*lua>
453 luacmd('@@_item:wF', function()
454 local i = scan_int()
455 local t = @@_table()
456 local item = t[i]
457 if item then
458 put_next(use_none)
459 else
460 tex.l_@@_bad_index_int = i
461 put_next(use_i)
462 end
463 put_next(expand_after_scan_stop)
464 scan_token()
465 if item then
466 sprint(-2, item)
467 end
468 end, 'global')
469
470 luacmd('@@_item:w', function()
471 local i = scan_int()
472 local t = @@_table()
473 sprint(-2, t[i])
474 end, 'global')
475 %</lua>
476 % \end{macrocode}
477 %
478 % \begin{macrocode}
479 %<*code>
480 \cs_new:Npn \__kernel_intarray_item:Nn #1#2
481 { \@@_item:w #2 #1 }
482 \cs_new:Npn \intarray_item:Nn #1#2
483 {
484 \@@_item:wF \int_eval:n {#2} #1
485 {
486 \msg_expandable_error:nnfff { kernel } { out-of-bounds }
487 { \token_to_str:N #1 } { \int_use:N \l_@@_bad_index_int } { \intarray_count:N #1 }
488 0
489 }
490 }
491 \cs_generate_variant:Nn \intarray_item:Nn { c }
492 % \end{macrocode}
493 % \end{macro}
494 % \end{macro}
495 %
496 % \begin{macro}{\intarray_rand_item:N, \intarray_rand_item:c}
497 % Importantly, \cs{intarray_item:Nn} only evaluates its argument once.
498 % \begin{macrocode}
499 \cs_new:Npn \intarray_rand_item:N #1
500 { \intarray_item:Nn #1 { \int_rand:n { \intarray_count:N #1 } } }
501 \cs_generate_variant:Nn \intarray_rand_item:N { c }
502 % \end{macrocode}
503 % \end{macro}
504 %
505 % \subsubsection{Working with contents of integer arrays}
506 %
507 % \begin{macro}{\intarray_const_from_clist:Nn, \intarray_const_from_clist:cn}
508 % We use the \cs{__kernel_intarray_gset:Nnn} which does not do bounds checking
509 % and instead automatically resizes the array.
510 % This is not implemented in Lua to ensure that the clist parsing is consistent
511 % with the clist module.
512 % \begin{macrocode}
513 \cs_new_protected:Npn \intarray_const_from_clist:Nn #1#2
514 {
515 \@@_new:N #1
516 \int_zero:N \l_@@_loop_int
517 \clist_map_inline:nn {#2}
518 {
519 \int_incr:N \l_@@_loop_int
520 \__kernel_intarray_gset:Nnn #1 \l_@@_loop_int { \int_eval:n {##1} } }
521 }
522 \cs_generate_variant:Nn \intarray_const_from_clist:Nn { c }
523 % \end{macrocode}
524 % \end{macro}
525 %
526 % \begin{macro}[rEXP]{\@@_to_clist:Nn, \@@_to_clist:w}
527 % The \cs{@@_to_clist:Nn} auxiliary allows to choose the delimiter and
528 % is also used in \cs{intarray_show:N}. Here we just pass the information
529 % to Lua and let \texttt{table.concat} do the actual work.
530 % We discard the category codes of the passed delimiter but this is not
531 % an issue since the delimiter is always just a comma or a comma and a space.
532 % In both cases \texttt{sprint(2, ...)} provides the right catcodes.
533 % \begin{macrocode}
534 %</code>
535 %<*lua>
536 local concat = table.concat
537 luacmd('@@_to_clist:Nn', function()
538 local t = @@_table()
539 local sep = token.scan_string()
540 sprint(-2, concat(t, sep))
541 end, 'global')
542 %</lua>
543 % \end{macrocode}
544 % \end{macro}
545 %
546 % \begin{macro}[rEXP]{\__kernel_intarray_range_to_clist:Nnn, \@@_range_to_clist:w}
547 % Loop through part of the array.
548 % \begin{macrocode}
549 %<*code>
550 \cs_new:Npn \__kernel_intarray_range_to_clist:Nnn #1#2#3
551 {
552 \@@_range_to_clist:w #1
553 \int_eval:n {#2} ~ \int_eval:n {#3} ~
554 }
555 %</code>
556 %<*lua>
557 luacmd('@@_range_to_clist:w', function()
558 local t = @@_table()
559 local from = scan_int()
560 local to = scan_int()
561 sprint(-2, concat(t, ',', from, to))
562 end, 'global')
563 %</lua>
564 % \end{macrocode}
565 % \end{macro}
566 %
567 % \begin{macro}{\__kernel_intarray_gset_range_from_clist:Nnn, \@@_gset_range:nNw}
568 % Loop through part of the array. We allow additional commas at the end.
569 % \begin{macrocode}
570 %<*code>
571 \cs_new_protected:Npn \__kernel_intarray_gset_range_from_clist:Nnn #1#2#3
572 {
573 \@@_gset_range:w \int_eval:w #2 #1 #3 , , \scan_stop:
574 }
575 %</code>
576 %<*lua>
577 luacmd('@@_gset_range:w', function()
578 local from = scan_int()
579 local t = @@_table()
580 while true do
581 local tok = scan_token()
582 if tok == comma then
583 repeat
584 tok = scan_token()
585 until tok ~= comma
586 break
587 else
588 put_next(tok)
589 end
590 t[from] = scan_int()
591 scan_token()
592 from = from + 1
593 end
594 end, 'global', 'protected')
595 %</lua>
596 % \end{macrocode}
597 % \end{macro}
598 %
599 % \begin{macro}{\@@_gset_overflow_test:nw}
600 % In order to allow some code sharing later we provide the
601 % \cs{@@_gset_overflow_test:nw} name here. It doesn't actually test anything
602 % since the Lua implementation accepts all integers which could be tested with
603 % \cs{tex_ifabsnum:D}.
604 % \begin{macrocode}
605 %<*code>
606 \cs_new_protected:Npn \@@_gset_overflow_test:nw #1
607 {
608 }
609 % \end{macrocode}
610 % \end{macro}
611 %
612 % \subsection{Font dimension based implementation}
613 %
614 % Go to the false branch of the conditional above.
615 % \begin{macrocode}
616 }
617 {
618 % \end{macrocode}
619 %
620 % \subsubsection{Allocating arrays}
621 %
622 % \begin{macro}{\@@_entry:w, \@@_count:w}
623 % We use these primitives quite a lot in this module.
624 % \begin{macrocode}
625 \cs_new_eq:NN \@@_entry:w \tex_fontdimen:D
626 \cs_new_eq:NN \@@_count:w \tex_hyphenchar:D
627 % \end{macrocode}
628 % \end{macro}
629 %
630 % \begin{variable}{\c_@@_sp_dim}
631 % Used to convert integers to dimensions fast.
632 % \begin{macrocode}
633 \dim_const:Nn \c_@@_sp_dim { 1 sp }
634 % \end{macrocode}
635 % \end{variable}
636 %
637 % \begin{variable}{\g_@@_font_int}
638 % Used to assign one font per array.
639 % \begin{macrocode}
640 \int_new:N \g_@@_font_int
641 % \end{macrocode}
642 % \end{variable}
643 %
644 % \begin{macro}{\intarray_new:Nn, \intarray_new:cn}
645 % \begin{macro}{\@@_new:N}
646 % Declare |#1| to be a font (arbitrarily |cmr10| at a never-used
647 % size). Store the array's size as the \tn{hyphenchar} of that font
648 % and make sure enough \tn{fontdimen} are allocated, by setting the
649 % last one. Then clear any \tn{fontdimen} that |cmr10| starts with.
650 % It seems \LuaTeX{}'s |cmr10| has an extra \tn{fontdimen} parameter
651 % number $8$ compared to other engines (for a math font we would
652 % replace $8$ by $22$ or some such).
653 % Every \texttt{intarray} must be global; it's enough to run this
654 % check in \cs{intarray_new:Nn}.
655 % \begin{macrocode}
656 \cs_new_protected:Npn \@@_new:N #1
657 {
658 \__kernel_chk_if_free_cs:N #1
659 \int_gincr:N \g_@@_font_int
660 \tex_global:D \tex_font:D #1
661 = cmr10~at~ \g_@@_font_int \c_@@_sp_dim \scan_stop:
662 \int_step_inline:nn { 8 }
663 { \__kernel_intarray_gset:Nnn #1 {##1} \c_zero_int }
664 }
665 \cs_new_protected:Npn \intarray_new:Nn #1#2
666 {
667 \@@_new:N #1
668 \@@_count:w #1 = \int_eval:n {#2} \scan_stop:
669 \int_compare:nNnT { \intarray_count:N #1 } < 0
670 {
671 \msg_error:nne { kernel } { negative-array-size }
672 { \intarray_count:N #1 }
673 }
674 \int_compare:nNnT { \intarray_count:N #1 } > 0
675 { \__kernel_intarray_gset:Nnn #1 { \intarray_count:N #1 } { 0 } }
676 }
677 \cs_generate_variant:Nn \intarray_new:Nn { c }
678 % \end{macrocode}
679 % \end{macro}
680 % \end{macro}
681 %
682 % \begin{macro}[EXP]{\intarray_count:N, \intarray_count:c}
683 % Size of an array.
684 % \begin{macrocode}
685 \cs_new:Npn \intarray_count:N #1 { \int_value:w \@@_count:w #1 }
686 \cs_generate_variant:Nn \intarray_count:N { c }
687 % \end{macrocode}
688 % \end{macro}
689 %
690 % \subsubsection{Array items}
691 %
692 % \begin{macro}[EXP]{\@@_signed_max_dim:n}
693 % Used when an item to be stored is larger than \cs{c_max_dim} in
694 % absolute value; it is replaced by $\pm\cs{c_max_dim}$.
695 % \begin{macrocode}
696 \cs_new:Npn \@@_signed_max_dim:n #1
697 { \int_value:w \int_compare:nNnT {#1} < 0 { - } \c_max_dim }
698 % \end{macrocode}
699 % \end{macro}
700 %
701 % \begin{macro}[EXP]{\@@_bounds:NNnTF, \@@_bounds_error:NNnw}
702 % The functions \cs{intarray_gset:Nnn} and \cs{intarray_item:Nn} share
703 % bounds checking. The |T| branch is used if |#3| is within bounds of
704 % the array |#2|.
705 % \begin{macrocode}
706 \cs_new:Npn \@@_bounds:NNnTF #1#2#3
707 {
708 \if_int_compare:w 1 > #3 \exp_stop_f:
709 \@@_bounds_error:NNnw #1 #2 {#3}
710 \else:
711 \if_int_compare:w #3 > \intarray_count:N #2 \exp_stop_f:
712 \@@_bounds_error:NNnw #1 #2 {#3}
713 \fi:
714 \fi:
715 \use_i:nn
716 }
717 \cs_new:Npn \@@_bounds_error:NNnw #1#2#3#4 \use_i:nn #5#6
718 {
719 #4
720 #1 { kernel } { out-of-bounds }
721 { \token_to_str:N #2 } {#3} { \intarray_count:N #2 }
722 #6
723 }
724 % \end{macrocode}
725 % \end{macro}
726 %
727 % \begin{macro}{\intarray_gset:Nnn, \intarray_gset:cnn, \__kernel_intarray_gset:Nnn}
728 % \begin{macro}{\@@_gset:Nnn, \@@_gset_overflow:Nnn}
729 % Set the appropriate \tn{fontdimen}. The
730 % \cs{__kernel_intarray_gset:Nnn} function does not use
731 % \cs{int_eval:n}, namely its arguments must be suitable for
732 % \cs{int_value:w}. The user version checks the position and value
733 % are within bounds.
734 % \begin{macrocode}
735 \cs_new_protected:Npn \__kernel_intarray_gset:Nnn #1#2#3
736 { \@@_entry:w #2 #1 #3 \c_@@_sp_dim }
737 \cs_new_protected:Npn \intarray_gset:Nnn #1#2#3
738 {
739 \exp_after:wN \@@_gset:Nww
740 \exp_after:wN #1
741 \int_value:w \int_eval:n {#2} \exp_after:wN \@@_sep:
742 \int_value:w \int_eval:n {#3} \@@_sep:
743 }
744 \cs_generate_variant:Nn \intarray_gset:Nnn { c }
745 \cs_new_protected:Npn \@@_gset:Nww #1#2 \@@_sep: #3 \@@_sep:
746 {
747 \@@_bounds:NNnTF \msg_error:nneee #1 {#2}
748 {
749 \@@_gset_overflow_test:nw {#3}
750 \__kernel_intarray_gset:Nnn #1 {#2} {#3}
751 }
752 { }
753 }
754 \cs_if_exist:NTF \tex_ifabsnum:D
755 {
756 \cs_new_protected:Npn \@@_gset_overflow_test:nw #1
757 {
758 \tex_ifabsnum:D #1 > \c_max_dim
759 \exp_after:wN \@@_gset_overflow:NNnn
760 \fi:
761 }
762 }
763 {
764 \cs_new_protected:Npn \@@_gset_overflow_test:nw #1
765 {
766 \if_int_compare:w \int_abs:n {#1} > \c_max_dim
767 \exp_after:wN \@@_gset_overflow:NNnn
768 \fi:
769 }
770 }
771 \cs_new_protected:Npn \@@_gset_overflow:NNnn #1#2#3#4
772 {
773 \msg_error:nneeee { kernel } { overflow }
774 { \token_to_str:N #2 } {#3} {#4} { \@@_signed_max_dim:n {#4} }
775 #1 #2 {#3} { \@@_signed_max_dim:n {#4} }
776 }
777 % \end{macrocode}
778 % \end{macro}
779 % \end{macro}
780 %
781 % \begin{macro}{\intarray_gzero:N, \intarray_gzero:c}
782 % Set the appropriate \tn{fontdimen} to zero. No bound checking
783 % needed. The \cs{prg_replicate:nn} possibly uses quite a lot of
784 % memory, but this is somewhat comparable to the size of the array,
785 % and it is much faster than an \cs{int_step_inline:nn} loop.
786 % \begin{macrocode}
787 \cs_new_protected:Npn \intarray_gzero:N #1
788 {
789 \int_zero:N \l_@@_loop_int
790 \prg_replicate:nn { \intarray_count:N #1 }
791 {
792 \int_incr:N \l_@@_loop_int
793 \@@_entry:w \l_@@_loop_int #1 \c_zero_dim
794 }
795 }
796 \cs_generate_variant:Nn \intarray_gzero:N { c }
797 % \end{macrocode}
798 % \end{macro}
799 %
800 % \begin{macro}[EXP]{\intarray_item:Nn, \intarray_item:cn, \__kernel_intarray_item:Nn}
801 % \begin{macro}{\@@_item:Nw}
802 % Get the appropriate \tn{fontdimen} and perform bound checks. The
803 % \cs{__kernel_intarray_item:Nn} function omits bound checks and omits
804 % \cs{int_eval:n}, namely its argument must be a \TeX{} integer
805 % suitable for \cs{int_value:w}.
806 % \begin{macrocode}
807 \cs_new:Npn \__kernel_intarray_item:Nn #1#2
808 { \int_value:w \@@_entry:w #2 #1 }
809 \cs_new:Npn \intarray_item:Nn #1#2
810 {
811 \exp_after:wN \@@_item:Nw
812 \exp_after:wN #1
813 \int_value:w \int_eval:n {#2} \@@_sep:
814 }
815 \cs_generate_variant:Nn \intarray_item:Nn { c }
816 \cs_new:Npn \@@_item:Nw #1#2 \@@_sep:
817 {
818 \@@_bounds:NNnTF \msg_expandable_error:nnfff #1 {#2}
819 { \__kernel_intarray_item:Nn #1 {#2} }
820 { 0 }
821 }
822 % \end{macrocode}
823 % \end{macro}
824 % \end{macro}
825 %
826 % \begin{macro}{\intarray_rand_item:N, \intarray_rand_item:c}
827 % Importantly, \cs{intarray_item:Nn} only evaluates its argument once.
828 % \begin{macrocode}
829 \cs_new:Npn \intarray_rand_item:N #1
830 { \intarray_item:Nn #1 { \int_rand:n { \intarray_count:N #1 } } }
831 \cs_generate_variant:Nn \intarray_rand_item:N { c }
832 % \end{macrocode}
833 % \end{macro}
834 %
835 % \subsubsection{Working with contents of integer arrays}
836 %
837 % \begin{macro}{\intarray_const_from_clist:Nn, \intarray_const_from_clist:cn}
838 % \begin{macro}{\@@_const_from_clist:nN}
839 % Similar to \cs{intarray_new:Nn} (which we don't use because when
840 % debugging is enabled that function checks the variable name starts
841 % with |g_|). We make use of the fact that \TeX{} allows allocation
842 % of successive \tn{fontdimen} as long as no other font has been
843 % declared: no need to count the comma list items first. We need the
844 % code in \cs{intarray_gset:Nnn} that checks the item value is not too
845 % big, namely \cs{@@_gset_overflow_test:nw}, but not the code that
846 % checks bounds. At the end, set the size of the intarray.
847 % \begin{macrocode}
848 \cs_new_protected:Npn \intarray_const_from_clist:Nn #1#2
849 {
850 \@@_new:N #1
851 \int_zero:N \l_@@_loop_int
852 \clist_map_inline:nn {#2}
853 { \exp_args:Nf \@@_const_from_clist:nN { \int_eval:n {##1} } #1 }
854 \@@_count:w #1 \l_@@_loop_int
855 }
856 \cs_generate_variant:Nn \intarray_const_from_clist:Nn { c }
857 \cs_new_protected:Npn \@@_const_from_clist:nN #1#2
858 {
859 \int_incr:N \l_@@_loop_int
860 \@@_gset_overflow_test:nw {#1}
861 \__kernel_intarray_gset:Nnn #2 \l_@@_loop_int {#1}
862 }
863 % \end{macrocode}
864 % \end{macro}
865 % \end{macro}
866 %
867 % \begin{macro}[rEXP]{\@@_to_clist:Nn, \@@_to_clist:w}
868 % Loop through the array, putting a comma before each item. Remove
869 % the leading comma with |f|-expansion. We also use the auxiliary in
870 % \cs{intarray_show:N} with argument comma, space.
871 % \begin{macrocode}
872 \cs_new:Npn \@@_to_clist:Nn #1#2
873 {
874 \int_compare:nNnF { \intarray_count:N #1 } = \c_zero_int
875 {
876 \exp_last_unbraced:Nf \use_none:n
877 { \@@_to_clist:w 1 \@@_sep: #1 {#2} \prg_break_point: }
878 }
879 }
880 \cs_new:Npn \@@_to_clist:w #1 \@@_sep: #2#3
881 {
882 \if_int_compare:w #1 > \@@_count:w #2
883 \prg_break:n
884 \fi:
885 #3 \__kernel_intarray_item:Nn #2 {#1}
886 \exp_after:wN \@@_to_clist:w
887 \int_value:w \int_eval:w #1 + \c_one_int \@@_sep: #2 {#3}
888 }
889 % \end{macrocode}
890 % \end{macro}
891 %
892 % \begin{macro}[rEXP]{\__kernel_intarray_range_to_clist:Nnn, \@@_range_to_clist:ww}
893 % Loop through part of the array.
894 % \begin{macrocode}
895 \cs_new:Npn \__kernel_intarray_range_to_clist:Nnn #1#2#3
896 {
897 \exp_last_unbraced:Nf \use_none:n
898 {
899 \exp_after:wN \@@_range_to_clist:ww
900 \int_value:w \int_eval:w #2 \exp_after:wN \@@_sep:
901 \int_value:w \int_eval:w #3 \@@_sep:
902 #1 \prg_break_point:
903 }
904 }
905 \cs_new:Npn \@@_range_to_clist:ww #1 \@@_sep: #2 \@@_sep: #3
906 {
907 \if_int_compare:w #1 > #2 \exp_stop_f:
908 \prg_break:n
909 \fi:
910 , \__kernel_intarray_item:Nn #3 {#1}
911 \exp_after:wN \@@_range_to_clist:ww
912 \int_value:w \int_eval:w #1 + \c_one_int \@@_sep: #2 \@@_sep: #3
913 }
914 % \end{macrocode}
915 % \end{macro}
916 %
917 % \begin{macro}{\__kernel_intarray_gset_range_from_clist:Nnn, \@@_gset_range:Nw}
918 % Loop through part of the array.
919 % \begin{macrocode}
920 \cs_new_protected:Npn \__kernel_intarray_gset_range_from_clist:Nnn #1#2#3
921 {
922 \int_set:Nn \l_@@_loop_int {#2}
923 \@@_gset_range:Nw #1 #3 , , \prg_break_point:
924 }
925 \cs_new_protected:Npn \@@_gset_range:Nw #1 #2 ,
926 {
927 \if_catcode:w \scan_stop: \tl_to_str:n {#2} \scan_stop:
928 \prg_break:n
929 \fi:
930 \__kernel_intarray_gset:Nnn #1 \l_@@_loop_int {#2}
931 \int_incr:N \l_@@_loop_int
932 \@@_gset_range:Nw #1
933 }
934 % \end{macrocode}
935 % \end{macro}
936 %
937 % \begin{macrocode}
938 }
939 % \end{macrocode}
940 %
941 % \subsection{Common parts}
942 %
943 % \begin{macro}[pTF]{\intarray_if_exist:N, \intarray_if_exist:c}
944 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
945 % \begin{macrocode}
946 \prg_new_eq_conditional:NNn \intarray_if_exist:N \cs_if_exist:N
947 { TF , T , F , p }
948 \prg_new_eq_conditional:NNn \intarray_if_exist:c \cs_if_exist:c
949 { TF , T , F , p }
950 % \end{macrocode}
951 % \end{macro}
952 %
953 % \begin{macro}{\intarray_show:N, \intarray_show:c, \intarray_log:N, \intarray_log:c}
954 % Convert the list to a comma list (with spaces after each comma)
955 % \begin{macrocode}
956 \cs_new_protected:Npn \intarray_show:N { \@@_show:NN \msg_show:nneeee }
957 \cs_generate_variant:Nn \intarray_show:N { c }
958 \cs_new_protected:Npn \intarray_log:N { \@@_show:NN \msg_log:nneeee }
959 \cs_generate_variant:Nn \intarray_log:N { c }
960 \cs_new_protected:Npn \@@_show:NN #1#2
961 {
962 \__kernel_chk_defined:NT #2
963 {
964 #1 { intarray } { show }
965 { \token_to_str:N #2 }
966 { \intarray_count:N #2 }
967 { >~ \@@_to_clist:Nn #2 { , ~ } }
968 { }
969 }
970 }
971 % \end{macrocode}
972 % \end{macro}
973 %
974 % \begin{macrocode}
975 %</code>
976 % \end{macrocode}
977 %
978 % \end{implementation}
979 %
980 % \PrintIndex