stack-typed 2.4.5 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -36
- package/dist/cjs/index.cjs +480 -100
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +479 -99
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +480 -100
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +479 -99
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/stack-typed.js +477 -97
- package/dist/umd/stack-typed.js.map +1 -1
- package/dist/umd/stack-typed.min.js +1 -1
- package/dist/umd/stack-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
package/dist/umd/stack-typed.js
CHANGED
|
@@ -29,52 +29,6 @@ var stackTyped = (() => {
|
|
|
29
29
|
Stack: () => Stack
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
// src/common/error.ts
|
|
33
|
-
var ERR = {
|
|
34
|
-
// Range / index
|
|
35
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
36
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
37
|
-
// Type / argument
|
|
38
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
39
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
40
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
41
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
42
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
43
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
44
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
45
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
46
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
47
|
-
// State / operation
|
|
48
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
49
|
-
// Matrix
|
|
50
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
51
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
52
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
53
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
54
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// src/common/index.ts
|
|
58
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
59
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
60
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
61
|
-
return DFSOperation2;
|
|
62
|
-
})(DFSOperation || {});
|
|
63
|
-
var Range = class {
|
|
64
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
65
|
-
this.low = low;
|
|
66
|
-
this.high = high;
|
|
67
|
-
this.includeLow = includeLow;
|
|
68
|
-
this.includeHigh = includeHigh;
|
|
69
|
-
}
|
|
70
|
-
// Determine whether a key is within the range
|
|
71
|
-
isInRange(key, comparator) {
|
|
72
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
73
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
74
|
-
return lowCheck && highCheck;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
32
|
// src/data-structures/base/iterable-element-base.ts
|
|
79
33
|
var IterableElementBase = class {
|
|
80
34
|
/**
|
|
@@ -97,7 +51,7 @@ var stackTyped = (() => {
|
|
|
97
51
|
if (options) {
|
|
98
52
|
const { toElementFn } = options;
|
|
99
53
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
100
|
-
else if (toElementFn) throw new TypeError(
|
|
54
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
101
55
|
}
|
|
102
56
|
}
|
|
103
57
|
/**
|
|
@@ -253,7 +207,7 @@ var stackTyped = (() => {
|
|
|
253
207
|
acc = initialValue;
|
|
254
208
|
} else {
|
|
255
209
|
const first = iter.next();
|
|
256
|
-
if (first.done) throw new TypeError(
|
|
210
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
257
211
|
acc = first.value;
|
|
258
212
|
index = 1;
|
|
259
213
|
}
|
|
@@ -319,10 +273,44 @@ var stackTyped = (() => {
|
|
|
319
273
|
return this._elements;
|
|
320
274
|
}
|
|
321
275
|
/**
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
276
|
+
* Get the number of stored elements.
|
|
277
|
+
* @remarks Time O(1), Space O(1)
|
|
278
|
+
* @returns Current size.
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
* @example
|
|
310
|
+
* // Get number of elements
|
|
311
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
312
|
+
* console.log(stack.size); // 3;
|
|
313
|
+
*/
|
|
326
314
|
get size() {
|
|
327
315
|
return this.elements.length;
|
|
328
316
|
}
|
|
@@ -340,36 +328,207 @@ var stackTyped = (() => {
|
|
|
340
328
|
return new this(elements, options);
|
|
341
329
|
}
|
|
342
330
|
/**
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
331
|
+
* Check whether the stack is empty.
|
|
332
|
+
* @remarks Time O(1), Space O(1)
|
|
333
|
+
* @returns True if size is 0.
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
* @example
|
|
367
|
+
* // Check if stack has elements
|
|
368
|
+
* const stack = new Stack<number>();
|
|
369
|
+
* console.log(stack.isEmpty()); // true;
|
|
370
|
+
* stack.push(1);
|
|
371
|
+
* console.log(stack.isEmpty()); // false;
|
|
372
|
+
*/
|
|
347
373
|
isEmpty() {
|
|
348
374
|
return this.elements.length === 0;
|
|
349
375
|
}
|
|
350
376
|
/**
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
377
|
+
* Get the top element without removing it.
|
|
378
|
+
* @remarks Time O(1), Space O(1)
|
|
379
|
+
* @returns Top element or undefined.
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
* @example
|
|
413
|
+
* // View the top element without removing it
|
|
414
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
415
|
+
* console.log(stack.peek()); // 'c';
|
|
416
|
+
* console.log(stack.size); // 3;
|
|
417
|
+
*/
|
|
355
418
|
peek() {
|
|
356
419
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
357
420
|
}
|
|
358
421
|
/**
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
422
|
+
* Push one element onto the top.
|
|
423
|
+
* @remarks Time O(1), Space O(1)
|
|
424
|
+
* @param element - Element to push.
|
|
425
|
+
* @returns True when pushed.
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
* @example
|
|
459
|
+
* // basic Stack creation and push operation
|
|
460
|
+
* // Create a simple Stack with initial values
|
|
461
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
462
|
+
*
|
|
463
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
464
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
465
|
+
*
|
|
466
|
+
* // Check length
|
|
467
|
+
* console.log(stack.size); // 5;
|
|
468
|
+
*
|
|
469
|
+
* // Push a new element to the top
|
|
470
|
+
* stack.push(6);
|
|
471
|
+
* console.log(stack.size); // 6;
|
|
472
|
+
*/
|
|
364
473
|
push(element) {
|
|
365
474
|
this.elements.push(element);
|
|
366
475
|
return true;
|
|
367
476
|
}
|
|
368
477
|
/**
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
478
|
+
* Pop and return the top element.
|
|
479
|
+
* @remarks Time O(1), Space O(1)
|
|
480
|
+
* @returns Removed element or undefined.
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
* @example
|
|
514
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
515
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
516
|
+
*
|
|
517
|
+
* // Peek at the top element without removing
|
|
518
|
+
* const top = stack.peek();
|
|
519
|
+
* console.log(top); // 50;
|
|
520
|
+
*
|
|
521
|
+
* // Pop removes from the top (LIFO order)
|
|
522
|
+
* const popped = stack.pop();
|
|
523
|
+
* console.log(popped); // 50;
|
|
524
|
+
*
|
|
525
|
+
* // Next pop gets the previous element
|
|
526
|
+
* const next = stack.pop();
|
|
527
|
+
* console.log(next); // 40;
|
|
528
|
+
*
|
|
529
|
+
* // Verify length decreased
|
|
530
|
+
* console.log(stack.size); // 3;
|
|
531
|
+
*/
|
|
373
532
|
pop() {
|
|
374
533
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
375
534
|
}
|
|
@@ -388,11 +547,45 @@ var stackTyped = (() => {
|
|
|
388
547
|
return ans;
|
|
389
548
|
}
|
|
390
549
|
/**
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
550
|
+
* Delete the first occurrence of a specific element.
|
|
551
|
+
* @remarks Time O(N), Space O(1)
|
|
552
|
+
* @param element - Element to remove (using the configured equality).
|
|
553
|
+
* @returns True if an element was removed.
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
* @example
|
|
584
|
+
* // Remove element
|
|
585
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
586
|
+
* stack.delete(2);
|
|
587
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
588
|
+
*/
|
|
396
589
|
delete(element) {
|
|
397
590
|
const idx = this._indexOfByEquals(element);
|
|
398
591
|
return this.deleteAt(idx);
|
|
@@ -424,30 +617,137 @@ var stackTyped = (() => {
|
|
|
424
617
|
return false;
|
|
425
618
|
}
|
|
426
619
|
/**
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
620
|
+
* Remove all elements and reset storage.
|
|
621
|
+
* @remarks Time O(1), Space O(1)
|
|
622
|
+
* @returns void
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
* @example
|
|
654
|
+
* // Remove all elements
|
|
655
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
656
|
+
* stack.clear();
|
|
657
|
+
* console.log(stack.isEmpty()); // true;
|
|
658
|
+
*/
|
|
431
659
|
clear() {
|
|
432
660
|
this._elements = [];
|
|
433
661
|
}
|
|
434
662
|
/**
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
663
|
+
* Deep clone this stack.
|
|
664
|
+
* @remarks Time O(N), Space O(N)
|
|
665
|
+
* @returns A new stack with the same content.
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
* @example
|
|
697
|
+
* // Create independent copy
|
|
698
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
699
|
+
* const copy = stack.clone();
|
|
700
|
+
* copy.pop();
|
|
701
|
+
* console.log(stack.size); // 3;
|
|
702
|
+
* console.log(copy.size); // 2;
|
|
703
|
+
*/
|
|
439
704
|
clone() {
|
|
440
705
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
441
706
|
for (const v of this) out.push(v);
|
|
442
707
|
return out;
|
|
443
708
|
}
|
|
444
709
|
/**
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
710
|
+
* Filter elements into a new stack of the same class.
|
|
711
|
+
* @remarks Time O(N), Space O(N)
|
|
712
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
713
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
714
|
+
* @returns A new stack with kept values.
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
* @example
|
|
746
|
+
* // Filter elements
|
|
747
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
748
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
749
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
750
|
+
*/
|
|
451
751
|
filter(predicate, thisArg) {
|
|
452
752
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
453
753
|
let index = 0;
|
|
@@ -474,15 +774,49 @@ var stackTyped = (() => {
|
|
|
474
774
|
return out;
|
|
475
775
|
}
|
|
476
776
|
/**
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
777
|
+
* Map values into a new stack (possibly different element type).
|
|
778
|
+
* @remarks Time O(N), Space O(N)
|
|
779
|
+
* @template EM
|
|
780
|
+
* @template RM
|
|
781
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
782
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
783
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
784
|
+
* @returns A new Stack with mapped elements.
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
* @example
|
|
815
|
+
* // Transform elements
|
|
816
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
817
|
+
* const doubled = stack.map(x => x * 2);
|
|
818
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
819
|
+
*/
|
|
486
820
|
map(callback, options, thisArg) {
|
|
487
821
|
const out = this._createLike([], { ...options != null ? options : {} });
|
|
488
822
|
let index = 0;
|
|
@@ -544,6 +878,52 @@ var stackTyped = (() => {
|
|
|
544
878
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
545
879
|
}
|
|
546
880
|
};
|
|
881
|
+
|
|
882
|
+
// src/common/error.ts
|
|
883
|
+
var ERR = {
|
|
884
|
+
// Range / index
|
|
885
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
886
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
887
|
+
// Type / argument
|
|
888
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
889
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
890
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
891
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
892
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
893
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
894
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
895
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
896
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
897
|
+
// State / operation
|
|
898
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
899
|
+
// Matrix
|
|
900
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
901
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
902
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
903
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
904
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
// src/common/index.ts
|
|
908
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
909
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
910
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
911
|
+
return DFSOperation2;
|
|
912
|
+
})(DFSOperation || {});
|
|
913
|
+
var Range = class {
|
|
914
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
915
|
+
this.low = low;
|
|
916
|
+
this.high = high;
|
|
917
|
+
this.includeLow = includeLow;
|
|
918
|
+
this.includeHigh = includeHigh;
|
|
919
|
+
}
|
|
920
|
+
// Determine whether a key is within the range
|
|
921
|
+
isInRange(key, comparator) {
|
|
922
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
923
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
924
|
+
return lowCheck && highCheck;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
547
927
|
return __toCommonJS(src_exports);
|
|
548
928
|
})();
|
|
549
929
|
/**
|