module Data.PrimitiveArray.FillTables where
import Control.Monad.Primitive
import Control.Monad (when)
import Data.Vector.Fusion.Stream as S
import Data.Vector.Fusion.Stream.Monadic as M
import Data.Vector.Fusion.Stream.Size
import Data.PrimitiveArray.Class
import Data.PrimitiveArray.Index
unsafeRunFillTables
:: ( Index sh, IndexStream sh
, WriteCell m (tail :. (MutArr m (arr sh elm), t)) sh
, MPrimArrayOps arr sh elm
, Monad m
, PrimMonad m
)
=> (tail :. (MutArr m (arr sh elm), t)) -> m ()
unsafeRunFillTables (ts:.(t,f)) = M.mapM_ (unsafeWriteCell (ts:.(t,f))) $ streamUp from to where
(from,to) = boundsM t
class (Monad m) => WriteCell m c sh where
unsafeWriteCell :: c -> sh -> m ()
writeCell :: c -> sh -> m ()
instance (Monad m) => WriteCell m Z sh where
unsafeWriteCell _ _ = return ()
writeCell _ _ = return ()
instance (WriteCell m cs sh, Monad m, MPrimArrayOps arr sh a, PrimMonad m) => WriteCell m (cs:.(MutArr m (arr sh a), sh -> m a)) sh where
unsafeWriteCell (cs:.(t,f)) sh = unsafeWriteCell cs sh >> (f sh >>= writeM t sh)
writeCell (cs:.(t,f)) sh = writeCell cs sh >> (when (inBoundsM t sh) (f sh >>= writeM t sh))