max-priority-queue-typed 2.4.4 → 2.5.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 +63 -0
- package/dist/cjs/index.cjs +403 -98
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +402 -97
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +403 -99
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +402 -98
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -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 +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -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 +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/max-priority-queue-typed.js +400 -95
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -24,6 +24,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
24
24
|
var src_exports = {};
|
|
25
25
|
__export(src_exports, {
|
|
26
26
|
DFSOperation: () => DFSOperation,
|
|
27
|
+
ERR: () => ERR,
|
|
27
28
|
FibonacciHeap: () => FibonacciHeap,
|
|
28
29
|
FibonacciHeapNode: () => FibonacciHeapNode,
|
|
29
30
|
Heap: () => Heap,
|
|
@@ -252,6 +253,52 @@ var maxPriorityQueueTyped = (() => {
|
|
|
252
253
|
}
|
|
253
254
|
};
|
|
254
255
|
|
|
256
|
+
// src/common/error.ts
|
|
257
|
+
var ERR = {
|
|
258
|
+
// Range / index
|
|
259
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
260
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
261
|
+
// Type / argument
|
|
262
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
263
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
264
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
265
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
266
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
267
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
268
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
269
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
270
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
271
|
+
// State / operation
|
|
272
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
273
|
+
// Matrix
|
|
274
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
275
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
276
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
277
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
278
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// src/common/index.ts
|
|
282
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
283
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
284
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
285
|
+
return DFSOperation2;
|
|
286
|
+
})(DFSOperation || {});
|
|
287
|
+
var Range = class {
|
|
288
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
289
|
+
this.low = low;
|
|
290
|
+
this.high = high;
|
|
291
|
+
this.includeLow = includeLow;
|
|
292
|
+
this.includeHigh = includeHigh;
|
|
293
|
+
}
|
|
294
|
+
// Determine whether a key is within the range
|
|
295
|
+
isInRange(key, comparator) {
|
|
296
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
297
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
298
|
+
return lowCheck && highCheck;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
255
302
|
// src/data-structures/heap/heap.ts
|
|
256
303
|
var Heap = class _Heap extends IterableElementBase {
|
|
257
304
|
/**
|
|
@@ -267,7 +314,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
267
314
|
__publicField(this, "_elements", []);
|
|
268
315
|
__publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
|
|
269
316
|
if (typeof a === "object" || typeof b === "object") {
|
|
270
|
-
throw TypeError(
|
|
317
|
+
throw new TypeError(ERR.comparatorRequired("Heap"));
|
|
271
318
|
}
|
|
272
319
|
if (a > b) return 1;
|
|
273
320
|
if (a < b) return -1;
|
|
@@ -289,10 +336,30 @@ var maxPriorityQueueTyped = (() => {
|
|
|
289
336
|
return this._elements;
|
|
290
337
|
}
|
|
291
338
|
/**
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
339
|
+
* Get the number of elements.
|
|
340
|
+
* @remarks Time O(1), Space O(1)
|
|
341
|
+
* @returns Heap size.
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
* @example
|
|
354
|
+
* // Track heap capacity
|
|
355
|
+
* const heap = new Heap<number>();
|
|
356
|
+
* console.log(heap.size); // 0;
|
|
357
|
+
* heap.add(10);
|
|
358
|
+
* heap.add(20);
|
|
359
|
+
* console.log(heap.size); // 2;
|
|
360
|
+
* heap.poll();
|
|
361
|
+
* console.log(heap.size); // 1;
|
|
362
|
+
*/
|
|
296
363
|
get size() {
|
|
297
364
|
return this.elements.length;
|
|
298
365
|
}
|
|
@@ -331,21 +398,61 @@ var maxPriorityQueueTyped = (() => {
|
|
|
331
398
|
return new _Heap(elements, options);
|
|
332
399
|
}
|
|
333
400
|
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
401
|
+
* Insert an element.
|
|
402
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
403
|
+
* @param element - Element to insert.
|
|
404
|
+
* @returns True.
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
* @example
|
|
417
|
+
* // basic Heap creation and add operation
|
|
418
|
+
* // Create a min heap (default)
|
|
419
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
420
|
+
*
|
|
421
|
+
* // Verify size
|
|
422
|
+
* console.log(minHeap.size); // 6;
|
|
423
|
+
*
|
|
424
|
+
* // Add new element
|
|
425
|
+
* minHeap.add(4);
|
|
426
|
+
* console.log(minHeap.size); // 7;
|
|
427
|
+
*
|
|
428
|
+
* // Min heap property: smallest element at root
|
|
429
|
+
* const min = minHeap.peek();
|
|
430
|
+
* console.log(min); // 1;
|
|
431
|
+
*/
|
|
339
432
|
add(element) {
|
|
340
433
|
this._elements.push(element);
|
|
341
434
|
return this._bubbleUp(this.elements.length - 1);
|
|
342
435
|
}
|
|
343
436
|
/**
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
437
|
+
* Insert many elements from an iterable.
|
|
438
|
+
* @remarks Time O(N log N), Space O(1)
|
|
439
|
+
* @param elements - Iterable of elements or raw values.
|
|
440
|
+
* @returns Array of per-element success flags.
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
* @example
|
|
450
|
+
* // Add multiple elements
|
|
451
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
452
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
453
|
+
* console.log(heap.peek()); // 1;
|
|
454
|
+
* console.log(heap.size); // 4;
|
|
455
|
+
*/
|
|
349
456
|
addMany(elements) {
|
|
350
457
|
const flags = [];
|
|
351
458
|
for (const el of elements) {
|
|
@@ -360,10 +467,46 @@ var maxPriorityQueueTyped = (() => {
|
|
|
360
467
|
return flags;
|
|
361
468
|
}
|
|
362
469
|
/**
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
470
|
+
* Remove and return the top element.
|
|
471
|
+
* @remarks Time O(log N), Space O(1)
|
|
472
|
+
* @returns Top element or undefined.
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
* @example
|
|
485
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
486
|
+
* interface Task {
|
|
487
|
+
* id: number;
|
|
488
|
+
* priority: number;
|
|
489
|
+
* name: string;
|
|
490
|
+
* }
|
|
491
|
+
*
|
|
492
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
493
|
+
* const tasks: Task[] = [
|
|
494
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
495
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
496
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
497
|
+
* ];
|
|
498
|
+
*
|
|
499
|
+
* const maxHeap = new Heap(tasks, {
|
|
500
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
501
|
+
* });
|
|
502
|
+
*
|
|
503
|
+
* console.log(maxHeap.size); // 3;
|
|
504
|
+
*
|
|
505
|
+
* // Peek returns highest priority task
|
|
506
|
+
* const topTask = maxHeap.peek();
|
|
507
|
+
* console.log(topTask?.priority); // 8;
|
|
508
|
+
* console.log(topTask?.name); // 'Alert';
|
|
509
|
+
*/
|
|
367
510
|
poll() {
|
|
368
511
|
if (this.elements.length === 0) return;
|
|
369
512
|
const value = this.elements[0];
|
|
@@ -375,26 +518,125 @@ var maxPriorityQueueTyped = (() => {
|
|
|
375
518
|
return value;
|
|
376
519
|
}
|
|
377
520
|
/**
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
521
|
+
* Get the current top element without removing it.
|
|
522
|
+
* @remarks Time O(1), Space O(1)
|
|
523
|
+
* @returns Top element or undefined.
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
* @example
|
|
536
|
+
* // Heap for event processing with priority
|
|
537
|
+
* interface Event {
|
|
538
|
+
* id: number;
|
|
539
|
+
* type: 'critical' | 'warning' | 'info';
|
|
540
|
+
* timestamp: number;
|
|
541
|
+
* message: string;
|
|
542
|
+
* }
|
|
543
|
+
*
|
|
544
|
+
* // Custom priority: critical > warning > info
|
|
545
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
546
|
+
*
|
|
547
|
+
* const eventHeap = new Heap<Event>([], {
|
|
548
|
+
* comparator: (a: Event, b: Event) => {
|
|
549
|
+
* const priorityA = priorityMap[a.type];
|
|
550
|
+
* const priorityB = priorityMap[b.type];
|
|
551
|
+
* return priorityB - priorityA; // Higher priority first
|
|
552
|
+
* }
|
|
553
|
+
* });
|
|
554
|
+
*
|
|
555
|
+
* // Add events in random order
|
|
556
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
557
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
558
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
559
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
560
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
561
|
+
*
|
|
562
|
+
* console.log(eventHeap.size); // 5;
|
|
563
|
+
*
|
|
564
|
+
* // Process events by priority (critical first)
|
|
565
|
+
* const processedOrder: Event[] = [];
|
|
566
|
+
* while (eventHeap.size > 0) {
|
|
567
|
+
* const event = eventHeap.poll();
|
|
568
|
+
* if (event) {
|
|
569
|
+
* processedOrder.push(event);
|
|
570
|
+
* }
|
|
571
|
+
* }
|
|
572
|
+
*
|
|
573
|
+
* // Verify critical events came first
|
|
574
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
575
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
576
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
577
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
578
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
579
|
+
*
|
|
580
|
+
* // Verify O(log n) operations
|
|
581
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
582
|
+
*
|
|
583
|
+
* // Add - O(log n)
|
|
584
|
+
* newHeap.add(2);
|
|
585
|
+
* console.log(newHeap.size); // 5;
|
|
586
|
+
*
|
|
587
|
+
* // Poll - O(log n)
|
|
588
|
+
* const removed = newHeap.poll();
|
|
589
|
+
* console.log(removed); // 1;
|
|
590
|
+
*
|
|
591
|
+
* // Peek - O(1)
|
|
592
|
+
* const top = newHeap.peek();
|
|
593
|
+
* console.log(top); // 2;
|
|
594
|
+
*/
|
|
382
595
|
peek() {
|
|
383
596
|
return this.elements[0];
|
|
384
597
|
}
|
|
385
598
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
599
|
+
* Check whether the heap is empty.
|
|
600
|
+
* @remarks Time O(1), Space O(1)
|
|
601
|
+
* @returns True if size is 0.
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
* @example
|
|
612
|
+
* // Check if heap is empty
|
|
613
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
614
|
+
* console.log(heap.isEmpty()); // true;
|
|
615
|
+
* heap.add(1);
|
|
616
|
+
* console.log(heap.isEmpty()); // false;
|
|
617
|
+
*/
|
|
390
618
|
isEmpty() {
|
|
391
619
|
return this.size === 0;
|
|
392
620
|
}
|
|
393
621
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
622
|
+
* Remove all elements.
|
|
623
|
+
* @remarks Time O(1), Space O(1)
|
|
624
|
+
* @returns void
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
* @example
|
|
635
|
+
* // Remove all elements
|
|
636
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
637
|
+
* heap.clear();
|
|
638
|
+
* console.log(heap.isEmpty()); // true;
|
|
639
|
+
*/
|
|
398
640
|
clear() {
|
|
399
641
|
this._elements = [];
|
|
400
642
|
}
|
|
@@ -409,21 +651,41 @@ var maxPriorityQueueTyped = (() => {
|
|
|
409
651
|
return this.fix();
|
|
410
652
|
}
|
|
411
653
|
/**
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
654
|
+
* Check if an equal element exists in the heap.
|
|
655
|
+
* @remarks Time O(N), Space O(1)
|
|
656
|
+
* @param element - Element to search for.
|
|
657
|
+
* @returns True if found.
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
* @example
|
|
661
|
+
* // Check element existence
|
|
662
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
663
|
+
* console.log(heap.has(1)); // true;
|
|
664
|
+
* console.log(heap.has(99)); // false;
|
|
665
|
+
*/
|
|
417
666
|
has(element) {
|
|
418
667
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
419
668
|
return false;
|
|
420
669
|
}
|
|
421
670
|
/**
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
671
|
+
* Delete one occurrence of an element.
|
|
672
|
+
* @remarks Time O(N), Space O(1)
|
|
673
|
+
* @param element - Element to delete.
|
|
674
|
+
* @returns True if an element was removed.
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
* @example
|
|
684
|
+
* // Remove specific element
|
|
685
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
686
|
+
* heap.delete(4);
|
|
687
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
688
|
+
*/
|
|
427
689
|
delete(element) {
|
|
428
690
|
let index = -1;
|
|
429
691
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -481,11 +743,18 @@ var maxPriorityQueueTyped = (() => {
|
|
|
481
743
|
return this;
|
|
482
744
|
}
|
|
483
745
|
/**
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
746
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
747
|
+
* @remarks Time O(N), Space O(H)
|
|
748
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
749
|
+
* @returns Array of visited elements.
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
* @example
|
|
753
|
+
* // Depth-first traversal
|
|
754
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
755
|
+
* const result = heap.dfs('IN');
|
|
756
|
+
* console.log(result.length); // 3;
|
|
757
|
+
*/
|
|
489
758
|
dfs(order = "PRE") {
|
|
490
759
|
const result = [];
|
|
491
760
|
const _dfs = (index) => {
|
|
@@ -522,10 +791,26 @@ var maxPriorityQueueTyped = (() => {
|
|
|
522
791
|
return results;
|
|
523
792
|
}
|
|
524
793
|
/**
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
794
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
795
|
+
* @remarks Time O(N log N), Space O(N)
|
|
796
|
+
* @returns Sorted array of elements.
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
* @example
|
|
809
|
+
* // Sort elements using heap
|
|
810
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
811
|
+
* const sorted = heap.sort();
|
|
812
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
813
|
+
*/
|
|
529
814
|
sort() {
|
|
530
815
|
const visited = [];
|
|
531
816
|
const cloned = this._createInstance();
|
|
@@ -537,22 +822,52 @@ var maxPriorityQueueTyped = (() => {
|
|
|
537
822
|
return visited;
|
|
538
823
|
}
|
|
539
824
|
/**
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
825
|
+
* Deep clone this heap.
|
|
826
|
+
* @remarks Time O(N), Space O(N)
|
|
827
|
+
* @returns A new heap with the same elements.
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
* @example
|
|
838
|
+
* // Create independent copy
|
|
839
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
840
|
+
* const copy = heap.clone();
|
|
841
|
+
* copy.poll();
|
|
842
|
+
* console.log(heap.size); // 3;
|
|
843
|
+
* console.log(copy.size); // 2;
|
|
844
|
+
*/
|
|
544
845
|
clone() {
|
|
545
846
|
const next = this._createInstance();
|
|
546
847
|
for (const x of this.elements) next.add(x);
|
|
547
848
|
return next;
|
|
548
849
|
}
|
|
549
850
|
/**
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
851
|
+
* Filter elements into a new heap of the same class.
|
|
852
|
+
* @remarks Time O(N log N), Space O(N)
|
|
853
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
854
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
855
|
+
* @returns A new heap with the kept elements.
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
* @example
|
|
866
|
+
* // Filter elements
|
|
867
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
868
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
869
|
+
* console.log(evens.size); // 2;
|
|
870
|
+
*/
|
|
556
871
|
filter(callback, thisArg) {
|
|
557
872
|
const out = this._createInstance();
|
|
558
873
|
let i = 0;
|
|
@@ -566,18 +881,31 @@ var maxPriorityQueueTyped = (() => {
|
|
|
566
881
|
return out;
|
|
567
882
|
}
|
|
568
883
|
/**
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
884
|
+
* Map elements into a new heap of possibly different element type.
|
|
885
|
+
* @remarks Time O(N log N), Space O(N)
|
|
886
|
+
* @template EM
|
|
887
|
+
* @template RM
|
|
888
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
889
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
890
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
891
|
+
* @returns A new heap with mapped elements.
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
* @example
|
|
901
|
+
* // Transform elements
|
|
902
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
903
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
904
|
+
* console.log(doubled.peek()); // 2;
|
|
905
|
+
*/
|
|
578
906
|
map(callback, options, thisArg) {
|
|
579
907
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
580
|
-
if (!comparator) throw new TypeError("Heap.map
|
|
908
|
+
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
581
909
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
582
910
|
let i = 0;
|
|
583
911
|
for (const x of this) {
|
|
@@ -705,7 +1033,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
705
1033
|
__publicField(this, "_comparator");
|
|
706
1034
|
this.clear();
|
|
707
1035
|
this._comparator = comparator || this._defaultComparator;
|
|
708
|
-
if (typeof this.comparator !== "function") throw new
|
|
1036
|
+
if (typeof this.comparator !== "function") throw new TypeError(ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
709
1037
|
}
|
|
710
1038
|
/**
|
|
711
1039
|
* Get the circular root list head.
|
|
@@ -923,9 +1251,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
923
1251
|
super(elements, {
|
|
924
1252
|
comparator: (a, b) => {
|
|
925
1253
|
if (typeof a === "object" || typeof b === "object") {
|
|
926
|
-
throw TypeError(
|
|
927
|
-
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
928
|
-
);
|
|
1254
|
+
throw new TypeError(ERR.comparatorRequired("MaxPriorityQueue"));
|
|
929
1255
|
}
|
|
930
1256
|
if (a < b) return 1;
|
|
931
1257
|
if (a > b) return -1;
|
|
@@ -935,27 +1261,6 @@ var maxPriorityQueueTyped = (() => {
|
|
|
935
1261
|
});
|
|
936
1262
|
}
|
|
937
1263
|
};
|
|
938
|
-
|
|
939
|
-
// src/common/index.ts
|
|
940
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
941
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
942
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
943
|
-
return DFSOperation2;
|
|
944
|
-
})(DFSOperation || {});
|
|
945
|
-
var Range = class {
|
|
946
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
947
|
-
this.low = low;
|
|
948
|
-
this.high = high;
|
|
949
|
-
this.includeLow = includeLow;
|
|
950
|
-
this.includeHigh = includeHigh;
|
|
951
|
-
}
|
|
952
|
-
// Determine whether a key is within the range
|
|
953
|
-
isInRange(key, comparator) {
|
|
954
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
955
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
956
|
-
return lowCheck && highCheck;
|
|
957
|
-
}
|
|
958
|
-
};
|
|
959
1264
|
return __toCommonJS(src_exports);
|
|
960
1265
|
})();
|
|
961
1266
|
/**
|