module GraphRewriting.Graph (module GraphRewriting.Graph, module GraphRewriting.Graph.Types, Graph (..)) where
import GraphRewriting.Graph.Types
import GraphRewriting.Graph.Internal
import Control.Monad.State
import qualified Data.IntMap as Map
import qualified Data.IntSet as Set
import Data.Maybe (fromJust)
emptyGraph ∷ Graph n
emptyGraph = Graph {nodeMap = Map.empty, edgeMap = Map.empty, nextKey = 0}
nodes ∷ Graph n → [n]
nodes = Map.elems . nodeMap
edges ∷ Graph n → [(Edge, [n])]
edges g = map lookupNodes $ Map.assocs (edgeMap g) where
lookupNodes (e,ns) = (Edge e, map (\n → fromJust $ Map.lookup n $ nodeMap g) $ Set.elems ns)
unsafeMapNodes ∷ (n → n') → Graph n → Graph n'
unsafeMapNodes f g = g {nodeMap = Map.map f $ nodeMap g}
unsafeMapNodesUnique ∷ (Int → n → n') → Graph n → Graph n'
unsafeMapNodesUnique f g = g {nodeMap = Map.mapWithKey f $ nodeMap g}
runGraph ∷ Rewrite n a → Graph n → (a, Graph n)
runGraph = runState . rewrite
evalGraph ∷ Rewrite n a → Graph n → a
evalGraph = evalState . rewrite
execGraph ∷ Rewrite n a → Graph n → Graph n
execGraph = execState . rewrite