module Data.Massiv.Array.Manifest.Unboxed
( U (..)
, VU.Unbox
, Array(..)
) where
import Control.DeepSeq (NFData (..), deepseq)
import Data.Massiv.Array.Delayed.Internal (eq)
import Data.Massiv.Array.Manifest.Internal (M, toManifest)
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.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as MVU
import GHC.Exts as GHC (IsList (..))
import Prelude hiding (mapM)
data U = U deriving Show
type instance EltRepr U ix = M
data instance Array U ix e = UArray { uComp :: !Comp
, uSize :: !ix
, uData :: !(VU.Vector e)
}
instance (Index ix, NFData e) => NFData (Array U ix e) where
rnf (UArray c sz v) = c `deepseq` sz `deepseq` v `deepseq` ()
instance (VU.Unbox e, Index ix) => Construct U ix e where
getComp = uComp
setComp c arr = arr { uComp = c }
unsafeMakeArray Seq !sz f = unsafeGenerateArray sz f
unsafeMakeArray (ParOn wIds) !sz f = unsafeGenerateArrayP wIds sz f
instance (VU.Unbox e, Eq e, Index ix) => Eq (Array U ix e) where
(==) = eq (==)
instance (VU.Unbox e, Index ix) => Source U ix e where
unsafeLinearIndex (UArray _ _ v) = VU.unsafeIndex v
instance (VU.Unbox e, Index ix) => Size U ix e where
size = uSize
unsafeResize !sz !arr = arr { uSize = sz }
unsafeExtract !sIx !newSz !arr = unsafeExtract sIx newSz (toManifest arr)
instance VU.Unbox e => Slice U Ix1 e where
unsafeSlice arr i _ _ = Just (unsafeLinearIndex arr i)
instance ( VU.Unbox e
, Index ix
, Index (Lower ix)
, Elt U ix e ~ Elt M ix e
, Elt M ix e ~ Array M (Lower ix) e
) =>
Slice U ix e where
unsafeSlice arr = unsafeSlice (toManifest arr)
instance VU.Unbox e => OuterSlice U Ix1 e where
unsafeOuterSlice = unsafeLinearIndex
instance ( VU.Unbox e
, Index ix
, Index (Lower ix)
, Elt U ix e ~ Elt M ix e
, Elt M ix e ~ Array M (Lower ix) e
) =>
OuterSlice U ix e where
unsafeOuterSlice arr = unsafeOuterSlice (toManifest arr)
instance VU.Unbox e => InnerSlice U Ix1 e where
unsafeInnerSlice arr _ = unsafeLinearIndex arr
instance ( VU.Unbox e
, Index ix
, Index (Lower ix)
, Elt U ix e ~ Elt M ix e
, Elt M ix e ~ Array M (Lower ix) e
) =>
InnerSlice U ix e where
unsafeInnerSlice arr = unsafeInnerSlice (toManifest arr)
instance (VU.Unbox e, Index ix) => Manifest U ix e where
unsafeLinearIndexM (UArray _ _ v) = VU.unsafeIndex v
instance (VU.Unbox e, Index ix) => Mutable U ix e where
data MArray s U ix e = MUArray ix (VU.MVector s e)
msize (MUArray sz _) = sz
unsafeThaw (UArray _ sz v) = MUArray sz <$> VU.unsafeThaw v
unsafeFreeze comp (MUArray sz v) = UArray comp sz <$> VU.unsafeFreeze v
unsafeNew sz = MUArray sz <$> MVU.unsafeNew (totalElem sz)
unsafeNewZero sz = MUArray sz <$> MVU.new (totalElem sz)
unsafeLinearRead (MUArray _ v) i = MVU.unsafeRead v i
unsafeLinearWrite (MUArray _ v) i = MVU.unsafeWrite v i
instance ( VU.Unbox e
, IsList (Array L ix e)
, Nested LN ix e
, Nested L ix e
, Ragged L ix e
) =>
IsList (Array U ix e) where
type Item (Array U ix e) = Item (Array L ix e)
fromList = A.fromLists' Seq
toList = GHC.toList . toListArray