{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Massiv.Array.Mutable (
sizeOfMArray,
msize,
resizeMArrayM,
flattenMArray,
outerSliceMArrayM,
outerSlicesMArray,
read,
readM,
write,
write_,
writeM,
modify,
modify_,
modifyM,
modifyM_,
swap,
swap_,
swapM,
swapM_,
zipSwapM_,
thaw,
thawS,
freeze,
freezeS,
newMArray,
newMArray',
makeMArray,
makeMArrayLinear,
makeMArrayS,
makeMArrayLinearS,
createArray_,
createArray,
createArrayS_,
createArrayS,
createArrayST_,
createArrayST,
generateArray,
generateArrayLinear,
generateArrayS,
generateArrayLinearS,
generateSplitSeedArray,
generateArrayWS,
generateArrayLinearWS,
unfoldrPrimM_,
iunfoldrPrimM_,
unfoldrPrimM,
iunfoldrPrimM,
unfoldlPrimM_,
iunfoldlPrimM_,
unfoldlPrimM,
iunfoldlPrimM,
forPrimM,
forPrimM_,
iforPrimM,
iforPrimM_,
iforLinearPrimM,
iforLinearPrimM_,
for2PrimM_,
ifor2PrimM_,
withMArray,
withMArray_,
withLoadMArray_,
withMArrayS,
withLoadMArrayS,
withMArrayS_,
withLoadMArrayS_,
withMArrayST,
withLoadMArrayST,
withMArrayST_,
withLoadMArrayST_,
initialize,
initializeNew,
Manifest,
MArray,
RealWorld,
computeInto,
loadArray,
loadArrayS,
) where
import Control.Monad (unless, void, when, (>=>))
import Control.Monad.Primitive
import Control.Monad.ST
import Control.Scheduler
import Data.IORef
import Data.Massiv.Array.Delayed.Pull (D)
import Data.Massiv.Array.Mutable.Internal
import Data.Massiv.Core.Common
import Data.Maybe (fromMaybe)
import System.IO.Unsafe (unsafePerformIO)
import Prelude hiding (mapM, read)
resizeMArrayM
:: (Manifest r e, Index ix', Index ix, MonadThrow m)
=> Sz ix'
-> MArray s r ix e
-> m (MArray s r ix' e)
resizeMArrayM :: forall r e ix' ix (m :: * -> *) s.
(Manifest r e, Index ix', Index ix, MonadThrow m) =>
Sz ix' -> MArray s r ix e -> m (MArray s r ix' e)
resizeMArrayM Sz ix'
sz MArray s r ix e
marr =
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 MArray s r ix e
marr forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *) ix ix'.
(MonadThrow m, Index ix, Index ix') =>
Sz ix -> Sz ix' -> m ()
guardNumberOfElements (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r ix e
marr) Sz ix'
sz
{-# INLINE resizeMArrayM #-}
flattenMArray :: (Manifest r e, Index ix) => MArray s r ix e -> MVector s r e
flattenMArray :: forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> MVector s r e
flattenMArray MArray s r ix e
marr = 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 (forall ix. Index ix => Sz ix -> Sz1
toLinearSz (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r ix e
marr)) MArray s r ix e
marr
{-# INLINE flattenMArray #-}
outerSliceMArrayM
:: forall r ix e m s
. (MonadThrow m, Index (Lower ix), Index ix, Manifest r e)
=> MArray s r ix e
-> Ix1
-> m (MArray s r (Lower ix) e)
outerSliceMArrayM :: forall r ix e (m :: * -> *) s.
(MonadThrow m, Index (Lower ix), Index ix, Manifest r e) =>
MArray s r ix e -> Ix1 -> m (MArray s r (Lower ix) e)
outerSliceMArrayM !MArray s r ix e
marr !Ix1
i = do
let (Sz1
k, Sz (Lower ix)
szL) = forall ix. Index ix => Sz ix -> (Sz1, Sz (Lower ix))
unconsSz (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r ix e
marr)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz1
k Ix1
i) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException Sz1
k Ix1
i
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 (Lower ix)
szL forall a b. (a -> b) -> a -> b
$ forall r e ix s.
(Manifest r e, Index ix) =>
Ix1 -> Sz1 -> MArray s r ix e -> MVector s r e
unsafeLinearSliceMArray (Ix1
i forall a. Num a => a -> a -> a
* forall ix. Index ix => Sz ix -> Ix1
totalElem Sz (Lower ix)
szL) (forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz (Lower ix)
szL) MArray s r ix e
marr
{-# INLINE outerSliceMArrayM #-}
outerSlicesMArray
:: forall r ix e s
. (Index (Lower ix), Index ix, Manifest r e)
=> Comp
-> MArray s r ix e
-> Vector D (MArray s r (Lower ix) e)
outerSlicesMArray :: forall r ix e s.
(Index (Lower ix), Index ix, Manifest r e) =>
Comp -> MArray s r ix e -> Vector D (MArray s r (Lower ix) e)
outerSlicesMArray Comp
comp MArray s r ix e
marr =
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz1
k (\Ix1
i -> 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 (Lower ix)
szL forall a b. (a -> b) -> a -> b
$ forall r e ix s.
(Manifest r e, Index ix) =>
Ix1 -> Sz1 -> MArray s r ix e -> MVector s r e
unsafeLinearSliceMArray (Ix1
i forall a. Num a => a -> a -> a
* forall ix. Sz ix -> ix
unSz Sz1
kL) Sz1
kL MArray s r ix e
marr)
where
kL :: Sz1
kL = forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz (Lower ix)
szL
(Sz1
k, Sz (Lower ix)
szL) = forall ix. Index ix => Sz ix -> (Sz1, Sz (Lower ix))
unconsSz forall a b. (a -> b) -> a -> b
$ forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r ix e
marr
{-# INLINE outerSlicesMArray #-}
newMArray'
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> m (MArray (PrimState m) r ix e)
newMArray' :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
newMArray' 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
{-# INLINE newMArray' #-}
thaw :: forall r ix e m. (Manifest r e, Index ix, MonadIO m) => Array r ix e -> m (MArray RealWorld r ix e)
thaw :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Array r ix e -> m (MArray RealWorld r ix e)
thaw Array r ix e
arr =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let sz :: Sz ix
sz = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr
totalLength :: Ix1
totalLength = forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz
MArray RealWorld 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
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. Ix1 -> Ix1 -> (Ix1 -> Ix1 -> a) -> a
splitLinearly (forall s a. Scheduler s a -> Ix1
numWorkers Scheduler RealWorld ()
scheduler) Ix1
totalLength forall a b. (a -> b) -> a -> b
$ \Ix1
chunkLength Ix1
slackStart -> do
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< Ix1
slackStart) (forall a. Num a => a -> a -> a
+ Ix1
chunkLength) forall a b. (a -> b) -> a -> b
$ \ !Ix1
start ->
forall s (m :: * -> *).
MonadPrimBase s m =>
Scheduler s () -> m () -> m ()
scheduleWork_ Scheduler RealWorld ()
scheduler forall a b. (a -> b) -> a -> b
$ forall r e ix' ix (m :: * -> *).
(Manifest r e, 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
arr Ix1
start MArray RealWorld r ix e
marr Ix1
start (forall ix. ix -> Sz ix
SafeSz Ix1
chunkLength)
let slackLength :: Ix1
slackLength = Ix1
totalLength forall a. Num a => a -> a -> a
- Ix1
slackStart
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Ix1
slackLength forall a. Ord a => a -> a -> Bool
> Ix1
0) forall a b. (a -> b) -> a -> b
$
forall s (m :: * -> *).
MonadPrimBase s m =>
Scheduler s () -> m () -> m ()
scheduleWork_ Scheduler RealWorld ()
scheduler forall a b. (a -> b) -> a -> b
$
forall r e ix' ix (m :: * -> *).
(Manifest r e, 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
arr Ix1
slackStart MArray RealWorld r ix e
marr Ix1
slackStart (forall ix. ix -> Sz ix
SafeSz Ix1
slackLength)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray RealWorld r ix e
marr
{-# INLINE thaw #-}
thawS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Array r ix e
-> m (MArray (PrimState m) r ix e)
thawS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e -> m (MArray (PrimState m) r ix e)
thawS Array r ix e
arr = do
MArray (PrimState m) r ix e
tmarr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)
forall r e ix' ix (m :: * -> *).
(Manifest r e, 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
arr Ix1
0 MArray (PrimState m) r ix e
tmarr Ix1
0 (forall ix. ix -> Sz ix
SafeSz (forall ix. Index ix => Sz ix -> Ix1
totalElem (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray (PrimState m) r ix e
tmarr
{-# INLINE thawS #-}
freeze
:: forall r ix e m
. (Manifest r e, Index ix, MonadIO m)
=> Comp
-> MArray RealWorld r ix e
-> m (Array r ix e)
freeze :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Comp -> MArray RealWorld r ix e -> m (Array r ix e)
freeze Comp
comp MArray RealWorld r ix e
smarr =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let sz :: Sz ix
sz = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray RealWorld r ix e
smarr
totalLength :: Ix1
totalLength = forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz
MArray RealWorld r ix e
tmarr <- 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
Comp -> (Scheduler RealWorld () -> IO ()) -> IO ()
withMassivScheduler_ Comp
comp forall a b. (a -> b) -> a -> b
$ \Scheduler RealWorld ()
scheduler ->
forall a. Ix1 -> Ix1 -> (Ix1 -> Ix1 -> a) -> a
splitLinearly (forall s a. Scheduler s a -> Ix1
numWorkers Scheduler RealWorld ()
scheduler) Ix1
totalLength forall a b. (a -> b) -> a -> b
$ \Ix1
chunkLength Ix1
slackStart -> do
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< Ix1
slackStart) (forall a. Num a => a -> a -> a
+ Ix1
chunkLength) forall a b. (a -> b) -> a -> b
$ \ !Ix1
start ->
forall s (m :: * -> *).
MonadPrimBase s m =>
Scheduler s () -> m () -> m ()
scheduleWork_ Scheduler RealWorld ()
scheduler forall a b. (a -> b) -> a -> b
$ forall r e ix' ix (m :: * -> *).
(Manifest r e, Index ix', Index ix, PrimMonad m) =>
MArray (PrimState m) r ix' e
-> Ix1 -> MArray (PrimState m) r ix e -> Ix1 -> Sz1 -> m ()
unsafeLinearCopy MArray RealWorld r ix e
smarr Ix1
start MArray RealWorld r ix e
tmarr Ix1
start (forall ix. ix -> Sz ix
SafeSz Ix1
chunkLength)
let slackLength :: Ix1
slackLength = Ix1
totalLength forall a. Num a => a -> a -> a
- Ix1
slackStart
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Ix1
slackLength forall a. Ord a => a -> a -> Bool
> Ix1
0) forall a b. (a -> b) -> a -> b
$
forall s (m :: * -> *).
MonadPrimBase s m =>
Scheduler s () -> m () -> m ()
scheduleWork_ Scheduler RealWorld ()
scheduler forall a b. (a -> b) -> a -> b
$
forall r e ix' ix (m :: * -> *).
(Manifest r e, Index ix', Index ix, PrimMonad m) =>
MArray (PrimState m) r ix' e
-> Ix1 -> MArray (PrimState m) r ix e -> Ix1 -> Sz1 -> m ()
unsafeLinearCopy MArray RealWorld r ix e
smarr Ix1
slackStart MArray RealWorld r ix e
tmarr Ix1
slackStart (forall ix. ix -> Sz ix
SafeSz Ix1
slackLength)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp MArray RealWorld r ix e
tmarr
{-# INLINE freeze #-}
freezeS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> m (Array r ix e)
freezeS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> m (Array r ix e)
freezeS MArray (PrimState m) r ix e
smarr = do
let 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
smarr
MArray (PrimState m) r ix e
tmarr <- 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
-> Ix1 -> MArray (PrimState m) r ix e -> Ix1 -> Sz1 -> m ()
unsafeLinearCopy MArray (PrimState m) r ix e
smarr Ix1
0 MArray (PrimState m) r ix e
tmarr Ix1
0 (forall ix. ix -> Sz ix
SafeSz (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz))
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray (PrimState m) r ix e
tmarr
{-# INLINE freezeS #-}
unsafeNewUpper
:: (Load r' ix e, Manifest r e, PrimMonad m) => Array r' ix e -> m (MArray (PrimState m) r Ix1 e)
unsafeNewUpper :: forall r' ix e r (m :: * -> *).
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e -> m (MArray (PrimState m) r Ix1 e)
unsafeNewUpper !Array r' ix e
arr = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (forall a. a -> Maybe a -> a
fromMaybe forall ix. Index ix => Sz ix
zeroSz (forall r ix e. Shape r ix => Array r ix e -> Maybe Sz1
maxLinearSize Array r' ix e
arr))
{-# INLINE unsafeNewUpper #-}
loadArrayS
:: forall r ix e r' m
. (Load r' ix e, Manifest r e, PrimMonad m)
=> Array r' ix e
-> m (MArray (PrimState m) r ix e)
loadArrayS :: forall r ix e r' (m :: * -> *).
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e -> m (MArray (PrimState m) r ix e)
loadArrayS Array r' ix e
arr = do
MVector (PrimState m) r e
marr <- forall r' ix e r (m :: * -> *).
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e -> m (MArray (PrimState m) r Ix1 e)
unsafeNewUpper Array r' ix e
arr
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall r ix e r' s.
(Load r ix e, Manifest r' e) =>
MVector s r' e -> Array r ix e -> ST s (MArray s r' ix e)
unsafeLoadIntoST MVector (PrimState m) r e
marr Array r' ix e
arr
{-# INLINE loadArrayS #-}
loadArray
:: forall r ix e r' m
. (Load r' ix e, Manifest r e, MonadIO m)
=> Array r' ix e
-> m (MArray RealWorld r ix e)
loadArray :: forall r ix e r' (m :: * -> *).
(Load r' ix e, Manifest r e, MonadIO m) =>
Array r' ix e -> m (MArray RealWorld r ix e)
loadArray Array r' ix e
arr =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
MVector RealWorld r e
marr <- forall r' ix e r (m :: * -> *).
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e -> m (MArray (PrimState m) r Ix1 e)
unsafeNewUpper Array r' ix e
arr
forall r ix e r'.
(Load r ix e, Manifest r' e) =>
MVector RealWorld r' e
-> Array r ix e -> IO (MArray RealWorld r' ix e)
unsafeLoadIntoIO MVector RealWorld r e
marr Array r' ix e
arr
{-# INLINE loadArray #-}
computeInto
:: (Load r' ix' e, Manifest r e, Index ix, MonadIO m)
=> MArray RealWorld r ix e
-> Array r' ix' e
-> m ()
computeInto :: forall r' ix' e r ix (m :: * -> *).
(Load r' ix' e, Manifest r e, Index ix, MonadIO m) =>
MArray RealWorld r ix e -> Array r' ix' e -> m ()
computeInto !MArray RealWorld r ix e
mArr !Array r' ix' e
arr =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ 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
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall ix. Index ix => Sz ix -> Ix1
totalElem (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray RealWorld r ix e
mArr) forall a. Eq a => a -> a -> Bool
== forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix'
sz) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$
forall ix ix'.
(Index ix, Index ix') =>
Sz ix -> Sz ix' -> SizeException
SizeElementsMismatchException (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray RealWorld r ix e
mArr) 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 (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall r ix e s.
Load r ix e =>
Scheduler s () -> Array r ix e -> (Ix1 -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ 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 -> Ix1 -> e -> m ()
unsafeLinearWrite MArray RealWorld r ix e
mArr)
{-# INLINE computeInto #-}
makeMArrayS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (ix -> m e)
-> m (MArray (PrimState m) r ix e)
makeMArrayS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (ix -> m e) -> m (MArray (PrimState m) r ix e)
makeMArrayS Sz ix
sz ix -> m e
f = forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (Ix1 -> m e) -> m (MArray (PrimState m) r ix e)
makeMArrayLinearS Sz ix
sz (ix -> m e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE makeMArrayS #-}
makeMArrayLinearS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (Int -> m e)
-> m (MArray (PrimState m) r ix e)
makeMArrayLinearS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (Ix1 -> m e) -> m (MArray (PrimState m) r ix e)
makeMArrayLinearS Sz ix
sz Ix1 -> m e
f = 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 (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) (\ !Ix1
i -> Ix1 -> m e
f Ix1
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 -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i)
forall (m :: * -> *) a. Monad m => a -> m a
return MArray (PrimState m) r ix e
marr
{-# INLINE makeMArrayLinearS #-}
makeMArray
:: forall r ix e m
. (MonadUnliftIO m, Manifest r e, Index ix)
=> Comp
-> Sz ix
-> (ix -> m e)
-> m (MArray RealWorld r ix e)
makeMArray :: forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (ix -> m e) -> m (MArray RealWorld r ix e)
makeMArray Comp
comp Sz ix
sz ix -> m e
f = forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (MArray RealWorld r ix e)
makeMArrayLinear Comp
comp Sz ix
sz (ix -> m e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE makeMArray #-}
makeMArrayLinear
:: forall r ix e m
. (MonadUnliftIO m, Manifest r e, Index ix)
=> Comp
-> Sz ix
-> (Int -> m e)
-> m (MArray RealWorld r ix e)
makeMArrayLinear :: forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (MArray RealWorld r ix e)
makeMArrayLinear Comp
comp Sz ix
sz Ix1 -> m e
f = do
MArray RealWorld r ix e
marr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ 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.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m ()
withScheduler_ Comp
comp forall a b. (a -> b) -> a -> b
$ \Scheduler RealWorld ()
scheduler ->
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run ->
forall s (m :: * -> *) b c.
MonadPrimBase s m =>
Scheduler s () -> Ix1 -> (Ix1 -> m b) -> (Ix1 -> b -> m c) -> m ()
splitLinearlyWithM_ Scheduler RealWorld ()
scheduler (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz) (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix1 -> m e
f) (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray RealWorld r ix e
marr)
forall (m :: * -> *) a. Monad m => a -> m a
return MArray RealWorld r ix e
marr
{-# INLINE makeMArrayLinear #-}
createArray_
:: forall r ix e a m
. (Manifest r e, Index ix, MonadUnliftIO m)
=> Comp
-> Sz ix
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a)
-> m (Array r ix e)
createArray_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, MonadUnliftIO m) =>
Comp
-> Sz ix
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a)
-> m (Array r ix e)
createArray_ Comp
comp Sz ix
sz Scheduler RealWorld () -> MArray RealWorld r ix e -> m a
action = do
MArray RealWorld r ix e
marr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
newMArray' Sz ix
sz
forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m ()
withScheduler_ Comp
comp (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a
`action` MArray RealWorld r ix e
marr)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp MArray RealWorld r ix e
marr
{-# INLINE createArray_ #-}
createArray
:: forall r ix e a m b
. (Manifest r e, Index ix, MonadUnliftIO m)
=> Comp
-> Sz ix
-> (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b)
-> m ([a], Array r ix e)
createArray :: forall r ix e a (m :: * -> *) b.
(Manifest r e, Index ix, MonadUnliftIO m) =>
Comp
-> Sz ix
-> (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b)
-> m ([a], Array r ix e)
createArray Comp
comp Sz ix
sz Scheduler RealWorld a -> MArray RealWorld r ix e -> m b
action = do
MArray RealWorld r ix e
marr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
newMArray' Sz ix
sz
[a]
a <- forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m [a]
withScheduler Comp
comp (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b
`action` MArray RealWorld r ix e
marr)
Array r ix e
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp MArray RealWorld r ix e
marr
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
a, Array r ix e
arr)
{-# INLINE createArray #-}
createArrayS_
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (MArray (PrimState m) r ix e -> m a)
-> m (Array r ix e)
createArrayS_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
createArrayS_ Sz ix
sz MArray (PrimState m) r ix e -> m a
action = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
createArrayS Sz ix
sz MArray (PrimState m) r ix e -> m a
action
{-# INLINE createArrayS_ #-}
createArrayS
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (MArray (PrimState m) r ix e -> m a)
-> m (a, Array r ix e)
createArrayS :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
createArrayS Sz ix
sz MArray (PrimState m) r ix e -> m a
action = do
MArray (PrimState m) r ix e
marr <- forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
newMArray' Sz ix
sz
a
a <- MArray (PrimState m) r ix e -> m a
action MArray (PrimState m) r ix e
marr
Array r ix e
arr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray (PrimState m) r ix e
marr
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, Array r ix e
arr)
{-# INLINE createArrayS #-}
createArrayST_
:: forall r ix e a
. (Manifest r e, Index ix)
=> Sz ix
-> (forall s. MArray s r ix e -> ST s a)
-> Array r ix e
createArrayST_ :: forall r ix e a.
(Manifest r e, Index ix) =>
Sz ix -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
createArrayST_ Sz ix
sz forall s. MArray s r ix e -> ST s a
action = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
createArrayS_ Sz ix
sz forall s. MArray s r ix e -> ST s a
action
{-# INLINE createArrayST_ #-}
createArrayST
:: forall r ix e a
. (Manifest r e, Index ix)
=> Sz ix
-> (forall s. MArray s r ix e -> ST s a)
-> (a, Array r ix e)
createArrayST :: forall r ix e a.
(Manifest r e, Index ix) =>
Sz ix -> (forall s. MArray s r ix e -> ST s a) -> (a, Array r ix e)
createArrayST Sz ix
sz forall s. MArray s r ix e -> ST s a
action = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
createArrayS Sz ix
sz forall s. MArray s r ix e -> ST s a
action
{-# INLINE createArrayST #-}
generateArrayS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (ix -> m e)
-> m (Array r ix e)
generateArrayS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (ix -> m e) -> m (Array r ix e)
generateArrayS Sz ix
sz ix -> m e
gen = forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinearS Sz ix
sz (ix -> m e
gen forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE generateArrayS #-}
generateArrayLinearS
:: forall r ix e m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (Int -> m e)
-> m (Array r ix e)
generateArrayLinearS :: forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinearS Sz ix
sz Ix1 -> m e
gen = 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 (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) forall a b. (a -> b) -> a -> b
$ \Ix1
i -> Ix1 -> m e
gen Ix1
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 -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray (PrimState m) r ix e
marr
{-# INLINE generateArrayLinearS #-}
generateArray
:: forall r ix e m
. (MonadUnliftIO m, Manifest r e, Index ix)
=> Comp
-> Sz ix
-> (ix -> m e)
-> m (Array r ix e)
generateArray :: forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (ix -> m e) -> m (Array r ix e)
generateArray Comp
comp Sz ix
sz ix -> m e
f = forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinear Comp
comp Sz ix
sz (ix -> m e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE generateArray #-}
generateArrayLinear
:: forall r ix e m
. (MonadUnliftIO m, Manifest r e, Index ix)
=> Comp
-> Sz ix
-> (Ix1 -> m e)
-> m (Array r ix e)
generateArrayLinear :: forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinear Comp
comp Sz ix
sz Ix1 -> m e
f = forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (MArray RealWorld r ix e)
makeMArrayLinear Comp
comp Sz ix
sz Ix1 -> m e
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp
{-# INLINE generateArrayLinear #-}
generateSplitSeedArray
:: forall r ix e g it
. (Iterator it, Manifest r e, Index ix)
=> it
-> g
-> (forall s. g -> ST s (g, g))
-> Comp
-> Sz ix
-> (forall s. Ix1 -> ix -> g -> ST s (e, g))
-> (g, [g], Array r ix e)
generateSplitSeedArray :: forall r ix e g it.
(Iterator it, Manifest r e, Index ix) =>
it
-> g
-> (forall s. g -> ST s (g, g))
-> Comp
-> Sz ix
-> (forall s. Ix1 -> ix -> g -> ST s (e, g))
-> (g, [g], Array r ix e)
generateSplitSeedArray it
it g
seed forall s. g -> ST s (g, g)
splitSeed Comp
comp Sz ix
sz forall s. Ix1 -> ix -> g -> ST s (e, g)
genFunc =
forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
MArray RealWorld 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
IORef (Maybe g)
ref <- forall a. a -> IO (IORef a)
newIORef forall a. Maybe a
Nothing
Results g
res <- forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m (Results a)
withSchedulerR Comp
comp forall a b. (a -> b) -> a -> b
$ \Scheduler RealWorld g
scheduler -> do
g
fin <- forall a. ST RealWorld a -> IO a
stToIO forall a b. (a -> b) -> a -> b
$
forall it ix s a.
(Iterator it, Index ix) =>
it
-> Scheduler s a
-> Ix1
-> Sz ix
-> a
-> (a -> ST s (a, a))
-> (Ix1 -> ix -> a -> ST s a)
-> ST s a
iterTargetFullAccST it
it Scheduler RealWorld g
scheduler Ix1
0 Sz ix
sz g
seed forall s. g -> ST s (g, g)
splitSeed forall a b. (a -> b) -> a -> b
$ \ !Ix1
i ix
ix !g
g ->
forall s. Ix1 -> ix -> g -> ST s (e, g)
genFunc Ix1
i ix
ix g
g forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(e
x, g
g') -> g
g' 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 -> Ix1 -> e -> m ()
unsafeLinearWrite MArray RealWorld r ix e
marr Ix1
i e
x
forall a. IORef a -> a -> IO ()
writeIORef IORef (Maybe g)
ref forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just g
fin
Maybe g
mFin <- forall a. IORef a -> IO a
readIORef IORef (Maybe g)
ref
case Results g
res of
Finished [g]
gs
| Just g
fin <- Maybe g
mFin -> do
Array r ix e
arr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp MArray RealWorld r ix e
marr
forall (f :: * -> *) a. Applicative f => a -> f a
pure (g
fin, [g]
gs, Array r ix e
arr)
Results g
_ ->
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Parallelized array filling finished prematurely. "
forall a. [a] -> [a] -> [a]
++ [Char]
"This feature is not supported by the 'generateSplitSeedArray' function."
{-# INLINE generateSplitSeedArray #-}
generateArrayLinearWS
:: forall r ix e s m
. (Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m)
=> WorkerStates s
-> Sz ix
-> (Int -> s -> m e)
-> m (Array r ix e)
generateArrayLinearWS :: forall r ix e s (m :: * -> *).
(Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m) =>
WorkerStates s -> Sz ix -> (Ix1 -> s -> m e) -> m (Array r ix e)
generateArrayLinearWS WorkerStates s
states Sz ix
sz Ix1 -> s -> m e
make = 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 (m :: * -> *) ws b.
MonadUnliftIO m =>
WorkerStates ws -> (SchedulerWS ws () -> m b) -> m ()
withSchedulerWS_ WorkerStates s
states forall a b. (a -> b) -> a -> b
$ \SchedulerWS s ()
schedulerWS ->
forall (m :: * -> *) ws b c.
MonadUnliftIO m =>
SchedulerWS ws ()
-> Ix1 -> (Ix1 -> ws -> m b) -> (Ix1 -> b -> m c) -> m ()
splitLinearlyWithStatefulM_
SchedulerWS s ()
schedulerWS
(forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz)
Ix1 -> s -> m e
make
(forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall ws. WorkerStates ws -> Comp
workerStatesComp WorkerStates s
states) MArray (PrimState m) r ix e
marr
{-# INLINE generateArrayLinearWS #-}
generateArrayWS
:: forall r ix e s m
. (Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m)
=> WorkerStates s
-> Sz ix
-> (ix -> s -> m e)
-> m (Array r ix e)
generateArrayWS :: forall r ix e s (m :: * -> *).
(Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m) =>
WorkerStates s -> Sz ix -> (ix -> s -> m e) -> m (Array r ix e)
generateArrayWS WorkerStates s
states Sz ix
sz ix -> s -> m e
make = forall r ix e s (m :: * -> *).
(Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m) =>
WorkerStates s -> Sz ix -> (Ix1 -> s -> m e) -> m (Array r ix e)
generateArrayLinearWS WorkerStates s
states Sz ix
sz (ix -> s -> m e
make forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex Sz ix
sz)
{-# INLINE generateArrayWS #-}
unfoldrPrimM_
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> m (e, a))
-> a
-> m (Array r ix e)
unfoldrPrimM_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (e, a)) -> a -> m (Array r ix e)
unfoldrPrimM_ Sz ix
sz a -> m (e, a)
gen a
acc0 = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (e, a)) -> a -> m (a, Array r ix e)
unfoldrPrimM Sz ix
sz a -> m (e, a)
gen a
acc0
{-# INLINE unfoldrPrimM_ #-}
iunfoldrPrimM_
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> ix -> m (e, a))
-> a
-> m (Array r ix e)
iunfoldrPrimM_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (e, a)) -> a -> m (Array r ix e)
iunfoldrPrimM_ Sz ix
sz a -> ix -> m (e, a)
gen a
acc0 = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (e, a)) -> a -> m (a, Array r ix e)
iunfoldrPrimM Sz ix
sz a -> ix -> m (e, a)
gen a
acc0
{-# INLINE iunfoldrPrimM_ #-}
iunfoldrPrimM
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> ix -> m (e, a))
-> a
-> m (a, Array r ix e)
iunfoldrPrimM :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (e, a)) -> a -> m (a, Array r ix e)
iunfoldrPrimM Sz ix
sz a -> ix -> m (e, a)
gen a
acc0 =
forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
unsafeCreateArrayS Sz ix
sz forall a b. (a -> b) -> a -> b
$ \MArray (PrimState m) r ix e
marr ->
let 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
in forall ix (m :: * -> *) a.
(Index ix, Monad m) =>
Sz ix
-> Ix1
-> Ix1
-> Ix1
-> (Ix1 -> Ix1 -> Bool)
-> a
-> (Ix1 -> ix -> a -> m a)
-> m a
iterLinearM Sz ix
sz' Ix1
0 (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz') Ix1
1 forall a. Ord a => a -> a -> Bool
(<) a
acc0 forall a b. (a -> b) -> a -> b
$ \ !Ix1
i ix
ix !a
acc -> do
(e
e, a
acc') <- a -> ix -> m (e, a)
gen a
acc ix
ix
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i e
e
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
acc'
{-# INLINE iunfoldrPrimM #-}
unfoldrPrimM
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> m (e, a))
-> a
-> m (a, Array r ix e)
unfoldrPrimM :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (e, a)) -> a -> m (a, Array r ix e)
unfoldrPrimM Sz ix
sz a -> m (e, a)
gen a
acc0 =
forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
unsafeCreateArrayS Sz ix
sz forall a b. (a -> b) -> a -> b
$ \MArray (PrimState m) r ix e
marr ->
let 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
in forall (m :: * -> *) a.
Monad m =>
Ix1
-> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> a -> (Ix1 -> a -> m a) -> m a
loopM Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz') (forall a. Num a => a -> a -> a
+ Ix1
1) a
acc0 forall a b. (a -> b) -> a -> b
$ \ !Ix1
i !a
acc -> do
(e
e, a
acc') <- a -> m (e, a)
gen a
acc
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i e
e
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
acc'
{-# INLINE unfoldrPrimM #-}
unfoldlPrimM_
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> m (a, e))
-> a
-> m (Array r ix e)
unfoldlPrimM_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (a, e)) -> a -> m (Array r ix e)
unfoldlPrimM_ Sz ix
sz a -> m (a, e)
gen a
acc0 = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (a, e)) -> a -> m (a, Array r ix e)
unfoldlPrimM Sz ix
sz a -> m (a, e)
gen a
acc0
{-# INLINE unfoldlPrimM_ #-}
iunfoldlPrimM_
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> ix -> m (a, e))
-> a
-> m (Array r ix e)
iunfoldlPrimM_ :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (a, e)) -> a -> m (Array r ix e)
iunfoldlPrimM_ Sz ix
sz a -> ix -> m (a, e)
gen a
acc0 = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (a, e)) -> a -> m (a, Array r ix e)
iunfoldlPrimM Sz ix
sz a -> ix -> m (a, e)
gen a
acc0
{-# INLINE iunfoldlPrimM_ #-}
iunfoldlPrimM
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> ix -> m (a, e))
-> a
-> m (a, Array r ix e)
iunfoldlPrimM :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> ix -> m (a, e)) -> a -> m (a, Array r ix e)
iunfoldlPrimM Sz ix
sz a -> ix -> m (a, e)
gen a
acc0 =
forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
unsafeCreateArrayS Sz ix
sz forall a b. (a -> b) -> a -> b
$ \MArray (PrimState m) r ix e
marr ->
let 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
in forall ix (m :: * -> *) a.
(Index ix, Monad m) =>
Sz ix
-> Ix1
-> Ix1
-> Ix1
-> (Ix1 -> Ix1 -> Bool)
-> a
-> (Ix1 -> ix -> a -> m a)
-> m a
iterLinearM Sz ix
sz' (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz' forall a. Num a => a -> a -> a
- Ix1
1) Ix1
0 (forall a. Num a => a -> a
negate Ix1
1) forall a. Ord a => a -> a -> Bool
(>=) a
acc0 forall a b. (a -> b) -> a -> b
$ \ !Ix1
i ix
ix !a
acc -> do
(a
acc', e
e) <- a -> ix -> m (a, e)
gen a
acc ix
ix
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i e
e
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
acc'
{-# INLINE iunfoldlPrimM #-}
unfoldlPrimM
:: forall r ix e a m
. (Manifest r e, Index ix, PrimMonad m)
=> Sz ix
-> (a -> m (a, e))
-> a
-> m (a, Array r ix e)
unfoldlPrimM :: forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> (a -> m (a, e)) -> a -> m (a, Array r ix e)
unfoldlPrimM Sz ix
sz a -> m (a, e)
gen a
acc0 =
forall r ix e a (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
unsafeCreateArrayS Sz ix
sz forall a b. (a -> b) -> a -> b
$ \MArray (PrimState m) r ix e
marr ->
let 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
in forall (m :: * -> *) a.
Monad m =>
Ix1
-> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> a -> (Ix1 -> a -> m a) -> m a
loopDeepM Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz') (forall a. Num a => a -> a -> a
+ Ix1
1) a
acc0 forall a b. (a -> b) -> a -> b
$ \ !Ix1
i !a
acc -> do
(a
acc', e
e) <- a -> m (a, e)
gen a
acc
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix e
marr Ix1
i e
e
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
acc'
{-# INLINE unfoldlPrimM #-}
forPrimM_ :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> m ()) -> m ()
forPrimM_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m ()) -> m ()
forPrimM_ MArray (PrimState m) r ix e
marr e -> m ()
f =
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> e -> m ()
f)
{-# INLINE forPrimM_ #-}
forPrimM :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> m e) -> m ()
forPrimM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> m ()
forPrimM MArray (PrimState m) r ix e
marr e -> m e
f =
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> Ix1 -> m e
unsafeLinearModify MArray (PrimState m) r ix e
marr e -> m e
f)
{-# INLINE forPrimM #-}
iforPrimM_
:: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (ix -> e -> m ()) -> m ()
iforPrimM_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (ix -> e -> m ()) -> m ()
iforPrimM_ MArray (PrimState m) r ix e
marr ix -> e -> m ()
f = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (Ix1 -> e -> m ()) -> m ()
iforLinearPrimM_ MArray (PrimState m) r ix e
marr (ix -> e -> m ()
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex (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 iforPrimM_ #-}
iforPrimM
:: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (ix -> e -> m e) -> m ()
iforPrimM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (ix -> e -> m e) -> m ()
iforPrimM MArray (PrimState m) r ix e
marr ix -> e -> m e
f = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (Ix1 -> e -> m e) -> m ()
iforLinearPrimM MArray (PrimState m) r ix e
marr (ix -> e -> m e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Ix1 -> ix
fromLinearIndex (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 iforPrimM #-}
iforLinearPrimM_
:: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (Int -> e -> m ()) -> m ()
iforLinearPrimM_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (Ix1 -> e -> m ()) -> m ()
iforLinearPrimM_ MArray (PrimState m) r ix e
marr Ix1 -> e -> m ()
f =
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) (\Ix1
i -> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead MArray (PrimState m) r ix e
marr Ix1
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ix1 -> e -> m ()
f Ix1
i)
{-# INLINE iforLinearPrimM_ #-}
iforLinearPrimM
:: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> (Int -> e -> m e) -> m ()
iforLinearPrimM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (Ix1 -> e -> m e) -> m ()
iforLinearPrimM MArray (PrimState m) r ix e
marr Ix1 -> e -> m e
f =
forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< forall ix. Index ix => Sz ix -> Ix1
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 a. Num a => a -> a -> a
+ Ix1
1) (\Ix1
i -> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> Ix1 -> m e
unsafeLinearModify MArray (PrimState m) r ix e
marr (Ix1 -> e -> m e
f Ix1
i) Ix1
i)
{-# INLINE iforLinearPrimM #-}
for2PrimM_
:: forall r1 r2 e1 e2 ix m
. (PrimMonad m, Index ix, Manifest r1 e1, Manifest r2 e2)
=> MArray (PrimState m) r1 ix e1
-> MArray (PrimState m) r2 ix e2
-> (e1 -> e2 -> m ())
-> m ()
for2PrimM_ :: forall r1 r2 e1 e2 ix (m :: * -> *).
(PrimMonad m, Index ix, Manifest r1 e1, Manifest r2 e2) =>
MArray (PrimState m) r1 ix e1
-> MArray (PrimState m) r2 ix e2 -> (e1 -> e2 -> m ()) -> m ()
for2PrimM_ MArray (PrimState m) r1 ix e1
m1 MArray (PrimState m) r2 ix e2
m2 e1 -> e2 -> m ()
f = forall r1 r2 e1 e2 ix (m :: * -> *).
(PrimMonad m, Index ix, Manifest r1 e1, Manifest r2 e2) =>
MArray (PrimState m) r1 ix e1
-> MArray (PrimState m) r2 ix e2
-> (ix -> e1 -> e2 -> m ())
-> m ()
ifor2PrimM_ MArray (PrimState m) r1 ix e1
m1 MArray (PrimState m) r2 ix e2
m2 (forall a b. a -> b -> a
const e1 -> e2 -> m ()
f)
{-# INLINE for2PrimM_ #-}
ifor2PrimM_
:: forall r1 r2 e1 e2 ix m
. (PrimMonad m, Index ix, Manifest r1 e1, Manifest r2 e2)
=> MArray (PrimState m) r1 ix e1
-> MArray (PrimState m) r2 ix e2
-> (ix -> e1 -> e2 -> m ())
-> m ()
ifor2PrimM_ :: forall r1 r2 e1 e2 ix (m :: * -> *).
(PrimMonad m, Index ix, Manifest r1 e1, Manifest r2 e2) =>
MArray (PrimState m) r1 ix e1
-> MArray (PrimState m) r2 ix e2
-> (ix -> e1 -> e2 -> m ())
-> m ()
ifor2PrimM_ MArray (PrimState m) r1 ix e1
m1 MArray (PrimState m) r2 ix e2
m2 ix -> e1 -> e2 -> m ()
f = do
let sz :: ix
sz = forall ix. Index ix => (Ix1 -> Ix1 -> Ix1) -> ix -> ix -> ix
liftIndex2 forall a. Ord a => a -> a -> a
min (forall ix. Sz ix -> ix
unSz (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r1 ix e1
m1)) (forall ix. Sz ix -> ix
unSz (forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r2 ix e2
m2))
forall ix (f :: * -> *) a.
(Index ix, Applicative f) =>
ix -> ix -> ix -> (Ix1 -> Ix1 -> Bool) -> (ix -> f a) -> f ()
iterA_ forall ix. Index ix => ix
zeroIndex ix
sz forall ix. Index ix => ix
oneIndex forall a. Ord a => a -> a -> Bool
(<) forall a b. (a -> b) -> a -> b
$ \ix
ix -> do
e1
e1 <- 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) r1 ix e1
m1 ix
ix
e2
e2 <- 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) r2 ix e2
m2 ix
ix
ix -> e1 -> e2 -> m ()
f ix
ix e1
e1 e2
e2
{-# INLINE ifor2PrimM_ #-}
withMArray
:: (Manifest r e, Index ix, MonadUnliftIO m)
=> Array r ix e
-> (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b)
-> m ([a], Array r ix e)
withMArray :: forall r e ix (m :: * -> *) a b.
(Manifest r e, Index ix, MonadUnliftIO m) =>
Array r ix e
-> (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b)
-> m ([a], Array r ix e)
withMArray Array r ix e
arr Scheduler RealWorld a -> MArray RealWorld r ix e -> m b
action = do
MArray RealWorld r ix e
marr <- forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Array r ix e -> m (MArray RealWorld r ix e)
thaw Array r ix e
arr
[a]
xs <- forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m [a]
withScheduler (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) (Scheduler RealWorld a -> MArray RealWorld r ix e -> m b
`action` MArray RealWorld r ix e
marr)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((,) [a]
xs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) MArray RealWorld r ix e
marr)
{-# INLINE withMArray #-}
withMArray_
:: (Manifest r e, Index ix, MonadUnliftIO m)
=> Array r ix e
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a)
-> m (Array r ix e)
withMArray_ :: forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, MonadUnliftIO m) =>
Array r ix e
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a)
-> m (Array r ix e)
withMArray_ Array r ix e
arr Scheduler RealWorld () -> MArray RealWorld r ix e -> m a
action = do
MArray RealWorld r ix e
marr <- forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Array r ix e -> m (MArray RealWorld r ix e)
thaw Array r ix e
arr
forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m ()
withScheduler_ (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) (Scheduler RealWorld () -> MArray RealWorld r ix e -> m a
`action` MArray RealWorld r ix e
marr)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) MArray RealWorld r ix e
marr
{-# INLINE withMArray_ #-}
withLoadMArray_
:: forall r ix e r' m b
. (Load r' ix e, Manifest r e, MonadUnliftIO m)
=> Array r' ix e
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m b)
-> m (Array r ix e)
withLoadMArray_ :: forall r ix e r' (m :: * -> *) b.
(Load r' ix e, Manifest r e, MonadUnliftIO m) =>
Array r' ix e
-> (Scheduler RealWorld () -> MArray RealWorld r ix e -> m b)
-> m (Array r ix e)
withLoadMArray_ Array r' ix e
arr Scheduler RealWorld () -> MArray RealWorld r ix e -> m b
action = do
MArray RealWorld r ix e
marr <- forall r ix e r' (m :: * -> *).
(Load r' ix e, Manifest r e, MonadIO m) =>
Array r' ix e -> m (MArray RealWorld r ix e)
loadArray Array r' ix e
arr
forall (m :: * -> *) a b.
MonadUnliftIO m =>
Comp -> (Scheduler RealWorld a -> m b) -> m ()
withScheduler_ (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r' ix e
arr) (Scheduler RealWorld () -> MArray RealWorld r ix e -> m b
`action` MArray RealWorld r ix e
marr)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r' ix e
arr) MArray RealWorld r ix e
marr
{-# INLINE [2] withLoadMArray_ #-}
{-# RULES
"withLoadMArray_/withMArray_" [~2] withLoadMArray_ = withMArray_
"withLoadMArrayS/withMArrayS" [~2] withLoadMArrayS = withMArrayS
"withLoadMArrayS_/withMArrayS_" [~2] withLoadMArrayS_ = withMArrayS_
#-}
withMArrayS
:: (Manifest r e, Index ix, PrimMonad m)
=> Array r ix e
-> (MArray (PrimState m) r ix e -> m a)
-> m (a, Array r ix e)
withMArrayS :: forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withMArrayS Array r ix e
arr MArray (PrimState m) r ix e -> m a
action = do
MArray (PrimState m) r ix e
marr <- forall r ix e (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e -> m (MArray (PrimState m) r ix e)
thawS Array r ix e
arr
a
a <- MArray (PrimState m) r ix e -> m a
action MArray (PrimState m) r ix e
marr
(,) a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) MArray (PrimState m) r ix e
marr
{-# INLINE withMArrayS #-}
withMArrayS_
:: (Manifest r e, Index ix, PrimMonad m)
=> Array r ix e
-> (MArray (PrimState m) r ix e -> m a)
-> m (Array r ix e)
withMArrayS_ :: forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
withMArrayS_ Array r ix e
arr MArray (PrimState m) r ix e -> m a
action = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withMArrayS Array r ix e
arr MArray (PrimState m) r ix e -> m a
action
{-# INLINE withMArrayS_ #-}
withLoadMArrayS
:: forall r ix e r' m a
. (Load r' ix e, Manifest r e, PrimMonad m)
=> Array r' ix e
-> (MArray (PrimState m) r ix e -> m a)
-> m (a, Array r ix e)
withLoadMArrayS :: forall r ix e r' (m :: * -> *) a.
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withLoadMArrayS Array r' ix e
arr MArray (PrimState m) r ix e -> m a
action = do
MArray (PrimState m) r ix e
marr <- forall r ix e r' (m :: * -> *).
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e -> m (MArray (PrimState m) r ix e)
loadArrayS Array r' ix e
arr
a
a <- MArray (PrimState m) r ix e -> m a
action MArray (PrimState m) r ix e
marr
(,) a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r' ix e
arr) MArray (PrimState m) r ix e
marr
{-# INLINE [2] withLoadMArrayS #-}
withLoadMArrayS_
:: forall r ix e r' m a
. (Load r' ix e, Manifest r e, PrimMonad m)
=> Array r' ix e
-> (MArray (PrimState m) r ix e -> m a)
-> m (Array r ix e)
withLoadMArrayS_ :: forall r ix e r' (m :: * -> *) a.
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
withLoadMArrayS_ Array r' ix e
arr MArray (PrimState m) r ix e -> m a
action = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r ix e r' (m :: * -> *) a.
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withLoadMArrayS Array r' ix e
arr MArray (PrimState m) r ix e -> m a
action
{-# INLINE [2] withLoadMArrayS_ #-}
withMArrayST
:: (Manifest r e, Index ix)
=> Array r ix e
-> (forall s. MArray s r ix e -> ST s a)
-> (a, Array r ix e)
withMArrayST :: forall r e ix a.
(Manifest r e, Index ix) =>
Array r ix e
-> (forall s. MArray s r ix e -> ST s a) -> (a, Array r ix e)
withMArrayST Array r ix e
arr forall s. MArray s r ix e -> ST s a
f = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withMArrayS Array r ix e
arr forall s. MArray s r ix e -> ST s a
f
{-# INLINE withMArrayST #-}
withMArrayST_
:: (Manifest r e, Index ix) => Array r ix e -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
withMArrayST_ :: forall r e ix a.
(Manifest r e, Index ix) =>
Array r ix e
-> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
withMArrayST_ Array r ix e
arr forall s. MArray s r ix e -> ST s a
f = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *) a.
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
withMArrayS_ Array r ix e
arr forall s. MArray s r ix e -> ST s a
f
{-# INLINE withMArrayST_ #-}
withLoadMArrayST
:: forall r ix e r' a
. (Load r' ix e, Manifest r e)
=> Array r' ix e
-> (forall s. MArray s r ix e -> ST s a)
-> (a, Array r ix e)
withLoadMArrayST :: forall r ix e r' a.
(Load r' ix e, Manifest r e) =>
Array r' ix e
-> (forall s. MArray s r ix e -> ST s a) -> (a, Array r ix e)
withLoadMArrayST Array r' ix e
arr forall s. MArray s r ix e -> ST s a
f = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r ix e r' (m :: * -> *) a.
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
withLoadMArrayS Array r' ix e
arr forall s. MArray s r ix e -> ST s a
f
{-# INLINE [2] withLoadMArrayST #-}
withLoadMArrayST_
:: forall r ix e r' a
. (Load r' ix e, Manifest r e)
=> Array r' ix e
-> (forall s. MArray s r ix e -> ST s a)
-> Array r ix e
withLoadMArrayST_ :: forall r ix e r' a.
(Load r' ix e, Manifest r e) =>
Array r' ix e
-> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
withLoadMArrayST_ Array r' ix e
arr forall s. MArray s r ix e -> ST s a
f = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall r ix e r' (m :: * -> *) a.
(Load r' ix e, Manifest r e, PrimMonad m) =>
Array r' ix e
-> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
withLoadMArrayS_ Array r' ix e
arr forall s. MArray s r ix e -> ST s a
f
{-# INLINE [2] withLoadMArrayST_ #-}
read
:: (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> ix
-> m (Maybe e)
read :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> m (Maybe e)
read MArray (PrimState m) r ix e
marr ix
ix =
if forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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
then forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 ix
ix
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
{-# INLINE read #-}
readM
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m)
=> MArray (PrimState m) r ix e
-> ix
-> m e
readM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> ix -> m e
readM MArray (PrimState m) r ix e
marr ix
ix =
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> m (Maybe e)
read MArray (PrimState m) r ix e
marr ix
ix forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just e
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure e
e
Maybe e
Nothing -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (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 readM #-}
write :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m Bool
write :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> e -> m Bool
write MArray (PrimState m) r ix e
marr ix
ix e
e =
if forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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
then 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 ix
ix e
e forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
else forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
{-# INLINE write #-}
write_ :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m ()
write_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> e -> m ()
write_ MArray (PrimState m) r ix e
marr ix
ix = forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 ix
ix
{-# INLINE write_ #-}
writeM
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m) => MArray (PrimState m) r ix e -> ix -> e -> m ()
writeM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> ix -> e -> m ()
writeM MArray (PrimState m) r ix e
marr ix
ix e
e =
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> e -> m Bool
write MArray (PrimState m) r ix e
marr ix
ix e
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
`unless` forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (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 writeM #-}
modify
:: (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> ix
-> m (Maybe e)
modify :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m (Maybe e)
modify MArray (PrimState m) r ix e
marr e -> m e
f ix
ix =
if forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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
then forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
{-# INLINE modify #-}
modify_
:: (Manifest r e, Index ix, PrimMonad m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> ix
-> m ()
modify_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m ()
modify_ MArray (PrimState m) r ix e
marr e -> m e
f ix
ix = forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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) forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ 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
{-# INLINE modify_ #-}
modifyM
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> ix
-> m e
modifyM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m e
modifyM MArray (PrimState m) r ix e
marr e -> m e
f ix
ix
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = 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
| 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 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 modifyM #-}
modifyM_
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m)
=> MArray (PrimState m) r ix e
-> (e -> m e)
-> ix
-> m ()
modifyM_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m ()
modifyM_ MArray (PrimState m) r ix e
marr e -> m e
f ix
ix = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> (e -> m e) -> ix -> m e
modifyM MArray (PrimState m) r ix e
marr e -> m e
f ix
ix
{-# INLINE modifyM_ #-}
swap :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m (Maybe (e, e))
swap :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m (Maybe (e, e))
swap MArray (PrimState m) r ix e
marr ix
ix1 ix
ix2 =
let !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
in if forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix1 Bool -> Bool -> Bool
&& forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix2
then forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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
else forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
{-# INLINE swap #-}
swap_ :: (Manifest r e, Index ix, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m ()
swap_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m ()
swap_ MArray (PrimState m) r ix e
marr ix
ix1 ix
ix2 =
let !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
in forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix1 Bool -> Bool -> Bool
&& forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix2) forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ 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
{-# INLINE swap_ #-}
swapM
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m)
=> MArray (PrimState m) r ix e
-> ix
-> ix
-> m (e, e)
swapM :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m (e, e)
swapM MArray (PrimState m) r ix e
marr ix
ix1 ix
ix2
| Bool -> Bool
not (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix1) = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (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
ix1
| Bool -> Bool
not (forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex Sz ix
sz ix
ix2) = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> ix -> IndexException
IndexOutOfBoundsException (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
ix2
| Bool
otherwise = 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
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 swapM #-}
swapM_
:: (Manifest r e, Index ix, PrimMonad m, MonadThrow m)
=> MArray (PrimState m) r ix e
-> ix
-> ix
-> m ()
swapM_ :: forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m ()
swapM_ MArray (PrimState m) r ix e
marr ix
ix1 ix
ix2 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m, MonadThrow m) =>
MArray (PrimState m) r ix e -> ix -> ix -> m (e, e)
swapM MArray (PrimState m) r ix e
marr ix
ix1 ix
ix2
{-# INLINE swapM_ #-}
zipSwapM_
:: forall r1 r2 ix e m s
. (MonadPrim s m, Manifest r2 e, Manifest r1 e, Index ix)
=> ix
-> MArray s r1 ix e
-> MArray s r2 ix e
-> m ()
zipSwapM_ :: forall r1 r2 ix e (m :: * -> *) s.
(MonadPrim s m, Manifest r2 e, Manifest r1 e, Index ix) =>
ix -> MArray s r1 ix e -> MArray s r2 ix e -> m ()
zipSwapM_ ix
startIx MArray s r1 ix e
m1 MArray s r2 ix e
m2 = do
let sz1 :: Sz ix
sz1 = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r1 ix e
m1
sz2 :: Sz ix
sz2 = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray s r2 ix e
m2
sz :: ix
sz = forall ix. Index ix => (Ix1 -> Ix1 -> Ix1) -> ix -> ix -> ix
liftIndex2 forall a. Ord a => a -> a -> a
min (forall ix. Sz ix -> ix
unSz Sz ix
sz1) (forall ix. Sz ix -> ix
unSz Sz ix
sz2)
forall ix (f :: * -> *) a.
(Index ix, Applicative f) =>
ix -> ix -> ix -> (Ix1 -> Ix1 -> Bool) -> (ix -> f a) -> f ()
iterA_ ix
startIx ix
sz forall ix. Index ix => ix
oneIndex forall a. Ord a => a -> a -> Bool
(<) forall a b. (a -> b) -> a -> b
$ \ix
ix -> do
let i1 :: Ix1
i1 = forall ix. Index ix => Sz ix -> ix -> Ix1
toLinearIndex Sz ix
sz1 ix
ix
i2 :: Ix1
i2 = forall ix. Index ix => Sz ix -> ix -> Ix1
toLinearIndex Sz ix
sz2 ix
ix
e
e1 <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead MArray s r1 ix e
m1 Ix1
i1
e
e2 <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead MArray s r2 ix e
m2 Ix1
i2
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray s r2 ix e
m2 Ix1
i2 e
e1
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray s r1 ix e
m1 Ix1
i1 e
e2
{-# INLINE zipSwapM_ #-}
msize :: (Manifest r e, Index ix) => MArray s r ix e -> Sz ix
msize :: forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
msize = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray
{-# DEPRECATED msize "In favor of `sizeOfMArray`" #-}