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
|
@@ -227,6 +227,54 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
227
227
|
__name(_IterableElementBase, "IterableElementBase");
|
|
228
228
|
var IterableElementBase = _IterableElementBase;
|
|
229
229
|
|
|
230
|
+
// src/common/error.ts
|
|
231
|
+
var ERR = {
|
|
232
|
+
// Range / index
|
|
233
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
234
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
235
|
+
// Type / argument
|
|
236
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
237
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
238
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
239
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
240
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
241
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
242
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
243
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
244
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
245
|
+
// State / operation
|
|
246
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
247
|
+
// Matrix
|
|
248
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
249
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
250
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
251
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
252
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/common/index.ts
|
|
256
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
257
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
258
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
259
|
+
return DFSOperation2;
|
|
260
|
+
})(DFSOperation || {});
|
|
261
|
+
var _Range = class _Range {
|
|
262
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
263
|
+
this.low = low;
|
|
264
|
+
this.high = high;
|
|
265
|
+
this.includeLow = includeLow;
|
|
266
|
+
this.includeHigh = includeHigh;
|
|
267
|
+
}
|
|
268
|
+
// Determine whether a key is within the range
|
|
269
|
+
isInRange(key, comparator) {
|
|
270
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
271
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
272
|
+
return lowCheck && highCheck;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
__name(_Range, "Range");
|
|
276
|
+
var Range = _Range;
|
|
277
|
+
|
|
230
278
|
// src/data-structures/heap/heap.ts
|
|
231
279
|
var _Heap = class _Heap extends IterableElementBase {
|
|
232
280
|
/**
|
|
@@ -242,7 +290,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
242
290
|
__publicField(this, "_elements", []);
|
|
243
291
|
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
244
292
|
if (typeof a === "object" || typeof b === "object") {
|
|
245
|
-
throw TypeError(
|
|
293
|
+
throw new TypeError(ERR.comparatorRequired("Heap"));
|
|
246
294
|
}
|
|
247
295
|
if (a > b) return 1;
|
|
248
296
|
if (a < b) return -1;
|
|
@@ -264,10 +312,30 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
264
312
|
return this._elements;
|
|
265
313
|
}
|
|
266
314
|
/**
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
315
|
+
* Get the number of elements.
|
|
316
|
+
* @remarks Time O(1), Space O(1)
|
|
317
|
+
* @returns Heap size.
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
* @example
|
|
330
|
+
* // Track heap capacity
|
|
331
|
+
* const heap = new Heap<number>();
|
|
332
|
+
* console.log(heap.size); // 0;
|
|
333
|
+
* heap.add(10);
|
|
334
|
+
* heap.add(20);
|
|
335
|
+
* console.log(heap.size); // 2;
|
|
336
|
+
* heap.poll();
|
|
337
|
+
* console.log(heap.size); // 1;
|
|
338
|
+
*/
|
|
271
339
|
get size() {
|
|
272
340
|
return this.elements.length;
|
|
273
341
|
}
|
|
@@ -306,21 +374,61 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
306
374
|
return new _Heap(elements, options);
|
|
307
375
|
}
|
|
308
376
|
/**
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
377
|
+
* Insert an element.
|
|
378
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
379
|
+
* @param element - Element to insert.
|
|
380
|
+
* @returns True.
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
* @example
|
|
393
|
+
* // basic Heap creation and add operation
|
|
394
|
+
* // Create a min heap (default)
|
|
395
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
396
|
+
*
|
|
397
|
+
* // Verify size
|
|
398
|
+
* console.log(minHeap.size); // 6;
|
|
399
|
+
*
|
|
400
|
+
* // Add new element
|
|
401
|
+
* minHeap.add(4);
|
|
402
|
+
* console.log(minHeap.size); // 7;
|
|
403
|
+
*
|
|
404
|
+
* // Min heap property: smallest element at root
|
|
405
|
+
* const min = minHeap.peek();
|
|
406
|
+
* console.log(min); // 1;
|
|
407
|
+
*/
|
|
314
408
|
add(element) {
|
|
315
409
|
this._elements.push(element);
|
|
316
410
|
return this._bubbleUp(this.elements.length - 1);
|
|
317
411
|
}
|
|
318
412
|
/**
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
413
|
+
* Insert many elements from an iterable.
|
|
414
|
+
* @remarks Time O(N log N), Space O(1)
|
|
415
|
+
* @param elements - Iterable of elements or raw values.
|
|
416
|
+
* @returns Array of per-element success flags.
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
* @example
|
|
426
|
+
* // Add multiple elements
|
|
427
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
428
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
429
|
+
* console.log(heap.peek()); // 1;
|
|
430
|
+
* console.log(heap.size); // 4;
|
|
431
|
+
*/
|
|
324
432
|
addMany(elements) {
|
|
325
433
|
const flags = [];
|
|
326
434
|
for (const el of elements) {
|
|
@@ -335,10 +443,46 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
335
443
|
return flags;
|
|
336
444
|
}
|
|
337
445
|
/**
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
446
|
+
* Remove and return the top element.
|
|
447
|
+
* @remarks Time O(log N), Space O(1)
|
|
448
|
+
* @returns Top element or undefined.
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
* @example
|
|
461
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
462
|
+
* interface Task {
|
|
463
|
+
* id: number;
|
|
464
|
+
* priority: number;
|
|
465
|
+
* name: string;
|
|
466
|
+
* }
|
|
467
|
+
*
|
|
468
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
469
|
+
* const tasks: Task[] = [
|
|
470
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
471
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
472
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
473
|
+
* ];
|
|
474
|
+
*
|
|
475
|
+
* const maxHeap = new Heap(tasks, {
|
|
476
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
477
|
+
* });
|
|
478
|
+
*
|
|
479
|
+
* console.log(maxHeap.size); // 3;
|
|
480
|
+
*
|
|
481
|
+
* // Peek returns highest priority task
|
|
482
|
+
* const topTask = maxHeap.peek();
|
|
483
|
+
* console.log(topTask?.priority); // 8;
|
|
484
|
+
* console.log(topTask?.name); // 'Alert';
|
|
485
|
+
*/
|
|
342
486
|
poll() {
|
|
343
487
|
if (this.elements.length === 0) return;
|
|
344
488
|
const value = this.elements[0];
|
|
@@ -350,26 +494,125 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
350
494
|
return value;
|
|
351
495
|
}
|
|
352
496
|
/**
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
497
|
+
* Get the current top element without removing it.
|
|
498
|
+
* @remarks Time O(1), Space O(1)
|
|
499
|
+
* @returns Top element or undefined.
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
* @example
|
|
512
|
+
* // Heap for event processing with priority
|
|
513
|
+
* interface Event {
|
|
514
|
+
* id: number;
|
|
515
|
+
* type: 'critical' | 'warning' | 'info';
|
|
516
|
+
* timestamp: number;
|
|
517
|
+
* message: string;
|
|
518
|
+
* }
|
|
519
|
+
*
|
|
520
|
+
* // Custom priority: critical > warning > info
|
|
521
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
522
|
+
*
|
|
523
|
+
* const eventHeap = new Heap<Event>([], {
|
|
524
|
+
* comparator: (a: Event, b: Event) => {
|
|
525
|
+
* const priorityA = priorityMap[a.type];
|
|
526
|
+
* const priorityB = priorityMap[b.type];
|
|
527
|
+
* return priorityB - priorityA; // Higher priority first
|
|
528
|
+
* }
|
|
529
|
+
* });
|
|
530
|
+
*
|
|
531
|
+
* // Add events in random order
|
|
532
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
533
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
534
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
535
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
536
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
537
|
+
*
|
|
538
|
+
* console.log(eventHeap.size); // 5;
|
|
539
|
+
*
|
|
540
|
+
* // Process events by priority (critical first)
|
|
541
|
+
* const processedOrder: Event[] = [];
|
|
542
|
+
* while (eventHeap.size > 0) {
|
|
543
|
+
* const event = eventHeap.poll();
|
|
544
|
+
* if (event) {
|
|
545
|
+
* processedOrder.push(event);
|
|
546
|
+
* }
|
|
547
|
+
* }
|
|
548
|
+
*
|
|
549
|
+
* // Verify critical events came first
|
|
550
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
551
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
552
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
553
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
554
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
555
|
+
*
|
|
556
|
+
* // Verify O(log n) operations
|
|
557
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
558
|
+
*
|
|
559
|
+
* // Add - O(log n)
|
|
560
|
+
* newHeap.add(2);
|
|
561
|
+
* console.log(newHeap.size); // 5;
|
|
562
|
+
*
|
|
563
|
+
* // Poll - O(log n)
|
|
564
|
+
* const removed = newHeap.poll();
|
|
565
|
+
* console.log(removed); // 1;
|
|
566
|
+
*
|
|
567
|
+
* // Peek - O(1)
|
|
568
|
+
* const top = newHeap.peek();
|
|
569
|
+
* console.log(top); // 2;
|
|
570
|
+
*/
|
|
357
571
|
peek() {
|
|
358
572
|
return this.elements[0];
|
|
359
573
|
}
|
|
360
574
|
/**
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
575
|
+
* Check whether the heap is empty.
|
|
576
|
+
* @remarks Time O(1), Space O(1)
|
|
577
|
+
* @returns True if size is 0.
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
* @example
|
|
588
|
+
* // Check if heap is empty
|
|
589
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
590
|
+
* console.log(heap.isEmpty()); // true;
|
|
591
|
+
* heap.add(1);
|
|
592
|
+
* console.log(heap.isEmpty()); // false;
|
|
593
|
+
*/
|
|
365
594
|
isEmpty() {
|
|
366
595
|
return this.size === 0;
|
|
367
596
|
}
|
|
368
597
|
/**
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
598
|
+
* Remove all elements.
|
|
599
|
+
* @remarks Time O(1), Space O(1)
|
|
600
|
+
* @returns void
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
* @example
|
|
611
|
+
* // Remove all elements
|
|
612
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
613
|
+
* heap.clear();
|
|
614
|
+
* console.log(heap.isEmpty()); // true;
|
|
615
|
+
*/
|
|
373
616
|
clear() {
|
|
374
617
|
this._elements = [];
|
|
375
618
|
}
|
|
@@ -384,21 +627,41 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
384
627
|
return this.fix();
|
|
385
628
|
}
|
|
386
629
|
/**
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
630
|
+
* Check if an equal element exists in the heap.
|
|
631
|
+
* @remarks Time O(N), Space O(1)
|
|
632
|
+
* @param element - Element to search for.
|
|
633
|
+
* @returns True if found.
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
* @example
|
|
637
|
+
* // Check element existence
|
|
638
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
639
|
+
* console.log(heap.has(1)); // true;
|
|
640
|
+
* console.log(heap.has(99)); // false;
|
|
641
|
+
*/
|
|
392
642
|
has(element) {
|
|
393
643
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
394
644
|
return false;
|
|
395
645
|
}
|
|
396
646
|
/**
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
647
|
+
* Delete one occurrence of an element.
|
|
648
|
+
* @remarks Time O(N), Space O(1)
|
|
649
|
+
* @param element - Element to delete.
|
|
650
|
+
* @returns True if an element was removed.
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
* @example
|
|
660
|
+
* // Remove specific element
|
|
661
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
662
|
+
* heap.delete(4);
|
|
663
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
664
|
+
*/
|
|
402
665
|
delete(element) {
|
|
403
666
|
let index = -1;
|
|
404
667
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -456,11 +719,18 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
456
719
|
return this;
|
|
457
720
|
}
|
|
458
721
|
/**
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
722
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
723
|
+
* @remarks Time O(N), Space O(H)
|
|
724
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
725
|
+
* @returns Array of visited elements.
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
* @example
|
|
729
|
+
* // Depth-first traversal
|
|
730
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
731
|
+
* const result = heap.dfs('IN');
|
|
732
|
+
* console.log(result.length); // 3;
|
|
733
|
+
*/
|
|
464
734
|
dfs(order = "PRE") {
|
|
465
735
|
const result = [];
|
|
466
736
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -497,10 +767,26 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
497
767
|
return results;
|
|
498
768
|
}
|
|
499
769
|
/**
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
770
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
771
|
+
* @remarks Time O(N log N), Space O(N)
|
|
772
|
+
* @returns Sorted array of elements.
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
* @example
|
|
785
|
+
* // Sort elements using heap
|
|
786
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
787
|
+
* const sorted = heap.sort();
|
|
788
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
789
|
+
*/
|
|
504
790
|
sort() {
|
|
505
791
|
const visited = [];
|
|
506
792
|
const cloned = this._createInstance();
|
|
@@ -512,22 +798,52 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
512
798
|
return visited;
|
|
513
799
|
}
|
|
514
800
|
/**
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
801
|
+
* Deep clone this heap.
|
|
802
|
+
* @remarks Time O(N), Space O(N)
|
|
803
|
+
* @returns A new heap with the same elements.
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
* @example
|
|
814
|
+
* // Create independent copy
|
|
815
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
816
|
+
* const copy = heap.clone();
|
|
817
|
+
* copy.poll();
|
|
818
|
+
* console.log(heap.size); // 3;
|
|
819
|
+
* console.log(copy.size); // 2;
|
|
820
|
+
*/
|
|
519
821
|
clone() {
|
|
520
822
|
const next = this._createInstance();
|
|
521
823
|
for (const x of this.elements) next.add(x);
|
|
522
824
|
return next;
|
|
523
825
|
}
|
|
524
826
|
/**
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
827
|
+
* Filter elements into a new heap of the same class.
|
|
828
|
+
* @remarks Time O(N log N), Space O(N)
|
|
829
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
830
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
831
|
+
* @returns A new heap with the kept elements.
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
* @example
|
|
842
|
+
* // Filter elements
|
|
843
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
844
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
845
|
+
* console.log(evens.size); // 2;
|
|
846
|
+
*/
|
|
531
847
|
filter(callback, thisArg) {
|
|
532
848
|
const out = this._createInstance();
|
|
533
849
|
let i = 0;
|
|
@@ -541,18 +857,31 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
541
857
|
return out;
|
|
542
858
|
}
|
|
543
859
|
/**
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
860
|
+
* Map elements into a new heap of possibly different element type.
|
|
861
|
+
* @remarks Time O(N log N), Space O(N)
|
|
862
|
+
* @template EM
|
|
863
|
+
* @template RM
|
|
864
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
865
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
866
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
867
|
+
* @returns A new heap with mapped elements.
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
* @example
|
|
877
|
+
* // Transform elements
|
|
878
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
879
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
880
|
+
* console.log(doubled.peek()); // 2;
|
|
881
|
+
*/
|
|
553
882
|
map(callback, options, thisArg) {
|
|
554
883
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
555
|
-
if (!comparator) throw new TypeError("Heap.map
|
|
884
|
+
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
556
885
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
557
886
|
let i = 0;
|
|
558
887
|
for (const x of this) {
|
|
@@ -684,7 +1013,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
684
1013
|
__publicField(this, "_comparator");
|
|
685
1014
|
this.clear();
|
|
686
1015
|
this._comparator = comparator || this._defaultComparator;
|
|
687
|
-
if (typeof this.comparator !== "function") throw new
|
|
1016
|
+
if (typeof this.comparator !== "function") throw new TypeError(ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
688
1017
|
}
|
|
689
1018
|
/**
|
|
690
1019
|
* Get the circular root list head.
|
|
@@ -906,9 +1235,7 @@ var _MaxPriorityQueue = class _MaxPriorityQueue extends PriorityQueue {
|
|
|
906
1235
|
super(elements, {
|
|
907
1236
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
908
1237
|
if (typeof a === "object" || typeof b === "object") {
|
|
909
|
-
throw TypeError(
|
|
910
|
-
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
911
|
-
);
|
|
1238
|
+
throw new TypeError(ERR.comparatorRequired("MaxPriorityQueue"));
|
|
912
1239
|
}
|
|
913
1240
|
if (a < b) return 1;
|
|
914
1241
|
if (a > b) return -1;
|
|
@@ -920,29 +1247,6 @@ var _MaxPriorityQueue = class _MaxPriorityQueue extends PriorityQueue {
|
|
|
920
1247
|
};
|
|
921
1248
|
__name(_MaxPriorityQueue, "MaxPriorityQueue");
|
|
922
1249
|
var MaxPriorityQueue = _MaxPriorityQueue;
|
|
923
|
-
|
|
924
|
-
// src/common/index.ts
|
|
925
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
926
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
927
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
928
|
-
return DFSOperation2;
|
|
929
|
-
})(DFSOperation || {});
|
|
930
|
-
var _Range = class _Range {
|
|
931
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
932
|
-
this.low = low;
|
|
933
|
-
this.high = high;
|
|
934
|
-
this.includeLow = includeLow;
|
|
935
|
-
this.includeHigh = includeHigh;
|
|
936
|
-
}
|
|
937
|
-
// Determine whether a key is within the range
|
|
938
|
-
isInRange(key, comparator) {
|
|
939
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
940
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
941
|
-
return lowCheck && highCheck;
|
|
942
|
-
}
|
|
943
|
-
};
|
|
944
|
-
__name(_Range, "Range");
|
|
945
|
-
var Range = _Range;
|
|
946
1250
|
/**
|
|
947
1251
|
* data-structure-typed
|
|
948
1252
|
*
|
|
@@ -959,6 +1263,7 @@ var Range = _Range;
|
|
|
959
1263
|
*/
|
|
960
1264
|
|
|
961
1265
|
exports.DFSOperation = DFSOperation;
|
|
1266
|
+
exports.ERR = ERR;
|
|
962
1267
|
exports.FibonacciHeap = FibonacciHeap;
|
|
963
1268
|
exports.FibonacciHeapNode = FibonacciHeapNode;
|
|
964
1269
|
exports.Heap = Heap;
|