@isopodlabs/utilities 1.5.9 → 1.7.0

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.
Files changed (3) hide show
  1. package/dist/bits.d.ts +148 -57
  2. package/dist/bits.js +946 -344
  3. package/package.json +1 -1
package/dist/bits.js CHANGED
@@ -1,22 +1,24 @@
1
1
  "use strict";
2
+ //-----------------------------------------------------------------------------
3
+ // Bit twiddling functions
4
+ //-----------------------------------------------------------------------------
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DenseBits = exports.ImmutableDenseBits = exports.SparseBits = exports.ImmutableSparseBits = void 0;
6
+ exports.SparseBits2 = exports.SparseBits = exports.DenseBits32 = exports.DenseBits = void 0;
4
7
  exports.lowestSet32 = lowestSet32;
5
8
  exports.highestSet32 = highestSet32;
9
+ exports.highestSet1024 = highestSet1024;
6
10
  exports.countSet32 = countSet32;
7
11
  exports.nthSet32 = nthSet32;
12
+ exports.reverse32 = reverse32;
8
13
  exports.highestSet = highestSet;
9
14
  exports.lowestSet = lowestSet;
10
15
  exports.countSet = countSet;
11
16
  exports.nthSet = nthSet;
17
+ exports.reverse = reverse;
12
18
  exports.highestClear = highestClear;
13
19
  exports.lowestClear = lowestClear;
14
20
  exports.countClear = countClear;
15
21
  exports.clearLowest = clearLowest;
16
- const algorithm_1 = require("./algorithm");
17
- //-----------------------------------------------------------------------------
18
- // Bit twiddling functions
19
- //-----------------------------------------------------------------------------
20
22
  // Returns the index (0-31) of the lowest set bit, or 32 if none
21
23
  function lowestSet32(x) {
22
24
  return x === 0 ? 32 : 31 - Math.clz32(x & -x);
@@ -25,6 +27,10 @@ function lowestSet32(x) {
25
27
  function highestSet32(x) {
26
28
  return x ? 32 - Math.clz32(x) : 0;
27
29
  }
30
+ function highestSet1024(x) {
31
+ let b = Math.floor(Math.log2(x));
32
+ return 1n << BigInt(b) <= x ? b + 1 : b;
33
+ }
28
34
  // Returns the number of set bits
29
35
  function countSet32(x) {
30
36
  x = x - ((x >> 1) & 0x55555555);
@@ -61,6 +67,13 @@ function nthSet32(x, i) {
61
67
  ++n;
62
68
  return n;
63
69
  }
70
+ function reverse32(x) {
71
+ x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
72
+ x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
73
+ x = ((x >> 4) & 0x0F0F0F0F) | ((x & 0x0F0F0F0F) << 4);
74
+ x = ((x >> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
75
+ return (x >> 16) | (x << 16);
76
+ }
64
77
  /*
65
78
  const testersShift: bigint[] = []; //32 << i
66
79
  const testers: bigint[] = []; //1 << (32 << i)
@@ -121,45 +134,59 @@ export function bitcountCached(x: bigint): number {
121
134
  return Number(x & 0xFFFFFFFFn);
122
135
  }
123
136
  */
124
- function highestSetBig(x) {
137
+ /*
138
+ function highestSetBig32(x: bigint): number {
125
139
  let s = 0;
126
140
  let k = 0;
141
+
127
142
  for (let t = x >> 32n; t; t >>= BigInt(s)) {
128
143
  s = 32 << k++;
129
144
  x = t;
130
145
  }
146
+
131
147
  if (k) {
132
- // determine length by bisection
133
- k--;
134
- while (k--) {
135
- const b = x >> BigInt(32 << k);
148
+ while (--k) {
149
+ const b = x >> BigInt(16 << k);
136
150
  if (b) {
137
- s += 32 << k;
151
+ s += 16 << k;
138
152
  x = b;
139
153
  }
140
154
  }
141
155
  }
142
156
  return (s + 32) - Math.clz32(Number(x));
143
157
  }
158
+ */
159
+ function highestSetBig1024(x) {
160
+ let s = 0;
161
+ let k = 0;
162
+ for (let t = x >> 1024n; t; t >>= BigInt(s)) {
163
+ s = 1024 << k++;
164
+ x = t;
165
+ }
166
+ if (k) {
167
+ while (--k) {
168
+ const b = x >> BigInt(512 << k);
169
+ if (b) {
170
+ s += 512 << k;
171
+ x = b;
172
+ }
173
+ }
174
+ }
175
+ return highestSet1024(Number(x)) + s;
176
+ }
144
177
  function highestSet(x) {
145
178
  if (x < 0)
146
179
  x = ~x;
147
- if (x < 0x100000000)
148
- return highestSet32(Number(x));
149
- // For < 1024 bits, use log2
150
- if (x <= Number.MAX_VALUE) {
151
- const b = Math.floor(Math.log2(Number(x)));
152
- return (1n << BigInt(b)) <= x ? b + 1 : b;
153
- }
154
- return highestSetBig(BigInt(x));
180
+ return x < 0x100000000 ? highestSet32(Number(x))
181
+ : x <= Number.MAX_VALUE ? highestSet1024(Number(x))
182
+ : highestSetBig1024(BigInt(x));
155
183
  }
156
184
  function lowestSet(x) {
157
- if (x < 0)
158
- x = ~x;
159
- if (x < 0x100000000)
160
- return lowestSet32(Number(x));
161
- x = BigInt(x);
162
- return highestSetBig(x & -x) - 1;
185
+ if (typeof x === 'number') {
186
+ x = x < 0 ? (~x & (x + 1)) : x & -x;
187
+ return (x < 0x100000000 ? highestSet32(x) : highestSet1024(x)) - 1;
188
+ }
189
+ return highestSetBig1024(x < 0 ? (~x & (x + 1n)) : x & -x) - 1;
163
190
  }
164
191
  function countSet(x) {
165
192
  if (x < 0)
@@ -221,6 +248,38 @@ function nthSet(x, i) {
221
248
  }
222
249
  return n;
223
250
  }
251
+ function reverse(x) {
252
+ if (typeof x === 'number')
253
+ return reverse32(Number(x));
254
+ let k = 5;
255
+ for (let t = x >> 32n; t;)
256
+ t >>= BigInt(1 << k++);
257
+ let n = 1 << k;
258
+ let s = 0;
259
+ if (k === 5) {
260
+ s += highestSet32(Number(x));
261
+ }
262
+ else {
263
+ let t = x;
264
+ while (k >= 10) {
265
+ --k;
266
+ const b = t >> BigInt(1 << k);
267
+ if (b) {
268
+ s += 1 << k;
269
+ t = b;
270
+ }
271
+ }
272
+ s += highestSet1024(Number(t));
273
+ }
274
+ const shift = n - s;
275
+ let mask = (1n << BigInt(n)) - 1n;
276
+ while (n >>= 1) {
277
+ const nb = BigInt(n);
278
+ mask ^= (mask << nb);
279
+ x = ((x >> nb) & mask) | ((x << nb) & ~mask);
280
+ }
281
+ return x >> BigInt(shift);
282
+ }
224
283
  // Returns the index of the highest clear bit
225
284
  function highestClear(x) {
226
285
  return highestSet(~x);
@@ -237,487 +296,1030 @@ function clearLowest(x) {
237
296
  return typeof x === 'bigint' ? x & (x - 1n) : x & (x - 1);
238
297
  }
239
298
  //-----------------------------------------------------------------------------
240
- // SparseBits - a sparse bitset implementation, where each entry in the 'bits' array represents 32 bits
241
- // The 'undef' member indicates whether undefined entries are treated as 0 or 0xffffffff
299
+ // DenseBits - a dense bitset implementation using bigint
242
300
  //-----------------------------------------------------------------------------
243
- class ImmutableSparseBits {
244
- bits = []; //each entry represents 32 bits
245
- undef;
246
- static *whereGenerator(bits, undef, set, from = -1) {
247
- ++from;
248
- if (undef ? !set : set) {
249
- const keys = Object.keys(bits).map(k => +k);
250
- if (keys.length === 0)
251
- return;
252
- const i = from >> 5;
253
- let k = (0, algorithm_1.lowerBound)(keys, i);
254
- let v = bits[keys[k]] ^ undef;
255
- if (keys[k] === i)
256
- v &= -(1 << (from & 0x1f));
257
- for (;;) {
258
- const i = keys[k];
259
- while (v) {
260
- yield (i << 5) + lowestSet32(v);
261
- v = v & (v - 1);
301
+ class DenseBits {
302
+ bits;
303
+ constructor(bits = 0n) {
304
+ this.bits = bits;
305
+ }
306
+ create(bits) {
307
+ return new this.constructor(bits);
308
+ }
309
+ static fromIndices(...indices) {
310
+ let bits = 0n;
311
+ for (const i of indices)
312
+ bits |= 1n << BigInt(i);
313
+ return new this(bits);
314
+ }
315
+ get length() {
316
+ return highestSet(this.bits);
317
+ }
318
+ test(a) {
319
+ return !!(this.bits & (1n << BigInt(a)));
320
+ }
321
+ equals(other) {
322
+ return this.bits === other.bits;
323
+ }
324
+ contains(other) {
325
+ return (this.bits & other.bits) === other.bits;
326
+ }
327
+ intersects(other) {
328
+ return (this.bits & other.bits) !== 0n;
329
+ }
330
+ countSet() {
331
+ return countSet(this.bits);
332
+ }
333
+ nthSet(a) {
334
+ return nthSet(this.bits, a);
335
+ }
336
+ complement() {
337
+ return this.create(~this.bits);
338
+ }
339
+ intersect(other) {
340
+ return this.create(this.bits & other.bits);
341
+ }
342
+ union(other) {
343
+ return this.create(this.bits | other.bits);
344
+ }
345
+ xor(other) {
346
+ return this.create(this.bits ^ other.bits);
347
+ }
348
+ difference(other) {
349
+ return this.create(this.bits & ~other.bits);
350
+ }
351
+ next(a, set = true) {
352
+ let s = this.bits >> BigInt(a + 1);
353
+ s = set ? s & -s : (s + 1n) & ~s;
354
+ return s ? a + highestSet(s) : -1;
355
+ }
356
+ where(set, from = -1, to) {
357
+ let bits = this.bits >> BigInt(from + 1);
358
+ if (to !== undefined)
359
+ bits &= (1n << BigInt(to - from - 1)) - 1n;
360
+ return {
361
+ *[Symbol.iterator]() {
362
+ while (bits) {
363
+ const i = highestSet(set ? bits & -bits : (bits + 1n) & ~bits);
364
+ from += i;
365
+ yield from;
366
+ bits >>= BigInt(i);
262
367
  }
263
- ++k;
264
- if (k === keys.length)
265
- return;
266
- v = bits[keys[k]] ^ undef;
267
368
  }
268
- }
269
- else {
270
- let i = from >> 5;
271
- let v = ((bits[i] ?? undef) ^ ~undef) & -(1 << (from & 0x1f));
272
- for (;;) {
273
- while (v) {
274
- yield (i << 5) + lowestSet32(v);
275
- v = v & (v - 1);
369
+ };
370
+ }
371
+ ranges(set = true) {
372
+ let bits = this.bits;
373
+ return {
374
+ *[Symbol.iterator]() {
375
+ let offset = 0;
376
+ while (bits) {
377
+ const i = highestSet(bits & -bits);
378
+ if (!set && offset + i > 1)
379
+ yield [offset ? offset - 1 : 0, offset + i - 1];
380
+ bits >>= BigInt(i);
381
+ const j = highestSet(~bits & (bits + 1n));
382
+ bits >>= BigInt(j);
383
+ if (set)
384
+ yield [offset + i - 1, offset + i + j - 1];
385
+ offset += i + j;
276
386
  }
277
- ++i;
278
- v = ((bits[i] ?? undef) ^ ~undef);
279
387
  }
388
+ };
389
+ }
390
+ *[Symbol.iterator]() {
391
+ yield* this.where(true);
392
+ }
393
+ toSparse() {
394
+ const sparse = [];
395
+ for (let bits = this.bits, i = 0; bits; bits >>= 32n, i++) {
396
+ const v = Number(bits & 0xffffffffn);
397
+ if (v)
398
+ sparse[i] = v;
280
399
  }
400
+ return new SparseBits(sparse);
281
401
  }
282
- constructor(initial = false) {
283
- this.undef = initial ? -1 : 0;
402
+ slice(from, to) {
403
+ return to === undefined
404
+ ? this.create(this.bits >> BigInt(from))
405
+ : this.create(this.bits >> BigInt(from) & ((1n << BigInt(to - from)) - 1n));
284
406
  }
285
- create(init) {
286
- return new this.constructor(init);
407
+ // mutating methods
408
+ setMask(m) {
409
+ this.bits |= m;
287
410
  }
288
- copyUndefined(other) {
289
- for (const i in other.bits) {
290
- if (this.bits[i] === undefined)
291
- this.bits[i] = other.bits[i];
292
- }
411
+ clearMask(m) {
412
+ this.bits &= ~m;
413
+ }
414
+ set(a) {
415
+ this.setMask(1n << BigInt(a));
416
+ }
417
+ clear(a) {
418
+ this.clearMask(1n << BigInt(a));
419
+ }
420
+ setRange(a, b) {
421
+ this.setMask((1n << BigInt(b)) - (1n << BigInt(a)));
293
422
  return this;
294
423
  }
295
- flipUndefined(other) {
296
- for (const i in other.bits) {
297
- if (this.bits[i] === undefined)
298
- this.bits[i] = ~other.bits[i];
299
- }
424
+ clearRange(a, b) {
425
+ this.clearMask((1n << BigInt(b)) - (1n << BigInt(a)));
300
426
  return this;
301
427
  }
302
- static fromEntries(entries, initial = false) {
303
- const r = new this(initial);
304
- if (Array.isArray(entries)) {
305
- for (const [k, v] of entries)
306
- r.bits[k] = v;
307
- }
308
- else {
309
- for (const [k, v] of Object.entries(entries))
310
- r.bits[+k] = v;
311
- }
312
- return r;
428
+ selfComplement() {
429
+ this.bits = ~this.bits;
430
+ return this;
313
431
  }
314
- keys() {
315
- return Object.keys(this.bits).map(k => +k);
432
+ selfIntersect(other) {
433
+ this.bits &= other.bits;
434
+ return this;
316
435
  }
317
- entries() {
318
- //return this.bits;
319
- return Object.entries(this.bits).map(([k, v]) => [+k, v]);
436
+ selfUnion(other) {
437
+ this.bits |= other.bits;
438
+ return this;
439
+ }
440
+ selfXor(other) {
441
+ this.bits ^= other.bits;
442
+ return this;
443
+ }
444
+ selfDifference(other) {
445
+ this.bits &= ~other.bits;
446
+ return this;
447
+ }
448
+ }
449
+ exports.DenseBits = DenseBits;
450
+ ;
451
+ //-----------------------------------------------------------------------------
452
+ // DenseBits32 - a dense bitset implementation using Uint32Array
453
+ //-----------------------------------------------------------------------------
454
+ class DenseBits32 {
455
+ bits;
456
+ constructor(bits = new Uint32Array(1)) {
457
+ this.bits = bits;
458
+ }
459
+ create(bits) {
460
+ return new this.constructor(bits);
461
+ }
462
+ static fromIndices(...indices) {
463
+ const max = Math.max(...indices);
464
+ const bits = new Uint32Array(Math.ceil((max + 1) / 32));
465
+ for (const i of indices)
466
+ bits[i >>> 5] |= 1 << (i & 31);
467
+ return new this(bits);
468
+ }
469
+ get length() {
470
+ const n = this.bits.length;
471
+ return highestSet32(this.bits[n - 1]) + (n - 1) * 32;
320
472
  }
321
473
  test(a) {
322
- return !!((this.bits[a >> 5] ?? this.undef) & (1 << (a & 0x1f)));
474
+ return !!(this.bits[a >>> 5] & (1 << (a & 31)));
475
+ }
476
+ equals(other) {
477
+ if (this.bits.length !== other.bits.length)
478
+ return false;
479
+ for (let i = 0; i < this.bits.length; i++) {
480
+ if (this.bits[i] !== other.bits[i])
481
+ return false;
482
+ }
483
+ return true;
484
+ }
485
+ contains(other) {
486
+ return other.bits.every((b, i) => (b & this.bits[i]) === b);
487
+ }
488
+ intersects(other) {
489
+ return this.bits.some((b, i) => (b & other.bits[i]) !== 0);
323
490
  }
324
491
  countSet() {
325
- let count = 0;
326
- for (const i in this.bits)
327
- count += countSet32(this.bits[i]);
328
- return count;
492
+ return this.bits.reduce((a, b) => a + countSet32(b), 0);
329
493
  }
330
494
  nthSet(a) {
331
- if (this.undef === 0) {
332
- for (const i in this.bits) {
333
- const v = this.bits[i];
334
- const n = countSet32(v);
335
- if (a < n)
336
- return (+i << 5) + nthSet32(v, a);
337
- a -= n;
338
- }
339
- }
340
- else {
341
- let prev = 0;
342
- for (const i in this.bits) {
343
- const m = (+i - prev) << 5;
344
- if (a < m)
345
- return (prev << 5) + a;
346
- a -= m;
347
- const v = this.bits[i];
348
- const n = countSet32(v);
349
- if (a < n)
350
- return (+i << 5) + nthSet32(v, a);
351
- a -= n;
352
- prev = +i + 1;
353
- }
495
+ for (let i = 0; i < this.bits.length; i++) {
496
+ const c = countSet32(this.bits[i]);
497
+ if (a < c)
498
+ return i * 32 + nthSet32(this.bits[i], a);
499
+ a -= c;
354
500
  }
355
501
  return -1;
356
502
  }
357
503
  complement() {
358
- const result = this.create(this.undef === 0);
359
- for (const i in this.bits)
360
- result.bits[i] = ~this.bits[i];
361
- return result;
504
+ return this.create(this.bits.map(b => ~b));
362
505
  }
363
506
  intersect(other) {
364
- const result = this.create(!!(this.undef & other.undef));
365
- for (const i in this.bits)
366
- result.bits[i] = this.bits[i] & other.bits[i];
367
- return this.undef ? result.copyUndefined(other) : result;
507
+ return this.create(this.bits.map((b, i) => b & other.bits[i]));
368
508
  }
369
509
  union(other) {
370
- const result = this.create(!!(this.undef | other.undef));
371
- for (const i in other.bits)
372
- result.bits[i] = this.bits[i] | other.bits[i];
373
- return this.undef ? result : result.copyUndefined(other);
510
+ if (other.bits.length > this.bits.length)
511
+ return this.create(other.bits.map((b, i) => b | this.bits[i]));
512
+ return this.create(this.bits.map((b, i) => b | other.bits[i]));
374
513
  }
375
514
  xor(other) {
376
- const result = this.create(!!(this.undef ^ other.undef));
377
- for (const i in this.bits)
378
- result.bits[i] = this.bits[i] ^ other.bits[i];
379
- return this.undef ? result.flipUndefined(other) : result.copyUndefined(other);
515
+ if (other.bits.length > this.bits.length)
516
+ return this.create(other.bits.map((b, i) => b ^ this.bits[i]));
517
+ return this.create(this.bits.map((b, i) => b ^ other.bits[i]));
380
518
  }
381
- contains(other) {
382
- if (other.undef && !this.undef)
383
- return false;
384
- for (const i in other.bits) {
385
- if (other.bits[i] & ~(this.bits[i] ?? this.undef))
386
- return false;
387
- }
388
- return true;
519
+ difference(other) {
520
+ return this.create(this.bits.map((b, i) => b & ~other.bits[i]));
389
521
  }
390
522
  next(a, set = true) {
391
523
  ++a;
392
- const xor = this.undef;
393
- if (xor)
394
- set = !set;
395
- if (set) {
396
- const keys = Object.keys(this.bits).map(k => +k);
397
- if (keys.length === 0)
398
- return -1;
399
- const ai = a >> 5;
400
- let i = (0, algorithm_1.lowerBound)(keys, ai);
401
- let v = this.bits[keys[i]] ^ xor;
402
- if (keys[i] === ai)
403
- v &= -(1 << (a & 0x1f));
404
- while (!v) {
405
- ++i;
406
- if (i === keys.length)
407
- return -1;
408
- v = this.bits[keys[i]] ^ xor;
409
- }
410
- return (keys[i] << 5) + lowestSet32(v);
411
- }
412
- else {
413
- let i = a >> 5;
414
- if (this.bits[i] === undefined)
415
- return a;
416
- let v = (this.bits[i] ^ xor) | ((1 << (a & 0x1f)) - 1);
417
- while (!v) {
418
- ++i;
419
- if (this.bits[i] === undefined)
420
- break;
421
- v = this.bits[i] ^ xor;
524
+ const xor = set ? 0 : -1;
525
+ for (let i = a >>> 5; i < this.bits.length; i++) {
526
+ let b = this.bits[i] ^ xor;
527
+ if (b) {
528
+ if (i === (a >>> 5)) {
529
+ b &= -(1 << (a & 31));
530
+ if (!b)
531
+ continue;
532
+ }
533
+ return i * 32 + lowestSet32(b);
422
534
  }
423
- return (i << 5) + lowestSet32(~v);
424
535
  }
536
+ return -1;
425
537
  }
426
- where(set, from = -1) {
538
+ where(set, from = -1, to) {
539
+ const bits = this.bits;
540
+ ++from;
541
+ to = to === undefined ? bits.length * 32 : Math.min(to, bits.length * 32);
427
542
  return {
428
- [Symbol.iterator]: () => ImmutableSparseBits.whereGenerator(this.bits, this.undef, set, from)
543
+ *[Symbol.iterator]() {
544
+ const xor = set ? 0 : -1;
545
+ const end = (to + 31) >>> 5;
546
+ for (let i = from >>> 5; i < end; i++) {
547
+ let b = bits[i] ^ xor;
548
+ if (i === (from >>> 5))
549
+ b &= -(1 << (from & 31));
550
+ if (i === end - 1)
551
+ b &= (1 << (to & 31)) - 1;
552
+ while (b) {
553
+ yield i * 32 + lowestSet32(b);
554
+ b = b & (b - 1);
555
+ }
556
+ }
557
+ }
429
558
  };
430
559
  }
431
- ranges() {
560
+ ranges(set = true) {
432
561
  const bits = this.bits;
433
- const undef = this.undef;
434
562
  return {
435
563
  *[Symbol.iterator]() {
436
564
  let start = -1, end = 0;
437
- for (const i in bits) {
438
- let b = bits[i] ^ undef;
565
+ for (let i = 0; i < bits.length; i++) {
566
+ let b = bits[i];
439
567
  const c0 = +i * 32;
440
568
  while ((start < 0 ? b : ~b) !== 0) {
441
569
  if (start === -1) {
442
570
  start = c0 + lowestSet32(b);
443
- if (undef)
571
+ if (!set && end != start)
444
572
  yield [end, start];
445
573
  end = -1;
446
574
  b = b | (b - 1);
447
575
  }
448
576
  else {
449
577
  end = c0 + lowestSet32(~b);
450
- if (!undef)
578
+ if (set)
451
579
  yield [start, end];
452
580
  start = -1;
453
581
  b = b & (b + 1);
454
582
  }
455
583
  }
456
- if (start >= 0 && bits[+i + 1] === undefined) {
457
- if (!undef)
458
- yield [start, c0 + 32];
459
- start = -1;
460
- }
461
584
  }
462
- if (undef)
585
+ if (set) {
586
+ if (start >= 0)
587
+ yield [start, bits.length * 32];
588
+ }
589
+ else {
463
590
  yield [end, Infinity];
591
+ }
464
592
  }
465
593
  };
466
594
  }
467
595
  *[Symbol.iterator]() {
468
- yield* ImmutableSparseBits.whereGenerator(this.bits, this.undef, true, -1);
469
- //for (let i = this.next(-1); i !== -1; i = this.next(i))
470
- // yield i;
471
- }
472
- clean() {
473
- for (const i in this.bits) {
474
- if (this.bits[i] === this.undef)
475
- delete this.bits[i];
476
- }
477
- return this;
596
+ yield* this.where(true);
478
597
  }
479
- toDense() {
480
- let bits = 0n;
481
- if (this.undef) {
482
- for (const i in this.bits)
483
- bits |= BigInt(~this.bits[i]) << BigInt(+i * 32);
484
- bits = ~bits;
485
- }
486
- else {
487
- for (const i in this.bits)
488
- bits |= BigInt(this.bits[i]) << BigInt(+i * 32);
598
+ toSparse() {
599
+ const sparse = [];
600
+ for (let i = 0; i < this.bits.length; i++) {
601
+ const v = this.bits[i];
602
+ if (v)
603
+ sparse[i] = v;
489
604
  }
490
- return new DenseBits(bits);
491
- }
492
- }
493
- exports.ImmutableSparseBits = ImmutableSparseBits;
494
- class SparseBits extends ImmutableSparseBits {
605
+ return new SparseBits(sparse);
606
+ }
607
+ slice(from, to) {
608
+ to = to === undefined ? this.bits.length * 32 : Math.min(to, this.bits.length * 32);
609
+ const fromi = from >>> 5, toi = (to + 31) >>> 5;
610
+ const shift = from & 31;
611
+ const bits = shift === 0
612
+ ? this.bits.slice(fromi, toi)
613
+ : this.bits.subarray(fromi, toi).map((b, i) => (b >>> shift) | (this.bits[fromi + i + 1] << (32 - shift)));
614
+ const maskBits = (to - from) & 31;
615
+ if (maskBits)
616
+ bits[bits.length - 1] &= (1 << maskBits) - 1;
617
+ return this.create(bits);
618
+ }
619
+ // mutating methods
495
620
  setMask(i, m) {
496
- if (this.bits[i] !== undefined)
497
- this.bits[i] |= m;
498
- else if (!this.undef)
499
- this.bits[i] = m;
621
+ if (i >= this.bits.length) {
622
+ const bits = new Uint32Array(i + 1);
623
+ bits.set(this.bits);
624
+ this.bits = bits;
625
+ }
626
+ this.bits[i] |= m;
500
627
  }
501
628
  clearMask(i, m) {
502
- if (this.bits[i] !== undefined)
629
+ if (i < this.bits.length)
503
630
  this.bits[i] &= ~m;
504
- else if (this.undef)
505
- this.bits[i] = ~m;
506
631
  }
507
632
  set(a) {
508
- this.setMask(a >> 5, 1 << (a & 0x1f));
633
+ this.setMask(a >>> 5, 1 << (a & 31));
509
634
  }
510
635
  clear(a) {
511
- this.clearMask(a >> 5, 1 << (a & 0x1f));
636
+ this.clearMask(a >>> 5, 1 << (a & 31));
512
637
  }
513
638
  setRange(a, b) {
514
- let i = a >> 5, j = b >> 5;
515
- if (i === j) {
516
- this.setMask(i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)));
639
+ const ai = a >>> 5, bi = b >>> 5;
640
+ if (ai === bi) {
641
+ this.setMask(ai, (1 << (b & 31)) - (1 << (a & 31)));
517
642
  }
518
643
  else {
519
- this.setMask(i++, -(1 << (a & 0x1f)));
520
- if (this.undef) {
521
- while (i < j)
522
- delete this.bits[i++];
523
- }
524
- else {
525
- while (i < j)
526
- this.bits[i++] = -1;
527
- }
528
- this.setMask(i, (1 << (b & 0x1f)) - 1);
644
+ this.setMask(bi, (1 << (b & 31)) - 1);
645
+ this.setMask(ai, -(1 << (a & 31)));
646
+ for (let i = ai + 1; i < bi; i++)
647
+ this.bits[i] = -1;
529
648
  }
530
649
  return this;
531
650
  }
532
651
  clearRange(a, b) {
533
- let i = a >> 5, j = b >> 5;
534
- if (i === j) {
535
- this.clearMask(i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)));
536
- }
537
- else {
538
- this.clearMask(i++, -(1 << (a & 0x1f)));
539
- if (!this.undef) {
540
- while (i < j)
541
- delete this.bits[i++];
652
+ const ai = a >>> 5, bi = b >>> 5;
653
+ if (ai < this.bits.length) {
654
+ if (ai === bi) {
655
+ this.clearMask(ai, (1 << (b & 31)) - (1 << (a & 31)));
542
656
  }
543
657
  else {
544
- while (i < j)
545
- this.bits[i++] = 0;
658
+ this.clearMask(ai, -(1 << (a & 31)));
659
+ if (bi >= this.bits.length) {
660
+ this.bits = this.bits.slice(0, ai + 1);
661
+ }
662
+ else {
663
+ for (let i = ai + 1; i < bi; i++)
664
+ this.bits[i] = 0;
665
+ this.clearMask(bi, (1 << (b & 31)) - 1);
666
+ }
546
667
  }
547
- this.clearMask(i, (1 << (b & 0x1f)) - 1);
548
668
  }
549
669
  return this;
550
670
  }
551
671
  selfComplement() {
552
- this.undef = ~this.undef;
553
- for (const i in this.bits)
554
- this.bits[i] = ~this.bits[i];
672
+ this.bits = this.bits.map(b => ~b);
555
673
  return this;
556
674
  }
557
675
  selfIntersect(other) {
558
- for (const i in this.bits)
559
- this.bits[i] &= other.bits[i];
560
- if (this.undef)
561
- this.copyUndefined(other);
562
- this.undef &= other.undef;
676
+ this.bits = this.bits.map((b, i) => b & other.bits[i]);
563
677
  return this;
564
678
  }
565
679
  selfUnion(other) {
566
- for (const i in other.bits)
567
- this.bits[i] |= other.bits[i];
568
- if (!this.undef)
569
- this.copyUndefined(other);
570
- this.undef |= other.undef;
680
+ this.bits = other.bits.length > this.bits.length
681
+ ? other.bits.map((b, i) => b | this.bits[i])
682
+ : this.bits.map((b, i) => b | other.bits[i]);
571
683
  return this;
572
684
  }
573
685
  selfXor(other) {
574
- for (const i in this.bits)
575
- this.bits[i] ^= other.bits[i];
576
- if (this.undef)
577
- this.flipUndefined(other);
578
- else
579
- this.copyUndefined(other);
580
- this.undef &= other.undef;
686
+ this.bits = other.bits.length > this.bits.length
687
+ ? other.bits.map((b, i) => b ^ this.bits[i])
688
+ : this.bits.map((b, i) => b ^ other.bits[i]);
689
+ return this;
690
+ }
691
+ selfDifference(other) {
692
+ this.bits = this.bits.map((b, i) => b & ~other.bits[i]);
693
+ return this;
694
+ }
695
+ clean() {
696
+ let i = this.bits.length;
697
+ while (i-- && this.bits[i] === 0)
698
+ ;
699
+ this.bits = this.bits.slice(0, i + 1);
581
700
  return this;
582
701
  }
583
702
  }
584
- exports.SparseBits = SparseBits;
703
+ exports.DenseBits32 = DenseBits32;
585
704
  ;
586
705
  //-----------------------------------------------------------------------------
587
- // DenseBits - a dense bitset implementation using bigint
706
+ // SparseBits - a sparse bitset where each entry in the 'bits' array represents 32 bits
588
707
  //-----------------------------------------------------------------------------
589
- class ImmutableDenseBits {
708
+ function sparseFromIndices(indices) {
709
+ const bits = [];
710
+ for (const i of indices)
711
+ bits[i >> 5] |= 1 << (i & 0x1f);
712
+ return bits;
713
+ }
714
+ function sparseCopyUndefined(bits, other, xor = 0) {
715
+ for (const i in other) {
716
+ if (bits[i] === undefined)
717
+ bits[i] = other[i] ^ xor;
718
+ }
719
+ return bits;
720
+ }
721
+ function sparseDeleteUndefined(bits, other) {
722
+ for (const i in bits) {
723
+ if (other[i] === undefined)
724
+ delete bits[i];
725
+ }
726
+ return bits;
727
+ }
728
+ function sparseClean(bits, undef = 0) {
729
+ for (const i in bits) {
730
+ if (bits[i] === undef)
731
+ delete bits[i];
732
+ }
733
+ }
734
+ function sparseTest(bits, a, undef = 0) {
735
+ return !!((bits[a >> 5] ?? undef) & (1 << (a & 0x1f)));
736
+ }
737
+ function sparseSetMask(bits, i, m, undef = 0) {
738
+ if (bits[i] !== undefined)
739
+ bits[i] |= m;
740
+ else if (!undef)
741
+ bits[i] = m;
742
+ }
743
+ function sparseClearMask(bits, i, m, undef = 0) {
744
+ if (bits[i] !== undefined)
745
+ bits[i] &= ~m;
746
+ else if (undef)
747
+ bits[i] = ~m;
748
+ }
749
+ function sparseSetRange(bits, a, b, undef = 0) {
750
+ let i = a >> 5, j = b >> 5;
751
+ if (i === j) {
752
+ sparseSetMask(bits, i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)), undef);
753
+ }
754
+ else {
755
+ sparseSetMask(bits, i++, -(1 << (a & 0x1f)), undef);
756
+ if (undef) {
757
+ while (i < j)
758
+ delete bits[i++];
759
+ }
760
+ else {
761
+ while (i < j)
762
+ bits[i++] = -1;
763
+ }
764
+ sparseSetMask(bits, i, (1 << (b & 0x1f)) - 1, undef);
765
+ }
766
+ }
767
+ function sparseClearRange(bits, a, b, undef = 0) {
768
+ let i = a >> 5, j = b >> 5;
769
+ if (i === j) {
770
+ sparseClearMask(bits, i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)), undef);
771
+ }
772
+ else {
773
+ sparseClearMask(bits, i++, -(1 << (a & 0x1f)), undef);
774
+ if (!undef) {
775
+ while (i < j)
776
+ delete bits[i++];
777
+ }
778
+ else {
779
+ while (i < j)
780
+ bits[i++] = 0;
781
+ }
782
+ sparseClearMask(bits, i, (1 << (b & 0x1f)) - 1, undef);
783
+ }
784
+ }
785
+ function sparseCountSet(bits) {
786
+ let count = 0;
787
+ for (const i in bits)
788
+ count += countSet32(bits[i]);
789
+ return count;
790
+ }
791
+ function sparseNthSet(bits, a, undef = 0) {
792
+ if (undef === 0) {
793
+ for (const i in bits) {
794
+ const v = bits[i];
795
+ const n = countSet32(v);
796
+ if (a < n)
797
+ return (+i << 5) + nthSet32(v, a);
798
+ a -= n;
799
+ }
800
+ }
801
+ else {
802
+ let prev = 0;
803
+ for (const i in bits) {
804
+ const m = (+i - prev) << 5;
805
+ if (a < m)
806
+ return (prev << 5) + a;
807
+ a -= m;
808
+ const v = bits[i];
809
+ const n = countSet32(v);
810
+ if (a < n)
811
+ return (+i << 5) + nthSet32(v, a);
812
+ a -= n;
813
+ prev = +i + 1;
814
+ }
815
+ }
816
+ return -1;
817
+ }
818
+ function sparseComplement(bits) {
819
+ return bits.map(b => ~b);
820
+ }
821
+ function sparseIntersect(bits, other) {
822
+ const result = [];
823
+ for (const i in bits) {
824
+ if (other[i] !== undefined) {
825
+ result[i] = bits[i] & other[i];
826
+ }
827
+ }
828
+ return result;
829
+ }
830
+ function sparseUnion(bits, other) {
831
+ const result = [];
832
+ for (const i in bits) {
833
+ if (other[i] !== undefined) {
834
+ result[i] = bits[i] | other[i];
835
+ }
836
+ }
837
+ return result;
838
+ }
839
+ function sparseXor(bits, other) {
840
+ const result = [];
841
+ for (const i in bits) {
842
+ if (other[i] !== undefined) {
843
+ result[i] = bits[i] ^ other[i];
844
+ }
845
+ }
846
+ return result;
847
+ }
848
+ function sparseDifference(bits, other) {
849
+ const result = [];
850
+ for (const i in bits) {
851
+ if (other[i] !== undefined) {
852
+ result[i] = bits[i] & ~other[i];
853
+ }
854
+ }
855
+ return result;
856
+ }
857
+ function sparseSelfComplement(bits) {
858
+ for (const i in bits)
859
+ bits[i] = ~bits[i];
860
+ return bits;
861
+ }
862
+ function sparseSelfIntersect(bits, other) {
863
+ for (const i in bits)
864
+ if (other[i] !== undefined)
865
+ bits[i] &= other[i];
866
+ return bits;
867
+ }
868
+ function sparseSelfUnion(bits, other) {
869
+ for (const i in bits)
870
+ if (other[i] !== undefined)
871
+ bits[i] |= other[i];
872
+ return bits;
873
+ }
874
+ function sparseSelfXor(bits, other) {
875
+ for (const i in bits)
876
+ if (other[i] !== undefined)
877
+ bits[i] ^= other[i];
878
+ return bits;
879
+ }
880
+ function sparseSelfDifference(bits, other) {
881
+ for (const i in bits)
882
+ if (other[i] !== undefined)
883
+ bits[i] &= ~other[i];
884
+ return bits;
885
+ }
886
+ function sparseEquals(bits, other) {
887
+ const ka = Object.keys(bits), kb = Object.keys(other);
888
+ if (ka.length !== kb.length)
889
+ return false;
890
+ for (const k of ka) {
891
+ if (bits[+k] !== other[+k])
892
+ return false;
893
+ }
894
+ return true;
895
+ }
896
+ function sparseIntersects(bits, other, undef = 0) {
897
+ for (const i in bits) {
898
+ if ((bits[i] & (other[i] ?? undef)))
899
+ return true;
900
+ }
901
+ return false;
902
+ }
903
+ function sparseContains(bits, other, undef = 0) {
904
+ for (const i in other) {
905
+ if (other[i] & ~(bits[i] ?? undef))
906
+ return false;
907
+ }
908
+ return true;
909
+ }
910
+ function sparseNext(bits, from, set = true, undef = 0) {
911
+ ++from;
912
+ const from32 = from >> 5;
913
+ const fromM = 1 << (from & 0x1f);
914
+ if (undef ? !set : set) {
915
+ for (const i in bits) {
916
+ if (+i >= from32) {
917
+ const v = (bits[i] ^ undef) & (+i === from32 ? -fromM : -1);
918
+ if (v)
919
+ return (+i << 5) + lowestSet32(v);
920
+ }
921
+ }
922
+ return -1;
923
+ }
924
+ else {
925
+ if (bits[from32] === undefined)
926
+ return from;
927
+ let i = from32;
928
+ let v = (bits[i] ^ undef) | (fromM - 1);
929
+ while (!v) {
930
+ if (bits[++i] === undefined)
931
+ break;
932
+ v = bits[i] ^ undef;
933
+ }
934
+ return (i << 5) + lowestSet32(~v);
935
+ }
936
+ }
937
+ function* sparseWhere(bits, set, from = -1, to, undef = 0) {
938
+ ++from;
939
+ const from32 = from >> 5;
940
+ const to32 = to === undefined ? Infinity : to >> 5;
941
+ const fromM = 1 << (from & 0x1f);
942
+ const toM = to === undefined ? 0 : 1 << (to & 0x1f);
943
+ function* block(i, v) {
944
+ while (v) {
945
+ yield (i << 5) + lowestSet32(v);
946
+ v &= v - 1;
947
+ }
948
+ }
949
+ if (undef ? !set : set) {
950
+ for (const k in bits) {
951
+ const i = +k;
952
+ if (i >= from32) {
953
+ if (i >= to32)
954
+ break;
955
+ let v = bits[i] ^ undef;
956
+ if (i === from32)
957
+ v &= -fromM;
958
+ if (i === to32)
959
+ v &= (toM - 1);
960
+ yield* block(i, v);
961
+ }
962
+ }
963
+ }
964
+ else {
965
+ if (to32 > from32) {
966
+ yield* block(from32, ((bits[from32] ?? undef) ^ ~undef) & -fromM);
967
+ for (let i = from32 + 1; i < to32; i++)
968
+ yield* block(i, (bits[i] ?? undef) ^ ~undef);
969
+ yield* block(to32, ((bits[to32] ?? undef) ^ ~undef) & (toM - 1));
970
+ }
971
+ else if (to32 === from32) {
972
+ yield* block(from32, ((bits[from32] ?? undef) ^ ~undef) & (-fromM & (toM - 1)));
973
+ }
974
+ }
975
+ }
976
+ function* sparseRanges(bits, set, undef = 0) {
977
+ let start = -1, end = 0;
978
+ let other = undef ? set : !set;
979
+ for (const i in bits) {
980
+ let b = bits[i] ^ undef;
981
+ const c0 = +i * 32;
982
+ while ((start < 0 ? b : ~b) !== 0) {
983
+ if (start === -1) {
984
+ start = c0 + lowestSet32(b);
985
+ if (other && end != start)
986
+ yield [end, start];
987
+ end = -1;
988
+ b = b | (b - 1);
989
+ }
990
+ else {
991
+ end = c0 + lowestSet32(~b);
992
+ if (!other)
993
+ yield [start, end];
994
+ start = -1;
995
+ b = b & (b + 1);
996
+ }
997
+ }
998
+ if (start >= 0 && bits[+i + 1] === undefined) {
999
+ if (!other)
1000
+ yield [start, c0 + 32];
1001
+ start = -1;
1002
+ }
1003
+ }
1004
+ if (other)
1005
+ yield [end, Infinity];
1006
+ }
1007
+ function sparseSlice(bits, from, to) {
1008
+ const from32 = from >> 5;
1009
+ const fromM = 1 << (from & 0x1f);
1010
+ const to32 = to !== undefined ? to >> 5 : Infinity;
1011
+ const toM = to !== undefined ? 1 << (to & 0x1f) : 0;
1012
+ const result = [];
1013
+ for (const k in bits) {
1014
+ const i = +k;
1015
+ if (i >= from32) {
1016
+ if (i > to32)
1017
+ break;
1018
+ let b = bits[k];
1019
+ if (i === from32)
1020
+ b &= -fromM;
1021
+ if (i === to32)
1022
+ b &= toM - 1;
1023
+ if (b !== 0)
1024
+ result[k] = b;
1025
+ }
1026
+ }
1027
+ return result;
1028
+ }
1029
+ class SparseBits {
590
1030
  bits;
591
- constructor(bits = 0n) {
1031
+ constructor(bits = []) {
592
1032
  this.bits = bits;
593
1033
  }
594
- create(bits) {
1034
+ create(bits = []) {
595
1035
  return new this.constructor(bits);
596
1036
  }
597
- get length() {
598
- return highestSet(this.bits);
1037
+ static fromEntries(entries, ...extra) {
1038
+ return new this(Array.isArray(entries) ? Object.fromEntries(entries) : entries, ...extra);
1039
+ }
1040
+ static fromIndices(indicesOrFirst, ...rest) {
1041
+ return Array.isArray(indicesOrFirst)
1042
+ ? new this(sparseFromIndices(indicesOrFirst), ...rest)
1043
+ : new this(sparseFromIndices([indicesOrFirst, ...rest]));
1044
+ }
1045
+ copy() {
1046
+ return this.create({ ...this.bits });
1047
+ }
1048
+ empty() {
1049
+ for (const i in this.bits) {
1050
+ if (this.bits[i] !== 0)
1051
+ return false;
1052
+ }
1053
+ return true;
1054
+ }
1055
+ keys() {
1056
+ return Object.keys(this.bits).map(k => +k);
1057
+ }
1058
+ entries() {
1059
+ return Object.entries(this.bits).map(([k, v]) => [+k, v]);
599
1060
  }
600
1061
  test(a) {
601
- return !!(this.bits & (1n << BigInt(a)));
1062
+ return sparseTest(this.bits, a);
1063
+ }
1064
+ equals(other) {
1065
+ return sparseEquals(this.bits, other.bits);
1066
+ }
1067
+ contains(other) {
1068
+ return sparseContains(this.bits, other.bits);
1069
+ }
1070
+ intersects(other) {
1071
+ return sparseIntersects(this.bits, other.bits);
602
1072
  }
603
1073
  countSet() {
604
- return countSet(this.bits);
1074
+ return sparseCountSet(this.bits);
605
1075
  }
606
1076
  nthSet(a) {
607
- return nthSet(this.bits, a);
1077
+ return sparseNthSet(this.bits, a);
608
1078
  }
609
1079
  complement() {
610
- return this.create(~this.bits);
1080
+ return new SparseBits2(sparseComplement(this.bits), true);
611
1081
  }
612
1082
  intersect(other) {
613
- return this.create(this.bits & other.bits);
1083
+ return this.create(sparseIntersect(this.bits, other.bits));
614
1084
  }
615
1085
  union(other) {
616
- return this.create(this.bits | other.bits);
1086
+ return this.create(sparseCopyUndefined(sparseCopyUndefined(sparseUnion(this.bits, other.bits), other.bits), this.bits));
617
1087
  }
618
1088
  xor(other) {
619
- return this.create(this.bits ^ other.bits);
1089
+ return this.create(sparseCopyUndefined(sparseCopyUndefined(sparseXor(this.bits, other.bits), other.bits, -1), this.bits, -1));
620
1090
  }
621
- contains(other) {
622
- return (this.bits & other.bits) === other.bits;
1091
+ difference(other) {
1092
+ return this.create(sparseCopyUndefined(sparseDifference(this.bits, other.bits), this.bits));
623
1093
  }
624
- next(a, set = true) {
625
- let s = this.bits >> BigInt(a + 1);
626
- s = set ? s & -s : (s + 1n) & ~s;
627
- return s ? a + highestSet(s) : -1;
1094
+ next(from, set = true) {
1095
+ return sparseNext(this.bits, from, set);
628
1096
  }
629
- where(set, from = -1) {
630
- let bits = this.bits >> BigInt(from + 1);
1097
+ where(set, from = -1, to) {
631
1098
  return {
632
- *[Symbol.iterator]() {
633
- while (bits) {
634
- const i = highestSet(set ? bits & -bits : (bits + 1n) & ~bits);
635
- from += i;
636
- yield from;
637
- bits >>= BigInt(i);
638
- }
639
- }
640
- };
641
- /*
642
- const self = this;
643
- return {
644
- *[Symbol.iterator](): Generator<number> {
645
- for (let i = self.next(-1, set); i !== -1; i = self.next(i, set))
646
- yield i;
647
- }
1099
+ [Symbol.iterator]: () => sparseWhere(this.bits, set, from, to)
648
1100
  };
649
- */
650
1101
  }
651
- ranges() {
652
- let bits = this.bits;
1102
+ ranges(set = true) {
653
1103
  return {
654
- *[Symbol.iterator]() {
655
- let offset = 0;
656
- while (bits) {
657
- const i = highestSet(bits & -bits);
658
- bits >>= BigInt(i);
659
- const j = highestSet(~bits & (bits + 1n));
660
- bits >>= BigInt(j);
661
- yield [offset + i - 1, offset + i + j - 1];
662
- offset += i + j;
663
- }
664
- }
1104
+ [Symbol.iterator]: () => sparseRanges(this.bits, set)
665
1105
  };
666
1106
  }
667
1107
  *[Symbol.iterator]() {
668
- yield* this.where(true);
669
- //for (let i = this.next(-1); i !== -1; i = this.next(i))
670
- // yield i;
1108
+ yield* sparseWhere(this.bits, true, -1);
671
1109
  }
672
- toSparse() {
673
- const sparse = {};
674
- for (let bits = this.bits, i = 0; bits; bits >>= 32n, i++) {
675
- const v = Number(bits & 0xffffffffn);
676
- if (v)
677
- sparse[i] = v;
678
- }
679
- return SparseBits.fromEntries(sparse, false);
1110
+ clean() {
1111
+ sparseClean(this.bits);
1112
+ return this;
1113
+ }
1114
+ toDense() {
1115
+ let bits = 0n;
1116
+ for (const i in this.bits)
1117
+ bits |= BigInt(this.bits[i]) << BigInt(+i * 32);
1118
+ return new DenseBits(bits);
1119
+ }
1120
+ slice(from, to) {
1121
+ return this.create(sparseSlice(this.bits, from, to)); // ?? this.bits.length * 32));
1122
+ }
1123
+ //mutating methods
1124
+ set(a) {
1125
+ sparseSetMask(this.bits, a >> 5, 1 << (a & 0x1f));
1126
+ }
1127
+ clear(a) {
1128
+ sparseClearMask(this.bits, a >> 5, 1 << (a & 0x1f));
1129
+ }
1130
+ setRange(a, b) {
1131
+ sparseSetRange(this.bits, a, b);
1132
+ return this;
1133
+ }
1134
+ clearRange(a, b) {
1135
+ sparseClearRange(this.bits, a, b);
1136
+ return this;
1137
+ }
1138
+ selfIntersect(other) {
1139
+ sparseSelfIntersect(this.bits, other.bits);
1140
+ sparseDeleteUndefined(this.bits, other.bits);
1141
+ return this;
1142
+ }
1143
+ selfUnion(other) {
1144
+ sparseCopyUndefined(sparseSelfUnion(this.bits, other.bits), other.bits);
1145
+ return this;
1146
+ }
1147
+ selfXor(other) {
1148
+ sparseCopyUndefined(sparseSelfXor(this.bits, other.bits), other.bits);
1149
+ return this;
1150
+ }
1151
+ selfDifference(other) {
1152
+ sparseSelfDifference(this.bits, other.bits);
1153
+ return this;
680
1154
  }
681
1155
  }
682
- exports.ImmutableDenseBits = ImmutableDenseBits;
1156
+ exports.SparseBits = SparseBits;
683
1157
  ;
684
- class DenseBits extends ImmutableDenseBits {
685
- setMask(m) {
686
- this.bits |= m;
1158
+ //-----------------------------------------------------------------------------
1159
+ // SparseBits2 as above, with 'undef' indicating whether undefined entries are treated as 0 or 0xffffffff
1160
+ //-----------------------------------------------------------------------------
1161
+ class SparseBits2 extends SparseBits {
1162
+ undef;
1163
+ constructor(bits = [], initial = false) {
1164
+ super(bits);
1165
+ this.undef = initial ? -1 : 0;
687
1166
  }
688
- clearMask(m) {
689
- this.bits &= ~m;
1167
+ create(bits = [], initial = false) {
1168
+ return new this.constructor(bits, initial);
1169
+ }
1170
+ copy() {
1171
+ return this.create({ ...this.bits }, !!this.undef);
1172
+ }
1173
+ empty() {
1174
+ return !this.undef && super.empty();
1175
+ }
1176
+ test(a) {
1177
+ return sparseTest(this.bits, a, this.undef);
1178
+ }
1179
+ equals(other) {
1180
+ return this.undef === other.undef && sparseEquals(this.bits, other.bits);
1181
+ }
1182
+ contains(other) {
1183
+ if (other.undef && !this.undef)
1184
+ return false;
1185
+ return sparseContains(this.bits, other.bits, this.undef);
1186
+ }
1187
+ intersects(other) {
1188
+ if (this.undef)
1189
+ return !!other.undef || sparseIntersects(other.bits, this.bits, this.undef);
1190
+ return sparseIntersects(this.bits, other.bits, other.undef);
1191
+ }
1192
+ nthSet(a) {
1193
+ return sparseNthSet(this.bits, a, this.undef);
1194
+ }
1195
+ complement() {
1196
+ return this.create(sparseComplement(this.bits), this.undef === 0);
1197
+ }
1198
+ intersect(other) {
1199
+ const bits = sparseIntersect(this.bits, other.bits);
1200
+ if (other.undef)
1201
+ sparseCopyUndefined(bits, this.bits);
1202
+ if (this.undef)
1203
+ return this.create(sparseCopyUndefined(bits, other.bits), !!other.undef);
1204
+ return this.create(bits, false);
1205
+ }
1206
+ union(other) {
1207
+ const bits = sparseUnion(this.bits, other.bits);
1208
+ if (!other.undef)
1209
+ sparseCopyUndefined(bits, this.bits);
1210
+ if (this.undef)
1211
+ return this.create(bits, true);
1212
+ return this.create(sparseCopyUndefined(bits, other.bits), !!other.undef);
1213
+ }
1214
+ xor(other) {
1215
+ return this.create(sparseCopyUndefined(sparseCopyUndefined(sparseXor(this.bits, other.bits), other.bits, this.undef), this.bits, other.undef), !!(this.undef ^ other.undef));
690
1216
  }
1217
+ difference(other) {
1218
+ const bits = sparseDifference(this.bits, other.bits);
1219
+ if (!other.undef) {
1220
+ sparseCopyUndefined(bits, this.bits);
1221
+ return this.create(bits, !!this.undef);
1222
+ }
1223
+ if (this.undef)
1224
+ sparseCopyUndefined(bits, other.bits, -1);
1225
+ return this.create(bits, false);
1226
+ }
1227
+ next(from, set = true) {
1228
+ return sparseNext(this.bits, from, set, this.undef);
1229
+ }
1230
+ where(set, from = -1, to) {
1231
+ return {
1232
+ [Symbol.iterator]: () => sparseWhere(this.bits, set, from, to, this.undef)
1233
+ };
1234
+ }
1235
+ ranges(set = true) {
1236
+ return {
1237
+ [Symbol.iterator]: () => sparseRanges(this.bits, set, this.undef)
1238
+ };
1239
+ }
1240
+ *[Symbol.iterator]() {
1241
+ yield* sparseWhere(this.bits, true, -1, undefined, this.undef);
1242
+ }
1243
+ clean() {
1244
+ sparseClean(this.bits, this.undef);
1245
+ return this;
1246
+ }
1247
+ toDense() {
1248
+ let bits = 0n;
1249
+ if (this.undef) {
1250
+ for (const i in this.bits)
1251
+ bits |= BigInt(~this.bits[i]) << BigInt(+i * 32);
1252
+ bits = ~bits;
1253
+ }
1254
+ else {
1255
+ for (const i in this.bits)
1256
+ bits |= BigInt(this.bits[i]) << BigInt(+i * 32);
1257
+ }
1258
+ return new DenseBits(bits);
1259
+ }
1260
+ slice(from, to) {
1261
+ //to ??= this.bits.length * 32;
1262
+ return this.undef
1263
+ ? this.create(sparseSelfComplement(sparseSlice(sparseComplement(this.bits), from, to)), true)
1264
+ : this.create(sparseSlice(this.bits, from, to), false);
1265
+ }
1266
+ //mutating methods
691
1267
  set(a) {
692
- this.setMask(1n << BigInt(a));
1268
+ sparseSetMask(this.bits, a >> 5, 1 << (a & 0x1f), this.undef);
693
1269
  }
694
1270
  clear(a) {
695
- this.clearMask(1n << BigInt(a));
1271
+ sparseClearMask(this.bits, a >> 5, 1 << (a & 0x1f), this.undef);
696
1272
  }
697
1273
  setRange(a, b) {
698
- this.setMask((1n << BigInt(b)) - (1n << BigInt(a)));
1274
+ sparseSetRange(this.bits, a, b, this.undef);
699
1275
  return this;
700
1276
  }
701
1277
  clearRange(a, b) {
702
- this.clearMask((1n << BigInt(b)) - (1n << BigInt(a)));
1278
+ sparseClearRange(this.bits, a, b, this.undef);
703
1279
  return this;
704
1280
  }
705
1281
  selfComplement() {
706
- this.bits = ~this.bits;
1282
+ sparseSelfComplement(this.bits);
1283
+ this.undef = ~this.undef;
707
1284
  return this;
708
1285
  }
709
1286
  selfIntersect(other) {
710
- this.bits &= other.bits;
1287
+ sparseSelfIntersect(this.bits, other.bits);
1288
+ if (this.undef) {
1289
+ sparseCopyUndefined(this.bits, other.bits);
1290
+ this.undef = other.undef;
1291
+ }
1292
+ else if (!other.undef) {
1293
+ sparseDeleteUndefined(this.bits, other.bits);
1294
+ }
711
1295
  return this;
712
1296
  }
713
1297
  selfUnion(other) {
714
- this.bits |= other.bits;
1298
+ sparseSelfUnion(this.bits, other.bits);
1299
+ if (!this.undef) {
1300
+ sparseCopyUndefined(this.bits, other.bits);
1301
+ this.undef = other.undef;
1302
+ }
1303
+ else if (other.undef) {
1304
+ sparseDeleteUndefined(this.bits, other.bits);
1305
+ }
715
1306
  return this;
716
1307
  }
717
1308
  selfXor(other) {
718
- this.bits ^= other.bits;
1309
+ sparseSelfXor(this.bits, other.bits);
1310
+ sparseCopyUndefined(this.bits, other.bits, this.undef);
1311
+ this.undef ^= other.undef;
1312
+ return this;
1313
+ }
1314
+ selfDifference(other) {
1315
+ sparseSelfDifference(this.bits, other.bits);
1316
+ if (other.undef) {
1317
+ if (this.undef)
1318
+ sparseCopyUndefined(this.bits, other.bits, -1);
1319
+ this.undef = 0;
1320
+ }
719
1321
  return this;
720
1322
  }
721
1323
  }
722
- exports.DenseBits = DenseBits;
1324
+ exports.SparseBits2 = SparseBits2;
723
1325
  ;