module ByteString.TreeBuilder.Tree where

import ByteString.TreeBuilder.Prelude hiding (foldl, foldr, foldlM, length)
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as C
import qualified Data.ByteString.Lazy.Internal as E
import qualified ByteString.TreeBuilder.Prelude as F
import qualified Data.ByteString.Internal as G
import qualified Foreign as H


data Tree =
  Empty |
  Leaf !ByteString |
  Branch !Tree !Tree

{-# INLINE foldl #-}
foldl :: (a -> ByteString -> a) -> a -> Tree -> a
foldl :: (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step a
init =
  \case
    Tree
Empty ->
      a
init
    Leaf ByteString
value ->
      a -> ByteString -> a
step a
init ByteString
value
    Branch Tree
tree1 Tree
tree2 ->
      (a -> ByteString -> a) -> a -> Tree -> a
forall a. (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step ((a -> ByteString -> a) -> a -> Tree -> a
forall a. (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step a
init Tree
tree1) Tree
tree2

{-# INLINE foldr #-}
foldr :: (ByteString -> a -> a) -> a -> Tree -> a
foldr :: (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step a
init =
  \case
    Tree
Empty ->
      a
init
    Leaf ByteString
value ->
      ByteString -> a -> a
step ByteString
value a
init
    Branch Tree
tree1 Tree
tree2 ->
      (ByteString -> a -> a) -> a -> Tree -> a
forall a. (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step ((ByteString -> a -> a) -> a -> Tree -> a
forall a. (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step a
init Tree
tree2) Tree
tree1

{-# INLINE foldlM #-}
foldlM :: Monad m => (a -> ByteString -> m a) -> a -> Tree -> m a
foldlM :: (a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init =
  \case
    Tree
Empty ->
      a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
init
    Leaf ByteString
value ->
      a -> ByteString -> m a
step a
init ByteString
value
    Branch Tree
tree1 Tree
tree2 ->
      (a -> ByteString -> m a) -> a -> Tree -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init Tree
tree1 m a -> (a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
init2 -> (a -> ByteString -> m a) -> a -> Tree -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init2 Tree
tree2