MCPcopy Index your code
hub / github.com/Effect-TS/effect / addEdge

Function addEdge

packages/effect/src/Graph.ts:1145–1197  ·  view source on GitHub ↗
(
  mutable: MutableGraph<N, E, T>,
  source: NodeIndex,
  target: NodeIndex,
  data: E
)

Source from the content-addressed store, hash-verified

1143 * @category mutations
1144 */
1145export const addEdge = <N, E, T extends Kind = "directed">(
1146 mutable: MutableGraph<N, E, T>,
1147 source: NodeIndex,
1148 target: NodeIndex,
1149 data: E
1150): EdgeIndex => {
1151 // Validate that both nodes exist
1152 if (!mutable.nodes.has(source)) {
1153 throw missingNode(source)
1154 }
1155 if (!mutable.nodes.has(target)) {
1156 throw missingNode(target)
1157 }
1158
1159 const edgeIndex = mutable.nextEdgeIndex
1160
1161 // Create edge data
1162 const edgeData = new Edge({ source, target, data })
1163 mutable.edges.set(edgeIndex, edgeData)
1164
1165 // Update adjacency lists
1166 const sourceAdjacency = mutable.adjacency.get(source)
1167 if (sourceAdjacency !== undefined) {
1168 sourceAdjacency.push(edgeIndex)
1169 }
1170
1171 const targetReverseAdjacency = mutable.reverseAdjacency.get(target)
1172 if (targetReverseAdjacency !== undefined) {
1173 targetReverseAdjacency.push(edgeIndex)
1174 }
1175
1176 // For undirected graphs, add reverse connections
1177 if (mutable.type === "undirected") {
1178 const targetAdjacency = mutable.adjacency.get(target)
1179 if (targetAdjacency !== undefined) {
1180 targetAdjacency.push(edgeIndex)
1181 }
1182
1183 const sourceReverseAdjacency = mutable.reverseAdjacency.get(source)
1184 if (sourceReverseAdjacency !== undefined) {
1185 sourceReverseAdjacency.push(edgeIndex)
1186 }
1187 }
1188
1189 // Update allocators
1190 mutable.nextEdgeIndex = mutable.nextEdgeIndex + 1
1191
1192 // Only invalidate cycle flag if the graph was acyclic
1193 // Adding edges cannot remove cycles from cyclic graphs
1194 invalidateCycleFlagOnAddition(mutable)
1195
1196 return edgeIndex
1197}
1198
1199/**
1200 * Removes a node and all its incident edges from a mutable graph.

Callers

nothing calls this directly

Calls 4

missingNodeFunction · 0.85
setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected