@isopodlabs/utilities 1.5.4 → 1.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bits.js ADDED
@@ -0,0 +1,605 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DenseBits = exports.ImmutableDenseBits = exports.SparseBits = exports.ImmutableSparseBits = void 0;
4
+ exports.lowestSet32 = lowestSet32;
5
+ exports.highestSet32 = highestSet32;
6
+ exports.countSet32 = countSet32;
7
+ exports.highestSet = highestSet;
8
+ exports.lowestSet = lowestSet;
9
+ exports.countSet = countSet;
10
+ exports.highestClear = highestClear;
11
+ exports.lowestClear = lowestClear;
12
+ exports.countClear = countClear;
13
+ const algorithm_1 = require("./algorithm");
14
+ //-----------------------------------------------------------------------------
15
+ // Bit twiddling functions
16
+ //-----------------------------------------------------------------------------
17
+ function lowestSet32(x) {
18
+ return x === 0 ? 32 : 31 - Math.clz32(x & -x);
19
+ }
20
+ function highestSet32(x) {
21
+ return x ? 32 - Math.clz32(x) : 0;
22
+ }
23
+ function countSet32(x) {
24
+ x = x - ((x >> 1) & 0x55555555);
25
+ x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
26
+ return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
27
+ }
28
+ /*
29
+ const testersShift: bigint[] = []; //32 << i
30
+ const testers: bigint[] = []; //1 << (32 << i)
31
+ const masks: bigint[][] = [];
32
+
33
+ export function highestSetCached(x: bigint): number {
34
+ if (x < 0n)
35
+ x = ~x;
36
+
37
+ let k = 0
38
+ for (;;) {
39
+ if (!testers[k]) {
40
+ testersShift[k] = BigInt(32 << k);
41
+ testers[k] = 1n << testersShift[k];
42
+ }
43
+ if (x < testers[k])
44
+ break
45
+ k++
46
+ }
47
+
48
+ if (k === 0)
49
+ return highestSet32(Number(x));
50
+
51
+ // determine length by bisection
52
+ k--
53
+ let i = 1 << k;
54
+ let a = x >> testersShift[k];
55
+ while (k--) {
56
+ let b = a >> testersShift[k]
57
+ if (b) {
58
+ i += 1 << k;
59
+ a = b;
60
+ }
61
+ }
62
+
63
+ return (i + 1) * 32 - Math.clz32(Number(a));
64
+ }
65
+
66
+
67
+ export function bitcountCached(x: bigint): number {
68
+ let k = 0;
69
+ for (let t = x >> 32n; t;)
70
+ t >>= BigInt(32 << k++);
71
+
72
+ if (!masks[k]) {
73
+ const limit = 1n << BigInt(32 << k);
74
+ const nmasks = highestSet32(k + 5);
75
+ masks[k] = Array.from({ length: nmasks }, (_, j) => limit / ((1n << BigInt(1 << j)) + 1n));
76
+ }
77
+
78
+ masks[k].forEach((mask, j) =>
79
+ x = (x & mask) + ((x >> BigInt(1 << j)) & mask)
80
+ );
81
+
82
+ for (let i = masks[k].length; i < k + 5; ++i)
83
+ x += x >> BigInt(1 << i);
84
+
85
+ return Number(x & 0xFFFFFFFFn);
86
+ }
87
+ */
88
+ function highestSet(x) {
89
+ if (x < 0)
90
+ x = ~x;
91
+ if (x < 0x100000000)
92
+ return highestSet32(Number(x));
93
+ // For numbers within safe float range, use log2
94
+ if (x <= Number.MAX_VALUE) {
95
+ const b = Math.floor(Math.log2(Number(x)));
96
+ return (1n << BigInt(b)) <= x ? b + 1 : b;
97
+ }
98
+ // For arbitrarily large bigints, use bit-shifting method
99
+ let y = BigInt(x);
100
+ let s = 0;
101
+ let k = 0;
102
+ for (let t = y >> 32n; t; t >>= BigInt(s)) {
103
+ s = 32 << k++;
104
+ y = t;
105
+ }
106
+ if (k) {
107
+ // determine length by bisection
108
+ k--;
109
+ while (k--) {
110
+ const b = y >> BigInt(32 << k);
111
+ if (b) {
112
+ s += 32 << k;
113
+ y = b;
114
+ }
115
+ }
116
+ }
117
+ return (s + 32) - Math.clz32(Number(y));
118
+ }
119
+ function lowestSet(x) {
120
+ if (x < 0x100000000)
121
+ return lowestSet32(Number(x));
122
+ const xb = BigInt(x < 0 ? ~x : x);
123
+ return highestSet(xb & -xb) - 1;
124
+ }
125
+ function countSet(x) {
126
+ if (x < 0)
127
+ x = ~x;
128
+ if (x < 0x100000000)
129
+ return countSet32(Number(x));
130
+ x = BigInt(x);
131
+ let k = 5;
132
+ for (let t = x >> 32n; t;)
133
+ t >>= BigInt(1 << k++);
134
+ const n = 1 << k;
135
+ const limit = 1n << BigInt(n);
136
+ let i = 1;
137
+ for (; i < k; i <<= 1) {
138
+ const bi = BigInt(i);
139
+ const mask = limit / ((1n << bi) + 1n);
140
+ x = (x & mask) + ((x >> bi) & mask);
141
+ }
142
+ //we can add the rest with a multiply and shift (which turns out to be slower)
143
+ //const mask = limit / ((1n << bi) - 1n);
144
+ //x = (x * mask) >> BigInt(n - i);
145
+ //we can skip the masking when the total can fit
146
+ for (; i < n; i <<= 1)
147
+ x += x >> BigInt(i);
148
+ return Number(x & 0xffffffffn);
149
+ }
150
+ function highestClear(x) {
151
+ return highestSet(~x);
152
+ }
153
+ function lowestClear(x) {
154
+ return lowestSet(~x);
155
+ }
156
+ function countClear(x) {
157
+ return countSet(~x);
158
+ }
159
+ //-----------------------------------------------------------------------------
160
+ // SparseBits - a sparse bitset implementation, where each entry in the 'bits' array represents 32 bits
161
+ // The 'undef' member indicates whether undefined entries are treated as 0 or 0xffffffff
162
+ //-----------------------------------------------------------------------------
163
+ class ImmutableSparseBits {
164
+ bits = []; //each entry represents 32 bits
165
+ undef;
166
+ static *whereGenerator(bits, undef, set, from = -1) {
167
+ ++from;
168
+ if (undef ? !set : set) {
169
+ const keys = Object.keys(bits).map(k => +k);
170
+ if (keys.length === 0)
171
+ return;
172
+ const i = from >> 5;
173
+ let k = (0, algorithm_1.lowerBound)(keys, i);
174
+ let v = bits[keys[k]] ^ undef;
175
+ if (keys[k] === i)
176
+ v &= -(1 << (from & 0x1f));
177
+ for (;;) {
178
+ const i = keys[k];
179
+ while (v) {
180
+ yield (i << 5) + lowestSet32(v);
181
+ v = v & (v - 1);
182
+ }
183
+ ++k;
184
+ if (k === keys.length)
185
+ return;
186
+ v = bits[keys[k]] ^ undef;
187
+ }
188
+ }
189
+ else {
190
+ let i = from >> 5;
191
+ let v = ((bits[i] ?? undef) ^ ~undef) & -(1 << (from & 0x1f));
192
+ for (;;) {
193
+ while (v) {
194
+ yield (i << 5) + lowestSet32(v);
195
+ v = v & (v - 1);
196
+ }
197
+ ++i;
198
+ v = ((bits[i] ?? undef) ^ ~undef);
199
+ }
200
+ }
201
+ }
202
+ constructor(initial = false) {
203
+ this.undef = initial ? -1 : 0;
204
+ }
205
+ create(init) {
206
+ return new this.constructor(init);
207
+ }
208
+ copyUndefined(other) {
209
+ for (const i in other.bits) {
210
+ if (this.bits[i] === undefined)
211
+ this.bits[i] = other.bits[i];
212
+ }
213
+ return this;
214
+ }
215
+ flipUndefined(other) {
216
+ for (const i in other.bits) {
217
+ if (this.bits[i] === undefined)
218
+ this.bits[i] = ~other.bits[i];
219
+ }
220
+ return this;
221
+ }
222
+ static fromEntries(entries, initial = false) {
223
+ const r = new this(initial);
224
+ if (Array.isArray(entries)) {
225
+ for (const [k, v] of entries)
226
+ r.bits[k] = v;
227
+ }
228
+ else {
229
+ for (const [k, v] of Object.entries(entries))
230
+ r.bits[+k] = v;
231
+ }
232
+ return r;
233
+ }
234
+ keys() {
235
+ return Object.keys(this.bits).map(k => +k);
236
+ }
237
+ entries() {
238
+ //return this.bits;
239
+ return Object.entries(this.bits).map(([k, v]) => [+k, v]);
240
+ }
241
+ complement() {
242
+ const result = this.create(this.undef === 0);
243
+ for (const i in this.bits)
244
+ result.bits[i] = ~this.bits[i];
245
+ return result;
246
+ }
247
+ intersect(other) {
248
+ const result = this.create(!!(this.undef & other.undef));
249
+ for (const i in this.bits)
250
+ result.bits[i] = this.bits[i] & other.bits[i];
251
+ return this.undef ? result.copyUndefined(other) : result;
252
+ }
253
+ union(other) {
254
+ const result = this.create(!!(this.undef | other.undef));
255
+ for (const i in other.bits)
256
+ result.bits[i] = this.bits[i] | other.bits[i];
257
+ return this.undef ? result : result.copyUndefined(other);
258
+ }
259
+ xor(other) {
260
+ const result = this.create(!!(this.undef ^ other.undef));
261
+ for (const i in this.bits)
262
+ result.bits[i] = this.bits[i] ^ other.bits[i];
263
+ return this.undef ? result.flipUndefined(other) : result.copyUndefined(other);
264
+ }
265
+ clean() {
266
+ for (const i in this.bits) {
267
+ if (this.bits[i] === this.undef)
268
+ delete this.bits[i];
269
+ }
270
+ return this;
271
+ }
272
+ contains(other) {
273
+ if (other.undef && !this.undef)
274
+ return false;
275
+ for (const i in other.bits) {
276
+ if (other.bits[i] & ~(this.bits[i] ?? this.undef))
277
+ return false;
278
+ }
279
+ return true;
280
+ }
281
+ has(a) {
282
+ return !!((this.bits[a >> 5] ?? this.undef) & (1 << (a & 0x1f)));
283
+ }
284
+ next(a, set = true) {
285
+ ++a;
286
+ const xor = this.undef;
287
+ if (xor)
288
+ set = !set;
289
+ if (set) {
290
+ const keys = Object.keys(this.bits).map(k => +k);
291
+ if (keys.length === 0)
292
+ return -1;
293
+ const ai = a >> 5;
294
+ let i = (0, algorithm_1.lowerBound)(keys, ai);
295
+ let v = this.bits[keys[i]] ^ xor;
296
+ if (keys[i] === ai)
297
+ v &= -(1 << (a & 0x1f));
298
+ while (!v) {
299
+ ++i;
300
+ if (i === keys.length)
301
+ return -1;
302
+ v = this.bits[keys[i]] ^ xor;
303
+ }
304
+ return (keys[i] << 5) + lowestSet32(v);
305
+ }
306
+ else {
307
+ let i = a >> 5;
308
+ if (this.bits[i] === undefined)
309
+ return a;
310
+ let v = (this.bits[i] ^ xor) | ((1 << (a & 0x1f)) - 1);
311
+ while (!v) {
312
+ ++i;
313
+ if (this.bits[i] === undefined)
314
+ break;
315
+ v = this.bits[i] ^ xor;
316
+ }
317
+ return (i << 5) + lowestSet32(~v);
318
+ }
319
+ }
320
+ toDense() {
321
+ let bits = 0n;
322
+ if (this.undef) {
323
+ for (const i in this.bits)
324
+ bits |= BigInt(~this.bits[i]) << BigInt(+i * 32);
325
+ bits = ~bits;
326
+ }
327
+ else {
328
+ for (const i in this.bits)
329
+ bits |= BigInt(this.bits[i]) << BigInt(+i * 32);
330
+ }
331
+ return new DenseBits(bits);
332
+ }
333
+ where(set, from = -1) {
334
+ return {
335
+ [Symbol.iterator]: () => ImmutableSparseBits.whereGenerator(this.bits, this.undef, set, from)
336
+ };
337
+ }
338
+ ranges() {
339
+ const bits = this.bits;
340
+ const undef = this.undef;
341
+ return {
342
+ *[Symbol.iterator]() {
343
+ let start = -1, end = 0;
344
+ for (const i in bits) {
345
+ let b = bits[i] ^ undef;
346
+ const c0 = +i * 32;
347
+ while ((start < 0 ? b : ~b) !== 0) {
348
+ if (start === -1) {
349
+ start = c0 + lowestSet32(b);
350
+ if (undef)
351
+ yield [end, start];
352
+ end = -1;
353
+ b = b | (b - 1);
354
+ }
355
+ else {
356
+ end = c0 + lowestSet32(~b);
357
+ if (!undef)
358
+ yield [start, end];
359
+ start = -1;
360
+ b = b & (b + 1);
361
+ }
362
+ }
363
+ if (start >= 0 && bits[+i + 1] === undefined) {
364
+ if (!undef)
365
+ yield [start, c0 + 32];
366
+ start = -1;
367
+ }
368
+ }
369
+ if (undef)
370
+ yield [end, Infinity];
371
+ }
372
+ };
373
+ }
374
+ *[Symbol.iterator]() {
375
+ yield* ImmutableSparseBits.whereGenerator(this.bits, this.undef, true, -1);
376
+ //for (let i = this.next(-1); i !== -1; i = this.next(i))
377
+ // yield i;
378
+ }
379
+ }
380
+ exports.ImmutableSparseBits = ImmutableSparseBits;
381
+ class SparseBits extends ImmutableSparseBits {
382
+ setMask(i, m) {
383
+ if (this.bits[i] !== undefined)
384
+ this.bits[i] |= m;
385
+ else if (!this.undef)
386
+ this.bits[i] = m;
387
+ }
388
+ clearMask(i, m) {
389
+ if (this.bits[i] !== undefined)
390
+ this.bits[i] &= ~m;
391
+ else if (this.undef)
392
+ this.bits[i] = ~m;
393
+ }
394
+ selfComplement() {
395
+ this.undef = ~this.undef;
396
+ for (const i in this.bits)
397
+ this.bits[i] = ~this.bits[i];
398
+ return this;
399
+ }
400
+ selfIntersect(other) {
401
+ for (const i in this.bits)
402
+ this.bits[i] &= other.bits[i];
403
+ if (this.undef)
404
+ this.copyUndefined(other);
405
+ this.undef &= other.undef;
406
+ return this;
407
+ }
408
+ selfUnion(other) {
409
+ for (const i in other.bits)
410
+ this.bits[i] |= other.bits[i];
411
+ if (!this.undef)
412
+ this.copyUndefined(other);
413
+ this.undef |= other.undef;
414
+ return this;
415
+ }
416
+ selfXor(other) {
417
+ for (const i in this.bits)
418
+ this.bits[i] ^= other.bits[i];
419
+ if (this.undef)
420
+ this.flipUndefined(other);
421
+ else
422
+ this.copyUndefined(other);
423
+ this.undef &= other.undef;
424
+ return this;
425
+ }
426
+ set(a) {
427
+ this.setMask(a >> 5, 1 << (a & 0x1f));
428
+ }
429
+ clear(a) {
430
+ this.clearMask(a >> 5, 1 << (a & 0x1f));
431
+ }
432
+ has(a) {
433
+ const i = a >> 5;
434
+ return !!((this.bits[i] ?? this.undef) & (1 << (a & 0x1f)));
435
+ }
436
+ setRange(a, b) {
437
+ let i = a >> 5, j = b >> 5;
438
+ if (i === j) {
439
+ this.setMask(i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)));
440
+ }
441
+ else {
442
+ this.setMask(i++, -(1 << (a & 0x1f)));
443
+ if (this.undef) {
444
+ while (i < j)
445
+ delete this.bits[i++];
446
+ }
447
+ else {
448
+ while (i < j)
449
+ this.bits[i++] = -1;
450
+ }
451
+ this.setMask(i, (1 << (b & 0x1f)) - 1);
452
+ }
453
+ return this;
454
+ }
455
+ clearRange(a, b) {
456
+ let i = a >> 5, j = b >> 5;
457
+ if (i === j) {
458
+ this.clearMask(i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)));
459
+ }
460
+ else {
461
+ this.clearMask(i++, -(1 << (a & 0x1f)));
462
+ if (!this.undef) {
463
+ while (i < j)
464
+ delete this.bits[i++];
465
+ }
466
+ else {
467
+ while (i < j)
468
+ this.bits[i++] = 0;
469
+ }
470
+ this.clearMask(i, (1 << (b & 0x1f)) - 1);
471
+ }
472
+ return this;
473
+ }
474
+ }
475
+ exports.SparseBits = SparseBits;
476
+ ;
477
+ //-----------------------------------------------------------------------------
478
+ // DenseBits - a dense bitset implementation using bigint
479
+ //-----------------------------------------------------------------------------
480
+ class ImmutableDenseBits {
481
+ bits;
482
+ constructor(bits = 0n) {
483
+ this.bits = bits;
484
+ }
485
+ create(bits) {
486
+ return new this.constructor(bits);
487
+ }
488
+ complement() {
489
+ return this.create(~this.bits);
490
+ }
491
+ intersect(other) {
492
+ return this.create(this.bits & other.bits);
493
+ }
494
+ union(other) {
495
+ return this.create(this.bits | other.bits);
496
+ }
497
+ xor(other) {
498
+ return this.create(this.bits ^ other.bits);
499
+ }
500
+ has(a) {
501
+ return !!(this.bits & (1n << BigInt(a)));
502
+ }
503
+ next(a, set = true) {
504
+ let s = this.bits >> BigInt(a + 1);
505
+ s = set ? s & -s : (s + 1n) & ~s;
506
+ return s ? a + highestSet(s) : -1;
507
+ }
508
+ get length() {
509
+ return highestSet(this.bits);
510
+ }
511
+ where(set, from = -1) {
512
+ let bits = this.bits >> BigInt(from + 1);
513
+ return {
514
+ *[Symbol.iterator]() {
515
+ while (bits) {
516
+ const i = highestSet(set ? bits & -bits : (bits + 1n) & ~bits);
517
+ from += i;
518
+ yield from;
519
+ bits >>= BigInt(i);
520
+ }
521
+ }
522
+ };
523
+ /*
524
+ const self = this;
525
+ return {
526
+ *[Symbol.iterator](): Generator<number> {
527
+ for (let i = self.next(-1, set); i !== -1; i = self.next(i, set))
528
+ yield i;
529
+ }
530
+ };
531
+ */
532
+ }
533
+ ranges() {
534
+ let bits = this.bits;
535
+ return {
536
+ *[Symbol.iterator]() {
537
+ let offset = 0;
538
+ while (bits) {
539
+ const i = highestSet(bits & -bits);
540
+ bits >>= BigInt(i);
541
+ const j = highestSet(~bits & (bits + 1n));
542
+ bits >>= BigInt(j);
543
+ yield [offset + i - 1, offset + i + j - 1];
544
+ offset += i + j;
545
+ }
546
+ }
547
+ };
548
+ }
549
+ *[Symbol.iterator]() {
550
+ yield* this.where(true);
551
+ //for (let i = this.next(-1); i !== -1; i = this.next(i))
552
+ // yield i;
553
+ }
554
+ toSparse() {
555
+ const sparse = {};
556
+ for (let bits = this.bits, i = 0; bits; bits >>= 32n, i++) {
557
+ const v = Number(bits & 0xffffffffn);
558
+ if (v)
559
+ sparse[i] = v;
560
+ }
561
+ return SparseBits.fromEntries(sparse, false);
562
+ }
563
+ }
564
+ exports.ImmutableDenseBits = ImmutableDenseBits;
565
+ ;
566
+ class DenseBits extends ImmutableDenseBits {
567
+ setMask(m) {
568
+ this.bits |= m;
569
+ }
570
+ clearMask(m) {
571
+ this.bits &= ~m;
572
+ }
573
+ selfComplement() {
574
+ this.bits = ~this.bits;
575
+ return this;
576
+ }
577
+ selfIntersect(other) {
578
+ this.bits &= other.bits;
579
+ return this;
580
+ }
581
+ selfUnion(other) {
582
+ this.bits |= other.bits;
583
+ return this;
584
+ }
585
+ selfXor(other) {
586
+ this.bits ^= other.bits;
587
+ return this;
588
+ }
589
+ set(a) {
590
+ this.setMask(1n << BigInt(a));
591
+ }
592
+ clear(a) {
593
+ this.clearMask(1n << BigInt(a));
594
+ }
595
+ setRange(a, b) {
596
+ this.setMask((1n << BigInt(b)) - (1n << BigInt(a)));
597
+ return this;
598
+ }
599
+ clearRange(a, b) {
600
+ this.clearMask((1n << BigInt(b)) - (1n << BigInt(a)));
601
+ return this;
602
+ }
603
+ }
604
+ exports.DenseBits = DenseBits;
605
+ ;
package/dist/glob.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare function parseGlob(glob: string): string;
2
+ export declare function anchoredRe(re: string): RegExp;
3
+ export declare function globToRe(glob: string): RegExp;
4
+ export declare function globToReMulti(globs: string[]): RegExp;