Skip to content

Commit

Permalink
🐛✨ cycle product now part of Cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
rodigu committed Apr 7, 2022
1 parent 56c2122 commit b992a3b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ export class Cycle extends Network {
private loop_vertex: base_id;
private tip_vertex: base_id;
private is_closed: boolean;
private dynamic_product: number;

constructor(args: {
is_directed: boolean;
Expand All @@ -922,6 +923,8 @@ export class Cycle extends Network {
this.tip_vertex = this.edge_list[0].pairVertex(loop_vertex)!;
}
this.is_closed = false;
this.dynamic_product = 1;
this.updateProduct(initial_edge);
}

/**
Expand All @@ -936,6 +939,11 @@ export class Cycle extends Network {
return this.loop_vertex;
}

get product(): number {
if (this.is_closed) return this.dynamic_product;
return 0;
}

/**
* Returns true if the cycle is closed, otherwise returns false.
* @returns boolean
Expand All @@ -961,6 +969,8 @@ export class Cycle extends Network {
this.tip_vertex = edge.from;
else this.tip_vertex = edge.to;

this.updateProduct(edge);

return true;
}

Expand All @@ -978,6 +988,7 @@ export class Cycle extends Network {
super.addEdge(edge);
this.is_closed = true;
this.tip_vertex = this.loop_vertex;
this.updateProduct(edge);
return true;
}

Expand All @@ -1004,6 +1015,11 @@ export class Cycle extends Network {
});
}

private updateProduct(edge: EdgeArgs) {
const weight = edge.weight ?? 1;
this.dynamic_product *= this.tip_vertex === edge.to ? weight : 1 / weight;
}

private canCloseWith(edge: EdgeArgs): boolean {
const edge_has_tip_and_loop_vertex =
(edge.from === this.tip_vertex && edge.to === this.loop_vertex) ||
Expand Down

0 comments on commit b992a3b

Please sign in to comment.