Skip to content

Commit

Permalink
extract copyConnectedComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 16, 2023
1 parent 9d8ea58 commit 5a680ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# connected component

extract `copyConnectedComponent`

clonePortRecord

clonePortEntry
Expand Down
28 changes: 28 additions & 0 deletions src/lang/net/copyConnectedComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Node } from "../node"
import { Net } from "./Net"
import { findPortRecordOrFail } from "./findPortRecordOrFail"
import { hasNode } from "./hasNode"

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

copyNode(net, component, node)

const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
if (portEntry.connection) {
copyConnectedComponent(net, component, portEntry.connection.port.node)
}
}
}

export function copyNode(source: Net, target: Net, node: Node): void {
const portRecord = findPortRecordOrFail(source, node)
//
}
27 changes: 1 addition & 26 deletions src/lang/net/findConnectedComponent.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
import { Node } from "../node"
import { Net } from "./Net"
import { copyConnectedComponent } from "./copyConnectedComponent"
import { createNet } from "./createNet"
import { findPortRecordOrFail } from "./findPortRecordOrFail"
import { hasNode } from "./hasNode"

export function findConnectedComponent(net: Net, node: Node): Net {
const component = createNet()
copyConnectedComponent(net, component, node)
return component
}

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

copyNode(net, component, node)

const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
if (portEntry.connection) {
copyConnectedComponent(net, component, portEntry.connection.port.node)
}
}
}

export function copyNode(source: Net, target: Net, node: Node): void {
const portRecord = findPortRecordOrFail(source, node)
//
}

0 comments on commit 5a680ca

Please sign in to comment.