Hyperlinkv0.8.0-beta.28

Graph

Graph.beginMutationconsteffect/Graph.ts:479
<N, E, T extends Kind = "directed">(graph: Graph<N, E, T>): MutableGraph<
  N,
  E,
  T
>

Creates a mutable scope for safe graph mutations by copying the data structure.

Example (Beginning a mutation scope)

import { Graph } from "effect"

const graph = Graph.directed<string, number>()
const mutable = Graph.beginMutation(graph)
// Now mutable can be safely modified without affecting original graph
mutations
Source effect/Graph.ts:47928 lines
export const beginMutation = <N, E, T extends Kind = "directed">(
  graph: Graph<N, E, T>
): MutableGraph<N, E, T> => {
  // Copy adjacency maps with deep cloned arrays
  const adjacency = new Map<NodeIndex, Array<EdgeIndex>>()
  const reverseAdjacency = new Map<NodeIndex, Array<EdgeIndex>>()

  for (const [nodeIndex, edges] of graph.adjacency) {
    adjacency.set(nodeIndex, [...edges])
  }

  for (const [nodeIndex, edges] of graph.reverseAdjacency) {
    reverseAdjacency.set(nodeIndex, [...edges])
  }

  const mutable: Mutable<MutableGraph<N, E, T>> = Object.create(ProtoGraph)
  mutable.type = graph.type
  mutable.nodes = new Map(graph.nodes)
  mutable.edges = new Map(graph.edges)
  mutable.adjacency = adjacency
  mutable.reverseAdjacency = reverseAdjacency
  mutable.nextNodeIndex = graph.nextNodeIndex
  mutable.nextEdgeIndex = graph.nextEdgeIndex
  mutable.acyclic = graph.acyclic
  mutable.mutable = true

  return mutable
}
Referenced by 3 symbols