on f104f91 help sign in
43 lines 1.3 kB history raw
1-- | Internal structure of the DirectedGraph type. Not exported outside of the
2-- library.
3--
4module Hakyll.Core.DirectedGraph.Internal
5 ( Node (..)
6 , DirectedGraph (..)
7 ) where
9import Prelude hiding (reverse, filter)
10import Data.Monoid (Monoid, mempty, mappend)
11import Data.Set (Set)
12import Data.Map (Map)
13import qualified Data.Map as M
14import qualified Data.Set as S
16-- | A node in the directed graph
17--
18data Node a = Node
19 { nodeTag :: a -- ^ Tag identifying the node
20 , nodeNeighbours :: Set a -- ^ Edges starting at this node
21 } deriving (Show)
23-- | Append two nodes. Useful for joining graphs.
24--
25appendNodes :: Ord a => Node a -> Node a -> Node a
26appendNodes (Node t1 n1) (Node t2 n2)
27 | t1 /= t2 = error'
28 | otherwise = Node t1 (n1 `S.union` n2)
29 where
30 error' = error $ "Hakyll.Core.DirectedGraph.Internal.appendNodes: "
31 ++ "Appending differently tagged nodes"
33-- | Type used to represent a directed graph
34--
35newtype DirectedGraph a = DirectedGraph {unDirectedGraph :: Map a (Node a)}
36 deriving (Show)
38-- | Allow users to concatenate different graphs
39--
40instance Ord a => Monoid (DirectedGraph a) where
41 mempty = DirectedGraph M.empty
42 mappend (DirectedGraph m1) (DirectedGraph m2) = DirectedGraph $
43 M.unionWith appendNodes m1 m2