max-heap-typed 2.2.1 → 2.2.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.
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/max-heap-typed.js.map +1 -1
- package/dist/umd/max-heap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
|
@@ -186,7 +186,7 @@ export class TreeMultiMapNode<K = any, V = any> {
|
|
|
186
186
|
*
|
|
187
187
|
* @example
|
|
188
188
|
* // players ranked by score with their equipment
|
|
189
|
-
*
|
|
189
|
+
* type Equipment = {
|
|
190
190
|
* name: string; // Equipment name
|
|
191
191
|
* quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
192
192
|
* level: number;
|
|
@@ -347,7 +347,7 @@ export class TreeMultiMapNode<K = any, V = any> {
|
|
|
347
347
|
* // },
|
|
348
348
|
* // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
349
349
|
* // ]
|
|
350
|
-
* // ]
|
|
350
|
+
* // ];
|
|
351
351
|
*/
|
|
352
352
|
export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
353
353
|
/**
|
|
@@ -35,7 +35,132 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
35
35
|
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
36
36
|
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
37
37
|
* @remarks Time O(1), Space O(1)
|
|
38
|
-
* @example
|
|
38
|
+
* @example
|
|
39
|
+
* // basic DirectedGraph vertex and edge creation
|
|
40
|
+
* // Create a simple directed graph
|
|
41
|
+
* const graph = new DirectedGraph<string>();
|
|
42
|
+
*
|
|
43
|
+
* // Add vertices
|
|
44
|
+
* graph.addVertex('A');
|
|
45
|
+
* graph.addVertex('B');
|
|
46
|
+
* graph.addVertex('C');
|
|
47
|
+
*
|
|
48
|
+
* // Verify vertices exist
|
|
49
|
+
* console.log(graph.hasVertex('A')); // true;
|
|
50
|
+
* console.log(graph.hasVertex('B')); // true;
|
|
51
|
+
* console.log(graph.hasVertex('C')); // true;
|
|
52
|
+
* console.log(graph.hasVertex('D')); // false;
|
|
53
|
+
*
|
|
54
|
+
* // Check vertex count
|
|
55
|
+
* console.log(graph.size); // 3;
|
|
56
|
+
* @example
|
|
57
|
+
* // DirectedGraph edge operations
|
|
58
|
+
* const graph = new DirectedGraph<string>();
|
|
59
|
+
*
|
|
60
|
+
* // Add vertices
|
|
61
|
+
* graph.addVertex('A');
|
|
62
|
+
* graph.addVertex('B');
|
|
63
|
+
* graph.addVertex('C');
|
|
64
|
+
*
|
|
65
|
+
* // Add directed edges
|
|
66
|
+
* graph.addEdge('A', 'B', 1);
|
|
67
|
+
* graph.addEdge('B', 'C', 2);
|
|
68
|
+
* graph.addEdge('A', 'C', 3);
|
|
69
|
+
*
|
|
70
|
+
* // Verify edges exist
|
|
71
|
+
* console.log(graph.hasEdge('A', 'B')); // true;
|
|
72
|
+
* console.log(graph.hasEdge('B', 'C')); // true;
|
|
73
|
+
* console.log(graph.hasEdge('C', 'B')); // false; // Graph is directed
|
|
74
|
+
*
|
|
75
|
+
* // Get neighbors of A
|
|
76
|
+
* const neighborsA = graph.getNeighbors('A');
|
|
77
|
+
* console.log(neighborsA[0].key); // 'B';
|
|
78
|
+
* console.log(neighborsA[1].key); // 'C';
|
|
79
|
+
* @example
|
|
80
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
81
|
+
* const graph = new DirectedGraph<string>();
|
|
82
|
+
*
|
|
83
|
+
* // Build a small graph
|
|
84
|
+
* graph.addVertex('X');
|
|
85
|
+
* graph.addVertex('Y');
|
|
86
|
+
* graph.addVertex('Z');
|
|
87
|
+
* graph.addEdge('X', 'Y', 1);
|
|
88
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
89
|
+
*
|
|
90
|
+
* // Delete an edge
|
|
91
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
92
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
93
|
+
*
|
|
94
|
+
* // Edge in other direction should not exist
|
|
95
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
96
|
+
*
|
|
97
|
+
* // Other edges should remain
|
|
98
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
99
|
+
*
|
|
100
|
+
* // Delete a vertex
|
|
101
|
+
* graph.deleteVertex('Y');
|
|
102
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
103
|
+
* console.log(graph.size); // 2;
|
|
104
|
+
* @example
|
|
105
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
106
|
+
* const graph = new DirectedGraph<string>();
|
|
107
|
+
*
|
|
108
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
109
|
+
* graph.addVertex('Design');
|
|
110
|
+
* graph.addVertex('Implement');
|
|
111
|
+
* graph.addVertex('Test');
|
|
112
|
+
* graph.addVertex('Deploy');
|
|
113
|
+
*
|
|
114
|
+
* // Add dependency edges
|
|
115
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
116
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
117
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
118
|
+
*
|
|
119
|
+
* // Topological sort gives valid execution order
|
|
120
|
+
* const executionOrder = graph.topologicalSort();
|
|
121
|
+
* console.log(executionOrder); // defined;
|
|
122
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
123
|
+
*
|
|
124
|
+
* // All vertices should be included
|
|
125
|
+
* console.log(executionOrder?.length); // 4;
|
|
126
|
+
* @example
|
|
127
|
+
* // DirectedGraph dijkstra shortest path for network routing
|
|
128
|
+
* // Build a weighted directed graph representing network nodes and costs
|
|
129
|
+
* const network = new DirectedGraph<string>();
|
|
130
|
+
*
|
|
131
|
+
* // Add network nodes
|
|
132
|
+
* network.addVertex('Router-A');
|
|
133
|
+
* network.addVertex('Router-B');
|
|
134
|
+
* network.addVertex('Router-C');
|
|
135
|
+
* network.addVertex('Router-D');
|
|
136
|
+
* network.addVertex('Router-E');
|
|
137
|
+
*
|
|
138
|
+
* // Add weighted edges (network latency costs)
|
|
139
|
+
* network.addEdge('Router-A', 'Router-B', 5);
|
|
140
|
+
* network.addEdge('Router-A', 'Router-C', 10);
|
|
141
|
+
* network.addEdge('Router-B', 'Router-D', 3);
|
|
142
|
+
* network.addEdge('Router-C', 'Router-D', 2);
|
|
143
|
+
* network.addEdge('Router-D', 'Router-E', 4);
|
|
144
|
+
* network.addEdge('Router-B', 'Router-E', 12);
|
|
145
|
+
*
|
|
146
|
+
* // Find shortest path from Router-A to Router-E
|
|
147
|
+
* const { minDist, minPath } = network.dijkstra('Router-A', 'Router-E', true, true) || {
|
|
148
|
+
* minDist: undefined,
|
|
149
|
+
* minPath: undefined
|
|
150
|
+
* };
|
|
151
|
+
*
|
|
152
|
+
* // Verify shortest path is found
|
|
153
|
+
* console.log(minDist); // defined;
|
|
154
|
+
* console.log(minPath); // defined;
|
|
155
|
+
*
|
|
156
|
+
* // Shortest path should be A -> B -> D -> E with cost 5+3+4=12
|
|
157
|
+
* // Or A -> C -> D -> E with cost 10+2+4=16
|
|
158
|
+
* // So the minimum is 12
|
|
159
|
+
* console.log(minDist); // <= 16;
|
|
160
|
+
*
|
|
161
|
+
* // Verify path is valid (includes start and end)
|
|
162
|
+
* console.log(minPath?.[0].key); // 'Router-A';
|
|
163
|
+
* console.log(minPath?.[minPath.length - 1].key); // 'Router-E';
|
|
39
164
|
*/
|
|
40
165
|
export class DirectedGraph<
|
|
41
166
|
V = any,
|
|
@@ -33,7 +33,166 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
33
33
|
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
34
34
|
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
35
35
|
* @remarks Time O(1), Space O(1)
|
|
36
|
-
* @example
|
|
36
|
+
* @example
|
|
37
|
+
* // basic UndirectedGraph vertex and edge creation
|
|
38
|
+
* // Create a simple undirected graph
|
|
39
|
+
* const graph = new UndirectedGraph<string>();
|
|
40
|
+
*
|
|
41
|
+
* // Add vertices
|
|
42
|
+
* graph.addVertex('A');
|
|
43
|
+
* graph.addVertex('B');
|
|
44
|
+
* graph.addVertex('C');
|
|
45
|
+
* graph.addVertex('D');
|
|
46
|
+
*
|
|
47
|
+
* // Verify vertices exist
|
|
48
|
+
* console.log(graph.hasVertex('A')); // true;
|
|
49
|
+
* console.log(graph.hasVertex('B')); // true;
|
|
50
|
+
* console.log(graph.hasVertex('E')); // false;
|
|
51
|
+
*
|
|
52
|
+
* // Check vertex count
|
|
53
|
+
* console.log(graph.size); // 4;
|
|
54
|
+
* @example
|
|
55
|
+
* // UndirectedGraph edge operations (bidirectional)
|
|
56
|
+
* const graph = new UndirectedGraph<string>();
|
|
57
|
+
*
|
|
58
|
+
* // Add vertices
|
|
59
|
+
* graph.addVertex('A');
|
|
60
|
+
* graph.addVertex('B');
|
|
61
|
+
* graph.addVertex('C');
|
|
62
|
+
*
|
|
63
|
+
* // Add undirected edges (both directions automatically)
|
|
64
|
+
* graph.addEdge('A', 'B', 1);
|
|
65
|
+
* graph.addEdge('B', 'C', 2);
|
|
66
|
+
* graph.addEdge('A', 'C', 3);
|
|
67
|
+
*
|
|
68
|
+
* // Verify edges exist in both directions
|
|
69
|
+
* console.log(graph.hasEdge('A', 'B')); // true;
|
|
70
|
+
* console.log(graph.hasEdge('B', 'A')); // true; // Bidirectional!
|
|
71
|
+
*
|
|
72
|
+
* console.log(graph.hasEdge('C', 'B')); // true;
|
|
73
|
+
* console.log(graph.hasEdge('B', 'C')); // true; // Bidirectional!
|
|
74
|
+
*
|
|
75
|
+
* // Get neighbors of A
|
|
76
|
+
* const neighborsA = graph.getNeighbors('A');
|
|
77
|
+
* console.log(neighborsA[0].key); // 'B';
|
|
78
|
+
* console.log(neighborsA[1].key); // 'C';
|
|
79
|
+
* @example
|
|
80
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
81
|
+
* const graph = new UndirectedGraph<string>();
|
|
82
|
+
*
|
|
83
|
+
* // Build a simple undirected graph
|
|
84
|
+
* graph.addVertex('X');
|
|
85
|
+
* graph.addVertex('Y');
|
|
86
|
+
* graph.addVertex('Z');
|
|
87
|
+
* graph.addEdge('X', 'Y', 1);
|
|
88
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
89
|
+
* graph.addEdge('X', 'Z', 3);
|
|
90
|
+
*
|
|
91
|
+
* // Delete an edge
|
|
92
|
+
* graph.deleteEdge('X', 'Y');
|
|
93
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
94
|
+
*
|
|
95
|
+
* // Bidirectional deletion confirmed
|
|
96
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
97
|
+
*
|
|
98
|
+
* // Other edges should remain
|
|
99
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
100
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
101
|
+
*
|
|
102
|
+
* // Delete a vertex
|
|
103
|
+
* graph.deleteVertex('Y');
|
|
104
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
105
|
+
* console.log(graph.size); // 2;
|
|
106
|
+
* @example
|
|
107
|
+
* // UndirectedGraph connectivity and neighbors
|
|
108
|
+
* const graph = new UndirectedGraph<string>();
|
|
109
|
+
*
|
|
110
|
+
* // Build a friendship network
|
|
111
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
112
|
+
* for (const person of people) {
|
|
113
|
+
* graph.addVertex(person);
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* // Add friendships (undirected edges)
|
|
117
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
118
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
119
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
120
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
121
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
122
|
+
*
|
|
123
|
+
* // Get friends of each person
|
|
124
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
125
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
126
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
127
|
+
* console.log(aliceFriends.length); // 2;
|
|
128
|
+
*
|
|
129
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
130
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
131
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
132
|
+
* console.log(dianaFriends.length); // 2;
|
|
133
|
+
*
|
|
134
|
+
* // Verify bidirectional friendship
|
|
135
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
136
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
137
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
138
|
+
* @example
|
|
139
|
+
* // UndirectedGraph for social network connectivity analysis
|
|
140
|
+
* interface Person {
|
|
141
|
+
* id: number;
|
|
142
|
+
* name: string;
|
|
143
|
+
* location: string;
|
|
144
|
+
* }
|
|
145
|
+
*
|
|
146
|
+
* // UndirectedGraph is perfect for modeling symmetric relationships
|
|
147
|
+
* // (friendships, collaborations, partnerships)
|
|
148
|
+
* const socialNetwork = new UndirectedGraph<number, Person>();
|
|
149
|
+
*
|
|
150
|
+
* // Add people as vertices
|
|
151
|
+
* const people: [number, Person][] = [
|
|
152
|
+
* [1, { id: 1, name: 'Alice', location: 'New York' }],
|
|
153
|
+
* [2, { id: 2, name: 'Bob', location: 'San Francisco' }],
|
|
154
|
+
* [3, { id: 3, name: 'Charlie', location: 'Boston' }],
|
|
155
|
+
* [4, { id: 4, name: 'Diana', location: 'New York' }],
|
|
156
|
+
* [5, { id: 5, name: 'Eve', location: 'Seattle' }]
|
|
157
|
+
* ];
|
|
158
|
+
*
|
|
159
|
+
* for (const [id] of people) {
|
|
160
|
+
* socialNetwork.addVertex(id);
|
|
161
|
+
* }
|
|
162
|
+
*
|
|
163
|
+
* // Add friendships (automatically bidirectional)
|
|
164
|
+
* socialNetwork.addEdge(1, 2, 1); // Alice <-> Bob
|
|
165
|
+
* socialNetwork.addEdge(1, 3, 1); // Alice <-> Charlie
|
|
166
|
+
* socialNetwork.addEdge(2, 4, 1); // Bob <-> Diana
|
|
167
|
+
* socialNetwork.addEdge(3, 5, 1); // Charlie <-> Eve
|
|
168
|
+
* socialNetwork.addEdge(4, 5, 1); // Diana <-> Eve
|
|
169
|
+
*
|
|
170
|
+
* console.log(socialNetwork.size); // 5;
|
|
171
|
+
*
|
|
172
|
+
* // Find direct connections for Alice
|
|
173
|
+
* const aliceConnections = socialNetwork.getNeighbors(1);
|
|
174
|
+
* console.log(aliceConnections[0].key); // 2;
|
|
175
|
+
* console.log(aliceConnections[1].key); // 3;
|
|
176
|
+
* console.log(aliceConnections.length); // 2;
|
|
177
|
+
*
|
|
178
|
+
* // Verify bidirectional connections
|
|
179
|
+
* console.log(socialNetwork.hasEdge(1, 2)); // true;
|
|
180
|
+
* console.log(socialNetwork.hasEdge(2, 1)); // true; // Friendship works both ways!
|
|
181
|
+
*
|
|
182
|
+
* // Remove a person from network
|
|
183
|
+
* socialNetwork.deleteVertex(2); // Bob leaves
|
|
184
|
+
* console.log(socialNetwork.hasVertex(2)); // false;
|
|
185
|
+
* console.log(socialNetwork.size); // 4;
|
|
186
|
+
*
|
|
187
|
+
* // Alice loses Bob as a friend
|
|
188
|
+
* const updatedAliceConnections = socialNetwork.getNeighbors(1);
|
|
189
|
+
* console.log(updatedAliceConnections[0].key); // 3;
|
|
190
|
+
* console.log(updatedAliceConnections[1]); // undefined;
|
|
191
|
+
*
|
|
192
|
+
* // Diana loses Bob as a friend
|
|
193
|
+
* const dianaConnections = socialNetwork.getNeighbors(4);
|
|
194
|
+
* console.log(dianaConnections[0].key); // 5;
|
|
195
|
+
* console.log(dianaConnections[1]); // undefined;
|
|
37
196
|
*/
|
|
38
197
|
export class UndirectedGraph<
|
|
39
198
|
V = any,
|
|
@@ -29,7 +29,7 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
29
29
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
30
30
|
* @example
|
|
31
31
|
* // should maintain insertion order
|
|
32
|
-
*
|
|
32
|
+
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
33
33
|
* linkedHashMap.set(1, 'A');
|
|
34
34
|
* linkedHashMap.set(2, 'B');
|
|
35
35
|
* linkedHashMap.set(3, 'C');
|
|
@@ -39,40 +39,123 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
39
39
|
* // [1, 'A'],
|
|
40
40
|
* // [2, 'B'],
|
|
41
41
|
* // [3, 'C']
|
|
42
|
-
* // ]
|
|
42
|
+
* // ];
|
|
43
43
|
* @example
|
|
44
|
-
* //
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
44
|
+
* // basic HashMap creation and set operation
|
|
45
|
+
* // Create a simple HashMap with key-value pairs
|
|
46
|
+
* const map = new HashMap<number, string>([
|
|
47
|
+
* [1, 'one'],
|
|
48
|
+
* [2, 'two'],
|
|
49
|
+
* [3, 'three']
|
|
50
|
+
* ]);
|
|
49
51
|
*
|
|
50
|
-
*
|
|
51
|
-
* console.log(
|
|
52
|
-
*
|
|
53
|
-
*
|
|
52
|
+
* // Verify size
|
|
53
|
+
* console.log(map.size); // 3;
|
|
54
|
+
*
|
|
55
|
+
* // Set a new key-value pair
|
|
56
|
+
* map.set(4, 'four');
|
|
57
|
+
* console.log(map.size); // 4;
|
|
58
|
+
*
|
|
59
|
+
* // Verify entries
|
|
60
|
+
* console.log([...map.entries()]); // length: 4;
|
|
54
61
|
* @example
|
|
55
|
-
* //
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
62
|
+
* // HashMap get and has operations
|
|
63
|
+
* const map = new HashMap<string, number>([
|
|
64
|
+
* ['apple', 1],
|
|
65
|
+
* ['banana', 2],
|
|
66
|
+
* ['cherry', 3]
|
|
67
|
+
* ]);
|
|
68
|
+
*
|
|
69
|
+
* // Check if key exists
|
|
70
|
+
* console.log(map.has('apple')); // true;
|
|
71
|
+
* console.log(map.has('date')); // false;
|
|
60
72
|
*
|
|
61
|
-
*
|
|
62
|
-
* console.log(
|
|
63
|
-
* console.log(
|
|
73
|
+
* // Get value by key
|
|
74
|
+
* console.log(map.get('banana')); // 2;
|
|
75
|
+
* console.log(map.get('grape')); // undefined;
|
|
76
|
+
*
|
|
77
|
+
* // Get all keys and values
|
|
78
|
+
* const keys = [...map.keys()];
|
|
79
|
+
* const values = [...map.values()];
|
|
80
|
+
* console.log(keys); // contains 'apple';
|
|
81
|
+
* console.log(values); // contains 3;
|
|
64
82
|
* @example
|
|
65
|
-
* //
|
|
66
|
-
*
|
|
83
|
+
* // HashMap iteration and filter operations
|
|
84
|
+
* const map = new HashMap<number, string>([
|
|
85
|
+
* [1, 'Alice'],
|
|
86
|
+
* [2, 'Bob'],
|
|
87
|
+
* [3, 'Charlie'],
|
|
88
|
+
* [4, 'Diana'],
|
|
89
|
+
* [5, 'Eve']
|
|
90
|
+
* ]);
|
|
91
|
+
*
|
|
92
|
+
* // Iterate through entries
|
|
93
|
+
* const entries: [number, string][] = [];
|
|
94
|
+
* for (const [key, value] of map) {
|
|
95
|
+
* entries.push([key, value]);
|
|
96
|
+
* }
|
|
97
|
+
* console.log(entries); // length: 5;
|
|
98
|
+
*
|
|
99
|
+
* // Filter operation (for iteration with collection methods)
|
|
100
|
+
* const filtered = [...map].filter(([key]) => key > 2);
|
|
101
|
+
* console.log(filtered.length); // 3;
|
|
67
102
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
103
|
+
* // Map operation
|
|
104
|
+
* const values = [...map.values()].map(v => v.length);
|
|
105
|
+
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
106
|
+
* console.log(values); // contains 7;
|
|
107
|
+
* @example
|
|
108
|
+
* // HashMap for user session caching O(1) performance
|
|
109
|
+
* interface UserSession {
|
|
110
|
+
* userId: number;
|
|
111
|
+
* username: string;
|
|
112
|
+
* loginTime: number;
|
|
113
|
+
* lastActivity: number;
|
|
71
114
|
* }
|
|
72
115
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
116
|
+
* // HashMap provides O(1) average-case performance for set/get/delete
|
|
117
|
+
* // Perfect for session management with fast lookups
|
|
118
|
+
* const sessionCache = new HashMap<string, UserSession>();
|
|
119
|
+
*
|
|
120
|
+
* // Simulate user sessions
|
|
121
|
+
* const sessions: [string, UserSession][] = [
|
|
122
|
+
* ['session_001', { userId: 1, username: 'alice', loginTime: 1000, lastActivity: 1050 }],
|
|
123
|
+
* ['session_002', { userId: 2, username: 'bob', loginTime: 1100, lastActivity: 1150 }],
|
|
124
|
+
* ['session_003', { userId: 3, username: 'charlie', loginTime: 1200, lastActivity: 1250 }]
|
|
125
|
+
* ];
|
|
126
|
+
*
|
|
127
|
+
* // Store sessions with O(1) insertion
|
|
128
|
+
* for (const [token, session] of sessions) {
|
|
129
|
+
* sessionCache.set(token, session);
|
|
130
|
+
* }
|
|
131
|
+
*
|
|
132
|
+
* console.log(sessionCache.size); // 3;
|
|
133
|
+
*
|
|
134
|
+
* // Retrieve session with O(1) lookup
|
|
135
|
+
* const userSession = sessionCache.get('session_001');
|
|
136
|
+
* console.log(userSession?.username); // 'alice';
|
|
137
|
+
* console.log(userSession?.userId); // 1;
|
|
138
|
+
*
|
|
139
|
+
* // Update session with O(1) operation
|
|
140
|
+
* if (userSession) {
|
|
141
|
+
* userSession.lastActivity = 2000;
|
|
142
|
+
* sessionCache.set('session_001', userSession);
|
|
143
|
+
* }
|
|
144
|
+
*
|
|
145
|
+
* // Check updated value
|
|
146
|
+
* const updated = sessionCache.get('session_001');
|
|
147
|
+
* console.log(updated?.lastActivity); // 2000;
|
|
148
|
+
*
|
|
149
|
+
* // Cleanup: delete expired sessions
|
|
150
|
+
* sessionCache.delete('session_002');
|
|
151
|
+
* console.log(sessionCache.has('session_002')); // false;
|
|
152
|
+
*
|
|
153
|
+
* // Verify remaining sessions
|
|
154
|
+
* console.log(sessionCache.size); // 2;
|
|
155
|
+
*
|
|
156
|
+
* // Get all active sessions
|
|
157
|
+
* const activeCount = [...sessionCache.values()].length;
|
|
158
|
+
* console.log(activeCount); // 2;
|
|
76
159
|
*/
|
|
77
160
|
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
78
161
|
/**
|