data-structure-typed 2.2.6 → 2.2.8
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/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +47 -1
- package/README.md +19 -8
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +109 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +109 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +109 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +109 -107
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +105 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.leetcode.config.js +1 -1
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
var IterableElementBase = class {
|
|
4
|
+
static {
|
|
5
|
+
__name(this, "IterableElementBase");
|
|
6
|
+
}
|
|
7
|
+
constructor(options) {
|
|
8
|
+
if (options) {
|
|
9
|
+
const { toElementFn } = options;
|
|
10
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
11
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
_toElementFn;
|
|
15
|
+
get toElementFn() {
|
|
16
|
+
return this._toElementFn;
|
|
17
|
+
}
|
|
18
|
+
*[Symbol.iterator](...args) {
|
|
19
|
+
yield* this._getIterator(...args);
|
|
20
|
+
}
|
|
21
|
+
*values() {
|
|
22
|
+
for (const item of this) yield item;
|
|
23
|
+
}
|
|
24
|
+
every(predicate, thisArg) {
|
|
25
|
+
let index = 0;
|
|
26
|
+
for (const item of this) {
|
|
27
|
+
if (thisArg === void 0) {
|
|
28
|
+
if (!predicate(item, index++, this)) return false;
|
|
29
|
+
} else {
|
|
30
|
+
const fn = predicate;
|
|
31
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
some(predicate, thisArg) {
|
|
37
|
+
let index = 0;
|
|
38
|
+
for (const item of this) {
|
|
39
|
+
if (thisArg === void 0) {
|
|
40
|
+
if (predicate(item, index++, this)) return true;
|
|
41
|
+
} else {
|
|
42
|
+
const fn = predicate;
|
|
43
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
forEach(callbackfn, thisArg) {
|
|
49
|
+
let index = 0;
|
|
50
|
+
for (const item of this) {
|
|
51
|
+
if (thisArg === void 0) {
|
|
52
|
+
callbackfn(item, index++, this);
|
|
53
|
+
} else {
|
|
54
|
+
const fn = callbackfn;
|
|
55
|
+
fn.call(thisArg, item, index++, this);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
find(predicate, thisArg) {
|
|
60
|
+
let index = 0;
|
|
61
|
+
for (const item of this) {
|
|
62
|
+
if (thisArg === void 0) {
|
|
63
|
+
if (predicate(item, index++, this)) return item;
|
|
64
|
+
} else {
|
|
65
|
+
const fn = predicate;
|
|
66
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
has(element) {
|
|
72
|
+
for (const ele of this) if (ele === element) return true;
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
reduce(callbackfn, initialValue) {
|
|
76
|
+
let index = 0;
|
|
77
|
+
const iter = this[Symbol.iterator]();
|
|
78
|
+
let acc;
|
|
79
|
+
if (arguments.length >= 2) {
|
|
80
|
+
acc = initialValue;
|
|
81
|
+
} else {
|
|
82
|
+
const first = iter.next();
|
|
83
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
84
|
+
acc = first.value;
|
|
85
|
+
index = 1;
|
|
86
|
+
}
|
|
87
|
+
for (const value of iter) {
|
|
88
|
+
acc = callbackfn(acc, value, index++, this);
|
|
89
|
+
}
|
|
90
|
+
return acc;
|
|
91
|
+
}
|
|
92
|
+
toArray() {
|
|
93
|
+
return [...this];
|
|
94
|
+
}
|
|
95
|
+
toVisual() {
|
|
96
|
+
return [...this];
|
|
97
|
+
}
|
|
98
|
+
print() {
|
|
99
|
+
console.log(this.toVisual());
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var Heap = class _Heap extends IterableElementBase {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "Heap");
|
|
105
|
+
}
|
|
106
|
+
_equals = Object.is;
|
|
107
|
+
constructor(elements = [], options) {
|
|
108
|
+
super(options);
|
|
109
|
+
if (options) {
|
|
110
|
+
const { comparator } = options;
|
|
111
|
+
if (comparator) this._comparator = comparator;
|
|
112
|
+
}
|
|
113
|
+
this.addMany(elements);
|
|
114
|
+
}
|
|
115
|
+
_elements = [];
|
|
116
|
+
get elements() {
|
|
117
|
+
return this._elements;
|
|
118
|
+
}
|
|
119
|
+
get size() {
|
|
120
|
+
return this.elements.length;
|
|
121
|
+
}
|
|
122
|
+
get leaf() {
|
|
123
|
+
return this.elements[this.size - 1] ?? void 0;
|
|
124
|
+
}
|
|
125
|
+
static from(elements, options) {
|
|
126
|
+
return new this(elements, options);
|
|
127
|
+
}
|
|
128
|
+
static heapify(elements, options) {
|
|
129
|
+
return new _Heap(elements, options);
|
|
130
|
+
}
|
|
131
|
+
add(element) {
|
|
132
|
+
this._elements.push(element);
|
|
133
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
134
|
+
}
|
|
135
|
+
addMany(elements) {
|
|
136
|
+
const flags = [];
|
|
137
|
+
for (const el of elements) {
|
|
138
|
+
if (this.toElementFn) {
|
|
139
|
+
const ok = this.add(this.toElementFn(el));
|
|
140
|
+
flags.push(ok);
|
|
141
|
+
} else {
|
|
142
|
+
const ok = this.add(el);
|
|
143
|
+
flags.push(ok);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return flags;
|
|
147
|
+
}
|
|
148
|
+
poll() {
|
|
149
|
+
if (this.elements.length === 0) return;
|
|
150
|
+
const value = this.elements[0];
|
|
151
|
+
const last = this.elements.pop();
|
|
152
|
+
if (this.elements.length) {
|
|
153
|
+
this.elements[0] = last;
|
|
154
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
155
|
+
}
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
peek() {
|
|
159
|
+
return this.elements[0];
|
|
160
|
+
}
|
|
161
|
+
isEmpty() {
|
|
162
|
+
return this.size === 0;
|
|
163
|
+
}
|
|
164
|
+
clear() {
|
|
165
|
+
this._elements = [];
|
|
166
|
+
}
|
|
167
|
+
refill(elements) {
|
|
168
|
+
this._elements = Array.from(elements);
|
|
169
|
+
return this.fix();
|
|
170
|
+
}
|
|
171
|
+
has(element) {
|
|
172
|
+
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
delete(element) {
|
|
176
|
+
let index = -1;
|
|
177
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
178
|
+
if (this._equals(this.elements[i], element)) {
|
|
179
|
+
index = i;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (index < 0) return false;
|
|
184
|
+
if (index === 0) {
|
|
185
|
+
this.poll();
|
|
186
|
+
} else if (index === this.elements.length - 1) {
|
|
187
|
+
this.elements.pop();
|
|
188
|
+
} else {
|
|
189
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
190
|
+
this._bubbleUp(index);
|
|
191
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
deleteBy(predicate) {
|
|
196
|
+
let idx = -1;
|
|
197
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
198
|
+
if (predicate(this.elements[i], i, this)) {
|
|
199
|
+
idx = i;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (idx < 0) return false;
|
|
204
|
+
if (idx === 0) {
|
|
205
|
+
this.poll();
|
|
206
|
+
} else if (idx === this.elements.length - 1) {
|
|
207
|
+
this.elements.pop();
|
|
208
|
+
} else {
|
|
209
|
+
this.elements.splice(idx, 1, this.elements.pop());
|
|
210
|
+
this._bubbleUp(idx);
|
|
211
|
+
this._sinkDown(idx, this.elements.length >> 1);
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
setEquality(equals) {
|
|
216
|
+
this._equals = equals;
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
dfs(order = "PRE") {
|
|
220
|
+
const result = [];
|
|
221
|
+
const _dfs = __name((index) => {
|
|
222
|
+
const left = 2 * index + 1, right = left + 1;
|
|
223
|
+
if (index < this.size) {
|
|
224
|
+
if (order === "IN") {
|
|
225
|
+
_dfs(left);
|
|
226
|
+
result.push(this.elements[index]);
|
|
227
|
+
_dfs(right);
|
|
228
|
+
} else if (order === "PRE") {
|
|
229
|
+
result.push(this.elements[index]);
|
|
230
|
+
_dfs(left);
|
|
231
|
+
_dfs(right);
|
|
232
|
+
} else if (order === "POST") {
|
|
233
|
+
_dfs(left);
|
|
234
|
+
_dfs(right);
|
|
235
|
+
result.push(this.elements[index]);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}, "_dfs");
|
|
239
|
+
_dfs(0);
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
fix() {
|
|
243
|
+
const results = [];
|
|
244
|
+
for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
|
|
245
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
246
|
+
}
|
|
247
|
+
return results;
|
|
248
|
+
}
|
|
249
|
+
sort() {
|
|
250
|
+
const visited = [];
|
|
251
|
+
const cloned = this._createInstance();
|
|
252
|
+
for (const x of this.elements) cloned.add(x);
|
|
253
|
+
while (!cloned.isEmpty()) {
|
|
254
|
+
const top = cloned.poll();
|
|
255
|
+
if (top !== void 0) visited.push(top);
|
|
256
|
+
}
|
|
257
|
+
return visited;
|
|
258
|
+
}
|
|
259
|
+
clone() {
|
|
260
|
+
const next = this._createInstance();
|
|
261
|
+
for (const x of this.elements) next.add(x);
|
|
262
|
+
return next;
|
|
263
|
+
}
|
|
264
|
+
filter(callback, thisArg) {
|
|
265
|
+
const out = this._createInstance();
|
|
266
|
+
let i = 0;
|
|
267
|
+
for (const x of this) {
|
|
268
|
+
if (thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
|
|
269
|
+
out.add(x);
|
|
270
|
+
} else {
|
|
271
|
+
i++;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
map(callback, options, thisArg) {
|
|
277
|
+
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
278
|
+
if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
|
|
279
|
+
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
280
|
+
let i = 0;
|
|
281
|
+
for (const x of this) {
|
|
282
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
283
|
+
out.add(v);
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
mapSame(callback, thisArg) {
|
|
288
|
+
const out = this._createInstance();
|
|
289
|
+
let i = 0;
|
|
290
|
+
for (const x of this) {
|
|
291
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
292
|
+
out.add(v);
|
|
293
|
+
}
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
_DEFAULT_COMPARATOR = __name((a, b) => {
|
|
297
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
298
|
+
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
299
|
+
}
|
|
300
|
+
if (a > b) return 1;
|
|
301
|
+
if (a < b) return -1;
|
|
302
|
+
return 0;
|
|
303
|
+
}, "_DEFAULT_COMPARATOR");
|
|
304
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
305
|
+
get comparator() {
|
|
306
|
+
return this._comparator;
|
|
307
|
+
}
|
|
308
|
+
*_getIterator() {
|
|
309
|
+
for (const element of this.elements) yield element;
|
|
310
|
+
}
|
|
311
|
+
_bubbleUp(index) {
|
|
312
|
+
const element = this.elements[index];
|
|
313
|
+
while (index > 0) {
|
|
314
|
+
const parent = index - 1 >> 1;
|
|
315
|
+
const parentItem = this.elements[parent];
|
|
316
|
+
if (this.comparator(parentItem, element) <= 0) break;
|
|
317
|
+
this.elements[index] = parentItem;
|
|
318
|
+
index = parent;
|
|
319
|
+
}
|
|
320
|
+
this.elements[index] = element;
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
_sinkDown(index, halfLength) {
|
|
324
|
+
const element = this.elements[index];
|
|
325
|
+
while (index < halfLength) {
|
|
326
|
+
let left = index << 1 | 1;
|
|
327
|
+
const right = left + 1;
|
|
328
|
+
let minItem = this.elements[left];
|
|
329
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
330
|
+
left = right;
|
|
331
|
+
minItem = this.elements[right];
|
|
332
|
+
}
|
|
333
|
+
if (this.comparator(minItem, element) >= 0) break;
|
|
334
|
+
this.elements[index] = minItem;
|
|
335
|
+
index = left;
|
|
336
|
+
}
|
|
337
|
+
this.elements[index] = element;
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
_createInstance(options) {
|
|
341
|
+
const Ctor = this.constructor;
|
|
342
|
+
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
|
|
343
|
+
return next;
|
|
344
|
+
}
|
|
345
|
+
_createLike(elements = [], options) {
|
|
346
|
+
const Ctor = this.constructor;
|
|
347
|
+
return new Ctor(elements, options);
|
|
348
|
+
}
|
|
349
|
+
_spawnLike(options) {
|
|
350
|
+
return this._createLike([], options);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
var PriorityQueue = class extends Heap {
|
|
354
|
+
static {
|
|
355
|
+
__name(this, "PriorityQueue");
|
|
356
|
+
}
|
|
357
|
+
constructor(elements = [], options) {
|
|
358
|
+
super(elements, options);
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
var MaxPriorityQueue = class extends PriorityQueue {
|
|
362
|
+
static {
|
|
363
|
+
__name(this, "MaxPriorityQueue");
|
|
364
|
+
}
|
|
365
|
+
constructor(elements = [], options) {
|
|
366
|
+
super(elements, {
|
|
367
|
+
comparator: __name((a, b) => {
|
|
368
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
369
|
+
throw TypeError(
|
|
370
|
+
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
if (a < b) return 1;
|
|
374
|
+
if (a > b) return -1;
|
|
375
|
+
return 0;
|
|
376
|
+
}, "comparator"),
|
|
377
|
+
...options
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
export {
|
|
382
|
+
MaxPriorityQueue
|
|
383
|
+
};
|