MCPcopy Create free account
hub / github.com/Effect-TS/effect / addEdge

Function addEdge

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

Source from the content-addressed store, hash-verified

2612 * @since 3.18.0
2613 */
2614export const addEdge = <N, E, T extends Kind = "directed">(
2615 mutable: MutableGraph<N, E, T>,
2616 source: NodeIndex,
2617 target: NodeIndex,
2618 data: E
2619): EdgeIndex => {
2620 const impl = getMutableImplForMutation(mutable)
2621
2622 // Validate that both nodes exist
2623 if (!impl.nodes.has(source)) {
2624 throw missingNode(source)
2625 }
2626 if (!impl.nodes.has(target)) {
2627 throw missingNode(target)
2628 }
2629
2630 const edgeIndex = impl.nextEdgeIndex
2631 if (!Number.isSafeInteger(edgeIndex)) {
2632 throw new GraphError({ message: "Graph has exhausted safe edge indexes" })
2633 }
2634
2635 // Create edge data
2636 const edgeData: Edge<E> = { source, target, data }
2637 impl.edges.set(edgeIndex, edgeData)
2638
2639 // Update adjacency lists
2640 const sourceAdjacency = impl.adjacency.get(source)
2641 if (sourceAdjacency !== undefined) {
2642 sourceAdjacency.push(edgeIndex)
2643 }
2644
2645 const targetReverseAdjacency = impl.reverseAdjacency.get(target)
2646 if (targetReverseAdjacency !== undefined) {
2647 targetReverseAdjacency.push(edgeIndex)
2648 }
2649
2650 // For undirected graphs, add reverse connections
2651 if (impl.type === "undirected") {
2652 const targetAdjacency = impl.adjacency.get(target)
2653 if (targetAdjacency !== undefined) {
2654 targetAdjacency.push(edgeIndex)
2655 }
2656
2657 const sourceReverseAdjacency = impl.reverseAdjacency.get(source)
2658 if (sourceReverseAdjacency !== undefined) {
2659 sourceReverseAdjacency.push(edgeIndex)
2660 }
2661 }
2662
2663 // Update allocators
2664 impl.nextEdgeIndex = impl.nextEdgeIndex + 1
2665
2666 // Only invalidate cycle flag if the graph was acyclic
2667 // Adding edges cannot remove cycles from cyclic graphs
2668 invalidateCycleFlagOnAddition(impl)
2669
2670 return edgeIndex
2671}

Callers 3

addEdgeByIdentityFunction · 0.85
Graph.tsFile · 0.85
copyIntoFunction · 0.85

Calls 7

missingNodeFunction · 0.85
hasMethod · 0.65
setMethod · 0.65
getMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected