module ELynx.Data.Tree.SumStat
( BrLnNChildren
, NChildSumStat
, toNChildSumStat
, formatNChildSumStat
) where
import qualified Data.ByteString.Builder as L
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Monoid ((<>))
import Data.Tree
import ELynx.Data.Tree.MeasurableTree
type BrLnNChildren = (Double, Int)
type NChildSumStat = [BrLnNChildren]
formatNChildSumStat :: NChildSumStat -> L.ByteString
formatNChildSumStat s = L.toLazyByteString . mconcat $ map formatNChildSumStatLine s
formatNChildSumStatLine :: BrLnNChildren -> L.Builder
formatNChildSumStatLine (l, n) = L.intDec n
<> L.char8 ' '
<> L.doubleDec l
<> L.char8 '\n'
toNChildSumStat :: Measurable a => Tree a -> NChildSumStat
toNChildSumStat (Node lbl []) = [(getLen lbl, 1)]
toNChildSumStat (Node lbl ts) = (getLen lbl, sumCh) : concat nChSS
where nChSS = map toNChildSumStat ts
sumCh = sum $ map (snd . head) nChSS