priority-queue-typed 2.4.5 → 2.5.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 +66 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/priority-queue-typed.js +691 -116
- package/dist/umd/priority-queue-typed.js.map +1 -1
- package/dist/umd/priority-queue-typed.min.js +1 -1
- package/dist/umd/priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -36,52 +36,6 @@ var priorityQueueTyped = (() => {
|
|
|
36
36
|
Range: () => Range
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
// src/common/error.ts
|
|
40
|
-
var ERR = {
|
|
41
|
-
// Range / index
|
|
42
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
43
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
44
|
-
// Type / argument
|
|
45
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
46
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
47
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
48
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
49
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
50
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
51
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
52
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
53
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
54
|
-
// State / operation
|
|
55
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
56
|
-
// Matrix
|
|
57
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
58
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
59
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
60
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
61
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// src/common/index.ts
|
|
65
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
66
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
67
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
68
|
-
return DFSOperation2;
|
|
69
|
-
})(DFSOperation || {});
|
|
70
|
-
var Range = class {
|
|
71
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
72
|
-
this.low = low;
|
|
73
|
-
this.high = high;
|
|
74
|
-
this.includeLow = includeLow;
|
|
75
|
-
this.includeHigh = includeHigh;
|
|
76
|
-
}
|
|
77
|
-
// Determine whether a key is within the range
|
|
78
|
-
isInRange(key, comparator) {
|
|
79
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
80
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
81
|
-
return lowCheck && highCheck;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
39
|
// src/data-structures/base/iterable-element-base.ts
|
|
86
40
|
var IterableElementBase = class {
|
|
87
41
|
/**
|
|
@@ -104,7 +58,7 @@ var priorityQueueTyped = (() => {
|
|
|
104
58
|
if (options) {
|
|
105
59
|
const { toElementFn } = options;
|
|
106
60
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
107
|
-
else if (toElementFn) throw new TypeError(
|
|
61
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
108
62
|
}
|
|
109
63
|
}
|
|
110
64
|
/**
|
|
@@ -260,7 +214,7 @@ var priorityQueueTyped = (() => {
|
|
|
260
214
|
acc = initialValue;
|
|
261
215
|
} else {
|
|
262
216
|
const first = iter.next();
|
|
263
|
-
if (first.done) throw new TypeError(
|
|
217
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
264
218
|
acc = first.value;
|
|
265
219
|
index = 1;
|
|
266
220
|
}
|
|
@@ -302,6 +256,52 @@ var priorityQueueTyped = (() => {
|
|
|
302
256
|
}
|
|
303
257
|
};
|
|
304
258
|
|
|
259
|
+
// src/common/error.ts
|
|
260
|
+
var ERR = {
|
|
261
|
+
// Range / index
|
|
262
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
263
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
264
|
+
// Type / argument
|
|
265
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
266
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
267
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
268
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
269
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
270
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
271
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
272
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
273
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
274
|
+
// State / operation
|
|
275
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
276
|
+
// Matrix
|
|
277
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
278
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
279
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
280
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
281
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/common/index.ts
|
|
285
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
286
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
287
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
288
|
+
return DFSOperation2;
|
|
289
|
+
})(DFSOperation || {});
|
|
290
|
+
var Range = class {
|
|
291
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
292
|
+
this.low = low;
|
|
293
|
+
this.high = high;
|
|
294
|
+
this.includeLow = includeLow;
|
|
295
|
+
this.includeHigh = includeHigh;
|
|
296
|
+
}
|
|
297
|
+
// Determine whether a key is within the range
|
|
298
|
+
isInRange(key, comparator) {
|
|
299
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
300
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
301
|
+
return lowCheck && highCheck;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
305
|
// src/data-structures/heap/heap.ts
|
|
306
306
|
var Heap = class _Heap extends IterableElementBase {
|
|
307
307
|
/**
|
|
@@ -339,10 +339,51 @@ var priorityQueueTyped = (() => {
|
|
|
339
339
|
return this._elements;
|
|
340
340
|
}
|
|
341
341
|
/**
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
342
|
+
* Get the number of elements.
|
|
343
|
+
* @remarks Time O(1), Space O(1)
|
|
344
|
+
* @returns Heap size.
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
* @example
|
|
378
|
+
* // Track heap capacity
|
|
379
|
+
* const heap = new Heap<number>();
|
|
380
|
+
* console.log(heap.size); // 0;
|
|
381
|
+
* heap.add(10);
|
|
382
|
+
* heap.add(20);
|
|
383
|
+
* console.log(heap.size); // 2;
|
|
384
|
+
* heap.poll();
|
|
385
|
+
* console.log(heap.size); // 1;
|
|
386
|
+
*/
|
|
346
387
|
get size() {
|
|
347
388
|
return this.elements.length;
|
|
348
389
|
}
|
|
@@ -381,21 +422,103 @@ var priorityQueueTyped = (() => {
|
|
|
381
422
|
return new _Heap(elements, options);
|
|
382
423
|
}
|
|
383
424
|
/**
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
425
|
+
* Insert an element.
|
|
426
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
427
|
+
* @param element - Element to insert.
|
|
428
|
+
* @returns True.
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
* @example
|
|
462
|
+
* // basic Heap creation and add operation
|
|
463
|
+
* // Create a min heap (default)
|
|
464
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
465
|
+
*
|
|
466
|
+
* // Verify size
|
|
467
|
+
* console.log(minHeap.size); // 6;
|
|
468
|
+
*
|
|
469
|
+
* // Add new element
|
|
470
|
+
* minHeap.add(4);
|
|
471
|
+
* console.log(minHeap.size); // 7;
|
|
472
|
+
*
|
|
473
|
+
* // Min heap property: smallest element at root
|
|
474
|
+
* const min = minHeap.peek();
|
|
475
|
+
* console.log(min); // 1;
|
|
476
|
+
*/
|
|
389
477
|
add(element) {
|
|
390
478
|
this._elements.push(element);
|
|
391
479
|
return this._bubbleUp(this.elements.length - 1);
|
|
392
480
|
}
|
|
393
481
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
482
|
+
* Insert many elements from an iterable.
|
|
483
|
+
* @remarks Time O(N log N), Space O(1)
|
|
484
|
+
* @param elements - Iterable of elements or raw values.
|
|
485
|
+
* @returns Array of per-element success flags.
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
* @example
|
|
516
|
+
* // Add multiple elements
|
|
517
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
518
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
519
|
+
* console.log(heap.peek()); // 1;
|
|
520
|
+
* console.log(heap.size); // 4;
|
|
521
|
+
*/
|
|
399
522
|
addMany(elements) {
|
|
400
523
|
const flags = [];
|
|
401
524
|
for (const el of elements) {
|
|
@@ -410,10 +533,67 @@ var priorityQueueTyped = (() => {
|
|
|
410
533
|
return flags;
|
|
411
534
|
}
|
|
412
535
|
/**
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
536
|
+
* Remove and return the top element.
|
|
537
|
+
* @remarks Time O(log N), Space O(1)
|
|
538
|
+
* @returns Top element or undefined.
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
* @example
|
|
572
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
573
|
+
* interface Task {
|
|
574
|
+
* id: number;
|
|
575
|
+
* priority: number;
|
|
576
|
+
* name: string;
|
|
577
|
+
* }
|
|
578
|
+
*
|
|
579
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
580
|
+
* const tasks: Task[] = [
|
|
581
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
582
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
583
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
584
|
+
* ];
|
|
585
|
+
*
|
|
586
|
+
* const maxHeap = new Heap(tasks, {
|
|
587
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
588
|
+
* });
|
|
589
|
+
*
|
|
590
|
+
* console.log(maxHeap.size); // 3;
|
|
591
|
+
*
|
|
592
|
+
* // Peek returns highest priority task
|
|
593
|
+
* const topTask = maxHeap.peek();
|
|
594
|
+
* console.log(topTask?.priority); // 8;
|
|
595
|
+
* console.log(topTask?.name); // 'Alert';
|
|
596
|
+
*/
|
|
417
597
|
poll() {
|
|
418
598
|
if (this.elements.length === 0) return;
|
|
419
599
|
const value = this.elements[0];
|
|
@@ -425,26 +605,188 @@ var priorityQueueTyped = (() => {
|
|
|
425
605
|
return value;
|
|
426
606
|
}
|
|
427
607
|
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
608
|
+
* Get the current top element without removing it.
|
|
609
|
+
* @remarks Time O(1), Space O(1)
|
|
610
|
+
* @returns Top element or undefined.
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
* @example
|
|
644
|
+
* // Heap for event processing with priority
|
|
645
|
+
* interface Event {
|
|
646
|
+
* id: number;
|
|
647
|
+
* type: 'critical' | 'warning' | 'info';
|
|
648
|
+
* timestamp: number;
|
|
649
|
+
* message: string;
|
|
650
|
+
* }
|
|
651
|
+
*
|
|
652
|
+
* // Custom priority: critical > warning > info
|
|
653
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
654
|
+
*
|
|
655
|
+
* const eventHeap = new Heap<Event>([], {
|
|
656
|
+
* comparator: (a: Event, b: Event) => {
|
|
657
|
+
* const priorityA = priorityMap[a.type];
|
|
658
|
+
* const priorityB = priorityMap[b.type];
|
|
659
|
+
* return priorityB - priorityA; // Higher priority first
|
|
660
|
+
* }
|
|
661
|
+
* });
|
|
662
|
+
*
|
|
663
|
+
* // Add events in random order
|
|
664
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
665
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
666
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
667
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
668
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
669
|
+
*
|
|
670
|
+
* console.log(eventHeap.size); // 5;
|
|
671
|
+
*
|
|
672
|
+
* // Process events by priority (critical first)
|
|
673
|
+
* const processedOrder: Event[] = [];
|
|
674
|
+
* while (eventHeap.size > 0) {
|
|
675
|
+
* const event = eventHeap.poll();
|
|
676
|
+
* if (event) {
|
|
677
|
+
* processedOrder.push(event);
|
|
678
|
+
* }
|
|
679
|
+
* }
|
|
680
|
+
*
|
|
681
|
+
* // Verify critical events came first
|
|
682
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
683
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
684
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
685
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
686
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
687
|
+
*
|
|
688
|
+
* // Verify O(log n) operations
|
|
689
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
690
|
+
*
|
|
691
|
+
* // Add - O(log n)
|
|
692
|
+
* newHeap.add(2);
|
|
693
|
+
* console.log(newHeap.size); // 5;
|
|
694
|
+
*
|
|
695
|
+
* // Poll - O(log n)
|
|
696
|
+
* const removed = newHeap.poll();
|
|
697
|
+
* console.log(removed); // 1;
|
|
698
|
+
*
|
|
699
|
+
* // Peek - O(1)
|
|
700
|
+
* const top = newHeap.peek();
|
|
701
|
+
* console.log(top); // 2;
|
|
702
|
+
*/
|
|
432
703
|
peek() {
|
|
433
704
|
return this.elements[0];
|
|
434
705
|
}
|
|
435
706
|
/**
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
707
|
+
* Check whether the heap is empty.
|
|
708
|
+
* @remarks Time O(1), Space O(1)
|
|
709
|
+
* @returns True if size is 0.
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
* @example
|
|
741
|
+
* // Check if heap is empty
|
|
742
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
743
|
+
* console.log(heap.isEmpty()); // true;
|
|
744
|
+
* heap.add(1);
|
|
745
|
+
* console.log(heap.isEmpty()); // false;
|
|
746
|
+
*/
|
|
440
747
|
isEmpty() {
|
|
441
748
|
return this.size === 0;
|
|
442
749
|
}
|
|
443
750
|
/**
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
751
|
+
* Remove all elements.
|
|
752
|
+
* @remarks Time O(1), Space O(1)
|
|
753
|
+
* @returns void
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
* @example
|
|
785
|
+
* // Remove all elements
|
|
786
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
787
|
+
* heap.clear();
|
|
788
|
+
* console.log(heap.isEmpty()); // true;
|
|
789
|
+
*/
|
|
448
790
|
clear() {
|
|
449
791
|
this._elements = [];
|
|
450
792
|
}
|
|
@@ -459,21 +801,83 @@ var priorityQueueTyped = (() => {
|
|
|
459
801
|
return this.fix();
|
|
460
802
|
}
|
|
461
803
|
/**
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
804
|
+
* Check if an equal element exists in the heap.
|
|
805
|
+
* @remarks Time O(N), Space O(1)
|
|
806
|
+
* @param element - Element to search for.
|
|
807
|
+
* @returns True if found.
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
* @example
|
|
832
|
+
* // Check element existence
|
|
833
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
834
|
+
* console.log(heap.has(1)); // true;
|
|
835
|
+
* console.log(heap.has(99)); // false;
|
|
836
|
+
*/
|
|
467
837
|
has(element) {
|
|
468
838
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
469
839
|
return false;
|
|
470
840
|
}
|
|
471
841
|
/**
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
842
|
+
* Delete one occurrence of an element.
|
|
843
|
+
* @remarks Time O(N), Space O(1)
|
|
844
|
+
* @param element - Element to delete.
|
|
845
|
+
* @returns True if an element was removed.
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
* @example
|
|
876
|
+
* // Remove specific element
|
|
877
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
878
|
+
* heap.delete(4);
|
|
879
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
880
|
+
*/
|
|
477
881
|
delete(element) {
|
|
478
882
|
let index = -1;
|
|
479
883
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -531,11 +935,39 @@ var priorityQueueTyped = (() => {
|
|
|
531
935
|
return this;
|
|
532
936
|
}
|
|
533
937
|
/**
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
938
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
939
|
+
* @remarks Time O(N), Space O(H)
|
|
940
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
941
|
+
* @returns Array of visited elements.
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
* @example
|
|
966
|
+
* // Depth-first traversal
|
|
967
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
968
|
+
* const result = heap.dfs('IN');
|
|
969
|
+
* console.log(result.length); // 3;
|
|
970
|
+
*/
|
|
539
971
|
dfs(order = "PRE") {
|
|
540
972
|
const result = [];
|
|
541
973
|
const _dfs = (index) => {
|
|
@@ -572,10 +1004,47 @@ var priorityQueueTyped = (() => {
|
|
|
572
1004
|
return results;
|
|
573
1005
|
}
|
|
574
1006
|
/**
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
1007
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1008
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1009
|
+
* @returns Sorted array of elements.
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
* @example
|
|
1043
|
+
* // Sort elements using heap
|
|
1044
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1045
|
+
* const sorted = heap.sort();
|
|
1046
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1047
|
+
*/
|
|
579
1048
|
sort() {
|
|
580
1049
|
const visited = [];
|
|
581
1050
|
const cloned = this._createInstance();
|
|
@@ -587,22 +1056,94 @@ var priorityQueueTyped = (() => {
|
|
|
587
1056
|
return visited;
|
|
588
1057
|
}
|
|
589
1058
|
/**
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
1059
|
+
* Deep clone this heap.
|
|
1060
|
+
* @remarks Time O(N), Space O(N)
|
|
1061
|
+
* @returns A new heap with the same elements.
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
* @example
|
|
1093
|
+
* // Create independent copy
|
|
1094
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1095
|
+
* const copy = heap.clone();
|
|
1096
|
+
* copy.poll();
|
|
1097
|
+
* console.log(heap.size); // 3;
|
|
1098
|
+
* console.log(copy.size); // 2;
|
|
1099
|
+
*/
|
|
594
1100
|
clone() {
|
|
595
1101
|
const next = this._createInstance();
|
|
596
1102
|
for (const x of this.elements) next.add(x);
|
|
597
1103
|
return next;
|
|
598
1104
|
}
|
|
599
1105
|
/**
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
1106
|
+
* Filter elements into a new heap of the same class.
|
|
1107
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1108
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1109
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1110
|
+
* @returns A new heap with the kept elements.
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
* @example
|
|
1142
|
+
* // Filter elements
|
|
1143
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1144
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1145
|
+
* console.log(evens.size); // 2;
|
|
1146
|
+
*/
|
|
606
1147
|
filter(callback, thisArg) {
|
|
607
1148
|
const out = this._createInstance();
|
|
608
1149
|
let i = 0;
|
|
@@ -616,15 +1157,49 @@ var priorityQueueTyped = (() => {
|
|
|
616
1157
|
return out;
|
|
617
1158
|
}
|
|
618
1159
|
/**
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
1160
|
+
* Map elements into a new heap of possibly different element type.
|
|
1161
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1162
|
+
* @template EM
|
|
1163
|
+
* @template RM
|
|
1164
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1165
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1166
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1167
|
+
* @returns A new heap with mapped elements.
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
* @example
|
|
1198
|
+
* // Transform elements
|
|
1199
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1200
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1201
|
+
* console.log(doubled.peek()); // 2;
|
|
1202
|
+
*/
|
|
628
1203
|
map(callback, options, thisArg) {
|
|
629
1204
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
630
1205
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|