| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3bitset.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2020-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 |
% http://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 |
% \title{^^A |
| 33 |
% The \pkg{l3bitset} module\\ Bitsets^^A |
| 34 |
% } |
| 35 |
% |
| 36 |
% \author{^^A |
| 37 |
% The \LaTeX{} Project\thanks |
| 38 |
% {^^A |
| 39 |
% E-mail: |
| 40 |
% \href{mailto:[email protected]} |
| 41 |
% {[email protected]}^^A |
| 42 |
% }^^A |
| 43 |
% } |
| 44 |
% |
| 45 |
% \date{Released 2026-05-15} |
| 46 |
% |
| 47 |
% \maketitle |
| 48 |
% |
| 49 |
% \begin{documentation} |
| 50 |
% |
| 51 |
% This module defines and implements the data type \texttt{bitset}, a vector of |
| 52 |
% bits. The size of the vector may grow dynamically. |
| 53 |
% Individual bits can be set and unset by names pointing to an index position. |
| 54 |
% The names |1|, |2|, |3|, \ldots\ are predeclared and point to the |
| 55 |
% index positions $1$, $2$, $3$,\ldots. More names can be added and existing names can |
| 56 |
% be changed. |
| 57 |
% The index is like all other indices in \pkg{expl3} modules \emph{1-based}. |
| 58 |
% A \texttt{bitset} can be output as binary number or---as needed e.g. in a |
| 59 |
% PDF dictionary---as decimal (arabic) number. |
| 60 |
% Currently only a small subset of the functions provided by the \pkg{bitset} |
| 61 |
% package are implemented here, mainly the functions needed to use bitsets in |
| 62 |
% PDF dictionaries. |
| 63 |
% |
| 64 |
% The bitset is stored as a string (but one shouldn't rely on the internal |
| 65 |
% representation) and so the vector size is theoretically |
| 66 |
% unlimited, only restricted by \TeX-memory. But the functions to set and clear |
| 67 |
% bits use integer functions for the index so bitsets can't be longer |
| 68 |
% than $2^{31} - 1$. |
| 69 |
% The export function |
| 70 |
% \cs{bitset_to_arabic:N} can use functions from the \texttt{int} module only if |
| 71 |
% the largest index used for this bitset is smaller than $32$, for longer |
| 72 |
% bitsets \texttt{fp} is used and this is slower. |
| 73 |
% |
| 74 |
% \section{Creating bitsets} |
| 75 |
% |
| 76 |
% \begin{function}[added = 2023-11-15] |
| 77 |
% {\bitset_new:N,\bitset_new:c,\bitset_new:Nn, \bitset_new:cn} |
| 78 |
% \begin{syntax} |
| 79 |
% \cs{bitset_new:N} \meta{bitset var} \\ |
| 80 |
% \cs{bitset_new:Nn} \meta{bitset var} |
| 81 |
% ~~\{ |
| 82 |
% ~~~~\meta{name_1} |=| \meta{index_1} |,| |
| 83 |
% ~~~~\meta{name_2} |=| \meta{index_2} |,| \ldots{} |
| 84 |
% ~~\} |
| 85 |
% \end{syntax} |
| 86 |
% Creates a new \meta{bitset var} or raises an error if the name is already taken. |
| 87 |
% The declaration is global. The \meta{bitset var} is initially $0$. |
| 88 |
% |
| 89 |
% Bitsets are implemented as string variables consisting of |
| 90 |
% \texttt{1}'s and \texttt{0}'s. |
| 91 |
% The rightmost number is the index position $1$, so |
| 92 |
% the string variable can be viewed directly as the binary number. |
| 93 |
% But one shouldn't rely on the internal representation, but use the |
| 94 |
% dedicated \cs{bitset_to_bin:N} instead to get the binary number. |
| 95 |
% |
| 96 |
% The name--index pairs given in the second |
| 97 |
% argument of \cs{bitset_new:Nn} declares names for some indices, |
| 98 |
% which can be used to set and unset bits. |
| 99 |
% The names |1|, |2|, |3|, \ldots\ are predeclared and point to the |
| 100 |
% index positions $1$, $2$, $3$, \ldots. |
| 101 |
% |
| 102 |
% \meta{index\ldots} should be a positive number or an |
| 103 |
% \meta{integer expression} which evaluates to a positive number. |
| 104 |
% The expression is evaluated when the index is used, not at declaration time. |
| 105 |
% The names \meta{name\ldots} |
| 106 |
% should be unique. Using a number as name, e.g.~|10=1|, is allowed, it |
| 107 |
% then overwrites the predeclared name |10|, |
| 108 |
% but the index position $10$ can then only be reached if some other |
| 109 |
% name for it exists, e.g. |ten=10|. |
| 110 |
% It is not necessary to give every index |
| 111 |
% a name, and an index can have more than one name. The named index |
| 112 |
% can be extended or changed with the next function. |
| 113 |
% \end{function} |
| 114 |
% |
| 115 |
% \begin{function}[added = 2023-11-15] |
| 116 |
% {\bitset_addto_named_index:Nn} |
| 117 |
% \begin{syntax} |
| 118 |
% \cs{bitset_addto_named_index:Nn} \meta{bitset var} |
| 119 |
% ~~\{ |
| 120 |
% ~~~~\meta{name_1} |=| \meta{index_1} |,| |
| 121 |
% ~~~~\meta{name_2} |=| \meta{index_2} |,| \ldots{} |
| 122 |
% ~~\} |
| 123 |
% \end{syntax} |
| 124 |
% This extends or changes the name--index pairs for \meta{bitset var} |
| 125 |
% globally as described for \cs{bitset_new:Nn}. |
| 126 |
% \end{function} |
| 127 |
% |
| 128 |
% For example after these settings |
| 129 |
% \begin{verbatim} |
| 130 |
% \bitset_new:Nn \l_pdfannot_F_bitset |
| 131 |
% { |
| 132 |
% Invisible = 1, |
| 133 |
% Hidden = 2, |
| 134 |
% Print = 3, |
| 135 |
% NoZoom = 4, |
| 136 |
% NoRotate = 5, |
| 137 |
% NoView = 6, |
| 138 |
% ReadOnly = 7, |
| 139 |
% Locked = 8, |
| 140 |
% ToggleNoView = 9, |
| 141 |
% LockedContents = 10 |
| 142 |
% } |
| 143 |
% \bitset_addto_named_index:Nn \l_pdfannot_F_bitset |
| 144 |
% { |
| 145 |
% print = 3 |
| 146 |
% } |
| 147 |
% \end{verbatim} |
| 148 |
% it is possible to set bit $3$ by using any of these alternatives: |
| 149 |
% \begin{verbatim} |
| 150 |
% \bitset_set_true:Nn \l_pdfannot_F_bitset {Print} |
| 151 |
% \bitset_set_true:Nn \l_pdfannot_F_bitset {print} |
| 152 |
% \bitset_set_true:Nn \l_pdfannot_F_bitset {3} |
| 153 |
% \end{verbatim} |
| 154 |
% |
| 155 |
% \begin{function}[EXP, pTF,added = 2023-11-15] |
| 156 |
% {\bitset_if_exist:N, \bitset_if_exist:c} |
| 157 |
% \begin{syntax} |
| 158 |
% \cs{bitset_if_exist_p:N} \meta{bitset var} |
| 159 |
% \cs{bitset_if_exist:NTF} \meta{bitset var} \Arg{true code} \Arg{false code} |
| 160 |
% \end{syntax} |
| 161 |
% Tests whether the \meta{bitset var} exist. |
| 162 |
% \end{function} |
| 163 |
% |
| 164 |
% \section{Setting and unsetting bits} |
| 165 |
% |
| 166 |
% \begin{function}[added = 2023-11-15] |
| 167 |
% { |
| 168 |
% \bitset_set_true:Nn, \bitset_set_true:cn, |
| 169 |
% \bitset_gset_true:Nn, \bitset_gset_true:cn |
| 170 |
% } |
| 171 |
% \begin{syntax} |
| 172 |
% \cs{bitset_set_true:Nn} \meta{bitset var} \Arg{name} |
| 173 |
% \end{syntax} |
| 174 |
% This sets the bit of the index position represented by \Arg{name} to $1$. |
| 175 |
% \meta{name} should be either one of the predeclared names |
| 176 |
% |1|, |2|, |3|, \ldots, or one of the names added manually. |
| 177 |
% Index position are 1-based. |
| 178 |
% If needed the length of the bit vector is enlarged. |
| 179 |
% \end{function} |
| 180 |
% |
| 181 |
% \begin{function}[added = 2023-11-15] |
| 182 |
% { |
| 183 |
% \bitset_set_false:Nn, \bitset_set_false:cn, |
| 184 |
% \bitset_gset_false:Nn, \bitset_gset_false:cn |
| 185 |
% } |
| 186 |
% \begin{syntax} |
| 187 |
% \cs{bitset_set_false:Nn} \meta{bitset var} \Arg{name} |
| 188 |
% \end{syntax} |
| 189 |
% This unsets the bit of the index position represented by \Arg{name} (sets |
| 190 |
% it to $0$). |
| 191 |
% \meta{name} should be either one of the predeclared names |
| 192 |
% |1|, |2|, |3|, \ldots, or one of the names added manually. |
| 193 |
% The index is $1$-based. If the index position is larger |
| 194 |
% than the current length of the bit vector |
| 195 |
% nothing happens. If the leading (left most) bit is unset, |
| 196 |
% zeros are not trimmed but stay in the bit vector and are still shown |
| 197 |
% by \cs{bitset_show:N}. |
| 198 |
% \end{function} |
| 199 |
% |
| 200 |
% \begin{function}[added = 2023-11-15] |
| 201 |
% {\bitset_clear:N,\bitset_clear:c,\bitset_gclear:N,\bitset_gclear:c} |
| 202 |
% \begin{syntax} |
| 203 |
% \cs{bitset_clear:N} \meta{bitset var} |
| 204 |
% \end{syntax} |
| 205 |
% This resets the bitset to the initial state. The declared names are not changed. |
| 206 |
% \end{function} |
| 207 |
% |
| 208 |
% \section{Using bitsets} |
| 209 |
% |
| 210 |
% \begin{function}[EXP,added = 2023-11-15] |
| 211 |
% {\bitset_item:Nn, \bitset_item:cn} |
| 212 |
% \begin{syntax} |
| 213 |
% \cs{bitset_item:Nn} \meta{bitset var} \Arg{name} |
| 214 |
% \end{syntax} |
| 215 |
% \cs{bitset_item:Nn} outputs \texttt{1} if the bit with |
| 216 |
% the index number represented by \meta{name} is set and \texttt{0} otherwise. |
| 217 |
% \meta{name} is either one of the predeclared names |
| 218 |
% |1|, |2|, |3|, \ldots, or one of the names added manually. |
| 219 |
% \end{function} |
| 220 |
% |
| 221 |
% \begin{function}[EXP,added = 2023-11-15] |
| 222 |
% {\bitset_to_bin:N, \bitset_to_bin:c} |
| 223 |
% \begin{syntax} |
| 224 |
% \cs{bitset_to_bin:N} \meta{bitset var} |
| 225 |
% \end{syntax} |
| 226 |
% This leaves the current value of the bitset expressed as |
| 227 |
% a binary (string) number in the input stream. |
| 228 |
% If no bit has been set yet, the output is zero. |
| 229 |
% \end{function} |
| 230 |
% |
| 231 |
% \begin{function}[EXP,added = 2023-11-15] |
| 232 |
% {\bitset_to_arabic:N, \bitset_to_arabic:c} |
| 233 |
% \begin{syntax} |
| 234 |
% \cs{bitset_to_arabic:N} \meta{bitset var} |
| 235 |
% \end{syntax} |
| 236 |
% This leaves the current value of the bitset expressed as |
| 237 |
% a decimal number in the input stream. If no bit has been set yet, |
| 238 |
% the output is zero. The function uses \cs{int_from_bin:n} if the largest |
| 239 |
% index that have been set or unset is smaller than $32$, and a slower |
| 240 |
% implementation based on \cs{fp_eval:n} otherwise. |
| 241 |
% \end{function} |
| 242 |
% |
| 243 |
% \begin{function}[EXP, added = 2024-11-12]{\bitset_use:N, \bitset_use:c} |
| 244 |
% \begin{syntax} |
| 245 |
% \cs{bitset_use:N} \meta{bitset var} |
| 246 |
% \end{syntax} |
| 247 |
% This leaves the current value of the bitset expressed as |
| 248 |
% a binary (string) number in the input stream. |
| 249 |
% If no bit has been set yet, the output is zero. This is |
| 250 |
% functionally equivalent to \cs{bitset_to_bin:N}. |
| 251 |
% \end{function} |
| 252 |
% |
| 253 |
% \begin{function}[added = 2023-11-15] |
| 254 |
% {\bitset_show:N, \bitset_show:c} |
| 255 |
% \begin{syntax} |
| 256 |
% \cs{bitset_show:N} \meta{bitset var} |
| 257 |
% \end{syntax} |
| 258 |
% Displays the binary and decimal values of the \meta{bitset var} |
| 259 |
% on the terminal. |
| 260 |
% \end{function} |
| 261 |
% |
| 262 |
% \begin{function}[added = 2023-11-15] |
| 263 |
% {\bitset_log:N, \bitset_log:c} |
| 264 |
% \begin{syntax} |
| 265 |
% \cs{bitset_log:N} \meta{bitset var} |
| 266 |
% \end{syntax} |
| 267 |
% Writes the binary and decimal values of the \meta{bitset var} |
| 268 |
% in the log file. |
| 269 |
% \end{function} |
| 270 |
% |
| 271 |
% \begin{function}[added = 2023-11-15] |
| 272 |
% { |
| 273 |
% \bitset_show_named_index:N, \bitset_show_named_index:c |
| 274 |
% } |
| 275 |
% \begin{syntax} |
| 276 |
% \cs{bitset_show_named_index:N} \meta{bitset var} |
| 277 |
% \end{syntax} |
| 278 |
% Displays declared name--index pairs of the \meta{bitset var} |
| 279 |
% on the terminal. |
| 280 |
% \end{function} |
| 281 |
% |
| 282 |
% \begin{function}[added = 2023-12-11] |
| 283 |
% { |
| 284 |
% \bitset_log_named_index:N, \bitset_log_named_index:c |
| 285 |
% } |
| 286 |
% \begin{syntax} |
| 287 |
% \cs{bitset_log_named_index:N} \meta{bitset var} |
| 288 |
% \end{syntax} |
| 289 |
% Writes declared name--index pairs of the \meta{bitset var} |
| 290 |
% in the log file. |
| 291 |
% \end{function} |
| 292 |
% |
| 293 |
% \end{documentation} |
| 294 |
% |
| 295 |
% \begin{implementation} |
| 296 |
% |
| 297 |
% \section{\pkg{l3bitset} implementation} |
| 298 |
% |
| 299 |
% \begin{macrocode} |
| 300 |
%<*code> |
| 301 |
% \end{macrocode} |
| 302 |
% |
| 303 |
% \begin{macrocode} |
| 304 |
%<@@=bitset> |
| 305 |
% \end{macrocode} |
| 306 |
% |
| 307 |
% A bitset is a string variable. |
| 308 |
% \begin{macro}{\bitset_new:N, \bitset_new:c} |
| 309 |
% \begin{macro}{\bitset_new:Nn, \bitset_new:cn} |
| 310 |
% \begin{macrocode} |
| 311 |
\cs_new_protected:Npn \bitset_new:N #1 |
| 312 |
{ |
| 313 |
\__kernel_chk_if_free_cs:N #1 |
| 314 |
\cs_gset_eq:NN #1 \c_zero_str |
| 315 |
\prop_new:c { g__bitset_ \cs_to_str:N #1 _name_prop } |
| 316 |
} |
| 317 |
\cs_new_protected:Npn \bitset_new:Nn #1 #2 |
| 318 |
{ |
| 319 |
\__kernel_chk_if_free_cs:N #1 |
| 320 |
\cs_gset_eq:NN #1 \c_zero_str |
| 321 |
\prop_new:c { g__bitset_ \cs_to_str:N #1 _name_prop } |
| 322 |
\prop_gset_from_keyval:cn |
| 323 |
{ g__bitset_ \cs_to_str:N #1 _name_prop } |
| 324 |
{#2} |
| 325 |
} |
| 326 |
\cs_generate_variant:Nn \bitset_new:N { c } |
| 327 |
\cs_generate_variant:Nn \bitset_new:Nn { c } |
| 328 |
% \end{macrocode} |
| 329 |
% \end{macro} |
| 330 |
% \end{macro} |
| 331 |
% |
| 332 |
% \begin{macro}{\bitset_addto_named_index:Nn} |
| 333 |
% \begin{macrocode} |
| 334 |
\cs_new_protected:Npn \bitset_addto_named_index:Nn #1#2 |
| 335 |
{ |
| 336 |
\prop_gput_from_keyval:cn |
| 337 |
{ g_@@_ \cs_to_str:N #1 _name_prop } { #2 } |
| 338 |
} |
| 339 |
% \end{macrocode} |
| 340 |
% \end{macro} |
| 341 |
% \begin{macro}[EXP,pTF] |
| 342 |
% { |
| 343 |
% \bitset_if_exist:N, \bitset_if_exist:c |
| 344 |
% } |
| 345 |
% Existence tests. |
| 346 |
% \begin{macrocode} |
| 347 |
\prg_new_eq_conditional:NNn |
| 348 |
\bitset_if_exist:N \str_if_exist:N { p , T , F , TF } |
| 349 |
\prg_new_eq_conditional:NNn |
| 350 |
\bitset_if_exist:c \str_if_exist:c { p , T , F , TF } |
| 351 |
% \end{macrocode} |
| 352 |
% \end{macro} |
| 353 |
% |
| 354 |
% \begin{macro}{\@@_set_true:Nn, \@@_gset_true:Nn, \@@_set_false:Nn, \@@_gset_false:Nn} |
| 355 |
% \begin{macro}{\@@_set:NNnN} |
| 356 |
% The internal command uses only numbers (integer expressions) for the |
| 357 |
% position. |
| 358 |
% A bit is set by either extending the string or by splitting it and |
| 359 |
% then inserting an $1$. It is not checked if the value was already $1$. |
| 360 |
% \begin{macrocode} |
| 361 |
\cs_new_protected:Npn \@@_set_true:Nn #1#2 |
| 362 |
{ \@@_set:NNnN \str_set:Ne #1 {#2} 1 } |
| 363 |
\cs_new_protected:Npn \@@_gset_true:Nn #1#2 |
| 364 |
{ \@@_set:NNnN \str_gset:Ne #1 {#2} 1 } |
| 365 |
\cs_new_protected:Npn \@@_set_false:Nn #1#2 |
| 366 |
{ \@@_set:NNnN \str_set:Ne #1 {#2} 0 } |
| 367 |
\cs_new_protected:Npn \@@_gset_false:Nn #1#2 |
| 368 |
{ \@@_set:NNnN \str_gset:Ne #1 {#2} 0 } |
| 369 |
\cs_new_protected:Npn \@@_set:NNnN #1#2#3#4 |
| 370 |
{ |
| 371 |
\int_compare:nNnT {#3} > { 0 } |
| 372 |
{ |
| 373 |
\int_compare:nNnTF { \str_count:N #2 } < {#3} |
| 374 |
{ |
| 375 |
#1 #2 |
| 376 |
{ |
| 377 |
#4 |
| 378 |
\prg_replicate:nn { #3 - \str_count:N #2 - 1 } { 0 } |
| 379 |
#2 |
| 380 |
} |
| 381 |
} |
| 382 |
{ |
| 383 |
#1 #2 |
| 384 |
{ |
| 385 |
\str_range:Nnn #2 { 1 } { -1 - (#3) } |
| 386 |
#4 |
| 387 |
\str_range:Nnn #2 { 1 - (#3) } { -1 } |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
% \end{macrocode} |
| 393 |
% \end{macro} |
| 394 |
% \end{macro} |
| 395 |
% |
| 396 |
% \begin{variable}{\l_@@_tmp_int} |
| 397 |
% \begin{macrocode} |
| 398 |
\int_new:N \l_@@_tmp_int |
| 399 |
% \end{macrocode} |
| 400 |
% \end{variable} |
| 401 |
% |
| 402 |
% \begin{macro}[TF]{\@@_test_digits:n} |
| 403 |
% \begin{macro}{\@@_test_digits_end:n} |
| 404 |
% \begin{macro}{\@@_test_digits:w} |
| 405 |
% \url{https://chat.stackexchange.com/transcript/message/56878159#56878159} |
| 406 |
% \begin{macrocode} |
| 407 |
\prg_new_protected_conditional:Npnn \@@_test_digits:n #1 { TF } |
| 408 |
{ |
| 409 |
\tex_afterassignment:D \@@_test_digits:w |
| 410 |
\l_@@_tmp_int = 0 \tl_trim_spaces_apply:nN {#1} \tl_to_str:n |
| 411 |
\@@_test_digits_end: |
| 412 |
\use_i:nnn \if_false: |
| 413 |
\@@_test_digits_end: |
| 414 |
\if_int_compare:w \c_zero_int < \l_@@_tmp_int |
| 415 |
\prg_return_true: |
| 416 |
\else: |
| 417 |
\prg_return_false: |
| 418 |
\fi: |
| 419 |
} |
| 420 |
\cs_new_eq:NN \@@_test_digits_end: \exp_stop_f: |
| 421 |
\cs_new_protected:Npn \@@_test_digits:w #1 \@@_test_digits_end: { } |
| 422 |
% \end{macrocode} |
| 423 |
% \end{macro} |
| 424 |
% \end{macro} |
| 425 |
% \end{macro} |
| 426 |
% |
| 427 |
% \begin{macro} |
| 428 |
% { |
| 429 |
% \bitset_set_true:Nn, \bitset_set_true:cn, |
| 430 |
% \bitset_gset_true:Nn, \bitset_gset_true:cn, |
| 431 |
% \bitset_set_false:Nn, \bitset_set_false:cn, |
| 432 |
% \bitset_gset_false:Nn, \bitset_gset_false:cn |
| 433 |
% } |
| 434 |
% \begin{macro}{\@@_set_aux:NNn} |
| 435 |
% The user commands must first translate the argument to an index number. |
| 436 |
% \begin{macrocode} |
| 437 |
\cs_new_protected:Npn \bitset_set_true:Nn #1#2 |
| 438 |
{ \@@_set:NNn \@@_set_true:Nn #1 {#2} } |
| 439 |
\cs_new_protected:Npn \bitset_gset_true:Nn #1#2 |
| 440 |
{ \@@_set:NNn \@@_gset_true:Nn #1 {#2} } |
| 441 |
\cs_new_protected:Npn \bitset_set_false:Nn #1#2 |
| 442 |
{ \@@_set:NNn \@@_set_false:Nn #1 {#2} } |
| 443 |
\cs_new_protected:Npn \bitset_gset_false:Nn #1#2 |
| 444 |
{ \@@_set:NNn \@@_gset_false:Nn #1 {#2} } |
| 445 |
\cs_new_protected:Npn \@@_set:NNn #1#2#3 |
| 446 |
{ |
| 447 |
\prop_if_in:cnTF { g_@@_ \cs_to_str:N #2 _name_prop } {#3} |
| 448 |
{ |
| 449 |
#1 #2 |
| 450 |
{ |
| 451 |
\prop_item:cn { g_@@_ \cs_to_str:N #2 _name_prop } {#3} |
| 452 |
} |
| 453 |
} |
| 454 |
{ |
| 455 |
\@@_test_digits:nTF {#3} |
| 456 |
{ |
| 457 |
#1 #2 {#3} |
| 458 |
\prop_gput:cnn { g_@@_ \cs_to_str:N #2 _name_prop } {#3} {#3} |
| 459 |
} |
| 460 |
{ |
| 461 |
\msg_warning:nnee { bitset } { unknown-name } |
| 462 |
{ \token_to_str:N #2 } |
| 463 |
{ \tl_to_str:n {#3} } |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
\cs_generate_variant:Nn \bitset_set_true:Nn { c } |
| 468 |
\cs_generate_variant:Nn \bitset_gset_true:Nn { c } |
| 469 |
\cs_generate_variant:Nn \bitset_set_false:Nn { c } |
| 470 |
\cs_generate_variant:Nn \bitset_gset_false:Nn { c } |
| 471 |
% \end{macrocode} |
| 472 |
% \end{macro} |
| 473 |
% \end{macro} |
| 474 |
% |
| 475 |
% \begin{macro} |
| 476 |
% { |
| 477 |
% \bitset_clear:N, \bitset_clear:c, |
| 478 |
% \bitset_gclear:N, \bitset_gclear:c |
| 479 |
% } |
| 480 |
% \begin{macrocode} |
| 481 |
\cs_new_protected:Npn \bitset_clear:N #1 |
| 482 |
{ |
| 483 |
\str_set_eq:NN #1 \c_zero_str |
| 484 |
} |
| 485 |
\cs_new_protected:Npn \bitset_gclear:N #1 |
| 486 |
{ |
| 487 |
\str_gset_eq:NN #1 \c_zero_str |
| 488 |
} |
| 489 |
\cs_generate_variant:Nn \bitset_clear:N { c } |
| 490 |
\cs_generate_variant:Nn \bitset_gclear:N { c } |
| 491 |
% \end{macrocode} |
| 492 |
% \end{macro} |
| 493 |
% |
| 494 |
% \begin{macro}[EXP] |
| 495 |
% { |
| 496 |
% \bitset_to_arabic:N, \bitset_to_arabic:c, |
| 497 |
% \bitset_to_bin:N, \bitset_to_bin:c, |
| 498 |
% } |
| 499 |
% \begin{macro}[EXP]{\@@_to_int:nN} |
| 500 |
% The naming of the commands follow the names in the \texttt{int} module. |
| 501 |
% \cs{bitset_to_arabic:N} uses \cs{int_from_bin:n} if the string is shorter |
| 502 |
% than $32$ and the slower \cs{fp_eval:n} for larger bitsets. |
| 503 |
% \begin{macrocode} |
| 504 |
\cs_new:Npn \bitset_to_arabic:N #1 |
| 505 |
{ |
| 506 |
\int_compare:nNnTF { \str_count:N #1 } < { 32 } |
| 507 |
{ \exp_args:No \int_from_bin:n {#1} } |
| 508 |
{ |
| 509 |
\exp_after:wN \@@_to_int:nN \exp_after:wN 0 |
| 510 |
#1 \q_recursion_tail \q_recursion_stop |
| 511 |
} |
| 512 |
} |
| 513 |
\cs_new:Npn \@@_to_int:nN #1#2 |
| 514 |
{ |
| 515 |
\quark_if_recursion_tail_stop_do:Nn #2 {#1} |
| 516 |
\exp_args:Nf \@@_to_int:nN { \fp_eval:n { #1 * 2 + #2 } } |
| 517 |
} |
| 518 |
\cs_new:Npn \bitset_to_bin:N #1 |
| 519 |
{ |
| 520 |
#1 |
| 521 |
} |
| 522 |
\cs_generate_variant:Nn \bitset_to_arabic:N { c } |
| 523 |
\cs_generate_variant:Nn \bitset_to_bin:N { c } |
| 524 |
% \end{macrocode} |
| 525 |
% \end{macro} |
| 526 |
% \end{macro} |
| 527 |
% |
| 528 |
% \begin{macro}{\bitset_use:N, \bitset_use:c} |
| 529 |
% \begin{macrocode} |
| 530 |
\cs_new_eq:NN \bitset_use:N \tl_use:N |
| 531 |
\cs_generate_variant:Nn \bitset_use:N { c } |
| 532 |
% \end{macrocode} |
| 533 |
% \end{macro} |
| 534 |
% |
| 535 |
% \begin{macro} |
| 536 |
% { |
| 537 |
% \bitset_item:Nn, \bitset_item:cn |
| 538 |
% } |
| 539 |
% All bits that have been set at anytime have an entry in the prop, |
| 540 |
% so we can take everything else as $0$. |
| 541 |
% \begin{macrocode} |
| 542 |
\cs_new:Npn \bitset_item:Nn #1#2 |
| 543 |
{ |
| 544 |
\prop_if_in:cnTF { g_@@_ \cs_to_str:N #1 _name_prop } {#2} |
| 545 |
{ |
| 546 |
\int_eval:n |
| 547 |
{ |
| 548 |
\str_item:Nn #1 |
| 549 |
{ 0 - ( \prop_item:cn { g_@@_ \cs_to_str:N #1 _name_prop } {#2} ) } |
| 550 |
+0 |
| 551 |
} |
| 552 |
} |
| 553 |
{ |
| 554 |
0 |
| 555 |
} |
| 556 |
} |
| 557 |
\cs_generate_variant:Nn \bitset_item:Nn { c } |
| 558 |
% \end{macrocode} |
| 559 |
% \end{macro} |
| 560 |
% |
| 561 |
% \begin{macro} |
| 562 |
% { |
| 563 |
% \bitset_show:N, \bitset_show:c, |
| 564 |
% \bitset_log:N, \bitset_log:c |
| 565 |
% } |
| 566 |
% \begin{macrocode} |
| 567 |
\cs_new_protected:Npn \bitset_show:N { \@@_show:NN \msg_show:nneeee } |
| 568 |
\cs_generate_variant:Nn \bitset_show:N { c } |
| 569 |
\cs_new_protected:Npn \bitset_log:N { \@@_show:NN \msg_log:nneeee } |
| 570 |
\cs_generate_variant:Nn \bitset_log:N { c } |
| 571 |
\cs_new_protected:Npn \@@_show:NN #1#2 |
| 572 |
{ |
| 573 |
\__kernel_chk_defined:NT #2 |
| 574 |
{ |
| 575 |
#1 { bitset } { show } |
| 576 |
{ \token_to_str:N #2 } |
| 577 |
{ \bitset_to_bin:N #2 } |
| 578 |
{ \bitset_to_arabic:N #2 } |
| 579 |
{ } |
| 580 |
} |
| 581 |
} |
| 582 |
% \end{macrocode} |
| 583 |
% \end{macro} |
| 584 |
% |
| 585 |
% \begin{macro} |
| 586 |
% { |
| 587 |
% \bitset_show_named_index:N, \bitset_show_named_index:c, |
| 588 |
% \bitset_log_named_index:N, \bitset_log_named_index:c |
| 589 |
% } |
| 590 |
% \begin{macrocode} |
| 591 |
\cs_new_protected:Npn \bitset_show_named_index:N |
| 592 |
{ \@@_show_named_index:NN \msg_show:nneeee } |
| 593 |
\cs_generate_variant:Nn \bitset_show_named_index:N { c } |
| 594 |
\cs_new_protected:Npn \bitset_log_named_index:N |
| 595 |
{ \@@_show_named_index:NN \msg_log:nneeee } |
| 596 |
\cs_generate_variant:Nn \bitset_log_named_index:N { c } |
| 597 |
\cs_new_protected:Npn \@@_show_named_index:NN #1#2 |
| 598 |
{ |
| 599 |
\__kernel_chk_defined:NT #2 |
| 600 |
{ |
| 601 |
#1 { bitset } { show-names } |
| 602 |
{ \token_to_str:N #2 } |
| 603 |
{ \prop_map_function:cN { g_@@_ \cs_to_str:N #2 _name_prop } \msg_show_item:nn } |
| 604 |
{ } { } |
| 605 |
} |
| 606 |
} |
| 607 |
% \end{macrocode} |
| 608 |
% \end{macro} |
| 609 |
% |
| 610 |
% \subsection{Messages} |
| 611 |
% \begin{macrocode} |
| 612 |
\msg_new:nnn { bitset } { show } |
| 613 |
{ |
| 614 |
The~bitset~#1~has~the~representation: \\ |
| 615 |
>~binary:~#2 \\ |
| 616 |
>~arabic:~#3 . |
| 617 |
} |
| 618 |
\msg_new:nnn { bitset } { show-names } |
| 619 |
{ |
| 620 |
The~bitset~#1~ |
| 621 |
\tl_if_empty:nTF {#2} |
| 622 |
{ knows~no~names~yet \\>~ . } |
| 623 |
{ knows~the~name/index~pairs~(without~outer~braces): #2 . } |
| 624 |
} |
| 625 |
\msg_new:nnn { bitset } { unknown-name } |
| 626 |
{ The~name~'#2'~is~unknown~for~bitset~\tl_to_str:n {#1} } |
| 627 |
\prop_gput:Nnn \g_msg_module_name_prop { bitset } { LaTeX } |
| 628 |
\prop_gput:Nnn \g_msg_module_type_prop { bitset } { } |
| 629 |
% \end{macrocode} |
| 630 |
% |
| 631 |
% \begin{macrocode} |
| 632 |
%</code> |
| 633 |
% \end{macrocode} |
| 634 |
% |
| 635 |
% \end{implementation} |
| 636 |
% |
| 637 |
% \PrintIndex |