Add tests to Graph
This commit is contained in:
3 files changed
+74
-11
No files matched your search
@@ -41,11 +41,11 @@ export function g1() {
|
||||
*
|
||||
*
|
||||
* ```plaintext
|
||||
* 0
|
||||
* A
|
||||
* / \
|
||||
* 1 2
|
||||
* B C
|
||||
* / \ / \
|
||||
* 3 4 5 6
|
||||
* D E F G
|
||||
* ```
|
||||
*/
|
||||
export function g2() {
|
||||
@@ -61,12 +61,12 @@ export function g2() {
|
||||
G.createVertex(null, 'G')
|
||||
]
|
||||
|
||||
G.createEdge(vertices[0], vertices[1], null)
|
||||
G.createEdge(vertices[0], vertices[2], null)
|
||||
G.createEdge(vertices[1], vertices[3], null)
|
||||
G.createEdge(vertices[1], vertices[4], null)
|
||||
G.createEdge(vertices[2], vertices[5], null)
|
||||
G.createEdge(vertices[2], vertices[6], null)
|
||||
G.createEdge(vertices[0], vertices[1], null, { id: 'AB' })
|
||||
G.createEdge(vertices[0], vertices[2], null, { id: 'AC' })
|
||||
G.createEdge(vertices[1], vertices[3], null, { id: 'BD' })
|
||||
G.createEdge(vertices[1], vertices[4], null, { id: 'BE' })
|
||||
G.createEdge(vertices[2], vertices[5], null, { id: 'CF' })
|
||||
G.createEdge(vertices[2], vertices[6], null, { id: 'CG' })
|
||||
|
||||
return G
|
||||
}
|
||||
@@ -225,20 +225,26 @@ export class Path<vData, eData> {
|
||||
graph: Graph<vData, eData>
|
||||
start: Vertex<vData, eData> | null
|
||||
edges: Edge<vData, eData>[]
|
||||
/** An internal cache of the vertices. */
|
||||
#vertices: Vertex<vData, eData>[] | null
|
||||
|
||||
constructor(graph: Graph<vData, eData>, start?: Vertex<vData, eData>) {
|
||||
this.graph = graph
|
||||
this.edges = []
|
||||
this.start = start ?? null
|
||||
this.#vertices = null
|
||||
}
|
||||
|
||||
addEdge(e: Edge<vData, eData>) {
|
||||
if (!this.start) this.start = e.u
|
||||
if (e.graph !== this.graph) throw Error("The edges in a path must all belong to the same graph.")
|
||||
this.edges.push(e)
|
||||
this.#vertices = null
|
||||
}
|
||||
|
||||
get vertices() {
|
||||
if (this.#vertices) return this.#vertices
|
||||
|
||||
const vertices: Vertex<vData, eData>[] = []
|
||||
if (!this.start) return vertices
|
||||
|
||||
@@ -250,12 +256,14 @@ export class Path<vData, eData> {
|
||||
vertices.push(current)
|
||||
}
|
||||
|
||||
return vertices
|
||||
this.#vertices = vertices
|
||||
return this.#vertices
|
||||
}
|
||||
|
||||
copy() {
|
||||
const path = new Path<vData, eData>(this.graph, this.start ?? undefined)
|
||||
path.edges = this.edges.slice()
|
||||
path.#vertices = this.#vertices?.slice() ?? null
|
||||
return path
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user