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