module ELynx.Tree.Parallel
( parTree,
parBranchFoldMap,
parLabelFoldMap,
)
where
import Control.Parallel.Strategies
import Data.Foldable
import ELynx.Tree.Rooted
myParList :: Strategy a -> Strategy [a]
myParList :: forall a. Strategy a -> Strategy [a]
myParList Strategy a
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return []
myParList Strategy a
s [a]
xs = do
[a]
ys <- forall a. Strategy a -> Strategy [a]
parList Strategy a
s forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail [a]
xs
a
y <- Strategy a
s forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
y forall a. a -> [a] -> [a]
: [a]
ys
parTree :: (NFData e, NFData a) => Int -> Strategy (Tree e a)
parTree :: forall e a. (NFData e, NFData a) => Int -> Strategy (Tree e a)
parTree Int
n t :: Tree e a
t@(Node e
br a
lb Forest e a
ts)
| Int
n forall a. Eq a => a -> a -> Bool
== Int
1 = do
Forest e a
ts' <- forall a. Strategy a -> Strategy [a]
myParList forall a. NFData a => Strategy a
rdeepseq Forest e a
ts
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e a. e -> a -> Forest e a -> Tree e a
Node e
br a
lb Forest e a
ts'
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
2 = do
Forest e a
ts' <- forall a. Strategy a -> Strategy [a]
myParList (forall e a. (NFData e, NFData a) => Int -> Strategy (Tree e a)
parTree (Int
n forall a. Num a => a -> a -> a
- Int
1)) Forest e a
ts
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e a. e -> a -> Forest e a -> Tree e a
Node e
br a
lb Forest e a
ts'
| Bool
otherwise = forall a. NFData a => Strategy a
rdeepseq Tree e a
t
branchFoldMap :: (e -> f) -> (f -> f -> f) -> Tree e a -> f
branchFoldMap :: forall e f a. (e -> f) -> (f -> f -> f) -> Tree e a -> f
branchFoldMap e -> f
f f -> f -> f
op (Node e
br a
_ Forest e a
ts) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' f -> f -> f
op (e -> f
f e
br) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall e f a. (e -> f) -> (f -> f -> f) -> Tree e a -> f
branchFoldMap e -> f
f f -> f -> f
op) Forest e a
ts
parBranchFoldMap :: NFData f => Int -> (e -> f) -> (f -> f -> f) -> Tree e a -> f
parBranchFoldMap :: forall f e a.
NFData f =>
Int -> (e -> f) -> (f -> f -> f) -> Tree e a -> f
parBranchFoldMap Int
n e -> f
f f -> f -> f
op t :: Tree e a
t@(Node e
br a
_ Forest e a
ts)
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
1 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' f -> f -> f
op (e -> f
f e
br) (forall a b. (a -> b) -> [a] -> [b]
map (forall f e a.
NFData f =>
Int -> (e -> f) -> (f -> f -> f) -> Tree e a -> f
parBranchFoldMap (Int
n forall a. Num a => a -> a -> a
- Int
1) e -> f
f f -> f -> f
op) Forest e a
ts forall a. a -> Strategy a -> a
`using` forall a. Strategy a -> Strategy [a]
myParList forall a. NFData a => Strategy a
rdeepseq)
| Bool
otherwise = forall e f a. (e -> f) -> (f -> f -> f) -> Tree e a -> f
branchFoldMap e -> f
f f -> f -> f
op Tree e a
t
nodeFoldMap :: (a -> b) -> (b -> b -> b) -> Tree e a -> b
nodeFoldMap :: forall a b e. (a -> b) -> (b -> b -> b) -> Tree e a -> b
nodeFoldMap a -> b
f b -> b -> b
op (Node e
_ a
lb Forest e a
ts) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' b -> b -> b
op (a -> b
f a
lb) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b e. (a -> b) -> (b -> b -> b) -> Tree e a -> b
nodeFoldMap a -> b
f b -> b -> b
op) Forest e a
ts
parLabelFoldMap :: NFData b => Int -> (a -> b) -> (b -> b -> b) -> Tree e a -> b
parLabelFoldMap :: forall b a e.
NFData b =>
Int -> (a -> b) -> (b -> b -> b) -> Tree e a -> b
parLabelFoldMap Int
n a -> b
f b -> b -> b
op t :: Tree e a
t@(Node e
_ a
lb Forest e a
ts)
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
1 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' b -> b -> b
op (a -> b
f a
lb) (forall a b. (a -> b) -> [a] -> [b]
map (forall b a e.
NFData b =>
Int -> (a -> b) -> (b -> b -> b) -> Tree e a -> b
parLabelFoldMap (Int
n forall a. Num a => a -> a -> a
- Int
1) a -> b
f b -> b -> b
op) Forest e a
ts forall a. a -> Strategy a -> a
`using` forall a. Strategy a -> Strategy [a]
myParList forall a. NFData a => Strategy a
rdeepseq)
| Bool
otherwise = forall a b e. (a -> b) -> (b -> b -> b) -> Tree e a -> b
nodeFoldMap a -> b
f b -> b -> b
op Tree e a
t