@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.
- package/README.md +44 -7
- package/dist/es2015/index.min.mjs +2 -2
- package/dist/es2015/index.min.mjs.map +1 -1
- package/dist/es2015/index.mjs +243 -119
- package/dist/es2015/index.mjs.map +1 -1
- package/dist/es2015/index.umd.js +244 -118
- 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 +243 -119
- package/dist/es2018/index.mjs.map +1 -1
- package/dist/es2018/index.umd.js +244 -118
- 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/index.d.ts +2 -0
- package/lib/es2015/index.js +2 -0
- package/lib/es2015/index.js.map +1 -1
- package/lib/es2015/sparse-map.d.ts +5 -5
- package/lib/es2015/sparse-map.js +24 -25
- package/lib/es2015/sparse-map.js.map +1 -1
- package/lib/es2015/sparse-set.d.ts +1 -0
- package/lib/es2015/sparse-set.js +17 -12
- package/lib/es2015/sparse-set.js.map +1 -1
- package/lib/es2015/typed-sparse-map.d.ts +16 -0
- package/lib/es2015/typed-sparse-map.js +70 -0
- package/lib/es2015/typed-sparse-map.js.map +1 -0
- package/lib/es2015/typed-sparse-set.d.ts +13 -0
- package/lib/es2015/typed-sparse-set.js +49 -0
- package/lib/es2015/typed-sparse-set.js.map +1 -0
- 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/index.d.ts +2 -0
- package/lib/es2018/index.js +2 -0
- package/lib/es2018/index.js.map +1 -1
- package/lib/es2018/sparse-map.d.ts +5 -5
- package/lib/es2018/sparse-map.js +24 -25
- package/lib/es2018/sparse-map.js.map +1 -1
- package/lib/es2018/sparse-set.d.ts +1 -0
- package/lib/es2018/sparse-set.js +17 -12
- package/lib/es2018/sparse-set.js.map +1 -1
- package/lib/es2018/typed-sparse-map.d.ts +16 -0
- package/lib/es2018/typed-sparse-map.js +70 -0
- package/lib/es2018/typed-sparse-map.js.map +1 -0
- package/lib/es2018/typed-sparse-set.d.ts +13 -0
- package/lib/es2018/typed-sparse-set.js +49 -0
- package/lib/es2018/typed-sparse-set.js.map +1 -0
- package/package.json +2 -2
package/dist/es2015/index.umd.js
CHANGED
|
@@ -9647,6 +9647,221 @@
|
|
|
9647
9647
|
}
|
|
9648
9648
|
}
|
|
9649
9649
|
|
|
9650
|
+
class SparseSet {
|
|
9651
|
+
constructor() {
|
|
9652
|
+
this.indexToValue = [];
|
|
9653
|
+
this.valueToIndex = [];
|
|
9654
|
+
}
|
|
9655
|
+
get [Symbol.toStringTag]() {
|
|
9656
|
+
return this.constructor.name;
|
|
9657
|
+
}
|
|
9658
|
+
[Symbol.iterator]() {
|
|
9659
|
+
return this.indexToValue[Symbol.iterator]();
|
|
9660
|
+
}
|
|
9661
|
+
values() {
|
|
9662
|
+
return this.indexToValue[Symbol.iterator]();
|
|
9663
|
+
}
|
|
9664
|
+
has(value) {
|
|
9665
|
+
// 跟一般的实现不同, 不需要访问values数组.
|
|
9666
|
+
return value in this.valueToIndex;
|
|
9667
|
+
}
|
|
9668
|
+
add(value) {
|
|
9669
|
+
if (!this.has(value)) {
|
|
9670
|
+
const index = this.indexToValue.length;
|
|
9671
|
+
this.indexToValue.push(value);
|
|
9672
|
+
this.valueToIndex[value] = index;
|
|
9673
|
+
}
|
|
9674
|
+
}
|
|
9675
|
+
delete(value) {
|
|
9676
|
+
if (this.has(value)) {
|
|
9677
|
+
const lastValue = this.indexToValue.pop();
|
|
9678
|
+
if (value === lastValue) {
|
|
9679
|
+
delete this.valueToIndex[value];
|
|
9680
|
+
}
|
|
9681
|
+
else {
|
|
9682
|
+
const index = this.valueToIndex[value];
|
|
9683
|
+
this.indexToValue[index] = lastValue;
|
|
9684
|
+
this.valueToIndex[lastValue] = index;
|
|
9685
|
+
// 跟一般的实现不同, 直接删除值的索引.
|
|
9686
|
+
delete this.valueToIndex[value];
|
|
9687
|
+
}
|
|
9688
|
+
}
|
|
9689
|
+
}
|
|
9690
|
+
}
|
|
9691
|
+
|
|
9692
|
+
class SparseMap {
|
|
9693
|
+
constructor() {
|
|
9694
|
+
this.keyToIndex = [];
|
|
9695
|
+
this.indexToKey = [];
|
|
9696
|
+
this.indexToValue = [];
|
|
9697
|
+
}
|
|
9698
|
+
get [Symbol.toStringTag]() {
|
|
9699
|
+
return this.constructor.name;
|
|
9700
|
+
}
|
|
9701
|
+
*entries() {
|
|
9702
|
+
for (let i = 0; i < this.indexToKey.length; i++) {
|
|
9703
|
+
yield [this.indexToKey[i], this.indexToValue[i]];
|
|
9704
|
+
}
|
|
9705
|
+
}
|
|
9706
|
+
keys() {
|
|
9707
|
+
return this.indexToKey.values();
|
|
9708
|
+
}
|
|
9709
|
+
values() {
|
|
9710
|
+
return this.indexToValue.values();
|
|
9711
|
+
}
|
|
9712
|
+
has(key) {
|
|
9713
|
+
// 跟一般的稀疏集实现不同, 不需要访问values数组.
|
|
9714
|
+
return key in this.keyToIndex;
|
|
9715
|
+
}
|
|
9716
|
+
get(key) {
|
|
9717
|
+
if (this.has(key)) {
|
|
9718
|
+
const index = this.keyToIndex[key];
|
|
9719
|
+
return this.indexToValue[index];
|
|
9720
|
+
}
|
|
9721
|
+
else {
|
|
9722
|
+
return undefined;
|
|
9723
|
+
}
|
|
9724
|
+
}
|
|
9725
|
+
set(key, value) {
|
|
9726
|
+
if (this.has(key)) {
|
|
9727
|
+
const index = this.keyToIndex[key];
|
|
9728
|
+
this.indexToValue[index] = value;
|
|
9729
|
+
}
|
|
9730
|
+
else {
|
|
9731
|
+
const index = this.indexToKey.length;
|
|
9732
|
+
this.indexToKey.push(key);
|
|
9733
|
+
this.indexToValue.push(value);
|
|
9734
|
+
this.keyToIndex[key] = index;
|
|
9735
|
+
}
|
|
9736
|
+
}
|
|
9737
|
+
delete(key) {
|
|
9738
|
+
if (this.has(key)) {
|
|
9739
|
+
const lastKey = this.indexToKey.pop();
|
|
9740
|
+
const lastValue = this.indexToValue.pop();
|
|
9741
|
+
if (key === lastKey) {
|
|
9742
|
+
delete this.keyToIndex[key];
|
|
9743
|
+
}
|
|
9744
|
+
else {
|
|
9745
|
+
const index = this.keyToIndex[key];
|
|
9746
|
+
this.indexToKey[index] = lastKey;
|
|
9747
|
+
this.indexToValue[index] = lastValue;
|
|
9748
|
+
this.keyToIndex[key] = index;
|
|
9749
|
+
delete this.keyToIndex[key];
|
|
9750
|
+
}
|
|
9751
|
+
}
|
|
9752
|
+
}
|
|
9753
|
+
}
|
|
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
|
+
|
|
9650
9865
|
// 相关提案: https://github.com/tc39/proposal-resizablearraybuffer
|
|
9651
9866
|
class DynamicTypedArray {
|
|
9652
9867
|
constructor(typedArrayConstructor, { capacity = 0, growthFactor = 1.5 } = {}) {
|
|
@@ -9654,7 +9869,7 @@
|
|
|
9654
9869
|
this._length = 0;
|
|
9655
9870
|
es2018$d.assert(Number.isInteger(capacity), 'capacity must be an integer');
|
|
9656
9871
|
es2018$d.assert(capacity >= 0, 'capacity must be greater than or equal to 0');
|
|
9657
|
-
es2018$d.assert(growthFactor
|
|
9872
|
+
es2018$d.assert(growthFactor >= 1, 'growthFactory must be greater than or equal to 1');
|
|
9658
9873
|
this.array = new typedArrayConstructor(capacity);
|
|
9659
9874
|
this.growthFactor = growthFactor;
|
|
9660
9875
|
}
|
|
@@ -9733,131 +9948,40 @@
|
|
|
9733
9948
|
}
|
|
9734
9949
|
}
|
|
9735
9950
|
function computeNewCapacity(oldCapacity, targetLength, growthFactor) {
|
|
9736
|
-
if (
|
|
9951
|
+
if (growthFactor === 1) {
|
|
9737
9952
|
return targetLength;
|
|
9738
9953
|
}
|
|
9739
|
-
else if (oldCapacity < targetLength) {
|
|
9740
|
-
// 增加容量以适配targetLength
|
|
9741
|
-
let newCapacity = Math.max(oldCapacity, 1);
|
|
9742
|
-
while (newCapacity < targetLength) {
|
|
9743
|
-
newCapacity *= growthFactor;
|
|
9744
|
-
}
|
|
9745
|
-
return Math.floor(newCapacity);
|
|
9746
|
-
}
|
|
9747
9954
|
else {
|
|
9748
|
-
|
|
9749
|
-
|
|
9750
|
-
// 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
|
|
9751
|
-
if (targetLength === 0) {
|
|
9752
|
-
return 0;
|
|
9955
|
+
if (oldCapacity === targetLength) {
|
|
9956
|
+
return targetLength;
|
|
9753
9957
|
}
|
|
9754
|
-
else {
|
|
9755
|
-
|
|
9756
|
-
|
|
9757
|
-
|
|
9758
|
-
|
|
9759
|
-
break;
|
|
9760
|
-
}
|
|
9761
|
-
else {
|
|
9762
|
-
newCapacity = temp;
|
|
9763
|
-
}
|
|
9958
|
+
else if (oldCapacity < targetLength) {
|
|
9959
|
+
// 增加容量以适配targetLength
|
|
9960
|
+
let newCapacity = Math.max(oldCapacity, 1);
|
|
9961
|
+
while (newCapacity < targetLength) {
|
|
9962
|
+
newCapacity *= growthFactor;
|
|
9764
9963
|
}
|
|
9765
9964
|
return Math.floor(newCapacity);
|
|
9766
9965
|
}
|
|
9767
|
-
}
|
|
9768
|
-
}
|
|
9769
|
-
|
|
9770
|
-
class SparseSet {
|
|
9771
|
-
constructor() {
|
|
9772
|
-
this.indexToValue = new DynamicTypedArray(Uint32Array);
|
|
9773
|
-
this.valueToIndex = new DynamicTypedArray(Uint32Array);
|
|
9774
|
-
}
|
|
9775
|
-
get [Symbol.toStringTag]() {
|
|
9776
|
-
return this.constructor.name;
|
|
9777
|
-
}
|
|
9778
|
-
[Symbol.iterator]() {
|
|
9779
|
-
return this.indexToValue.internalTypedArray[Symbol.iterator]();
|
|
9780
|
-
}
|
|
9781
|
-
has(value) {
|
|
9782
|
-
const index = this.valueToIndex.internalTypedArray[value];
|
|
9783
|
-
return this.indexToValue.internalTypedArray[index] === value;
|
|
9784
|
-
}
|
|
9785
|
-
add(value) {
|
|
9786
|
-
if (!this.has(value)) {
|
|
9787
|
-
const index = this.indexToValue.length;
|
|
9788
|
-
this.indexToValue.push(value);
|
|
9789
|
-
this.valueToIndex.set(value, index);
|
|
9790
|
-
}
|
|
9791
|
-
}
|
|
9792
|
-
delete(value) {
|
|
9793
|
-
const index = this.valueToIndex.internalTypedArray[value];
|
|
9794
|
-
if (this.indexToValue.internalTypedArray[index] === value) {
|
|
9795
|
-
const lastValue = this.indexToValue.pop();
|
|
9796
|
-
if (value !== lastValue) {
|
|
9797
|
-
this.indexToValue.internalTypedArray[index] = lastValue;
|
|
9798
|
-
this.valueToIndex.internalTypedArray[lastValue] = index;
|
|
9799
|
-
}
|
|
9800
|
-
}
|
|
9801
|
-
}
|
|
9802
|
-
}
|
|
9803
|
-
|
|
9804
|
-
class SparseMap {
|
|
9805
|
-
constructor() {
|
|
9806
|
-
this.keyToIndex = new DynamicTypedArray(Uint32Array);
|
|
9807
|
-
this.indexToKey = new DynamicTypedArray(Uint32Array);
|
|
9808
|
-
this.indexToValue = new DynamicTypedArray(Uint32Array);
|
|
9809
|
-
}
|
|
9810
|
-
get [Symbol.toStringTag]() {
|
|
9811
|
-
return this.constructor.name;
|
|
9812
|
-
}
|
|
9813
|
-
*entries() {
|
|
9814
|
-
for (let i = 0; i < this.indexToKey.length; i++) {
|
|
9815
|
-
yield [
|
|
9816
|
-
this.indexToKey.internalTypedArray[i],
|
|
9817
|
-
this.indexToValue.internalTypedArray[i]
|
|
9818
|
-
];
|
|
9819
|
-
}
|
|
9820
|
-
}
|
|
9821
|
-
keys() {
|
|
9822
|
-
return this.indexToKey.internalTypedArray.values();
|
|
9823
|
-
}
|
|
9824
|
-
values() {
|
|
9825
|
-
return this.indexToValue.internalTypedArray.values();
|
|
9826
|
-
}
|
|
9827
|
-
has(key) {
|
|
9828
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9829
|
-
return this.indexToKey.internalTypedArray[index] === key;
|
|
9830
|
-
}
|
|
9831
|
-
get(key) {
|
|
9832
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9833
|
-
if (this.indexToKey.internalTypedArray[index] === key) {
|
|
9834
|
-
return this.indexToValue.internalTypedArray[index];
|
|
9835
|
-
}
|
|
9836
|
-
else {
|
|
9837
|
-
return undefined;
|
|
9838
|
-
}
|
|
9839
|
-
}
|
|
9840
|
-
set(key, value) {
|
|
9841
|
-
const index = this.keyToIndex.internalTypedArray[key];
|
|
9842
|
-
if (this.indexToKey.internalTypedArray[index] === key) {
|
|
9843
|
-
this.indexToValue.internalTypedArray[index] = value;
|
|
9844
|
-
}
|
|
9845
9966
|
else {
|
|
9846
|
-
|
|
9847
|
-
|
|
9848
|
-
|
|
9849
|
-
|
|
9850
|
-
|
|
9851
|
-
|
|
9852
|
-
|
|
9853
|
-
|
|
9854
|
-
|
|
9855
|
-
|
|
9856
|
-
|
|
9857
|
-
|
|
9858
|
-
|
|
9859
|
-
|
|
9860
|
-
|
|
9967
|
+
// 尝试减少容量以适配targetLength
|
|
9968
|
+
// `targetLength = 0`是特殊情况,
|
|
9969
|
+
// 此时无论怎么将oldCapacity除以growthFactory几次, 都不可能得到比targetLength小的结果.
|
|
9970
|
+
if (targetLength === 0) {
|
|
9971
|
+
return 0;
|
|
9972
|
+
}
|
|
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);
|
|
9861
9985
|
}
|
|
9862
9986
|
}
|
|
9863
9987
|
}
|
|
@@ -9878,6 +10002,8 @@
|
|
|
9878
10002
|
exports.SparseSet = SparseSet;
|
|
9879
10003
|
exports.TLRUMap = TLRUMap;
|
|
9880
10004
|
exports.TrieMap = TrieMap;
|
|
10005
|
+
exports.TypedSparseMap = TypedSparseMap;
|
|
10006
|
+
exports.TypedSparseSet = TypedSparseSet;
|
|
9881
10007
|
exports.convertArrayToCons = convertArrayToCons;
|
|
9882
10008
|
exports.convertConsToArray = convertConsToArray;
|
|
9883
10009
|
|