[project @ 2000-11-14 17:41:04 by sewardj]
[ghc-hetmet.git] / ghc / compiler / utils / Digraph.lhs
index 15df0ba..1544c7b 100644 (file)
@@ -2,7 +2,7 @@
 module Digraph(
 
        -- At present the only one with a "nice" external interface
-       stronglyConnComp, stronglyConnCompR, SCC(..),
+       stronglyConnComp, stronglyConnCompR, SCC(..), flattenSCC, flattenSCCs,
 
        Graph, Vertex, 
        graphFromEdges, buildG, transposeG, reverseE, outdegree, indegree,
@@ -34,12 +34,16 @@ module Digraph(
 
 #define ARR_ELT                (COMMA)
 
-import Array
-import List
+import Util    ( sortLt )
+
+-- Extensions
 import ST
-import ArrBase
+
+-- std interfaces
 import Maybe
-import Util    ( sortLt )
+import Array
+import List
+import Outputable
 \end{code}
 
 
@@ -53,6 +57,18 @@ import Util  ( sortLt )
 data SCC vertex = AcyclicSCC vertex
                | CyclicSCC  [vertex]
 
+flattenSCCs :: [SCC a] -> [a]
+flattenSCCs = concatMap flattenSCC
+
+flattenSCC (AcyclicSCC v) = [v]
+flattenSCC (CyclicSCC vs) = vs
+
+instance Outputable a => Outputable (SCC a) where
+   ppr (AcyclicSCC v) = text "NONREC" $$ (nest 3 (ppr v))
+   ppr (CyclicSCC vs) = text "REC" $$ (nest 3 (vcat (map ppr vs)))
+\end{code}
+
+\begin{code}
 stronglyConnComp
        :: Ord key
        => [(node, key, [key])]         -- The graph; its ok for the
@@ -115,11 +131,7 @@ mapT    :: (Vertex -> a -> b) -> Table a -> Table b
 mapT f t = array (bounds t) [ (,) v (f v (t!v)) | v <- indices t ]
 
 buildG :: Bounds -> [Edge] -> Graph
-#ifdef REALLY_HASKELL_1_3
 buildG bounds edges = accumArray (flip (:)) [] bounds edges
-#else
-buildG bounds edges = accumArray (flip (:)) [] bounds [(,) k v | (k,v) <- edges]
-#endif
 
 transposeG  :: Graph -> Graph
 transposeG g = buildG (bounds g) (reverseE g)
@@ -150,7 +162,7 @@ graphFromEdges edges
     edges1         = zipWith (,) [0..] sorted_edges
 
     graph          = array bounds [(,) v (mapMaybe key_vertex ks) | (,) v (_,    _, ks) <- edges1]
-    key_map        = array bounds [(,) v k                            | (,) v (_,    k, _ ) <- edges1]
+    key_map        = array bounds [(,) v k                        | (,) v (_,    k, _ ) <- edges1]
     vertex_map     = array bounds edges1
 
     (_,k1,_) `lt` (_,k2,_) = case k1 `compare` k2 of { LT -> True; other -> False }
@@ -221,16 +233,16 @@ draw (Node x ts) = grp this (space (length this)) (stLoop ts)
 %************************************************************************
 
 \begin{code}
-type Set s    = MutableArray s Vertex Bool
+type Set s    = STArray s Vertex Bool
 
 mkEmpty      :: Bounds -> ST s (Set s)
-mkEmpty bnds  = newArray bnds False
+mkEmpty bnds  = newSTArray bnds False
 
 contains     :: Set s -> Vertex -> ST s Bool
-contains m v  = readArray m v
+contains m v  = readSTArray m v
 
 include      :: Set s -> Vertex -> ST s ()
-include m v   = writeArray m v True
+include m v   = writeSTArray m v True
 \end{code}
 
 \begin{code}
@@ -373,13 +385,13 @@ path g v w    = w `elem` (reachable g v)
 
 \begin{code}
 bcc :: Graph -> Forest [Vertex]
-bcc g = (concat . map bicomps . map (label g dnum)) forest
+bcc g = (concat . map bicomps . map (do_label g dnum)) forest
  where forest = dff g
        dnum   = preArr (bounds g) forest
 
-label :: Graph -> Table Int -> Tree Vertex -> Tree (Vertex,Int,Int)
-label g dnum (Node v ts) = Node (v,dnum!v,lv) us
- where us = map (label g dnum) ts
+do_label :: Graph -> Table Int -> Tree Vertex -> Tree (Vertex,Int,Int)
+do_label g dnum (Node v ts) = Node (v,dnum!v,lv) us
+ where us = map (do_label g dnum) ts
        lv = minimum ([dnum!v] ++ [dnum!w | w <- g!v]
                      ++ [lu | Node (u,du,lu) xs <- us])