max-priority-queue-typed 2.4.5 → 2.5.0
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/README.md +63 -0
- package/dist/cjs/index.cjs +400 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +399 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +400 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +399 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/max-priority-queue-typed.js +397 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -66,65 +66,6 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
66
66
|
* console.log(neighborsA[0].key); // 'B';
|
|
67
67
|
* console.log(neighborsA[1].key); // 'C';
|
|
68
68
|
* @example
|
|
69
|
-
* // UndirectedGraph deleteEdge and vertex operations
|
|
70
|
-
* const graph = new UndirectedGraph<string>();
|
|
71
|
-
*
|
|
72
|
-
* // Build a simple undirected graph
|
|
73
|
-
* graph.addVertex('X');
|
|
74
|
-
* graph.addVertex('Y');
|
|
75
|
-
* graph.addVertex('Z');
|
|
76
|
-
* graph.addEdge('X', 'Y', 1);
|
|
77
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
78
|
-
* graph.addEdge('X', 'Z', 3);
|
|
79
|
-
*
|
|
80
|
-
* // Delete an edge
|
|
81
|
-
* graph.deleteEdge('X', 'Y');
|
|
82
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
83
|
-
*
|
|
84
|
-
* // Bidirectional deletion confirmed
|
|
85
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
86
|
-
*
|
|
87
|
-
* // Other edges should remain
|
|
88
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
89
|
-
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
90
|
-
*
|
|
91
|
-
* // Delete a vertex
|
|
92
|
-
* graph.deleteVertex('Y');
|
|
93
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
94
|
-
* console.log(graph.size); // 2;
|
|
95
|
-
* @example
|
|
96
|
-
* // UndirectedGraph connectivity and neighbors
|
|
97
|
-
* const graph = new UndirectedGraph<string>();
|
|
98
|
-
*
|
|
99
|
-
* // Build a friendship network
|
|
100
|
-
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
101
|
-
* for (const person of people) {
|
|
102
|
-
* graph.addVertex(person);
|
|
103
|
-
* }
|
|
104
|
-
*
|
|
105
|
-
* // Add friendships (undirected edges)
|
|
106
|
-
* graph.addEdge('Alice', 'Bob', 1);
|
|
107
|
-
* graph.addEdge('Alice', 'Charlie', 1);
|
|
108
|
-
* graph.addEdge('Bob', 'Diana', 1);
|
|
109
|
-
* graph.addEdge('Charlie', 'Eve', 1);
|
|
110
|
-
* graph.addEdge('Diana', 'Eve', 1);
|
|
111
|
-
*
|
|
112
|
-
* // Get friends of each person
|
|
113
|
-
* const aliceFriends = graph.getNeighbors('Alice');
|
|
114
|
-
* console.log(aliceFriends[0].key); // 'Bob';
|
|
115
|
-
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
116
|
-
* console.log(aliceFriends.length); // 2;
|
|
117
|
-
*
|
|
118
|
-
* const dianaFriends = graph.getNeighbors('Diana');
|
|
119
|
-
* console.log(dianaFriends[0].key); // 'Bob';
|
|
120
|
-
* console.log(dianaFriends[1].key); // 'Eve';
|
|
121
|
-
* console.log(dianaFriends.length); // 2;
|
|
122
|
-
*
|
|
123
|
-
* // Verify bidirectional friendship
|
|
124
|
-
* const bobFriends = graph.getNeighbors('Bob');
|
|
125
|
-
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
126
|
-
* console.log(bobFriends[1].key); // 'Diana';
|
|
127
|
-
* @example
|
|
128
69
|
* // UndirectedGraph for social network connectivity analysis
|
|
129
70
|
* interface Person {
|
|
130
71
|
* id: number;
|
|
@@ -233,6 +174,21 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
233
174
|
* @param v2 - The other vertex or key.
|
|
234
175
|
* @returns Edge instance or `undefined`.
|
|
235
176
|
* @remarks Time O(1) avg, Space O(1)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
* @example
|
|
186
|
+
* // Get edge between vertices
|
|
187
|
+
* const g = new UndirectedGraph();
|
|
188
|
+
* g.addVertex('A');
|
|
189
|
+
* g.addVertex('B');
|
|
190
|
+
* g.addEdge('A', 'B', 7);
|
|
191
|
+
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
236
192
|
*/
|
|
237
193
|
getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined;
|
|
238
194
|
/**
|
|
@@ -249,6 +205,44 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
249
205
|
* @param otherSideVertexKey - Required second endpoint when deleting by pair.
|
|
250
206
|
* @returns Removed edge or `undefined`.
|
|
251
207
|
* @remarks Time O(1) avg, Space O(1)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
* @example
|
|
220
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
221
|
+
* const graph = new UndirectedGraph<string>();
|
|
222
|
+
*
|
|
223
|
+
* // Build a simple undirected graph
|
|
224
|
+
* graph.addVertex('X');
|
|
225
|
+
* graph.addVertex('Y');
|
|
226
|
+
* graph.addVertex('Z');
|
|
227
|
+
* graph.addEdge('X', 'Y', 1);
|
|
228
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
229
|
+
* graph.addEdge('X', 'Z', 3);
|
|
230
|
+
*
|
|
231
|
+
* // Delete an edge
|
|
232
|
+
* graph.deleteEdge('X', 'Y');
|
|
233
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
234
|
+
*
|
|
235
|
+
* // Bidirectional deletion confirmed
|
|
236
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
237
|
+
*
|
|
238
|
+
* // Other edges should remain
|
|
239
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
240
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
241
|
+
*
|
|
242
|
+
* // Delete a vertex
|
|
243
|
+
* graph.deleteVertex('Y');
|
|
244
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
245
|
+
* console.log(graph.size); // 2;
|
|
252
246
|
*/
|
|
253
247
|
deleteEdge(edgeOrOneSideVertexKey: EO | VertexKey, otherSideVertexKey?: VertexKey): EO | undefined;
|
|
254
248
|
/**
|
|
@@ -256,6 +250,22 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
256
250
|
* @param vertexOrKey - Vertex or key.
|
|
257
251
|
* @returns `true` if removed; otherwise `false`.
|
|
258
252
|
* @remarks Time O(deg), Space O(1)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
* @example
|
|
262
|
+
* // Remove vertex and edges
|
|
263
|
+
* const g = new UndirectedGraph();
|
|
264
|
+
* g.addVertex('A');
|
|
265
|
+
* g.addVertex('B');
|
|
266
|
+
* g.addEdge('A', 'B');
|
|
267
|
+
* g.deleteVertex('A');
|
|
268
|
+
* console.log(g.hasVertex('A')); // false;
|
|
259
269
|
*/
|
|
260
270
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
261
271
|
/**
|
|
@@ -276,8 +286,69 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
276
286
|
* Unique set of undirected edges across endpoints.
|
|
277
287
|
* @returns Array of edges.
|
|
278
288
|
* @remarks Time O(E), Space O(E)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
* @example
|
|
298
|
+
* // Get all edges
|
|
299
|
+
* const g = new UndirectedGraph();
|
|
300
|
+
* g.addVertex('A');
|
|
301
|
+
* g.addVertex('B');
|
|
302
|
+
* g.addEdge('A', 'B');
|
|
303
|
+
* console.log(g.edgeSet().length); // 1;
|
|
279
304
|
*/
|
|
280
305
|
edgeSet(): EO[];
|
|
306
|
+
/**
|
|
307
|
+
* UndirectedGraph connectivity and neighbors
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
* @example
|
|
320
|
+
* // UndirectedGraph connectivity and neighbors
|
|
321
|
+
* const graph = new UndirectedGraph<string>();
|
|
322
|
+
*
|
|
323
|
+
* // Build a friendship network
|
|
324
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
325
|
+
* for (const person of people) {
|
|
326
|
+
* graph.addVertex(person);
|
|
327
|
+
* }
|
|
328
|
+
*
|
|
329
|
+
* // Add friendships (undirected edges)
|
|
330
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
331
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
332
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
333
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
334
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
335
|
+
*
|
|
336
|
+
* // Get friends of each person
|
|
337
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
338
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
339
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
340
|
+
* console.log(aliceFriends.length); // 2;
|
|
341
|
+
*
|
|
342
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
343
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
344
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
345
|
+
* console.log(dianaFriends.length); // 2;
|
|
346
|
+
*
|
|
347
|
+
* // Verify bidirectional friendship
|
|
348
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
349
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
350
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
351
|
+
*/
|
|
281
352
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
282
353
|
/**
|
|
283
354
|
* Resolve an edge's two endpoints to vertex instances.
|
|
@@ -306,6 +377,24 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
306
377
|
* Tarjan-based bridge and articulation point detection.
|
|
307
378
|
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
308
379
|
* @remarks Time O(V + E), Space O(V + E)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
* @example
|
|
389
|
+
* // Find articulation points and bridges
|
|
390
|
+
* const g = new UndirectedGraph();
|
|
391
|
+
* g.addVertex('A');
|
|
392
|
+
* g.addVertex('B');
|
|
393
|
+
* g.addVertex('C');
|
|
394
|
+
* g.addEdge('A', 'B');
|
|
395
|
+
* g.addEdge('B', 'C');
|
|
396
|
+
* const result = g.tarjan();
|
|
397
|
+
* console.log(result); // defined;
|
|
309
398
|
*/
|
|
310
399
|
tarjan(): {
|
|
311
400
|
dfnMap: Map<VO, number>;
|
|
@@ -325,18 +414,74 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
325
414
|
* Uses DFS with parent tracking.
|
|
326
415
|
* @returns `true` if a cycle exists, `false` otherwise.
|
|
327
416
|
* @remarks Time O(V + E), Space O(V)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
* @example
|
|
426
|
+
* // Detect cycle
|
|
427
|
+
* const g = new UndirectedGraph();
|
|
428
|
+
* g.addVertex('A');
|
|
429
|
+
* g.addVertex('B');
|
|
430
|
+
* g.addVertex('C');
|
|
431
|
+
* g.addEdge('A', 'B');
|
|
432
|
+
* g.addEdge('B', 'C');
|
|
433
|
+
* console.log(g.hasCycle()); // false;
|
|
434
|
+
* g.addEdge('C', 'A');
|
|
435
|
+
* console.log(g.hasCycle()); // true;
|
|
328
436
|
*/
|
|
329
437
|
hasCycle(): boolean;
|
|
330
438
|
/**
|
|
331
439
|
* Get bridges discovered by `tarjan()`.
|
|
332
440
|
* @returns Array of edges that are bridges.
|
|
333
441
|
* @remarks Time O(B), Space O(1)
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
* @example
|
|
451
|
+
* // Find bridge edges
|
|
452
|
+
* const g = new UndirectedGraph();
|
|
453
|
+
* g.addVertex('A');
|
|
454
|
+
* g.addVertex('B');
|
|
455
|
+
* g.addVertex('C');
|
|
456
|
+
* g.addEdge('A', 'B');
|
|
457
|
+
* g.addEdge('B', 'C');
|
|
458
|
+
* const bridges = g.getBridges();
|
|
459
|
+
* console.log(bridges.length); // 2;
|
|
334
460
|
*/
|
|
335
461
|
getBridges(): EO[];
|
|
336
462
|
/**
|
|
337
463
|
* Get articulation points discovered by `tarjan()`.
|
|
338
464
|
* @returns Array of cut vertices.
|
|
339
465
|
* @remarks Time O(C), Space O(1)
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
* @example
|
|
475
|
+
* // Find articulation points
|
|
476
|
+
* const g = new UndirectedGraph();
|
|
477
|
+
* g.addVertex('A');
|
|
478
|
+
* g.addVertex('B');
|
|
479
|
+
* g.addVertex('C');
|
|
480
|
+
* g.addEdge('A', 'B');
|
|
481
|
+
* g.addEdge('B', 'C');
|
|
482
|
+
* const cuts = g.getCutVertices();
|
|
483
|
+
* console.log(cuts.length); // 1;
|
|
484
|
+
* console.log(cuts[0].key); // 'B';
|
|
340
485
|
*/
|
|
341
486
|
getCutVertices(): VO[];
|
|
342
487
|
/**
|
|
@@ -19,83 +19,6 @@ import { IterableEntryBase } from '../base';
|
|
|
19
19
|
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
20
20
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
21
21
|
* @example
|
|
22
|
-
* // should maintain insertion order
|
|
23
|
-
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
24
|
-
* linkedHashMap.set(1, 'A');
|
|
25
|
-
* linkedHashMap.set(2, 'B');
|
|
26
|
-
* linkedHashMap.set(3, 'C');
|
|
27
|
-
*
|
|
28
|
-
* const result = Array.from(linkedHashMap);
|
|
29
|
-
* console.log(result); // [
|
|
30
|
-
* // [1, 'A'],
|
|
31
|
-
* // [2, 'B'],
|
|
32
|
-
* // [3, 'C']
|
|
33
|
-
* // ];
|
|
34
|
-
* @example
|
|
35
|
-
* // basic HashMap creation and set operation
|
|
36
|
-
* // Create a simple HashMap with key-value pairs
|
|
37
|
-
* const map = new HashMap<number, string>([
|
|
38
|
-
* [1, 'one'],
|
|
39
|
-
* [2, 'two'],
|
|
40
|
-
* [3, 'three']
|
|
41
|
-
* ]);
|
|
42
|
-
*
|
|
43
|
-
* // Verify size
|
|
44
|
-
* console.log(map.size); // 3;
|
|
45
|
-
*
|
|
46
|
-
* // Set a new key-value pair
|
|
47
|
-
* map.set(4, 'four');
|
|
48
|
-
* console.log(map.size); // 4;
|
|
49
|
-
*
|
|
50
|
-
* // Verify entries
|
|
51
|
-
* console.log([...map.entries()]); // length: 4;
|
|
52
|
-
* @example
|
|
53
|
-
* // HashMap get and has operations
|
|
54
|
-
* const map = new HashMap<string, number>([
|
|
55
|
-
* ['apple', 1],
|
|
56
|
-
* ['banana', 2],
|
|
57
|
-
* ['cherry', 3]
|
|
58
|
-
* ]);
|
|
59
|
-
*
|
|
60
|
-
* // Check if key exists
|
|
61
|
-
* console.log(map.has('apple')); // true;
|
|
62
|
-
* console.log(map.has('date')); // false;
|
|
63
|
-
*
|
|
64
|
-
* // Get value by key
|
|
65
|
-
* console.log(map.get('banana')); // 2;
|
|
66
|
-
* console.log(map.get('grape')); // undefined;
|
|
67
|
-
*
|
|
68
|
-
* // Get all keys and values
|
|
69
|
-
* const keys = [...map.keys()];
|
|
70
|
-
* const values = [...map.values()];
|
|
71
|
-
* console.log(keys); // contains 'apple';
|
|
72
|
-
* console.log(values); // contains 3;
|
|
73
|
-
* @example
|
|
74
|
-
* // HashMap iteration and filter operations
|
|
75
|
-
* const map = new HashMap<number, string>([
|
|
76
|
-
* [1, 'Alice'],
|
|
77
|
-
* [2, 'Bob'],
|
|
78
|
-
* [3, 'Charlie'],
|
|
79
|
-
* [4, 'Diana'],
|
|
80
|
-
* [5, 'Eve']
|
|
81
|
-
* ]);
|
|
82
|
-
*
|
|
83
|
-
* // Iterate through entries
|
|
84
|
-
* const entries: [number, string][] = [];
|
|
85
|
-
* for (const [key, value] of map) {
|
|
86
|
-
* entries.push([key, value]);
|
|
87
|
-
* }
|
|
88
|
-
* console.log(entries); // length: 5;
|
|
89
|
-
*
|
|
90
|
-
* // Filter operation (for iteration with collection methods)
|
|
91
|
-
* const filtered = [...map].filter(([key]) => key > 2);
|
|
92
|
-
* console.log(filtered.length); // 3;
|
|
93
|
-
*
|
|
94
|
-
* // Map operation
|
|
95
|
-
* const values = [...map.values()].map(v => v.length);
|
|
96
|
-
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
97
|
-
* console.log(values); // contains 7;
|
|
98
|
-
* @example
|
|
99
22
|
* // HashMap for user session caching O(1) performance
|
|
100
23
|
* interface UserSession {
|
|
101
24
|
* userId: number;
|
|
@@ -147,6 +70,19 @@ import { IterableEntryBase } from '../base';
|
|
|
147
70
|
* // Get all active sessions
|
|
148
71
|
* const activeCount = [...sessionCache.values()].length;
|
|
149
72
|
* console.log(activeCount); // 2;
|
|
73
|
+
* @example
|
|
74
|
+
* // Aggregate values
|
|
75
|
+
* const counts = new HashMap<string, number>([['a', 5], ['b', 3], ['c', 8]]);
|
|
76
|
+
*
|
|
77
|
+
* const total = counts.reduce((sum, v) => sum + (v ?? 0), 0);
|
|
78
|
+
* console.log(total); // 16;
|
|
79
|
+
* @example
|
|
80
|
+
* // Iterate over entries
|
|
81
|
+
* const map = new HashMap<string, number>([['x', 1], ['y', 2]]);
|
|
82
|
+
* const keys: string[] = [];
|
|
83
|
+
*
|
|
84
|
+
* map.forEach((v, k) => keys.push(k));
|
|
85
|
+
* console.log(keys.sort()); // ['x', 'y'];
|
|
150
86
|
*/
|
|
151
87
|
export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
152
88
|
/**
|
|
@@ -200,12 +136,39 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
200
136
|
* Check whether the map is empty.
|
|
201
137
|
* @remarks Time O(1), Space O(1)
|
|
202
138
|
* @returns True if size is 0.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
* @example
|
|
149
|
+
* // Check if empty
|
|
150
|
+
* const map = new HashMap();
|
|
151
|
+
* console.log(map.isEmpty()); // true;
|
|
203
152
|
*/
|
|
204
153
|
isEmpty(): boolean;
|
|
205
154
|
/**
|
|
206
155
|
* Remove all entries and reset counters.
|
|
207
156
|
* @remarks Time O(N), Space O(1)
|
|
208
157
|
* @returns void
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
* @example
|
|
168
|
+
* // Remove all entries
|
|
169
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
170
|
+
* map.clear();
|
|
171
|
+
* console.log(map.isEmpty()); // true;
|
|
209
172
|
*/
|
|
210
173
|
clear(): void;
|
|
211
174
|
/**
|
|
@@ -220,6 +183,46 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
220
183
|
* @param key - Key.
|
|
221
184
|
* @param value - Value.
|
|
222
185
|
* @returns True when the operation succeeds.
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
* @example
|
|
209
|
+
* // basic HashMap creation and set operation
|
|
210
|
+
* // Create a simple HashMap with key-value pairs
|
|
211
|
+
* const map = new HashMap<number, string>([
|
|
212
|
+
* [1, 'one'],
|
|
213
|
+
* [2, 'two'],
|
|
214
|
+
* [3, 'three']
|
|
215
|
+
* ]);
|
|
216
|
+
*
|
|
217
|
+
* // Verify size
|
|
218
|
+
* console.log(map.size); // 3;
|
|
219
|
+
*
|
|
220
|
+
* // Set a new key-value pair
|
|
221
|
+
* map.set(4, 'four');
|
|
222
|
+
* console.log(map.size); // 4;
|
|
223
|
+
*
|
|
224
|
+
* // Verify entries
|
|
225
|
+
* console.log([...map.entries()]); // length: 4;
|
|
223
226
|
*/
|
|
224
227
|
set(key: K, value: V): boolean;
|
|
225
228
|
/**
|
|
@@ -227,6 +230,20 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
227
230
|
* @remarks Time O(N), Space O(N)
|
|
228
231
|
* @param entryOrRawElements - Iterable of entries or raw elements to insert.
|
|
229
232
|
* @returns Array of per-entry results.
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
* @example
|
|
243
|
+
* // Add multiple entries
|
|
244
|
+
* const map = new HashMap<string, number>();
|
|
245
|
+
* map.setMany([['a', 1], ['b', 2], ['c', 3]]);
|
|
246
|
+
* console.log(map.size); // 3;
|
|
230
247
|
*/
|
|
231
248
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
|
|
232
249
|
/**
|
|
@@ -234,6 +251,38 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
234
251
|
* @remarks Time O(1), Space O(1)
|
|
235
252
|
* @param key - Key to look up.
|
|
236
253
|
* @returns Value or undefined.
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
* @example
|
|
266
|
+
* // HashMap get and has operations
|
|
267
|
+
* const map = new HashMap<string, number>([
|
|
268
|
+
* ['apple', 1],
|
|
269
|
+
* ['banana', 2],
|
|
270
|
+
* ['cherry', 3]
|
|
271
|
+
* ]);
|
|
272
|
+
*
|
|
273
|
+
* // Check if key exists
|
|
274
|
+
* console.log(map.has('apple')); // true;
|
|
275
|
+
* console.log(map.has('date')); // false;
|
|
276
|
+
*
|
|
277
|
+
* // Get value by key
|
|
278
|
+
* console.log(map.get('banana')); // 2;
|
|
279
|
+
* console.log(map.get('grape')); // undefined;
|
|
280
|
+
*
|
|
281
|
+
* // Get all keys and values
|
|
282
|
+
* const keys = [...map.keys()];
|
|
283
|
+
* const values = [...map.values()];
|
|
284
|
+
* console.log(keys); // contains 'apple';
|
|
285
|
+
* console.log(values); // contains 3;
|
|
237
286
|
*/
|
|
238
287
|
get(key: K): V | undefined;
|
|
239
288
|
/**
|
|
@@ -241,6 +290,23 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
241
290
|
* @remarks Time O(1), Space O(1)
|
|
242
291
|
* @param key - Key to test.
|
|
243
292
|
* @returns True if present.
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
* @example
|
|
305
|
+
* // Check key existence
|
|
306
|
+
* const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
|
|
307
|
+
*
|
|
308
|
+
* console.log(map.has('a')); // true;
|
|
309
|
+
* console.log(map.has('z')); // false;
|
|
244
310
|
*/
|
|
245
311
|
has(key: K): boolean;
|
|
246
312
|
/**
|
|
@@ -248,6 +314,24 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
248
314
|
* @remarks Time O(1), Space O(1)
|
|
249
315
|
* @param key - Key to delete.
|
|
250
316
|
* @returns True if the key was found and removed.
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
* @example
|
|
329
|
+
* // Remove entries by key
|
|
330
|
+
* const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
|
|
331
|
+
*
|
|
332
|
+
* console.log(map.delete('y')); // true;
|
|
333
|
+
* console.log(map.has('y')); // false;
|
|
334
|
+
* console.log(map.size); // 2;
|
|
251
335
|
*/
|
|
252
336
|
delete(key: K): boolean;
|
|
253
337
|
/**
|
|
@@ -261,6 +345,21 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
261
345
|
* Deep clone this map, preserving hashing behavior.
|
|
262
346
|
* @remarks Time O(N), Space O(N)
|
|
263
347
|
* @returns A new map with the same content.
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
* @example
|
|
358
|
+
* // Create independent copy
|
|
359
|
+
* const map = new HashMap<string, number>([['a', 1]]);
|
|
360
|
+
* const copy = map.clone();
|
|
361
|
+
* copy.set('a', 99);
|
|
362
|
+
* console.log(map.get('a')); // 1;
|
|
264
363
|
*/
|
|
265
364
|
clone(): this;
|
|
266
365
|
/**
|
|
@@ -270,6 +369,24 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
270
369
|
* @param callbackfn - Mapping function (key, value, index, map) → newValue.
|
|
271
370
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
272
371
|
* @returns A new map with transformed values.
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
* @example
|
|
384
|
+
* // Transform all values
|
|
385
|
+
* const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
|
|
386
|
+
*
|
|
387
|
+
* const doubled = prices.map(v => (v ?? 0) * 2);
|
|
388
|
+
* console.log(doubled.get('apple')); // 2;
|
|
389
|
+
* console.log(doubled.get('banana')); // 4;
|
|
273
390
|
*/
|
|
274
391
|
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): any;
|
|
275
392
|
/**
|
|
@@ -278,6 +395,42 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
278
395
|
* @param predicate - Predicate (key, value, index, map) → boolean.
|
|
279
396
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
280
397
|
* @returns A new map containing entries that satisfied the predicate.
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
* @example
|
|
410
|
+
* // HashMap iteration and filter operations
|
|
411
|
+
* const map = new HashMap<number, string>([
|
|
412
|
+
* [1, 'Alice'],
|
|
413
|
+
* [2, 'Bob'],
|
|
414
|
+
* [3, 'Charlie'],
|
|
415
|
+
* [4, 'Diana'],
|
|
416
|
+
* [5, 'Eve']
|
|
417
|
+
* ]);
|
|
418
|
+
*
|
|
419
|
+
* // Iterate through entries
|
|
420
|
+
* const entries: [number, string][] = [];
|
|
421
|
+
* for (const [key, value] of map) {
|
|
422
|
+
* entries.push([key, value]);
|
|
423
|
+
* }
|
|
424
|
+
* console.log(entries); // length: 5;
|
|
425
|
+
*
|
|
426
|
+
* // Filter operation (for iteration with collection methods)
|
|
427
|
+
* const filtered = [...map].filter(([key]) => key > 2);
|
|
428
|
+
* console.log(filtered.length); // 3;
|
|
429
|
+
*
|
|
430
|
+
* // Map operation
|
|
431
|
+
* const values = [...map.values()].map(v => v.length);
|
|
432
|
+
* console.log(values); // contains 3; // 'Bob', 'Eve'
|
|
433
|
+
* console.log(values); // contains 7;
|
|
281
434
|
*/
|
|
282
435
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any;
|
|
283
436
|
/**
|