@blackglory/structures 0.10.3 → 0.10.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.
@@ -16955,8 +16955,8 @@ class SortedSet {
16955
16955
 
16956
16956
  class BitSet {
16957
16957
  constructor() {
16958
+ this.array = new DynamicTypedArray(Uint8Array);
16958
16959
  this.length = 0;
16959
- this.array = new DynamicTypedArray(Uint8Array); // 经测试, 比Uint32Array快
16960
16960
  }
16961
16961
  get [Symbol.toStringTag]() {
16962
16962
  return this.constructor.name;
@@ -16964,9 +16964,48 @@ class BitSet {
16964
16964
  get size() {
16965
16965
  return this.length;
16966
16966
  }
16967
+ [Symbol.iterator]() {
16968
+ return this.values();
16969
+ }
16970
+ *values() {
16971
+ if (this.length > 0) {
16972
+ const maxArrayLength = Math.ceil(this.length / BitSet.bitsPerElement);
16973
+ const remainder = BitSet.bitsPerElement - (maxArrayLength * BitSet.bitsPerElement - this.length);
16974
+ const lastIndex = maxArrayLength - 1;
16975
+ for (let index = 0; index < lastIndex; index++) {
16976
+ const element = this.array.internalTypedArray[index];
16977
+ for (let bit = 0; bit < BitSet.bitsPerElement; bit++) {
16978
+ const mask = this.getMask(bit);
16979
+ if ((element & mask) === mask) {
16980
+ yield index * BitSet.bitsPerElement + bit;
16981
+ }
16982
+ }
16983
+ }
16984
+ const lastElement = this.array.internalTypedArray[maxArrayLength - 1];
16985
+ for (let bit = 0; bit < remainder; bit++) {
16986
+ const mask = this.getMask(bit);
16987
+ if ((lastElement & mask) === mask) {
16988
+ yield lastIndex * BitSet.bitsPerElement + bit;
16989
+ }
16990
+ }
16991
+ }
16992
+ }
16993
+ _dumpBinaryStrings() {
16994
+ const result = [];
16995
+ for (let i = 0; i < this.array.length; i++) {
16996
+ const binary = this.array.internalTypedArray[i].toString(2);
16997
+ if (binary.length < BitSet.bitsPerElement) {
16998
+ result.push('0'.repeat(BitSet.bitsPerElement - binary.length) + binary);
16999
+ }
17000
+ else {
17001
+ result.push(binary);
17002
+ }
17003
+ }
17004
+ return result;
17005
+ }
16967
17006
  has(value) {
16968
17007
  var _a;
16969
- const { index, mask } = this.getPosition(value);
17008
+ const [index, mask] = this.getPosition(value);
16970
17009
  return (((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & mask) === mask;
16971
17010
  }
16972
17011
  add(value) {
@@ -16975,12 +17014,12 @@ class BitSet {
16975
17014
  if (value >= this.length) {
16976
17015
  this.length = value + 1;
16977
17016
  }
16978
- const { index, mask } = this.getPosition(value);
17017
+ const [index, mask] = this.getPosition(value);
16979
17018
  this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) | mask);
16980
17019
  }
16981
17020
  delete(value) {
16982
17021
  var _a;
16983
- const { index, mask } = this.getPosition(value);
17022
+ const [index, mask] = this.getPosition(value);
16984
17023
  this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & ~mask);
16985
17024
  }
16986
17025
  clear() {
@@ -16989,17 +17028,18 @@ class BitSet {
16989
17028
  }
16990
17029
  }
16991
17030
  getPosition(value) {
16992
- const bits = Uint8Array.BYTES_PER_ELEMENT * 8;
16993
- const remainder = value % bits;
16994
- const quotient = (value - remainder) / bits;
16995
- const index = Math.max(0, quotient - 1);
16996
- const mask = this.getMask(value);
16997
- return { index, mask };
17031
+ const remainder = value % BitSet.bitsPerElement;
17032
+ const quotient = (value - remainder) / BitSet.bitsPerElement;
17033
+ const index = quotient;
17034
+ const mask = this.getMask(remainder);
17035
+ return [index, mask];
16998
17036
  }
17037
+ // 输入一定是一个小于bitsPerElement的值, 取值范围是[0, bitsPerElement)
16999
17038
  getMask(value) {
17000
17039
  return Math.pow(2, value);
17001
17040
  }
17002
17041
  }
17042
+ BitSet.bitsPerElement = Uint8Array.BYTES_PER_ELEMENT * 8;
17003
17043
 
17004
17044
  export { BadEventError, BitSet, Box, DynamicTypedArray, Emitter, ExpirableMap, FiniteStateMachine, HashMap, HashSet, LRUMap, ObservableFiniteStateMachine, Queue, SortedSet, SparseMap, SparseSet, TLRUMap, TrieMap, TypedSparseMap, TypedSparseSet, convertArrayToCons, convertConsToArray };
17005
17045
  //# sourceMappingURL=index.mjs.map