linked-list-typed 1.49.8 → 1.49.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/data-structures/hash/hash-map.js +36 -15
- package/dist/types/data-structures/hash/hash-map.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -7
- package/src/data-structures/binary-tree/binary-tree.ts +8 -7
- package/src/data-structures/binary-tree/bst.ts +7 -6
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multimap.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +45 -20
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/hash/hash-map.ts +2 -1
|
@@ -13,20 +13,22 @@ import { IterableEntryBase } from '../base';
|
|
|
13
13
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
14
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
15
15
|
*/
|
|
16
|
-
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
16
|
+
export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
17
17
|
protected _store: {
|
|
18
18
|
[key: string]: HashMapStoreItem<K, V>;
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
|
+
protected _toEntryFn: (rawElement: R) => [K, V];
|
|
22
|
+
get toEntryFn(): (rawElement: R) => [K, V];
|
|
23
|
+
isEntry(rawElement: any): rawElement is [K, V];
|
|
21
24
|
/**
|
|
22
|
-
* The constructor function initializes a
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
27
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
25
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
26
|
+
* options.
|
|
27
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
28
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
29
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
28
30
|
*/
|
|
29
|
-
constructor(
|
|
31
|
+
constructor(rawCollection?: Iterable<R>, options?: HashMapOptions<K, V, R>);
|
|
30
32
|
protected _size: number;
|
|
31
33
|
get size(): number;
|
|
32
34
|
isEmpty(): boolean;
|
|
@@ -42,11 +44,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
42
44
|
*/
|
|
43
45
|
set(key: K, value: V): boolean;
|
|
44
46
|
/**
|
|
45
|
-
* The function
|
|
46
|
-
*
|
|
47
|
-
*
|
|
47
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
48
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
49
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
50
|
+
* `T`.
|
|
51
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
48
52
|
*/
|
|
49
|
-
setMany(
|
|
53
|
+
setMany(rawCollection: Iterable<R>): boolean[];
|
|
50
54
|
/**
|
|
51
55
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
52
56
|
* a string map.
|
|
@@ -10,28 +10,45 @@ const utils_1 = require("../../utils");
|
|
|
10
10
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
11
11
|
*/
|
|
12
12
|
class HashMap extends base_1.IterableEntryBase {
|
|
13
|
+
get toEntryFn() {
|
|
14
|
+
return this._toEntryFn;
|
|
15
|
+
}
|
|
16
|
+
isEntry(rawElement) {
|
|
17
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
18
|
+
}
|
|
13
19
|
/**
|
|
14
|
-
* The constructor function initializes a
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
19
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
20
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
21
|
+
* options.
|
|
22
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
23
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
24
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
20
25
|
*/
|
|
21
|
-
constructor(
|
|
26
|
+
constructor(rawCollection = [], options) {
|
|
22
27
|
super();
|
|
23
28
|
this._store = {};
|
|
24
29
|
this._objMap = new Map();
|
|
30
|
+
this._toEntryFn = (rawElement) => {
|
|
31
|
+
if (this.isEntry(rawElement)) {
|
|
32
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
33
|
+
return rawElement;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
throw new Error("If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified.");
|
|
37
|
+
}
|
|
38
|
+
};
|
|
25
39
|
this._size = 0;
|
|
26
40
|
this._hashFn = (key) => String(key);
|
|
27
41
|
if (options) {
|
|
28
|
-
const { hashFn } = options;
|
|
42
|
+
const { hashFn, toEntryFn } = options;
|
|
29
43
|
if (hashFn) {
|
|
30
44
|
this._hashFn = hashFn;
|
|
31
45
|
}
|
|
46
|
+
if (toEntryFn) {
|
|
47
|
+
this._toEntryFn = toEntryFn;
|
|
48
|
+
}
|
|
32
49
|
}
|
|
33
|
-
if (
|
|
34
|
-
this.setMany(
|
|
50
|
+
if (rawCollection) {
|
|
51
|
+
this.setMany(rawCollection);
|
|
35
52
|
}
|
|
36
53
|
}
|
|
37
54
|
get size() {
|
|
@@ -71,14 +88,18 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
71
88
|
return true;
|
|
72
89
|
}
|
|
73
90
|
/**
|
|
74
|
-
* The function
|
|
75
|
-
*
|
|
76
|
-
*
|
|
91
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
92
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
93
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
94
|
+
* `T`.
|
|
95
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
77
96
|
*/
|
|
78
|
-
setMany(
|
|
97
|
+
setMany(rawCollection) {
|
|
79
98
|
const results = [];
|
|
80
|
-
for (const
|
|
99
|
+
for (const rawEle of rawCollection) {
|
|
100
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
81
101
|
results.push(this.set(key, value));
|
|
102
|
+
}
|
|
82
103
|
return results;
|
|
83
104
|
}
|
|
84
105
|
/**
|
|
@@ -8,8 +8,9 @@ export type LinkedHashMapOptions<K> = {
|
|
|
8
8
|
hashFn?: (key: K) => string;
|
|
9
9
|
objHashFn?: (key: K) => object;
|
|
10
10
|
};
|
|
11
|
-
export type HashMapOptions<K> = {
|
|
11
|
+
export type HashMapOptions<K, V, T> = {
|
|
12
12
|
hashFn?: (key: K) => string;
|
|
13
|
+
toEntryFn?: (rawElement: T) => [K, V];
|
|
13
14
|
};
|
|
14
15
|
export type HashMapStoreItem<K, V> = {
|
|
15
16
|
key: K;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.9",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.49.
|
|
69
|
+
"data-structure-typed": "^1.49.9"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -15,7 +15,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
15
15
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
16
16
|
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
17
17
|
*/
|
|
18
|
-
*
|
|
18
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
19
19
|
yield* this._getIterator(...args);
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -30,7 +30,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
30
30
|
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
31
31
|
* be undefined.
|
|
32
32
|
*/
|
|
33
|
-
*
|
|
33
|
+
*entries(): IterableIterator<[K, V | undefined]> {
|
|
34
34
|
for (const item of this) {
|
|
35
35
|
yield item;
|
|
36
36
|
}
|
|
@@ -46,7 +46,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
46
46
|
*
|
|
47
47
|
* The function returns an iterator that yields the keys of a data structure.
|
|
48
48
|
*/
|
|
49
|
-
*
|
|
49
|
+
*keys(): IterableIterator<K> {
|
|
50
50
|
for (const item of this) {
|
|
51
51
|
yield item[0];
|
|
52
52
|
}
|
|
@@ -62,7 +62,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
62
62
|
*
|
|
63
63
|
* The function returns an iterator that yields the values of a collection.
|
|
64
64
|
*/
|
|
65
|
-
*
|
|
65
|
+
*values(): IterableIterator<V> {
|
|
66
66
|
for (const item of this) {
|
|
67
67
|
yield item[1];
|
|
68
68
|
}
|
|
@@ -212,7 +212,7 @@ export abstract class IterableElementBase<V> {
|
|
|
212
212
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
213
213
|
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
214
214
|
*/
|
|
215
|
-
*
|
|
215
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<V> {
|
|
216
216
|
yield* this._getIterator(...args);
|
|
217
217
|
}
|
|
218
218
|
|
|
@@ -226,7 +226,7 @@ export abstract class IterableElementBase<V> {
|
|
|
226
226
|
*
|
|
227
227
|
* The function returns an iterator that yields all the values in the object.
|
|
228
228
|
*/
|
|
229
|
-
*
|
|
229
|
+
*values(): IterableIterator<V> {
|
|
230
230
|
for (const item of this) {
|
|
231
231
|
yield item;
|
|
232
232
|
}
|
|
@@ -40,13 +40,14 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
|
|
|
40
40
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
41
41
|
*/
|
|
42
42
|
export class AVLTree<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
>
|
|
43
|
+
K = any,
|
|
44
|
+
V = any,
|
|
45
|
+
N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
46
|
+
TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>
|
|
47
|
+
>
|
|
48
48
|
extends BST<K, V, N, TREE>
|
|
49
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
49
|
+
implements IBinaryTree<K, V, N, TREE>
|
|
50
|
+
{
|
|
50
51
|
/**
|
|
51
52
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
52
53
|
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
@@ -277,7 +278,7 @@ export class AVLTree<
|
|
|
277
278
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
278
279
|
switch (
|
|
279
280
|
this._balanceFactor(A) // second O(1)
|
|
280
|
-
|
|
281
|
+
) {
|
|
281
282
|
case -2:
|
|
282
283
|
if (A && A.left) {
|
|
283
284
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -101,13 +101,14 @@ export class BinaryTreeNode<
|
|
|
101
101
|
*/
|
|
102
102
|
|
|
103
103
|
export class BinaryTree<
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
>
|
|
104
|
+
K = any,
|
|
105
|
+
V = any,
|
|
106
|
+
N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
107
|
+
TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>
|
|
108
|
+
>
|
|
109
109
|
extends IterableEntryBase<K, V | undefined>
|
|
110
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
110
|
+
implements IBinaryTree<K, V, N, TREE>
|
|
111
|
+
{
|
|
111
112
|
iterationType = IterationType.ITERATIVE;
|
|
112
113
|
|
|
113
114
|
/**
|
|
@@ -1922,7 +1923,7 @@ export class BinaryTree<
|
|
|
1922
1923
|
display(beginRoot);
|
|
1923
1924
|
}
|
|
1924
1925
|
|
|
1925
|
-
protected*
|
|
1926
|
+
protected *_getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1926
1927
|
if (!node) return;
|
|
1927
1928
|
|
|
1928
1929
|
if (this.iterationType === IterationType.ITERATIVE) {
|
|
@@ -83,13 +83,14 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
|
|
|
83
83
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
84
84
|
*/
|
|
85
85
|
export class BST<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
>
|
|
86
|
+
K = any,
|
|
87
|
+
V = any,
|
|
88
|
+
N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
89
|
+
TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>
|
|
90
|
+
>
|
|
91
91
|
extends BinaryTree<K, V, N, TREE>
|
|
92
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
92
|
+
implements IBinaryTree<K, V, N, TREE>
|
|
93
|
+
{
|
|
93
94
|
/**
|
|
94
95
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
95
96
|
* the tree with optional keysOrNodesOrEntries and options.
|
|
@@ -41,13 +41,14 @@ export class RedBlackTreeNode<
|
|
|
41
41
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
42
42
|
*/
|
|
43
43
|
export class RedBlackTree<
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
>
|
|
44
|
+
K = any,
|
|
45
|
+
V = any,
|
|
46
|
+
N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
47
|
+
TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>
|
|
48
|
+
>
|
|
49
49
|
extends BST<K, V, N, TREE>
|
|
50
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
50
|
+
implements IBinaryTree<K, V, N, TREE>
|
|
51
|
+
{
|
|
51
52
|
Sentinel: N = new RedBlackTreeNode<K, V>(NaN as K) as unknown as N;
|
|
52
53
|
|
|
53
54
|
/**
|
|
@@ -45,13 +45,14 @@ export class TreeMultimapNode<
|
|
|
45
45
|
* 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.
|
|
46
46
|
*/
|
|
47
47
|
export class TreeMultimap<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
>
|
|
48
|
+
K = any,
|
|
49
|
+
V = any,
|
|
50
|
+
N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
|
|
51
|
+
TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>
|
|
52
|
+
>
|
|
53
53
|
extends AVLTree<K, V, N, TREE>
|
|
54
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
54
|
+
implements IBinaryTree<K, V, N, TREE>
|
|
55
|
+
{
|
|
55
56
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) {
|
|
56
57
|
super([], options);
|
|
57
58
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
@@ -61,13 +61,14 @@ export abstract class AbstractEdge<E = any> {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export abstract class AbstractGraph<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
>
|
|
64
|
+
V = any,
|
|
65
|
+
E = any,
|
|
66
|
+
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
|
+
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
+
>
|
|
69
69
|
extends IterableEntryBase<VertexKey, V | undefined>
|
|
70
|
-
implements IGraph<V, E, VO, EO>
|
|
70
|
+
implements IGraph<V, E, VO, EO>
|
|
71
|
+
{
|
|
71
72
|
constructor() {
|
|
72
73
|
super();
|
|
73
74
|
}
|
|
@@ -610,14 +611,14 @@ export abstract class AbstractGraph<
|
|
|
610
611
|
}
|
|
611
612
|
|
|
612
613
|
getMinDist &&
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
614
|
+
distMap.forEach((d, v) => {
|
|
615
|
+
if (v !== srcVertex) {
|
|
616
|
+
if (d < minDist) {
|
|
617
|
+
minDist = d;
|
|
618
|
+
if (genPaths) minDest = v;
|
|
619
|
+
}
|
|
618
620
|
}
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
+
});
|
|
621
622
|
|
|
622
623
|
genPaths && getPaths(minDest);
|
|
623
624
|
|
|
@@ -1272,7 +1273,7 @@ export abstract class AbstractGraph<
|
|
|
1272
1273
|
return mapped;
|
|
1273
1274
|
}
|
|
1274
1275
|
|
|
1275
|
-
protected*
|
|
1276
|
+
protected *_getIterator(): IterableIterator<[VertexKey, V | undefined]> {
|
|
1276
1277
|
for (const vertex of this._vertexMap.values()) {
|
|
1277
1278
|
yield [vertex.key, vertex.value];
|
|
1278
1279
|
}
|
|
@@ -46,13 +46,14 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export class DirectedGraph<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
>
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
54
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
55
|
+
implements IGraph<V, E, VO, EO>
|
|
56
|
+
{
|
|
56
57
|
/**
|
|
57
58
|
* The constructor function initializes an instance of a class.
|
|
58
59
|
*/
|
|
@@ -43,13 +43,14 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class UndirectedGraph<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
>
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
-
implements IGraph<V, E, VO, EO>
|
|
52
|
+
implements IGraph<V, E, VO, EO>
|
|
53
|
+
{
|
|
53
54
|
/**
|
|
54
55
|
* The constructor initializes a new Map object to store edgeMap.
|
|
55
56
|
*/
|
|
@@ -21,28 +21,48 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
21
21
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
22
22
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
23
23
|
*/
|
|
24
|
-
export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
24
|
+
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
25
25
|
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
|
|
26
26
|
protected _objMap: Map<object, V> = new Map();
|
|
27
|
+
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
|
|
28
|
+
if (this.isEntry(rawElement)) {
|
|
29
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
30
|
+
return rawElement;
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(
|
|
33
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
get toEntryFn() {
|
|
39
|
+
return this._toEntryFn;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
isEntry(rawElement: any): rawElement is [K, V] {
|
|
43
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
44
|
+
}
|
|
27
45
|
|
|
28
46
|
/**
|
|
29
|
-
* The constructor function initializes a
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
34
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
47
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
48
|
+
* options.
|
|
49
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
50
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
51
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
35
52
|
*/
|
|
36
|
-
constructor(
|
|
53
|
+
constructor(rawCollection: Iterable<R> = [], options?: HashMapOptions<K, V, R>) {
|
|
37
54
|
super();
|
|
38
55
|
if (options) {
|
|
39
|
-
const { hashFn } = options;
|
|
56
|
+
const { hashFn, toEntryFn } = options;
|
|
40
57
|
if (hashFn) {
|
|
41
58
|
this._hashFn = hashFn;
|
|
42
59
|
}
|
|
60
|
+
if (toEntryFn) {
|
|
61
|
+
this._toEntryFn = toEntryFn;
|
|
62
|
+
}
|
|
43
63
|
}
|
|
44
|
-
if (
|
|
45
|
-
this.setMany(
|
|
64
|
+
if (rawCollection) {
|
|
65
|
+
this.setMany(rawCollection);
|
|
46
66
|
}
|
|
47
67
|
}
|
|
48
68
|
|
|
@@ -88,13 +108,18 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
88
108
|
}
|
|
89
109
|
|
|
90
110
|
/**
|
|
91
|
-
* The function
|
|
92
|
-
*
|
|
93
|
-
*
|
|
111
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
112
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
113
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
114
|
+
* `T`.
|
|
115
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
94
116
|
*/
|
|
95
|
-
setMany(
|
|
117
|
+
setMany(rawCollection: Iterable<R>): boolean[] {
|
|
96
118
|
const results: boolean[] = [];
|
|
97
|
-
for (const
|
|
119
|
+
for (const rawEle of rawCollection) {
|
|
120
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
121
|
+
results.push(this.set(key, value));
|
|
122
|
+
}
|
|
98
123
|
return results;
|
|
99
124
|
}
|
|
100
125
|
|
|
@@ -223,7 +248,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
223
248
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
224
249
|
* object map.
|
|
225
250
|
*/
|
|
226
|
-
protected*
|
|
251
|
+
protected *_getIterator(): IterableIterator<[K, V]> {
|
|
227
252
|
for (const node of Object.values(this._store)) {
|
|
228
253
|
yield [node.key, node.value] as [K, V];
|
|
229
254
|
}
|
|
@@ -322,7 +347,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
322
347
|
/**
|
|
323
348
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
324
349
|
*/
|
|
325
|
-
*
|
|
350
|
+
*begin() {
|
|
326
351
|
let node = this._head;
|
|
327
352
|
while (node !== this._sentinel) {
|
|
328
353
|
yield [node.key, node.value];
|
|
@@ -334,7 +359,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
334
359
|
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
335
360
|
* key and value.
|
|
336
361
|
*/
|
|
337
|
-
*
|
|
362
|
+
*reverseBegin() {
|
|
338
363
|
let node = this._tail;
|
|
339
364
|
while (node !== this._sentinel) {
|
|
340
365
|
yield [node.key, node.value];
|
|
@@ -635,7 +660,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
635
660
|
*
|
|
636
661
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
637
662
|
*/
|
|
638
|
-
protected*
|
|
663
|
+
protected *_getIterator() {
|
|
639
664
|
let node = this._head;
|
|
640
665
|
while (node !== this._sentinel) {
|
|
641
666
|
yield [node.key, node.value] as [K, V];
|
|
@@ -391,7 +391,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
391
391
|
return mappedHeap;
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
protected*
|
|
394
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
395
395
|
for (const element of this.elements) {
|
|
396
396
|
yield element;
|
|
397
397
|
}
|
|
@@ -808,7 +808,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
808
808
|
/**
|
|
809
809
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
810
810
|
*/
|
|
811
|
-
protected*
|
|
811
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
812
812
|
let current = this.head;
|
|
813
813
|
|
|
814
814
|
while (current) {
|
|
@@ -741,7 +741,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
741
741
|
return mappedList;
|
|
742
742
|
}
|
|
743
743
|
|
|
744
|
-
protected*
|
|
744
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
745
745
|
let current = this.head;
|
|
746
746
|
|
|
747
747
|
while (current) {
|
|
@@ -232,7 +232,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
232
232
|
/**
|
|
233
233
|
* The below function is a generator that yields elements from a collection one by one.
|
|
234
234
|
*/
|
|
235
|
-
*
|
|
235
|
+
*begin(): Generator<E> {
|
|
236
236
|
let index = 0;
|
|
237
237
|
while (index < this.size) {
|
|
238
238
|
yield this.getAt(index);
|
|
@@ -244,7 +244,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
244
244
|
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
245
245
|
* the last element.
|
|
246
246
|
*/
|
|
247
|
-
*
|
|
247
|
+
*reverseBegin(): Generator<E> {
|
|
248
248
|
let index = this.size - 1;
|
|
249
249
|
while (index >= 0) {
|
|
250
250
|
yield this.getAt(index);
|
|
@@ -735,7 +735,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
735
735
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
736
736
|
* object to be iterated over using a for...of loop.
|
|
737
737
|
*/
|
|
738
|
-
protected*
|
|
738
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
739
739
|
for (let i = 0; i < this.size; ++i) {
|
|
740
740
|
yield this.getAt(i);
|
|
741
741
|
}
|
|
@@ -345,7 +345,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
345
345
|
* Space Complexity: O(n)
|
|
346
346
|
*/
|
|
347
347
|
|
|
348
|
-
protected*
|
|
348
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
349
349
|
for (const item of this.elements) {
|
|
350
350
|
yield item;
|
|
351
351
|
}
|
|
@@ -229,7 +229,7 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
229
229
|
* Custom iterator for the Stack class.
|
|
230
230
|
* @returns An iterator object.
|
|
231
231
|
*/
|
|
232
|
-
protected*
|
|
232
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
233
233
|
for (let i = 0; i < this.elements.length; i++) {
|
|
234
234
|
yield this.elements[i];
|
|
235
235
|
}
|
|
@@ -410,7 +410,7 @@ export class Trie extends IterableElementBase<string> {
|
|
|
410
410
|
return newTrie;
|
|
411
411
|
}
|
|
412
412
|
|
|
413
|
-
protected*
|
|
413
|
+
protected *_getIterator(): IterableIterator<string> {
|
|
414
414
|
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
415
415
|
if (node.isEnd) {
|
|
416
416
|
yield path;
|
|
@@ -2,12 +2,12 @@ export type VertexKey = string | number;
|
|
|
2
2
|
|
|
3
3
|
export type DijkstraResult<V> =
|
|
4
4
|
| {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
distMap: Map<V, number>;
|
|
6
|
+
distPaths?: Map<V, V[]>;
|
|
7
|
+
preMap: Map<V, V | undefined>;
|
|
8
|
+
seen: Set<V>;
|
|
9
|
+
paths: V[][];
|
|
10
|
+
minDist: number;
|
|
11
|
+
minPath: V[];
|
|
12
|
+
}
|
|
13
13
|
| undefined;
|
|
@@ -10,8 +10,9 @@ export type LinkedHashMapOptions<K> = {
|
|
|
10
10
|
objHashFn?: (key: K) => object;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
export type HashMapOptions<K> = {
|
|
13
|
+
export type HashMapOptions<K, V, T> = {
|
|
14
14
|
hashFn?: (key: K) => string;
|
|
15
|
+
toEntryFn?: (rawElement: T) => [K, V];
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
export type HashMapStoreItem<K, V> = { key: K; value: V };
|