@@ -21,64 +21,71 @@ import Control.Monad.ST
2121import qualified Data.Map as M
2222import qualified Data.Set as S
2323
24- data Edge v = Edge v v
24+ data Edge k = Edge k k
2525
26- data Graph v = Graph [v ] [Edge v ]
26+ data Graph k v = Graph [v ] [Edge k ]
2727
2828type Index = Number
2929
30- scc :: forall v . (Eq v , Ord v ) => Graph v -> [[v ]]
31- scc = scc' id
30+ scc :: forall v . (Eq v , Ord v ) => Graph v v -> [[v ]]
31+ scc = scc' id id
3232
33- scc' :: forall k v . (Eq k , Ord k ) => (v -> k ) -> Graph v -> [[v ]]
34- scc' makeKey (Graph vs es) = runPure (runST (do
33+ scc' :: forall k v . (Eq k , Ord k ) => (v -> k ) -> ( k -> v ) -> Graph k v -> [[v ]]
34+ scc' makeKey makeVert (Graph vs es) = runPure (runST (do
3535 index <- newSTRef 0
3636 path <- newSTRef []
3737 indexMap <- newSTRef M .empty
3838 lowlinkMap <- newSTRef M .empty
3939 components <- newSTRef []
4040
4141 (let
42- indexOf v = do
42+ indexOf v = indexOfKey (makeKey v)
43+
44+ indexOfKey k = do
4345 m <- readSTRef indexMap
44- return $ M .lookup (makeKey v) m
46+ return $ M .lookup k m
4547
46- lowlinkOf v = do
48+ lowlinkOf v = lowlinkOfKey (makeKey v)
49+
50+ lowlinkOfKey k = do
4751 m <- readSTRef lowlinkMap
48- return $ M .lookup (makeKey v) m
52+ return $ M .lookup k m
4953
5054 go [] = readSTRef components
5155 go (v : vs) = do
5256 currentIndex <- indexOf v
53- when (isNothing currentIndex) $ strongConnect v
57+ when (isNothing currentIndex) $ strongConnect (makeKey v)
5458 go vs
5559
56- strongConnect v = do
60+ strongConnect k = do
61+ let v = makeVert k
62+
5763 i <- readSTRef index
5864
59- modifySTRef indexMap $ M .insert (makeKey v) i
60- modifySTRef lowlinkMap $ M .insert (makeKey v) i
65+ modifySTRef indexMap $ M .insert k i
66+ modifySTRef lowlinkMap $ M .insert k i
6167
6268 writeSTRef index $ i + 1
6369 modifySTRef path $ (:) v
6470
65- for es $ \(Edge v' w ) -> when (makeKey v == makeKey v ') $ do
66- wIndex <- indexOf w
71+ for es $ \(Edge k' l ) -> when (k == k ') $ do
72+ wIndex <- indexOfKey l
6773 currentPath <- readSTRef path
6874
6975 case wIndex of
7076 Nothing -> do
71- strongConnect w
72- wLowlink <- lowlinkOf w
77+ let w = makeVert l
78+ strongConnect l
79+ wLowlink <- lowlinkOfKey l
7380 for_ wLowlink $ \lowlink ->
74- modifySTRef lowlinkMap $ M .alter (maybeMin lowlink) (makeKey v)
75- _ -> when (makeKey w `elem` map makeKey currentPath) $ do
76- wIndex <- indexOf w
81+ modifySTRef lowlinkMap $ M .alter (maybeMin lowlink) k
82+ _ -> when (l `elem` map makeKey currentPath) $ do
83+ wIndex <- indexOfKey l
7784 for_ wIndex $ \index ->
78- modifySTRef lowlinkMap $ M .alter (maybeMin index) (makeKey v)
85+ modifySTRef lowlinkMap $ M .alter (maybeMin index) k
7986
80- vIndex <- indexOf v
81- vLowlink <- lowlinkOf v
87+ vIndex <- indexOfKey k
88+ vLowlink <- lowlinkOfKey k
8289
8390 when (vIndex == vLowlink) $ do
8491 currentPath <- readSTRef path
@@ -100,8 +107,8 @@ maybeMin i (Just j) = Just $ Math.min i j
100107-- |
101108-- Topological sort
102109--
103- topSort :: forall v . (Eq v , Ord v ) => Graph v -> [v ]
104- topSort = topSort' id
110+ topSort :: forall v . (Eq v , Ord v ) => Graph v v -> [v ]
111+ topSort = topSort' id id
105112
106- topSort' :: forall k v . (Eq k , Ord k ) => (v -> k ) -> Graph v -> [v ]
107- topSort' makeKey = reverse <<< concatMap id <<< scc' makeKey
113+ topSort' :: forall k v . (Eq k , Ord k ) => (v -> k ) -> ( k -> v ) -> Graph k v -> [v ]
114+ topSort' makeKey makeVert = reverse <<< concatMap id <<< scc' makeKey makeVert
0 commit comments