@blackglory/structures 0.10.0 → 0.10.1
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 +18 -1
- package/dist/es2015/index.min.mjs +2 -2
- package/dist/es2015/index.min.mjs.map +1 -1
- package/dist/es2015/index.mjs +198 -3
- package/dist/es2015/index.mjs.map +1 -1
- package/dist/es2015/index.umd.js +198 -2
- 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 +198 -3
- package/dist/es2018/index.mjs.map +1 -1
- package/dist/es2018/index.umd.js +198 -2
- 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/index.d.ts +1 -0
- package/lib/es2015/index.js +1 -0
- package/lib/es2015/index.js.map +1 -1
- package/lib/es2015/sorted-set.d.ts +10 -0
- package/lib/es2015/sorted-set.js +32 -0
- package/lib/es2015/sorted-set.js.map +1 -0
- package/lib/es2015/sparse-set.d.ts +1 -0
- package/lib/es2015/sparse-set.js +8 -2
- package/lib/es2015/sparse-set.js.map +1 -1
- package/lib/es2015/utils/skip-list.d.ts +24 -0
- package/lib/es2015/utils/skip-list.js +155 -0
- package/lib/es2015/utils/skip-list.js.map +1 -0
- package/lib/es2018/index.d.ts +1 -0
- package/lib/es2018/index.js +1 -0
- package/lib/es2018/index.js.map +1 -1
- package/lib/es2018/sorted-set.d.ts +10 -0
- package/lib/es2018/sorted-set.js +32 -0
- package/lib/es2018/sorted-set.js.map +1 -0
- package/lib/es2018/sparse-set.d.ts +1 -0
- package/lib/es2018/sparse-set.js +8 -2
- package/lib/es2018/sparse-set.js.map +1 -1
- package/lib/es2018/utils/skip-list.d.ts +24 -0
- package/lib/es2018/utils/skip-list.js +155 -0
- package/lib/es2018/utils/skip-list.js.map +1 -0
- package/package.json +1 -1
package/dist/es2018/index.mjs
CHANGED
|
@@ -9642,9 +9642,15 @@ class ObservableFiniteStateMachine extends FiniteStateMachine {
|
|
|
9642
9642
|
}
|
|
9643
9643
|
|
|
9644
9644
|
class SparseSet {
|
|
9645
|
-
constructor() {
|
|
9645
|
+
constructor(array = []) {
|
|
9646
9646
|
this.indexToValue = [];
|
|
9647
|
-
|
|
9647
|
+
const valueToIndex = [];
|
|
9648
|
+
if (array.length > 0) {
|
|
9649
|
+
for (const [index, value] of array.entries()) {
|
|
9650
|
+
valueToIndex[value] = index;
|
|
9651
|
+
}
|
|
9652
|
+
}
|
|
9653
|
+
this.valueToIndex = valueToIndex;
|
|
9648
9654
|
}
|
|
9649
9655
|
get [Symbol.toStringTag]() {
|
|
9650
9656
|
return this.constructor.name;
|
|
@@ -9981,5 +9987,194 @@ function computeNewCapacity(oldCapacity, targetLength, growthFactor) {
|
|
|
9981
9987
|
}
|
|
9982
9988
|
}
|
|
9983
9989
|
|
|
9984
|
-
|
|
9990
|
+
class SkipListNode {
|
|
9991
|
+
constructor(value) {
|
|
9992
|
+
this.value = value;
|
|
9993
|
+
this.previous = null;
|
|
9994
|
+
this.next = null;
|
|
9995
|
+
this.up = null;
|
|
9996
|
+
this.down = null;
|
|
9997
|
+
}
|
|
9998
|
+
}
|
|
9999
|
+
class SkipList {
|
|
10000
|
+
constructor(compare) {
|
|
10001
|
+
this.compare = compare;
|
|
10002
|
+
// 最顶层的首个节点
|
|
10003
|
+
this.head = new SkipListNode(null);
|
|
10004
|
+
// 最顶层的最后一个节点
|
|
10005
|
+
this.tail = new SkipListNode(null);
|
|
10006
|
+
this.height = 0;
|
|
10007
|
+
this._size = 0;
|
|
10008
|
+
this.head.next = this.tail;
|
|
10009
|
+
this.tail.previous = this.head;
|
|
10010
|
+
}
|
|
10011
|
+
// 元素数量
|
|
10012
|
+
get size() {
|
|
10013
|
+
return this._size;
|
|
10014
|
+
}
|
|
10015
|
+
*elements() {
|
|
10016
|
+
let currentNode = this.head;
|
|
10017
|
+
while (currentNode.down) {
|
|
10018
|
+
currentNode = currentNode.down;
|
|
10019
|
+
}
|
|
10020
|
+
while (currentNode.next && es2018$e.isntNull(currentNode.next.value)) {
|
|
10021
|
+
currentNode = currentNode.next;
|
|
10022
|
+
yield currentNode;
|
|
10023
|
+
}
|
|
10024
|
+
}
|
|
10025
|
+
dumpList() {
|
|
10026
|
+
const result = [];
|
|
10027
|
+
let head = this.head;
|
|
10028
|
+
do {
|
|
10029
|
+
result.push([]);
|
|
10030
|
+
let currentNode = head;
|
|
10031
|
+
do {
|
|
10032
|
+
result[result.length - 1].push(currentNode.value);
|
|
10033
|
+
currentNode = currentNode.next;
|
|
10034
|
+
} while (currentNode);
|
|
10035
|
+
} while (head = head.down);
|
|
10036
|
+
return result;
|
|
10037
|
+
}
|
|
10038
|
+
delete(value) {
|
|
10039
|
+
let currentNode = this.findNode(value);
|
|
10040
|
+
while (currentNode) {
|
|
10041
|
+
// 被删除的节点肯定不会是head或tail, 因此一定有previous
|
|
10042
|
+
currentNode.previous.next = currentNode.next;
|
|
10043
|
+
// 被删除的节点肯定不会是head或tail, 因此一定有next
|
|
10044
|
+
currentNode.next.previous = currentNode.previous;
|
|
10045
|
+
currentNode = currentNode.down;
|
|
10046
|
+
}
|
|
10047
|
+
this._size--;
|
|
10048
|
+
}
|
|
10049
|
+
has(value) {
|
|
10050
|
+
return es2018$e.isntNull(this.findNode(value));
|
|
10051
|
+
}
|
|
10052
|
+
findNode(value) {
|
|
10053
|
+
let currentNode = this.head;
|
|
10054
|
+
while (true) {
|
|
10055
|
+
while (currentNode.next && es2018$e.isntNull(currentNode.next.value)) {
|
|
10056
|
+
const result = this.compare(currentNode.next.value, value);
|
|
10057
|
+
if (result === 0) {
|
|
10058
|
+
return currentNode.next;
|
|
10059
|
+
}
|
|
10060
|
+
else if (result < 0) {
|
|
10061
|
+
currentNode = currentNode.next;
|
|
10062
|
+
}
|
|
10063
|
+
else {
|
|
10064
|
+
break;
|
|
10065
|
+
}
|
|
10066
|
+
}
|
|
10067
|
+
if (currentNode.down) {
|
|
10068
|
+
currentNode = currentNode.down;
|
|
10069
|
+
}
|
|
10070
|
+
else {
|
|
10071
|
+
return null;
|
|
10072
|
+
}
|
|
10073
|
+
}
|
|
10074
|
+
}
|
|
10075
|
+
add(value) {
|
|
10076
|
+
let previousNode = this.findPreviousNode(value);
|
|
10077
|
+
// 如果previousNode.next.value等于value, 则说明value已经存在
|
|
10078
|
+
if (previousNode.next &&
|
|
10079
|
+
es2018$e.isntNull(previousNode.next.value) &&
|
|
10080
|
+
this.compare(previousNode.next.value, value) === 0) {
|
|
10081
|
+
return;
|
|
10082
|
+
}
|
|
10083
|
+
let newNode = new SkipListNode(value);
|
|
10084
|
+
// 在底层加入新节点
|
|
10085
|
+
if (previousNode.next) {
|
|
10086
|
+
newNode.next = previousNode.next;
|
|
10087
|
+
previousNode.next.previous = newNode;
|
|
10088
|
+
}
|
|
10089
|
+
previousNode.next = newNode;
|
|
10090
|
+
newNode.previous = previousNode;
|
|
10091
|
+
// 在高层加入新节点, 具体加到多高是随机的, 从第一层(将其视作索引值0)开始建造
|
|
10092
|
+
// 层数非常高是极为罕见的, 因为每次建造层都会导致下一层被建造的概率减半.
|
|
10093
|
+
let level = 0;
|
|
10094
|
+
while (Math.random() < 0.5) {
|
|
10095
|
+
// currentLevel的索引与当前跳跃列表的高度相同, 需要在这之上建立一个新层
|
|
10096
|
+
if (level === this.height) {
|
|
10097
|
+
const newHead = new SkipListNode(null);
|
|
10098
|
+
const newTail = new SkipListNode(null);
|
|
10099
|
+
newHead.next = newTail;
|
|
10100
|
+
newHead.down = this.head;
|
|
10101
|
+
newTail.previous = newHead;
|
|
10102
|
+
newTail.down = this.tail;
|
|
10103
|
+
this.head.up = newHead;
|
|
10104
|
+
this.tail.up = newTail;
|
|
10105
|
+
this.head = newHead;
|
|
10106
|
+
this.tail = newTail;
|
|
10107
|
+
this.height++;
|
|
10108
|
+
}
|
|
10109
|
+
// 从新节点的前一个节点遍历, 直到找到能够通往上层的节点
|
|
10110
|
+
while (!previousNode.up) {
|
|
10111
|
+
// head节点必然会有up, 所以不可能访问进入循环体, 不可能访问`head.previous`
|
|
10112
|
+
previousNode = previousNode.previous;
|
|
10113
|
+
}
|
|
10114
|
+
// 上一层的对应节点
|
|
10115
|
+
previousNode = previousNode.up;
|
|
10116
|
+
// 创建新节点的上层节点
|
|
10117
|
+
const upperNewNode = new SkipListNode(value);
|
|
10118
|
+
if (previousNode.next) {
|
|
10119
|
+
upperNewNode.next = previousNode.next;
|
|
10120
|
+
previousNode.next.previous = upperNewNode;
|
|
10121
|
+
}
|
|
10122
|
+
previousNode.next = upperNewNode;
|
|
10123
|
+
upperNewNode.previous = previousNode;
|
|
10124
|
+
upperNewNode.down = newNode;
|
|
10125
|
+
newNode.up = upperNewNode;
|
|
10126
|
+
newNode = upperNewNode;
|
|
10127
|
+
level++;
|
|
10128
|
+
}
|
|
10129
|
+
this._size++;
|
|
10130
|
+
}
|
|
10131
|
+
findPreviousNode(value) {
|
|
10132
|
+
let currentNode = this.head;
|
|
10133
|
+
while (true) {
|
|
10134
|
+
while (currentNode.next && es2018$e.isntNull(currentNode.next.value)) {
|
|
10135
|
+
const result = this.compare(currentNode.next.value, value);
|
|
10136
|
+
if (result >= 0) {
|
|
10137
|
+
break;
|
|
10138
|
+
}
|
|
10139
|
+
else {
|
|
10140
|
+
currentNode = currentNode.next;
|
|
10141
|
+
}
|
|
10142
|
+
}
|
|
10143
|
+
if (currentNode.down) {
|
|
10144
|
+
currentNode = currentNode.down;
|
|
10145
|
+
}
|
|
10146
|
+
else {
|
|
10147
|
+
return currentNode;
|
|
10148
|
+
}
|
|
10149
|
+
}
|
|
10150
|
+
}
|
|
10151
|
+
}
|
|
10152
|
+
|
|
10153
|
+
class SortedSet {
|
|
10154
|
+
constructor(compare) {
|
|
10155
|
+
this.skipList = new SkipList(compare);
|
|
10156
|
+
}
|
|
10157
|
+
get [Symbol.toStringTag]() {
|
|
10158
|
+
return this.constructor.name;
|
|
10159
|
+
}
|
|
10160
|
+
*[Symbol.iterator]() {
|
|
10161
|
+
for (const value of this.values()) {
|
|
10162
|
+
yield value;
|
|
10163
|
+
}
|
|
10164
|
+
}
|
|
10165
|
+
values() {
|
|
10166
|
+
return es2018$4.map(this.skipList.elements(), node => node.value);
|
|
10167
|
+
}
|
|
10168
|
+
has(value) {
|
|
10169
|
+
return this.skipList.has(value);
|
|
10170
|
+
}
|
|
10171
|
+
add(value) {
|
|
10172
|
+
this.skipList.add(value);
|
|
10173
|
+
}
|
|
10174
|
+
delete(value) {
|
|
10175
|
+
this.skipList.delete(value);
|
|
10176
|
+
}
|
|
10177
|
+
}
|
|
10178
|
+
|
|
10179
|
+
export { BadEventError, Box, DynamicTypedArray, Emitter, ExpirableMap, FiniteStateMachine, HashMap, HashSet, LRUMap, ObservableFiniteStateMachine, Queue, SortedSet, SparseMap, SparseSet, TLRUMap, TrieMap, TypedSparseMap, TypedSparseSet, convertArrayToCons, convertConsToArray };
|
|
9985
10180
|
//# sourceMappingURL=index.mjs.map
|