on 9939708 help sign in
40 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 "appendNodes: Appending differently tagged nodes"
28 | otherwise = Node t1 (n1 `S.union` n2)
30-- | Type used to represent a directed graph
31--
32newtype DirectedGraph a = DirectedGraph {unDirectedGraph :: Map a (Node a)}
33 deriving (Show)
35-- | Allow users to concatenate different graphs
36--
37instance Ord a => Monoid (DirectedGraph a) where
38 mempty = DirectedGraph M.empty
39 mappend (DirectedGraph m1) (DirectedGraph m2) = DirectedGraph $
40 M.unionWith appendNodes m1 m2