@@ -1,6 +1,6 @@
1+--------------------------------------------------------------------------------
1-- | Representation of a directed graph. In Hakyll, this is used for dependency
2-- | Representation of a directed graph. In Hakyll, this is used for dependency
2-- tracking.
3-- tracking.
4module Hakyll.Core.DirectedGraph
4module Hakyll.Core.DirectedGraph
5 ( DirectedGraph
5 ( DirectedGraph
@@ -12,6 +12,8 @@ module Hakyll.Core.DirectedGraph
12 , reachableNodes
12 , reachableNodes
16+--------------------------------------------------------------------------------
15import Prelude hiding (reverse)
17import Prelude hiding (reverse)
16import Control.Arrow (second)
18import Control.Arrow (second)
17import Data.Monoid (mconcat)
19import Data.Monoid (mconcat)
@@ -20,47 +22,55 @@ import Data.Maybe (fromMaybe)
20import qualified Data.Map as M
22import qualified Data.Map as M
21import qualified Data.Set as S
23import qualified Data.Set as S
26+--------------------------------------------------------------------------------
23import Hakyll.Core.DirectedGraph.Internal
27import Hakyll.Core.DirectedGraph.Internal
30+--------------------------------------------------------------------------------
25-- | Construction of directed graphs
31-- | Construction of directed graphs
27fromList :: Ord a
32fromList :: Ord a
28 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
33 => [(a, Set a)] -- ^ List of (node, reachable neighbours)
29 -> DirectedGraph a -- ^ Resulting directed graph
34 -> DirectedGraph a -- ^ Resulting directed graph
30fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
35fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
38+--------------------------------------------------------------------------------
32-- | Deconstruction of directed graphs
39-- | Deconstruction of directed graphs
34toList :: DirectedGraph a
40toList :: DirectedGraph a
35 -> [(a, Set a)]
41 -> [(a, Set a)]
36toList = map (second nodeNeighbours) . M.toList . unDirectedGraph
42toList = map (second nodeNeighbours) . M.toList . unDirectedGraph
45+--------------------------------------------------------------------------------
38-- | Check if a node lies in the given graph
46-- | Check if a node lies in the given graph
40member :: Ord a
47member :: Ord a
41 => a -- ^ Node to check for
48 => a -- ^ Node to check for
42 -> DirectedGraph a -- ^ Directed graph to check in
49 -> DirectedGraph a -- ^ Directed graph to check in
43 -> Bool -- ^ If the node lies in the graph
50 -> Bool -- ^ If the node lies in the graph
44member n = M.member n . unDirectedGraph
51member n = M.member n . unDirectedGraph
54+--------------------------------------------------------------------------------
46-- | Get all nodes in the graph
55-- | Get all nodes in the graph
48nodes :: Ord a
56nodes :: Ord a
49 => DirectedGraph a -- ^ Graph to get the nodes from
57 => DirectedGraph a -- ^ Graph to get the nodes from
50 -> Set a -- ^ All nodes in the graph
58 -> Set a -- ^ All nodes in the graph
51nodes = M.keysSet . unDirectedGraph
59nodes = M.keysSet . unDirectedGraph
62+--------------------------------------------------------------------------------
53-- | Get a set of reachable neighbours from a directed graph
63-- | Get a set of reachable neighbours from a directed graph
55neighbours :: Ord a
64neighbours :: Ord a
56 => a -- ^ Node to get the neighbours of
65 => a -- ^ Node to get the neighbours of
57 -> DirectedGraph a -- ^ Graph to search in
66 -> DirectedGraph a -- ^ Graph to search in
58 -> Set a -- ^ Set containing the neighbours
67 -> Set a -- ^ Set containing the neighbours
59−neighbours x = fromMaybe S.empty . fmap nodeNeighbours
68+neighbours x = fromMaybe S.empty . fmap nodeNeighbours .
60− . M.lookup x . unDirectedGraph
69+ M.lookup x . unDirectedGraph
72+--------------------------------------------------------------------------------
62-- | Reverse a directed graph (i.e. flip all edges)
73-- | Reverse a directed graph (i.e. flip all edges)
64reverse :: Ord a
74reverse :: Ord a
65 => DirectedGraph a
75 => DirectedGraph a
66 -> DirectedGraph a
76 -> DirectedGraph a
@@ -69,8 +79,9 @@ reverse = mconcat . map reverse' . M.toList . unDirectedGraph
69 reverse' (id', Node _ neighbours') = fromList $
79 reverse' (id', Node _ neighbours') = fromList $
70 zip (S.toList neighbours') $ repeat $ S.singleton id'
80 zip (S.toList neighbours') $ repeat $ S.singleton id'
83+--------------------------------------------------------------------------------
72-- | Find all reachable nodes from a given set of nodes in the directed graph
84-- | Find all reachable nodes from a given set of nodes in the directed graph
74reachableNodes :: Ord a => Set a -> DirectedGraph a -> Set a
85reachableNodes :: Ord a => Set a -> DirectedGraph a -> Set a
75reachableNodes set graph = reachable (setNeighbours set) set
86reachableNodes set graph = reachable (setNeighbours set) set