Skip to content

Commit

Permalink
compute ancestors in graph
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Jun 20, 2024
1 parent a5cd3c9 commit 52bb4d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Juvix/Data/DependencyInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ nodesOnCycles = foldr go mempty . buildSCCs
go x acc = case x of
CyclicSCC ns -> foldr HashSet.insert acc ns
_ -> acc

-- | Computes the ancestors of `startNodes`, i.e., the set of nodes from which
-- some node in `startNodes` is reachable. Complexity O(V + E).
computeAncestors :: forall n. (Hashable n) => DependencyInfo n -> [n] -> HashSet n
computeAncestors depInfo startNodes =
HashSet.fromList
. map (fst . (depInfo ^. depInfoNodeFromVertex))
. filter (/= v)
$ Graph.reachable graph' v
where
adjacent = mapMaybe (depInfo ^. depInfoVertexFromName) startNodes
(v, graph') = addFreshVertex adjacent (Graph.transposeG (depInfo ^. depInfoGraph))
8 changes: 8 additions & 0 deletions src/Juvix/Prelude/Base/Foundation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ import Data.Type.Equality (type (~))
import Data.Typeable hiding (TyCon)
import Data.Void
import Data.Word
import GHC.Arr qualified as Array
import GHC.Base (assert)
import GHC.Enum
import GHC.Err qualified as Err
Expand Down Expand Up @@ -661,3 +662,10 @@ zipWithNextLoop l = NonEmpty.reverse (go [] l)
go acc = \case
lastA :| [] -> (lastA, h) :| acc
x :| y : as -> go ((x, y) : acc) (y :| as)

addFreshVertex :: [Vertex] -> Graph -> (Vertex, Graph)
addFreshVertex adjacent graph =
(v, Array.array (lo, v) (Array.assocs graph ++ [(v, adjacent)]))
where
(lo, hi) = Array.bounds graph
v = hi + 1

0 comments on commit 52bb4d6

Please sign in to comment.