stack-typed 1.48.0 → 1.48.2

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.
Files changed (54) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +45 -40
  6. package/dist/data-structures/binary-tree/binary-tree.js +91 -88
  7. package/dist/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  8. package/dist/data-structures/binary-tree/tree-multimap.js +16 -0
  9. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  10. package/dist/data-structures/graph/abstract-graph.js +50 -27
  11. package/dist/data-structures/hash/hash-map.d.ts +160 -44
  12. package/dist/data-structures/hash/hash-map.js +314 -82
  13. package/dist/data-structures/heap/heap.d.ts +50 -7
  14. package/dist/data-structures/heap/heap.js +60 -30
  15. package/dist/data-structures/index.d.ts +1 -0
  16. package/dist/data-structures/index.js +1 -0
  17. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  18. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  19. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  20. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  21. package/dist/data-structures/queue/deque.d.ts +29 -51
  22. package/dist/data-structures/queue/deque.js +36 -71
  23. package/dist/data-structures/queue/queue.d.ts +49 -48
  24. package/dist/data-structures/queue/queue.js +69 -82
  25. package/dist/data-structures/stack/stack.d.ts +43 -10
  26. package/dist/data-structures/stack/stack.js +50 -31
  27. package/dist/data-structures/trie/trie.d.ts +41 -6
  28. package/dist/data-structures/trie/trie.js +53 -32
  29. package/dist/types/data-structures/base/base.d.ts +5 -0
  30. package/dist/types/data-structures/base/base.js +2 -0
  31. package/dist/types/data-structures/base/index.d.ts +1 -0
  32. package/dist/types/data-structures/base/index.js +17 -0
  33. package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
  34. package/dist/types/data-structures/index.d.ts +1 -0
  35. package/dist/types/data-structures/index.js +1 -0
  36. package/package.json +2 -2
  37. package/src/data-structures/base/index.ts +1 -0
  38. package/src/data-structures/base/iterable-base.ts +329 -0
  39. package/src/data-structures/binary-tree/binary-tree.ts +98 -93
  40. package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
  41. package/src/data-structures/graph/abstract-graph.ts +55 -28
  42. package/src/data-structures/hash/hash-map.ts +334 -83
  43. package/src/data-structures/heap/heap.ts +63 -36
  44. package/src/data-structures/index.ts +1 -0
  45. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  46. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  47. package/src/data-structures/queue/deque.ts +40 -82
  48. package/src/data-structures/queue/queue.ts +72 -87
  49. package/src/data-structures/stack/stack.ts +53 -34
  50. package/src/data-structures/trie/trie.ts +58 -35
  51. package/src/types/data-structures/base/base.ts +6 -0
  52. package/src/types/data-structures/base/index.ts +1 -0
  53. package/src/types/data-structures/hash/hash-map.ts +2 -0
  54. package/src/types/data-structures/index.ts +1 -0
@@ -6,8 +6,9 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types';
9
- import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
9
+ import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout, PairCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ import { IterablePairBase } from "../base";
11
12
  /**
12
13
  * Represents a node in a binary tree.
13
14
  * @template V - The type of data stored in the node.
@@ -41,7 +42,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
41
42
  * 8. Full Trees: Every node has either 0 or 2 children.
42
43
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
43
44
  */
44
- export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> {
45
+ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> extends IterablePairBase<BTNKey, V | undefined> implements IBinaryTree<V, N, TREE> {
45
46
  iterationType: IterationType;
46
47
  /**
47
48
  * The constructor function initializes a binary tree object with optional elements and options.
@@ -462,54 +463,57 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
462
463
  morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNodeKeyOrNode<N>): ReturnType<C>[];
463
464
  /**
464
465
  * Time complexity: O(n)
465
- * Space complexity: O(1)
466
+ * Space complexity: O(n)
466
467
  */
467
468
  /**
468
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
469
- * entry and the tree as arguments.
470
- * @param callback - The callback parameter is a function that will be called for each entry in the
471
- * tree. It takes two parameters: entry and tree.
469
+ * Time complexity: O(n)
470
+ * Space complexity: O(n)
471
+ *
472
+ * The `clone` function creates a new tree object and copies all the nodes from the original tree to
473
+ * the new tree.
474
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
472
475
  */
473
- forEach(callback: (entry: [BTNKey, V | undefined], tree: this) => void): void;
476
+ clone(): TREE;
474
477
  /**
475
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
476
- * adding the entries that satisfy the given predicate.
477
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
478
- * `tree`.
479
- * @returns The `filter` method is returning a new tree object that contains only the entries that
480
- * satisfy the given predicate function.
478
+ * Time Complexity: O(n)
479
+ * Space Complexity: O(n)
481
480
  */
482
- filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean): TREE;
483
481
  /**
484
- * The `map` function creates a new tree by applying a callback function to each entry in the current
485
- * tree.
486
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
487
- * @returns The `map` method is returning a new tree object.
488
- */
489
- map(callback: (entry: [BTNKey, V | undefined], tree: this) => V): TREE;
482
+ * Time Complexity: O(n)
483
+ * Space Complexity: O(n)
484
+ *
485
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
486
+ * adding only the elements that satisfy the given predicate function.
487
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
488
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
489
+ * included in the filtered tree or not.
490
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
491
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
492
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
493
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
494
+ * pass the given predicate function.
495
+ */
496
+ filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any): TREE;
490
497
  /**
491
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
492
- * entry, accumulating a single value.
493
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
494
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
495
- * based on the logic defined in the callback function.
496
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
497
- * is the value that will be passed as the first argument to the callback function when reducing the
498
- * elements of the tree.
499
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
500
- * all the entries in the tree and applying the callback function to each entry.
498
+ * Time Complexity: O(n)
499
+ * Space Complexity: O(n)
501
500
  */
502
- reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T;
503
501
  /**
504
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
505
- * either an iterative or recursive manner.
506
- * @param node - The `node` parameter represents the current node in the binary tree from which the
507
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
508
- * that if no node is provided, the iteration will start from the root of the binary tree.
509
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
510
- * binary tree nodes in a specific order.
502
+ * Time Complexity: O(n)
503
+ * Space Complexity: O(n)
504
+ *
505
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
506
+ * the original tree.
507
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
508
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
509
+ * the index of the current pair, and a reference to the tree itself. The callback function should
510
+ * return a new
511
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
512
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
513
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
514
+ * @returns The `map` method is returning a new tree object.
511
515
  */
512
- [Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>;
516
+ map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any): TREE;
513
517
  /**
514
518
  * The `print` function is used to display a binary tree structure in a visually appealing way.
515
519
  * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
@@ -518,6 +522,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
518
522
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
519
523
  */
520
524
  print(beginRoot?: BTNodeKeyOrNode<N>, options?: BinaryTreePrintOptions): void;
525
+ protected _getIterator(node?: N | null | undefined): IterableIterator<[BTNKey, V | undefined]>;
521
526
  protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
522
527
  protected _defaultOneParamCallback: (node: N) => number;
523
528
  /**
@@ -11,6 +11,7 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
11
  const types_1 = require("../../types");
12
12
  const utils_1 = require("../../utils");
13
13
  const queue_1 = require("../queue");
14
+ const base_1 = require("../base");
14
15
  /**
15
16
  * Represents a node in a binary tree.
16
17
  * @template V - The type of data stored in the node.
@@ -69,7 +70,7 @@ exports.BinaryTreeNode = BinaryTreeNode;
69
70
  * 8. Full Trees: Every node has either 0 or 2 children.
70
71
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
71
72
  */
72
- class BinaryTree {
73
+ class BinaryTree extends base_1.IterablePairBase {
73
74
  /**
74
75
  * The constructor function initializes a binary tree object with optional elements and options.
75
76
  * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
@@ -80,6 +81,7 @@ class BinaryTree {
80
81
  * required.
81
82
  */
82
83
  constructor(elements, options) {
84
+ super();
83
85
  this.iterationType = types_1.IterationType.ITERATIVE;
84
86
  this._defaultOneParamCallback = (node) => node.key;
85
87
  if (options) {
@@ -1406,113 +1408,86 @@ class BinaryTree {
1406
1408
  }
1407
1409
  /**
1408
1410
  * Time complexity: O(n)
1409
- * Space complexity: O(1)
1411
+ * Space complexity: O(n)
1410
1412
  */
1411
1413
  /**
1412
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1413
- * entry and the tree as arguments.
1414
- * @param callback - The callback parameter is a function that will be called for each entry in the
1415
- * tree. It takes two parameters: entry and tree.
1416
- */
1417
- forEach(callback) {
1418
- for (const entry of this) {
1419
- callback(entry, this);
1420
- }
1414
+ * Time complexity: O(n)
1415
+ * Space complexity: O(n)
1416
+ *
1417
+ * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1418
+ * the new tree.
1419
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1420
+ */
1421
+ clone() {
1422
+ const cloned = this.createTree();
1423
+ this.bfs(node => cloned.add([node.key, node.value]));
1424
+ return cloned;
1421
1425
  }
1422
1426
  /**
1423
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
1424
- * adding the entries that satisfy the given predicate.
1425
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
1426
- * `tree`.
1427
- * @returns The `filter` method is returning a new tree object that contains only the entries that
1428
- * satisfy the given predicate function.
1427
+ * Time Complexity: O(n)
1428
+ * Space Complexity: O(n)
1429
1429
  */
1430
- filter(predicate) {
1430
+ /**
1431
+ * Time Complexity: O(n)
1432
+ * Space Complexity: O(n)
1433
+ *
1434
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
1435
+ * adding only the elements that satisfy the given predicate function.
1436
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1437
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1438
+ * included in the filtered tree or not.
1439
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1440
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1441
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1442
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1443
+ * pass the given predicate function.
1444
+ */
1445
+ filter(predicate, thisArg) {
1431
1446
  const newTree = this.createTree();
1447
+ let index = 0;
1432
1448
  for (const [key, value] of this) {
1433
- if (predicate([key, value], this)) {
1449
+ if (predicate.call(thisArg, value, key, index++, this)) {
1434
1450
  newTree.add([key, value]);
1435
1451
  }
1436
1452
  }
1437
1453
  return newTree;
1438
1454
  }
1439
1455
  /**
1440
- * The `map` function creates a new tree by applying a callback function to each entry in the current
1441
- * tree.
1442
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
1456
+ * Time Complexity: O(n)
1457
+ * Space Complexity: O(n)
1458
+ */
1459
+ /**
1460
+ * Time Complexity: O(n)
1461
+ * Space Complexity: O(n)
1462
+ *
1463
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
1464
+ * the original tree.
1465
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
1466
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1467
+ * the index of the current pair, and a reference to the tree itself. The callback function should
1468
+ * return a new
1469
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1470
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1471
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
1443
1472
  * @returns The `map` method is returning a new tree object.
1444
1473
  */
1445
- map(callback) {
1474
+ map(callback, thisArg) {
1446
1475
  const newTree = this.createTree();
1476
+ let index = 0;
1447
1477
  for (const [key, value] of this) {
1448
- newTree.add([key, callback([key, value], this)]);
1478
+ newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1449
1479
  }
1450
1480
  return newTree;
1451
1481
  }
1452
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1453
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1454
- // const newTree = this.createTree();
1455
- // for (const [key, value] of this) {
1456
- // newTree.add(key, callback([key, value], this));
1457
- // }
1458
- // return newTree;
1459
- // }
1460
- /**
1461
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1462
- * entry, accumulating a single value.
1463
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
1464
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
1465
- * based on the logic defined in the callback function.
1466
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
1467
- * is the value that will be passed as the first argument to the callback function when reducing the
1468
- * elements of the tree.
1469
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
1470
- * all the entries in the tree and applying the callback function to each entry.
1471
- */
1472
- reduce(callback, initialValue) {
1473
- let accumulator = initialValue;
1474
- for (const [key, value] of this) {
1475
- accumulator = callback(accumulator, [key, value], this);
1476
- }
1477
- return accumulator;
1478
- }
1479
- /**
1480
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
1481
- * either an iterative or recursive manner.
1482
- * @param node - The `node` parameter represents the current node in the binary tree from which the
1483
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
1484
- * that if no node is provided, the iteration will start from the root of the binary tree.
1485
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1486
- * binary tree nodes in a specific order.
1487
- */
1488
- *[Symbol.iterator](node = this.root) {
1489
- if (!node)
1490
- return;
1491
- if (this.iterationType === types_1.IterationType.ITERATIVE) {
1492
- const stack = [];
1493
- let current = node;
1494
- while (current || stack.length > 0) {
1495
- while (current && !isNaN(current.key)) {
1496
- stack.push(current);
1497
- current = current.left;
1498
- }
1499
- current = stack.pop();
1500
- if (current && !isNaN(current.key)) {
1501
- yield [current.key, current.value];
1502
- current = current.right;
1503
- }
1504
- }
1505
- }
1506
- else {
1507
- if (node.left && !isNaN(node.key)) {
1508
- yield* this[Symbol.iterator](node.left);
1509
- }
1510
- yield [node.key, node.value];
1511
- if (node.right && !isNaN(node.key)) {
1512
- yield* this[Symbol.iterator](node.right);
1513
- }
1514
- }
1515
- }
1482
+ // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1483
+ // // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1484
+ // // const newTree = this.createTree();
1485
+ // // for (const [key, value] of this) {
1486
+ // // newTree.add(key, callback([key, value], this));
1487
+ // // }
1488
+ // // return newTree;
1489
+ // // }
1490
+ //
1516
1491
  /**
1517
1492
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1518
1493
  * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
@@ -1542,6 +1517,34 @@ class BinaryTree {
1542
1517
  };
1543
1518
  display(beginRoot);
1544
1519
  }
1520
+ *_getIterator(node = this.root) {
1521
+ if (!node)
1522
+ return;
1523
+ if (this.iterationType === types_1.IterationType.ITERATIVE) {
1524
+ const stack = [];
1525
+ let current = node;
1526
+ while (current || stack.length > 0) {
1527
+ while (current && !isNaN(current.key)) {
1528
+ stack.push(current);
1529
+ current = current.left;
1530
+ }
1531
+ current = stack.pop();
1532
+ if (current && !isNaN(current.key)) {
1533
+ yield [current.key, current.value];
1534
+ current = current.right;
1535
+ }
1536
+ }
1537
+ }
1538
+ else {
1539
+ if (node.left && !isNaN(node.key)) {
1540
+ yield* this[Symbol.iterator](node.left);
1541
+ }
1542
+ yield [node.key, node.value];
1543
+ if (node.right && !isNaN(node.key)) {
1544
+ yield* this[Symbol.iterator](node.right);
1545
+ }
1546
+ }
1547
+ }
1545
1548
  _displayAux(node, options) {
1546
1549
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1547
1550
  const emptyDisplayLayout = [['─'], 1, 0, 0];
@@ -137,6 +137,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
137
137
  * The clear() function clears the contents of a data structure and sets the count to zero.
138
138
  */
139
139
  clear(): void;
140
+ /**
141
+ * Time complexity: O(n)
142
+ * Space complexity: O(n)
143
+ */
144
+ /**
145
+ * Time complexity: O(n)
146
+ * Space complexity: O(n)
147
+ *
148
+ * The `clone` function creates a deep copy of a tree object.
149
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
150
+ */
151
+ clone(): TREE;
140
152
  /**
141
153
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
142
154
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -280,6 +280,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
280
280
  super.clear();
281
281
  this._count = 0;
282
282
  }
283
+ /**
284
+ * Time complexity: O(n)
285
+ * Space complexity: O(n)
286
+ */
287
+ /**
288
+ * Time complexity: O(n)
289
+ * Space complexity: O(n)
290
+ *
291
+ * The `clone` function creates a deep copy of a tree object.
292
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
293
+ */
294
+ clone() {
295
+ const cloned = this.createTree();
296
+ this.bfs(node => cloned.add([node.key, node.value], node.count));
297
+ return cloned;
298
+ }
283
299
  /**
284
300
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
285
301
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -1,5 +1,7 @@
1
1
  import type { DijkstraResult, VertexKey } from '../../types';
2
+ import { PairCallback } from "../../types";
2
3
  import { IGraph } from '../../interfaces';
4
+ import { IterablePairBase } from "../base";
3
5
  export declare abstract class AbstractVertex<V = any> {
4
6
  key: VertexKey;
5
7
  value: V | undefined;
@@ -28,7 +30,8 @@ export declare abstract class AbstractEdge<E = any> {
28
30
  protected _hashCode: string;
29
31
  get hashCode(): string;
30
32
  }
31
- export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> {
33
+ export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> extends IterablePairBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
34
+ constructor();
32
35
  protected _vertices: Map<VertexKey, VO>;
33
36
  get vertices(): Map<VertexKey, VO>;
34
37
  /**
@@ -443,11 +446,46 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
443
446
  * @returns the bridges found using the Tarjan algorithm.
444
447
  */
445
448
  getBridges(): EO[];
446
- [Symbol.iterator](): Iterator<[VertexKey, V | undefined]>;
447
- forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void;
448
- filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][];
449
- map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[];
450
- reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T;
449
+ /**
450
+ * Time Complexity: O(n)
451
+ * Space Complexity: O(n)
452
+ */
453
+ /**
454
+ * Time Complexity: O(n)
455
+ * Space Complexity: O(n)
456
+ *
457
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
458
+ * pairs that satisfy a given predicate.
459
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
460
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
461
+ * in the filtered array. The callback function should return `true` if the element should be
462
+ * included, and `
463
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
464
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
465
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
466
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
467
+ * that satisfy the given predicate function.
468
+ */
469
+ filter(predicate: PairCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][];
470
+ /**
471
+ * Time Complexity: O(n)
472
+ * Space Complexity: O(n)
473
+ */
474
+ /**
475
+ * Time Complexity: O(n)
476
+ * Space Complexity: O(n)
477
+ *
478
+ * The `map` function iterates over the elements of a collection and applies a callback function to
479
+ * each element, returning an array of the results.
480
+ * @param callback - The callback parameter is a function that will be called for each element in the
481
+ * map. It takes four arguments:
482
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
483
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
484
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
485
+ * @returns The `map` function is returning an array of type `T[]`.
486
+ */
487
+ map<T>(callback: PairCallback<VertexKey, V | undefined, T>, thisArg?: any): T[];
488
+ protected _getIterator(): IterableIterator<[VertexKey, V | undefined]>;
451
489
  protected abstract _addEdgeOnly(edge: EO): boolean;
452
490
  protected _addVertexOnly(newVertex: VO): boolean;
453
491
  protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
@@ -11,6 +11,7 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  const priority_queue_1 = require("../priority-queue");
13
13
  const queue_1 = require("../queue");
14
+ const base_1 = require("../base");
14
15
  class AbstractVertex {
15
16
  /**
16
17
  * The function is a protected constructor that takes an key and an optional value as parameters.
@@ -45,8 +46,9 @@ class AbstractEdge {
45
46
  }
46
47
  }
47
48
  exports.AbstractEdge = AbstractEdge;
48
- class AbstractGraph {
49
+ class AbstractGraph extends base_1.IterablePairBase {
49
50
  constructor() {
51
+ super();
50
52
  this._vertices = new Map();
51
53
  }
52
54
  get vertices() {
@@ -1028,46 +1030,67 @@ class AbstractGraph {
1028
1030
  getBridges() {
1029
1031
  return this.tarjan(false, true, false, false).bridges;
1030
1032
  }
1031
- *[Symbol.iterator]() {
1032
- for (const vertex of this._vertices.values()) {
1033
- yield [vertex.key, vertex.value];
1034
- }
1035
- }
1036
- forEach(callback) {
1037
- let index = 0;
1038
- for (const vertex of this) {
1039
- callback(vertex, index, this._vertices);
1040
- index++;
1041
- }
1042
- }
1043
- filter(predicate) {
1033
+ /**
1034
+ * Time Complexity: O(n)
1035
+ * Space Complexity: O(n)
1036
+ */
1037
+ /**
1038
+ * Time Complexity: O(n)
1039
+ * Space Complexity: O(n)
1040
+ *
1041
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
1042
+ * pairs that satisfy a given predicate.
1043
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
1044
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
1045
+ * in the filtered array. The callback function should return `true` if the element should be
1046
+ * included, and `
1047
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1048
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
1049
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
1050
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
1051
+ * that satisfy the given predicate function.
1052
+ */
1053
+ filter(predicate, thisArg) {
1044
1054
  const filtered = [];
1045
1055
  let index = 0;
1046
- for (const entry of this) {
1047
- if (predicate(entry, index, this._vertices)) {
1048
- filtered.push(entry);
1056
+ for (const [key, value] of this) {
1057
+ if (predicate.call(thisArg, value, key, index, this)) {
1058
+ filtered.push([key, value]);
1049
1059
  }
1050
1060
  index++;
1051
1061
  }
1052
1062
  return filtered;
1053
1063
  }
1054
- map(callback) {
1064
+ /**
1065
+ * Time Complexity: O(n)
1066
+ * Space Complexity: O(n)
1067
+ */
1068
+ /**
1069
+ * Time Complexity: O(n)
1070
+ * Space Complexity: O(n)
1071
+ *
1072
+ * The `map` function iterates over the elements of a collection and applies a callback function to
1073
+ * each element, returning an array of the results.
1074
+ * @param callback - The callback parameter is a function that will be called for each element in the
1075
+ * map. It takes four arguments:
1076
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1077
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
1078
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
1079
+ * @returns The `map` function is returning an array of type `T[]`.
1080
+ */
1081
+ map(callback, thisArg) {
1055
1082
  const mapped = [];
1056
1083
  let index = 0;
1057
- for (const entry of this) {
1058
- mapped.push(callback(entry, index, this._vertices));
1084
+ for (const [key, value] of this) {
1085
+ mapped.push(callback.call(thisArg, value, key, index, this));
1059
1086
  index++;
1060
1087
  }
1061
1088
  return mapped;
1062
1089
  }
1063
- reduce(callback, initialValue) {
1064
- let accumulator = initialValue;
1065
- let index = 0;
1066
- for (const entry of this) {
1067
- accumulator = callback(accumulator, entry, index, this._vertices);
1068
- index++;
1090
+ *_getIterator() {
1091
+ for (const vertex of this._vertices.values()) {
1092
+ yield [vertex.key, vertex.value];
1069
1093
  }
1070
- return accumulator;
1071
1094
  }
1072
1095
  _addVertexOnly(newVertex) {
1073
1096
  if (this.hasVertex(newVertex)) {