From fef30e3f3a208a2f82b2c1fdd0df6bad37199eb7 Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Fri, 28 Jul 2023 16:46:27 +0800 Subject: [PATCH] extract `netRemoveNode` & `netRemoveEdge` --- TODO.md | 4 ---- src/lang/graph/Action.ts | 8 +++++--- src/lang/graph/Net.ts | 24 ++++++------------------ src/lang/graph/netReleaseFreePorts.ts | 6 ++++-- src/lang/graph/netRemoveEdge.ts | 8 ++++++++ src/lang/graph/netRemoveNode.ts | 8 ++++++++ 6 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 src/lang/graph/netRemoveEdge.ts create mode 100644 src/lang/graph/netRemoveNode.ts diff --git a/TODO.md b/TODO.md index 599575e3..451feccd 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,3 @@ -extract `netRemoveNode` - -extract `netRemoveEdge` - extract `netConnect` extract `netCleanUpWires` diff --git a/src/lang/graph/Action.ts b/src/lang/graph/Action.ts index 1527a5c8..f93ea9bd 100644 --- a/src/lang/graph/Action.ts +++ b/src/lang/graph/Action.ts @@ -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( @@ -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( @@ -54,7 +56,7 @@ function disconnectInput( } input.push(port.connection.port) - net.removeEdge(port.connection.edge) + netRemoveEdge(net, port.connection.edge) } } } @@ -73,7 +75,7 @@ function disconnectOutput( } output.unshift(port.connection.port) - net.removeEdge(port.connection.edge) + netRemoveEdge(net, port.connection.edge) } } } diff --git a/src/lang/graph/Net.ts b/src/lang/graph/Net.ts index 3eaa552f..43718ceb 100644 --- a/src/lang/graph/Net.ts +++ b/src/lang/graph/Net.ts @@ -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 @@ -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) } @@ -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) - } - } } diff --git a/src/lang/graph/netReleaseFreePorts.ts b/src/lang/graph/netReleaseFreePorts.ts index 1652abf6..62459106 100644 --- a/src/lang/graph/netReleaseFreePorts.ts +++ b/src/lang/graph/netReleaseFreePorts.ts @@ -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 @@ -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) } diff --git a/src/lang/graph/netRemoveEdge.ts b/src/lang/graph/netRemoveEdge.ts new file mode 100644 index 00000000..1bfb752a --- /dev/null +++ b/src/lang/graph/netRemoveEdge.ts @@ -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) + } +} diff --git a/src/lang/graph/netRemoveNode.ts b/src/lang/graph/netRemoveNode.ts new file mode 100644 index 00000000..9064b3fe --- /dev/null +++ b/src/lang/graph/netRemoveNode.ts @@ -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) + } +}