@@ -1,6 +1,6 @@
1+--------------------------------------------------------------------------------
12-- | Representation of a directed graph. In Hakyll, this is used for dependency
23-- tracking.
3−--
44module Hakyll.Core.DirectedGraph
55 ( DirectedGraph
66 , fromList
@@ -12,6 +12,8 @@ module Hakyll.Core.DirectedGraph
1212 , reachableNodes
1313 ) where
1414
15+
16+--------------------------------------------------------------------------------
1517import Prelude hiding (reverse)
1618import Control.Arrow (second)
1719import Data.Monoid (mconcat)
@@ -20,47 +22,55 @@ import Data.Maybe (fromMaybe)
2022import qualified Data.Map as M
2123import qualified Data.Set as S
2224
25+
26+--------------------------------------------------------------------------------
2327import Hakyll.Core.DirectedGraph.Internal
2428
29+
30+--------------------------------------------------------------------------------
2531-- | Construction of directed graphs
26−--
2732fromList :: Ord a
2833 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
2934 -> DirectedGraph a -- ^ Resulting directed graph
3035fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
3136
37+
38+--------------------------------------------------------------------------------
3239-- | Deconstruction of directed graphs
33−--
3440toList :: DirectedGraph a
3541 -> [(a, Set a)]
3642toList = map (second nodeNeighbours) . M.toList . unDirectedGraph
3743
44+
45+--------------------------------------------------------------------------------
3846-- | Check if a node lies in the given graph
39−--
4047member :: Ord a
4148 => a -- ^ Node to check for
4249 -> DirectedGraph a -- ^ Directed graph to check in
4350 -> Bool -- ^ If the node lies in the graph
4451member n = M.member n . unDirectedGraph
4552
53+
54+--------------------------------------------------------------------------------
4655-- | Get all nodes in the graph
47−--
4856nodes :: Ord a
4957 => DirectedGraph a -- ^ Graph to get the nodes from
5058 -> Set a -- ^ All nodes in the graph
5159nodes = M.keysSet . unDirectedGraph
5260
61+
62+--------------------------------------------------------------------------------
5363-- | Get a set of reachable neighbours from a directed graph
54−--
5564neighbours :: Ord a
5665 => a -- ^ Node to get the neighbours of
5766 -> DirectedGraph a -- ^ Graph to search in
5867 -> Set a -- ^ Set containing the neighbours
59−neighbours x = fromMaybe S.empty . fmap nodeNeighbours
60− . M.lookup x . unDirectedGraph
68+neighbours x = fromMaybe S.empty . fmap nodeNeighbours .
69+ M.lookup x . unDirectedGraph
6170
71+
72+--------------------------------------------------------------------------------
6273-- | Reverse a directed graph (i.e. flip all edges)
63−--
6474reverse :: Ord a
6575 => DirectedGraph a
6676 -> DirectedGraph a
@@ -69,8 +79,9 @@ reverse = mconcat . map reverse' . M.toList . unDirectedGraph
6979 reverse' (id', Node _ neighbours') = fromList $
7080 zip (S.toList neighbours') $ repeat $ S.singleton id'
7181
82+
83+--------------------------------------------------------------------------------
7284-- | Find all reachable nodes from a given set of nodes in the directed graph
73−--
7485reachableNodes :: Ord a => Set a -> DirectedGraph a -> Set a
7586reachableNodes set graph = reachable (setNeighbours set) set
7687 where