@datagrok-libraries/utils 0.0.6 → 0.0.10

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 (42) hide show
  1. package/README.md +2 -2
  2. package/package.json +29 -26
  3. package/src/bit-array.d.ts +82 -0
  4. package/src/bit-array.d.ts.map +1 -0
  5. package/src/bit-array.js +666 -0
  6. package/src/operations.d.ts +75 -0
  7. package/src/operations.d.ts.map +1 -0
  8. package/src/operations.js +155 -0
  9. package/src/reduce-dimensionality.d.ts +38 -0
  10. package/src/reduce-dimensionality.d.ts.map +1 -0
  11. package/src/reduce-dimensionality.js +233 -0
  12. package/src/sequence-encoder.d.ts +59 -0
  13. package/src/sequence-encoder.d.ts.map +1 -0
  14. package/src/sequence-encoder.js +116 -0
  15. package/src/spe.d.ts +68 -0
  16. package/src/spe.d.ts.map +1 -0
  17. package/src/spe.js +151 -0
  18. package/src/string-measure.d.ts +33 -0
  19. package/src/string-measure.d.ts.map +1 -0
  20. package/src/string-measure.js +46 -0
  21. package/src/string-utils.d.ts +2 -0
  22. package/src/string-utils.d.ts.map +1 -0
  23. package/src/string-utils.js +9 -0
  24. package/src/table-validation.d.ts +2 -0
  25. package/src/table-validation.d.ts.map +1 -0
  26. package/src/table-validation.js +47 -0
  27. package/src/test-utils.d.ts +11 -0
  28. package/src/test-utils.d.ts.map +1 -0
  29. package/src/test-utils.js +81 -0
  30. package/src/{type_declarations.ts → type_declarations.d.ts} +56 -54
  31. package/src/type_declarations.d.ts.map +1 -0
  32. package/src/type_declarations.js +37 -0
  33. package/tsconfig.json +71 -71
  34. package/src/bit-array.ts +0 -767
  35. package/src/operations.ts +0 -160
  36. package/src/reduce-dimensionality.ts +0 -278
  37. package/src/sequence-encoder.ts +0 -132
  38. package/src/spe.ts +0 -172
  39. package/src/string-measure.ts +0 -54
  40. package/src/string-utils.ts +0 -8
  41. package/src/table-validation.ts +0 -67
  42. package/src/test-utils.ts +0 -97
package/src/bit-array.ts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6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
45
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
46
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
47
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
48
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
49
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
51
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
52
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
53
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
54
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
55
-
56
- private _data: Uint32Array;
57
- private _length = 0;
58
- private _version = 0;
59
- private _updateLevel = 0;
60
- private _selectedCount = 0;
61
- private _selectedCountVersion = -1;
62
- private _selectedIndexesVersion = -1;
63
- private _versionedName = '';
64
- private _versionedNameVersion = -1;
65
- SHRINK_THRESHOLD = 0x100;
66
-
67
- constructor(data: Uint32Array, length: number)
68
- constructor(length: number, defaultValue?: boolean)
69
- constructor(arg: number | Uint32Array, defaultValue: boolean | number = false) {
70
- if (typeof arg === 'number') {
71
- const length = arg;
72
- let buff = BitArray._createBuffer(length);
73
- if (defaultValue)
74
- for (let i = 0; i < buff.length; i++)
75
- buff[i] = -1;
76
- this._data = buff;
77
- this._length = length;
78
- } else if (arg instanceof Uint32Array) {
79
- this._data = arg as Uint32Array;
80
- this._length = defaultValue as number;
81
- } else {
82
- throw new Error("Invalid constructor");
83
- }
84
- }
85
-
86
-
87
- getRawData() { return this._data; }
88
-
89
- assureGoez(num: number, argName: String): void {
90
- if (num < 0) throw new Error(`${argName} should be greater than zero`);
91
- }
92
-
93
- assureInRange(value: number, min: number, max: number, argName: String): void {
94
- if ((value < min) || (value > max)) {
95
- throw new Error(`Argument ${argName} (${value}) out of range (${min}, ${max})`);
96
- }
97
- }
98
-
99
- copy(src: Uint32Array, dst: Uint32Array, count: number): void {
100
- for (let i = 0; i < count; i++) {
101
- dst[i] = src[i];
102
- }
103
- }
104
-
105
- copyFrom(other: BitArray): void {
106
- if (this._length != other._length) {
107
- throw new Error(`Lengths differ (${this._length} != ${other._length})`);
108
- }
109
- this.copy(other._data, this._data, this.lengthInInts);
110
- this._version++;
111
- }
112
-
113
- get length() {
114
- return this._length;
115
- }
116
-
117
- get buffer() {
118
- return this._data;
119
- }
120
-
121
- set buffer(data: Uint32Array) {
122
- this._data = data;
123
- this._version++;
124
- }
125
-
126
- get version() {
127
- return this._version;
128
- }
129
-
130
- set version(value: number) {
131
- this._version = value;
132
- }
133
-
134
- incrementVersion(notify = true): void {
135
- this._version++;
136
- }
137
-
138
- get lengthInInts() {
139
- return Math.floor((this._length + 0x1f) / 0x20);
140
- }
141
-
142
- get versionedName() {
143
- return this._version == this._versionedNameVersion ? this._versionedName : '';
144
- }
145
-
146
- set versionedName(name: string) {
147
- this._versionedName = name;
148
- this._versionedNameVersion = this._version;
149
- }
150
-
151
- get self() {
152
- return this;
153
- }
154
-
155
- setLength(value: number): void {
156
- if (value < 0) {
157
- throw new Error('should be >= 0');
158
- }
159
-
160
- if (value == this._length) return;
161
- const nIntsNeeded = Math.floor((value + 0x1f) / 0x20);
162
- if ((nIntsNeeded > this._data.length) || ((nIntsNeeded + this.SHRINK_THRESHOLD) < this._data.length)) {
163
- const newData = new Uint32Array(nIntsNeeded);
164
- this.copy(this._data, newData, (nIntsNeeded > this._data.length) ? this._data.length : nIntsNeeded);
165
- this._data = newData;
166
- }
167
-
168
- if (value > this._length) {
169
- if (this._length % 0x20 > 0) {
170
- this._data[this.lengthInInts - 1] &= (1 << ((this._length % 0x20) & 0x1f)) - 1;
171
- }
172
- this._data.fill(0, this.lengthInInts, nIntsNeeded);
173
- }
174
- this._length = value;
175
- this._version++;
176
- }
177
-
178
- static fromAnd(set1: BitArray, set2: BitArray): BitArray {
179
- if (set1._length != set2._length) {
180
- throw new Error(`Lengths differ (${set1._length} != ${set2._length})`);
181
- }
182
-
183
- const temp = new BitArray(set1._length);
184
- temp._length = set1._length;
185
- temp._data = BitArray._createBuffer(temp._length);
186
- temp._version = 0;
187
-
188
- const len = set1.lengthInInts;
189
- for (let i = 0; i < len; i++) {
190
- temp._data[i] = set1._data[i] & set2._data[i];
191
- }
192
- return temp;
193
- }
194
-
195
- private static _createBuffer(length: number): Uint32Array {
196
- return new Uint32Array(Math.floor((length + 0x1f) / 0x20));
197
- }
198
-
199
- static fromValues(values: Array<boolean>): BitArray {
200
- const temp = new BitArray(values.length);
201
- temp._version = 0;
202
-
203
- for (let i = 0; i < temp._length; i++) {
204
- if (values[i]) {
205
- temp._data[Math.floor(i / 0x20)] |= 1 << ((i % 0x20) & 0x1f);
206
- }
207
- }
208
- return temp;
209
- }
210
-
211
- /// Constructs a [BitSet] of length [count], where idx-th bit is determined by a call to [flag] (idx).
212
- static fromSeq(count: number, flag: Function): BitArray {
213
- const temp = new BitArray(count);
214
- for (let i = 0; i < count; ++i) {
215
- temp.setBit(i, flag(i));
216
- }
217
- temp._version = 0;
218
- return temp;
219
- }
220
-
221
- /// Constructs a [BitSet] from a string [s] containing '0' or '1'.
222
- static fromString(s: string): BitArray {
223
- return BitArray.fromSeq(s.length, (i: number) => s.charAt(i) == '1');
224
- }
225
-
226
- /// Constructs a [BitSet], based on length [_length] and byte array [_data].
227
- static fromUint32Array(_length: number, _data: Uint32Array): BitArray {
228
- const temp = new BitArray(_length);
229
- temp._data = _data;
230
- return temp;
231
- }
232
-
233
- /// Deserializes a [BitSet] from [bytes].
234
- static fromBytes(bytes: Uint8Array): BitArray {
235
- const len = bytes.length;
236
- const temp = new BitArray(len * 8);
237
- temp._data = new Uint32Array(Math.floor((len + 3) / 4));
238
- temp._length = len * 8;
239
- let num1 = 0; let num2 = 0;
240
-
241
- while ((len - num2) >= 4) {
242
- temp._data[num1++] = (
243
- ((bytes[num2] & 0xff) |
244
- ((bytes[num2 + 1] & 0xff) << 8)) |
245
- ((bytes[num2 + 2] & 0xff) << 0x10)) |
246
- ((bytes[num2 + 3] & 0xff) << 0x18);
247
-
248
- num2 += 4;
249
- }
250
-
251
- if (len - num2 == 3) {
252
- temp._data[num1] = (bytes[num2 + 2] & 0xff) << 0x10;
253
- }
254
-
255
- if (len - num2 == 2) {
256
- temp._data[num1] |= (bytes[num2 + 1] & 0xff) << 8;
257
- }
258
-
259
- if (len - num2 == 1) {
260
- temp._data[num1] |= bytes[num2] & 0xff;
261
- }
262
-
263
- temp._version = 0;
264
- return temp;
265
- }
266
-
267
- toString(): string {
268
- return `${this._length} bits, ${this.countBits(true)} set`;
269
- }
270
-
271
- /// Performs deep comparison of two bitsets.
272
- equals(other: BitArray): boolean {
273
- if (this == other) return true;
274
- if (other == null) return false;
275
- if (this._length != other._length) return false;
276
- if (this._length == 0) return true;
277
-
278
- for (let i = 0; i < this._data.length - 1; i++) {
279
- if (this._data[i] != other._data[i]) return false;
280
- }
281
-
282
- for (let i = (this._data.length - 1) * 8; i < this._length; i++) {
283
- if (this.getBit(i) != other.getBit(i)) {
284
- return false;
285
- }
286
- }
287
- return true;
288
- }
289
-
290
- /** Clones a bitset. */
291
- clone(): BitArray {
292
- const bitArray = new BitArray(0, false);
293
- bitArray._data = Uint32Array.from(this._data); // effective length: (lengthInInts)
294
- bitArray._length = this._length;
295
- bitArray._version = this._version;
296
- return bitArray;
297
- }
298
-
299
- /** Initializes a bitset. */
300
- init(flag: Function, notify: boolean): BitArray {
301
- this.setAll(false, false);
302
-
303
- for (let i = 0; i < this._length; i++) {
304
- if (flag(i)) {
305
- this._data[Math.floor(i / 0x20)] |= 1 << ((i % 0x20) & 0x1f);
306
- }
307
- }
308
-
309
- this.incrementVersion(notify);
310
- return this;
311
- }
312
-
313
- /// Inverts a bitset.
314
- invert(notify = true): void {
315
- for (let i = 0; i < this._data.length; i++) {
316
- this._data[i] ^= -1;
317
- }
318
- this.incrementVersion(notify);
319
- }
320
-
321
- /// Sets all bits to [value], optionally suppressing notifications.
322
- setAll(value: boolean, notify = false): void {
323
- const flags = value ? -1 : 0;
324
- const len = this.lengthInInts;
325
-
326
- for (let i = 0; i < len; i++) {
327
- this._data[i] = flags;
328
- } //todo: optimize
329
-
330
- this.incrementVersion(notify);
331
- }
332
-
333
- /// Sets bits at [indexes] position to [value].
334
- /// Clears the bitset if [clear] flag is true.
335
- /// Change notification is raised when [notify] is true.
336
- setIndexes(indexes: Array<number>, value=true, clear=true, notify=true): void {
337
- if (clear) {
338
- this.setAll(!value, false);
339
- }
340
-
341
- for (const i of indexes) {
342
- this.setFast(i, value);
343
- }
344
-
345
- this.incrementVersion(notify);
346
- }
347
-
348
- everyIndex(indexes: Array<number>, value=true): boolean {
349
- for (const index of indexes) {
350
- if (this.getBit(index) != value) {
351
- return false;
352
- }
353
- }
354
- return true;
355
- }
356
-
357
- anyIndex(indexes: Array<number>, value=true): boolean {
358
- for (const index of indexes) {
359
- if (this.getBit(index) == value) {
360
- return true;
361
- }
362
- }
363
- return false;
364
- }
365
-
366
- setWhere(check: Function, value=true, clear=true, notify=true, allowClear=true): void {
367
- if (clear && allowClear) {
368
- this.setAll(!value, false);
369
- }
370
-
371
- if (allowClear) {
372
- for (let i = 0; i < this._length; i++) {
373
- if (check(i)) {
374
- this.setFast(i, value);
375
- }
376
- }
377
- } else {
378
- for (let i = 0; i < this._length; i++) {
379
- this.setFast(i, check(i) ? value : !value);
380
- }
381
- }
382
-
383
- this.incrementVersion(notify);
384
- }
385
-
386
- setRange(from: number, to: number, value: boolean, notify=true): BitArray {
387
- this.assureInRange(from, 0, this._length - 1, 'from');
388
- this.assureInRange(to, 0, this._length - 1, 'to');
389
-
390
- const start = Math.min(from, to);
391
- const end = Math.max(from, to);
392
-
393
- //todo: optimize
394
- if (value) {
395
- for (let i = start; i <= end; i++) {
396
- this.setTrue(i);
397
- }
398
- } else {
399
- for (let i = start; i <= end; i++) {
400
- this.setFalse(i);
401
- }
402
- }
403
-
404
- this.incrementVersion(notify);
405
- return this;
406
- }
407
-
408
- /// Sets n randomly chosen bits to value, remaining bits to !value.
409
- setRandom(n: number, value: boolean, notify=true): void {
410
- if (n < 0 || n > this._length) {
411
- throw new Error('n must be >= 0 && <= Count');
412
- }
413
-
414
- if (n > this._length / 2) {
415
- this.setRandom(this._length - n, !value);
416
- }
417
-
418
- this.setAll(!value);
419
-
420
- for (let k = 0; k < n; ) {
421
- const i = Math.floor(Math.random() * this._length);
422
- if (this.getBit(i) == value) continue;
423
- this.setFast(i, value);
424
- k++;
425
- }
426
-
427
- this.incrementVersion(notify);
428
- }
429
-
430
- /// Modifies current bitset by performing the bitwise AND operation against the
431
- /// corresponding elements in the specified bitset.
432
- and(value: BitArray, notify=true): BitArray {
433
- if (this._length != value._length) {
434
- throw new Error('Array lengths differ.');
435
- }
436
-
437
- for (let i = 0, len = this.lengthInInts; i < len; i++) {
438
- this._data[i] &= value._data[i];
439
- }
440
-
441
- this.incrementVersion(notify);
442
- return this;
443
- }
444
-
445
- /// Performs the bitwise AND NOT operation on the elements in the current bitset
446
- /// against the corresponding elements in the specified bitset.
447
- andNot(value: BitArray, notify=true): BitArray {
448
- if (this._length != value._length) {
449
- throw new Error('Array lengths differ.');
450
- }
451
-
452
- const len = this.lengthInInts;
453
- for (let num2 = 0; num2 < len; num2++) {
454
- this._data[num2] &= ~value._data[num2];
455
- }
456
-
457
- this.incrementVersion(notify);
458
- return this;
459
- }
460
-
461
- /// Performs the bitwise NOT AND operation on the elements in the current bitset
462
- /// against the corresponding elements in the specified bitset.
463
- notAnd(value: BitArray, notify=true): BitArray {
464
- if (this._length != value._length) {
465
- throw new Error('Array lengths differ.');
466
- }
467
-
468
- for (let i = 0, len = this.lengthInInts; i < len; i++) {
469
- this._data[i] = (~this._data[i]) & value._data[i];
470
- }
471
-
472
- this.incrementVersion(notify);
473
- return this;
474
- }
475
-
476
- /// Inverts all bit values in the current bitset
477
- not(notify = true): BitArray {
478
- for (let i = 0, len = this.lengthInInts; i < len; i++) {
479
- this._data[i] = ~this._data[i];
480
- }
481
-
482
- this.incrementVersion(notify);
483
- return this;
484
- }
485
-
486
- /// Performs the bitwise OR operation on the elements in the current bitset
487
- /// against the corresponding elements in the specified bitset.
488
- or(value: BitArray, notify = true) {
489
- if (this._length != value._length) {
490
- throw new Error('Array lengths differ.');
491
- }
492
-
493
- for (let i = 0, len = this.lengthInInts; i < len; i++) {
494
- this._data[i] |= value._data[i];
495
- }
496
-
497
- this.incrementVersion(notify);
498
- return this;
499
- }
500
-
501
- /// Performs the bitwise exclusive OR operation on the elements in the current bitset
502
- /// against the corresponding elements in the specified bitset.
503
- xor(value: BitArray, notify = true) {
504
- if (this._length != value._length) {
505
- throw new Error('Array lengths differ.');
506
- }
507
-
508
- for (let i = 0, len = this.lengthInInts; i < len; i++) {
509
- this._data[i] ^= value._data[i];
510
- }
511
-
512
- this.incrementVersion(notify);
513
- return this;
514
- }
515
-
516
- /// Inserts n 0-bits at position pos, resizing self and shifting bits appropriately.
517
- insertAt(pos: number, n: number, flag = false): void {
518
- this.assureInRange(pos, 0, this._length, 'pos');
519
-
520
- if (n == 0) return;
521
-
522
- //TODO: optimize
523
- //the most primitive implementation, optimize it later!
524
-
525
- // beginUpdate();
526
- const oldlength = this._length;
527
- this.setLength(this._length + n);
528
-
529
- //if (!contains(!flag)) return; // nothing to do
530
-
531
- for (let i = oldlength - 1; i >= pos; i--) {
532
- this.setBit(i + n, this.getBit(i));
533
- }
534
-
535
- for (let i = pos; i < pos + n; i++) {
536
- this.setBit(i, flag);
537
- }
538
-
539
- // endUpdate();
540
- }
541
-
542
- /// Deletes n bits beginning at position pos, resizing self and shifting remaining
543
- /// bits appropriately.
544
- removeAt(pos: number, n = 1): void {
545
- // the most primitive implementation, optimize it later!
546
- if (n < 0) {
547
- throw new Error('n cannot be negative');
548
- }
549
-
550
- this.assureInRange(pos, 0, this._length - n, 'pos');
551
-
552
- if (this.contains(true)) {
553
- for (let i = pos; i < this._length - n; i++) {
554
- this.setBit(i, this.getBit(i + n));
555
- }
556
- }
557
-
558
- this.setLength(this._length - n);
559
- }
560
-
561
- removeByMask(mask: BitArray, flag = true): BitArray {
562
- if (this._length != mask.length) {
563
- throw new Error('length != mask.length');
564
- }
565
-
566
- if (mask == this) { // no need to iterate
567
- this.setLength(mask.countBits(!flag));
568
- this.setAll(!flag);
569
- } else {
570
- let dstIdx = 0;
571
-
572
- for (let srcIdx = -1; (srcIdx = mask.findNext(srcIdx, !flag)) != -1;) {
573
- this.setFast(dstIdx++, this.getBit(srcIdx));
574
- }
575
-
576
- this._length = dstIdx;
577
- this._version++;
578
- }
579
-
580
- return this;
581
- }
582
-
583
- /// Similar to the [] operator.
584
- getBit(pos: number): boolean {
585
- return (this._data[Math.floor(pos / 0x20)] & (1 << (pos & 0x1f))) != 0;
586
- }
587
-
588
- /// Similar to the [] operator.
589
- setBit(pos: number, bit: boolean, notify = true) {
590
- this.setFast(pos, bit);
591
- if (notify) {
592
- this._version++;
593
- } else {
594
- this._version++;
595
- }
596
- }
597
-
598
- /// Sets [i]-th bit to [value], does not check bounds, does not increment version
599
- setFast(i: number, value: boolean): void {
600
- if (value) {
601
- this._data[Math.floor(i / 0x20)] |= 1 << (i & 0x1f);
602
- } else {
603
- this._data[Math.floor(i / 0x20)] &= ~(1 << (i & 0x1f));
604
- }
605
- }
606
-
607
- setTrue(pos: number): void {
608
- this._data[Math.floor(pos / 0x20)] |= 1 << (pos & 0x1f);
609
- }
610
- setFalse(pos: number) {
611
- this._data[Math.floor(pos / 0x20)] &= ~(1 << (pos & 0x1f));
612
- }
613
-
614
- trueCount(): number {
615
- return this.countBits(true);
616
- }
617
- falseCount(): number {
618
- return this.countBits(false);
619
- }
620
-
621
- /// Counts bits of the specified value.
622
- countBits(value: boolean): number {
623
- if (this._length == 0) return 0;
624
-
625
- if (this._selectedCountVersion != this._version) {
626
- this._selectedCount = 0;
627
- const len = this.lengthInInts;
628
- let i = 0;
629
- for (; i < len - 1; i++) {
630
- for (let k = this._data[i]; k != 0; k >>>= 8) { //todo: cast data[i] to uint
631
- this._selectedCount += BitArray._onBitCount[k & 0xff];
632
- }
633
- }
634
-
635
- // The last int.
636
- let k = this._data[i];
637
- const remainingBits = this._length & 0x1f;
638
- if (remainingBits != 0) {// if remainingBits == 0, the last int is fully used and ALL bits should be left as is.
639
- k &= ~((4294967295) << remainingBits);
640
- }
641
-
642
- for (; k != 0; k >>>= 8) {
643
- this._selectedCount += BitArray._onBitCount[k & 0xff];
644
- }
645
-
646
- this._selectedCountVersion = this._version;
647
- }
648
-
649
- return (value ? this._selectedCount : this._length - this._selectedCount);
650
- }
651
-
652
- /// Returns a number of set bits where also [check] is true
653
- countWhere(check: Function): number {
654
- let result = 0;
655
- if (this.trueCount() == this._length) {
656
- for (let i = 0; i < this._length; i++) {
657
- result += check(i) ? 1 : 0;
658
- }
659
- } else {
660
- for (let i = -1; (i = this.findNext(i, true)) != -1;) {
661
- result += check(i) ? 1 : 0;
662
- }
663
- }
664
- return result;
665
- }
666
-
667
- /// Performs bit "and" and counts bits of the specified value, without bitset modification.
668
- andWithCountBits(second: BitArray, value: boolean): number {
669
- if (this._length == 0) return 0;
670
-
671
- let count = 0; const len = this.lengthInInts; let i = 0;
672
- for (; i < len - 1; i++) {
673
- for (var k = this._data[i] & second._data[i]; k != 0; k >>>= 8) {
674
- count += BitArray._onBitCount[k & 0xff];
675
- }
676
- }
677
-
678
- // The last int.
679
- var k = this._data[i] & second._data[i];
680
- const remainingBits = this._length & 0x1f;
681
- if (remainingBits != 0) {
682
- k &= ~((4294967295) << remainingBits);
683
- }
684
- for (; k != 0; k >>>= 8) {
685
- count += BitArray._onBitCount[k & 0xff];
686
- }
687
-
688
- return (value ? count : this._length - count);
689
- }
690
-
691
- clear(): void {
692
- this.setLength(0);
693
- }
694
- contains(value: boolean): boolean {
695
- return this.findNext(-1, value) >= 0;
696
- }
697
- get allTrue() {
698
- return this.countBits(true) == this._length;
699
- }
700
- get allFalse() {
701
- return this.countBits(false) == this._length;
702
- }
703
- get anyTrue() {
704
- return this.countBits(true) > 0;
705
- }
706
- get anyFalse() {
707
- return this.countBits(false) > 0;
708
- }
709
-
710
- /// Returns the position of the next bit of the specified value, starting from the specified position.
711
- /// Returns -1, if there are no such bits.
712
- findNext(index: number, value = true): number {
713
- this.assureInRange(index, -1, this._length, 'index');
714
-
715
- if (index >= this._length - 1) return -1;
716
- index = index < 0 ? 0 : index + 1; // skip start
717
- let unusedBits = index & 0x1f;
718
- const numInts = this.lengthInInts;
719
-
720
- for (let i = Math.floor(index / 32); i < numInts; i++) {
721
- let k = (value ? this._data[i] : ~this._data[i]); // uint cast
722
- if (unusedBits != 0) {
723
- k &= ((0xffffffff << unusedBits) & 0xffffffff);
724
- unusedBits = 0;
725
- } else if (!value && k == -4294967296) // looking for false, all bits are set
726
- {
727
- continue;
728
- }
729
-
730
- for (let j = 0; k != 0; j += 8, k >>>= 8) {
731
- const p = BitArray._firstOnBit[k & 0xff];
732
- if (p >= 0) {
733
- index = p + (i * 32) + j;
734
- if (index >= this._length) return -1;
735
- return index;
736
- }
737
- }
738
- }
739
- return -1;
740
- }
741
-
742
- /// Finds previous bit of the specified value in the bitset.
743
- findPrev(index: number, value = true): number {
744
- if (index == 0) return -1;
745
- this.assureInRange(index, -1, this._length, 'index');
746
-
747
- index = index < 0 ? this._length - 1 : index - 1; // skip start
748
-
749
- const lastIntIdx = Math.floor(index / 0x20);
750
- let remainingBits = (index + 1) & 0x1f;
751
-
752
- for (let i = lastIntIdx; i >= 0; i--) {
753
- let k = (value ? this._data[i] : ~this._data[i]); // cast
754
- if (remainingBits != 0) {
755
- k &= ~((4294967295) << remainingBits);
756
- remainingBits = 0;
757
- }
758
- for (let j = 24; k != 0; j -= 8, k <<= 8) {
759
- const p = BitArray._lastOnBit[k >>> 0x18];
760
- if (p >= 0) {
761
- return p + (i * 32) + j;
762
- }
763
- }
764
- }
765
- return -1;
766
- }
767
- }