data-structure-typed 1.34.7 → 1.34.8

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.
Files changed (95) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  4. package/dist/data-structures/binary-tree/avl-tree.js +8 -8
  5. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  6. package/dist/data-structures/binary-tree/binary-tree.js +4 -4
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.js +59 -59
  9. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  10. package/dist/data-structures/binary-tree/rb-tree.js +4 -4
  11. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  12. package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/data-structures/graph/abstract-graph.js +49 -49
  15. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  16. package/dist/data-structures/graph/directed-graph.js +33 -33
  17. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  18. package/dist/data-structures/graph/map-graph.js +4 -4
  19. package/dist/data-structures/graph/map-graph.js.map +1 -1
  20. package/dist/data-structures/graph/undirected-graph.js +14 -14
  21. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  22. package/dist/data-structures/tree/tree.js +5 -5
  23. package/dist/data-structures/tree/tree.js.map +1 -1
  24. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
  25. package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
  26. package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
  27. package/lib/data-structures/binary-tree/avl-tree.js +13 -13
  28. package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
  29. package/lib/data-structures/binary-tree/binary-tree.js +7 -7
  30. package/lib/data-structures/binary-tree/bst.d.ts +34 -34
  31. package/lib/data-structures/binary-tree/bst.js +80 -80
  32. package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
  33. package/lib/data-structures/binary-tree/rb-tree.js +4 -4
  34. package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
  35. package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
  36. package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
  37. package/lib/data-structures/graph/abstract-graph.js +81 -81
  38. package/lib/data-structures/graph/directed-graph.d.ts +51 -51
  39. package/lib/data-structures/graph/directed-graph.js +63 -63
  40. package/lib/data-structures/graph/map-graph.d.ts +13 -13
  41. package/lib/data-structures/graph/map-graph.js +12 -12
  42. package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
  43. package/lib/data-structures/graph/undirected-graph.js +32 -32
  44. package/lib/data-structures/tree/tree.d.ts +4 -4
  45. package/lib/data-structures/tree/tree.js +6 -6
  46. package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
  47. package/lib/interfaces/abstract-graph.d.ts +13 -13
  48. package/lib/interfaces/avl-tree.d.ts +3 -3
  49. package/lib/interfaces/bst.d.ts +8 -8
  50. package/lib/interfaces/directed-graph.d.ts +5 -5
  51. package/lib/interfaces/rb-tree.d.ts +2 -2
  52. package/lib/interfaces/undirected-graph.d.ts +2 -2
  53. package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
  54. package/lib/types/data-structures/abstract-graph.d.ts +2 -2
  55. package/lib/types/data-structures/bst.d.ts +2 -2
  56. package/lib/types/data-structures/tree-multiset.d.ts +1 -1
  57. package/lib/types/utils/validate-type.d.ts +8 -8
  58. package/package.json +1 -1
  59. package/scripts/rename_clear_files.sh +29 -0
  60. package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
  61. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  62. package/src/data-structures/binary-tree/binary-tree.ts +8 -8
  63. package/src/data-structures/binary-tree/bst.ts +98 -90
  64. package/src/data-structures/binary-tree/rb-tree.ts +9 -9
  65. package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
  66. package/src/data-structures/graph/abstract-graph.ts +109 -104
  67. package/src/data-structures/graph/directed-graph.ts +77 -77
  68. package/src/data-structures/graph/map-graph.ts +20 -15
  69. package/src/data-structures/graph/undirected-graph.ts +39 -39
  70. package/src/data-structures/tree/tree.ts +7 -7
  71. package/src/interfaces/abstract-binary-tree.ts +24 -24
  72. package/src/interfaces/abstract-graph.ts +13 -13
  73. package/src/interfaces/avl-tree.ts +3 -3
  74. package/src/interfaces/bst.ts +8 -8
  75. package/src/interfaces/directed-graph.ts +5 -5
  76. package/src/interfaces/rb-tree.ts +2 -2
  77. package/src/interfaces/undirected-graph.ts +2 -2
  78. package/src/types/data-structures/abstract-binary-tree.ts +3 -3
  79. package/src/types/data-structures/abstract-graph.ts +2 -2
  80. package/src/types/data-structures/bst.ts +2 -2
  81. package/src/types/data-structures/tree-multiset.ts +1 -1
  82. package/src/types/utils/validate-type.ts +10 -10
  83. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
  84. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
  85. package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
  86. package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
  87. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
  88. package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
  89. package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
  90. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  91. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  92. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  93. package/test/unit/data-structures/tree/tree.test.ts +2 -2
  94. package/umd/bundle.min.js +1 -1
  95. package/umd/bundle.min.js.map +1 -1
@@ -7,19 +7,19 @@
7
7
  */
8
8
  import {arrayRemove} from '../../utils';
9
9
  import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
- import type {TopologicalStatus, VertexId} from '../../types';
10
+ import type {TopologicalStatus, VertexKey} from '../../types';
11
11
  import {IDirectedGraph} from '../../interfaces';
12
12
 
13
13
  export class DirectedVertex<V = any> extends AbstractVertex<V> {
14
14
  /**
15
15
  * The constructor function initializes a vertex with an optional value.
16
- * @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
16
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
17
17
  * used to uniquely identify the vertex within a graph or data structure.
18
18
  * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
19
19
  * vertex. If no value is provided, the vertex will be initialized with a default value.
20
20
  */
21
- constructor(id: VertexId, val?: V) {
22
- super(id, val);
21
+ constructor(key: VertexKey, val?: V) {
22
+ super(key, val);
23
23
  }
24
24
  }
25
25
 
@@ -27,37 +27,37 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
27
27
  /**
28
28
  * The constructor function initializes the source and destination vertices of an edge, along with an optional weight
29
29
  * and value.
30
- * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
30
+ * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
31
31
  * a graph.
32
- * @param {VertexId} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
33
- * `VertexId`, which is likely a unique identifier for a vertex in a graph.
32
+ * @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
33
+ * `VertexKey`, which is likely a unique identifier for a vertex in a graph.
34
34
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
35
35
  * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value associated with
36
36
  * the edge.
37
37
  */
38
- constructor(src: VertexId, dest: VertexId, weight?: number, val?: V) {
38
+ constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V) {
39
39
  super(weight, val);
40
40
  this._src = src;
41
41
  this._dest = dest;
42
42
  }
43
43
 
44
- private _src: VertexId;
44
+ private _src: VertexKey;
45
45
 
46
- get src(): VertexId {
46
+ get src(): VertexKey {
47
47
  return this._src;
48
48
  }
49
49
 
50
- set src(v: VertexId) {
50
+ set src(v: VertexKey) {
51
51
  this._src = v;
52
52
  }
53
53
 
54
- private _dest: VertexId;
54
+ private _dest: VertexKey;
55
55
 
56
- get dest(): VertexId {
56
+ get dest(): VertexKey {
57
57
  return this._dest;
58
58
  }
59
59
 
60
- set dest(v: VertexId) {
60
+ set dest(v: VertexKey) {
61
61
  this._dest = v;
62
62
  }
63
63
  }
@@ -92,15 +92,15 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
92
92
 
93
93
  /**
94
94
  * The function creates a new vertex with an optional value and returns it.
95
- * @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is of type `VertexId`, which
95
+ * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
96
96
  * could be a number or a string depending on how you want to identify your vertices.
97
97
  * @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
98
98
  * it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
99
- * assigned the same value as the 'id' parameter
99
+ * assigned the same value as the 'key' parameter
100
100
  * @returns a new instance of a DirectedVertex object, casted as type V.
101
101
  */
102
- createVertex(id: VertexId, val?: V['val']): V {
103
- return new DirectedVertex(id, val ?? id) as V;
102
+ createVertex(key: VertexKey, val?: V['val']): V {
103
+ return new DirectedVertex(key, val ?? key) as V;
104
104
  }
105
105
 
106
106
  /**
@@ -110,37 +110,37 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
110
110
 
111
111
  /**
112
112
  * The function creates a directed edge between two vertices with an optional weight and value.
113
- * @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
114
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
113
+ * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
114
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
115
115
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
116
116
  * weight is provided, it defaults to 1.
117
117
  * @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
118
118
  * is used to store additional information or data associated with the edge.
119
119
  * @returns a new instance of a DirectedEdge object, casted as type E.
120
120
  */
121
- createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E {
121
+ createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
122
122
  return new DirectedEdge(src, dest, weight ?? 1, val) as E;
123
123
  }
124
124
 
125
125
  /**
126
126
  * The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
127
- * @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a vertex object or a vertex ID.
128
- * @param {V | null | VertexId} destOrId - The `destOrId` parameter in the `getEdge` function represents the
129
- * destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexId`), or `null` if the
127
+ * @param {V | null | VertexKey} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
128
+ * @param {V | null | VertexKey} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
129
+ * destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexKey`), or `null` if the
130
130
  * destination is not specified.
131
131
  * @returns the first edge found between the source and destination vertices, or null if no such edge is found.
132
132
  */
133
- getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
133
+ getEdge(srcOrKey: V | null | VertexKey, destOrKey: V | null | VertexKey): E | null {
134
134
  let edges: E[] = [];
135
135
 
136
- if (srcOrId !== null && destOrId !== null) {
137
- const src: V | null = this._getVertex(srcOrId);
138
- const dest: V | null = this._getVertex(destOrId);
136
+ if (srcOrKey !== null && destOrKey !== null) {
137
+ const src: V | null = this._getVertex(srcOrKey);
138
+ const dest: V | null = this._getVertex(destOrKey);
139
139
 
140
140
  if (src && dest) {
141
141
  const srcOutEdges = this._outEdgeMap.get(src);
142
142
  if (srcOutEdges) {
143
- edges = srcOutEdges.filter(edge => edge.dest === dest.id);
143
+ edges = srcOutEdges.filter(edge => edge.dest === dest.key);
144
144
  }
145
145
  }
146
146
  }
@@ -150,13 +150,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
150
150
 
151
151
  /**
152
152
  * The function removes an edge between two vertices in a graph and returns the removed edge.
153
- * @param {V | VertexId} srcOrId - The source vertex or its ID.
154
- * @param {V | VertexId} destOrId - The `destOrId` parameter represents the destination vertex or its ID.
153
+ * @param {V | VertexKey} srcOrKey - The source vertex or its ID.
154
+ * @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
155
155
  * @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
156
156
  */
157
- removeEdgeSrcToDest(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
158
- const src: V | null = this._getVertex(srcOrId);
159
- const dest: V | null = this._getVertex(destOrId);
157
+ removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
158
+ const src: V | null = this._getVertex(srcOrKey);
159
+ const dest: V | null = this._getVertex(destOrKey);
160
160
  let removed: E | null = null;
161
161
  if (!src || !dest) {
162
162
  return null;
@@ -164,12 +164,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
164
164
 
165
165
  const srcOutEdges = this._outEdgeMap.get(src);
166
166
  if (srcOutEdges) {
167
- arrayRemove<E>(srcOutEdges, (edge: E) => edge.dest === dest.id);
167
+ arrayRemove<E>(srcOutEdges, (edge: E) => edge.dest === dest.key);
168
168
  }
169
169
 
170
170
  const destInEdges = this._inEdgeMap.get(dest);
171
171
  if (destInEdges) {
172
- removed = arrayRemove<E>(destInEdges, (edge: E) => edge.src === src.id)[0] || null;
172
+ removed = arrayRemove<E>(destInEdges, (edge: E) => edge.src === src.key)[0] || null;
173
173
  }
174
174
  return removed;
175
175
  }
@@ -187,12 +187,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
187
187
  if (src && dest) {
188
188
  const srcOutEdges = this._outEdgeMap.get(src);
189
189
  if (srcOutEdges && srcOutEdges.length > 0) {
190
- arrayRemove(srcOutEdges, (edge: E) => edge.src === src.id);
190
+ arrayRemove(srcOutEdges, (edge: E) => edge.src === src.key);
191
191
  }
192
192
 
193
193
  const destInEdges = this._inEdgeMap.get(dest);
194
194
  if (destInEdges && destInEdges.length > 0) {
195
- removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.id)[0];
195
+ removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.key)[0];
196
196
  }
197
197
  }
198
198
 
@@ -201,13 +201,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
201
201
 
202
202
  /**
203
203
  * The function removes edges between two vertices and returns the removed edges.
204
- * @param {VertexId | V} v1 - The parameter `v1` can be either a `VertexId` or a `V`. A `VertexId` represents the
204
+ * @param {VertexKey | V} v1 - The parameter `v1` can be either a `VertexKey` or a `V`. A `VertexKey` represents the
205
205
  * unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
206
- * @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V` object. It is used to specify
206
+ * @param {VertexKey | V} v2 - The parameter `v2` represents either a `VertexKey` or a `V` object. It is used to specify
207
207
  * the second vertex in the edge that needs to be removed.
208
208
  * @returns an array of removed edges (E[]).
209
209
  */
210
- removeEdgesBetween(v1: VertexId | V, v2: VertexId | V): E[] {
210
+ removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
211
211
  const removed: E[] = [];
212
212
 
213
213
  if (v1 && v2) {
@@ -223,12 +223,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
223
223
 
224
224
  /**
225
225
  * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
226
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
227
- * (`VertexId`).
226
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
227
+ * (`VertexKey`).
228
228
  * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
229
229
  */
230
- incomingEdgesOf(vertexOrId: V | VertexId): E[] {
231
- const target = this._getVertex(vertexOrId);
230
+ incomingEdgesOf(vertexOrKey: V | VertexKey): E[] {
231
+ const target = this._getVertex(vertexOrKey);
232
232
  if (target) {
233
233
  return this.inEdgeMap.get(target) || [];
234
234
  }
@@ -237,12 +237,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
237
237
 
238
238
  /**
239
239
  * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
240
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
241
- * (`VertexId`).
240
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`V`) or a vertex ID
241
+ * (`VertexKey`).
242
242
  * @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
243
243
  */
244
- outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
245
- const target = this._getVertex(vertexOrId);
244
+ outgoingEdgesOf(vertexOrKey: V | VertexKey): E[] {
245
+ const target = this._getVertex(vertexOrKey);
246
246
  if (target) {
247
247
  return this._outEdgeMap.get(target) || [];
248
248
  }
@@ -251,38 +251,38 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
251
251
 
252
252
  /**
253
253
  * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
254
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
254
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
255
255
  * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
256
256
  */
257
- degreeOf(vertexOrId: VertexId | V): number {
258
- return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
257
+ degreeOf(vertexOrKey: VertexKey | V): number {
258
+ return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
259
259
  }
260
260
 
261
261
  /**
262
262
  * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
263
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
263
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
264
264
  * @returns The number of incoming edges of the specified vertex or vertex ID.
265
265
  */
266
- inDegreeOf(vertexOrId: VertexId | V): number {
267
- return this.incomingEdgesOf(vertexOrId).length;
266
+ inDegreeOf(vertexOrKey: VertexKey | V): number {
267
+ return this.incomingEdgesOf(vertexOrKey).length;
268
268
  }
269
269
 
270
270
  /**
271
271
  * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
272
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
272
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
273
273
  * @returns The number of outgoing edges from the specified vertex or vertex ID.
274
274
  */
275
- outDegreeOf(vertexOrId: VertexId | V): number {
276
- return this.outgoingEdgesOf(vertexOrId).length;
275
+ outDegreeOf(vertexOrKey: VertexKey | V): number {
276
+ return this.outgoingEdgesOf(vertexOrKey).length;
277
277
  }
278
278
 
279
279
  /**
280
280
  * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
281
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
281
+ * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
282
282
  * @returns The function `edgesOf` returns an array of edges.
283
283
  */
284
- edgesOf(vertexOrId: VertexId | V): E[] {
285
- return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
284
+ edgesOf(vertexOrKey: VertexKey | V): E[] {
285
+ return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
286
286
  }
287
287
 
288
288
  /**
@@ -305,11 +305,11 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
305
305
 
306
306
  /**
307
307
  * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
308
- * @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
309
- * find the destinations. It can be either a `V` object, a `VertexId` value, or `null`.
308
+ * @param {V | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
309
+ * find the destinations. It can be either a `V` object, a `VertexKey` value, or `null`.
310
310
  * @returns an array of vertices (V[]).
311
311
  */
312
- getDestinations(vertex: V | VertexId | null): V[] {
312
+ getDestinations(vertex: V | VertexKey | null): V[] {
313
313
  if (vertex === null) {
314
314
  return [];
315
315
  }
@@ -327,23 +327,23 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
327
327
  /**
328
328
  * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
329
329
  * in the sorted order, or null if the graph contains a cycle.
330
- * @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
331
- * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
332
- * specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
330
+ * @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
331
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
332
+ * specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
333
333
  * @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
334
334
  */
335
- topologicalSort(propertyName?: 'vertex' | 'id'): Array<V | VertexId> | null {
336
- propertyName = propertyName ?? 'id';
335
+ topologicalSort(propertyName?: 'vertex' | 'key'): Array<V | VertexKey> | null {
336
+ propertyName = propertyName ?? 'key';
337
337
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
338
338
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
339
- const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V | VertexId, TopologicalStatus>();
339
+ const statusMap: Map<V | VertexKey, TopologicalStatus> = new Map<V | VertexKey, TopologicalStatus>();
340
340
  for (const entry of this.vertices) {
341
341
  statusMap.set(entry[1], 0);
342
342
  }
343
343
 
344
- let sorted: (V | VertexId)[] = [];
344
+ let sorted: (V | VertexKey)[] = [];
345
345
  let hasCycle = false;
346
- const dfs = (cur: V | VertexId) => {
346
+ const dfs = (cur: V | VertexKey) => {
347
347
  statusMap.set(cur, 1);
348
348
  const children = this.getDestinations(cur);
349
349
  for (const child of children) {
@@ -366,7 +366,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
366
366
 
367
367
  if (hasCycle) return null;
368
368
 
369
- if (propertyName === 'id') sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.id : vertex));
369
+ if (propertyName === 'key') sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
370
370
  return sorted.reverse();
371
371
  }
372
372
 
@@ -384,13 +384,13 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
384
384
 
385
385
  /**
386
386
  * The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
387
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
388
- * (`VertexId`).
387
+ * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
388
+ * (`VertexKey`).
389
389
  * @returns an array of vertices (V[]).
390
390
  */
391
- getNeighbors(vertexOrId: V | VertexId): V[] {
391
+ getNeighbors(vertexOrKey: V | VertexKey): V[] {
392
392
  const neighbors: V[] = [];
393
- const vertex = this._getVertex(vertexOrId);
393
+ const vertex = this._getVertex(vertexOrKey);
394
394
  if (vertex) {
395
395
  const outEdges = this.outgoingEdgesOf(vertex);
396
396
  for (const outEdge of outEdges) {
@@ -1,10 +1,10 @@
1
- import {MapGraphCoordinate, VertexId} from '../../types';
1
+ 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
5
  /**
6
- * The constructor function initializes an object with an id, latitude, longitude, and an optional value.
7
- * @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex.
6
+ * The constructor function initializes an object with an key, latitude, longitude, and an optional value.
7
+ * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex.
8
8
  * @param {number} lat - The "lat" parameter represents the latitude of a vertex. Latitude is a geographic coordinate
9
9
  * that specifies the north-south position of a point on the Earth's surface. It is measured in degrees, with positive
10
10
  * values representing points north of the equator and negative values representing points south of the equator.
@@ -14,8 +14,8 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
14
14
  * @param {V} [val] - The "val" parameter is an optional value of type V. It is not required to be provided when
15
15
  * creating an instance of the class.
16
16
  */
17
- constructor(id: VertexId, lat: number, long: number, val?: V) {
18
- super(id, val);
17
+ constructor(key: VertexKey, lat: number, long: number, val?: V) {
18
+ super(key, val);
19
19
  this._lat = lat;
20
20
  this._long = long;
21
21
  }
@@ -45,14 +45,14 @@ export class MapEdge<V = any> extends DirectedEdge<V> {
45
45
  /**
46
46
  * The constructor function initializes a new instance of a class with the given source, destination, weight, and
47
47
  * value.
48
- * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
48
+ * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
49
49
  * a graph.
50
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
50
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
51
51
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
52
52
  * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to store additional
53
53
  * information or data associated with the edge.
54
54
  */
55
- constructor(src: VertexId, dest: VertexId, weight?: number, val?: V) {
55
+ constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V) {
56
56
  super(src, dest, weight, val);
57
57
  }
58
58
  }
@@ -97,8 +97,8 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
97
97
  }
98
98
 
99
99
  /**
100
- * The function creates a new vertex with the given id, value, latitude, and longitude.
101
- * @param {VertexId} id - The id parameter is the unique identifier for the vertex. It is of type VertexId, which could
100
+ * The function creates a new vertex with the given key, value, latitude, and longitude.
101
+ * @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
102
102
  * be a string or a number depending on how you define it in your code.
103
103
  * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the vertex. It
104
104
  * is of type `V['val']`, which means it should be of the same type as the `val` property of the vertex class `V`.
@@ -107,14 +107,19 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
107
107
  * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
108
108
  * @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
109
109
  */
110
- override createVertex(id: VertexId, val?: V['val'], lat: number = this.origin[0], long: number = this.origin[1]): V {
111
- return new MapVertex(id, lat, long, val) as V;
110
+ override createVertex(
111
+ key: VertexKey,
112
+ val?: V['val'],
113
+ lat: number = this.origin[0],
114
+ long: number = this.origin[1]
115
+ ): V {
116
+ return new MapVertex(key, lat, long, val) as V;
112
117
  }
113
118
 
114
119
  /**
115
120
  * The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
116
- * @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
117
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
121
+ * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
122
+ * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
118
123
  * created.
119
124
  * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
120
125
  * is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
@@ -123,7 +128,7 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
123
128
  * depending on the specific implementation of the `MapEdge` class.
124
129
  * @returns a new instance of the `MapEdge` class, casted as type `E`.
125
130
  */
126
- override createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E {
131
+ override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
127
132
  return new MapEdge(src, dest, weight, val) as E;
128
133
  }
129
134
  }