1-- | Representation of a directed graph. In Hakyll, this is used for dependency
4module Hakyll.Core.DirectedGraph
15import Prelude hiding (reverse)
16import Control.Arrow (second)
17import Data.Monoid (mconcat)
19import Data.Maybe (fromMaybe)
20import qualified Data.Map as M
21import qualified Data.Set as S
23import Hakyll.Core.DirectedGraph.Internal
25-- | Construction of directed graphs
28 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
29 -> DirectedGraph a -- ^ Resulting directed graph
30fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
32-- | Deconstruction of directed graphs
34toList :: DirectedGraph a
36toList = map (second nodeNeighbours) . M.toList . unDirectedGraph
38-- | Check if a node lies in the given graph
41 => a -- ^ Node to check for
42 -> DirectedGraph a -- ^ Directed graph to check in
43 -> Bool -- ^ If the node lies in the graph
44member n = M.member n . unDirectedGraph
46-- | Get all nodes in the graph
49 => DirectedGraph a -- ^ Graph to get the nodes from
50 -> Set a -- ^ All nodes in the graph
51nodes = M.keysSet . unDirectedGraph
53-- | Get a set of reachable neighbours from a directed graph
56 => a -- ^ Node to get the neighbours of
57 -> DirectedGraph a -- ^ Graph to search in
58 -> Set a -- ^ Set containing the neighbours
59neighbours x = fromMaybe S.empty . fmap nodeNeighbours
60 . M.lookup x . unDirectedGraph
62-- | Reverse a directed graph (i.e. flip all edges)
67reverse = mconcat . map reverse' . M.toList . unDirectedGraph
69 reverse' (id', Node _ neighbours') = fromList $
70 zip (S.toList neighbours') $ repeat $ S.singleton id'
72-- | Find all reachable nodes from a given set of nodes in the directed graph
74reachableNodes :: Ord a => Set a -> DirectedGraph a -> Set a
75reachableNodes set graph = reachable (setNeighbours set) set
77 reachable next visited
78 | S.null next = visited
79 | otherwise = reachable (sanitize neighbours') (next `S.union` visited)
81 sanitize = S.filter (`S.notMember` visited)
82 neighbours' = setNeighbours (sanitize next)
84 setNeighbours = S.unions . map (`neighbours` graph) . S.toList
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