module Data.IntGraph (
Node,
NodeSet,
Edge,
IntGraph(..),
empty,
addNode,
nodes,
removeNode,
null,
nullEdges)
where
import Prelude hiding (null)
import qualified Data.IntMap.Strict as I
import Data.IntMap.Strict (IntMap)
import qualified Data.IntSet as S
import Data.IntSet (IntSet)
import Data.Semigroup
import Data.Monoid
type Node = Int
type NodeSet = IntSet
type Edge = (Node, Node)
newtype IntGraph
= IG (IntMap NodeSet)
addNode :: Node -> IntGraph -> IntGraph
addNode node (IG graph) = IG $ I.insertWith S.union node S.empty graph
nodes :: IntGraph -> [Node]
nodes (IG graph) = I.keys graph
removeNode :: Node -> IntGraph -> IntGraph
removeNode node (IG graph) = IG $ I.map (S.delete node) $ I.delete node graph
empty = IG I.empty
null :: IntGraph -> Bool
null (IG graph) = I.null graph
nullEdges :: IntGraph -> Bool
nullEdges (IG graph) = I.null $ I.filter (not . S.null) graph
overlay :: IntGraph -> IntGraph -> IntGraph
overlay (IG g1) (IG g2) = IG $ I.unionWith (S.union) g1 g2
instance Eq IntGraph where
(IG g1) == (IG g2) = g1 == g2
instance Semigroup IntGraph where
(<>) = overlay
instance Monoid IntGraph where
mempty = empty
mappend = overlay