module Data.Graph.Inductive.Basic
(
grev,
undir,unlab,
gsel, gfold,
efilter,elfilter,
hasLoop,isSimple,
postorder, postorderF, preorder, preorderF
)
where
import Data.Graph.Inductive.Graph
import Data.Graph.Inductive.Internal.Thread (Collect, Split, SplitM, threadList,
threadMaybe)
import Data.List (nub)
import Data.Tree
grev :: (DynGraph gr) => gr a b -> gr a b
grev :: forall (gr :: * -> * -> *) a b. DynGraph gr => gr a b -> gr a b
grev = forall (gr :: * -> * -> *) a b c d.
DynGraph gr =>
(Context a b -> Context c d) -> gr a b -> gr c d
gmap (\(Adj b
p,Node
v,a
l,Adj b
s)->(Adj b
s,Node
v,a
l,Adj b
p))
undir :: (Eq b,DynGraph gr) => gr a b -> gr a b
undir :: forall b (gr :: * -> * -> *) a.
(Eq b, DynGraph gr) =>
gr a b -> gr a b
undir = forall (gr :: * -> * -> *) a b c d.
DynGraph gr =>
(Context a b -> Context c d) -> gr a b -> gr c d
gmap (\(Adj b
p,Node
v,a
l,Adj b
s)->let ps :: Adj b
ps = forall a. Eq a => [a] -> [a]
nub (Adj b
pforall a. [a] -> [a] -> [a]
++Adj b
s) in (Adj b
ps,Node
v,a
l,Adj b
ps))
unlab :: (DynGraph gr) => gr a b -> gr () ()
unlab :: forall (gr :: * -> * -> *) a b. DynGraph gr => gr a b -> gr () ()
unlab = forall (gr :: * -> * -> *) a b c d.
DynGraph gr =>
(Context a b -> Context c d) -> gr a b -> gr c d
gmap (\(Adj b
p,Node
v,a
_,Adj b
s)->(forall {a} {b}. [(a, b)] -> [((), b)]
unlabAdj Adj b
p,Node
v,(),forall {a} {b}. [(a, b)] -> [((), b)]
unlabAdj Adj b
s))
where unlabAdj :: [(a, b)] -> [((), b)]
unlabAdj = forall a b. (a -> b) -> [a] -> [b]
map (\(a
_,b
v)->((),b
v))
gsel :: (Graph gr) => (Context a b -> Bool) -> gr a b -> [Context a b]
gsel :: forall (gr :: * -> * -> *) a b.
Graph gr =>
(Context a b -> Bool) -> gr a b -> [Context a b]
gsel Context a b -> Bool
p = forall (gr :: * -> * -> *) a b c.
Graph gr =>
(Context a b -> c -> c) -> c -> gr a b -> c
ufold (\Context a b
c [Context a b]
cs->if Context a b -> Bool
p Context a b
c then Context a b
cforall a. a -> [a] -> [a]
:[Context a b]
cs else [Context a b]
cs) []
efilter :: (DynGraph gr) => (LEdge b -> Bool) -> gr a b -> gr a b
efilter :: forall (gr :: * -> * -> *) b a.
DynGraph gr =>
(LEdge b -> Bool) -> gr a b -> gr a b
efilter LEdge b -> Bool
f = forall (gr :: * -> * -> *) a b c.
Graph gr =>
(Context a b -> c -> c) -> c -> gr a b -> c
ufold forall {gr :: * -> * -> *} {a}.
DynGraph gr =>
([(b, Node)], Node, a, [(b, Node)]) -> gr a b -> gr a b
cfilter forall (gr :: * -> * -> *) a b. Graph gr => gr a b
empty
where cfilter :: ([(b, Node)], Node, a, [(b, Node)]) -> gr a b -> gr a b
cfilter ([(b, Node)]
p,Node
v,a
l,[(b, Node)]
s) gr a b
g = ([(b, Node)]
p',Node
v,a
l,[(b, Node)]
s') forall (gr :: * -> * -> *) a b.
DynGraph gr =>
Context a b -> gr a b -> gr a b
& gr a b
g
where p' :: [(b, Node)]
p' = forall a. (a -> Bool) -> [a] -> [a]
filter (\(b
b,Node
u)->LEdge b -> Bool
f (Node
u,Node
v,b
b)) [(b, Node)]
p
s' :: [(b, Node)]
s' = forall a. (a -> Bool) -> [a] -> [a]
filter (\(b
b,Node
w)->LEdge b -> Bool
f (Node
v,Node
w,b
b)) [(b, Node)]
s
elfilter :: (DynGraph gr) => (b -> Bool) -> gr a b -> gr a b
elfilter :: forall (gr :: * -> * -> *) b a.
DynGraph gr =>
(b -> Bool) -> gr a b -> gr a b
elfilter b -> Bool
f = forall (gr :: * -> * -> *) b a.
DynGraph gr =>
(LEdge b -> Bool) -> gr a b -> gr a b
efilter (\(Node
_,Node
_,b
b)->b -> Bool
f b
b)
hasLoop :: (Graph gr) => gr a b -> Bool
hasLoop :: forall (gr :: * -> * -> *) a b. Graph gr => gr a b -> Bool
hasLoop = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (gr :: * -> * -> *) a b.
Graph gr =>
(Context a b -> Bool) -> gr a b -> [Context a b]
gsel (\Context a b
c->forall a b. Context a b -> Node
node' Context a b
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` forall a b. Context a b -> [Node]
suc' Context a b
c)
isSimple :: (Graph gr) => gr a b -> Bool
isSimple :: forall (gr :: * -> * -> *) a b. Graph gr => gr a b -> Bool
isSimple = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (gr :: * -> * -> *) a b. Graph gr => gr a b -> Bool
hasLoop
threadGraph :: (Graph gr) => (Context a b -> r -> t)
-> Split (gr a b) (Context a b) r -> SplitM (gr a b) Node t
threadGraph :: forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> r -> t)
-> Split (gr a b) (Context a b) r -> SplitM (gr a b) Node t
threadGraph Context a b -> r -> t
f Split (gr a b) (Context a b) r
c = forall i r a t j.
(i -> r -> a) -> Split t i r -> SplitM t j i -> SplitM t j a
threadMaybe Context a b -> r -> t
f Split (gr a b) (Context a b) r
c forall (gr :: * -> * -> *) a b.
Graph gr =>
Node -> gr a b -> Decomp gr a b
match
gfold1 :: (Graph gr) => (Context a b -> [Node]) -> (Context a b -> r -> t)
-> Collect (Maybe t) r -> SplitM (gr a b) Node t
gfold1 :: forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> r -> t)
-> Collect (Maybe t) r
-> SplitM (gr a b) Node t
gfold1 Context a b -> [Node]
f Context a b -> r -> t
d Collect (Maybe t) r
b = forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> r -> t)
-> Split (gr a b) (Context a b) r -> SplitM (gr a b) Node t
threadGraph Context a b -> r -> t
d (forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> r -> t)
-> Collect (Maybe t) r
-> [Node]
-> gr a b
-> (r, gr a b)
gfoldn Context a b -> [Node]
f Context a b -> r -> t
d Collect (Maybe t) r
b forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context a b -> [Node]
f)
gfoldn :: (Graph gr) => (Context a b -> [Node]) -> (Context a b -> r -> t)
-> Collect (Maybe t) r -> [Node] -> gr a b -> (r, gr a b)
gfoldn :: forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> r -> t)
-> Collect (Maybe t) r
-> [Node]
-> gr a b
-> (r, gr a b)
gfoldn Context a b -> [Node]
f Context a b -> r -> t
d Collect (Maybe t) r
b = forall r c t i. Collect r c -> Split t i r -> [i] -> t -> (c, t)
threadList Collect (Maybe t) r
b (forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> r -> t)
-> Collect (Maybe t) r
-> SplitM (gr a b) Node t
gfold1 Context a b -> [Node]
f Context a b -> r -> t
d Collect (Maybe t) r
b)
gfold :: (Graph gr) => (Context a b -> [Node])
-> (Context a b -> c -> d)
-> (Maybe d -> c -> c, c)
-> [Node]
-> gr a b
-> c
gfold :: forall (gr :: * -> * -> *) a b c d.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> c -> d)
-> (Maybe d -> c -> c, c)
-> [Node]
-> gr a b
-> c
gfold Context a b -> [Node]
f Context a b -> c -> d
d (Maybe d -> c -> c, c)
b [Node]
l gr a b
g = forall a b. (a, b) -> a
fst (forall (gr :: * -> * -> *) a b r t.
Graph gr =>
(Context a b -> [Node])
-> (Context a b -> r -> t)
-> Collect (Maybe t) r
-> [Node]
-> gr a b
-> (r, gr a b)
gfoldn Context a b -> [Node]
f Context a b -> c -> d
d (Maybe d -> c -> c, c)
b [Node]
l gr a b
g)
postorder :: Tree a -> [a]
postorder :: forall a. Tree a -> [a]
postorder (Node a
v [Tree a]
ts) = forall a. [Tree a] -> [a]
postorderF [Tree a]
ts forall a. [a] -> [a] -> [a]
++ [a
v]
postorderF :: [Tree a] -> [a]
postorderF :: forall a. [Tree a] -> [a]
postorderF = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a. Tree a -> [a]
postorder
preorder :: Tree a -> [a]
preorder :: forall a. Tree a -> [a]
preorder = forall a. Tree a -> [a]
flatten
preorderF :: [Tree a] -> [a]
preorderF :: forall a. [Tree a] -> [a]
preorderF = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a. Tree a -> [a]
preorder