module Data.Massiv.Array.Manifest.BoxedStrict
( B (..)
, Array(..)
) where
import Control.DeepSeq (NFData (..))
import qualified Data.Foldable as F (Foldable (..))
import Data.Massiv.Array.Delayed.Internal (eq, ord)
import Data.Massiv.Array.Manifest.BoxedNF (deepseqArray,
deepseqArrayP)
import Data.Massiv.Array.Unsafe (unsafeGenerateArray,
unsafeGenerateArrayP)
import Data.Massiv.Array.Manifest.Internal
import Data.Massiv.Array.Manifest.List as A
import Data.Massiv.Array.Mutable
import Data.Massiv.Array.Ops.Fold
import Data.Massiv.Core.Common
import Data.Massiv.Core.List
import qualified Data.Primitive.Array as A
import GHC.Base (build)
import GHC.Exts as GHC (IsList (..))
import Prelude hiding (mapM)
data B = B deriving Show
type instance EltRepr B ix = M
data instance Array B ix e = BArray { bComp :: !Comp
, bSize :: !ix
, bData :: !(A.Array e)
}
instance (Index ix, NFData e) => NFData (Array B ix e) where
rnf (BArray comp sz arr) =
case comp of
Seq -> deepseqArray sz arr ()
ParOn wIds -> deepseqArrayP wIds sz arr ()
instance (Index ix, Eq e) => Eq (Array B ix e) where
(==) = eq (==)
instance (Index ix, Ord e) => Ord (Array B ix e) where
compare = ord compare
instance Index ix => Construct B ix e where
getComp = bComp
setComp c arr = arr { bComp = c }
unsafeMakeArray Seq !sz f = unsafeGenerateArray sz f
unsafeMakeArray (ParOn wIds) !sz f = unsafeGenerateArrayP wIds sz f
instance Index ix => Source B ix e where
unsafeLinearIndex (BArray _ _ a) = A.indexArray a
instance Index ix => Size B ix e where
size = bSize
unsafeResize !sz !arr = arr { bSize = sz }
unsafeExtract !sIx !newSz !arr = unsafeExtract sIx newSz (toManifest arr)
instance ( NFData e
, Index ix
, Index (Lower ix)
, Elt M ix e ~ Array M (Lower ix) e
, Elt B ix e ~ Array M (Lower ix) e
) =>
OuterSlice B ix e where
unsafeOuterSlice arr = unsafeOuterSlice (toManifest arr)
instance ( NFData e
, Index ix
, Index (Lower ix)
, Elt M ix e ~ Array M (Lower ix) e
, Elt B ix e ~ Array M (Lower ix) e
) =>
InnerSlice B ix e where
unsafeInnerSlice arr = unsafeInnerSlice (toManifest arr)
instance Index ix => Manifest B ix e where
unsafeLinearIndexM (BArray _ _ a) = A.indexArray a
uninitialized :: a
uninitialized = error "Data.Array.Massiv.Manifest.BoxedStrict: uninitialized element"
instance Index ix => Mutable B ix e where
data MArray s B ix e = MBArray !ix !(A.MutableArray s e)
msize (MBArray sz _) = sz
unsafeThaw (BArray _ sz a) = MBArray sz <$> A.unsafeThawArray a
unsafeFreeze comp (MBArray sz ma) = BArray comp sz <$> A.unsafeFreezeArray ma
unsafeNew sz = MBArray sz <$> A.newArray (totalElem sz) uninitialized
unsafeNewZero = unsafeNew
unsafeLinearRead (MBArray _ ma) i = A.readArray ma i
unsafeLinearWrite (MBArray _ ma) i e = e `seq` A.writeArray ma i e
instance Index ix => Foldable (Array B ix) where
foldl = lazyFoldlS
foldl' = foldlS
foldr = foldrFB
foldr' = foldrS
null (BArray _ sz _) = totalElem sz == 0
sum = F.foldl' (+) 0
product = F.foldl' (*) 1
length = totalElem . size
toList arr = build (\ c n -> foldrFB c n arr)
instance ( IsList (Array L ix e)
, Nested LN ix e
, Nested L ix e
, Ragged L ix e
) =>
IsList (Array B ix e) where
type Item (Array B ix e) = Item (Array L ix e)
fromList = A.fromLists' Seq
toList = GHC.toList . toListArray