Skip to content

Commit

Permalink
ambr NodePortRecord PortRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 16, 2023
1 parent d3a7d99 commit 4b105f4
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 17 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# net

`nodeInputPorts` & `nodeOutputPorts` return `Record` instead of `Array`
`nodeInputPortRecord` & `nodeOutputPortRecord` return `Record`

`rearrangeNodePorts` use record

# connected component

Expand Down
10 changes: 5 additions & 5 deletions src/lang/connect/connect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkPortSigns } from "../check/checkPortSigns"
import { Net } from "../net"
import { findNodePortRecordOrFail } from "../net/findNodePortRecordOrFail"
import { findPortEntry } from "../net/findPortEntry"
import { findPortRecordOrFail } from "../net/findPortRecordOrFail"
import { Port } from "../port"
import { formatValue } from "../value/formatValue"

Expand Down Expand Up @@ -32,11 +32,11 @@ export function connect(net: Net, first: Port, second: Port): void {

const edge = { first, second }

const firstNodePortRecord = findNodePortRecordOrFail(net, first.node)
firstNodePortRecord[first.name].connection = { edge, port: second }
const firstPortRecord = findPortRecordOrFail(net, first.node)
firstPortRecord[first.name].connection = { edge, port: second }

const secondNodePortRecord = findNodePortRecordOrFail(net, second.node)
secondNodePortRecord[second.name].connection = { edge, port: first }
const secondPortRecord = findPortRecordOrFail(net, second.node)
secondPortRecord[second.name].connection = { edge, port: first }

if (first.isPrincipal && second.isPrincipal) {
net.activeEdges.push(edge)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/freshen/refreshNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Net } from "../net"
import { findNodePortRecordOrFail } from "../net/findNodePortRecordOrFail"
import { findPortRecordOrFail } from "../net/findPortRecordOrFail"
import { Node } from "../node"
import { freshenType } from "./freshenType"

Expand All @@ -19,7 +19,7 @@ export function refreshNode(
): void {
const occurredNames = new Map()

const portRecord = findNodePortRecordOrFail(net, node)
const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
portEntry.t = freshenType(typeVarCounters, portEntry.t, occurredNames)
}
Expand Down
6 changes: 3 additions & 3 deletions src/lang/interact/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Edge } from "../edge"
import { Env } from "../env"
import { lookupRuleByPorts } from "../mod/lookupRuleByPorts"
import { deleteEdgesOfNode } from "../net/deleteEdgesOfNode"
import { deleteNodePortRecord } from "../net/deleteNodePortRecord"
import { deletePortRecord } from "../net/deletePortRecord"

export type InteractOptions = {
checking?: Checking
Expand All @@ -31,6 +31,6 @@ export function interact(
})
}

deleteNodePortRecord(env.net, activeEdge.first.node)
deleteNodePortRecord(env.net, activeEdge.second.node)
deletePortRecord(env.net, activeEdge.first.node)
deletePortRecord(env.net, activeEdge.second.node)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Node } from "../node"
import { nodeKeyId } from "../node/nodeKeyId"
import { Net } from "./Net"

export function deleteNodePortRecord(net: Net, node: Node): void {
export function deletePortRecord(net: Net, node: Node): void {
net.nodePortRecords.delete(nodeKeyId(node))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatNode } from "../node/formatNode"
import { nodeKeyId } from "../node/nodeKeyId"
import { Net, PortRecord } from "./Net"

export function findNodePortRecordOrFail(net: Net, node: Node): PortRecord {
export function findPortRecordOrFail(net: Net, node: Node): PortRecord {
let ports = net.nodePortRecords.get(nodeKeyId(node))
if (ports === undefined) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/nodeInputPorts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Node } from "../node"
import { Port } from "../port"
import { Net } from "./Net"
import { findNodePortRecordOrFail } from "./findNodePortRecordOrFail"
import { findPortRecordOrFail } from "./findPortRecordOrFail"

export function nodeInputPorts(net: Net, node: Node): Array<Port> {
const portRecord = findNodePortRecordOrFail(net, node)
const portRecord = findPortRecordOrFail(net, node)
return Object.values(portRecord)
.filter(({ sign }) => sign === -1)
.map((portEntry) => ({
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/nodeOutputPorts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Node } from "../node"
import { Port } from "../port"
import { Net } from "./Net"
import { findNodePortRecordOrFail } from "./findNodePortRecordOrFail"
import { findPortRecordOrFail } from "./findPortRecordOrFail"

export function nodeOutputPorts(net: Net, node: Node): Array<Port> {
const portRecord = findNodePortRecordOrFail(net, node)
const portRecord = findPortRecordOrFail(net, node)
return Object.values(portRecord)
.filter(({ sign }) => sign === 1)
.map((portEntry) => ({
Expand Down

0 comments on commit 4b105f4

Please sign in to comment.