@blackglory/structures 0.9.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 (42) hide show
  1. package/README.md +34 -41
  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 +139 -117
  5. package/dist/es2015/index.mjs.map +1 -1
  6. package/dist/es2015/index.umd.js +139 -117
  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 +139 -117
  13. package/dist/es2018/index.mjs.map +1 -1
  14. package/dist/es2018/index.umd.js +139 -117
  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/sparse-set.d.ts +1 -0
  22. package/lib/es2015/sparse-set.js +3 -0
  23. package/lib/es2015/sparse-set.js.map +1 -1
  24. package/lib/es2015/typed-sparse-map.d.ts +5 -9
  25. package/lib/es2015/typed-sparse-map.js +23 -20
  26. package/lib/es2015/typed-sparse-map.js.map +1 -1
  27. package/lib/es2015/typed-sparse-set.d.ts +3 -5
  28. package/lib/es2015/typed-sparse-set.js +19 -9
  29. package/lib/es2015/typed-sparse-set.js.map +1 -1
  30. package/lib/es2018/dynamic-typed-array.d.ts +1 -1
  31. package/lib/es2018/dynamic-typed-array.js +25 -20
  32. package/lib/es2018/dynamic-typed-array.js.map +1 -1
  33. package/lib/es2018/sparse-set.d.ts +1 -0
  34. package/lib/es2018/sparse-set.js +3 -0
  35. package/lib/es2018/sparse-set.js.map +1 -1
  36. package/lib/es2018/typed-sparse-map.d.ts +5 -9
  37. package/lib/es2018/typed-sparse-map.js +23 -20
  38. package/lib/es2018/typed-sparse-map.js.map +1 -1
  39. package/lib/es2018/typed-sparse-set.d.ts +3 -5
  40. package/lib/es2018/typed-sparse-set.js +19 -9
  41. package/lib/es2018/typed-sparse-set.js.map +1 -1
  42. package/package.json +1 -1
@@ -9658,6 +9658,9 @@
9658
9658
  [Symbol.iterator]() {
9659
9659
  return this.indexToValue[Symbol.iterator]();
9660
9660
  }
9661
+ values() {
9662
+ return this.indexToValue[Symbol.iterator]();
9663
+ }
9661
9664
  has(value) {
9662
9665
  // 跟一般的实现不同, 不需要访问values数组.
9663
9666
  return value in this.valueToIndex;
@@ -9749,6 +9752,116 @@
9749
9752
  }
9750
9753
  }
9751
9754
 
9755
+ class TypedSparseMap {
9756
+ constructor(array) {
9757
+ this.keyToIndex = [];
9758
+ this.indexToKey = [];
9759
+ es2018$d.assert(array.length === 0, 'array should be empty');
9760
+ this.indexToValue = array;
9761
+ }
9762
+ get [Symbol.toStringTag]() {
9763
+ return this.constructor.name;
9764
+ }
9765
+ *entries() {
9766
+ for (let i = 0; i < this.indexToKey.length; i++) {
9767
+ yield [
9768
+ this.indexToKey[i],
9769
+ this.indexToValue.internalTypedArray[i]
9770
+ ];
9771
+ }
9772
+ }
9773
+ keys() {
9774
+ return this.indexToKey.values();
9775
+ }
9776
+ *values() {
9777
+ for (let i = 0; i < this.indexToKey.length; i++) {
9778
+ yield this.indexToValue.internalTypedArray[i];
9779
+ }
9780
+ }
9781
+ has(key) {
9782
+ const index = this.keyToIndex[key];
9783
+ return this.indexToKey[index] === key;
9784
+ }
9785
+ get(key) {
9786
+ const index = this.keyToIndex[key];
9787
+ if (this.indexToKey[index] === key) {
9788
+ return this.indexToValue.internalTypedArray[index];
9789
+ }
9790
+ else {
9791
+ return undefined;
9792
+ }
9793
+ }
9794
+ set(key, value) {
9795
+ const index = this.keyToIndex[key];
9796
+ if (this.indexToKey[index] === key) {
9797
+ this.indexToValue.internalTypedArray[index] = value;
9798
+ }
9799
+ else {
9800
+ const index = this.indexToKey.length;
9801
+ this.indexToKey.push(key);
9802
+ this.indexToValue.push(value);
9803
+ this.keyToIndex[key] = index;
9804
+ }
9805
+ }
9806
+ delete(key) {
9807
+ const index = this.keyToIndex[key];
9808
+ if (this.indexToKey[index] === key) {
9809
+ const lastKey = this.indexToKey.pop();
9810
+ const lastValue = this.indexToValue.pop();
9811
+ if (key !== lastKey) {
9812
+ this.indexToKey[index] = lastKey;
9813
+ this.indexToValue.internalTypedArray[index] = lastValue;
9814
+ this.keyToIndex[lastKey] = index;
9815
+ }
9816
+ }
9817
+ }
9818
+ }
9819
+
9820
+ class TypedSparseSet {
9821
+ constructor(array) {
9822
+ const valueToIndex = [];
9823
+ if (array.length > 0) {
9824
+ for (const [index, value] of array.internalTypedArray.entries()) {
9825
+ valueToIndex[value] = index;
9826
+ }
9827
+ }
9828
+ this.indexToValue = array;
9829
+ this.valueToIndex = valueToIndex;
9830
+ }
9831
+ get [Symbol.toStringTag]() {
9832
+ return this.constructor.name;
9833
+ }
9834
+ [Symbol.iterator]() {
9835
+ return this.values();
9836
+ }
9837
+ *values() {
9838
+ for (let i = 0; i < this.indexToValue.length; i++) {
9839
+ yield this.indexToValue.internalTypedArray[i];
9840
+ }
9841
+ }
9842
+ has(value) {
9843
+ const index = this.valueToIndex[value];
9844
+ return this.indexToValue.internalTypedArray[index] === value;
9845
+ }
9846
+ add(value) {
9847
+ if (!this.has(value)) {
9848
+ const index = this.indexToValue.length;
9849
+ this.indexToValue.push(value);
9850
+ this.valueToIndex[value] = index;
9851
+ }
9852
+ }
9853
+ delete(value) {
9854
+ const index = this.valueToIndex[value];
9855
+ if (this.indexToValue.internalTypedArray[index] === value) {
9856
+ const lastValue = this.indexToValue.pop();
9857
+ if (value !== lastValue) {
9858
+ this.indexToValue.internalTypedArray[index] = lastValue;
9859
+ this.valueToIndex[lastValue] = index;
9860
+ }
9861
+ }
9862
+ }
9863
+ }
9864
+
9752
9865
  // 相关提案: https://github.com/tc39/proposal-resizablearraybuffer
9753
9866
  class DynamicTypedArray {
9754
9867
  constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
@@ -9756,7 +9869,7 @@
9756
9869
  this._length = 0;
9757
9870
  es2018$d.assert(Number.isInteger(capacity), 'capacity must be an integer');
9758
9871
  es2018$d.assert(capacity >= 0, 'capacity must be greater than or equal to 0');
9759
- es2018$d.assert(growthFactor > 1, 'growthFactory must be greater than or equal to 1');
9872
+ es2018$d.assert(growthFactor >= 1, 'growthFactory must be greater than or equal to 1');
9760
9873
  this.array = new typedArrayConstructor(capacity);
9761
9874
  this.growthFactor = growthFactor;
9762
9875
  }
@@ -9835,131 +9948,40 @@
9835
9948
  }
9836
9949
  }
9837
9950
  function computeNewCapacity(oldCapacity, targetLength, growthFactor) {
9838
- if (oldCapacity === targetLength) {
9951
+ if (growthFactor === 1) {
9839
9952
  return targetLength;
9840
9953
  }
9841
- else if (oldCapacity < targetLength) {
9842
- // 增加容量以适配targetLength
9843
- let newCapacity = Math.max(oldCapacity, 1);
9844
- while (newCapacity < targetLength) {
9845
- newCapacity *= growthFactor;
9846
- }
9847
- return Math.floor(newCapacity);
9848
- }
9849
9954
  else {
9850
- // 尝试减少容量以适配targetLength
9851
- // `targetLength = 0`是特殊情况,
9852
- // 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
9853
- if (targetLength === 0) {
9854
- return 0;
9955
+ if (oldCapacity === targetLength) {
9956
+ return targetLength;
9855
9957
  }
9856
- else {
9857
- let newCapacity = oldCapacity;
9858
- while (true) {
9859
- const temp = newCapacity / growthFactor;
9860
- if (temp < targetLength) {
9861
- break;
9862
- }
9863
- else {
9864
- newCapacity = temp;
9865
- }
9958
+ else if (oldCapacity < targetLength) {
9959
+ // 增加容量以适配targetLength
9960
+ let newCapacity = Math.max(oldCapacity, 1);
9961
+ while (newCapacity < targetLength) {
9962
+ newCapacity *= growthFactor;
9866
9963
  }
9867
9964
  return Math.floor(newCapacity);
9868
9965
  }
9869
- }
9870
- }
9871
-
9872
- class TypedSparseMap {
9873
- constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
9874
- this.indexToValue = new DynamicTypedArray(typedArrayConstructor, { capacity, growthFactor });
9875
- this.keyToIndex = new DynamicTypedArray(Uint32Array, { capacity, growthFactor });
9876
- this.indexToKey = new DynamicTypedArray(Uint32Array, { capacity, growthFactor });
9877
- }
9878
- get [Symbol.toStringTag]() {
9879
- return this.constructor.name;
9880
- }
9881
- *entries() {
9882
- for (let i = 0; i < this.indexToKey.length; i++) {
9883
- yield [
9884
- this.indexToKey.internalTypedArray[i],
9885
- this.indexToValue.internalTypedArray[i]
9886
- ];
9887
- }
9888
- }
9889
- keys() {
9890
- return this.indexToKey.internalTypedArray.values();
9891
- }
9892
- values() {
9893
- return this.indexToValue.internalTypedArray.values();
9894
- }
9895
- has(key) {
9896
- const index = this.keyToIndex.internalTypedArray[key];
9897
- return this.indexToKey.internalTypedArray[index] === key;
9898
- }
9899
- get(key) {
9900
- const index = this.keyToIndex.internalTypedArray[key];
9901
- if (this.indexToKey.internalTypedArray[index] === key) {
9902
- return this.indexToValue.internalTypedArray[index];
9903
- }
9904
- else {
9905
- return undefined;
9906
- }
9907
- }
9908
- set(key, value) {
9909
- const index = this.keyToIndex.internalTypedArray[key];
9910
- if (this.indexToKey.internalTypedArray[index] === key) {
9911
- this.indexToValue.internalTypedArray[index] = value;
9912
- }
9913
9966
  else {
9914
- const index = this.indexToKey.length;
9915
- this.indexToKey.push(key);
9916
- this.indexToValue.push(value);
9917
- this.keyToIndex.set(key, index);
9918
- }
9919
- }
9920
- delete(key) {
9921
- const index = this.keyToIndex.internalTypedArray[key];
9922
- if (this.indexToKey.internalTypedArray[index] === key) {
9923
- const lastKey = this.indexToKey.pop();
9924
- const lastValue = this.indexToValue.pop();
9925
- if (key !== lastKey) {
9926
- this.indexToKey.internalTypedArray[index] = lastKey;
9927
- this.indexToValue.internalTypedArray[index] = lastValue;
9928
- this.keyToIndex.internalTypedArray[lastKey] = index;
9967
+ // 尝试减少容量以适配targetLength
9968
+ // `targetLength = 0`是特殊情况,
9969
+ // 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
9970
+ if (targetLength === 0) {
9971
+ return 0;
9929
9972
  }
9930
- }
9931
- }
9932
- }
9933
-
9934
- class TypedSparseSet {
9935
- constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
9936
- this.indexToValue = new DynamicTypedArray(typedArrayConstructor, { capacity, growthFactor });
9937
- this.valueToIndex = new DynamicTypedArray(Uint32Array, { capacity, growthFactor });
9938
- }
9939
- get [Symbol.toStringTag]() {
9940
- return this.constructor.name;
9941
- }
9942
- [Symbol.iterator]() {
9943
- return this.indexToValue.internalTypedArray[Symbol.iterator]();
9944
- }
9945
- has(value) {
9946
- const index = this.valueToIndex.internalTypedArray[value];
9947
- return this.indexToValue.internalTypedArray[index] === value;
9948
- }
9949
- add(value) {
9950
- if (!this.has(value)) {
9951
- const index = this.indexToValue.length;
9952
- this.indexToValue.push(value);
9953
- this.valueToIndex.set(value, index);
9954
- }
9955
- }
9956
- delete(value) {
9957
- const index = this.valueToIndex.internalTypedArray[value];
9958
- if (this.indexToValue.internalTypedArray[index] === value) {
9959
- const lastValue = this.indexToValue.pop();
9960
- if (value !== lastValue) {
9961
- this.indexToValue.internalTypedArray[index] = lastValue;
9962
- this.valueToIndex.internalTypedArray[lastValue] = index;
9973
+ else {
9974
+ let newCapacity = oldCapacity;
9975
+ while (true) {
9976
+ const temp = newCapacity / growthFactor;
9977
+ if (temp < targetLength) {
9978
+ break;
9979
+ }
9980
+ else {
9981
+ newCapacity = temp;
9982
+ }
9983
+ }
9984
+ return Math.floor(newCapacity);
9963
9985
  }
9964
9986
  }
9965
9987
  }