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