-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.ts
53 lines (46 loc) · 1.32 KB
/
edge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { base_id, EdgeArgs } from "./enums.ts";
export class Edge {
private from: base_id;
private to: base_id;
weight: number;
/**
* Create an edge between `from` and `to`.
*
* Weight is set to 1 by default (i.e. unweighted).
* @param {EdgeArgs} args
*/
constructor(args: EdgeArgs) {
this.from = args.from;
this.to = args.to;
this.weight = args.weight ?? 1;
}
/**
* Returns an object with the two vertices in the egde.
* @returns {{ from:base_id, to:base_id }}
*/
get vertices(): { from: base_id; to: base_id } {
return { from: this.from, to: this.to };
}
get args(): EdgeArgs {
return { from: this.from, to: this.to, weight: this.weight };
}
isSameAs(edge: Edge, is_directed = false): boolean {
const { vertices } = this;
return (
(edge.vertices.from === vertices.from &&
edge.vertices.to === vertices.to) ||
(!is_directed &&
edge.vertices.from === vertices.to &&
edge.vertices.to === vertices.from)
);
}
pairVertex(vertex_id: base_id): base_id | undefined {
if (vertex_id === this.to) return this.from;
else if (vertex_id === this.from) return this.to;
return undefined;
}
hasVertex(vertex_id: base_id): boolean {
if (this.from === vertex_id || this.to === vertex_id) return true;
return false;
}
}