@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.
@@ -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;
@@ -16968,15 +16968,44 @@ class BitSet {
16968
16968
  return this.values();
16969
16969
  }
16970
16970
  *values() {
16971
- for (let i = 0; i < this.length; i++) {
16972
- if (this.has(i)) {
16973
- yield i;
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
+ }
16974
16990
  }
16975
16991
  }
16976
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
+ }
16977
17006
  has(value) {
16978
17007
  var _a;
16979
- const { index, mask } = this.getPosition(value);
17008
+ const [index, mask] = this.getPosition(value);
16980
17009
  return (((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & mask) === mask;
16981
17010
  }
16982
17011
  add(value) {
@@ -16985,13 +17014,15 @@ class BitSet {
16985
17014
  if (value >= this.length) {
16986
17015
  this.length = value + 1;
16987
17016
  }
16988
- const { index, mask } = this.getPosition(value);
17017
+ const [index, mask] = this.getPosition(value);
16989
17018
  this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) | mask);
16990
17019
  }
16991
17020
  delete(value) {
16992
17021
  var _a;
16993
- const { index, mask } = this.getPosition(value);
16994
- this.array.set(index, ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0) & ~mask);
17022
+ const [index, mask] = this.getPosition(value);
17023
+ const element = ((_a = this.array.get(index)) !== null && _a !== void 0 ? _a : 0);
17024
+ this.array.set(index, element & ~mask);
17025
+ return (element & mask) === mask;
16995
17026
  }
16996
17027
  clear() {
16997
17028
  for (let i = this.array.length; i--;) {
@@ -16999,17 +17030,18 @@ class BitSet {
16999
17030
  }
17000
17031
  }
17001
17032
  getPosition(value) {
17002
- const bits = Uint8Array.BYTES_PER_ELEMENT * 8;
17003
- const remainder = value % bits;
17004
- const quotient = (value - remainder) / bits;
17005
- const index = Math.max(0, quotient - 1);
17006
- const mask = this.getMask(value);
17007
- return { index, mask };
17033
+ const remainder = value % BitSet.bitsPerElement;
17034
+ const quotient = (value - remainder) / BitSet.bitsPerElement;
17035
+ const index = quotient;
17036
+ const mask = this.getMask(remainder);
17037
+ return [index, mask];
17008
17038
  }
17039
+ // 输入一定是一个小于bitsPerElement的值, 取值范围是[0, bitsPerElement)
17009
17040
  getMask(value) {
17010
17041
  return Math.pow(2, value);
17011
17042
  }
17012
17043
  }
17044
+ BitSet.bitsPerElement = Uint8Array.BYTES_PER_ELEMENT * 8;
17013
17045
 
17014
17046
  export { BadEventError, BitSet, Box, DynamicTypedArray, Emitter, ExpirableMap, FiniteStateMachine, HashMap, HashSet, LRUMap, ObservableFiniteStateMachine, Queue, SortedSet, SparseMap, SparseSet, TLRUMap, TrieMap, TypedSparseMap, TypedSparseSet, convertArrayToCons, convertConsToArray };
17015
17047
  //# sourceMappingURL=index.mjs.map