{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Massiv.Array.Delayed.Stream
( DS(..)
, Array (..)
, toStreamArray
, toSteps
, fromSteps
, takeS
, dropS
, filterS
, filterM
, mapMaybeS
, mapMaybeM
, unfoldr
, unfoldrN
) where
import Control.Applicative
import Control.Monad (void)
import Data.Coerce
import Data.Massiv.Array.Delayed.Pull
import qualified Data.Massiv.Array.Manifest.Vector.Stream as S
import Data.Massiv.Core.Common
import GHC.Exts
import Prelude hiding (take, drop)
import Data.Vector.Fusion.Bundle.Size (upperBound)
data DS = DS
newtype instance Array DS Ix1 e = DSArray
{ dsArray :: S.Steps S.Id e
}
toSteps :: Array DS Ix1 e -> Steps Id e
toSteps = coerce
{-# INLINE toSteps #-}
fromSteps :: Steps Id e -> Array DS Ix1 e
fromSteps = coerce
{-# INLINE fromSteps #-}
instance Functor (Array DS Ix1) where
fmap f = coerce . fmap f . dsArray
{-# INLINE fmap #-}
instance Applicative (Array DS Ix1) where
pure = fromSteps . S.singleton
{-# INLINE pure #-}
(<*>) a1 a2 = fromSteps (S.zipWith ($) (coerce a1) (coerce a2))
{-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
liftA2 f a1 a2 = fromSteps (S.zipWith f (coerce a1) (coerce a2))
{-# INLINE liftA2 #-}
#endif
instance Monad (Array DS Ix1) where
return = fromSteps . S.singleton
{-# INLINE return #-}
(>>=) arr f = coerce (S.concatMap (coerce . f) (dsArray arr))
{-# INLINE (>>=) #-}
instance Foldable (Array DS Ix1) where
foldr f acc = S.foldr f acc . toSteps
{-# INLINE foldr #-}
length = S.length . coerce
{-# INLINE length #-}
instance Semigroup (Array DS Ix1 e) where
(<>) a1 a2 = fromSteps (coerce a1 `S.append` coerce a2)
{-# INLINE (<>) #-}
instance Monoid (Array DS Ix1 e) where
mempty = DSArray S.empty
{-# INLINE mempty #-}
mappend = (<>)
{-# INLINE mappend #-}
instance IsList (Array DS Ix1 e) where
type Item (Array DS Ix1 e) = e
fromList = fromSteps . S.fromList
{-# INLINE fromList #-}
fromListN n = fromSteps . S.fromListN n
{-# INLINE fromListN #-}
toList = S.toList . coerce
{-# INLINE toList #-}
instance S.Stream DS Ix1 e where
toStream = coerce
{-# INLINE toStream #-}
toStreamArray :: Source r ix e => Array r ix e -> Array DS Ix1 e
toStreamArray = DSArray . S.steps
{-# INLINE toStreamArray #-}
instance Construct DS Ix1 e where
setComp _ arr = arr
{-# INLINE setComp #-}
makeArrayLinear _ (Sz k) = fromSteps . S.generate k
{-# INLINE makeArrayLinear #-}
instance Extract DS Ix1 e where
unsafeExtract sIx newSz = fromSteps . S.slice sIx (unSz newSz) . dsArray
{-# INLINE unsafeExtract #-}
instance Load DS Ix1 e where
size = coerce . S.length . coerce
{-# INLINE size #-}
maxSize = coerce . upperBound . stepsSize . dsArray
{-# INLINE maxSize #-}
getComp _ = Seq
{-# INLINE getComp #-}
loadArrayM _scheduler arr uWrite =
case stepsSize (dsArray arr) of
S.Exact _ ->
void $ S.foldlM (\i e -> uWrite i e >> pure (i + 1)) 0 (S.transStepsId (coerce arr))
_ -> error "Loading Stream array is not supported with loadArrayM"
{-# INLINE loadArrayM #-}
unsafeLoadIntoS marr (DSArray sts) =
S.unstreamIntoM marr (stepsSize sts) (stepsStream sts)
{-# INLINE unsafeLoadIntoS #-}
unsafeLoadInto marr arr = liftIO $ unsafeLoadIntoS marr arr
{-# INLINE unsafeLoadInto #-}
unfoldr :: (s -> Maybe (e, s)) -> s -> Array DS Ix1 e
unfoldr f = DSArray . S.unfoldr f
{-# INLINE unfoldr #-}
unfoldrN ::
Sz1
-> (s -> Maybe (e, s))
-> s
-> Array DS Ix1 e
unfoldrN n f = DSArray . S.unfoldrN n f
{-# INLINE unfoldrN #-}
filterS :: S.Stream r ix e => (e -> Bool) -> Array r ix e -> Array DS Ix1 e
filterS f = DSArray . S.filter f . S.toStream
{-# INLINE filterS #-}
filterM :: (S.Stream r ix e, Applicative f) => (e -> f Bool) -> Array r ix e -> f (Array DS Ix1 e)
filterM f arr = DSArray <$> S.filterA f (S.toStream arr)
{-# INLINE filterM #-}
mapMaybeS :: S.Stream r ix a => (a -> Maybe b) -> Array r ix a -> Array DS Ix1 b
mapMaybeS f = DSArray . S.mapMaybe f . S.toStream
{-# INLINE mapMaybeS #-}
mapMaybeM ::
(S.Stream r ix a, Applicative f) => (a -> f (Maybe b)) -> Array r ix a -> f (Array DS Ix1 b)
mapMaybeM f arr = DSArray <$> S.mapMaybeA f (S.toStream arr)
{-# INLINE mapMaybeM #-}
takeS :: Stream r ix e => Sz1 -> Array r ix e -> Array DS Ix1 e
takeS n = fromSteps . S.take (unSz n) . S.toStream
{-# INLINE takeS #-}
dropS :: Stream r ix e => Sz1 -> Array r ix e -> Array DS Ix1 e
dropS n = fromSteps . S.drop (unSz n) . S.toStream
{-# INLINE dropS #-}