1--------------------------------------------------------------------------------
2-- | Representation of a directed graph. In Hakyll, this is used for dependency
4{-# LANGUAGE DeriveDataTypeable #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6module Hakyll.Core.DirectedGraph
18--------------------------------------------------------------------------------
19import Control.Arrow (second)
20import Control.DeepSeq (NFData)
21import Data.Binary (Binary)
23import qualified Data.Map as M
24import Data.Maybe (fromMaybe)
25import Data.Monoid (Monoid (..))
27import qualified Data.Set as S
28import Data.Typeable (Typeable)
29import Prelude hiding (reverse)
32--------------------------------------------------------------------------------
33-- | Type used to represent a directed graph
34newtype DirectedGraph a = DirectedGraph {unDirectedGraph :: Map a [a]}
35 deriving (Show, Binary, NFData, Typeable)
38--------------------------------------------------------------------------------
39-- | Allow users to concatenate different graphs
40instance Ord a => Monoid (DirectedGraph a) where
41 mempty = DirectedGraph M.empty
42 mappend (DirectedGraph m1) (DirectedGraph m2) = DirectedGraph $
43 M.unionWith (\x y -> sortUnique (x ++ y)) m1 m2
46--------------------------------------------------------------------------------
47-- | Construction of directed graphs
49 => [(a, [a])] -- ^ List of (node, reachable neighbours)
50 -> DirectedGraph a -- ^ Resulting directed graph
51fromList = DirectedGraph . M.fromList . map (second sortUnique)
54--------------------------------------------------------------------------------
55-- | Deconstruction of directed graphs
56toList :: DirectedGraph a
58toList = M.toList . unDirectedGraph
61--------------------------------------------------------------------------------
62-- | Check if a node lies in the given graph
64 => a -- ^ Node to check for
65 -> DirectedGraph a -- ^ Directed graph to check in
66 -> Bool -- ^ If the node lies in the graph
67member n = M.member n . unDirectedGraph
70--------------------------------------------------------------------------------
71-- | Get all nodes in the graph
73 => DirectedGraph a -- ^ Graph to get the nodes from
74 -> Set a -- ^ All nodes in the graph
75nodes = M.keysSet . unDirectedGraph
78--------------------------------------------------------------------------------
79-- | Get a set of reachable neighbours from a directed graph
81 => a -- ^ Node to get the neighbours of
82 -> DirectedGraph a -- ^ Graph to search in
83 -> [a] -- ^ Set containing the neighbours
84neighbours x dg = fromMaybe [] $ M.lookup x $ unDirectedGraph dg
87--------------------------------------------------------------------------------
88-- | Sort and make unique
89sortUnique :: Ord a => [a] -> [a]
90sortUnique = S.toAscList . S.fromList
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list