@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.
- package/README.md +34 -41
- package/dist/es2015/index.min.mjs +2 -2
- package/dist/es2015/index.min.mjs.map +1 -1
- package/dist/es2015/index.mjs +139 -117
- package/dist/es2015/index.mjs.map +1 -1
- package/dist/es2015/index.umd.js +139 -117
- package/dist/es2015/index.umd.js.map +1 -1
- package/dist/es2015/index.umd.min.js +2 -2
- package/dist/es2015/index.umd.min.js.map +1 -1
- package/dist/es2018/index.min.mjs +2 -2
- package/dist/es2018/index.min.mjs.map +1 -1
- package/dist/es2018/index.mjs +139 -117
- package/dist/es2018/index.mjs.map +1 -1
- package/dist/es2018/index.umd.js +139 -117
- package/dist/es2018/index.umd.js.map +1 -1
- package/dist/es2018/index.umd.min.js +2 -2
- package/dist/es2018/index.umd.min.js.map +1 -1
- package/lib/es2015/dynamic-typed-array.d.ts +1 -1
- package/lib/es2015/dynamic-typed-array.js +25 -20
- package/lib/es2015/dynamic-typed-array.js.map +1 -1
- package/lib/es2015/sparse-set.d.ts +1 -0
- package/lib/es2015/sparse-set.js +3 -0
- package/lib/es2015/sparse-set.js.map +1 -1
- package/lib/es2015/typed-sparse-map.d.ts +5 -9
- package/lib/es2015/typed-sparse-map.js +23 -20
- package/lib/es2015/typed-sparse-map.js.map +1 -1
- package/lib/es2015/typed-sparse-set.d.ts +3 -5
- package/lib/es2015/typed-sparse-set.js +19 -9
- package/lib/es2015/typed-sparse-set.js.map +1 -1
- package/lib/es2018/dynamic-typed-array.d.ts +1 -1
- package/lib/es2018/dynamic-typed-array.js +25 -20
- package/lib/es2018/dynamic-typed-array.js.map +1 -1
- package/lib/es2018/sparse-set.d.ts +1 -0
- package/lib/es2018/sparse-set.js +3 -0
- package/lib/es2018/sparse-set.js.map +1 -1
- package/lib/es2018/typed-sparse-map.d.ts +5 -9
- package/lib/es2018/typed-sparse-map.js +23 -20
- package/lib/es2018/typed-sparse-map.js.map +1 -1
- package/lib/es2018/typed-sparse-set.d.ts +3 -5
- package/lib/es2018/typed-sparse-set.js +19 -9
- package/lib/es2018/typed-sparse-set.js.map +1 -1
- package/package.json +1 -1
package/dist/es2018/index.mjs
CHANGED
|
@@ -9652,6 +9652,9 @@ class SparseSet {
|
|
|
9652
9652
|
[Symbol.iterator]() {
|
|
9653
9653
|
return this.indexToValue[Symbol.iterator]();
|
|
9654
9654
|
}
|
|
9655
|
+
values() {
|
|
9656
|
+
return this.indexToValue[Symbol.iterator]();
|
|
9657
|
+
}
|
|
9655
9658
|
has(value) {
|
|
9656
9659
|
// 跟一般的实现不同, 不需要访问values数组.
|
|
9657
9660
|
return value in this.valueToIndex;
|
|
@@ -9743,6 +9746,116 @@ class SparseMap {
|
|
|
9743
9746
|
}
|
|
9744
9747
|
}
|
|
9745
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
|
+
|
|
9746
9859
|
// 相关提案: https://github.com/tc39/proposal-resizablearraybuffer
|
|
9747
9860
|
class DynamicTypedArray {
|
|
9748
9861
|
constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
|
|
@@ -9750,7 +9863,7 @@ class DynamicTypedArray {
|
|
|
9750
9863
|
this._length = 0;
|
|
9751
9864
|
es2018$d.assert(Number.isInteger(capacity), 'capacity must be an integer');
|
|
9752
9865
|
es2018$d.assert(capacity >= 0, 'capacity must be greater than or equal to 0');
|
|
9753
|
-
es2018$d.assert(growthFactor
|
|
9866
|
+
es2018$d.assert(growthFactor >= 1, 'growthFactory must be greater than or equal to 1');
|
|
9754
9867
|
this.array = new typedArrayConstructor(capacity);
|
|
9755
9868
|
this.growthFactor = growthFactor;
|
|
9756
9869
|
}
|
|
@@ -9829,131 +9942,40 @@ class DynamicTypedArray {
|
|
|
9829
9942
|
}
|
|
9830
9943
|
}
|
|
9831
9944
|
function computeNewCapacity(oldCapacity, targetLength, growthFactor) {
|
|
9832
|
-
if (
|
|
9945
|
+
if (growthFactor === 1) {
|
|
9833
9946
|
return targetLength;
|
|
9834
9947
|
}
|
|
9835
|
-
else if (oldCapacity < targetLength) {
|
|
9836
|
-
// 增加容量以适配targetLength
|
|
9837
|
-
let newCapacity = Math.max(oldCapacity, 1);
|
|
9838
|
-
while (newCapacity < targetLength) {
|
|
9839
|
-
newCapacity *= growthFactor;
|
|
9840
|
-
}
|
|
9841
|
-
return Math.floor(newCapacity);
|
|
9842
|
-
}
|
|
9843
9948
|
else {
|
|
9844
|
-
|
|
9845
|
-
|
|
9846
|
-
// 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
|
|
9847
|
-
if (targetLength === 0) {
|
|
9848
|
-
return 0;
|
|
9949
|
+
if (oldCapacity === targetLength) {
|
|
9950
|
+
return targetLength;
|
|
9849
9951
|
}
|
|
9850
|
-
else {
|
|
9851
|
-
|
|
9852
|
-
|
|
9853
|
-
|
|
9854
|
-
|
|
9855
|
-
break;
|
|
9856
|
-
}
|
|
9857
|
-
else {
|
|
9858
|
-
newCapacity = temp;
|
|
9859
|
-
}
|
|
9952
|
+
else if (oldCapacity < targetLength) {
|
|
9953
|
+
// 增加容量以适配targetLength
|
|
9954
|
+
let newCapacity = Math.max(oldCapacity, 1);
|
|
9955
|
+
while (newCapacity < targetLength) {
|
|
9956
|
+
newCapacity *= growthFactor;
|
|
9860
9957
|
}
|
|
9861
9958
|
return Math.floor(newCapacity);
|
|
9862
9959
|
}
|
|
9863
|
-
}
|
|
9864
|
-
}
|
|
9865
|
-
|
|
9866
|
-
class TypedSparseMap {
|
|
9867
|
-
constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
|
|
9868
|
-
this.indexToValue = new DynamicTypedArray(typedArrayConstructor, { capacity, growthFactor });
|
|
9869
|
-
this.keyToIndex = new DynamicTypedArray(Uint32Array, { capacity, growthFactor });
|
|
9870
|
-
this.indexToKey = new DynamicTypedArray(Uint32Array, { capacity, growthFactor });
|
|
9871
|
-
}
|
|
9872
|
-
get [Symbol.toStringTag]() {
|
|
9873
|
-
return this.constructor.name;
|
|
9874
|
-
}
|
|
9875
|
-
*entries() {
|
|
9876
|
-
for (let i = 0; i < this.indexToKey.length; i++) {
|
|
9877
|
-
yield [
|
|
9878
|
-
this.indexToKey.internalTypedArray[i],
|
|
9879
|
-
this.indexToValue.internalTypedArray[i]
|
|
9880
|
-
];
|
|
9881
|
-
}
|
|
9882
|
-
}
|
|
9883
|
-
keys() {
|
|
9884
|
-
return this.indexToKey.internalTypedArray.values();
|
|
9885
|
-
}
|
|
9886
|
-
values() {
|
|
9887
|
-
return this.indexToValue.internalTypedArray.values();
|
|
9888
|
-
}
|
|
9889
|
-
has(key) {
|
|
9890
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9891
|
-
return this.indexToKey.internalTypedArray[index] === key;
|
|
9892
|
-
}
|
|
9893
|
-
get(key) {
|
|
9894
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9895
|
-
if (this.indexToKey.internalTypedArray[index] === key) {
|
|
9896
|
-
return this.indexToValue.internalTypedArray[index];
|
|
9897
|
-
}
|
|
9898
|
-
else {
|
|
9899
|
-
return undefined;
|
|
9900
|
-
}
|
|
9901
|
-
}
|
|
9902
|
-
set(key, value) {
|
|
9903
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9904
|
-
if (this.indexToKey.internalTypedArray[index] === key) {
|
|
9905
|
-
this.indexToValue.internalTypedArray[index] = value;
|
|
9906
|
-
}
|
|
9907
9960
|
else {
|
|
9908
|
-
|
|
9909
|
-
|
|
9910
|
-
|
|
9911
|
-
|
|
9912
|
-
|
|
9913
|
-
}
|
|
9914
|
-
delete(key) {
|
|
9915
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9916
|
-
if (this.indexToKey.internalTypedArray[index] === key) {
|
|
9917
|
-
const lastKey = this.indexToKey.pop();
|
|
9918
|
-
const lastValue = this.indexToValue.pop();
|
|
9919
|
-
if (key !== lastKey) {
|
|
9920
|
-
this.indexToKey.internalTypedArray[index] = lastKey;
|
|
9921
|
-
this.indexToValue.internalTypedArray[index] = lastValue;
|
|
9922
|
-
this.keyToIndex.internalTypedArray[lastKey] = index;
|
|
9961
|
+
// 尝试减少容量以适配targetLength
|
|
9962
|
+
// `targetLength = 0`是特殊情况,
|
|
9963
|
+
// 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
|
|
9964
|
+
if (targetLength === 0) {
|
|
9965
|
+
return 0;
|
|
9923
9966
|
}
|
|
9924
|
-
|
|
9925
|
-
|
|
9926
|
-
|
|
9927
|
-
|
|
9928
|
-
|
|
9929
|
-
|
|
9930
|
-
|
|
9931
|
-
|
|
9932
|
-
|
|
9933
|
-
|
|
9934
|
-
|
|
9935
|
-
|
|
9936
|
-
[Symbol.iterator]() {
|
|
9937
|
-
return this.indexToValue.internalTypedArray[Symbol.iterator]();
|
|
9938
|
-
}
|
|
9939
|
-
has(value) {
|
|
9940
|
-
const index = this.valueToIndex.internalTypedArray[value];
|
|
9941
|
-
return this.indexToValue.internalTypedArray[index] === value;
|
|
9942
|
-
}
|
|
9943
|
-
add(value) {
|
|
9944
|
-
if (!this.has(value)) {
|
|
9945
|
-
const index = this.indexToValue.length;
|
|
9946
|
-
this.indexToValue.push(value);
|
|
9947
|
-
this.valueToIndex.set(value, index);
|
|
9948
|
-
}
|
|
9949
|
-
}
|
|
9950
|
-
delete(value) {
|
|
9951
|
-
const index = this.valueToIndex.internalTypedArray[value];
|
|
9952
|
-
if (this.indexToValue.internalTypedArray[index] === value) {
|
|
9953
|
-
const lastValue = this.indexToValue.pop();
|
|
9954
|
-
if (value !== lastValue) {
|
|
9955
|
-
this.indexToValue.internalTypedArray[index] = lastValue;
|
|
9956
|
-
this.valueToIndex.internalTypedArray[lastValue] = index;
|
|
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);
|
|
9957
9979
|
}
|
|
9958
9980
|
}
|
|
9959
9981
|
}
|