Graph.hs revision 7b8cbe941bf90256d31f11e5e9c60c7b933b7b62
1008N/ATree-based implementation of 'Graph' and 'DynGraph' using Data.IntMap
1008N/Amodule Common.Lib.Graph
1821N/Aimport Data.Graph.Inductive.Graph as Graph
1821N/Aimport qualified Data.IntMap as Map
1821N/Anewtype Gr a b = Gr { convertToMap :: Map.IntMap (GrContext a b) }
1821N/A , nodeSuccs :: Map.IntMap [b]
1821N/A , nodePreds :: Map.IntMap [b] }
1821N/AunsafeConstructGr :: Map.IntMap (GrContext a b) -> Gr a b
1821N/A labNodes = map (\ (v, c) -> (v, nodeLabel c)) . Map.toList . convertToMap
1821N/A . Map.toList . convertToMap
1821N/A , nodeSuccs = Map.delete v sm
1821N/A , nodePreds = Map.delete v pm } gr
1821N/AshowGraph :: (Show a, Show b) => Map.IntMap (GrContext a b) -> String
1821N/A l -> [(v, l)]) ++ Map.toList (nodeSuccs c)))
1821N/A $ Map.toList gr
1821N/AmkAdj :: Map.IntMap [b] -> Adj b
1821N/AmkAdj = concatMap (\ (w, l) -> map (\ e -> (e, w)) l) . Map.toList
1821N/AdecomposeGr v (Gr g) = case Map.lookup v g of
1821N/A g1 = Map.delete v g
1821N/AaddSuccs v ls c = c { nodeSuccs = Map.insert v ls $ nodeSuccs c }
1821N/AaddPreds v ls c = c { nodePreds = Map.insert v ls $ nodePreds c }
1821N/AclearSucc v _ c = c { nodeSuccs = Map.delete v $ nodeSuccs c }
1821N/AclearPred v _ c = c { nodePreds = Map.delete v $ nodePreds c }
1821N/A -> ([b] -> GrContext a b -> GrContext a b) -> Map.IntMap (GrContext a b)
1821N/AupdAdj g m f = Map.foldWithKey (\ v -> updGrContext v . f) g m
1821N/AupdGrContext v f r = case Map.lookup v r of
1821N/A Nothing -> error $ "Common.Lib.Graph.updGrContext no node: " ++ show v
1821N/A Just c -> Map.insert v (f c) r
1821N/A g3 = Map.insert v c g2
1008N/A in if Map.member v g
0N/A then error $ "Common.Lib.Graph.composeGr no node: " ++ show v
1008N/A Map.foldWithKey (\ nxt lbls l ->
1008N/A Nothing -> error $ "Common.Lib.Graph.getPaths no node: " ++ show src
1008N/A in Map.foldWithKey (\ nxt lbls ->
1008N/A (map (\ lbl -> [(src, tgt, lbl)]) $ Map.findWithDefault [] tgt s)
1008N/A (Map.delete tgt s)
1821N/A Nothing -> error $ "Common.Lib.Graph.getPathsTo no node: " ++ show src
1008N/AdelLEdge cmp (v, w, l) (Gr m) = let e = show (v, w) in case Map.lookup v m of
1008N/A ls = if b then loops c else Map.findWithDefault [] w sm
0N/A error $ "Common.Lib.Graph.delLEdge no edge: " ++ e
1008N/A ([_], rs) -> if b then Gr $ Map.insert v c { loops = rs } m else
1008N/A $ Map.insert v c
1008N/A { nodeSuccs = if null rs then Map.delete w sm else
0N/A Map.insert w rs sm } m
1821N/A error $ "Common.Lib.Graph.delLEdge multiple edges: " ++ e
1821N/A Nothing -> error $ "Common.Lib.Graph.delLEdge no node: "
1821N/A let e = show (v, w) in case Map.lookup v m of
1821N/A ls = if b then loops c else Map.findWithDefault [] w sm
1008N/A error $ "Common.Lib.Graph.insLEdge multiple edges: " ++ e
1008N/A else (if b then Gr $ Map.insert v c { loops = ns } m else
1008N/A Nothing -> error $ "Common.Lib.Graph.insLEdge no node: "
1821N/A let err = "Common.Lib.Graph.delLNode: node " ++ show v in
1821N/A case Map.lookup v m of
1947N/A if eq l $ nodeLabel c then Gr (Map.delete v m)
1947N/AlabelNode (v, l) (Gr m) = case Map.lookup v m of
2017N/A Just c -> (Gr $ Map.insert v (c { nodeLabel = l }) m, nodeLabel c)
1947N/A Nothing -> error $ "Common.Lib.Graph.labelNode no node: " ++ show v
1947N/A _ -> error "Common.Lib.Graph.getNewNode"
1821N/ArmIsolated (Gr m) = Gr $ Map.filter (not . isIsolated) m