@blackglory/structures 0.10.4 → 0.10.7

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.
@@ -16961,8 +16961,8 @@
16961
16961
 
16962
16962
  class BitSet {
16963
16963
  constructor() {
16964
+ this.array = new DynamicTypedArray(Uint8Array);
16964
16965
  this.length = 0;
16965
- this.array = new DynamicTypedArray(Uint8Array); // 经测试, 比Uint32Array快
16966
16966
  }
16967
16967
  get [Symbol.toStringTag]() {
16968
16968
  return this.constructor.name;
@@ -16974,15 +16974,44 @@
16974
16974
  return this.values();
16975
16975
  }
16976
16976
  *values() {
16977
- for (let i = 0; i < this.length; i++) {
16978
- if (this.has(i)) {
16979
- yield i;
16977
+ if (this.length > 0) {
16978
+ const maxArrayLength = Math.ceil(this.length / BitSet.bitsPerElement);
16979
+ const remainder = BitSet.bitsPerElement - (maxArrayLength * BitSet.bitsPerElement - this.length);
16980
+ const lastIndex = maxArrayLength - 1;
16981
+ for (let index = 0; index < lastIndex; index++) {
16982
+ const element = this.array.internalTypedArray[index];
16983
+ for (let bit = 0; bit < BitSet.bitsPerElement; bit++) {
16984
+ const mask = this.getMask(bit);
16985
+ if ((element & mask) === mask) {
16986
+ yield index * BitSet.bitsPerElement + bit;
16987
+ }
16988
+ }
16989
+ }
16990
+ const lastElement = this.array.internalTypedArray[maxArrayLength - 1];
16991
+ for (let bit = 0; bit < remainder; bit++) {
16992
+ const mask = this.getMask(bit);
16993
+ if ((lastElement & mask) === mask) {
16994
+ yield lastIndex * BitSet.bitsPerElement + bit;
16995
+ }
16980
16996
  }
16981
16997
  }
16982
16998
  }
16999
+ _dumpBinaryStrings() {
17000
+ const result = [];
17001
+ for (let i = 0; i < this.array.length; i++) {
17002
+ const binary = this.array.internalTypedArray[i].toString(2);
17003
+ if (binary.length < BitSet.bitsPerElement) {
17004
+ result.push('0'.repeat(BitSet.bitsPerElement - binary.length) + binary);
17005
+ }
17006
+ else {
17007
+ result.push(binary);
17008
+ }
17009
+ }
17010
+ return result;
17011
+ }
16983
17012
  has(value) {
16984
17013
  var _a;
16985
- const { index, mask } = this.getPosition(value);
17014
+ const [index, mask] = this.getPosition(value);
16986
17015
  return (((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & mask) === mask;
16987
17016
  }
16988
17017
  add(value) {
@@ -16991,13 +17020,15 @@
16991
17020
  if (value >= this.length) {
16992
17021
  this.length = value + 1;
16993
17022
  }
16994
- const { index, mask } = this.getPosition(value);
17023
+ const [index, mask] = this.getPosition(value);
16995
17024
  this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) | mask);
16996
17025
  }
16997
17026
  delete(value) {
16998
17027
  var _a;
16999
- const { index, mask } = this.getPosition(value);
17000
- this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & ~mask);
17028
+ const [index, mask] = this.getPosition(value);
17029
+ const element = ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0);
17030
+ this.array.set(index, element & ~mask);
17031
+ return (element & mask) === mask;
17001
17032
  }
17002
17033
  clear() {
17003
17034
  for (let i = this.array.length; i--;) {
@@ -17005,17 +17036,18 @@
17005
17036
  }
17006
17037
  }
17007
17038
  getPosition(value) {
17008
- const bits = Uint8Array.BYTES_PER_ELEMENT * 8;
17009
- const remainder = value % bits;
17010
- const quotient = (value - remainder) / bits;
17011
- const index = Math.max(0, quotient - 1);
17012
- const mask = this.getMask(value);
17013
- return { index, mask };
17039
+ const remainder = value % BitSet.bitsPerElement;
17040
+ const quotient = (value - remainder) / BitSet.bitsPerElement;
17041
+ const index = quotient;
17042
+ const mask = this.getMask(remainder);
17043
+ return [index, mask];
17014
17044
  }
17045
+ // 输入一定是一个小于bitsPerElement的值, 取值范围是[0, bitsPerElement)
17015
17046
  getMask(value) {
17016
17047
  return Math.pow(2, value);
17017
17048
  }
17018
17049
  }
17050
+ BitSet.bitsPerElement = Uint8Array.BYTES_PER_ELEMENT * 8;
17019
17051
 
17020
17052
  exports.BadEventError = BadEventError;
17021
17053
  exports.BitSet = BitSet;