@blackglory/structures 0.7.0 → 0.10.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 (54) hide show
  1. package/README.md +44 -7
  2. package/dist/es2015/index.min.mjs +2 -2
  3. package/dist/es2015/index.min.mjs.map +1 -1
  4. package/dist/es2015/index.mjs +243 -119
  5. package/dist/es2015/index.mjs.map +1 -1
  6. package/dist/es2015/index.umd.js +244 -118
  7. package/dist/es2015/index.umd.js.map +1 -1
  8. package/dist/es2015/index.umd.min.js +2 -2
  9. package/dist/es2015/index.umd.min.js.map +1 -1
  10. package/dist/es2018/index.min.mjs +2 -2
  11. package/dist/es2018/index.min.mjs.map +1 -1
  12. package/dist/es2018/index.mjs +243 -119
  13. package/dist/es2018/index.mjs.map +1 -1
  14. package/dist/es2018/index.umd.js +244 -118
  15. package/dist/es2018/index.umd.js.map +1 -1
  16. package/dist/es2018/index.umd.min.js +2 -2
  17. package/dist/es2018/index.umd.min.js.map +1 -1
  18. package/lib/es2015/dynamic-typed-array.d.ts +1 -1
  19. package/lib/es2015/dynamic-typed-array.js +25 -20
  20. package/lib/es2015/dynamic-typed-array.js.map +1 -1
  21. package/lib/es2015/index.d.ts +2 -0
  22. package/lib/es2015/index.js +2 -0
  23. package/lib/es2015/index.js.map +1 -1
  24. package/lib/es2015/sparse-map.d.ts +5 -5
  25. package/lib/es2015/sparse-map.js +24 -25
  26. package/lib/es2015/sparse-map.js.map +1 -1
  27. package/lib/es2015/sparse-set.d.ts +1 -0
  28. package/lib/es2015/sparse-set.js +17 -12
  29. package/lib/es2015/sparse-set.js.map +1 -1
  30. package/lib/es2015/typed-sparse-map.d.ts +16 -0
  31. package/lib/es2015/typed-sparse-map.js +70 -0
  32. package/lib/es2015/typed-sparse-map.js.map +1 -0
  33. package/lib/es2015/typed-sparse-set.d.ts +13 -0
  34. package/lib/es2015/typed-sparse-set.js +49 -0
  35. package/lib/es2015/typed-sparse-set.js.map +1 -0
  36. package/lib/es2018/dynamic-typed-array.d.ts +1 -1
  37. package/lib/es2018/dynamic-typed-array.js +25 -20
  38. package/lib/es2018/dynamic-typed-array.js.map +1 -1
  39. package/lib/es2018/index.d.ts +2 -0
  40. package/lib/es2018/index.js +2 -0
  41. package/lib/es2018/index.js.map +1 -1
  42. package/lib/es2018/sparse-map.d.ts +5 -5
  43. package/lib/es2018/sparse-map.js +24 -25
  44. package/lib/es2018/sparse-map.js.map +1 -1
  45. package/lib/es2018/sparse-set.d.ts +1 -0
  46. package/lib/es2018/sparse-set.js +17 -12
  47. package/lib/es2018/sparse-set.js.map +1 -1
  48. package/lib/es2018/typed-sparse-map.d.ts +16 -0
  49. package/lib/es2018/typed-sparse-map.js +70 -0
  50. package/lib/es2018/typed-sparse-map.js.map +1 -0
  51. package/lib/es2018/typed-sparse-set.d.ts +13 -0
  52. package/lib/es2018/typed-sparse-set.js +49 -0
  53. package/lib/es2018/typed-sparse-set.js.map +1 -0
  54. package/package.json +2 -2
@@ -9641,6 +9641,221 @@ class ObservableFiniteStateMachine extends FiniteStateMachine {
9641
9641
  }
9642
9642
  }
9643
9643
 
9644
+ class SparseSet {
9645
+ constructor() {
9646
+ this.indexToValue = [];
9647
+ this.valueToIndex = [];
9648
+ }
9649
+ get [Symbol.toStringTag]() {
9650
+ return this.constructor.name;
9651
+ }
9652
+ [Symbol.iterator]() {
9653
+ return this.indexToValue[Symbol.iterator]();
9654
+ }
9655
+ values() {
9656
+ return this.indexToValue[Symbol.iterator]();
9657
+ }
9658
+ has(value) {
9659
+ // 跟一般的实现不同, 不需要访问values数组.
9660
+ return value in this.valueToIndex;
9661
+ }
9662
+ add(value) {
9663
+ if (!this.has(value)) {
9664
+ const index = this.indexToValue.length;
9665
+ this.indexToValue.push(value);
9666
+ this.valueToIndex[value] = index;
9667
+ }
9668
+ }
9669
+ delete(value) {
9670
+ if (this.has(value)) {
9671
+ const lastValue = this.indexToValue.pop();
9672
+ if (value === lastValue) {
9673
+ delete this.valueToIndex[value];
9674
+ }
9675
+ else {
9676
+ const index = this.valueToIndex[value];
9677
+ this.indexToValue[index] = lastValue;
9678
+ this.valueToIndex[lastValue] = index;
9679
+ // 跟一般的实现不同, 直接删除值的索引.
9680
+ delete this.valueToIndex[value];
9681
+ }
9682
+ }
9683
+ }
9684
+ }
9685
+
9686
+ class SparseMap {
9687
+ constructor() {
9688
+ this.keyToIndex = [];
9689
+ this.indexToKey = [];
9690
+ this.indexToValue = [];
9691
+ }
9692
+ get [Symbol.toStringTag]() {
9693
+ return this.constructor.name;
9694
+ }
9695
+ *entries() {
9696
+ for (let i = 0; i < this.indexToKey.length; i++) {
9697
+ yield [this.indexToKey[i], this.indexToValue[i]];
9698
+ }
9699
+ }
9700
+ keys() {
9701
+ return this.indexToKey.values();
9702
+ }
9703
+ values() {
9704
+ return this.indexToValue.values();
9705
+ }
9706
+ has(key) {
9707
+ // 跟一般的稀疏集实现不同, 不需要访问values数组.
9708
+ return key in this.keyToIndex;
9709
+ }
9710
+ get(key) {
9711
+ if (this.has(key)) {
9712
+ const index = this.keyToIndex[key];
9713
+ return this.indexToValue[index];
9714
+ }
9715
+ else {
9716
+ return undefined;
9717
+ }
9718
+ }
9719
+ set(key, value) {
9720
+ if (this.has(key)) {
9721
+ const index = this.keyToIndex[key];
9722
+ this.indexToValue[index] = value;
9723
+ }
9724
+ else {
9725
+ const index = this.indexToKey.length;
9726
+ this.indexToKey.push(key);
9727
+ this.indexToValue.push(value);
9728
+ this.keyToIndex[key] = index;
9729
+ }
9730
+ }
9731
+ delete(key) {
9732
+ if (this.has(key)) {
9733
+ const lastKey = this.indexToKey.pop();
9734
+ const lastValue = this.indexToValue.pop();
9735
+ if (key === lastKey) {
9736
+ delete this.keyToIndex[key];
9737
+ }
9738
+ else {
9739
+ const index = this.keyToIndex[key];
9740
+ this.indexToKey[index] = lastKey;
9741
+ this.indexToValue[index] = lastValue;
9742
+ this.keyToIndex[key] = index;
9743
+ delete this.keyToIndex[key];
9744
+ }
9745
+ }
9746
+ }
9747
+ }
9748
+
9749
+ class TypedSparseMap {
9750
+ constructor(array) {
9751
+ this.keyToIndex = [];
9752
+ this.indexToKey = [];
9753
+ es2018$d.assert(array.length === 0, 'array should be empty');
9754
+ this.indexToValue = array;
9755
+ }
9756
+ get [Symbol.toStringTag]() {
9757
+ return this.constructor.name;
9758
+ }
9759
+ *entries() {
9760
+ for (let i = 0; i < this.indexToKey.length; i++) {
9761
+ yield [
9762
+ this.indexToKey[i],
9763
+ this.indexToValue.internalTypedArray[i]
9764
+ ];
9765
+ }
9766
+ }
9767
+ keys() {
9768
+ return this.indexToKey.values();
9769
+ }
9770
+ *values() {
9771
+ for (let i = 0; i < this.indexToKey.length; i++) {
9772
+ yield this.indexToValue.internalTypedArray[i];
9773
+ }
9774
+ }
9775
+ has(key) {
9776
+ const index = this.keyToIndex[key];
9777
+ return this.indexToKey[index] === key;
9778
+ }
9779
+ get(key) {
9780
+ const index = this.keyToIndex[key];
9781
+ if (this.indexToKey[index] === key) {
9782
+ return this.indexToValue.internalTypedArray[index];
9783
+ }
9784
+ else {
9785
+ return undefined;
9786
+ }
9787
+ }
9788
+ set(key, value) {
9789
+ const index = this.keyToIndex[key];
9790
+ if (this.indexToKey[index] === key) {
9791
+ this.indexToValue.internalTypedArray[index] = value;
9792
+ }
9793
+ else {
9794
+ const index = this.indexToKey.length;
9795
+ this.indexToKey.push(key);
9796
+ this.indexToValue.push(value);
9797
+ this.keyToIndex[key] = index;
9798
+ }
9799
+ }
9800
+ delete(key) {
9801
+ const index = this.keyToIndex[key];
9802
+ if (this.indexToKey[index] === key) {
9803
+ const lastKey = this.indexToKey.pop();
9804
+ const lastValue = this.indexToValue.pop();
9805
+ if (key !== lastKey) {
9806
+ this.indexToKey[index] = lastKey;
9807
+ this.indexToValue.internalTypedArray[index] = lastValue;
9808
+ this.keyToIndex[lastKey] = index;
9809
+ }
9810
+ }
9811
+ }
9812
+ }
9813
+
9814
+ class TypedSparseSet {
9815
+ constructor(array) {
9816
+ const valueToIndex = [];
9817
+ if (array.length > 0) {
9818
+ for (const [index, value] of array.internalTypedArray.entries()) {
9819
+ valueToIndex[value] = index;
9820
+ }
9821
+ }
9822
+ this.indexToValue = array;
9823
+ this.valueToIndex = valueToIndex;
9824
+ }
9825
+ get [Symbol.toStringTag]() {
9826
+ return this.constructor.name;
9827
+ }
9828
+ [Symbol.iterator]() {
9829
+ return this.values();
9830
+ }
9831
+ *values() {
9832
+ for (let i = 0; i < this.indexToValue.length; i++) {
9833
+ yield this.indexToValue.internalTypedArray[i];
9834
+ }
9835
+ }
9836
+ has(value) {
9837
+ const index = this.valueToIndex[value];
9838
+ return this.indexToValue.internalTypedArray[index] === value;
9839
+ }
9840
+ add(value) {
9841
+ if (!this.has(value)) {
9842
+ const index = this.indexToValue.length;
9843
+ this.indexToValue.push(value);
9844
+ this.valueToIndex[value] = index;
9845
+ }
9846
+ }
9847
+ delete(value) {
9848
+ const index = this.valueToIndex[value];
9849
+ if (this.indexToValue.internalTypedArray[index] === value) {
9850
+ const lastValue = this.indexToValue.pop();
9851
+ if (value !== lastValue) {
9852
+ this.indexToValue.internalTypedArray[index] = lastValue;
9853
+ this.valueToIndex[lastValue] = index;
9854
+ }
9855
+ }
9856
+ }
9857
+ }
9858
+
9644
9859
  // 相关提案: https://github.com/tc39/proposal-resizablearraybuffer
9645
9860
  class DynamicTypedArray {
9646
9861
  constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
@@ -9648,7 +9863,7 @@ class DynamicTypedArray {
9648
9863
  this._length = 0;
9649
9864
  es2018$d.assert(Number.isInteger(capacity), 'capacity must be an integer');
9650
9865
  es2018$d.assert(capacity >= 0, 'capacity must be greater than or equal to 0');
9651
- es2018$d.assert(growthFactor > 1, 'growthFactory must be greater than or equal to 1');
9866
+ es2018$d.assert(growthFactor >= 1, 'growthFactory must be greater than or equal to 1');
9652
9867
  this.array = new typedArrayConstructor(capacity);
9653
9868
  this.growthFactor = growthFactor;
9654
9869
  }
@@ -9727,135 +9942,44 @@ class DynamicTypedArray {
9727
9942
  }
9728
9943
  }
9729
9944
  function computeNewCapacity(oldCapacity, targetLength, growthFactor) {
9730
- if (oldCapacity === targetLength) {
9945
+ if (growthFactor === 1) {
9731
9946
  return targetLength;
9732
9947
  }
9733
- else if (oldCapacity < targetLength) {
9734
- // 增加容量以适配targetLength
9735
- let newCapacity = Math.max(oldCapacity, 1);
9736
- while (newCapacity < targetLength) {
9737
- newCapacity *= growthFactor;
9738
- }
9739
- return Math.floor(newCapacity);
9740
- }
9741
9948
  else {
9742
- // 尝试减少容量以适配targetLength
9743
- // `targetLength = 0`是特殊情况,
9744
- // 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
9745
- if (targetLength === 0) {
9746
- return 0;
9949
+ if (oldCapacity === targetLength) {
9950
+ return targetLength;
9747
9951
  }
9748
- else {
9749
- let newCapacity = oldCapacity;
9750
- while (true) {
9751
- const temp = newCapacity / growthFactor;
9752
- if (temp < targetLength) {
9753
- break;
9754
- }
9755
- else {
9756
- newCapacity = temp;
9757
- }
9952
+ else if (oldCapacity < targetLength) {
9953
+ // 增加容量以适配targetLength
9954
+ let newCapacity = Math.max(oldCapacity, 1);
9955
+ while (newCapacity < targetLength) {
9956
+ newCapacity *= growthFactor;
9758
9957
  }
9759
9958
  return Math.floor(newCapacity);
9760
9959
  }
9761
- }
9762
- }
9763
-
9764
- class SparseSet {
9765
- constructor() {
9766
- this.indexToValue = new DynamicTypedArray(Uint32Array);
9767
- this.valueToIndex = new DynamicTypedArray(Uint32Array);
9768
- }
9769
- get [Symbol.toStringTag]() {
9770
- return this.constructor.name;
9771
- }
9772
- [Symbol.iterator]() {
9773
- return this.indexToValue.internalTypedArray[Symbol.iterator]();
9774
- }
9775
- has(value) {
9776
- const index = this.valueToIndex.internalTypedArray[value];
9777
- return this.indexToValue.internalTypedArray[index] === value;
9778
- }
9779
- add(value) {
9780
- if (!this.has(value)) {
9781
- const index = this.indexToValue.length;
9782
- this.indexToValue.push(value);
9783
- this.valueToIndex.set(value, index);
9784
- }
9785
- }
9786
- delete(value) {
9787
- const index = this.valueToIndex.internalTypedArray[value];
9788
- if (this.indexToValue.internalTypedArray[index] === value) {
9789
- const lastValue = this.indexToValue.pop();
9790
- if (value !== lastValue) {
9791
- this.indexToValue.internalTypedArray[index] = lastValue;
9792
- this.valueToIndex.internalTypedArray[lastValue] = index;
9793
- }
9794
- }
9795
- }
9796
- }
9797
-
9798
- class SparseMap {
9799
- constructor() {
9800
- this.keyToIndex = new DynamicTypedArray(Uint32Array);
9801
- this.indexToKey = new DynamicTypedArray(Uint32Array);
9802
- this.indexToValue = new DynamicTypedArray(Uint32Array);
9803
- }
9804
- get [Symbol.toStringTag]() {
9805
- return this.constructor.name;
9806
- }
9807
- *entries() {
9808
- for (let i = 0; i < this.indexToKey.length; i++) {
9809
- yield [
9810
- this.indexToKey.internalTypedArray[i],
9811
- this.indexToValue.internalTypedArray[i]
9812
- ];
9813
- }
9814
- }
9815
- keys() {
9816
- return this.indexToKey.internalTypedArray.values();
9817
- }
9818
- values() {
9819
- return this.indexToValue.internalTypedArray.values();
9820
- }
9821
- has(key) {
9822
- const index = this.keyToIndex.internalTypedArray[key];
9823
- return this.indexToKey.internalTypedArray[index] === key;
9824
- }
9825
- get(key) {
9826
- const index = this.keyToIndex.internalTypedArray[key];
9827
- if (this.indexToKey.internalTypedArray[index] === key) {
9828
- return this.indexToValue.internalTypedArray[index];
9829
- }
9830
- else {
9831
- return undefined;
9832
- }
9833
- }
9834
- set(key, value) {
9835
- const index = this.keyToIndex.internalTypedArray[key];
9836
- if (this.indexToKey.internalTypedArray[index] === key) {
9837
- this.indexToValue.internalTypedArray[index] = value;
9838
- }
9839
9960
  else {
9840
- const index = this.indexToKey.length;
9841
- this.indexToKey.push(key);
9842
- this.indexToValue.push(value);
9843
- this.keyToIndex.set(key, index);
9844
- }
9845
- }
9846
- delete(key) {
9847
- const index = this.keyToIndex.internalTypedArray[key];
9848
- if (this.indexToKey.internalTypedArray[index] === key) {
9849
- const lastKey = this.indexToKey.pop();
9850
- const lastValue = this.indexToValue.pop();
9851
- if (key !== lastKey) {
9852
- this.indexToKey.internalTypedArray[index] = lastKey;
9853
- this.indexToValue.internalTypedArray[index] = lastValue;
9854
- this.keyToIndex.internalTypedArray[lastKey] = index;
9961
+ // 尝试减少容量以适配targetLength
9962
+ // `targetLength = 0`是特殊情况,
9963
+ // 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
9964
+ if (targetLength === 0) {
9965
+ return 0;
9966
+ }
9967
+ else {
9968
+ let newCapacity = oldCapacity;
9969
+ while (true) {
9970
+ const temp = newCapacity / growthFactor;
9971
+ if (temp < targetLength) {
9972
+ break;
9973
+ }
9974
+ else {
9975
+ newCapacity = temp;
9976
+ }
9977
+ }
9978
+ return Math.floor(newCapacity);
9855
9979
  }
9856
9980
  }
9857
9981
  }
9858
9982
  }
9859
9983
 
9860
- export { BadEventError, Box, DynamicTypedArray, Emitter, ExpirableMap, FiniteStateMachine, HashMap, HashSet, LRUMap, ObservableFiniteStateMachine, Queue, SparseMap, SparseSet, TLRUMap, TrieMap, convertArrayToCons, convertConsToArray };
9984
+ export { BadEventError, Box, DynamicTypedArray, Emitter, ExpirableMap, FiniteStateMachine, HashMap, HashSet, LRUMap, ObservableFiniteStateMachine, Queue, SparseMap, SparseSet, TLRUMap, TrieMap, TypedSparseMap, TypedSparseSet, convertArrayToCons, convertConsToArray };
9861
9985
  //# sourceMappingURL=index.mjs.map