1-- | Representation of a directed graph. In Hakyll, this is used for dependency
4module Hakyll.Core.DirectedGraph
14import Prelude hiding (reverse, filter)
15import Data.Monoid (mconcat)
17import Data.Maybe (fromMaybe)
18import qualified Data.Map as M
19import qualified Data.Set as S
21import Hakyll.Core.DirectedGraph.Internal
23-- | Construction of directed graphs
26 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
27 -> DirectedGraph a -- ^ Resulting directed graph
28fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
30-- | Get all nodes in the graph
33 => DirectedGraph a -- ^ Graph to get the nodes from
34 -> Set a -- ^ All nodes in the graph
35nodes = M.keysSet . unDirectedGraph
37-- | Get a set of reachable neighbours from a directed graph
40 => a -- ^ Node to get the neighbours of
41 -> DirectedGraph a -- ^ Graph to search in
42 -> Set a -- ^ Set containing the neighbours
43neighbours x = fromMaybe S.empty . fmap nodeNeighbours
44 . M.lookup x . unDirectedGraph
46-- | Reverse a directed graph (i.e. flip all edges)
51reverse = mconcat . map reverse' . M.toList . unDirectedGraph
53 reverse' (id', Node _ neighbours') = fromList $
54 zip (S.toList neighbours') $ repeat $ S.singleton id'
56-- | Filter a directed graph (i.e. remove nodes based on a predicate)
59 => (a -> Bool) -- ^ Predicate
60 -> DirectedGraph a -- ^ Graph
61 -> DirectedGraph a -- ^ Resulting graph
63 DirectedGraph . M.filterWithKey (\k _ -> predicate k) . unDirectedGraph
65-- | Find all reachable nodes from a given node in the directed graph
67reachableNodes :: Ord a => a -> DirectedGraph a -> Set a
68reachableNodes x graph = reachable (neighbours x graph) (S.singleton x)
70 reachable next visited
71 | S.null next = visited
72 | otherwise = reachable (sanitize neighbours') (next `S.union` visited)
74 sanitize = S.filter (`S.notMember` visited)
75 neighbours' = S.unions $ map (flip neighbours graph)
76 $ S.toList $ sanitize next
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list