1-- | Representation of a directed graph. In Hakyll, this is used for dependency
4module Hakyll.Core.DirectedGraph
13import Prelude hiding (reverse, filter)
14import Data.Monoid (mconcat)
16import Data.Maybe (fromMaybe)
17import qualified Data.Map as M
18import qualified Data.Set as S
20import Hakyll.Core.DirectedGraph.Internal
22-- | Construction of directed graphs
25 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
26 -> DirectedGraph a -- ^ Resulting directed graph
27fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
29-- | Get a set of reachable neighbours from a directed graph
32 => a -- ^ Node to get the neighbours of
33 -> DirectedGraph a -- ^ Graph to search in
34 -> Set a -- ^ Set containing the neighbours
35neighbours x = fromMaybe S.empty . fmap nodeNeighbours
36 . M.lookup x . unDirectedGraph
38-- | Reverse a directed graph (i.e. flip all edges)
43reverse = mconcat . map reverse' . M.toList . unDirectedGraph
45 reverse' (id', Node _ neighbours') = fromList $
46 zip (S.toList neighbours') $ repeat $ S.singleton id'
48-- | Filter a directed graph (i.e. remove nodes based on a predicate)
51 => (a -> Bool) -- ^ Predicate
52 -> DirectedGraph a -- ^ Graph
53 -> DirectedGraph a -- ^ Resulting graph
55 DirectedGraph . M.filterWithKey (\k _ -> predicate k) . unDirectedGraph
57-- | Find all reachable nodes from a given node in the directed graph
59reachableNodes :: Ord a => a -> DirectedGraph a -> Set a
60reachableNodes x graph = reachable (neighbours x graph) (S.singleton x)
62 reachable next visited
63 | S.null next = visited
64 | otherwise = reachable (sanitize neighbours') (next `S.union` visited)
66 sanitize = S.filter (`S.notMember` visited)
67 neighbours' = S.unions $ map (flip neighbours graph)
68 $ S.toList $ sanitize next
71exampleGraph :: DirectedGraph Int
72exampleGraph = fromList
73 [ makeNode 8 [2, 4, 6]
80 makeNode tag deps = (tag, S.fromList deps)
82cyclic :: DirectedGraph Int
85 , (2, S.fromList [1, 3])
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