module Data.Repa.Flow.Chunked.Groups
( groupsBy_i
, GroupsDict)
where
import Data.Repa.Flow.Chunked.Base
import Data.Repa.Flow.States
import Data.Repa.Fusion.Unpack
import Data.Repa.Array.Meta.Tuple as A
import Data.Repa.Array.Generic.Index as A
import Data.Repa.Array.Generic.Target as A
import Data.Repa.Array.Generic as A hiding (GroupsDict)
import qualified Data.Repa.Flow.Generic as G
#include "repa-flow.h"
type GroupsDict i m lVal lGrp tGrp lLen tLen a
= ( Flow i m lVal a, A.Index lVal ~ Int
, TargetI lGrp a
, TargetI lLen Int
, Unpack (Buffer lGrp a) tGrp
, Unpack (Buffer lLen Int) tLen)
groupsBy_i
:: GroupsDict i m lVal lGrp tGrp lLen tLen a
=> Name lGrp
-> Name lLen
-> (a -> a -> Bool)
-> Sources i m lVal a
-> m (Sources i m (T2 lGrp lLen) (a, Int))
groupsBy_i _ _ f (G.Sources n pull_chunk)
= do
refs <- newRefs n Nothing
let pull_groupsBy i eat eject
= pull_chunk i eat_groupsBy eject_groupsBy
where
eat_groupsBy chunk
= do state <- readRefs refs i
let (segs, state') = A.groupsWith name name f state chunk
writeRefs refs i state'
eat segs
eject_groupsBy
= do state <- readRefs refs i
case state of
Nothing -> eject
Just seg
-> do writeRefs refs i Nothing
eat (A.fromList name [seg])
return $ G.Sources n pull_groupsBy