min-heap-typed 1.45.0 → 1.45.1
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 +58 -58
- package/dist/data-structures/hash/hash-map.js +73 -73
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +103 -103
- package/src/data-structures/hash/hash-table.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +8 -5
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- 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/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +18 -4
- package/src/utils/utils.ts +6 -6
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {BTNKey, TreeMultimapNodeNested, TreeMultimapOptions} from '../../types';
|
|
9
|
-
import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
|
|
10
|
-
import {IBinaryTree} from '../../interfaces';
|
|
11
|
-
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
|
-
|
|
13
|
-
export class TreeMultimapNode<
|
|
8
|
+
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType } from '../../types';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
+
|
|
13
|
+
export class TreeMultimapNode<
|
|
14
|
+
V = any,
|
|
15
|
+
N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>
|
|
16
|
+
> extends AVLTreeNode<V, N> {
|
|
14
17
|
count: number;
|
|
15
18
|
|
|
16
19
|
/**
|
|
@@ -292,7 +295,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
292
295
|
if (!parent) {
|
|
293
296
|
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
294
297
|
} else {
|
|
295
|
-
const {familyPosition: fp} = curr;
|
|
298
|
+
const { familyPosition: fp } = curr;
|
|
296
299
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
297
300
|
parent.left = curr.right;
|
|
298
301
|
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
@@ -320,7 +323,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
320
323
|
if (orgCurrent) this._count -= orgCurrent.count;
|
|
321
324
|
}
|
|
322
325
|
|
|
323
|
-
deletedResult.push({deleted: orgCurrent, needBalanced});
|
|
326
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
324
327
|
|
|
325
328
|
if (needBalanced) {
|
|
326
329
|
this._balancePath(needBalanced);
|
|
@@ -396,7 +399,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
396
399
|
srcNode = this.ensureNotKey(srcNode);
|
|
397
400
|
destNode = this.ensureNotKey(destNode);
|
|
398
401
|
if (srcNode && destNode) {
|
|
399
|
-
const {key, value, count, height} = destNode;
|
|
402
|
+
const { key, value, count, height } = destNode;
|
|
400
403
|
const tempNode = this.createNode(key, value, count);
|
|
401
404
|
if (tempNode) {
|
|
402
405
|
tempNode.height = height;
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {uuidV4} from '../../utils';
|
|
9
|
-
import {PriorityQueue} from '../priority-queue';
|
|
10
|
-
import type {DijkstraResult, VertexKey} from '../../types';
|
|
11
|
-
import {IGraph} from '../../interfaces';
|
|
12
|
-
import {Queue} from '../queue';
|
|
8
|
+
import { uuidV4 } from '../../utils';
|
|
9
|
+
import { PriorityQueue } from '../priority-queue';
|
|
10
|
+
import type { DijkstraResult, VertexKey } from '../../types';
|
|
11
|
+
import { IGraph } from '../../interfaces';
|
|
12
|
+
import { Queue } from '../queue';
|
|
13
13
|
|
|
14
14
|
export abstract class AbstractVertex<V = any> {
|
|
15
15
|
key: VertexKey;
|
|
@@ -300,11 +300,11 @@ export abstract class AbstractGraph<
|
|
|
300
300
|
return [];
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
const stack: {vertex: VO; path: VO[]}[] = [];
|
|
304
|
-
stack.push({vertex: vertex1, path: [vertex1]});
|
|
303
|
+
const stack: { vertex: VO; path: VO[] }[] = [];
|
|
304
|
+
stack.push({ vertex: vertex1, path: [vertex1] });
|
|
305
305
|
|
|
306
306
|
while (stack.length > 0) {
|
|
307
|
-
const {vertex, path} = stack.pop()!;
|
|
307
|
+
const { vertex, path } = stack.pop()!;
|
|
308
308
|
|
|
309
309
|
if (vertex === vertex2) {
|
|
310
310
|
paths.push(path);
|
|
@@ -315,7 +315,7 @@ export abstract class AbstractGraph<
|
|
|
315
315
|
for (const neighbor of neighbors) {
|
|
316
316
|
if (!path.includes(neighbor)) {
|
|
317
317
|
const newPath = [...path, neighbor];
|
|
318
|
-
stack.push({vertex: neighbor, path: newPath});
|
|
318
|
+
stack.push({ vertex: neighbor, path: newPath });
|
|
319
319
|
}
|
|
320
320
|
}
|
|
321
321
|
}
|
|
@@ -514,7 +514,12 @@ export abstract class AbstractGraph<
|
|
|
514
514
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
515
515
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
516
516
|
*/
|
|
517
|
-
dijkstraWithoutHeap(
|
|
517
|
+
dijkstraWithoutHeap(
|
|
518
|
+
src: VO | VertexKey,
|
|
519
|
+
dest?: VO | VertexKey | null,
|
|
520
|
+
getMinDist?: boolean,
|
|
521
|
+
genPaths?: boolean
|
|
522
|
+
): DijkstraResult<VO> {
|
|
518
523
|
if (getMinDist === undefined) getMinDist = false;
|
|
519
524
|
if (genPaths === undefined) genPaths = false;
|
|
520
525
|
|
|
@@ -586,7 +591,7 @@ export abstract class AbstractGraph<
|
|
|
586
591
|
if (genPaths) {
|
|
587
592
|
getPaths(destVertex);
|
|
588
593
|
}
|
|
589
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
594
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
590
595
|
}
|
|
591
596
|
const neighbors = this.getNeighbors(cur);
|
|
592
597
|
for (const neighbor of neighbors) {
|
|
@@ -609,18 +614,18 @@ export abstract class AbstractGraph<
|
|
|
609
614
|
}
|
|
610
615
|
|
|
611
616
|
getMinDist &&
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
}
|
|
617
|
+
distMap.forEach((d, v) => {
|
|
618
|
+
if (v !== srcVertex) {
|
|
619
|
+
if (d < minDist) {
|
|
620
|
+
minDist = d;
|
|
621
|
+
if (genPaths) minDest = v;
|
|
618
622
|
}
|
|
619
|
-
}
|
|
623
|
+
}
|
|
624
|
+
});
|
|
620
625
|
|
|
621
626
|
genPaths && getPaths(minDest);
|
|
622
627
|
|
|
623
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
628
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
624
629
|
}
|
|
625
630
|
|
|
626
631
|
/**
|
|
@@ -657,7 +662,12 @@ export abstract class AbstractGraph<
|
|
|
657
662
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
658
663
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
659
664
|
*/
|
|
660
|
-
dijkstra(
|
|
665
|
+
dijkstra(
|
|
666
|
+
src: VO | VertexKey,
|
|
667
|
+
dest?: VO | VertexKey | null,
|
|
668
|
+
getMinDist?: boolean,
|
|
669
|
+
genPaths?: boolean
|
|
670
|
+
): DijkstraResult<VO> {
|
|
661
671
|
if (getMinDist === undefined) getMinDist = false;
|
|
662
672
|
if (genPaths === undefined) genPaths = false;
|
|
663
673
|
|
|
@@ -681,8 +691,8 @@ export abstract class AbstractGraph<
|
|
|
681
691
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
682
692
|
}
|
|
683
693
|
|
|
684
|
-
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
685
|
-
heap.add({key: 0, value: srcVertex});
|
|
694
|
+
const heap = new PriorityQueue<{ key: number; value: VO }>({ comparator: (a, b) => a.key - b.key });
|
|
695
|
+
heap.add({ key: 0, value: srcVertex });
|
|
686
696
|
|
|
687
697
|
distMap.set(srcVertex, 0);
|
|
688
698
|
preMap.set(srcVertex, null);
|
|
@@ -723,7 +733,7 @@ export abstract class AbstractGraph<
|
|
|
723
733
|
if (genPaths) {
|
|
724
734
|
getPaths(destVertex);
|
|
725
735
|
}
|
|
726
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
736
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
727
737
|
}
|
|
728
738
|
const neighbors = this.getNeighbors(cur);
|
|
729
739
|
for (const neighbor of neighbors) {
|
|
@@ -733,7 +743,7 @@ export abstract class AbstractGraph<
|
|
|
733
743
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
734
744
|
if (distSrcToNeighbor) {
|
|
735
745
|
if (dist + weight < distSrcToNeighbor) {
|
|
736
|
-
heap.add({key: dist + weight, value: neighbor});
|
|
746
|
+
heap.add({ key: dist + weight, value: neighbor });
|
|
737
747
|
preMap.set(neighbor, cur);
|
|
738
748
|
distMap.set(neighbor, dist + weight);
|
|
739
749
|
}
|
|
@@ -760,7 +770,7 @@ export abstract class AbstractGraph<
|
|
|
760
770
|
getPaths(minDest);
|
|
761
771
|
}
|
|
762
772
|
|
|
763
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
773
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
764
774
|
}
|
|
765
775
|
|
|
766
776
|
/**
|
|
@@ -800,7 +810,7 @@ export abstract class AbstractGraph<
|
|
|
800
810
|
// TODO
|
|
801
811
|
let hasNegativeCycle: boolean | undefined;
|
|
802
812
|
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
803
|
-
if (!srcVertex) return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
813
|
+
if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
804
814
|
|
|
805
815
|
const vertices = this._vertices;
|
|
806
816
|
const numOfVertices = vertices.size;
|
|
@@ -872,7 +882,7 @@ export abstract class AbstractGraph<
|
|
|
872
882
|
}
|
|
873
883
|
}
|
|
874
884
|
|
|
875
|
-
return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
885
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
876
886
|
}
|
|
877
887
|
|
|
878
888
|
/**
|
|
@@ -913,7 +923,7 @@ export abstract class AbstractGraph<
|
|
|
913
923
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
914
924
|
* path between vertices in the
|
|
915
925
|
*/
|
|
916
|
-
floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
926
|
+
floydWarshall(): { costs: number[][]; predecessor: (VO | null)[][] } {
|
|
917
927
|
const idAndVertices = [...this._vertices];
|
|
918
928
|
const n = idAndVertices.length;
|
|
919
929
|
|
|
@@ -945,7 +955,7 @@ export abstract class AbstractGraph<
|
|
|
945
955
|
}
|
|
946
956
|
}
|
|
947
957
|
}
|
|
948
|
-
return {costs, predecessor};
|
|
958
|
+
return { costs, predecessor };
|
|
949
959
|
}
|
|
950
960
|
|
|
951
961
|
/**
|
|
@@ -982,7 +992,12 @@ export abstract class AbstractGraph<
|
|
|
982
992
|
* are arrays of vertices that form cycles within the SCCs.
|
|
983
993
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
984
994
|
*/
|
|
985
|
-
tarjan(
|
|
995
|
+
tarjan(
|
|
996
|
+
needCutVertexes: boolean = false,
|
|
997
|
+
needBridges: boolean = false,
|
|
998
|
+
needSCCs: boolean = true,
|
|
999
|
+
needCycles: boolean = false
|
|
1000
|
+
) {
|
|
986
1001
|
// !! in undirected graph we will not let child visit parent when dfs
|
|
987
1002
|
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
988
1003
|
// !! bridge: low(child) > dfn(cur)
|
|
@@ -1081,7 +1096,7 @@ export abstract class AbstractGraph<
|
|
|
1081
1096
|
});
|
|
1082
1097
|
}
|
|
1083
1098
|
|
|
1084
|
-
return {dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles};
|
|
1099
|
+
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
1085
1100
|
}
|
|
1086
1101
|
|
|
1087
1102
|
/**
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {arrayRemove} from '../../utils';
|
|
9
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
-
import type {TopologicalStatus, VertexKey} from '../../types';
|
|
11
|
-
import {IGraph} from '../../interfaces';
|
|
8
|
+
import { arrayRemove } from '../../utils';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
|
+
import type { TopologicalStatus, VertexKey } from '../../types';
|
|
11
|
+
import { IGraph } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -45,7 +45,12 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export class DirectedGraph<
|
|
48
|
+
export class DirectedGraph<
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
49
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
50
55
|
implements IGraph<V, E, VO, EO> {
|
|
51
56
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {MapGraphCoordinate, VertexKey} from '../../types';
|
|
2
|
-
import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
|
|
1
|
+
import { MapGraphCoordinate, VertexKey } from '../../types';
|
|
2
|
+
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
5
5
|
lat: number;
|
|
@@ -40,12 +40,12 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export class MapGraph<
|
|
44
|
-
V,
|
|
45
|
-
E,
|
|
46
|
-
VO
|
|
47
|
-
EO
|
|
48
|
-
> {
|
|
43
|
+
export class MapGraph<
|
|
44
|
+
V = any,
|
|
45
|
+
E = any,
|
|
46
|
+
VO extends MapVertex<V> = MapVertex<V>,
|
|
47
|
+
EO extends MapEdge<E> = MapEdge<E>
|
|
48
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
49
49
|
/**
|
|
50
50
|
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
51
51
|
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {arrayRemove} from '../../utils';
|
|
9
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
-
import type {VertexKey} from '../../types';
|
|
11
|
-
import {IGraph} from '../../interfaces';
|
|
8
|
+
import { arrayRemove } from '../../utils';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
|
+
import type { VertexKey } from '../../types';
|
|
11
|
+
import { IGraph } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -43,11 +43,11 @@ 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
52
|
implements IGraph<V, E, VO, EO> {
|
|
53
53
|
/**
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {isObjOrFunc, rangeCheck, throwRangeError} from
|
|
10
|
-
import {HashMapLinkedNode, HashMapOptions, IterateDirection} from
|
|
9
|
+
import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils';
|
|
10
|
+
import { HashMapLinkedNode, HashMapOptions, IterateDirection } from '../../types';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
@@ -16,9 +16,8 @@ import {HashMapLinkedNode, HashMapOptions, IterateDirection} from "../../types";
|
|
|
16
16
|
*/
|
|
17
17
|
export class HashMapIterator<K, V> {
|
|
18
18
|
readonly hashMap: HashMap<K, V>;
|
|
19
|
-
|
|
20
|
-
protected _node: HashMapLinkedNode<K, V>;
|
|
21
19
|
readonly iterateDirection: IterateDirection;
|
|
20
|
+
protected _node: HashMapLinkedNode<K, V>;
|
|
22
21
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
23
22
|
|
|
24
23
|
/**
|
|
@@ -36,13 +35,16 @@ export class HashMapIterator<K, V> {
|
|
|
36
35
|
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
37
36
|
* methods of the object being created.
|
|
38
37
|
*/
|
|
39
|
-
constructor(
|
|
40
|
-
|
|
38
|
+
constructor(
|
|
39
|
+
node: HashMapLinkedNode<K, V>,
|
|
40
|
+
sentinel: HashMapLinkedNode<K, V>,
|
|
41
|
+
hashMap: HashMap<K, V>,
|
|
42
|
+
iterateDirection: IterateDirection = IterateDirection.DEFAULT
|
|
43
|
+
) {
|
|
41
44
|
this._node = node;
|
|
42
45
|
this._sentinel = sentinel;
|
|
43
46
|
this.iterateDirection = iterateDirection;
|
|
44
47
|
|
|
45
|
-
|
|
46
48
|
if (this.iterateDirection === IterateDirection.DEFAULT) {
|
|
47
49
|
this.prev = function () {
|
|
48
50
|
if (this._node.prev === this._sentinel) {
|
|
@@ -87,7 +89,7 @@ export class HashMapIterator<K, V> {
|
|
|
87
89
|
throwRangeError();
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
return new Proxy(<[K, V]
|
|
92
|
+
return new Proxy(<[K, V]>(<unknown>[]), {
|
|
91
93
|
get: (target, prop: '0' | '1') => {
|
|
92
94
|
if (prop === '0') return this._node.key;
|
|
93
95
|
else if (prop === '1') return this._node.value;
|
|
@@ -113,7 +115,6 @@ export class HashMapIterator<K, V> {
|
|
|
113
115
|
return this._node !== this._sentinel;
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
|
|
117
118
|
prev() {
|
|
118
119
|
return this;
|
|
119
120
|
}
|
|
@@ -124,18 +125,12 @@ export class HashMapIterator<K, V> {
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
export class HashMap<K = any, V = any> {
|
|
128
|
+
readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
127
129
|
protected _nodes: HashMapLinkedNode<K, V>[] = [];
|
|
128
130
|
protected _orgMap: Record<string, HashMapLinkedNode<K, V>> = {};
|
|
129
131
|
protected _head: HashMapLinkedNode<K, V>;
|
|
130
132
|
protected _tail: HashMapLinkedNode<K, V>;
|
|
131
133
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
132
|
-
readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
133
|
-
|
|
134
|
-
protected _size = 0;
|
|
135
|
-
|
|
136
|
-
get size() {
|
|
137
|
-
return this._size;
|
|
138
|
-
}
|
|
139
134
|
|
|
140
135
|
/**
|
|
141
136
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
@@ -151,7 +146,85 @@ export class HashMap<K = any, V = any> {
|
|
|
151
146
|
hashMap.forEach(el => {
|
|
152
147
|
this.set(el[0], el[1]);
|
|
153
148
|
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected _size = 0;
|
|
152
|
+
|
|
153
|
+
get size() {
|
|
154
|
+
return this._size;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(1)
|
|
159
|
+
* Space Complexity: O(1)
|
|
160
|
+
*
|
|
161
|
+
* The function returns a new iterator object for a HashMap.
|
|
162
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
163
|
+
*/
|
|
164
|
+
get begin() {
|
|
165
|
+
return new HashMapIterator<K, V>(this._head, this._sentinel, this);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(1)
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*
|
|
172
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
173
|
+
* end values.
|
|
174
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
175
|
+
*/
|
|
176
|
+
get end() {
|
|
177
|
+
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this);
|
|
178
|
+
}
|
|
154
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(1)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
184
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
185
|
+
* a HashMap in reverse order.
|
|
186
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
187
|
+
*/
|
|
188
|
+
get reverseBegin() {
|
|
189
|
+
return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(1)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
196
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
197
|
+
* HashMap in reverse order.
|
|
198
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
199
|
+
*/
|
|
200
|
+
get reverseEnd() {
|
|
201
|
+
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(1)
|
|
206
|
+
* Space Complexity: O(1)
|
|
207
|
+
*
|
|
208
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
209
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
210
|
+
* value (V).
|
|
211
|
+
*/
|
|
212
|
+
get front() {
|
|
213
|
+
if (this._size === 0) return;
|
|
214
|
+
return <[K, V]>[this._head.key, this._head.value];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Time Complexity: O(1)
|
|
219
|
+
* Space Complexity: O(1)
|
|
220
|
+
*
|
|
221
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
222
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
223
|
+
* data structure.
|
|
224
|
+
*/
|
|
225
|
+
get back() {
|
|
226
|
+
if (this._size === 0) return;
|
|
227
|
+
return <[K, V]>[this._tail.key, this._tail.value];
|
|
155
228
|
}
|
|
156
229
|
|
|
157
230
|
/**
|
|
@@ -170,7 +243,7 @@ export class HashMap<K = any, V = any> {
|
|
|
170
243
|
set(key: K, value?: V, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
171
244
|
let newTail;
|
|
172
245
|
if (isObjectKey) {
|
|
173
|
-
const index = (<Record<symbol, number
|
|
246
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
174
247
|
if (index !== undefined) {
|
|
175
248
|
this._nodes[<number>index].value = <V>value;
|
|
176
249
|
return this._size;
|
|
@@ -187,12 +260,12 @@ export class HashMap<K = any, V = any> {
|
|
|
187
260
|
};
|
|
188
261
|
this._nodes.push(newTail);
|
|
189
262
|
} else {
|
|
190
|
-
const node = this._orgMap[<string
|
|
263
|
+
const node = this._orgMap[<string>(<unknown>key)];
|
|
191
264
|
if (node) {
|
|
192
265
|
node.value = <V>value;
|
|
193
266
|
return this._size;
|
|
194
267
|
}
|
|
195
|
-
this._orgMap[<string
|
|
268
|
+
this._orgMap[<string>(<unknown>key)] = newTail = {
|
|
196
269
|
key: key,
|
|
197
270
|
value: <V>value,
|
|
198
271
|
prev: this._tail,
|
|
@@ -228,10 +301,10 @@ export class HashMap<K = any, V = any> {
|
|
|
228
301
|
*/
|
|
229
302
|
get(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
230
303
|
if (isObjectKey) {
|
|
231
|
-
const index = (<Record<symbol, number
|
|
304
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
232
305
|
return index !== undefined ? this._nodes[index].value : undefined;
|
|
233
306
|
}
|
|
234
|
-
const node = this._orgMap[<string
|
|
307
|
+
const node = this._orgMap[<string>(<unknown>key)];
|
|
235
308
|
return node ? node.value : undefined;
|
|
236
309
|
}
|
|
237
310
|
|
|
@@ -269,16 +342,16 @@ export class HashMap<K = any, V = any> {
|
|
|
269
342
|
* @returns a new instance of the `HashMapIterator` class.
|
|
270
343
|
*/
|
|
271
344
|
getIterator(key: K, isObjectKey?: boolean) {
|
|
272
|
-
let node: HashMapLinkedNode<K, V
|
|
345
|
+
let node: HashMapLinkedNode<K, V>;
|
|
273
346
|
if (isObjectKey) {
|
|
274
|
-
const index = (<Record<symbol, number
|
|
347
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
275
348
|
if (index === undefined) {
|
|
276
|
-
node = this._sentinel
|
|
349
|
+
node = this._sentinel;
|
|
277
350
|
} else {
|
|
278
351
|
node = this._nodes[index];
|
|
279
352
|
}
|
|
280
353
|
} else {
|
|
281
|
-
node = this._orgMap[<string
|
|
354
|
+
node = this._orgMap[<string>(<unknown>key)] || this._sentinel;
|
|
282
355
|
}
|
|
283
356
|
return new HashMapIterator<K, V>(node, this._sentinel, this);
|
|
284
357
|
}
|
|
@@ -299,15 +372,15 @@ export class HashMap<K = any, V = any> {
|
|
|
299
372
|
delete(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
|
|
300
373
|
let node;
|
|
301
374
|
if (isObjectKey) {
|
|
302
|
-
const index = (<Record<symbol, number
|
|
375
|
+
const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
303
376
|
if (index === undefined) return false;
|
|
304
|
-
delete (<Record<symbol, number
|
|
377
|
+
delete (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
|
|
305
378
|
node = this._nodes[index];
|
|
306
379
|
delete this._nodes[index];
|
|
307
380
|
} else {
|
|
308
|
-
node = this._orgMap[<string
|
|
381
|
+
node = this._orgMap[<string>(<unknown>key)];
|
|
309
382
|
if (node === undefined) return false;
|
|
310
|
-
delete this._orgMap[<string
|
|
383
|
+
delete this._orgMap[<string>(<unknown>key)];
|
|
311
384
|
}
|
|
312
385
|
this._deleteNode(node);
|
|
313
386
|
return true;
|
|
@@ -362,79 +435,6 @@ export class HashMap<K = any, V = any> {
|
|
|
362
435
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
363
436
|
}
|
|
364
437
|
|
|
365
|
-
/**
|
|
366
|
-
* Time Complexity: O(1)
|
|
367
|
-
* Space Complexity: O(1)
|
|
368
|
-
*
|
|
369
|
-
* The function returns a new iterator object for a HashMap.
|
|
370
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
371
|
-
*/
|
|
372
|
-
get begin() {
|
|
373
|
-
return new HashMapIterator<K, V>(this._head, this._sentinel, this);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Time Complexity: O(1)
|
|
378
|
-
* Space Complexity: O(1)
|
|
379
|
-
*
|
|
380
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
381
|
-
* end values.
|
|
382
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
383
|
-
*/
|
|
384
|
-
get end() {
|
|
385
|
-
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Time Complexity: O(1)
|
|
390
|
-
* Space Complexity: O(1)
|
|
391
|
-
*
|
|
392
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
393
|
-
* a HashMap in reverse order.
|
|
394
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
395
|
-
*/
|
|
396
|
-
get reverseBegin() {
|
|
397
|
-
return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Time Complexity: O(1)
|
|
402
|
-
* Space Complexity: O(1)
|
|
403
|
-
*
|
|
404
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
405
|
-
* HashMap in reverse order.
|
|
406
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
407
|
-
*/
|
|
408
|
-
get reverseEnd() {
|
|
409
|
-
return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Time Complexity: O(1)
|
|
414
|
-
* Space Complexity: O(1)
|
|
415
|
-
*
|
|
416
|
-
* The function returns the key-value pair at the front of a data structure.
|
|
417
|
-
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
418
|
-
* value (V).
|
|
419
|
-
*/
|
|
420
|
-
get front() {
|
|
421
|
-
if (this._size === 0) return;
|
|
422
|
-
return <[K, V]>[this._head.key, this._head.value];
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Time Complexity: O(1)
|
|
427
|
-
* Space Complexity: O(1)
|
|
428
|
-
*
|
|
429
|
-
* The function returns the key-value pair at the end of a data structure.
|
|
430
|
-
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
431
|
-
* data structure.
|
|
432
|
-
*/
|
|
433
|
-
get back() {
|
|
434
|
-
if (this._size === 0) return;
|
|
435
|
-
return <[K, V]>[this._tail.key, this._tail.value];
|
|
436
|
-
}
|
|
437
|
-
|
|
438
438
|
/**
|
|
439
439
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
440
440
|
* Space Complexity: O(1)
|
|
@@ -478,7 +478,7 @@ export class HashMap<K = any, V = any> {
|
|
|
478
478
|
* and next nodes in the list.
|
|
479
479
|
*/
|
|
480
480
|
protected _deleteNode(node: HashMapLinkedNode<K, V>) {
|
|
481
|
-
const {prev, next} = node;
|
|
481
|
+
const { prev, next } = node;
|
|
482
482
|
prev.next = next;
|
|
483
483
|
next.prev = prev;
|
|
484
484
|
if (node === this._head) {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeMap {
|
|
1
|
+
export class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeSet {
|
|
1
|
+
export class TreeSet {
|
|
2
|
+
}
|