{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Massiv.Core.Common (
Array,
Vector,
Matrix,
MArray,
MVector,
MMatrix,
Steps (..),
Stream (..),
Strategy (..),
Source (..),
PrefIndex (..),
Load (..),
StrideLoad (..),
Size (..),
Shape (..),
Manifest (..),
Mutable,
Comp (..),
Scheduler,
numWorkers,
scheduleWork,
scheduleWork_,
WorkerStates,
unsafeRead,
unsafeWrite,
unsafeModify,
unsafeLinearModify,
unsafeSwap,
unsafeLinearSwap,
unsafeDefaultLinearShrink,
Ragged (..),
empty,
singleton,
elemsCount,
isNotNull,
isEmpty,
isNotEmpty,
Sz (SafeSz),
LengthHint (..),
(!?),
index,
indexM,
(!),
index',
(??),
defaultIndex,
borderIndex,
evaluateM,
evaluate',
inline0,
inline1,
inline2,
module Data.Massiv.Core.Index,
Semigroup ((<>)),
MonadThrow (..),
IndexException (..),
SizeException (..),
ShapeException (..),
module Data.Massiv.Core.Exception,
Proxy (..),
Id (..),
runST,
ST,
MonadUnliftIO (..),
MonadIO (liftIO),
PrimMonad (PrimState),
RealWorld,
) where
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup((<>)))
#endif
import Control.Monad.Catch (MonadThrow (..))
import Control.Monad.IO.Unlift (MonadIO (liftIO), MonadUnliftIO (..))
import Control.Monad.Primitive
import Control.Monad.ST
import Control.Scheduler (
Comp (..),
Scheduler,
WorkerStates,
numWorkers,
scheduleWork,
scheduleWork_,
trivialScheduler_,
)
import Data.Kind
import Data.Massiv.Core.Exception
import Data.Massiv.Core.Index
import Data.Massiv.Core.Index.Internal (Sz (SafeSz))
import Data.Typeable
import qualified Data.Stream.Monadic as S (Stream)
import Data.Vector.Fusion.Util
import GHC.Exts (IsList)
#include "massiv.h"
data family Array r ix e :: Type
type Vector r e = Array r Ix1 e
type Matrix r e = Array r Ix2 e
data family MArray s r ix e :: Type
type MVector s r e = MArray s r Ix1 e
type MMatrix s r e = MArray s r Ix2 e
class Load r ix e => Stream r ix e where
toStream :: Array r ix e -> Steps Id e
toStreamIx :: Array r ix e -> Steps Id (ix, e)
data Steps m e = Steps
{ forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream :: S.Stream m e
, forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize :: LengthHint
}
class Typeable r => Strategy r where
setComp :: Comp -> Array r ix e -> Array r ix e
getComp :: Array r ix e -> Comp
repr :: r
repr =
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Array representation should never be evaluated: "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy :: Proxy r))
data LengthHint
=
LengthExact Sz1
|
LengthMax Sz1
|
LengthUnknown
deriving (LengthHint -> LengthHint -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LengthHint -> LengthHint -> Bool
$c/= :: LengthHint -> LengthHint -> Bool
== :: LengthHint -> LengthHint -> Bool
$c== :: LengthHint -> LengthHint -> Bool
Eq, Int -> LengthHint -> ShowS
[LengthHint] -> ShowS
LengthHint -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [LengthHint] -> ShowS
$cshowList :: [LengthHint] -> ShowS
show :: LengthHint -> [Char]
$cshow :: LengthHint -> [Char]
showsPrec :: Int -> LengthHint -> ShowS
$cshowsPrec :: Int -> LengthHint -> ShowS
Show)
class Index ix => Shape r ix where
linearSizeHint :: Array r ix e -> LengthHint
linearSizeHint = Sz1 -> LengthHint
LengthExact forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Shape r ix => Array r ix e -> Sz1
linearSize
{-# INLINE linearSizeHint #-}
linearSize :: Array r ix e -> Sz1
default linearSize :: Size r => Array r ix e -> Sz1
linearSize = forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount
{-# INLINE linearSize #-}
outerSize :: Array r ix e -> Sz ix
default outerSize :: Size r => Array r ix e -> Sz ix
outerSize = forall r ix e. Size r => Array r ix e -> Sz ix
size
{-# INLINE outerSize #-}
maxLinearSize :: Array r ix e -> Maybe Sz1
maxLinearSize = LengthHint -> Maybe Sz1
lengthHintUpperBound forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Shape r ix => Array r ix e -> LengthHint
linearSizeHint
{-# INLINE maxLinearSize #-}
isNull :: Array r ix e -> Bool
isNull = (forall ix. Index ix => Sz ix
zeroSz forall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Shape r ix => Array r ix e -> Sz1
linearSize
{-# INLINE isNull #-}
lengthHintUpperBound :: LengthHint -> Maybe Sz1
lengthHintUpperBound :: LengthHint -> Maybe Sz1
lengthHintUpperBound = \case
LengthExact Sz1
sz -> forall a. a -> Maybe a
Just Sz1
sz
LengthMax Sz1
sz -> forall a. a -> Maybe a
Just Sz1
sz
LengthHint
LengthUnknown -> forall a. Maybe a
Nothing
{-# INLINE lengthHintUpperBound #-}
class Size r where
size :: Array r ix e -> Sz ix
unsafeResize :: (Index ix, Index ix') => Sz ix' -> Array r ix e -> Array r ix' e
data PrefIndex ix e
= PrefIndex (ix -> e)
| PrefIndexLinear (Int -> e)
instance Functor (PrefIndex ix) where
fmap :: forall a b. (a -> b) -> PrefIndex ix a -> PrefIndex ix b
fmap a -> b
f = \case
PrefIndex ix -> a
ig -> forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. ix -> a
ig)
PrefIndexLinear Int -> a
ig -> forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
ig)
{-# INLINE fmap #-}
<$ :: forall a b. a -> PrefIndex ix b -> PrefIndex ix a
(<$) a
e PrefIndex ix b
_ = forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (forall a b. a -> b -> a
const a
e)
{-# INLINE (<$) #-}
class (Strategy r, Size r) => Source r e where
{-# MINIMAL (unsafeIndex | unsafeLinearIndex), unsafeLinearSlice #-}
unsafeIndex :: HAS_CALL_STACK => Index ix => Array r ix e -> ix -> e
unsafeIndex !Array r ix e
arr = forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)
{-# INLINE unsafeIndex #-}
unsafeLinearIndex :: HAS_CALL_STACK => Index ix => Array r ix e -> Int -> e
unsafeLinearIndex !Array r ix e
arr = forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)
{-# INLINE unsafeLinearIndex #-}
unsafePrefIndex :: HAS_CALL_STACK => Index ix => Array r ix e -> PrefIndex ix e
unsafePrefIndex !Array r ix e
arr = forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr)
{-# INLINE unsafePrefIndex #-}
unsafeOuterSlice
:: HAS_CALL_STACK
=> (Index ix, Index (Lower ix))
=> Array r ix e
-> Sz (Lower ix)
-> Int
-> Array r (Lower ix) e
unsafeOuterSlice Array r ix e
arr Sz (Lower ix)
sz Int
i = forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize Sz (Lower ix)
sz forall a b. (a -> b) -> a -> b
$ forall r e ix.
(Source r e, Index ix) =>
Int -> Sz1 -> Array r ix e -> Array r Int e
unsafeLinearSlice Int
i (forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz (Lower ix)
sz) Array r ix e
arr
{-# INLINE unsafeOuterSlice #-}
unsafeLinearSlice :: Index ix => Ix1 -> Sz1 -> Array r ix e -> Array r Ix1 e
class (Strategy r, Shape r ix) => Load r ix e where
{-# MINIMAL (makeArray | makeArrayLinear), (iterArrayLinearST_ | iterArrayLinearWithSetST_) #-}
makeArray
:: Comp
-> Sz ix
-> (ix -> e)
-> Array r ix e
makeArray Comp
comp Sz ix
sz ix -> e
f = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz (ix -> e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE makeArray #-}
makeArrayLinear :: Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz Int -> e
f = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz ix
sz (Int -> e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz)
{-# INLINE makeArrayLinear #-}
replicate :: Comp -> Sz ix -> e -> Array r ix e
replicate Comp
comp Sz ix
sz !e
e = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz (forall a b. a -> b -> a
const e
e)
{-# INLINE replicate #-}
iterArrayLinearST_
:: Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> ST s ()
iterArrayLinearST_ Scheduler s ()
scheduler Array r ix e
arr Int -> e -> ST s ()
uWrite =
forall r ix e s.
Load r ix e =>
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
iterArrayLinearWithSetST_ Scheduler s ()
scheduler Array r ix e
arr Int -> e -> ST s ()
uWrite forall a b. (a -> b) -> a -> b
$ \Int
offset Sz1
sz e
e ->
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
offset (forall a. Ord a => a -> a -> Bool
< (Int
offset forall a. Num a => a -> a -> a
+ forall ix. Sz ix -> ix
unSz Sz1
sz)) (forall a. Num a => a -> a -> a
+ Int
1) (Int -> e -> ST s ()
`uWrite` e
e)
{-# INLINE iterArrayLinearST_ #-}
iterArrayLinearWithSetST_
:: Scheduler s ()
-> Array r ix e
-> (Ix1 -> e -> ST s ())
-> (Ix1 -> Sz1 -> e -> ST s ())
-> ST s ()
iterArrayLinearWithSetST_ Scheduler s ()
scheduler Array r ix e
arr Int -> e -> ST s ()
uWrite Int -> Sz1 -> e -> ST s ()
_ = forall r ix e s.
Load r ix e =>
Scheduler s () -> Array r ix e -> (Int -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ Scheduler s ()
scheduler Array r ix e
arr Int -> e -> ST s ()
uWrite
{-# INLINE iterArrayLinearWithSetST_ #-}
unsafeLoadIntoST
:: Manifest r' e
=> MVector s r' e
-> Array r ix e
-> ST s (MArray s r' ix e)
unsafeLoadIntoST MVector s r' e
mvec Array r ix e
arr = do
let sz :: Sz ix
sz = forall r ix e. Shape r ix => Array r ix e -> Sz ix
outerSize Array r ix e
arr
MVector s r' e
mvec' <- forall r e (f :: * -> *).
(Manifest r e, PrimMonad f) =>
MVector (PrimState f) r e -> Sz1 -> f (MVector (PrimState f) r e)
resizeMVector MVector s r' e
mvec forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
forall r ix e s.
Load r ix e =>
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
iterArrayLinearWithSetST_ forall s. Scheduler s ()
trivialScheduler_ Array r ix e
arr (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MVector s r' e
mvec') (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MVector s r' e
mvec')
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall r e ix' ix s.
(Manifest r e, Index ix', Index ix) =>
Sz ix' -> MArray s r ix e -> MArray s r ix' e
unsafeResizeMArray Sz ix
sz MVector s r' e
mvec'
{-# INLINE unsafeLoadIntoST #-}
unsafeLoadIntoIO
:: Manifest r' e
=> MVector RealWorld r' e
-> Array r ix e
-> IO (MArray RealWorld r' ix e)
unsafeLoadIntoIO MVector RealWorld r' e
mvec Array r ix e
arr = do
let sz :: Sz ix
sz = forall r ix e. Shape r ix => Array r ix e -> Sz ix
outerSize Array r ix e
arr
MVector RealWorld r' e
mvec' <- forall r e (f :: * -> *).
(Manifest r e, PrimMonad f) =>
MVector (PrimState f) r e -> Sz1 -> f (MVector (PrimState f) r e)
resizeMVector MVector RealWorld r' e
mvec forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
Comp -> (Scheduler RealWorld () -> IO ()) -> IO ()
withMassivScheduler_ (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) forall a b. (a -> b) -> a -> b
$ \Scheduler RealWorld ()
scheduler ->
forall a. ST RealWorld a -> IO a
stToIO forall a b. (a -> b) -> a -> b
$
forall r ix e s.
Load r ix e =>
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
iterArrayLinearWithSetST_ Scheduler RealWorld ()
scheduler Array r ix e
arr (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MVector RealWorld r' e
mvec') (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MVector RealWorld r' e
mvec')
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall r e ix' ix s.
(Manifest r e, Index ix', Index ix) =>
Sz ix' -> MArray s r ix e -> MArray s r ix' e
unsafeResizeMArray Sz ix
sz MVector RealWorld r' e
mvec'
{-# INLINE unsafeLoadIntoIO #-}
resizeMVector
:: (Manifest r e, PrimMonad f)
=> MVector (PrimState f) r e
-> Sz1
-> f (MVector (PrimState f) r e)
resizeMVector :: forall r e (f :: * -> *).
(Manifest r e, PrimMonad f) =>
MVector (PrimState f) r e -> Sz1 -> f (MVector (PrimState f) r e)
resizeMVector MVector (PrimState f) r e
mvec Sz1
k =
let mk :: Sz1
mk = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MVector (PrimState f) r e
mvec
in if Sz1
k forall a. Eq a => a -> a -> Bool
== Sz1
mk
then forall (f :: * -> *) a. Applicative f => a -> f a
pure MVector (PrimState f) r e
mvec
else
if Sz1
k forall a. Ord a => a -> a -> Bool
< Sz1
mk
then forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MVector (PrimState f) r e
mvec Sz1
k
else forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearGrow MVector (PrimState f) r e
mvec Sz1
k
{-# INLINE resizeMVector #-}
class Load r ix e => StrideLoad r ix e where
iterArrayLinearWithStrideST_
:: Scheduler s ()
-> Stride ix
-> Sz ix
-> Array r ix e
-> (Int -> e -> ST s ())
-> ST s ()
default iterArrayLinearWithStrideST_
:: Source r e
=> Scheduler s ()
-> Stride ix
-> Sz ix
-> Array r ix e
-> (Int -> e -> ST s ())
-> ST s ()
iterArrayLinearWithStrideST_ Scheduler s ()
scheduler Stride ix
stride Sz ix
resultSize Array r ix e
arr =
forall s (m :: * -> *) b.
MonadPrimBase s m =>
Scheduler s () -> Int -> (Int -> b) -> (Int -> b -> m ()) -> m ()
splitLinearlyWith_ Scheduler s ()
scheduler (forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
resultSize) Int -> e
unsafeLinearIndexWithStride
where
!strideIx :: ix
strideIx = forall ix. Stride ix -> ix
unStride Stride ix
stride
unsafeLinearIndexWithStride :: Int -> e
unsafeLinearIndexWithStride =
forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 forall a. Num a => a -> a -> a
(*) ix
strideIx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex Sz ix
resultSize
{-# INLINE unsafeLinearIndexWithStride #-}
{-# INLINE iterArrayLinearWithStrideST_ #-}
type Mutable r e = Manifest r e
{-# DEPRECATED Mutable "In favor of `Manifest`" #-}
class Source r e => Manifest r e where
unsafeLinearIndexM :: Index ix => Array r ix e -> Int -> e
sizeOfMArray :: Index ix => MArray s r ix e -> Sz ix
unsafeResizeMArray :: (Index ix', Index ix) => Sz ix' -> MArray s r ix e -> MArray s r ix' e
unsafeLinearSliceMArray :: Index ix => Ix1 -> Sz1 -> MArray s r ix e -> MVector s r e
unsafeThaw :: (Index ix, PrimMonad m) => Array r ix e -> m (MArray (PrimState m) r ix e)
unsafeFreeze :: (Index ix, PrimMonad m) => Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeNew :: (Index ix, PrimMonad m) => Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearRead
:: HAS_CALL_STACK
=> (Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Int
-> m e
unsafeLinearWrite
:: HAS_CALL_STACK
=> (Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Int
-> e
-> m ()
initialize :: (Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> m ()
initializeNew :: (Index ix, PrimMonad m) => Maybe e -> Sz ix -> m (MArray (PrimState m) r ix e)
initializeNew Maybe e
Nothing Sz ix
sz = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \MArray (PrimState m) r ix e
ma -> MArray (PrimState m) r ix e
ma forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> m ()
initialize MArray (PrimState m) r ix e
ma
initializeNew (Just e
e) Sz ix
sz = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e
{-# INLINE initializeNew #-}
newMArray :: (Index ix, PrimMonad m) => Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e = do
MArray (PrimState m) r ix e
marr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
MArray (PrimState m) r ix e
marr forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MArray (PrimState m) r ix e
marr Int
0 (forall ix. ix -> Sz ix
SafeSz (forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz)) e
e
{-# INLINE newMArray #-}
unsafeLinearSet
:: HAS_CALL_STACK
=> (Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Ix1
-> Sz1
-> e
-> m ()
unsafeLinearSet MArray (PrimState m) r ix e
marr Int
offset Sz1
len e
e =
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
offset (forall a. Ord a => a -> a -> Bool
< (Int
offset forall a. Num a => a -> a -> a
+ forall ix. Sz ix -> ix
unSz Sz1
len)) (forall a. Num a => a -> a -> a
+ Int
1) (\Int
i -> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Int
i e
e)
{-# INLINE unsafeLinearSet #-}
unsafeLinearCopy
:: HAS_CALL_STACK
=> (Index ix', Index ix, PrimMonad m)
=> MArray (PrimState m) r ix' e
-> Ix1
-> MArray (PrimState m) r ix e
-> Ix1
-> Sz1
-> m ()
unsafeLinearCopy MArray (PrimState m) r ix' e
marrFrom Int
iFrom MArray (PrimState m) r ix e
marrTo Int
iTo (SafeSz Int
k) = do
let delta :: Int
delta = Int
iTo forall a. Num a => a -> a -> a
- Int
iFrom
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
iFrom (forall a. Ord a => a -> a -> Bool
< Int
k forall a. Num a => a -> a -> a
+ Int
iFrom) (forall a. Num a => a -> a -> a
+ Int
1) forall a b. (a -> b) -> a -> b
$ \Int
i ->
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix' e
marrFrom Int
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marrTo (Int
i forall a. Num a => a -> a -> a
+ Int
delta)
{-# INLINE unsafeLinearCopy #-}
unsafeArrayLinearCopy
:: HAS_CALL_STACK
=> (Index ix', Index ix, PrimMonad m)
=> Array r ix' e
-> Ix1
-> MArray (PrimState m) r ix e
-> Ix1
-> Sz1
-> m ()
unsafeArrayLinearCopy Array r ix' e
arrFrom Int
iFrom MArray (PrimState m) r ix e
marrTo Int
iTo (SafeSz Int
k) = do
let delta :: Int
delta = Int
iTo forall a. Num a => a -> a -> a
- Int
iFrom
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
iFrom (forall a. Ord a => a -> a -> Bool
< Int
k forall a. Num a => a -> a -> a
+ Int
iFrom) (forall a. Num a => a -> a -> a
+ Int
1) forall a b. (a -> b) -> a -> b
$ \Int
i ->
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marrTo (Int
i forall a. Num a => a -> a -> a
+ Int
delta) (forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix' e
arrFrom Int
i)
{-# INLINE unsafeArrayLinearCopy #-}
unsafeLinearShrink
:: HAS_CALL_STACK
=> (Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Sz ix
-> m (MArray (PrimState m) r ix e)
unsafeLinearShrink = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeDefaultLinearShrink
{-# INLINE unsafeLinearShrink #-}
unsafeLinearGrow
:: HAS_CALL_STACK
=> (Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Sz ix
-> m (MArray (PrimState m) r ix e)
unsafeLinearGrow MArray (PrimState m) r ix e
marr Sz ix
sz = do
MArray (PrimState m) r ix e
marr' <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
forall r e ix' ix (m :: * -> *).
(Manifest r e, Index ix', Index ix, PrimMonad m) =>
MArray (PrimState m) r ix' e
-> Int -> MArray (PrimState m) r ix e -> Int -> Sz1 -> m ()
unsafeLinearCopy MArray (PrimState m) r ix e
marr Int
0 MArray (PrimState m) r ix e
marr' Int
0 forall a b. (a -> b) -> a -> b
$ forall ix. ix -> Sz ix
SafeSz (forall ix. Index ix => Sz ix -> Int
totalElem (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r ix e
marr))
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray (PrimState m) r ix e
marr'
{-# INLINE unsafeLinearGrow #-}
unsafeDefaultLinearShrink
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Sz ix
-> m (MArray (PrimState m) r ix e)
unsafeDefaultLinearShrink :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeDefaultLinearShrink MArray (PrimState m) r ix e
marr Sz ix
sz = do
MArray (PrimState m) r ix e
marr' <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
forall r e ix' ix (m :: * -> *).
(Manifest r e, Index ix', Index ix, PrimMonad m) =>
MArray (PrimState m) r ix' e
-> Int -> MArray (PrimState m) r ix e -> Int -> Sz1 -> m ()
unsafeLinearCopy MArray (PrimState m) r ix e
marr Int
0 MArray (PrimState m) r ix e
marr' Int
0 forall a b. (a -> b) -> a -> b
$ forall ix. ix -> Sz ix
SafeSz (forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray (PrimState m) r ix e
marr'
{-# INLINE unsafeDefaultLinearShrink #-}
unsafeRead
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> ix
-> m e
unsafeRead :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> m e
unsafeRead MArray (PrimState m) r ix e
marr = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r ix e
marr)
{-# INLINE unsafeRead #-}
unsafeWrite
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> ix
-> e
-> m ()
unsafeWrite :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> e -> m ()
unsafeWrite MArray (PrimState m) r ix e
marr = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r ix e
marr)
{-# INLINE unsafeWrite #-}
unsafeLinearModify
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> Int
-> m e
unsafeLinearModify :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> Int -> m e
unsafeLinearModify !MArray (PrimState m) r ix e
marr e -> m e
f !Int
i = do
e
v <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr Int
i
e
v' <- e -> m e
f e
v
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Int
i e
v'
forall (f :: * -> *) a. Applicative f => a -> f a
pure e
v
{-# INLINE unsafeLinearModify #-}
unsafeModify
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> ix
-> m e
unsafeModify :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m e
unsafeModify MArray (PrimState m) r ix e
marr e -> m e
f ix
ix = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> Int -> m e
unsafeLinearModify MArray (PrimState m) r ix e
marr e -> m e
f (forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r ix e
marr) ix
ix)
{-# INLINE unsafeModify #-}
unsafeSwap
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> ix
-> ix
-> m (e, e)
unsafeSwap :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m (e, e)
unsafeSwap !MArray (PrimState m) r ix e
marr !ix
ix1 !ix
ix2 = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Int -> m (e, e)
unsafeLinearSwap MArray (PrimState m) r ix e
marr (forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz ix
ix1) (forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz ix
ix2)
where
sz :: Sz ix
sz = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r ix e
marr
{-# INLINE unsafeSwap #-}
unsafeLinearSwap
:: HAS_CALL_STACK
=> (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> Int
-> Int
-> m (e, e)
unsafeLinearSwap :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Int -> m (e, e)
unsafeLinearSwap !MArray (PrimState m) r ix e
marr !Int
i1 !Int
i2 = do
e
val1 <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr Int
i1
e
val2 <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr Int
i2
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Int
i1 e
val2
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Int
i2 e
val1
forall (m :: * -> *) a. Monad m => a -> m a
return (e
val1, e
val2)
{-# INLINE unsafeLinearSwap #-}
class (IsList (Array r ix e), Load r ix e) => Ragged r ix e where
generateRaggedM :: Monad m => Comp -> Sz ix -> (ix -> m e) -> m (Array r ix e)
flattenRagged :: Array r ix e -> Vector r e
loadRaggedST
:: Scheduler s () -> Array r ix e -> (Ix1 -> e -> ST s ()) -> Ix1 -> Ix1 -> Sz ix -> ST s ()
raggedFormat :: (e -> String) -> String -> Array r ix e -> String
empty
:: forall r ix e
. Load r ix e
=> Array r ix e
empty :: forall r ix e. Load r ix e => Array r ix e
empty = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq forall ix. Index ix => Sz ix
zeroSz (forall a b. a -> b -> a
const (forall e a. (HasCallStack, Exception e) => e -> a
throwImpossible Uninitialized
Uninitialized))
{-# INLINE empty #-}
singleton
:: forall r ix e
. Load r ix e
=> e
-> Array r ix e
singleton :: forall r ix e. Load r ix e => e -> Array r ix e
singleton = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq forall ix. Index ix => Sz ix
oneSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
{-# INLINE singleton #-}
infixl 4 !, !?, ??
(!)
:: forall r ix e
. (HasCallStack, Manifest r e, Index ix)
=> Array r ix e
-> ix
-> e
! :: forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
(!) Array r ix e
arr = forall a. HasCallStack => Either SomeException a -> a
throwEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e (m :: * -> *).
(Index ix, Source r e, MonadThrow m) =>
Array r ix e -> ix -> m e
evaluateM Array r ix e
arr
{-# INLINE (!) #-}
(!?)
:: forall r ix e m
. (Index ix, Manifest r e, MonadThrow m)
=> Array r ix e
-> ix
-> m e
!? :: forall r ix e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
Array r ix e -> ix -> m e
(!?) = forall ix r e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
Array r ix e -> ix -> m e
indexM
{-# INLINE (!?) #-}
(??) :: (Index ix, Manifest r e, MonadThrow m) => m (Array r ix e) -> ix -> m e
?? :: forall ix r e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
m (Array r ix e) -> ix -> m e
(??) m (Array r ix e)
marr ix
ix = m (Array r ix e)
marr forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall r ix e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
Array r ix e -> ix -> m e
!? ix
ix)
{-# INLINE (??) #-}
index :: (Index ix, Manifest r e) => Array r ix e -> ix -> Maybe e
index :: forall ix r e.
(Index ix, Manifest r e) =>
Array r ix e -> ix -> Maybe e
index = forall ix r e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
Array r ix e -> ix -> m e
indexM
{-# INLINE index #-}
indexM :: (Index ix, Manifest r e, MonadThrow m) => Array r ix e -> ix -> m e
indexM :: forall ix r e (m :: * -> *).
(Index ix, Manifest r e, MonadThrow m) =>
Array r ix e -> ix -> m e
indexM = forall ix r e (m :: * -> *).
(Index ix, Source r e, MonadThrow m) =>
Array r ix e -> ix -> m e
evaluateM
{-# INLINE indexM #-}
defaultIndex :: (Index ix, Manifest r e) => e -> Array r ix e -> ix -> e
defaultIndex :: forall ix r e.
(Index ix, Manifest r e) =>
e -> Array r ix e -> ix -> e
defaultIndex e
defVal = forall ix r e.
(Index ix, Manifest r e) =>
Border e -> Array r ix e -> ix -> e
borderIndex (forall e. e -> Border e
Fill e
defVal)
{-# INLINE defaultIndex #-}
borderIndex :: (Index ix, Manifest r e) => Border e -> Array r ix e -> ix -> e
borderIndex :: forall ix r e.
(Index ix, Manifest r e) =>
Border e -> Array r ix e -> ix -> e
borderIndex Border e
border Array r ix e
arr = forall ix e. Index ix => Border e -> Sz ix -> (ix -> e) -> ix -> e
handleBorderIndex Border e
border (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr) (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr)
{-# INLINE borderIndex #-}
index' :: (HasCallStack, Index ix, Manifest r e) => Array r ix e -> ix -> e
index' :: forall ix r e.
(HasCallStack, Index ix, Manifest r e) =>
Array r ix e -> ix -> e
index' Array r ix e
arr ix
ix = forall a. HasCallStack => Either SomeException a -> a
throwEither (forall ix r e (m :: * -> *).
(Index ix, Source r e, MonadThrow m) =>
Array r ix e -> ix -> m e
evaluateM Array r ix e
arr ix
ix)
{-# INLINE index' #-}
evaluateM :: (Index ix, Source r e, MonadThrow m) => Array r ix e -> ix -> m e
evaluateM :: forall ix r e (m :: * -> *).
(Index ix, Source r e, MonadThrow m) =>
Array r ix e -> ix -> m e
evaluateM Array r ix e
arr ix
ix
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr) ix
ix = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr ix
ix)
| Bool
otherwise = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr) ix
ix)
{-# INLINE evaluateM #-}
evaluate' :: (HasCallStack, Index ix, Source r e) => Array r ix e -> ix -> e
evaluate' :: forall ix r e.
(HasCallStack, Index ix, Source r e) =>
Array r ix e -> ix -> e
evaluate' Array r ix e
arr ix
ix = forall a. HasCallStack => Either SomeException a -> a
throwEither (forall ix r e (m :: * -> *).
(Index ix, Source r e, MonadThrow m) =>
Array r ix e -> ix -> m e
evaluateM Array r ix e
arr ix
ix)
{-# INLINE evaluate' #-}
isNotNull :: Shape r ix => Array r ix e -> Bool
isNotNull :: forall r ix e. Shape r ix => Array r ix e -> Bool
isNotNull = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Shape r ix => Array r ix e -> Bool
isNull
{-# INLINE isNotNull #-}
isEmpty :: (Index ix, Size r) => Array r ix e -> Bool
isEmpty :: forall ix r e. (Index ix, Size r) => Array r ix e -> Bool
isEmpty = (forall a. Eq a => a -> a -> Bool
== Int
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount
{-# INLINE isEmpty #-}
isNotEmpty :: (Index ix, Size r) => Array r ix e -> Bool
isNotEmpty :: forall ix r e. (Index ix, Size r) => Array r ix e -> Bool
isNotEmpty = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Bool
isEmpty
{-# INLINE isNotEmpty #-}
elemsCount :: (Index ix, Size r) => Array r ix e -> Int
elemsCount :: forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount = forall ix. Index ix => Sz ix -> Int
totalElem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Size r => Array r ix e -> Sz ix
size
{-# INLINE elemsCount #-}
inline0 :: (a -> b) -> a -> b
inline0 :: forall a b. (a -> b) -> a -> b
inline0 a -> b
f = a -> b
f
{-# INLINE [0] inline0 #-}
inline1 :: (a -> b) -> a -> b
inline1 :: forall a b. (a -> b) -> a -> b
inline1 a -> b
f = a -> b
f
{-# INLINE [1] inline1 #-}
inline2 :: (a -> b) -> a -> b
inline2 :: forall a b. (a -> b) -> a -> b
inline2 a -> b
f = a -> b
f
{-# INLINE [2] inline2 #-}