Skip to content

Commit

Permalink
extract findPortInActiveEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 30, 2023
1 parent 2ce94aa commit 70683a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
12 changes: 11 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
`interact` -- be sure about memeory leak of `connection`
drop the `net` prefix for `graph/` functions

use unicode for subscript number

rename PortConnect to PortReconnect

`connect` v.s. `reconnect`

- `reconnect` is used in `PortReconnect`

`interact` -- be sure about memeory leak from `port.connection`

# bug

Expand Down
29 changes: 29 additions & 0 deletions src/lang/graph/findPortInActiveEdge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ActiveEdge, Node, Port } from "../graph"

export function findPortInActiveEdge(
nodeName: string,
portName: string,
activeEdge: ActiveEdge,
): Port | undefined {
if (nodeName === activeEdge.start.node.name) {
return findPortInNode(portName, activeEdge.start.node)
}

if (nodeName === activeEdge.end.node.name) {
return findPortInNode(portName, activeEdge.end.node)
}
}

function findPortInNode(portName: string, node: Node): Port | undefined {
for (const port of node.input) {
if (port.name === portName) {
return port.connection?.port
}
}

for (const port of node.output) {
if (port.name === portName) {
return port.connection?.port
}
}
}
2 changes: 1 addition & 1 deletion src/lang/words/PortConnect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Net } from "../graph"
import { findPortInActiveEdge } from "../graph/findPortInActiveEdge"
import { netConnectPorts } from "../graph/netConnectPorts"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word, WordOptions } from "../word"
import { findPortInActiveEdge } from "./PortPush"

export class PortConnect implements Word {
constructor(
Expand Down
31 changes: 2 additions & 29 deletions src/lang/words/PortPush.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ActiveEdge, Net, Node, Port } from "../graph"
import { Net } from "../graph"
import { findPortInActiveEdge } from "../graph/findPortInActiveEdge"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word, WordOptions } from "../word"
Expand Down Expand Up @@ -28,31 +29,3 @@ export class PortPush implements Word {
net.portStack.push(found)
}
}

export function findPortInActiveEdge(
nodeName: string,
portName: string,
activeEdge: ActiveEdge,
): Port | undefined {
if (nodeName === activeEdge.start.node.name) {
return findPortInNode(portName, activeEdge.start.node)
}

if (nodeName === activeEdge.end.node.name) {
return findPortInNode(portName, activeEdge.end.node)
}
}

function findPortInNode(portName: string, node: Node): Port | undefined {
for (const port of node.input) {
if (port.name === portName) {
return port.connection?.port
}
}

for (const port of node.output) {
if (port.name === portName) {
return port.connection?.port
}
}
}

0 comments on commit 70683a4

Please sign in to comment.