{-# LANGUAGE PatternGuards, FlexibleContexts #-}
module Data.Graph.Haggle.VertexMap (
VertexMap,
emptyVertexMap,
vertexForLabel,
lookupVertexForLabel,
vertexMapFromGraph,
VertexMapRef,
newVertexMapRef,
vertexForLabelRef,
vertexMapFromRef ) where
import qualified Control.DeepSeq as DS
import Control.Monad ( liftM )
import qualified Control.Monad.Primitive as P
import qualified Control.Monad.Ref as R
import Data.Map ( Map )
import qualified Data.Map as M
import Data.Tuple ( swap )
import Data.Graph.Haggle.Classes
newtype VertexMap nl = VM (Map nl Vertex)
instance (DS.NFData nl) => DS.NFData (VertexMap nl) where
rnf (VM m) = m `DS.deepseq` ()
emptyVertexMap :: VertexMap nl
emptyVertexMap = VM M.empty
vertexForLabel :: (MLabeledVertex g, Ord (MVertexLabel g), P.PrimMonad m, R.MonadRef m)
=> g m
-> VertexMap (MVertexLabel g)
-> MVertexLabel g
-> m (Vertex, VertexMap (MVertexLabel g))
vertexForLabel g vm@(VM m) lbl
| Just v <- M.lookup lbl m = return (v, vm)
| otherwise = do
v <- addLabeledVertex g lbl
let m' = M.insert lbl v m
return (v, VM m')
lookupVertexForLabel :: (Ord nl) => nl -> VertexMap nl -> Maybe Vertex
lookupVertexForLabel lbl (VM m) = M.lookup lbl m
vertexMapFromGraph :: (HasVertexLabel g, Ord (VertexLabel g))
=> g -> VertexMap (VertexLabel g)
vertexMapFromGraph = VM . M.fromList . map swap . labeledVertices
newtype VertexMapRef nl m = VMR (R.Ref m (VertexMap nl))
vertexMapFromRef :: (P.PrimMonad m, R.MonadRef m) => VertexMapRef nl m -> m (VertexMap nl)
vertexMapFromRef (VMR ref) = R.readRef ref
newVertexMapRef :: (P.PrimMonad m, R.MonadRef m) => m (VertexMapRef nl m)
newVertexMapRef = liftM VMR $ R.newRef emptyVertexMap
vertexForLabelRef :: (MLabeledVertex g, Ord (MVertexLabel g), P.PrimMonad m, R.MonadRef m)
=> g m
-> VertexMapRef (MVertexLabel g) m
-> MVertexLabel g
-> m Vertex
vertexForLabelRef g (VMR ref) lbl = do
vm <- R.readRef ref
(v, vm') <- vertexForLabel g vm lbl
R.writeRef ref vm'
return v