%
\begin{code}
-{-# OPTIONS -w #-}
--- The above warning supression flag is a temporary kludge.
--- While working on this module you are encouraged to remove it and fix
--- any warnings in the module. See
--- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
--- for details
-
module Digraph(
-- At present the only one with a "nice" external interface
bcc
) where
-# include "HsVersions.h"
+#include "HsVersions.h"
------------------------------------------------------------------------------
-- A version of the graph algorithms described in:
import Data.Array
import Data.List
-#if __GLASGOW_HASKELL__ > 604
+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ > 604
import Data.Array.ST
#else
import Data.Array.ST hiding ( indices, bounds )
flattenSCCs :: [SCC a] -> [a]
flattenSCCs = concatMap flattenSCC
+flattenSCC :: SCC a -> [a]
flattenSCC (AcyclicSCC v) = [v]
flattenSCC (CyclicSCC vs) = vs
ppr (CyclicSCC vs) = text "REC" $$ (nest 3 (vcat (map ppr vs)))
\end{code}
+Note [Nodes, keys, vertices]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * A 'node' is a big blob of client-stuff
+
+ * Each 'node' has a unique (client) 'key', but the latter
+ is in Ord and has fast comparison
+
+ * Digraph then maps each 'key' to a Vertex (Int) which is
+ arranged densely in 0.n
+
\begin{code}
stronglyConnComp
:: Ord key
outdegree :: Graph -> Table Int
outdegree = mapT numEdges
- where numEdges v ws = length ws
+ where numEdges _ ws = length ws
indegree :: Graph -> Table Int
indegree = outdegree . transposeG
max_v = length edges - 1
bounds = (0,max_v) :: (Vertex, Vertex)
sorted_edges = let
- (_,k1,_) `le` (_,k2,_) = case k1 `compare` k2 of { GT -> False; other -> True }
+ (_,k1,_) `le` (_,k2,_) = (k1 `compare` k2) /= GT
in
sortLe le edges
edges1 = zipWith (,) [0..] sorted_edges
\begin{code}
instance Show a => Show (Tree a) where
- showsPrec p t s = showTree t ++ s
+ showsPrec _ t s = showTree t ++ s
showTree :: Show a => Tree a -> String
showTree = drawTree . mapTree show
drawTree :: Tree String -> String
drawTree = unlines . draw
+draw :: Tree String -> [String]
draw (Node x ts) = grp this (space (length this)) (stLoop ts)
where this = s1 ++ x ++ " "
stLoop [t] = grp s2 " " (draw t)
stLoop (t:ts) = grp s3 s4 (draw t) ++ [s4] ++ rsLoop ts
+ rsLoop [] = []
rsLoop [t] = grp s5 " " (draw t)
rsLoop (t:ts) = grp s6 s4 (draw t) ++ [s4] ++ rsLoop ts
chop m ts)
chop :: Set s -> Forest Vertex -> ST s (Forest Vertex)
-chop m [] = return []
+chop _ [] = return []
chop m (Node v ts : us)
= contains m v >>= \visited ->
if visited then
------------------------------------------------------------
\begin{code}
---preorder :: Tree a -> [a]
+preorder :: Tree a -> [a]
preorder (Node a ts) = a : preorderF ts
preorderF :: Forest a -> [a]
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])
+ ++ [lu | Node (_,_,lu) _ <- us])
bicomps :: Tree (Vertex,Int,Int) -> Forest [Vertex]
-bicomps (Node (v,dv,lv) ts)
- = [ Node (v:vs) us | (l,Node vs us) <- map collect ts]
+bicomps (Node (v,_,_) ts)
+ = [ Node (v:vs) us | (_,Node vs us) <- map collect ts]
collect :: Tree (Vertex,Int,Int) -> (Int, Tree [Vertex])
collect (Node (v,dv,lv) ts) = (lv, Node (v:vs) cs)
where collected = map collect ts
- vs = concat [ ws | (lw, Node ws us) <- collected, lw<dv]
+ vs = concat [ ws | (lw, Node ws _) <- collected, lw<dv]
cs = concat [ if lw<dv then us else [Node (v:ws) us]
| (lw, Node ws us) <- collected ]
\end{code}