min-heap-typed 1.39.6 → 1.40.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/dist/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- 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 +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- 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 +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- 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 +2 -2
|
@@ -12,6 +12,9 @@ import {IGraph} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export abstract class AbstractVertex<V = any> {
|
|
15
|
+
key: VertexKey;
|
|
16
|
+
value: V | undefined;
|
|
17
|
+
|
|
15
18
|
/**
|
|
16
19
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
17
20
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
@@ -20,32 +23,16 @@ export abstract class AbstractVertex<V = any> {
|
|
|
20
23
|
* vertex. If no value is provided, it will be set to undefined.
|
|
21
24
|
*/
|
|
22
25
|
protected constructor(key: VertexKey, value?: V) {
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
private _key: VertexKey;
|
|
28
|
-
|
|
29
|
-
get key(): VertexKey {
|
|
30
|
-
return this._key;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
set key(v: VertexKey) {
|
|
34
|
-
this._key = v;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private _value: V | undefined;
|
|
38
|
-
|
|
39
|
-
get value(): V | undefined {
|
|
40
|
-
return this._value;
|
|
26
|
+
this.key = key;
|
|
27
|
+
this.value = value;
|
|
41
28
|
}
|
|
42
29
|
|
|
43
|
-
set value(value: V | undefined) {
|
|
44
|
-
this._value = value;
|
|
45
|
-
}
|
|
46
30
|
}
|
|
47
31
|
|
|
48
32
|
export abstract class AbstractEdge<E = any> {
|
|
33
|
+
value: E | undefined;
|
|
34
|
+
weight: number;
|
|
35
|
+
|
|
49
36
|
/**
|
|
50
37
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
51
38
|
* object.
|
|
@@ -56,31 +43,11 @@ export abstract class AbstractEdge<E = any> {
|
|
|
56
43
|
* meaning it can be omitted when creating an instance of the class.
|
|
57
44
|
*/
|
|
58
45
|
protected constructor(weight?: number, value?: E) {
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
46
|
+
this.weight = weight !== undefined ? weight : 1;
|
|
47
|
+
this.value = value;
|
|
61
48
|
this._hashCode = uuidV4();
|
|
62
49
|
}
|
|
63
50
|
|
|
64
|
-
private _value: E | undefined;
|
|
65
|
-
|
|
66
|
-
get value(): E | undefined {
|
|
67
|
-
return this._value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
set value(value: E | undefined) {
|
|
71
|
-
this._value = value;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private _weight: number;
|
|
75
|
-
|
|
76
|
-
get weight(): number {
|
|
77
|
-
return this._weight;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
set weight(v: number) {
|
|
81
|
-
this._weight = v;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
51
|
protected _hashCode: string;
|
|
85
52
|
|
|
86
53
|
get hashCode(): string {
|
|
@@ -91,15 +58,6 @@ export abstract class AbstractEdge<E = any> {
|
|
|
91
58
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
92
59
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
93
60
|
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The function sets the value of the _hashCode property to the provided string.
|
|
97
|
-
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
98
|
-
* "_hashCode" property.
|
|
99
|
-
*/
|
|
100
|
-
protected _setHashCode(v: string) {
|
|
101
|
-
this._hashCode = v;
|
|
102
|
-
}
|
|
103
61
|
}
|
|
104
62
|
|
|
105
63
|
export abstract class AbstractGraph<
|
|
@@ -107,9 +65,8 @@ export abstract class AbstractGraph<
|
|
|
107
65
|
E = any,
|
|
108
66
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
109
67
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
110
|
-
> implements IGraph<V, E, VO, EO>
|
|
111
|
-
|
|
112
|
-
private _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
68
|
+
> implements IGraph<V, E, VO, EO> {
|
|
69
|
+
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
113
70
|
|
|
114
71
|
get vertices(): Map<VertexKey, VO> {
|
|
115
72
|
return this._vertices;
|
|
@@ -556,14 +513,14 @@ export abstract class AbstractGraph<
|
|
|
556
513
|
}
|
|
557
514
|
|
|
558
515
|
getMinDist &&
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
}
|
|
516
|
+
distMap.forEach((d, v) => {
|
|
517
|
+
if (v !== srcVertex) {
|
|
518
|
+
if (d < minDist) {
|
|
519
|
+
minDist = d;
|
|
520
|
+
if (genPaths) minDest = v;
|
|
565
521
|
}
|
|
566
|
-
}
|
|
522
|
+
}
|
|
523
|
+
});
|
|
567
524
|
|
|
568
525
|
genPaths && getPaths(minDest);
|
|
569
526
|
|
|
@@ -625,7 +582,7 @@ export abstract class AbstractGraph<
|
|
|
625
582
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
626
583
|
}
|
|
627
584
|
|
|
628
|
-
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
585
|
+
const heap = new PriorityQueue<{ key: number; value: VO }>({comparator: (a, b) => a.key - b.key});
|
|
629
586
|
heap.add({key: 0, value: srcVertex});
|
|
630
587
|
|
|
631
588
|
distMap.set(srcVertex, 0);
|
|
@@ -854,7 +811,7 @@ export abstract class AbstractGraph<
|
|
|
854
811
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
855
812
|
* path between vertices in the
|
|
856
813
|
*/
|
|
857
|
-
floyd(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
814
|
+
floyd(): { costs: number[][]; predecessor: (VO | null)[][] } {
|
|
858
815
|
const idAndVertices = [...this._vertices];
|
|
859
816
|
const n = idAndVertices.length;
|
|
860
817
|
|
|
@@ -1040,7 +997,4 @@ export abstract class AbstractGraph<
|
|
|
1040
997
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
1041
998
|
}
|
|
1042
999
|
|
|
1043
|
-
protected _setVertices(value: Map<VertexKey, VO>) {
|
|
1044
|
-
this._vertices = value;
|
|
1045
|
-
}
|
|
1046
1000
|
}
|
|
@@ -24,6 +24,9 @@ export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
27
|
+
src: VertexKey;
|
|
28
|
+
dest: VertexKey;
|
|
29
|
+
|
|
27
30
|
/**
|
|
28
31
|
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
29
32
|
* and value.
|
|
@@ -37,40 +40,19 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
37
40
|
*/
|
|
38
41
|
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
39
42
|
super(weight, value);
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private _src: VertexKey;
|
|
45
|
-
|
|
46
|
-
get src(): VertexKey {
|
|
47
|
-
return this._src;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
set src(v: VertexKey) {
|
|
51
|
-
this._src = v;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private _dest: VertexKey;
|
|
55
|
-
|
|
56
|
-
get dest(): VertexKey {
|
|
57
|
-
return this._dest;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
set dest(v: VertexKey) {
|
|
61
|
-
this._dest = v;
|
|
43
|
+
this.src = src;
|
|
44
|
+
this.dest = dest;
|
|
62
45
|
}
|
|
63
46
|
}
|
|
64
47
|
|
|
65
48
|
export class DirectedGraph<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
71
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
72
|
-
implements IGraph<V, E, VO, EO>
|
|
73
|
-
{
|
|
55
|
+
implements IGraph<V, E, VO, EO> {
|
|
74
56
|
/**
|
|
75
57
|
* The constructor function initializes an instance of a class.
|
|
76
58
|
*/
|
|
@@ -78,13 +60,13 @@ export class DirectedGraph<
|
|
|
78
60
|
super();
|
|
79
61
|
}
|
|
80
62
|
|
|
81
|
-
|
|
63
|
+
protected _outEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
82
64
|
|
|
83
65
|
get outEdgeMap(): Map<VO, EO[]> {
|
|
84
66
|
return this._outEdgeMap;
|
|
85
67
|
}
|
|
86
68
|
|
|
87
|
-
|
|
69
|
+
protected _inEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
88
70
|
|
|
89
71
|
get inEdgeMap(): Map<VO, EO[]> {
|
|
90
72
|
return this._inEdgeMap;
|
|
@@ -464,12 +446,4 @@ export class DirectedGraph<
|
|
|
464
446
|
return false;
|
|
465
447
|
}
|
|
466
448
|
}
|
|
467
|
-
|
|
468
|
-
protected _setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
469
|
-
this._outEdgeMap = value;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
protected _setInEdgeMap(value: Map<VO, EO[]>) {
|
|
473
|
-
this._inEdgeMap = value;
|
|
474
|
-
}
|
|
475
449
|
}
|
|
@@ -2,6 +2,9 @@ import {MapGraphCoordinate, VertexKey} from '../../types';
|
|
|
2
2
|
import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
5
|
+
lat: number;
|
|
6
|
+
long: number;
|
|
7
|
+
|
|
5
8
|
/**
|
|
6
9
|
* The constructor function initializes an object with an key, latitude, longitude, and an optional value.
|
|
7
10
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex.
|
|
@@ -16,28 +19,8 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
16
19
|
*/
|
|
17
20
|
constructor(key: VertexKey, value: V, lat: number, long: number) {
|
|
18
21
|
super(key, value);
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private _lat: number;
|
|
24
|
-
|
|
25
|
-
get lat(): number {
|
|
26
|
-
return this._lat;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set lat(value: number) {
|
|
30
|
-
this._lat = value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private _long: number;
|
|
34
|
-
|
|
35
|
-
get long(): number {
|
|
36
|
-
return this._long;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set long(value: number) {
|
|
40
|
-
this._long = value;
|
|
22
|
+
this.lat = lat;
|
|
23
|
+
this.long = long;
|
|
41
24
|
}
|
|
42
25
|
}
|
|
43
26
|
|
|
@@ -78,26 +61,18 @@ export class MapGraph<
|
|
|
78
61
|
this._bottomRight = bottomRight;
|
|
79
62
|
}
|
|
80
63
|
|
|
81
|
-
|
|
64
|
+
protected _origin: MapGraphCoordinate = [0, 0];
|
|
82
65
|
|
|
83
66
|
get origin(): MapGraphCoordinate {
|
|
84
67
|
return this._origin;
|
|
85
68
|
}
|
|
86
69
|
|
|
87
|
-
|
|
88
|
-
this._origin = value;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private _bottomRight: MapGraphCoordinate | undefined;
|
|
70
|
+
protected _bottomRight: MapGraphCoordinate | undefined;
|
|
92
71
|
|
|
93
72
|
get bottomRight(): MapGraphCoordinate | undefined {
|
|
94
73
|
return this._bottomRight;
|
|
95
74
|
}
|
|
96
75
|
|
|
97
|
-
set bottomRight(value: MapGraphCoordinate | undefined) {
|
|
98
|
-
this._bottomRight = value;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
76
|
/**
|
|
102
77
|
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
|
103
78
|
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
|
@@ -24,6 +24,8 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
27
|
+
vertices: [VertexKey, VertexKey];
|
|
28
|
+
|
|
27
29
|
/**
|
|
28
30
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
29
31
|
* value.
|
|
@@ -36,29 +38,18 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
36
38
|
*/
|
|
37
39
|
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
38
40
|
super(weight, value);
|
|
39
|
-
this.
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private _vertices: [VertexKey, VertexKey];
|
|
43
|
-
|
|
44
|
-
get vertices() {
|
|
45
|
-
return this._vertices;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
set vertices(v: [VertexKey, VertexKey]) {
|
|
49
|
-
this._vertices = v;
|
|
41
|
+
this.vertices = [v1, v2];
|
|
50
42
|
}
|
|
51
43
|
}
|
|
52
44
|
|
|
53
45
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
59
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
60
|
-
implements IGraph<V, E, VO, EO>
|
|
61
|
-
{
|
|
52
|
+
implements IGraph<V, E, VO, EO> {
|
|
62
53
|
/**
|
|
63
54
|
* The constructor initializes a new Map object to store edges.
|
|
64
55
|
*/
|
|
@@ -265,12 +256,4 @@ export class UndirectedGraph<
|
|
|
265
256
|
}
|
|
266
257
|
return true;
|
|
267
258
|
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* The function sets the edges of a graph.
|
|
271
|
-
* @param v - A map where the keys are of type VO and the values are arrays of type EO.
|
|
272
|
-
*/
|
|
273
|
-
protected _setEdges(v: Map<VO, EO[]>) {
|
|
274
|
-
this._edges = v;
|
|
275
|
-
}
|
|
276
259
|
}
|
|
@@ -38,66 +38,42 @@ export class HashMap<K, V> {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
protected _initialCapacity: number;
|
|
42
42
|
|
|
43
43
|
get initialCapacity(): number {
|
|
44
44
|
return this._initialCapacity;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
this._initialCapacity = value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private _loadFactor: number;
|
|
47
|
+
protected _loadFactor: number;
|
|
52
48
|
|
|
53
49
|
get loadFactor(): number {
|
|
54
50
|
return this._loadFactor;
|
|
55
51
|
}
|
|
56
52
|
|
|
57
|
-
|
|
58
|
-
this._loadFactor = value;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private _capacityMultiplier: number;
|
|
53
|
+
protected _capacityMultiplier: number;
|
|
62
54
|
|
|
63
55
|
get capacityMultiplier(): number {
|
|
64
56
|
return this._capacityMultiplier;
|
|
65
57
|
}
|
|
66
58
|
|
|
67
|
-
|
|
68
|
-
this._capacityMultiplier = value;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private _size: number;
|
|
59
|
+
protected _size: number;
|
|
72
60
|
|
|
73
61
|
get size(): number {
|
|
74
62
|
return this._size;
|
|
75
63
|
}
|
|
76
64
|
|
|
77
|
-
|
|
78
|
-
this._size = value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private _table: Array<Array<[K, V]>>;
|
|
65
|
+
protected _table: Array<Array<[K, V]>>;
|
|
82
66
|
|
|
83
67
|
get table(): Array<Array<[K, V]>> {
|
|
84
68
|
return this._table;
|
|
85
69
|
}
|
|
86
70
|
|
|
87
|
-
|
|
88
|
-
this._table = value;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private _hashFn: HashFunction<K>;
|
|
71
|
+
protected _hashFn: HashFunction<K>;
|
|
92
72
|
|
|
93
73
|
get hashFn(): HashFunction<K> {
|
|
94
74
|
return this._hashFn;
|
|
95
75
|
}
|
|
96
76
|
|
|
97
|
-
set hashFn(value: HashFunction<K>) {
|
|
98
|
-
this._hashFn = value;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
77
|
set(key: K, value: V): void {
|
|
102
78
|
const loadFactor = this.size / this.table.length;
|
|
103
79
|
if (loadFactor >= this.loadFactor) {
|
|
@@ -118,7 +94,7 @@ export class HashMap<K, V> {
|
|
|
118
94
|
}
|
|
119
95
|
|
|
120
96
|
this.table[index].push([key, value]);
|
|
121
|
-
this.
|
|
97
|
+
this._size++;
|
|
122
98
|
}
|
|
123
99
|
|
|
124
100
|
get(key: K): V | undefined {
|
|
@@ -145,7 +121,7 @@ export class HashMap<K, V> {
|
|
|
145
121
|
for (let i = 0; i < this.table[index].length; i++) {
|
|
146
122
|
if (this.table[index][i][0] === key) {
|
|
147
123
|
this.table[index].splice(i, 1);
|
|
148
|
-
this.
|
|
124
|
+
this._size--;
|
|
149
125
|
|
|
150
126
|
// Check if the table needs to be resized down
|
|
151
127
|
const loadFactor = this.size / this.table.length;
|
|
@@ -157,7 +133,7 @@ export class HashMap<K, V> {
|
|
|
157
133
|
}
|
|
158
134
|
}
|
|
159
135
|
|
|
160
|
-
*entries(): IterableIterator<[K, V]> {
|
|
136
|
+
* entries(): IterableIterator<[K, V]> {
|
|
161
137
|
for (const bucket of this.table) {
|
|
162
138
|
if (bucket) {
|
|
163
139
|
for (const [key, value] of bucket) {
|
|
@@ -172,15 +148,15 @@ export class HashMap<K, V> {
|
|
|
172
148
|
}
|
|
173
149
|
|
|
174
150
|
clear(): void {
|
|
175
|
-
this.
|
|
176
|
-
this.
|
|
151
|
+
this._size = 0;
|
|
152
|
+
this._table = new Array(this.initialCapacity);
|
|
177
153
|
}
|
|
178
154
|
|
|
179
155
|
isEmpty(): boolean {
|
|
180
156
|
return this.size === 0;
|
|
181
157
|
}
|
|
182
158
|
|
|
183
|
-
|
|
159
|
+
protected _hash(key: K): number {
|
|
184
160
|
return this._hashFn(key);
|
|
185
161
|
}
|
|
186
162
|
|
|
@@ -190,7 +166,7 @@ export class HashMap<K, V> {
|
|
|
190
166
|
* @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
|
|
191
167
|
* the number of buckets that the new table should have.
|
|
192
168
|
*/
|
|
193
|
-
|
|
169
|
+
protected resizeTable(newCapacity: number): void {
|
|
194
170
|
const newTable = new Array(newCapacity);
|
|
195
171
|
for (const bucket of this._table) {
|
|
196
172
|
// Note that this is this._table
|
|
@@ -21,8 +21,8 @@ export class HashTableNode<K, V> {
|
|
|
21
21
|
import {HashFunction} from '../../types';
|
|
22
22
|
|
|
23
23
|
export class HashTable<K, V> {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
protected static readonly DEFAULT_CAPACITY = 16;
|
|
25
|
+
protected static readonly LOAD_FACTOR = 0.75;
|
|
26
26
|
|
|
27
27
|
constructor(capacity: number = HashTable.DEFAULT_CAPACITY, hashFn?: HashFunction<K>) {
|
|
28
28
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
@@ -31,42 +31,30 @@ export class HashTable<K, V> {
|
|
|
31
31
|
this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
protected _capacity: number;
|
|
35
35
|
|
|
36
36
|
get capacity(): number {
|
|
37
37
|
return this._capacity;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
this._capacity = value;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private _size: number;
|
|
40
|
+
protected _size: number;
|
|
45
41
|
|
|
46
42
|
get size(): number {
|
|
47
43
|
return this._size;
|
|
48
44
|
}
|
|
49
45
|
|
|
50
|
-
|
|
46
|
+
protected _buckets: Array<HashTableNode<K, V> | null>;
|
|
51
47
|
|
|
52
48
|
get buckets(): Array<HashTableNode<K, V> | null> {
|
|
53
49
|
return this._buckets;
|
|
54
50
|
}
|
|
55
51
|
|
|
56
|
-
|
|
57
|
-
this._buckets = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private _hashFn: HashFunction<K>;
|
|
52
|
+
protected _hashFn: HashFunction<K>;
|
|
61
53
|
|
|
62
54
|
get hashFn(): HashFunction<K> {
|
|
63
55
|
return this._hashFn;
|
|
64
56
|
}
|
|
65
57
|
|
|
66
|
-
set hashFn(value: HashFunction<K>) {
|
|
67
|
-
this._hashFn = value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
58
|
/**
|
|
71
59
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
72
60
|
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeMap {
|
|
1
|
+
export class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeSet {
|
|
1
|
+
export class TreeSet {
|
|
2
|
+
}
|