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