diff options
Diffstat (limited to 'src/lib/Codec/Pesto/Graph.lhs')
| -rw-r--r-- | src/lib/Codec/Pesto/Graph.lhs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/lib/Codec/Pesto/Graph.lhs b/src/lib/Codec/Pesto/Graph.lhs index 5628ec3..b959d59 100644 --- a/src/lib/Codec/Pesto/Graph.lhs +++ b/src/lib/Codec/Pesto/Graph.lhs @@ -28,6 +28,7 @@ Language semantics > import Data.List (sort, nub, sortOn) > import Test.HUnit hiding (test, Node) > import Data.Ratio ((%), Ratio) +> import Data.Either (fromRight) > > import Codec.Pesto.Parse hiding (test) @@ -158,7 +159,9 @@ These are helper functions: > consumeStack (_, s, edges) i = > let > stack = dropWhile null s -> (top:sx) = if null stack then [[]] else stack +> alwaysStack = if null stack then [[]] else stack +> top = head alwaysStack +> sx = tail alwaysStack > in (Just i, []:top:sx, edgesTo i top ++ edges) > edgesTo i = map (\x -> (x, i)) @@ -342,8 +345,8 @@ Appendix > runGraphWith f doc expect = sort edges ~?= sort expect > where -> (Right op) = (head . extract . snd . unzip) <$> parse ("%pesto " ++ doc) -> nodes = zip [firstNodeId..] op +> op = (head . extract . snd . unzip) <$> parse ("%pesto " ++ doc) +> nodes = zip [firstNodeId..] (fromRight (error "unreachable") op) > edges = f nodes > cmpGraph = runGraphWith toGraph > cmpGraphRef = runGraphWith resolveReferences @@ -363,6 +366,8 @@ Find graph’s root node(s), that is a node without outgoing edges: Get all nodes with edges pointing towards nodeid > incomingEdges edges nodeid = filter ((==) nodeid . snd) edges + +> incomingNodes :: Nodes a -> Edges -> NodeId -> Nodes a > incomingNodes nodes edges nodeid = map ((!!) nodes . fst) $ incomingEdges edges nodeid > outgoingEdges edges nodeid = filter ((==) nodeid . fst) edges |
