--Making a vector version of Data.Graph.Inductive.Graph module AdjGraph where import Data.Graph.Inductive.Graph import Data.List (find) import Data.Maybe (isNothing,isJust) import Control.Monad (liftM) import qualified Data.Vector as V data Gr a b = Gr (GraphRep a b) type GraphRep a b = V.Vector (Maybe (Context a b)) instance Graph Gr where empty = Gr (V.empty) isEmpty (Gr g) = V.null g || V.all (isNothing) g match = matchGr mkGraph = makeGr labNodes (Gr g) = V.toList $ V.map pullMiddle $ V.filter isJust g where pullMiddle (Just (_,a,b,_))=(a,b) instance DynGraph Gr where c@(i,n,a,o) & (Gr g) = case (n>0 && n Gr $ g V.++ ns `V.snoc` (Just c) where ns = V.replicate (n - V.length g) Nothing True -> Gr $ g V.// [(n,Just c)] makeGr :: [LNode a] -> [LEdge b] -> Gr a b makeGr as bs =Gr $ V.fromList $ map toContext [0..maxNode] where maxNode = maximum.fst $ unzip as toContext n = case theLNode of Nothing -> Nothing Just (_,a) -> Just ( map (\(b,_,c)->(c,b)) inLEdges,n,a, map (\(_,a,c)->(c,a)) outLEdges) where theLNode = find ((==n).fst) as outLEdges = filter ((==n).first) bs where first (a,_,_)=a inLEdges = filter ((==n).second) bs where second (_,a,_)=a matchGr :: Node -> Gr a b -> Decomp Gr a b matchGr node (Gr g) = (g V.! node, Gr $ g V.// [(node,Nothing)] ) {-case (node > 0 && node (Nothing, Gr g) True -> (g V.! node, Gr $ g V.// [(node,Nothing)] ) -} {-# RULES "gmap/AdjGraph Testing" gmap = fastGMap #-} {-# NOINLINE fastGMap #-} fastGMap :: (Context a b -> Context c d) -> Gr a b -> Gr c d fastGMap f (Gr g) = Gr (V.map (liftM f) g)