module Data.Massiv.Array.Manifest.Storable
( S (..)
, Array(..)
, VS.Storable
, withPtr
, unsafeWithPtr
) where
import Control.DeepSeq (NFData (..), deepseq)
import Data.Massiv.Array.Delayed.Internal (eq, ord)
import Data.Massiv.Array.Manifest.Internal
import Data.Massiv.Array.Manifest.List as A
import Data.Massiv.Array.Mutable
import Data.Massiv.Array.Unsafe (unsafeGenerateArray,
unsafeGenerateArrayP)
import Data.Massiv.Core.Common
import Data.Massiv.Core.List
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as MVS
import Foreign.Ptr
import GHC.Exts as GHC (IsList (..))
import Prelude hiding (mapM)
data S = S deriving Show
type instance EltRepr S ix = M
data instance Array S ix e = SArray { sComp :: !Comp
, sSize :: !ix
, sData :: !(VS.Vector e)
}
instance (Index ix, NFData e) => NFData (Array S ix e) where
rnf (SArray c sz v) = c `deepseq` sz `deepseq` v `deepseq` ()
instance (VS.Storable e, Eq e, Index ix) => Eq (Array S ix e) where
(==) = eq (==)
instance (VS.Storable e, Ord e, Index ix) => Ord (Array S ix e) where
compare = ord compare
instance (VS.Storable e, Index ix) => Construct S ix e where
getComp = sComp
setComp c arr = arr { sComp = c }
unsafeMakeArray Seq !sz f = unsafeGenerateArray sz f
unsafeMakeArray (ParOn wIds) !sz f = unsafeGenerateArrayP wIds sz f
instance (VS.Storable e, Index ix) => Source S ix e where
unsafeLinearIndex (SArray _ _ v) = VS.unsafeIndex v
instance (VS.Storable e, Index ix) => Size S ix e where
size = sSize
unsafeResize !sz !arr = arr { sSize = sz }
unsafeExtract !sIx !newSz !arr = unsafeExtract sIx newSz (toManifest arr)
instance ( VS.Storable e
, Index ix
, Index (Lower ix)
, Elt M ix e ~ Array M (Lower ix) e
, Elt S ix e ~ Array M (Lower ix) e
) =>
OuterSlice S ix e where
unsafeOuterSlice arr = unsafeOuterSlice (toManifest arr)
instance ( VS.Storable e
, Index ix
, Index (Lower ix)
, Elt M ix e ~ Array M (Lower ix) e
, Elt S ix e ~ Array M (Lower ix) e
) =>
InnerSlice S ix e where
unsafeInnerSlice arr = unsafeInnerSlice (toManifest arr)
instance (Index ix, VS.Storable e) => Manifest S ix e where
unsafeLinearIndexM (SArray _ _ v) = VS.unsafeIndex v
instance (Index ix, VS.Storable e) => Mutable S ix e where
data MArray s S ix e = MSArray !ix !(VS.MVector s e)
msize (MSArray sz _) = sz
unsafeThaw (SArray _ sz v) = MSArray sz <$> VS.unsafeThaw v
unsafeFreeze comp (MSArray sz v) = SArray comp sz <$> VS.unsafeFreeze v
unsafeNew sz = MSArray sz <$> MVS.unsafeNew (totalElem sz)
unsafeNewZero sz = MSArray sz <$> MVS.new (totalElem sz)
unsafeLinearRead (MSArray _ v) i = MVS.unsafeRead v i
unsafeLinearWrite (MSArray _ v) i = MVS.unsafeWrite v i
instance ( VS.Storable e
, IsList (Array L ix e)
, Nested LN ix e
, Nested L ix e
, Ragged L ix e
) =>
IsList (Array S ix e) where
type Item (Array S ix e) = Item (Array L ix e)
fromList = A.fromLists' Seq
toList = GHC.toList . toListArray
unsafeWithPtr :: VS.Storable a => Array S ix a -> (Ptr a -> IO b) -> IO b
unsafeWithPtr arr = VS.unsafeWith (sData arr)
withPtr :: (Index ix, VS.Storable a) => MArray RealWorld S ix a -> (Ptr a -> IO b) -> IO b
withPtr (MSArray _ mv) = MVS.unsafeWith mv