data-structure-typed 1.48.1 → 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 (114) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +182 -182
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  15. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  16. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/cjs/data-structures/hash/hash-map.js +69 -173
  19. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  20. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  21. package/dist/cjs/data-structures/heap/heap.js +60 -30
  22. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  23. package/dist/cjs/data-structures/index.d.ts +1 -0
  24. package/dist/cjs/data-structures/index.js +1 -0
  25. package/dist/cjs/data-structures/index.js.map +1 -1
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  27. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  30. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  33. package/dist/cjs/data-structures/queue/deque.js +36 -71
  34. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  35. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/cjs/data-structures/queue/queue.js +69 -82
  37. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  38. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  39. package/dist/cjs/data-structures/stack/stack.js +50 -31
  40. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  41. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  42. package/dist/cjs/data-structures/trie/trie.js +53 -32
  43. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/cjs/types/data-structures/base/base.js +3 -0
  46. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  47. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  48. package/dist/cjs/types/data-structures/base/index.js +18 -0
  49. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  50. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  51. package/dist/cjs/types/data-structures/index.js +1 -0
  52. package/dist/cjs/types/data-structures/index.js.map +1 -1
  53. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  54. package/dist/mjs/data-structures/base/index.js +1 -0
  55. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  56. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  57. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  58. package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
  59. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  60. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
  62. package/dist/mjs/data-structures/hash/hash-map.js +69 -173
  63. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  64. package/dist/mjs/data-structures/heap/heap.js +60 -30
  65. package/dist/mjs/data-structures/index.d.ts +1 -0
  66. package/dist/mjs/data-structures/index.js +1 -0
  67. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  68. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  69. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  70. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  71. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  72. package/dist/mjs/data-structures/queue/deque.js +36 -71
  73. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  74. package/dist/mjs/data-structures/queue/queue.js +66 -79
  75. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  76. package/dist/mjs/data-structures/stack/stack.js +50 -31
  77. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  78. package/dist/mjs/data-structures/trie/trie.js +53 -32
  79. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  80. package/dist/mjs/types/data-structures/base/base.js +1 -0
  81. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  82. package/dist/mjs/types/data-structures/base/index.js +1 -0
  83. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  84. package/dist/mjs/types/data-structures/index.js +1 -0
  85. package/dist/umd/data-structure-typed.js +846 -715
  86. package/dist/umd/data-structure-typed.min.js +2 -2
  87. package/dist/umd/data-structure-typed.min.js.map +1 -1
  88. package/package.json +1 -1
  89. package/src/data-structures/base/index.ts +1 -0
  90. package/src/data-structures/base/iterable-base.ts +329 -0
  91. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  92. package/src/data-structures/graph/abstract-graph.ts +55 -28
  93. package/src/data-structures/hash/hash-map.ts +76 -185
  94. package/src/data-structures/heap/heap.ts +63 -36
  95. package/src/data-structures/index.ts +1 -0
  96. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  97. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  98. package/src/data-structures/queue/deque.ts +40 -82
  99. package/src/data-structures/queue/queue.ts +72 -87
  100. package/src/data-structures/stack/stack.ts +53 -34
  101. package/src/data-structures/trie/trie.ts +58 -35
  102. package/src/types/data-structures/base/base.ts +6 -0
  103. package/src/types/data-structures/base/index.ts +1 -0
  104. package/src/types/data-structures/index.ts +1 -0
  105. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
  107. package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
  108. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  109. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
  110. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  111. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  112. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  113. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  114. package/test/unit/data-structures/queue/deque.test.ts +25 -0
@@ -8,6 +8,7 @@
8
8
  import { FamilyPosition, IterationType } from '../../types';
9
9
  import { trampoline } from '../../utils';
10
10
  import { Queue } from '../queue';
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.
@@ -70,7 +71,7 @@ export class BinaryTreeNode {
70
71
  * 8. Full Trees: Every node has either 0 or 2 children.
71
72
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
72
73
  */
73
- export class BinaryTree {
74
+ export class BinaryTree extends IterablePairBase {
74
75
  iterationType = IterationType.ITERATIVE;
75
76
  /**
76
77
  * The constructor function initializes a binary tree object with optional elements and options.
@@ -82,6 +83,7 @@ export class BinaryTree {
82
83
  * required.
83
84
  */
84
85
  constructor(elements, options) {
86
+ super();
85
87
  if (options) {
86
88
  const { iterationType } = options;
87
89
  if (iterationType) {
@@ -1403,43 +1405,6 @@ export class BinaryTree {
1403
1405
  }
1404
1406
  return ans;
1405
1407
  }
1406
- /**
1407
- * Time complexity: O(n)
1408
- * Space complexity: O(n)
1409
- */
1410
- /**
1411
- * Time complexity: O(n)
1412
- * Space complexity: O(n)
1413
- *
1414
- * The function "keys" returns an array of keys from a given object.
1415
- * @returns an array of BTNKey objects.
1416
- */
1417
- keys() {
1418
- const keys = [];
1419
- for (const entry of this) {
1420
- keys.push(entry[0]);
1421
- }
1422
- return keys;
1423
- }
1424
- /**
1425
- * Time complexity: O(n)
1426
- * Space complexity: O(n)
1427
- */
1428
- /**
1429
- * Time complexity: O(n)
1430
- * Space complexity: O(n)
1431
- *
1432
- * The function "values" returns an array of values from a map-like object.
1433
- * @returns The `values()` method is returning an array of values (`V`) from the entries in the
1434
- * object.
1435
- */
1436
- values() {
1437
- const values = [];
1438
- for (const entry of this) {
1439
- values.push(entry[1]);
1440
- }
1441
- return values;
1442
- }
1443
1408
  /**
1444
1409
  * Time complexity: O(n)
1445
1410
  * Space complexity: O(n)
@@ -1458,114 +1423,70 @@ export class BinaryTree {
1458
1423
  return cloned;
1459
1424
  }
1460
1425
  /**
1461
- * Time complexity: O(n)
1462
- * Space complexity: O(1)
1463
- */
1464
- /**
1465
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1466
- * entry and the tree as arguments.
1467
- * @param callback - The callback parameter is a function that will be called for each entry in the
1468
- * tree. It takes two parameters: entry and tree.
1426
+ * Time Complexity: O(n)
1427
+ * Space Complexity: O(n)
1469
1428
  */
1470
- forEach(callback) {
1471
- for (const entry of this) {
1472
- callback(entry, this);
1473
- }
1474
- }
1475
1429
  /**
1476
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
1477
- * adding the entries that satisfy the given predicate.
1478
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
1479
- * `tree`.
1480
- * @returns The `filter` method is returning a new tree object that contains only the entries that
1481
- * satisfy the given predicate function.
1482
- */
1483
- filter(predicate) {
1430
+ * Time Complexity: O(n)
1431
+ * Space Complexity: O(n)
1432
+ *
1433
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
1434
+ * adding only the elements that satisfy the given predicate function.
1435
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1436
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1437
+ * included in the filtered tree or not.
1438
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1439
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1440
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1441
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1442
+ * pass the given predicate function.
1443
+ */
1444
+ filter(predicate, thisArg) {
1484
1445
  const newTree = this.createTree();
1446
+ let index = 0;
1485
1447
  for (const [key, value] of this) {
1486
- if (predicate([key, value], this)) {
1448
+ if (predicate.call(thisArg, value, key, index++, this)) {
1487
1449
  newTree.add([key, value]);
1488
1450
  }
1489
1451
  }
1490
1452
  return newTree;
1491
1453
  }
1492
1454
  /**
1493
- * The `map` function creates a new tree by applying a callback function to each entry in the current
1494
- * tree.
1495
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
1455
+ * Time Complexity: O(n)
1456
+ * Space Complexity: O(n)
1457
+ */
1458
+ /**
1459
+ * Time Complexity: O(n)
1460
+ * Space Complexity: O(n)
1461
+ *
1462
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
1463
+ * the original tree.
1464
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
1465
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1466
+ * the index of the current pair, and a reference to the tree itself. The callback function should
1467
+ * return a new
1468
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1469
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1470
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
1496
1471
  * @returns The `map` method is returning a new tree object.
1497
1472
  */
1498
- map(callback) {
1473
+ map(callback, thisArg) {
1499
1474
  const newTree = this.createTree();
1475
+ let index = 0;
1500
1476
  for (const [key, value] of this) {
1501
- newTree.add([key, callback([key, value], this)]);
1477
+ newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1502
1478
  }
1503
1479
  return newTree;
1504
1480
  }
1505
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1506
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1507
- // const newTree = this.createTree();
1508
- // for (const [key, value] of this) {
1509
- // newTree.add(key, callback([key, value], this));
1510
- // }
1511
- // return newTree;
1512
- // }
1513
- /**
1514
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1515
- * entry, accumulating a single value.
1516
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
1517
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
1518
- * based on the logic defined in the callback function.
1519
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
1520
- * is the value that will be passed as the first argument to the callback function when reducing the
1521
- * elements of the tree.
1522
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
1523
- * all the entries in the tree and applying the callback function to each entry.
1524
- */
1525
- reduce(callback, initialValue) {
1526
- let accumulator = initialValue;
1527
- for (const [key, value] of this) {
1528
- accumulator = callback(accumulator, [key, value], this);
1529
- }
1530
- return accumulator;
1531
- }
1532
- /**
1533
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
1534
- * either an iterative or recursive manner.
1535
- * @param node - The `node` parameter represents the current node in the binary tree from which the
1536
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
1537
- * that if no node is provided, the iteration will start from the root of the binary tree.
1538
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1539
- * binary tree nodes in a specific order.
1540
- */
1541
- *[Symbol.iterator](node = this.root) {
1542
- if (!node)
1543
- return;
1544
- if (this.iterationType === IterationType.ITERATIVE) {
1545
- const stack = [];
1546
- let current = node;
1547
- while (current || stack.length > 0) {
1548
- while (current && !isNaN(current.key)) {
1549
- stack.push(current);
1550
- current = current.left;
1551
- }
1552
- current = stack.pop();
1553
- if (current && !isNaN(current.key)) {
1554
- yield [current.key, current.value];
1555
- current = current.right;
1556
- }
1557
- }
1558
- }
1559
- else {
1560
- if (node.left && !isNaN(node.key)) {
1561
- yield* this[Symbol.iterator](node.left);
1562
- }
1563
- yield [node.key, node.value];
1564
- if (node.right && !isNaN(node.key)) {
1565
- yield* this[Symbol.iterator](node.right);
1566
- }
1567
- }
1568
- }
1481
+ // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1482
+ // // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1483
+ // // const newTree = this.createTree();
1484
+ // // for (const [key, value] of this) {
1485
+ // // newTree.add(key, callback([key, value], this));
1486
+ // // }
1487
+ // // return newTree;
1488
+ // // }
1489
+ //
1569
1490
  /**
1570
1491
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1571
1492
  * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
@@ -1595,6 +1516,34 @@ export class BinaryTree {
1595
1516
  };
1596
1517
  display(beginRoot);
1597
1518
  }
1519
+ *_getIterator(node = this.root) {
1520
+ if (!node)
1521
+ return;
1522
+ if (this.iterationType === IterationType.ITERATIVE) {
1523
+ const stack = [];
1524
+ let current = node;
1525
+ while (current || stack.length > 0) {
1526
+ while (current && !isNaN(current.key)) {
1527
+ stack.push(current);
1528
+ current = current.left;
1529
+ }
1530
+ current = stack.pop();
1531
+ if (current && !isNaN(current.key)) {
1532
+ yield [current.key, current.value];
1533
+ current = current.right;
1534
+ }
1535
+ }
1536
+ }
1537
+ else {
1538
+ if (node.left && !isNaN(node.key)) {
1539
+ yield* this[Symbol.iterator](node.left);
1540
+ }
1541
+ yield [node.key, node.value];
1542
+ if (node.right && !isNaN(node.key)) {
1543
+ yield* this[Symbol.iterator](node.right);
1544
+ }
1545
+ }
1546
+ }
1598
1547
  _displayAux(node, options) {
1599
1548
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1600
1549
  const emptyDisplayLayout = [['─'], 1, 0, 0];
@@ -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;
@@ -8,6 +8,7 @@
8
8
  import { uuidV4 } from '../../utils';
9
9
  import { PriorityQueue } from '../priority-queue';
10
10
  import { Queue } from '../queue';
11
+ import { IterablePairBase } from "../base";
11
12
  export class AbstractVertex {
12
13
  key;
13
14
  value;
@@ -45,7 +46,10 @@ export class AbstractEdge {
45
46
  return this._hashCode;
46
47
  }
47
48
  }
48
- export class AbstractGraph {
49
+ export class AbstractGraph extends IterablePairBase {
50
+ constructor() {
51
+ super();
52
+ }
49
53
  _vertices = new Map();
50
54
  get vertices() {
51
55
  return this._vertices;
@@ -1021,46 +1025,67 @@ export class AbstractGraph {
1021
1025
  getBridges() {
1022
1026
  return this.tarjan(false, true, false, false).bridges;
1023
1027
  }
1024
- *[Symbol.iterator]() {
1025
- for (const vertex of this._vertices.values()) {
1026
- yield [vertex.key, vertex.value];
1027
- }
1028
- }
1029
- forEach(callback) {
1030
- let index = 0;
1031
- for (const vertex of this) {
1032
- callback(vertex, index, this._vertices);
1033
- index++;
1034
- }
1035
- }
1036
- filter(predicate) {
1028
+ /**
1029
+ * Time Complexity: O(n)
1030
+ * Space Complexity: O(n)
1031
+ */
1032
+ /**
1033
+ * Time Complexity: O(n)
1034
+ * Space Complexity: O(n)
1035
+ *
1036
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
1037
+ * pairs that satisfy a given predicate.
1038
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
1039
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
1040
+ * in the filtered array. The callback function should return `true` if the element should be
1041
+ * included, and `
1042
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1043
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
1044
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
1045
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
1046
+ * that satisfy the given predicate function.
1047
+ */
1048
+ filter(predicate, thisArg) {
1037
1049
  const filtered = [];
1038
1050
  let index = 0;
1039
- for (const entry of this) {
1040
- if (predicate(entry, index, this._vertices)) {
1041
- filtered.push(entry);
1051
+ for (const [key, value] of this) {
1052
+ if (predicate.call(thisArg, value, key, index, this)) {
1053
+ filtered.push([key, value]);
1042
1054
  }
1043
1055
  index++;
1044
1056
  }
1045
1057
  return filtered;
1046
1058
  }
1047
- map(callback) {
1059
+ /**
1060
+ * Time Complexity: O(n)
1061
+ * Space Complexity: O(n)
1062
+ */
1063
+ /**
1064
+ * Time Complexity: O(n)
1065
+ * Space Complexity: O(n)
1066
+ *
1067
+ * The `map` function iterates over the elements of a collection and applies a callback function to
1068
+ * each element, returning an array of the results.
1069
+ * @param callback - The callback parameter is a function that will be called for each element in the
1070
+ * map. It takes four arguments:
1071
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1072
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
1073
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
1074
+ * @returns The `map` function is returning an array of type `T[]`.
1075
+ */
1076
+ map(callback, thisArg) {
1048
1077
  const mapped = [];
1049
1078
  let index = 0;
1050
- for (const entry of this) {
1051
- mapped.push(callback(entry, index, this._vertices));
1079
+ for (const [key, value] of this) {
1080
+ mapped.push(callback.call(thisArg, value, key, index, this));
1052
1081
  index++;
1053
1082
  }
1054
1083
  return mapped;
1055
1084
  }
1056
- reduce(callback, initialValue) {
1057
- let accumulator = initialValue;
1058
- let index = 0;
1059
- for (const entry of this) {
1060
- accumulator = callback(accumulator, entry, index, this._vertices);
1061
- index++;
1085
+ *_getIterator() {
1086
+ for (const vertex of this._vertices.values()) {
1087
+ yield [vertex.key, vertex.value];
1062
1088
  }
1063
- return accumulator;
1064
1089
  }
1065
1090
  _addVertexOnly(newVertex) {
1066
1091
  if (this.hasVertex(newVertex)) {