stack-typed 1.48.1 → 1.48.3

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 (76) 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/avl-tree.d.ts +16 -16
  6. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +140 -182
  9. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  10. package/dist/data-structures/binary-tree/bst.js +54 -57
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  12. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  14. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  15. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  16. package/dist/data-structures/graph/abstract-graph.js +50 -27
  17. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/data-structures/hash/hash-map.js +69 -173
  19. package/dist/data-structures/heap/heap.d.ts +50 -7
  20. package/dist/data-structures/heap/heap.js +60 -30
  21. package/dist/data-structures/index.d.ts +1 -0
  22. package/dist/data-structures/index.js +1 -0
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  26. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  27. package/dist/data-structures/queue/deque.d.ts +29 -51
  28. package/dist/data-structures/queue/deque.js +36 -71
  29. package/dist/data-structures/queue/queue.d.ts +49 -48
  30. package/dist/data-structures/queue/queue.js +69 -82
  31. package/dist/data-structures/stack/stack.d.ts +43 -10
  32. package/dist/data-structures/stack/stack.js +50 -31
  33. package/dist/data-structures/trie/trie.d.ts +41 -6
  34. package/dist/data-structures/trie/trie.js +53 -32
  35. package/dist/interfaces/binary-tree.d.ts +6 -6
  36. package/dist/types/common.d.ts +11 -8
  37. package/dist/types/common.js +6 -1
  38. package/dist/types/data-structures/base/base.d.ts +5 -0
  39. package/dist/types/data-structures/base/base.js +2 -0
  40. package/dist/types/data-structures/base/index.d.ts +1 -0
  41. package/dist/types/data-structures/base/index.js +17 -0
  42. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/types/data-structures/index.d.ts +1 -0
  48. package/dist/types/data-structures/index.js +1 -0
  49. package/package.json +2 -2
  50. package/src/data-structures/base/index.ts +1 -0
  51. package/src/data-structures/base/iterable-base.ts +329 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  53. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  54. package/src/data-structures/binary-tree/bst.ts +86 -82
  55. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  56. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  57. package/src/data-structures/graph/abstract-graph.ts +55 -28
  58. package/src/data-structures/hash/hash-map.ts +76 -185
  59. package/src/data-structures/heap/heap.ts +63 -36
  60. package/src/data-structures/index.ts +1 -0
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  63. package/src/data-structures/queue/deque.ts +40 -82
  64. package/src/data-structures/queue/queue.ts +72 -87
  65. package/src/data-structures/stack/stack.ts +53 -34
  66. package/src/data-structures/trie/trie.ts +58 -35
  67. package/src/interfaces/binary-tree.ts +5 -6
  68. package/src/types/common.ts +11 -8
  69. package/src/types/data-structures/base/base.ts +6 -0
  70. package/src/types/data-structures/base/index.ts +1 -0
  71. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  73. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  74. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  75. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  76. package/src/types/data-structures/index.ts +1 -0
@@ -5,15 +5,15 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNodeKeyOrNode, BTNKey, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
8
+ import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
9
  import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
12
+ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
13
13
  count: number;
14
14
  /**
15
15
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
16
- * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
16
+ * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
17
17
  * of the binary tree node.
18
18
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
19
19
  * tree node. If no value is provided, it will be `undefined`.
@@ -21,42 +21,42 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
21
21
  * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
22
22
  * parameter when creating a new instance of the `BinaryTreeNode` class.
23
23
  */
24
- constructor(key: BTNKey, value?: V, count?: number);
24
+ constructor(key: K, value?: V, count?: number);
25
25
  }
26
26
  /**
27
27
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
- export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
30
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>);
29
+ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
30
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
31
31
  private _count;
32
32
  get count(): number;
33
33
  /**
34
34
  * The function creates a new BSTNode with the given key, value, and count.
35
- * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
35
+ * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
36
36
  * distinguish one node from another in the tree.
37
37
  * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
38
38
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
39
39
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
40
40
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
41
41
  */
42
- createNode(key: BTNKey, value?: V, count?: number): N;
43
- createTree(options?: TreeMultimapOptions): TREE;
42
+ createNode(key: K, value?: V, count?: number): N;
43
+ createTree(options?: TreeMultimapOptions<K>): TREE;
44
44
  /**
45
45
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
46
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
46
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
47
47
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
48
48
  * class.
49
49
  */
50
- isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
50
+ isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
51
51
  /**
52
52
  * The function `exemplarToNode` converts an exemplar object into a node object.
53
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
53
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
54
54
  * the value type and `N` represents the node type.
55
55
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
56
56
  * times the node should be created. If not provided, it defaults to 1.
57
57
  * @returns a value of type `N` (the generic type parameter) or `undefined`.
58
58
  */
59
- exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
59
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
60
60
  /**
61
61
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
62
62
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -73,7 +73,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
73
73
  * is 1.
74
74
  * @returns either a node (`N`) or `undefined`.
75
75
  */
76
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
76
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
77
77
  /**
78
78
  * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
79
79
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -88,7 +88,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
88
88
  * either keys, nodes, or entries.
89
89
  * @returns The method is returning an array of type `N | undefined`.
90
90
  */
91
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
91
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[];
92
92
  /**
93
93
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
94
94
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -158,22 +158,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
158
158
  * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
159
159
  * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
160
160
  * `undefined` if there is no node to add.
161
- * @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
161
+ * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
162
162
  * which the new node will be added as a child. It can be either a node object (`N`) or a key value
163
- * (`BTNKey`).
163
+ * (`K`).
164
164
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
165
165
  * added, or `undefined` if no node was added.
166
166
  */
167
- protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
167
+ protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined;
168
168
  /**
169
169
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
170
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
171
- * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
172
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
170
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
171
+ * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
172
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
173
173
  * node where the values from the source node will be swapped to.
174
174
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
175
175
  * if either `srcNode` or `destNode` is undefined.
176
176
  */
177
- protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
177
+ protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
178
178
  protected _replaceNode(oldNode: N, newNode: N): N;
179
179
  }
@@ -6,7 +6,7 @@ const avl_tree_1 = require("./avl-tree");
6
6
  class TreeMultimapNode extends avl_tree_1.AVLTreeNode {
7
7
  /**
8
8
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
9
- * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
9
+ * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
10
10
  * of the binary tree node.
11
11
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
12
12
  * tree node. If no value is provided, it will be `undefined`.
@@ -38,7 +38,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
38
38
  }
39
39
  /**
40
40
  * The function creates a new BSTNode with the given key, value, and count.
41
- * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
41
+ * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
42
42
  * distinguish one node from another in the tree.
43
43
  * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
44
44
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
@@ -49,11 +49,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
49
49
  return new TreeMultimapNode(key, value, count);
50
50
  }
51
51
  createTree(options) {
52
- return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
52
+ return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
53
53
  }
54
54
  /**
55
55
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
56
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
57
57
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
58
  * class.
59
59
  */
@@ -62,7 +62,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
62
62
  }
63
63
  /**
64
64
  * The function `exemplarToNode` converts an exemplar object into a node object.
65
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
65
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
66
66
  * the value type and `N` represents the node type.
67
67
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
68
68
  * times the node should be created. If not provided, it defaults to 1.
@@ -85,7 +85,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
85
85
  node = this.createNode(key, value, count);
86
86
  }
87
87
  }
88
- else if (this.isNodeKey(exemplar)) {
88
+ else if (this.isNotNodeInstance(exemplar)) {
89
89
  node = this.createNode(exemplar, undefined, count);
90
90
  }
91
91
  else {
@@ -305,9 +305,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
305
305
  * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
306
306
  * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
307
307
  * `undefined` if there is no node to add.
308
- * @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
308
+ * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
309
309
  * which the new node will be added as a child. It can be either a node object (`N`) or a key value
310
- * (`BTNKey`).
310
+ * (`K`).
311
311
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
312
312
  * added, or `undefined` if no node was added.
313
313
  */
@@ -340,9 +340,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
340
340
  }
341
341
  /**
342
342
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
343
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
344
- * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
345
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
343
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
344
+ * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
345
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
346
346
  * node where the values from the source node will be swapped to.
347
347
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
348
348
  * if either `srcNode` or `destNode` is undefined.
@@ -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)) {
@@ -5,8 +5,9 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
9
- export declare class HashMap<K = any, V = any> {
8
+ import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem, PairCallback } from '../../types';
9
+ import { IterablePairBase } from "../base";
10
+ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
10
11
  protected _store: {
11
12
  [key: string]: HashMapStoreItem<K, V>;
12
13
  };
@@ -67,55 +68,13 @@ export declare class HashMap<K = any, V = any> {
67
68
  */
68
69
  delete(key: K): boolean;
69
70
  /**
70
- * The function returns an iterator that yields key-value pairs from both an object store and an
71
- * object map.
72
- */
73
- [Symbol.iterator](): IterableIterator<[K, V]>;
74
- /**
75
- * The function returns an iterator that yields key-value pairs from the object.
76
- */
77
- entries(): IterableIterator<[K, V]>;
78
- /**
79
- * The function `keys()` returns an iterator that yields all the keys of the object.
71
+ * Time Complexity: O(n)
72
+ * Space Complexity: O(n)
80
73
  */
81
- keys(): IterableIterator<K>;
82
- values(): IterableIterator<V>;
83
- /**
84
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
85
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
86
- * index, and map. It is used to test each element in the map against a condition. If the predicate
87
- * function returns false for any element, the every() method will return false. If the predicate
88
- * function returns true for all
89
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
90
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
91
- * passed as the `this` value to the `predicate` function. If `thisArg` is
92
- * @returns The method is returning a boolean value. It returns true if the predicate function
93
- * returns true for every element in the map, and false otherwise.
94
- */
95
- every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean;
96
- /**
97
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
98
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
99
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
100
- * key-value pair in the `HashMap`.
101
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
102
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
103
- * passed as the `this` value to the `predicate` function. If `thisArg` is
104
- * @returns a boolean value. It returns true if the predicate function returns true for any element
105
- * in the map, and false otherwise.
106
- */
107
- some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean;
108
- /**
109
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
110
- * each element.
111
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
112
- * takes four parameters:
113
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
114
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
115
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
116
- */
117
- forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void;
118
74
  /**
75
+ * Time Complexity: O(n)
76
+ * Space Complexity: O(n)
77
+ *
119
78
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
120
79
  * key-value pair in the original HashMap.
121
80
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -126,8 +85,15 @@ export declare class HashMap<K = any, V = any> {
126
85
  * @returns The `map` method is returning a new `HashMap` object with the transformed values based on
127
86
  * the provided callback function.
128
87
  */
129
- map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U>;
88
+ map<U>(callbackfn: PairCallback<K, V, U>, thisArg?: any): HashMap<K, U>;
89
+ /**
90
+ * Time Complexity: O(n)
91
+ * Space Complexity: O(n)
92
+ */
130
93
  /**
94
+ * Time Complexity: O(n)
95
+ * Space Complexity: O(n)
96
+ *
131
97
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
132
98
  * that satisfy a given predicate function.
133
99
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -140,25 +106,18 @@ export declare class HashMap<K = any, V = any> {
140
106
  * @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
141
107
  * from the original `HashMap` that pass the provided `predicate` function.
142
108
  */
143
- filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V>;
109
+ filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
110
+ print(): void;
144
111
  /**
145
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
146
- * each element, accumulating a single value.
147
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
148
- * takes five parameters:
149
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
150
- * is the value that will be used as the first argument of the callback function when reducing the
151
- * elements of the map.
152
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
153
- * all the elements in the `HashMap`.
112
+ * The function returns an iterator that yields key-value pairs from both an object store and an
113
+ * object map.
154
114
  */
155
- reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U;
156
- print(): void;
115
+ protected _getIterator(): IterableIterator<[K, V]>;
157
116
  protected _hashFn: (key: K) => string;
158
117
  protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
159
118
  protected _getNoObjKey(key: K): string;
160
119
  }
161
- export declare class LinkedHashMap<K = any, V = any> {
120
+ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K, V> {
162
121
  protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
163
122
  protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
164
123
  protected _head: HashMapLinkedNode<K, V | undefined>;
@@ -211,8 +170,6 @@ export declare class LinkedHashMap<K = any, V = any> {
211
170
  set(key: K, value?: V): number;
212
171
  has(key: K): boolean;
213
172
  setMany(entries: Iterable<[K, V]>): void;
214
- keys(): K[];
215
- values(): V[];
216
173
  /**
217
174
  * Time Complexity: O(1)
218
175
  * Space Complexity: O(1)
@@ -278,53 +235,55 @@ export declare class LinkedHashMap<K = any, V = any> {
278
235
  clear(): void;
279
236
  clone(): LinkedHashMap<K, V>;
280
237
  /**
281
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
282
- * Space Complexity: O(1)
283
- *
284
- * The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
285
- * each element.
286
- * @param callback - The callback parameter is a function that will be called for each element in the
287
- * LinkedHashMap. It takes three arguments:
238
+ * Time Complexity: O(n)
239
+ * Space Complexity: O(n)
288
240
  */
289
- forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void): void;
290
241
  /**
291
- * The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
292
- * key-value pairs that satisfy the predicate.
293
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
294
- * `map`.
295
- * @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
296
- * satisfy the given predicate function.
242
+ * Time Complexity: O(n)
243
+ * Space Complexity: O(n)
244
+ *
245
+ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
246
+ * map that satisfy a given predicate function.
247
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
248
+ * `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
249
+ * current element should be included in the filtered map or not.
250
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
251
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
252
+ * specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
253
+ * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
254
+ * `LinkedHashMap` object that satisfy the given predicate function.
297
255
  */
298
- filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V>;
256
+ filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
299
257
  /**
300
- * The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
301
- * by the callback.
302
- * @param callback - The `callback` parameter is a function that takes two arguments: `element` and
303
- * `map`.
304
- * @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
258
+ * Time Complexity: O(n)
259
+ * Space Complexity: O(n)
305
260
  */
306
- map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV>;
307
261
  /**
308
- * The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
309
- * each element, accumulating a single value.
310
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
311
- * element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
312
- * result.
313
- * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
314
- * is the value that will be passed as the first argument to the `callback` function when reducing
315
- * the elements of the map.
316
- * @returns The `reduce` function is returning the final value of the accumulator after iterating
317
- * over all the elements in the LinkedHashMap and applying the callback function to each element.
318
- */
319
- reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A;
262
+ * Time Complexity: O(n)
263
+ * Space Complexity: O(n)
264
+ *
265
+ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
266
+ * each key-value pair in the original map.
267
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
268
+ * in the map. It takes four arguments: the value of the current key-value pair, the key of the
269
+ * current key-value pair, the index of the current key-value pair, and the map itself. The callback
270
+ * function should
271
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
272
+ * specify the value of `this` within the callback function. If provided, the callback function will
273
+ * be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
274
+ * map
275
+ * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
276
+ * function.
277
+ */
278
+ map<NV>(callback: PairCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
279
+ print(): void;
320
280
  /**
321
281
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
322
282
  * Space Complexity: O(1)
323
283
  *
324
284
  * The above function is an iterator that yields key-value pairs from a linked list.
325
285
  */
326
- [Symbol.iterator](): Generator<[K, V], void, unknown>;
327
- print(): void;
286
+ protected _getIterator(): Generator<[K, V], void, unknown>;
328
287
  /**
329
288
  * Time Complexity: O(1)
330
289
  * Space Complexity: O(1)