{-# LANGUAGE UnicodeSyntax, FlexibleContexts #-}
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 :: forall n. Node -> n -> Rewrite n ()
writeNode Node
r = forall n. Node -> (n -> n) -> Rewrite n ()
modifyNode Node
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
modifyNode ∷ Node → (n → n) → Rewrite n ()
modifyNode :: forall n. Node -> (n -> n) -> Rewrite n ()
modifyNode Node
n n -> n
f = forall n. (IntMap n -> IntMap n) -> Rewrite n ()
modifyNodeMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Key -> a -> IntMap a -> IntMap a
Map.insert (Node -> Key
nKey Node
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. n -> n
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall n (m :: * -> *).
(MonadReader (Graph n) m, MonadFail m) =>
Node -> m n
readNode Node
n
updateNode ∷ View v n ⇒ Node → v → Rewrite n ()
updateNode :: forall v n. View v n => Node -> v -> Rewrite n ()
updateNode Node
n = forall v n. View v n => Node -> (v -> v) -> Rewrite n ()
adjustNode Node
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
adjustNode ∷ View v n ⇒ Node → (v → v) → Rewrite n ()
adjustNode :: forall v n. View v n => Node -> (v -> v) -> Rewrite n ()
adjustNode Node
n = forall n. Node -> (n -> n) -> Rewrite n ()
modifyNode Node
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v n. View v n => (v -> v) -> n -> n
adjust
adjustNodeM ∷ (View [Port] n, View v n) ⇒ Node → (v → Rewrite n v) → Rewrite n ()
adjustNodeM :: forall n v.
(View [Port] n, View v n) =>
Node -> (v -> Rewrite n v) -> Rewrite n ()
adjustNodeM Node
n v -> Rewrite n v
f = forall v n. View v n => Node -> v -> Rewrite n ()
updateNode Node
n forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< v -> Rewrite n v
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall v n (m :: * -> *).
(View v n, MonadReader (Graph n) m, MonadFail m) =>
Node -> m v
inspectNode Node
n
unregister ∷ Node → [Edge] → Rewrite n ()
unregister :: forall n. Node -> [Port] -> Rewrite n ()
unregister (Node Key
n) [Port]
es = forall n. (IntMap IntSet -> IntMap IntSet) -> Rewrite n ()
modifyEdgeMap forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a b. (a -> b) -> a -> b
$ forall a. (a -> Maybe a) -> Key -> IntMap a -> IntMap a
Map.update IntSet -> Maybe IntSet
deleteN) (forall a b. (a -> b) -> [a] -> [b]
map Port -> Key
eKey [Port]
es)
where deleteN :: IntSet -> Maybe IntSet
deleteN IntSet
ns = if IntSet
ns forall α. Eq α => α -> α -> Bool
≡ Key -> IntSet
Set.singleton Key
n then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Key -> IntSet -> IntSet
Set.delete Key
n IntSet
ns
register ∷ Node → [Edge] → Rewrite n ()
register :: forall n. Node -> [Port] -> Rewrite n ()
register (Node Key
n) [Port]
es = forall n. (IntMap IntSet -> IntMap IntSet) -> Rewrite n ()
modifyEdgeMap forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a b. (a -> b) -> a -> b
$ forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
Map.alter Maybe IntSet -> Maybe IntSet
addN) (forall a b. (a -> b) -> [a] -> [b]
map Port -> Key
eKey [Port]
es)
where addN :: Maybe IntSet -> Maybe IntSet
addN Maybe IntSet
ns = forall a. a -> Maybe a
Just (Key -> IntSet -> IntSet
Set.insert Key
n forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe IntSet
Set.empty Maybe IntSet
ns)