Skip to content

Commit

Permalink
extract netRemoveNode & netRemoveEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 28, 2023
1 parent a6a0a43 commit fef30e3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
extract `netRemoveNode`

extract `netRemoveEdge`

extract `netConnect`

extract `netCleanUpWires`
Expand Down
8 changes: 5 additions & 3 deletions src/lang/graph/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { InternalError } from "../errors"
import { Net, Node, Port } from "../graph"
import { Mod } from "../mod"
import { Rule } from "../rule"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

export class Action {
constructor(
Expand Down Expand Up @@ -37,7 +39,7 @@ function disconnectNode(
): void {
disconnectInput(net, node.input, input)
disconnectOutput(net, node.output, output)
net.removeNode(node)
netRemoveNode(net, node)
}

function disconnectInput(
Expand All @@ -54,7 +56,7 @@ function disconnectInput(
}

input.push(port.connection.port)
net.removeEdge(port.connection.edge)
netRemoveEdge(net, port.connection.edge)
}
}
}
Expand All @@ -73,7 +75,7 @@ function disconnectOutput(
}

output.unshift(port.connection.port)
net.removeEdge(port.connection.edge)
netRemoveEdge(net, port.connection.edge)
}
}
}
Expand Down
24 changes: 6 additions & 18 deletions src/lang/graph/Net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Action, Edge, Node, Port, createEdge } from "../graph"
import { Mod } from "../mod"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netReleaseFreePorts } from "./netReleaseFreePorts"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

export class Net {
mod: Mod
Expand All @@ -28,11 +30,11 @@ export class Net {
cleanUpWires(): void {
for (const wire of this.wires) {
if (wire.start.connection && wire.end.connection) {
this.removeEdge(wire.start.connection.edge)
this.removeEdge(wire.end.connection.edge)
netRemoveEdge(this, wire.start.connection.edge)
netRemoveEdge(this, wire.end.connection.edge)

this.removeNode(wire.start.node)
this.removeNode(wire.end.node)
netRemoveNode(this, wire.start.node)
netRemoveNode(this, wire.end.node)

this.connect(wire.start.connection.port, wire.end.connection.port)
}
Expand Down Expand Up @@ -60,18 +62,4 @@ export class Net {
this.edges.push(createEdge(start, end))
}
}

removeNode(node: Node): void {
const index = this.nodes.indexOf(node)
if (index !== -1) {
this.nodes.splice(index, 1)
}
}

removeEdge(edge: Edge): void {
const index = this.edges.indexOf(edge)
if (index !== -1) {
this.edges.splice(index, 1)
}
}
}
6 changes: 4 additions & 2 deletions src/lang/graph/netReleaseFreePorts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { InternalError } from "../errors"
import { Node } from "../graph"
import { Net } from "./Net"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

export function netReleaseFreePorts(net: Net, closer: Node | undefined): void {
if (closer === undefined) return
Expand All @@ -13,8 +15,8 @@ export function netReleaseFreePorts(net: Net, closer: Node | undefined): void {
}

net.portStack.push(port.connection.port)
net.removeEdge(port.connection.edge)
netRemoveEdge(net, port.connection.edge)
}

net.removeNode(closer)
netRemoveNode(net, closer)
}
8 changes: 8 additions & 0 deletions src/lang/graph/netRemoveEdge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Edge, Net } from "../graph"

export function netRemoveEdge(net: Net, edge: Edge): void {
const index = net.edges.indexOf(edge)
if (index !== -1) {
net.edges.splice(index, 1)
}
}
8 changes: 8 additions & 0 deletions src/lang/graph/netRemoveNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Net, Node } from "../graph"

export function netRemoveNode(net: Net, node: Node): void {
const index = net.nodes.indexOf(node)
if (index !== -1) {
net.nodes.splice(index, 1)
}
}

0 comments on commit fef30e3

Please sign in to comment.