{-# 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 =
[Char] -> r
forall a. HasCallStack => [Char] -> a
error ([Char] -> r) -> [Char] -> r
forall a b. (a -> b) -> a -> b
$
[Char]
"Array representation should never be evaluated: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TypeRep -> [Char]
forall a. Show a => a -> [Char]
show (Proxy r -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy r
forall {k} (t :: k). Proxy t
Proxy :: Proxy r))
data LengthHint
=
LengthExact Sz1
|
LengthMax Sz1
|
LengthUnknown
deriving (LengthHint -> LengthHint -> Bool
(LengthHint -> LengthHint -> Bool)
-> (LengthHint -> LengthHint -> Bool) -> Eq LengthHint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LengthHint -> LengthHint -> Bool
== :: LengthHint -> LengthHint -> Bool
$c/= :: LengthHint -> LengthHint -> Bool
/= :: LengthHint -> LengthHint -> Bool
Eq, Int -> LengthHint -> [Char] -> [Char]
[LengthHint] -> [Char] -> [Char]
LengthHint -> [Char]
(Int -> LengthHint -> [Char] -> [Char])
-> (LengthHint -> [Char])
-> ([LengthHint] -> [Char] -> [Char])
-> Show LengthHint
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> LengthHint -> [Char] -> [Char]
showsPrec :: Int -> LengthHint -> [Char] -> [Char]
$cshow :: LengthHint -> [Char]
show :: LengthHint -> [Char]
$cshowList :: [LengthHint] -> [Char] -> [Char]
showList :: [LengthHint] -> [Char] -> [Char]
Show)
class Index ix => Shape r ix where
linearSizeHint :: Array r ix e -> LengthHint
linearSizeHint = Sz1 -> LengthHint
LengthExact (Sz1 -> LengthHint)
-> (Array r ix e -> Sz1) -> Array r ix e -> LengthHint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Sz1
forall e. Array r ix e -> Sz1
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 = Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Int -> Sz1) -> (Array r ix e -> Int) -> Array r ix e -> Sz1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Int
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 = Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size
{-# INLINE outerSize #-}
maxLinearSize :: Array r ix e -> Maybe Sz1
maxLinearSize = LengthHint -> Maybe Sz1
lengthHintUpperBound (LengthHint -> Maybe Sz1)
-> (Array r ix e -> LengthHint) -> Array r ix e -> Maybe Sz1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> LengthHint
forall e. Array r ix e -> LengthHint
forall r ix e. Shape r ix => Array r ix e -> LengthHint
linearSizeHint
{-# INLINE maxLinearSize #-}
isNull :: Array r ix e -> Bool
isNull = (Sz1
forall ix. Index ix => Sz ix
zeroSz Sz1 -> Sz1 -> Bool
forall a. Eq a => a -> a -> Bool
==) (Sz1 -> Bool) -> (Array r ix e -> Sz1) -> Array r ix e -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Sz1
forall e. Array r ix e -> Sz1
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 -> Sz1 -> Maybe Sz1
forall a. a -> Maybe a
Just Sz1
sz
LengthMax Sz1
sz -> Sz1 -> Maybe Sz1
forall a. a -> Maybe a
Just Sz1
sz
LengthHint
LengthUnknown -> Maybe Sz1
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 -> (ix -> b) -> PrefIndex ix b
forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex (a -> b
f (a -> b) -> (ix -> a) -> ix -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ix -> a
ig)
PrefIndexLinear Int -> a
ig -> (Int -> b) -> PrefIndex ix b
forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (a -> b
f (a -> b) -> (Int -> a) -> Int -> b
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
_ = (Int -> a) -> PrefIndex ix a
forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (a -> Int -> a
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 = Array r ix e -> Int -> e
forall ix. Index ix => Array r ix e -> Int -> e
forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr (Int -> e) -> (ix -> Int) -> ix -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. 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 = Array r ix e -> ix -> e
forall ix. Index ix => Array r ix e -> ix -> e
forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr (ix -> e) -> (Int -> ix) -> Int -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> Int -> ix
forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. 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 = (Int -> e) -> PrefIndex ix e
forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear (Array r ix e -> Int -> e
forall ix. Index ix => Array r ix e -> Int -> e
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 = Sz (Lower ix) -> Array r Int e -> Array r (Lower ix) e
forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
forall ix ix' e.
(Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize Sz (Lower ix)
sz (Array r Int e -> Array r (Lower ix) e)
-> Array r Int e -> Array r (Lower ix) e
forall a b. (a -> b) -> a -> b
$ Int -> Sz1 -> Array r ix e -> Array r Int e
forall ix. Index ix => Int -> Sz1 -> Array r ix e -> Array r Int e
forall r e ix.
(Source r e, Index ix) =>
Int -> Sz1 -> Array r ix e -> Array r Int e
unsafeLinearSlice Int
i (Sz (Lower ix) -> Sz1
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 = Comp -> Sz ix -> (Int -> e) -> Array r ix e
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 (ix -> e) -> (Int -> ix) -> Int -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> Int -> ix
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 = Comp -> Sz ix -> (ix -> e) -> Array r ix e
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 (Int -> e) -> (ix -> Int) -> ix -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> ix -> Int
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 = Comp -> Sz ix -> (Int -> e) -> Array r ix e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz (e -> Int -> e
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 =
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
forall s.
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
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 ((Int -> Sz1 -> e -> ST s ()) -> ST s ())
-> (Int -> Sz1 -> e -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int
offset Sz1
sz e
e ->
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> ST s ()) -> ST s ()
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
offset (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Sz1 -> Int
forall ix. Sz ix -> ix
unSz Sz1
sz)) (Int -> Int -> Int
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 ()
_ = Scheduler s () -> Array r ix e -> (Int -> e -> ST s ()) -> ST s ()
forall s.
Scheduler s () -> Array r ix e -> (Int -> e -> ST s ()) -> 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 = Array r ix e -> Sz ix
forall e. Array r ix e -> Sz ix
forall r ix e. Shape r ix => Array r ix e -> Sz ix
outerSize Array r ix e
arr
MVector s r' e
mvec' <- MArray (PrimState (ST s)) r' Int e
-> Sz1 -> ST s (MArray (PrimState (ST s)) r' Int e)
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
MArray (PrimState (ST s)) r' Int e
mvec (Sz1 -> ST s (MArray (PrimState (ST s)) r' Int e))
-> Sz1 -> ST s (MArray (PrimState (ST s)) r' Int e)
forall a b. (a -> b) -> a -> b
$ Sz ix -> Sz1
forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
forall s.
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
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 ()
forall s. Scheduler s ()
trivialScheduler_ Array r ix e
arr (MArray (PrimState (ST s)) r' Int e -> Int -> e -> ST s ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r' ix e -> Int -> e -> m ()
unsafeLinearWrite MVector s r' e
MArray (PrimState (ST s)) r' Int e
mvec') (MArray (PrimState (ST s)) r' Int e -> Int -> Sz1 -> e -> ST s ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r' ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MVector s r' e
MArray (PrimState (ST s)) r' Int e
mvec')
MArray s r' ix e -> ST s (MArray s r' ix e)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray s r' ix e -> ST s (MArray s r' ix e))
-> MArray s r' ix e -> ST s (MArray s r' ix e)
forall a b. (a -> b) -> a -> b
$ Sz ix -> MVector s r' e -> MArray s r' ix e
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
forall ix' ix s.
(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 = Array r ix e -> Sz ix
forall e. Array r ix e -> Sz ix
forall r ix e. Shape r ix => Array r ix e -> Sz ix
outerSize Array r ix e
arr
MVector RealWorld r' e
mvec' <- MArray (PrimState IO) r' Int e
-> Sz1 -> IO (MArray (PrimState IO) r' Int e)
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
MArray (PrimState IO) r' Int e
mvec (Sz1 -> IO (MArray (PrimState IO) r' Int e))
-> Sz1 -> IO (MArray (PrimState IO) r' Int e)
forall a b. (a -> b) -> a -> b
$ Sz ix -> Sz1
forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
Comp -> (Scheduler RealWorld () -> IO ()) -> IO ()
withMassivScheduler_ (Array r ix e -> Comp
forall r ix e. Strategy r => Array r ix e -> Comp
forall ix e. Array r ix e -> Comp
getComp Array r ix e
arr) ((Scheduler RealWorld () -> IO ()) -> IO ())
-> (Scheduler RealWorld () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Scheduler RealWorld ()
scheduler ->
ST RealWorld () -> IO ()
forall a. ST RealWorld a -> IO a
stToIO (ST RealWorld () -> IO ()) -> ST RealWorld () -> IO ()
forall a b. (a -> b) -> a -> b
$
Scheduler RealWorld ()
-> Array r ix e
-> (Int -> e -> ST RealWorld ())
-> (Int -> Sz1 -> e -> ST RealWorld ())
-> ST RealWorld ()
forall s.
Scheduler s ()
-> Array r ix e
-> (Int -> e -> ST s ())
-> (Int -> Sz1 -> e -> ST s ())
-> ST s ()
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 (MArray (PrimState (ST RealWorld)) r' Int e
-> Int -> e -> ST RealWorld ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r' ix e -> Int -> e -> m ()
unsafeLinearWrite MVector RealWorld r' e
MArray (PrimState (ST RealWorld)) r' Int e
mvec') (MArray (PrimState (ST RealWorld)) r' Int e
-> Int -> Sz1 -> e -> ST RealWorld ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r' ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MVector RealWorld r' e
MArray (PrimState (ST RealWorld)) r' Int e
mvec')
MArray RealWorld r' ix e -> IO (MArray RealWorld r' ix e)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray RealWorld r' ix e -> IO (MArray RealWorld r' ix e))
-> MArray RealWorld r' ix e -> IO (MArray RealWorld r' ix e)
forall a b. (a -> b) -> a -> b
$ Sz ix -> MVector RealWorld r' e -> MArray RealWorld r' ix e
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
forall ix' ix s.
(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 = MVector (PrimState f) r e -> Sz1
forall ix s. Index ix => MArray s r ix e -> Sz ix
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 Sz1 -> Sz1 -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1
mk
then MVector (PrimState f) r e -> f (MVector (PrimState f) r e)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MVector (PrimState f) r e
mvec
else
if Sz1
k Sz1 -> Sz1 -> Bool
forall a. Ord a => a -> a -> Bool
< Sz1
mk
then MVector (PrimState f) r e -> Sz1 -> f (MVector (PrimState f) r e)
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)
forall ix (m :: * -> *).
(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 MVector (PrimState f) r e -> Sz1 -> f (MVector (PrimState f) r e)
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)
forall ix (m :: * -> *).
(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 =
Scheduler s ()
-> Int -> (Int -> e) -> (Int -> e -> ST s ()) -> ST s ()
forall s (m :: * -> *) b.
MonadPrimBase s m =>
Scheduler s () -> Int -> (Int -> b) -> (Int -> b -> m ()) -> m ()
splitLinearlyWith_ Scheduler s ()
scheduler (Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
resultSize) Int -> e
unsafeLinearIndexWithStride
where
!strideIx :: ix
strideIx = Stride ix -> ix
forall ix. Stride ix -> ix
unStride Stride ix
stride
unsafeLinearIndexWithStride :: Int -> e
unsafeLinearIndexWithStride =
Array r ix e -> ix -> e
forall ix. Index ix => Array r ix e -> ix -> e
forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr (ix -> e) -> (Int -> ix) -> Int -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int) -> ix -> ix -> ix
forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 Int -> Int -> Int
forall a. Num a => a -> a -> a
(*) ix
strideIx (ix -> ix) -> (Int -> ix) -> Int -> ix
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> Int -> ix
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 = Sz ix -> m (MArray (PrimState m) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz m (MArray (PrimState m) r ix e)
-> (MArray (PrimState m) r ix e -> m (MArray (PrimState m) r ix e))
-> m (MArray (PrimState m) r ix e)
forall a b. m a -> (a -> m b) -> m b
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 MArray (PrimState m) r ix e
-> m () -> m (MArray (PrimState m) r ix e)
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MArray (PrimState m) r ix e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> m ()
forall ix (m :: * -> *).
(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 = Sz ix -> e -> m (MArray (PrimState m) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(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 <- Sz ix -> m (MArray (PrimState m) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
MArray (PrimState m) r ix e
marr MArray (PrimState m) r ix e
-> m () -> m (MArray (PrimState m) r ix e)
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> Sz1 -> e -> m ()
unsafeLinearSet MArray (PrimState m) r ix e
marr Int
0 (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Sz ix -> Int
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 =
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
offset (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Sz1 -> Int
forall ix. Sz ix -> ix
unSz Sz1
len)) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (\Int
i -> MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(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 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
iFrom
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
iFrom (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
iFrom) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
MArray (PrimState m) r ix' e -> Int -> m e
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix' e
marrFrom Int
i m e -> (e -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marrTo (Int
i Int -> Int -> Int
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
iFrom
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
forall (f :: * -> *) a.
Applicative f =>
Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> f a) -> f ()
loopA_ Int
iFrom (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
iFrom) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marrTo (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta) (Array r ix' e -> Int -> e
forall ix. Index ix => Array r ix e -> Int -> e
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 = MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
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' <- Sz ix -> m (MArray (PrimState m) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
MArray (PrimState m) r ix e
-> Int -> MArray (PrimState m) r ix e -> Int -> Sz1 -> m ()
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 ()
forall ix' ix (m :: * -> *).
(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 (Sz1 -> m ()) -> Sz1 -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem (MArray (PrimState m) r ix e -> Sz ix
forall ix s. Index ix => MArray s r ix e -> Sz ix
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))
MArray (PrimState m) r ix e -> m (MArray (PrimState m) r ix e)
forall a. a -> m a
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' <- Sz ix -> m (MArray (PrimState m) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
MArray (PrimState m) r ix e
-> Int -> MArray (PrimState m) r ix e -> Int -> Sz1 -> m ()
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 ()
forall ix' ix (m :: * -> *).
(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 (Sz1 -> m ()) -> Sz1 -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz)
MArray (PrimState m) r ix e -> m (MArray (PrimState m) r ix e)
forall a. a -> m a
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 = MArray (PrimState m) r ix e -> Int -> m e
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr (Int -> m e) -> (ix -> Int) -> ix -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (MArray (PrimState m) r ix e -> Sz ix
forall ix s. Index ix => MArray s r ix e -> Sz ix
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 = MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr (Int -> e -> m ()) -> (ix -> Int) -> ix -> e -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (MArray (PrimState m) r ix e -> Sz ix
forall ix s. Index ix => MArray s r ix e -> Sz ix
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 <- MArray (PrimState m) r ix e -> Int -> m e
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
forall ix (m :: * -> *).
(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
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(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'
e -> m e
forall a. a -> m a
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 = MArray (PrimState m) r ix e -> (e -> m e) -> Int -> m e
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 (Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (MArray (PrimState m) r ix e -> Sz ix
forall ix s. Index ix => MArray s r ix e -> Sz ix
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 = MArray (PrimState m) r ix e -> Int -> Int -> m (e, e)
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 (Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz ix
ix1) (Sz ix -> ix -> Int
forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz ix
ix2)
where
sz :: Sz ix
sz = MArray (PrimState m) r ix e -> Sz ix
forall ix s. Index ix => MArray s r ix e -> Sz ix
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 <- MArray (PrimState m) r ix e -> Int -> m e
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
forall ix (m :: * -> *).
(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 <- MArray (PrimState m) r ix e -> Int -> m e
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr Int
i2
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(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
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(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
(e, e) -> m (e, e)
forall a. a -> m a
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 = Comp -> Sz ix -> (ix -> e) -> Array r ix e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq Sz ix
forall ix. Index ix => Sz ix
zeroSz (e -> ix -> e
forall a b. a -> b -> a
const (Uninitialized -> e
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 = Comp -> Sz ix -> (ix -> e) -> Array r ix e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq Sz ix
forall ix. Index ix => Sz ix
oneSz ((ix -> e) -> Array r ix e) -> (e -> ix -> e) -> e -> Array r ix e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ix -> e
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 = Either SomeException e -> e
forall a. HasCallStack => Either SomeException a -> a
throwEither (Either SomeException e -> e)
-> (ix -> Either SomeException e) -> ix -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> ix -> Either SomeException e
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
(!?) = 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 m (Array r ix e) -> (Array r ix e -> m e) -> m e
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (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
!? 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 = Array r ix e -> ix -> Maybe e
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 = Array r ix e -> ix -> m e
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 = Border e -> Array r ix e -> ix -> e
forall ix r e.
(Index ix, Manifest r e) =>
Border e -> Array r ix e -> ix -> e
borderIndex (e -> Border e
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 = Border e -> Sz ix -> (ix -> e) -> ix -> e
forall ix e. Index ix => Border e -> Sz ix -> (ix -> e) -> ix -> e
handleBorderIndex Border e
border (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size Array r ix e
arr) (Array r ix e -> ix -> e
forall ix. Index ix => Array r ix e -> ix -> e
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 = Either SomeException e -> e
forall a. HasCallStack => Either SomeException a -> a
throwEither (Array r ix e -> ix -> Either SomeException e
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
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size Array r ix e
arr) ix
ix = e -> m e
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array r ix e -> ix -> e
forall ix. Index ix => Array r ix e -> ix -> e
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 = IndexException -> m e
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM (Sz ix -> ix -> IndexException
forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. 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 = Either SomeException e -> e
forall a. HasCallStack => Either SomeException a -> a
throwEither (Array r ix e -> ix -> Either SomeException e
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 (Bool -> Bool) -> (Array r ix e -> Bool) -> Array r ix e -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Bool
forall e. Array r ix e -> Bool
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 = (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (Int -> Bool) -> (Array r ix e -> Int) -> Array r ix e -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Int
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 (Bool -> Bool) -> (Array r ix e -> Bool) -> Array r ix e -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Bool
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 = Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem (Sz ix -> Int) -> (Array r ix e -> Sz ix) -> Array r ix e -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. 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 #-}