data-structure-typed 1.49.4 → 1.49.5
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/CHANGELOG.md +1 -1
- package/README.md +1 -3
- package/README_zh-CN.md +1 -1
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +356 -524
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +15 -8
- package/src/data-structures/binary-tree/binary-tree.ts +57 -74
- package/src/data-structures/binary-tree/bst.ts +16 -13
- package/src/data-structures/binary-tree/rb-tree.ts +16 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +7 -2
- package/src/types/common.ts +4 -4
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -11
- package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/queue/deque.test.ts +6 -7
- package/test/performance/data-structures/queue/queue.test.ts +10 -9
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +59 -24
- package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
- package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
- package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -45,11 +45,14 @@ export class TreeMultimapNode<
|
|
|
45
45
|
/**
|
|
46
46
|
* 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.
|
|
47
47
|
*/
|
|
48
|
-
export class TreeMultimap<
|
|
49
|
-
|
|
48
|
+
export class TreeMultimap<
|
|
49
|
+
K = any,
|
|
50
|
+
V = any,
|
|
51
|
+
N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
|
|
52
|
+
TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>
|
|
53
|
+
>
|
|
50
54
|
extends AVLTree<K, V, N, TREE>
|
|
51
55
|
implements IBinaryTree<K, V, N, TREE> {
|
|
52
|
-
|
|
53
56
|
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
|
54
57
|
super([], options);
|
|
55
58
|
if (elements) this.addMany(elements);
|
|
@@ -60,7 +63,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
60
63
|
// TODO the _count is not accurate after nodes count modified
|
|
61
64
|
get count(): number {
|
|
62
65
|
let sum = 0;
|
|
63
|
-
this.subTreeTraverse(node => sum += node.count);
|
|
66
|
+
this.subTreeTraverse(node => (sum += node.count));
|
|
64
67
|
return sum;
|
|
65
68
|
}
|
|
66
69
|
|
|
@@ -80,7 +83,8 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
80
83
|
override createTree(options?: TreeMultimapOptions<K>): TREE {
|
|
81
84
|
return new TreeMultimap<K, V, N, TREE>([], {
|
|
82
85
|
iterationType: this.iterationType,
|
|
83
|
-
variant: this.variant,
|
|
86
|
+
variant: this.variant,
|
|
87
|
+
...options
|
|
84
88
|
}) as TREE;
|
|
85
89
|
}
|
|
86
90
|
|
|
@@ -101,7 +105,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
101
105
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
102
106
|
*/
|
|
103
107
|
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
104
|
-
return !(potentialKey instanceof TreeMultimapNode)
|
|
108
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
105
109
|
}
|
|
106
110
|
|
|
107
111
|
/**
|
|
@@ -357,47 +361,6 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
357
361
|
return cloned;
|
|
358
362
|
}
|
|
359
363
|
|
|
360
|
-
/**
|
|
361
|
-
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
362
|
-
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
363
|
-
*
|
|
364
|
-
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
365
|
-
* given parent node.
|
|
366
|
-
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
367
|
-
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
368
|
-
* `undefined` if there is no node to add.
|
|
369
|
-
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
370
|
-
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
371
|
-
* (`K`).
|
|
372
|
-
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
373
|
-
* added, or `undefined` if no node was added.
|
|
374
|
-
*/
|
|
375
|
-
protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined {
|
|
376
|
-
parent = this.ensureNode(parent);
|
|
377
|
-
if (parent) {
|
|
378
|
-
if (parent.left === undefined) {
|
|
379
|
-
parent.left = newNode;
|
|
380
|
-
if (newNode !== undefined) {
|
|
381
|
-
this._size = this.size + 1;
|
|
382
|
-
this._count += newNode.count;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
return parent.left;
|
|
386
|
-
} else if (parent.right === undefined) {
|
|
387
|
-
parent.right = newNode;
|
|
388
|
-
if (newNode !== undefined) {
|
|
389
|
-
this._size = this.size + 1;
|
|
390
|
-
this._count += newNode.count;
|
|
391
|
-
}
|
|
392
|
-
return parent.right;
|
|
393
|
-
} else {
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
} else {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
364
|
/**
|
|
402
365
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
403
366
|
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
@@ -433,7 +396,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
433
396
|
}
|
|
434
397
|
|
|
435
398
|
protected _replaceNode(oldNode: N, newNode: N): N {
|
|
436
|
-
newNode.count = oldNode.count + newNode.count
|
|
399
|
+
newNode.count = oldNode.count + newNode.count;
|
|
437
400
|
return super._replaceNode(oldNode, newNode);
|
|
438
401
|
}
|
|
439
402
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
9
9
|
import { uuidV4 } from '../../utils';
|
|
10
|
-
import { IterableEntryBase } from
|
|
10
|
+
import { IterableEntryBase } from '../base';
|
|
11
11
|
import { IGraph } from '../../interfaces';
|
|
12
12
|
import { Heap } from '../heap';
|
|
13
13
|
import { Queue } from '../queue';
|
|
@@ -65,7 +65,9 @@ export abstract class AbstractGraph<
|
|
|
65
65
|
E = any,
|
|
66
66
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
67
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
-
>
|
|
68
|
+
>
|
|
69
|
+
extends IterableEntryBase<VertexKey, V | undefined>
|
|
70
|
+
implements IGraph<V, E, VO, EO> {
|
|
69
71
|
constructor() {
|
|
70
72
|
super();
|
|
71
73
|
}
|
|
@@ -165,7 +167,7 @@ export abstract class AbstractGraph<
|
|
|
165
167
|
|
|
166
168
|
isVertexKey(potentialKey: any): potentialKey is VertexKey {
|
|
167
169
|
const potentialKeyType = typeof potentialKey;
|
|
168
|
-
return potentialKeyType ===
|
|
170
|
+
return potentialKeyType === 'string' || potentialKeyType === 'number';
|
|
169
171
|
}
|
|
170
172
|
|
|
171
173
|
/**
|
|
@@ -662,7 +664,6 @@ export abstract class AbstractGraph<
|
|
|
662
664
|
getMinDist: boolean = false,
|
|
663
665
|
genPaths: boolean = false
|
|
664
666
|
): DijkstraResult<VO> {
|
|
665
|
-
|
|
666
667
|
let minDist = Infinity;
|
|
667
668
|
let minDest: VO | undefined = undefined;
|
|
668
669
|
let minPath: VO[] = [];
|
|
@@ -1170,7 +1171,10 @@ export abstract class AbstractGraph<
|
|
|
1170
1171
|
|
|
1171
1172
|
const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
|
|
1172
1173
|
if (visited.has(vertex)) {
|
|
1173
|
-
if (
|
|
1174
|
+
if (
|
|
1175
|
+
((!isInclude2Cycle && currentPath.length > 2) || (isInclude2Cycle && currentPath.length >= 2)) &&
|
|
1176
|
+
currentPath[0] === vertex.key
|
|
1177
|
+
) {
|
|
1174
1178
|
cycles.push([...currentPath]);
|
|
1175
1179
|
}
|
|
1176
1180
|
return;
|
|
@@ -1195,18 +1199,16 @@ export abstract class AbstractGraph<
|
|
|
1195
1199
|
const uniqueCycles = new Map<string, VertexKey[]>();
|
|
1196
1200
|
|
|
1197
1201
|
for (const cycle of cycles) {
|
|
1198
|
-
const sorted = [...cycle].sort().toString()
|
|
1202
|
+
const sorted = [...cycle].sort().toString();
|
|
1199
1203
|
|
|
1200
|
-
if (uniqueCycles.has(sorted)) continue
|
|
1204
|
+
if (uniqueCycles.has(sorted)) continue;
|
|
1201
1205
|
else {
|
|
1202
|
-
uniqueCycles.set(sorted, cycle)
|
|
1206
|
+
uniqueCycles.set(sorted, cycle);
|
|
1203
1207
|
}
|
|
1204
1208
|
}
|
|
1205
1209
|
|
|
1206
1210
|
// Convert the unique cycles back to an array
|
|
1207
|
-
return [...uniqueCycles].map(cycleString =>
|
|
1208
|
-
cycleString[1]
|
|
1209
|
-
);
|
|
1211
|
+
return [...uniqueCycles].map(cycleString => cycleString[1]);
|
|
1210
1212
|
}
|
|
1211
1213
|
|
|
1212
1214
|
/**
|
|
@@ -182,7 +182,6 @@ export class DirectedGraph<
|
|
|
182
182
|
* Space Complexity: O(1)
|
|
183
183
|
*/
|
|
184
184
|
|
|
185
|
-
|
|
186
185
|
/**
|
|
187
186
|
* Time Complexity: O(E) where E is the number of edgeMap
|
|
188
187
|
* Space Complexity: O(1)
|
|
@@ -248,7 +247,7 @@ export class DirectedGraph<
|
|
|
248
247
|
vertexKey = vertexOrKey;
|
|
249
248
|
} else {
|
|
250
249
|
vertex = vertexOrKey;
|
|
251
|
-
vertexKey = this._getVertexKey(vertexOrKey)
|
|
250
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
252
251
|
}
|
|
253
252
|
|
|
254
253
|
if (vertex) {
|
|
@@ -600,7 +599,6 @@ export class DirectedGraph<
|
|
|
600
599
|
}
|
|
601
600
|
}
|
|
602
601
|
|
|
603
|
-
|
|
604
602
|
/**
|
|
605
603
|
* Time Complexity: O(1)
|
|
606
604
|
* Space Complexity: O(1)
|
|
@@ -84,7 +84,12 @@ export class MapGraph<
|
|
|
84
84
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
85
85
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
|
86
86
|
*/
|
|
87
|
-
override createVertex(
|
|
87
|
+
override createVertex(
|
|
88
|
+
key: VertexKey,
|
|
89
|
+
value?: V,
|
|
90
|
+
lat: number = this.originCoord[0],
|
|
91
|
+
long: number = this.originCoord[1]
|
|
92
|
+
): VO {
|
|
88
93
|
return new MapVertex(key, value, lat, long) as VO;
|
|
89
94
|
}
|
|
90
95
|
|
|
@@ -162,7 +162,6 @@ export class UndirectedGraph<
|
|
|
162
162
|
* Space Complexity: O(1)
|
|
163
163
|
*/
|
|
164
164
|
|
|
165
|
-
|
|
166
165
|
/**
|
|
167
166
|
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
168
167
|
* Space Complexity: O(1)
|
|
@@ -192,7 +191,6 @@ export class UndirectedGraph<
|
|
|
192
191
|
|
|
193
192
|
if (oneSide && otherSide) {
|
|
194
193
|
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
195
|
-
|
|
196
194
|
} else {
|
|
197
195
|
return;
|
|
198
196
|
}
|
|
@@ -220,10 +218,10 @@ export class UndirectedGraph<
|
|
|
220
218
|
vertexKey = vertexOrKey;
|
|
221
219
|
} else {
|
|
222
220
|
vertex = vertexOrKey;
|
|
223
|
-
vertexKey = this._getVertexKey(vertexOrKey)
|
|
221
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
224
222
|
}
|
|
225
223
|
|
|
226
|
-
const neighbors = this.getNeighbors(vertexOrKey)
|
|
224
|
+
const neighbors = this.getNeighbors(vertexOrKey);
|
|
227
225
|
|
|
228
226
|
if (vertex) {
|
|
229
227
|
neighbors.forEach(neighbor => {
|
|
@@ -234,9 +232,8 @@ export class UndirectedGraph<
|
|
|
234
232
|
});
|
|
235
233
|
this._edgeMap.set(neighbor, restEdges);
|
|
236
234
|
}
|
|
237
|
-
})
|
|
235
|
+
});
|
|
238
236
|
this._edgeMap.delete(vertex);
|
|
239
|
-
|
|
240
237
|
}
|
|
241
238
|
|
|
242
239
|
return this._vertexMap.delete(vertexKey);
|
|
@@ -27,9 +27,12 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
27
27
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
28
28
|
* configuration options for the constructor. In this case, it has one property:
|
|
29
29
|
*/
|
|
30
|
-
constructor(
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
constructor(
|
|
31
|
+
elements: Iterable<[K, V]> = [],
|
|
32
|
+
options?: {
|
|
33
|
+
hashFn: (key: K) => string;
|
|
34
|
+
}
|
|
35
|
+
) {
|
|
33
36
|
super();
|
|
34
37
|
if (options) {
|
|
35
38
|
const { hashFn } = options;
|
|
@@ -73,7 +76,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
73
76
|
this._size++;
|
|
74
77
|
}
|
|
75
78
|
this._objMap.set(key, value);
|
|
76
|
-
|
|
77
79
|
} else {
|
|
78
80
|
const strKey = this._getNoObjKey(key);
|
|
79
81
|
if (this._store[strKey] === undefined) {
|
|
@@ -137,7 +139,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
137
139
|
delete(key: K): boolean {
|
|
138
140
|
if (this._isObjKey(key)) {
|
|
139
141
|
if (this._objMap.has(key)) {
|
|
140
|
-
this._size
|
|
142
|
+
this._size--;
|
|
141
143
|
}
|
|
142
144
|
|
|
143
145
|
return this._objMap.delete(key);
|
|
@@ -235,19 +237,19 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
235
237
|
|
|
236
238
|
protected _hashFn: (key: K) => string = (key: K) => String(key);
|
|
237
239
|
|
|
238
|
-
protected _isObjKey(key: any): key is
|
|
240
|
+
protected _isObjKey(key: any): key is object | ((...args: any[]) => any) {
|
|
239
241
|
const keyType = typeof key;
|
|
240
|
-
return (keyType === 'object' || keyType === 'function') && key !== null
|
|
242
|
+
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
241
243
|
}
|
|
242
244
|
|
|
243
245
|
protected _getNoObjKey(key: K): string {
|
|
244
246
|
const keyType = typeof key;
|
|
245
247
|
|
|
246
248
|
let strKey: string;
|
|
247
|
-
if (keyType !==
|
|
249
|
+
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
248
250
|
strKey = this._hashFn(key);
|
|
249
251
|
} else {
|
|
250
|
-
if (keyType ===
|
|
252
|
+
if (keyType === 'number') {
|
|
251
253
|
// TODO numeric key should has its own hash
|
|
252
254
|
strKey = <string>key;
|
|
253
255
|
} else {
|
|
@@ -264,7 +266,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
264
266
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
265
267
|
*/
|
|
266
268
|
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
267
|
-
|
|
268
269
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
269
270
|
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
|
|
270
271
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
@@ -273,12 +274,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
273
274
|
protected _hashFn: (key: K) => string;
|
|
274
275
|
protected _objHashFn: (key: K) => object;
|
|
275
276
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
277
|
+
constructor(
|
|
278
|
+
elements?: Iterable<[K, V]>,
|
|
279
|
+
options: HashMapOptions<K> = {
|
|
280
|
+
hashFn: (key: K) => String(key),
|
|
281
|
+
objHashFn: (key: K) => <object>key
|
|
282
|
+
}
|
|
283
|
+
) {
|
|
282
284
|
super();
|
|
283
285
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
284
286
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
@@ -31,13 +31,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
31
31
|
} else {
|
|
32
32
|
return a - b;
|
|
33
33
|
}
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
if (options) {
|
|
36
|
-
this.options = options
|
|
36
|
+
this.options = options;
|
|
37
37
|
} else {
|
|
38
38
|
this.options = {
|
|
39
39
|
comparator: defaultComparator
|
|
40
|
-
}
|
|
40
|
+
};
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
if (elements) {
|
|
@@ -225,7 +225,8 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
225
225
|
|
|
226
226
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
227
227
|
const _dfs = (index: number) => {
|
|
228
|
-
const left = 2 * index + 1,
|
|
228
|
+
const left = 2 * index + 1,
|
|
229
|
+
right = left + 1;
|
|
229
230
|
if (index < this.size) {
|
|
230
231
|
if (order === 'in') {
|
|
231
232
|
_dfs(left);
|
|
@@ -380,7 +381,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
380
381
|
* original Heap.
|
|
381
382
|
*/
|
|
382
383
|
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
|
|
383
|
-
|
|
384
384
|
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
|
|
385
385
|
let index = 0;
|
|
386
386
|
for (const el of this) {
|
|
@@ -432,13 +432,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
432
432
|
protected _sinkDown(index: number, halfLength: number): boolean {
|
|
433
433
|
const element = this.elements[index];
|
|
434
434
|
while (index < halfLength) {
|
|
435
|
-
let left = index << 1 | 1;
|
|
435
|
+
let left = (index << 1) | 1;
|
|
436
436
|
const right = left + 1;
|
|
437
437
|
let minItem = this.elements[left];
|
|
438
|
-
if (
|
|
439
|
-
right < this.elements.length &&
|
|
440
|
-
this.options.comparator(minItem, this.elements[right]) > 0
|
|
441
|
-
) {
|
|
438
|
+
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
442
439
|
left = right;
|
|
443
440
|
minItem = this.elements[right];
|
|
444
441
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback } from
|
|
9
|
-
import { IterableElementBase } from
|
|
8
|
+
import type { ElementCallback } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
export class SinglyLinkedListNode<E = any> {
|
|
12
12
|
value: E;
|
|
@@ -715,7 +715,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
715
715
|
return filteredList;
|
|
716
716
|
}
|
|
717
717
|
|
|
718
|
-
|
|
719
718
|
/**
|
|
720
719
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
721
720
|
* Space Complexity: O(n)
|