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