module Data.Graph.Internal where
import Data.Hashable
import qualified Data.HashMap.Lazy as HM
import Data.Graph.Types
type Links v e = HM.HashMap v e
insertLink :: (Hashable v, Eq v) => v -> a -> Links v a -> Links v a
insertLink = HM.insert
getLinks :: (Hashable v, Eq v) => v -> HM.HashMap v (Links v e) -> Links v e
getLinks = HM.lookupDefault HM.empty
linksToArcs :: [(v, Links v a)] -> [Arc v a]
linksToArcs = concatMap toArc
where
toArc :: (v, Links v a) -> [Arc v a]
toArc (fromV, links) = fmap (uncurry (Arc fromV)) (HM.toList links)
linksToEdges :: [(v, Links v a)] -> [Edge v a]
linksToEdges = concatMap toEdge
where
toEdge :: (v, Links v a) -> [Edge v a]
toEdge (fromV, links) = fmap (uncurry (Edge fromV)) (HM.toList links)
hashMapInsert :: (Eq k, Hashable k) => k -> v -> HM.HashMap k v -> HM.HashMap k v
hashMapInsert k v m = if not (HM.member k m) then HM.insert k v m else m