module Language.Lexer.Tlex.Data.Graph (
    transClosure,
) where

import qualified Data.Array    as Array
import           Data.Foldable
import qualified Data.Graph    as Graph

transClosure :: Graph.Graph -> Graph.Graph
transClosure :: Graph -> Graph
transClosure Graph
gr = (Vertex, Vertex) -> [[Vertex]] -> Graph
forall i e. Ix i => (i, i) -> [e] -> Array i e
Array.listArray (Vertex, Vertex)
r [ Vertex -> [Vertex]
goDfs Vertex
v | Vertex
v <- Graph -> [Vertex]
Graph.vertices Graph
gr ] where
    r :: (Vertex, Vertex)
r = Graph -> (Vertex, Vertex)
forall i e. Array i e -> (i, i)
Array.bounds Graph
gr

    goDfs :: Vertex -> [Vertex]
goDfs Vertex
v = (Tree Vertex -> [Vertex]) -> [Tree Vertex] -> [Vertex]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\Tree Vertex
t -> Tree Vertex -> [Vertex]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Tree Vertex
t) do Graph -> [Vertex] -> [Tree Vertex]
Graph.dfs Graph
gr [Vertex
v]