Skip to content

Commit

Permalink
moveConnectedComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 16, 2023
1 parent 531c69f commit de1651f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lang/net/moveConnectedComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Node } from "../node"
import { Net } from "./Net"
import { findPortRecordOrFail } from "./findPortRecordOrFail"
import { hasNode } from "./hasNode"
import { moveNode } from "./moveNode"

export function moveConnectedComponent(
net: Net,
component: Net,
node: Node,
): void {
if (hasNode(component, node)) {
return
}

moveNode(net, component, node)

const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
if (portEntry.connection) {
moveConnectedComponent(net, component, portEntry.connection.port.node)
}
}
}
10 changes: 10 additions & 0 deletions src/lang/net/moveNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Node } from "../node"
import { nodeKeyId } from "../node/nodeKeyId"
import { Net } from "./Net"
import { findNodeEntryOrFail } from "./findNodeEntryOrFail"

export function moveNode(source: Net, target: Net, node: Node): void {
const entry = findNodeEntryOrFail(source, node)
target.nodeEntries.set(nodeKeyId(node), entry)
source.nodeEntries.delete(nodeKeyId(node))
}

0 comments on commit de1651f

Please sign in to comment.