module GraphRewriting.Graph.Write.Unsafe
(module GraphRewriting.Graph.Write.Unsafe, module GraphRewriting.Graph.Types)
where
import Prelude.Unicode
import Data.Maybe (fromMaybe)
import GraphRewriting.Graph.Types
import GraphRewriting.Graph.Internal
import GraphRewriting.Graph.Read
import qualified Data.IntMap as Map
import qualified Data.IntSet as Set
writeNode ∷ Node → n → Rewrite n ()
writeNode r = modifyNode r . const
modifyNode ∷ Node → (n → n) → Rewrite n ()
modifyNode n f = modifyNodeMap . Map.insert (nKey n) . f =<< readNode n
updateNode ∷ View v n ⇒ Node → v → Rewrite n ()
updateNode n = adjustNode n . const
adjustNode ∷ View v n ⇒ Node → (v → v) → Rewrite n ()
adjustNode n = modifyNode n . adjust
adjustNodeM ∷ (View [Port] n, View v n) ⇒ Node → (v → Rewrite n v) → Rewrite n ()
adjustNodeM n f = updateNode n =<< f =<< inspectNode n
unregister ∷ Node → [Edge] → Rewrite n ()
unregister (Node n) es = modifyEdgeMap $ flip (foldr $ Map.update deleteN) (map eKey es)
where deleteN ns = if ns ≡ Set.singleton n then Nothing else Just $ Set.delete n ns
register ∷ Node → [Edge] → Rewrite n ()
register (Node n) es = modifyEdgeMap $ flip (foldr $ Map.alter addN) (map eKey es)
where addN ns = Just (Set.insert n $ fromMaybe Set.empty ns)