{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
module Data.Primitive.PrimArray
(
PrimArray(..)
, MutablePrimArray(..)
, newPrimArray
, newPinnedPrimArray
, newAlignedPinnedPrimArray
, resizeMutablePrimArray
#if __GLASGOW_HASKELL__ >= 710
, shrinkMutablePrimArray
#endif
, readPrimArray
, writePrimArray
, indexPrimArray
, freezePrimArray
, thawPrimArray
, unsafeFreezePrimArray
, unsafeThawPrimArray
, copyPrimArray
, copyMutablePrimArray
#if __GLASGOW_HASKELL__ >= 708
, copyPrimArrayToPtr
, copyMutablePrimArrayToPtr
#endif
, clonePrimArray
, cloneMutablePrimArray
, setPrimArray
, sameMutablePrimArray
, getSizeofMutablePrimArray
, sizeofMutablePrimArray
, sizeofPrimArray
, primArrayContents
, mutablePrimArrayContents
#if __GLASGOW_HASKELL__ >= 802
, isPrimArrayPinned
, isMutablePrimArrayPinned
#endif
, primArrayToList
, primArrayFromList
, primArrayFromListN
, foldrPrimArray
, foldrPrimArray'
, foldlPrimArray
, foldlPrimArray'
, foldlPrimArrayM'
, traversePrimArray_
, itraversePrimArray_
, mapPrimArray
, imapPrimArray
, generatePrimArray
, replicatePrimArray
, filterPrimArray
, mapMaybePrimArray
, traversePrimArray
, itraversePrimArray
, generatePrimArrayA
, replicatePrimArrayA
, filterPrimArrayA
, mapMaybePrimArrayA
, traversePrimArrayP
, itraversePrimArrayP
, generatePrimArrayP
, replicatePrimArrayP
, filterPrimArrayP
, mapMaybePrimArrayP
) where
import GHC.Exts
import GHC.Base ( Int(..) )
import Data.Primitive.Internal.Compat (isTrue#)
import Data.Primitive.Types
import Data.Primitive.ByteArray (ByteArray(..))
import Data.Monoid (Monoid(..),(<>))
import Control.Applicative
import Control.DeepSeq
import Control.Monad.Primitive
import Control.Monad.ST
import qualified Data.List as L
import qualified Data.Primitive.ByteArray as PB
import qualified Data.Primitive.Types as PT
import qualified GHC.ST as GHCST
#if MIN_VERSION_base(4,7,0)
import GHC.Exts (IsList(..))
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup)
import qualified Data.Semigroup as SG
#endif
#if __GLASGOW_HASKELL__ >= 802
import qualified GHC.Exts as Exts
#endif
data PrimArray a = PrimArray ByteArray#
instance NFData (PrimArray a) where
rnf :: PrimArray a -> ()
rnf (PrimArray ByteArray#
_) = ()
data MutablePrimArray s a = MutablePrimArray (MutableByteArray# s)
instance Eq (MutablePrimArray s a) where
== :: MutablePrimArray s a -> MutablePrimArray s a -> Bool
(==) = MutablePrimArray s a -> MutablePrimArray s a -> Bool
forall s a. MutablePrimArray s a -> MutablePrimArray s a -> Bool
sameMutablePrimArray
instance NFData (MutablePrimArray s a) where
rnf :: MutablePrimArray s a -> ()
rnf (MutablePrimArray MutableByteArray# s
_) = ()
sameByteArray :: ByteArray# -> ByteArray# -> Bool
sameByteArray :: ByteArray# -> ByteArray# -> Bool
sameByteArray ByteArray#
ba1 ByteArray#
ba2 =
case () -> () -> Int#
forall a. a -> a -> Int#
reallyUnsafePtrEquality# (ByteArray# -> ()
unsafeCoerce# ByteArray#
ba1 :: ()) (ByteArray# -> ()
unsafeCoerce# ByteArray#
ba2 :: ()) of
#if __GLASGOW_HASKELL__ >= 708
Int#
r -> Int# -> Bool
isTrue# Int#
r
#else
1# -> True
_ -> False
#endif
instance (Eq a, Prim a) => Eq (PrimArray a) where
a1 :: PrimArray a
a1@(PrimArray ByteArray#
ba1#) == :: PrimArray a -> PrimArray a -> Bool
== a2 :: PrimArray a
a2@(PrimArray ByteArray#
ba2#)
| ByteArray# -> ByteArray# -> Bool
sameByteArray ByteArray#
ba1# ByteArray#
ba2# = Bool
True
| Int
sz1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
sz2 = Bool
False
| Bool
otherwise = Int -> Bool
loop (Int -> Int -> Int
forall a. Integral a => a -> a -> a
quot Int
sz1 (a -> Int
forall a. Prim a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
sz1 :: Int
sz1 = ByteArray -> Int
PB.sizeofByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
ba1#)
sz2 :: Int
sz2 = ByteArray -> Int
PB.sizeofByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
ba2#)
loop :: Int -> Bool
loop !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
True
| Bool
otherwise = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a1 Int
i a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a2 Int
i Bool -> Bool -> Bool
&& Int -> Bool
loop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
{-# INLINE (==) #-}
instance (Ord a, Prim a) => Ord (PrimArray a) where
compare :: PrimArray a -> PrimArray a -> Ordering
compare a1 :: PrimArray a
a1@(PrimArray ByteArray#
ba1#) a2 :: PrimArray a
a2@(PrimArray ByteArray#
ba2#)
| ByteArray# -> ByteArray# -> Bool
sameByteArray ByteArray#
ba1# ByteArray#
ba2# = Ordering
EQ
| Bool
otherwise = Int -> Ordering
loop Int
0
where
sz1 :: Int
sz1 = ByteArray -> Int
PB.sizeofByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
ba1#)
sz2 :: Int
sz2 = ByteArray -> Int
PB.sizeofByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
ba2#)
sz :: Int
sz = Int -> Int -> Int
forall a. Integral a => a -> a -> a
quot (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
sz1 Int
sz2) (a -> Int
forall a. Prim a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a))
loop :: Int -> Ordering
loop !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a1 Int
i) (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a2 Int
i) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Int -> Ordering
loop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
sz1 Int
sz2
{-# INLINE compare #-}
#if MIN_VERSION_base(4,7,0)
instance Prim a => IsList (PrimArray a) where
type Item (PrimArray a) = a
fromList :: [Item (PrimArray a)] -> PrimArray a
fromList = [Item (PrimArray a)] -> PrimArray a
forall a. Prim a => [a] -> PrimArray a
primArrayFromList
fromListN :: Int -> [Item (PrimArray a)] -> PrimArray a
fromListN = Int -> [Item (PrimArray a)] -> PrimArray a
forall a. Prim a => Int -> [a] -> PrimArray a
primArrayFromListN
toList :: PrimArray a -> [Item (PrimArray a)]
toList = PrimArray a -> [Item (PrimArray a)]
forall a. Prim a => PrimArray a -> [a]
primArrayToList
#endif
instance (Show a, Prim a) => Show (PrimArray a) where
showsPrec :: Int -> PrimArray a -> ShowS
showsPrec Int
p PrimArray a
a = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"fromListN " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
a) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => a -> ShowS
shows (PrimArray a -> [a]
forall a. Prim a => PrimArray a -> [a]
primArrayToList PrimArray a
a)
die :: String -> String -> a
die :: String -> String -> a
die String
fun String
problem = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Data.Primitive.PrimArray." String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
fun String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
problem
primArrayFromList :: Prim a => [a] -> PrimArray a
primArrayFromList :: [a] -> PrimArray a
primArrayFromList [a]
vs = Int -> [a] -> PrimArray a
forall a. Prim a => Int -> [a] -> PrimArray a
primArrayFromListN ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
L.length [a]
vs) [a]
vs
primArrayFromListN :: forall a. Prim a => Int -> [a] -> PrimArray a
primArrayFromListN :: Int -> [a] -> PrimArray a
primArrayFromListN Int
len [a]
vs = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST forall s. ST s (PrimArray a)
run where
run :: forall s. ST s (PrimArray a)
run :: ST s (PrimArray a)
run = do
MutablePrimArray s a
arr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
len
let go :: [a] -> Int -> ST s ()
go :: [a] -> Int -> ST s ()
go [] !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len
then () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"fromListN" String
"list length less than specified size"
go (a
a : [a]
as) !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then do
MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
arr Int
ix a
a
[a] -> Int -> ST s ()
go [a]
as (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"fromListN" String
"list length greater than specified size"
[a] -> Int -> ST s ()
go [a]
vs Int
0
MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
arr
{-# INLINE primArrayToList #-}
primArrayToList :: forall a. Prim a => PrimArray a -> [a]
primArrayToList :: PrimArray a -> [a]
primArrayToList PrimArray a
xs = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
c b
n -> (a -> b -> b) -> b -> PrimArray a -> b
forall a b. Prim a => (a -> b -> b) -> b -> PrimArray a -> b
foldrPrimArray a -> b -> b
c b
n PrimArray a
xs)
primArrayToByteArray :: PrimArray a -> PB.ByteArray
primArrayToByteArray :: PrimArray a -> ByteArray
primArrayToByteArray (PrimArray ByteArray#
x) = ByteArray# -> ByteArray
PB.ByteArray ByteArray#
x
byteArrayToPrimArray :: ByteArray -> PrimArray a
byteArrayToPrimArray :: ByteArray -> PrimArray a
byteArrayToPrimArray (PB.ByteArray ByteArray#
x) = ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ByteArray#
x
#if MIN_VERSION_base(4,9,0)
instance Semigroup (PrimArray a) where
PrimArray a
x <> :: PrimArray a -> PrimArray a -> PrimArray a
<> PrimArray a
y = ByteArray -> PrimArray a
forall a. ByteArray -> PrimArray a
byteArrayToPrimArray (PrimArray a -> ByteArray
forall a. PrimArray a -> ByteArray
primArrayToByteArray PrimArray a
x ByteArray -> ByteArray -> ByteArray
forall a. Semigroup a => a -> a -> a
SG.<> PrimArray a -> ByteArray
forall a. PrimArray a -> ByteArray
primArrayToByteArray PrimArray a
y)
sconcat :: NonEmpty (PrimArray a) -> PrimArray a
sconcat = ByteArray -> PrimArray a
forall a. ByteArray -> PrimArray a
byteArrayToPrimArray (ByteArray -> PrimArray a)
-> (NonEmpty (PrimArray a) -> ByteArray)
-> NonEmpty (PrimArray a)
-> PrimArray a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty ByteArray -> ByteArray
forall a. Semigroup a => NonEmpty a -> a
SG.sconcat (NonEmpty ByteArray -> ByteArray)
-> (NonEmpty (PrimArray a) -> NonEmpty ByteArray)
-> NonEmpty (PrimArray a)
-> ByteArray
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PrimArray a -> ByteArray)
-> NonEmpty (PrimArray a) -> NonEmpty ByteArray
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PrimArray a -> ByteArray
forall a. PrimArray a -> ByteArray
primArrayToByteArray
stimes :: b -> PrimArray a -> PrimArray a
stimes b
i PrimArray a
arr = ByteArray -> PrimArray a
forall a. ByteArray -> PrimArray a
byteArrayToPrimArray (b -> ByteArray -> ByteArray
forall a b. (Semigroup a, Integral b) => b -> a -> a
SG.stimes b
i (PrimArray a -> ByteArray
forall a. PrimArray a -> ByteArray
primArrayToByteArray PrimArray a
arr))
#endif
instance Monoid (PrimArray a) where
mempty :: PrimArray a
mempty = PrimArray a
forall a. PrimArray a
emptyPrimArray
#if !(MIN_VERSION_base(4,11,0))
mappend x y = byteArrayToPrimArray (mappend (primArrayToByteArray x) (primArrayToByteArray y))
#endif
mconcat :: [PrimArray a] -> PrimArray a
mconcat = ByteArray -> PrimArray a
forall a. ByteArray -> PrimArray a
byteArrayToPrimArray (ByteArray -> PrimArray a)
-> ([PrimArray a] -> ByteArray) -> [PrimArray a] -> PrimArray a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteArray] -> ByteArray
forall a. Monoid a => [a] -> a
mconcat ([ByteArray] -> ByteArray)
-> ([PrimArray a] -> [ByteArray]) -> [PrimArray a] -> ByteArray
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PrimArray a -> ByteArray) -> [PrimArray a] -> [ByteArray]
forall a b. (a -> b) -> [a] -> [b]
map PrimArray a -> ByteArray
forall a. PrimArray a -> ByteArray
primArrayToByteArray
emptyPrimArray :: PrimArray a
{-# NOINLINE emptyPrimArray #-}
emptyPrimArray :: PrimArray a
emptyPrimArray = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ (State# (PrimState (ST s))
-> (# State# (PrimState (ST s)), PrimArray a #))
-> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState (ST s))
-> (# State# (PrimState (ST s)), PrimArray a #))
-> ST s (PrimArray a))
-> (State# (PrimState (ST s))
-> (# State# (PrimState (ST s)), PrimArray a #))
-> ST s (PrimArray a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState (ST s))
s0# -> case Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
0# State# s
State# (PrimState (ST s))
s0# of
(# State# s
s1#, MutableByteArray# s
arr# #) -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
arr# State# s
s1# of
(# State# s
s2#, ByteArray#
arr'# #) -> (# State# s
State# (PrimState (ST s))
s2#, ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ByteArray#
arr'# #)
newPrimArray :: forall m a. (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)
{-# INLINE newPrimArray #-}
newPrimArray :: Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray (I# Int#
n#)
= (State# (PrimState m)
-> (# State# (PrimState m), MutablePrimArray (PrimState m) a #))
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# ->
case Int#
-> State# (PrimState m)
-> (# State# (PrimState m), MutableByteArray# (PrimState m) #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# (Int#
n# Int# -> Int# -> Int#
*# a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)) State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, MutableByteArray# (PrimState m) -> MutablePrimArray (PrimState m) a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# (PrimState m)
arr# #)
)
resizeMutablePrimArray :: forall m a. (PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> m (MutablePrimArray (PrimState m) a)
{-# INLINE resizeMutablePrimArray #-}
#if __GLASGOW_HASKELL__ >= 710
resizeMutablePrimArray :: MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#) (I# Int#
n#)
= (State# (PrimState m)
-> (# State# (PrimState m), MutablePrimArray (PrimState m) a #))
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case MutableByteArray# (PrimState m)
-> Int#
-> State# (PrimState m)
-> (# State# (PrimState m), MutableByteArray# (PrimState m) #)
forall d.
MutableByteArray# d
-> Int# -> State# d -> (# State# d, MutableByteArray# d #)
resizeMutableByteArray# MutableByteArray# (PrimState m)
arr# (Int#
n# Int# -> Int# -> Int#
*# a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)) State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr'# #) -> (# State# (PrimState m)
s'#, MutableByteArray# (PrimState m) -> MutablePrimArray (PrimState m) a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# (PrimState m)
arr'# #))
#else
resizeMutablePrimArray arr n
= do arr' <- newPrimArray n
copyMutablePrimArray arr' 0 arr 0 (min (sizeofMutablePrimArray arr) n)
return arr'
#endif
#if __GLASGOW_HASKELL__ >= 710
shrinkMutablePrimArray :: forall m a. (PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> m ()
{-# INLINE shrinkMutablePrimArray #-}
shrinkMutablePrimArray :: MutablePrimArray (PrimState m) a -> Int -> m ()
shrinkMutablePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#) (I# Int#
n#)
= (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableByteArray# (PrimState m)
-> Int# -> State# (PrimState m) -> State# (PrimState m)
forall d. MutableByteArray# d -> Int# -> State# d -> State# d
shrinkMutableByteArray# MutableByteArray# (PrimState m)
arr# (Int#
n# Int# -> Int# -> Int#
*# a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
#endif
readPrimArray :: (Prim a, PrimMonad m) => MutablePrimArray (PrimState m) a -> Int -> m a
{-# INLINE readPrimArray #-}
readPrimArray :: MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#)
= (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (MutableByteArray# (PrimState m)
-> Int# -> State# (PrimState m) -> (# State# (PrimState m), a #)
forall a s.
Prim a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
readByteArray# MutableByteArray# (PrimState m)
arr# Int#
i#)
writePrimArray ::
(Prim a, PrimMonad m)
=> MutablePrimArray (PrimState m) a
-> Int
-> a
-> m ()
{-# INLINE writePrimArray #-}
writePrimArray :: MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#) a
x
= (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableByteArray# (PrimState m)
-> Int# -> a -> State# (PrimState m) -> State# (PrimState m)
forall a s.
Prim a =>
MutableByteArray# s -> Int# -> a -> State# s -> State# s
writeByteArray# MutableByteArray# (PrimState m)
arr# Int#
i# a
x)
copyMutablePrimArray :: forall m a.
(PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> m ()
{-# INLINE copyMutablePrimArray #-}
copyMutablePrimArray :: MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
dst#) (I# Int#
doff#) (MutablePrimArray MutableByteArray# (PrimState m)
src#) (I# Int#
soff#) (I# Int#
n#)
= (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableByteArray# (PrimState m)
-> Int#
-> MutableByteArray# (PrimState m)
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
MutableByteArray# d
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyMutableByteArray#
MutableByteArray# (PrimState m)
src#
(Int#
soff# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
MutableByteArray# (PrimState m)
dst#
(Int#
doff# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
(Int#
n# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
)
copyPrimArray :: forall m a.
(PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> PrimArray a
-> Int
-> Int
-> m ()
{-# INLINE copyPrimArray #-}
copyPrimArray :: MutablePrimArray (PrimState m) a
-> Int -> PrimArray a -> Int -> Int -> m ()
copyPrimArray (MutablePrimArray MutableByteArray# (PrimState m)
dst#) (I# Int#
doff#) (PrimArray ByteArray#
src#) (I# Int#
soff#) (I# Int#
n#)
= (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (ByteArray#
-> Int#
-> MutableByteArray# (PrimState m)
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
ByteArray#
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyByteArray#
ByteArray#
src#
(Int#
soff# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
MutableByteArray# (PrimState m)
dst#
(Int#
doff# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
(Int#
n# Int# -> Int# -> Int#
*# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
)
#if __GLASGOW_HASKELL__ >= 708
copyPrimArrayToPtr :: forall m a. (PrimMonad m, Prim a)
=> Ptr a
-> PrimArray a
-> Int
-> Int
-> m ()
{-# INLINE copyPrimArrayToPtr #-}
copyPrimArrayToPtr :: Ptr a -> PrimArray a -> Int -> Int -> m ()
copyPrimArrayToPtr (Ptr Addr#
addr#) (PrimArray ByteArray#
ba#) (I# Int#
soff#) (I# Int#
n#) =
(State# (PrimState m) -> (# State# (PrimState m), () #)) -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\ State# (PrimState m)
s# ->
let s'# :: State# (PrimState m)
s'# = ByteArray#
-> Int#
-> Addr#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
ByteArray# -> Int# -> Addr# -> Int# -> State# d -> State# d
copyByteArrayToAddr# ByteArray#
ba# (Int#
soff# Int# -> Int# -> Int#
*# Int#
siz#) Addr#
addr# (Int#
n# Int# -> Int# -> Int#
*# Int#
siz#) State# (PrimState m)
s#
in (# State# (PrimState m)
s'#, () #))
where siz# :: Int#
siz# = a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)
copyMutablePrimArrayToPtr :: forall m a. (PrimMonad m, Prim a)
=> Ptr a
-> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> m ()
{-# INLINE copyMutablePrimArrayToPtr #-}
copyMutablePrimArrayToPtr :: Ptr a -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArrayToPtr (Ptr Addr#
addr#) (MutablePrimArray MutableByteArray# (PrimState m)
mba#) (I# Int#
soff#) (I# Int#
n#) =
(State# (PrimState m) -> (# State# (PrimState m), () #)) -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\ State# (PrimState m)
s# ->
let s'# :: State# (PrimState m)
s'# = MutableByteArray# (PrimState m)
-> Int#
-> Addr#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
MutableByteArray# d
-> Int# -> Addr# -> Int# -> State# d -> State# d
copyMutableByteArrayToAddr# MutableByteArray# (PrimState m)
mba# (Int#
soff# Int# -> Int# -> Int#
*# Int#
siz#) Addr#
addr# (Int#
n# Int# -> Int# -> Int#
*# Int#
siz#) State# (PrimState m)
s#
in (# State# (PrimState m)
s'#, () #))
where siz# :: Int#
siz# = a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)
#endif
setPrimArray
:: (Prim a, PrimMonad m)
=> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> a
-> m ()
{-# INLINE setPrimArray #-}
setPrimArray :: MutablePrimArray (PrimState m) a -> Int -> Int -> a -> m ()
setPrimArray (MutablePrimArray MutableByteArray# (PrimState m)
dst#) (I# Int#
doff#) (I# Int#
sz#) a
x
= (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableByteArray# (PrimState m)
-> Int#
-> Int#
-> a
-> State# (PrimState m)
-> State# (PrimState m)
forall a s.
Prim a =>
MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s
PT.setByteArray# MutableByteArray# (PrimState m)
dst# Int#
doff# Int#
sz# a
x)
getSizeofMutablePrimArray :: forall m a. (PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> m Int
{-# INLINE getSizeofMutablePrimArray #-}
#if __GLASGOW_HASKELL__ >= 801
getSizeofMutablePrimArray :: MutablePrimArray (PrimState m) a -> m Int
getSizeofMutablePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#)
= (State# (PrimState m) -> (# State# (PrimState m), Int #)) -> m Int
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# ->
case MutableByteArray# (PrimState m)
-> State# (PrimState m) -> (# State# (PrimState m), Int# #)
forall d. MutableByteArray# d -> State# d -> (# State# d, Int# #)
getSizeofMutableByteArray# MutableByteArray# (PrimState m)
arr# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, Int#
sz# #) -> (# State# (PrimState m)
s'#, Int# -> Int
I# (Int# -> Int# -> Int#
quotInt# Int#
sz# (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a))) #)
)
#else
getSizeofMutablePrimArray arr
= return (sizeofMutablePrimArray arr)
#endif
sizeofMutablePrimArray :: forall s a. Prim a => MutablePrimArray s a -> Int
{-# INLINE sizeofMutablePrimArray #-}
sizeofMutablePrimArray :: MutablePrimArray s a -> Int
sizeofMutablePrimArray (MutablePrimArray MutableByteArray# s
arr#) =
Int# -> Int
I# (Int# -> Int# -> Int#
quotInt# (MutableByteArray# s -> Int#
forall d. MutableByteArray# d -> Int#
sizeofMutableByteArray# MutableByteArray# s
arr#) (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
sameMutablePrimArray :: MutablePrimArray s a -> MutablePrimArray s a -> Bool
{-# INLINE sameMutablePrimArray #-}
sameMutablePrimArray :: MutablePrimArray s a -> MutablePrimArray s a -> Bool
sameMutablePrimArray (MutablePrimArray MutableByteArray# s
arr#) (MutablePrimArray MutableByteArray# s
brr#)
= Int# -> Bool
isTrue# (MutableByteArray# s -> MutableByteArray# s -> Int#
forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# MutableByteArray# s
arr# MutableByteArray# s
brr#)
freezePrimArray
:: (PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> m (PrimArray a)
{-# INLINE freezePrimArray #-}
freezePrimArray :: MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
freezePrimArray !MutablePrimArray (PrimState m) a
src !Int
off !Int
len = do
MutablePrimArray (PrimState m) a
dst <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
len
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArray MutablePrimArray (PrimState m) a
dst Int
0 MutablePrimArray (PrimState m) a
src Int
off Int
len
MutablePrimArray (PrimState m) a -> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) a
dst
thawPrimArray
:: (PrimMonad m, Prim a)
=> PrimArray a
-> Int
-> Int
-> m (MutablePrimArray (PrimState m) a)
{-# INLINE thawPrimArray #-}
thawPrimArray :: PrimArray a -> Int -> Int -> m (MutablePrimArray (PrimState m) a)
thawPrimArray !PrimArray a
src !Int
off !Int
len = do
MutablePrimArray (PrimState m) a
dst <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
len
MutablePrimArray (PrimState m) a
-> Int -> PrimArray a -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> PrimArray a -> Int -> Int -> m ()
copyPrimArray MutablePrimArray (PrimState m) a
dst Int
0 PrimArray a
src Int
off Int
len
MutablePrimArray (PrimState m) a
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutablePrimArray (PrimState m) a
dst
unsafeFreezePrimArray
:: PrimMonad m => MutablePrimArray (PrimState m) a -> m (PrimArray a)
{-# INLINE unsafeFreezePrimArray #-}
unsafeFreezePrimArray :: MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray (MutablePrimArray MutableByteArray# (PrimState m)
arr#)
= (State# (PrimState m) -> (# State# (PrimState m), PrimArray a #))
-> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case MutableByteArray# (PrimState m)
-> State# (PrimState m) -> (# State# (PrimState m), ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# (PrimState m)
arr# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, ByteArray#
arr'# #) -> (# State# (PrimState m)
s'#, ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ByteArray#
arr'# #))
unsafeThawPrimArray
:: PrimMonad m => PrimArray a -> m (MutablePrimArray (PrimState m) a)
{-# INLINE unsafeThawPrimArray #-}
unsafeThawPrimArray :: PrimArray a -> m (MutablePrimArray (PrimState m) a)
unsafeThawPrimArray (PrimArray ByteArray#
arr#)
= (State# (PrimState m)
-> (# State# (PrimState m), MutablePrimArray (PrimState m) a #))
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> (# State# (PrimState m)
s#, MutableByteArray# (PrimState m) -> MutablePrimArray (PrimState m) a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray (ByteArray# -> MutableByteArray# (PrimState m)
unsafeCoerce# ByteArray#
arr#) #))
indexPrimArray :: forall a. Prim a => PrimArray a -> Int -> a
{-# INLINE indexPrimArray #-}
indexPrimArray :: PrimArray a -> Int -> a
indexPrimArray (PrimArray ByteArray#
arr#) (I# Int#
i#) = ByteArray# -> Int# -> a
forall a. Prim a => ByteArray# -> Int# -> a
indexByteArray# ByteArray#
arr# Int#
i#
sizeofPrimArray :: forall a. Prim a => PrimArray a -> Int
{-# INLINE sizeofPrimArray #-}
sizeofPrimArray :: PrimArray a -> Int
sizeofPrimArray (PrimArray ByteArray#
arr#) = Int# -> Int
I# (Int# -> Int# -> Int#
quotInt# (ByteArray# -> Int#
sizeofByteArray# ByteArray#
arr#) (a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)))
#if __GLASGOW_HASKELL__ >= 802
isPrimArrayPinned :: PrimArray a -> Bool
{-# INLINE isPrimArrayPinned #-}
isPrimArrayPinned :: PrimArray a -> Bool
isPrimArrayPinned (PrimArray ByteArray#
arr#) = Int# -> Bool
isTrue# (ByteArray# -> Int#
Exts.isByteArrayPinned# ByteArray#
arr#)
isMutablePrimArrayPinned :: MutablePrimArray s a -> Bool
{-# INLINE isMutablePrimArrayPinned #-}
isMutablePrimArrayPinned :: MutablePrimArray s a -> Bool
isMutablePrimArrayPinned (MutablePrimArray MutableByteArray# s
marr#) = Int# -> Bool
isTrue# (MutableByteArray# s -> Int#
forall d. MutableByteArray# d -> Int#
Exts.isMutableByteArrayPinned# MutableByteArray# s
marr#)
#endif
{-# INLINE foldrPrimArray #-}
foldrPrimArray :: forall a b. Prim a => (a -> b -> b) -> b -> PrimArray a -> b
foldrPrimArray :: (a -> b -> b) -> b -> PrimArray a -> b
foldrPrimArray a -> b -> b
f b
z PrimArray a
arr = Int -> b
go Int
0
where
!sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
go :: Int -> b
go !Int
i
| Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
i = a -> b -> b
f (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i) (Int -> b
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1))
| Bool
otherwise = b
z
{-# INLINE foldrPrimArray' #-}
foldrPrimArray' :: forall a b. Prim a => (a -> b -> b) -> b -> PrimArray a -> b
foldrPrimArray' :: (a -> b -> b) -> b -> PrimArray a -> b
foldrPrimArray' a -> b -> b
f b
z0 PrimArray a
arr = Int -> b -> b
go (PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) b
z0
where
go :: Int -> b -> b
go !Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = b
acc
| Bool
otherwise = Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (a -> b -> b
f (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i) b
acc)
{-# INLINE foldlPrimArray #-}
foldlPrimArray :: forall a b. Prim a => (b -> a -> b) -> b -> PrimArray a -> b
foldlPrimArray :: (b -> a -> b) -> b -> PrimArray a -> b
foldlPrimArray b -> a -> b
f b
z PrimArray a
arr = Int -> b
go (PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
go :: Int -> b
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = b
z
| Bool
otherwise = b -> a -> b
f (Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i)
{-# INLINE foldlPrimArray' #-}
foldlPrimArray' :: forall a b. Prim a => (b -> a -> b) -> b -> PrimArray a -> b
foldlPrimArray' :: (b -> a -> b) -> b -> PrimArray a -> b
foldlPrimArray' b -> a -> b
f b
z0 PrimArray a
arr = Int -> b -> b
go Int
0 b
z0
where
!sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
go :: Int -> b -> b
go !Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> a -> b
f b
acc (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i))
| Bool
otherwise = b
acc
{-# INLINE foldlPrimArrayM' #-}
foldlPrimArrayM' :: (Prim a, Monad m) => (b -> a -> m b) -> b -> PrimArray a -> m b
foldlPrimArrayM' :: (b -> a -> m b) -> b -> PrimArray a -> m b
foldlPrimArrayM' b -> a -> m b
f b
z0 PrimArray a
arr = Int -> b -> m b
go Int
0 b
z0
where
!sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
go :: Int -> b -> m b
go !Int
i !b
acc1
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = do
b
acc2 <- b -> a -> m b
f b
acc1 (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i)
Int -> b -> m b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
acc2
| Bool
otherwise = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
acc1
{-# INLINE traversePrimArrayP #-}
traversePrimArrayP :: (PrimMonad m, Prim a, Prim b)
=> (a -> m b)
-> PrimArray a
-> m (PrimArray b)
traversePrimArrayP :: (a -> m b) -> PrimArray a -> m (PrimArray b)
traversePrimArrayP a -> m b
f PrimArray a
arr = do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray (PrimState m) b
marr <- Int -> m (MutablePrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> m ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
b
b <- a -> m b
f (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ix)
MutablePrimArray (PrimState m) b -> Int -> b -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) b
marr Int
ix b
b
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> m ()
go Int
0
MutablePrimArray (PrimState m) b -> m (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) b
marr
{-# INLINE filterPrimArrayP #-}
filterPrimArrayP :: (PrimMonad m, Prim a)
=> (a -> m Bool)
-> PrimArray a
-> m (PrimArray a)
filterPrimArrayP :: (a -> m Bool) -> PrimArray a -> m (PrimArray a)
filterPrimArrayP a -> m Bool
f PrimArray a
arr = do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray (PrimState m) a
marr <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> Int -> m Int
go !Int
ixSrc !Int
ixDst = if Int
ixSrc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let a :: a
a = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ixSrc
Bool
b <- a -> m Bool
f a
a
if Bool
b
then do
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) a
marr Int
ixDst a
a
Int -> Int -> m Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else Int -> Int -> m Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
ixDst
else Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
Int
lenDst <- Int -> Int -> m Int
go Int
0 Int
0
MutablePrimArray (PrimState m) a
marr' <- MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray MutablePrimArray (PrimState m) a
marr Int
lenDst
MutablePrimArray (PrimState m) a -> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) a
marr'
{-# INLINE mapMaybePrimArrayP #-}
mapMaybePrimArrayP :: (PrimMonad m, Prim a, Prim b)
=> (a -> m (Maybe b))
-> PrimArray a
-> m (PrimArray b)
mapMaybePrimArrayP :: (a -> m (Maybe b)) -> PrimArray a -> m (PrimArray b)
mapMaybePrimArrayP a -> m (Maybe b)
f PrimArray a
arr = do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray (PrimState m) b
marr <- Int -> m (MutablePrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> Int -> m Int
go !Int
ixSrc !Int
ixDst = if Int
ixSrc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let a :: a
a = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ixSrc
Maybe b
mb <- a -> m (Maybe b)
f a
a
case Maybe b
mb of
Just b
b -> do
MutablePrimArray (PrimState m) b -> Int -> b -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) b
marr Int
ixDst b
b
Int -> Int -> m Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Maybe b
Nothing -> Int -> Int -> m Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
ixDst
else Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
Int
lenDst <- Int -> Int -> m Int
go Int
0 Int
0
MutablePrimArray (PrimState m) b
marr' <- MutablePrimArray (PrimState m) b
-> Int -> m (MutablePrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray MutablePrimArray (PrimState m) b
marr Int
lenDst
MutablePrimArray (PrimState m) b -> m (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) b
marr'
{-# INLINE generatePrimArrayP #-}
generatePrimArrayP :: (PrimMonad m, Prim a)
=> Int
-> (Int -> m a)
-> m (PrimArray a)
generatePrimArrayP :: Int -> (Int -> m a) -> m (PrimArray a)
generatePrimArrayP Int
sz Int -> m a
f = do
MutablePrimArray (PrimState m) a
marr <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> m ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
a
b <- Int -> m a
f Int
ix
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) a
marr Int
ix a
b
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> m ()
go Int
0
MutablePrimArray (PrimState m) a -> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) a
marr
{-# INLINE replicatePrimArrayP #-}
replicatePrimArrayP :: (PrimMonad m, Prim a)
=> Int
-> m a
-> m (PrimArray a)
replicatePrimArrayP :: Int -> m a -> m (PrimArray a)
replicatePrimArrayP Int
sz m a
f = do
MutablePrimArray (PrimState m) a
marr <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> m ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
a
b <- m a
f
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) a
marr Int
ix a
b
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> m ()
go Int
0
MutablePrimArray (PrimState m) a -> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) a
marr
{-# INLINE mapPrimArray #-}
mapPrimArray :: (Prim a, Prim b)
=> (a -> b)
-> PrimArray a
-> PrimArray b
mapPrimArray :: (a -> b) -> PrimArray a -> PrimArray b
mapPrimArray a -> b
f PrimArray a
arr = (forall s. ST s (PrimArray b)) -> PrimArray b
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray b)) -> PrimArray b)
-> (forall s. ST s (PrimArray b)) -> PrimArray b
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray s b
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> ST s ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let b :: b
b = a -> b
f (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ix)
MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr Int
ix b
b
Int -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> ST s ()
go Int
0
MutablePrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr
{-# INLINE imapPrimArray #-}
imapPrimArray :: (Prim a, Prim b)
=> (Int -> a -> b)
-> PrimArray a
-> PrimArray b
imapPrimArray :: (Int -> a -> b) -> PrimArray a -> PrimArray b
imapPrimArray Int -> a -> b
f PrimArray a
arr = (forall s. ST s (PrimArray b)) -> PrimArray b
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray b)) -> PrimArray b)
-> (forall s. ST s (PrimArray b)) -> PrimArray b
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray s b
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> ST s ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let b :: b
b = Int -> a -> b
f Int
ix (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ix)
MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr Int
ix b
b
Int -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> ST s ()
go Int
0
MutablePrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr
{-# INLINE filterPrimArray #-}
filterPrimArray :: Prim a
=> (a -> Bool)
-> PrimArray a
-> PrimArray a
filterPrimArray :: (a -> Bool) -> PrimArray a -> PrimArray a
filterPrimArray a -> Bool
p PrimArray a
arr = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray s a
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> Int -> ST s Int
go !Int
ixSrc !Int
ixDst = if Int
ixSrc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let !a :: a
a = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ixSrc
if a -> Bool
p a
a
then do
MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr Int
ixDst a
a
Int -> Int -> ST s Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else Int -> Int -> ST s Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
ixDst
else Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
Int
dstLen <- Int -> Int -> ST s Int
go Int
0 Int
0
MutablePrimArray s a
marr' <- MutablePrimArray (PrimState (ST s)) a
-> Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr Int
dstLen
MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr'
filterPrimArrayA ::
(Applicative f, Prim a)
=> (a -> f Bool)
-> PrimArray a
-> f (PrimArray a)
filterPrimArrayA :: (a -> f Bool) -> PrimArray a -> f (PrimArray a)
filterPrimArrayA a -> f Bool
f = \ !PrimArray a
ary ->
let
!len :: Int
len = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
ary
go :: Int -> f (IxSTA a)
go !Int
ixSrc
| Int
ixSrc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = IxSTA a -> f (IxSTA a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IxSTA a -> f (IxSTA a)) -> IxSTA a -> f (IxSTA a)
forall a b. (a -> b) -> a -> b
$ (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
forall a.
(forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
IxSTA ((forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a)
-> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
forall a b. (a -> b) -> a -> b
$ \Int
ixDst MutableByteArray# s
_ -> Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
| Bool
otherwise = let x :: a
x = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
ary Int
ixSrc in
(Bool -> IxSTA a -> IxSTA a)
-> f Bool -> f (IxSTA a) -> f (IxSTA a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
(\Bool
keep (IxSTA forall s. Int -> MutableByteArray# s -> ST s Int
m) -> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
forall a.
(forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
IxSTA ((forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a)
-> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
forall a b. (a -> b) -> a -> b
$ \Int
ixDst MutableByteArray# s
mary -> if Bool
keep
then MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
ixDst a
x ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> MutableByteArray# s -> ST s Int
forall s. Int -> MutableByteArray# s -> ST s Int
m (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MutableByteArray# s
mary
else Int -> MutableByteArray# s -> ST s Int
forall s. Int -> MutableByteArray# s -> ST s Int
m Int
ixDst MutableByteArray# s
mary
)
(a -> f Bool
f a
x)
(Int -> f (IxSTA a)
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray a -> f (PrimArray a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray a
forall a. PrimArray a
emptyPrimArray
else Int -> IxSTA a -> PrimArray a
forall a. Prim a => Int -> IxSTA a -> PrimArray a
runIxSTA Int
len (IxSTA a -> PrimArray a) -> f (IxSTA a) -> f (PrimArray a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (IxSTA a)
go Int
0
mapMaybePrimArrayA ::
(Applicative f, Prim a, Prim b)
=> (a -> f (Maybe b))
-> PrimArray a
-> f (PrimArray b)
mapMaybePrimArrayA :: (a -> f (Maybe b)) -> PrimArray a -> f (PrimArray b)
mapMaybePrimArrayA a -> f (Maybe b)
f = \ !PrimArray a
ary ->
let
!len :: Int
len = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
ary
go :: Int -> f (IxSTA b)
go !Int
ixSrc
| Int
ixSrc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = IxSTA b -> f (IxSTA b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IxSTA b -> f (IxSTA b)) -> IxSTA b -> f (IxSTA b)
forall a b. (a -> b) -> a -> b
$ (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b
forall a.
(forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
IxSTA ((forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b)
-> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b
forall a b. (a -> b) -> a -> b
$ \Int
ixDst MutableByteArray# s
_ -> Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
| Bool
otherwise = let x :: a
x = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
ary Int
ixSrc in
(Maybe b -> IxSTA b -> IxSTA b)
-> f (Maybe b) -> f (IxSTA b) -> f (IxSTA b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
(\Maybe b
mb (IxSTA forall s. Int -> MutableByteArray# s -> ST s Int
m) -> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b
forall a.
(forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA a
IxSTA ((forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b)
-> (forall s. Int -> MutableByteArray# s -> ST s Int) -> IxSTA b
forall a b. (a -> b) -> a -> b
$ \Int
ixDst MutableByteArray# s
mary -> case Maybe b
mb of
Just b -> MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s b
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
ixDst b
b ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> MutableByteArray# s -> ST s Int
forall s. Int -> MutableByteArray# s -> ST s Int
m (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MutableByteArray# s
mary
Maybe b
Nothing -> Int -> MutableByteArray# s -> ST s Int
forall s. Int -> MutableByteArray# s -> ST s Int
m Int
ixDst MutableByteArray# s
mary
)
(a -> f (Maybe b)
f a
x)
(Int -> f (IxSTA b)
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray b -> f (PrimArray b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray b
forall a. PrimArray a
emptyPrimArray
else Int -> IxSTA b -> PrimArray b
forall a. Prim a => Int -> IxSTA a -> PrimArray a
runIxSTA Int
len (IxSTA b -> PrimArray b) -> f (IxSTA b) -> f (PrimArray b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (IxSTA b)
go Int
0
{-# INLINE mapMaybePrimArray #-}
mapMaybePrimArray :: (Prim a, Prim b)
=> (a -> Maybe b)
-> PrimArray a
-> PrimArray b
mapMaybePrimArray :: (a -> Maybe b) -> PrimArray a -> PrimArray b
mapMaybePrimArray a -> Maybe b
p PrimArray a
arr = (forall s. ST s (PrimArray b)) -> PrimArray b
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray b)) -> PrimArray b)
-> (forall s. ST s (PrimArray b)) -> PrimArray b
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray s b
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> Int -> ST s Int
go !Int
ixSrc !Int
ixDst = if Int
ixSrc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let !a :: a
a = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ixSrc
case a -> Maybe b
p a
a of
Just b
b -> do
MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr Int
ixDst b
b
Int -> Int -> ST s Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Maybe b
Nothing -> Int -> Int -> ST s Int
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
ixDst
else Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
ixDst
Int
dstLen <- Int -> Int -> ST s Int
go Int
0 Int
0
MutablePrimArray s b
marr' <- MutablePrimArray (PrimState (ST s)) b
-> Int -> ST s (MutablePrimArray (PrimState (ST s)) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr Int
dstLen
MutablePrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s b
MutablePrimArray (PrimState (ST s)) b
marr'
traversePrimArray ::
(Applicative f, Prim a, Prim b)
=> (a -> f b)
-> PrimArray a
-> f (PrimArray b)
traversePrimArray :: (a -> f b) -> PrimArray a -> f (PrimArray b)
traversePrimArray a -> f b
f = \ !PrimArray a
ary ->
let
!len :: Int
len = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
ary
go :: Int -> f (STA b)
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = STA b -> f (STA b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA b -> f (STA b)) -> STA b -> f (STA b)
forall a b. (a -> b) -> a -> b
$ (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b)
-> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary -> MutablePrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray (MutableByteArray# s -> MutablePrimArray s b
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary)
| a
x <- PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
ary Int
i
= (b -> STA b -> STA b) -> f b -> f (STA b) -> f (STA b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\b
b (STA forall s. MutableByteArray# s -> ST s (PrimArray b)
m) -> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b)
-> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary ->
MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s b
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
i b
b ST s () -> ST s (PrimArray b) -> ST s (PrimArray b)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MutableByteArray# s -> ST s (PrimArray b)
forall s. MutableByteArray# s -> ST s (PrimArray b)
m MutableByteArray# s
mary)
(a -> f b
f a
x) (Int -> f (STA b)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray b -> f (PrimArray b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray b
forall a. PrimArray a
emptyPrimArray
else Int -> STA b -> PrimArray b
forall a. Prim a => Int -> STA a -> PrimArray a
runSTA Int
len (STA b -> PrimArray b) -> f (STA b) -> f (PrimArray b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA b)
go Int
0
itraversePrimArray ::
(Applicative f, Prim a, Prim b)
=> (Int -> a -> f b)
-> PrimArray a
-> f (PrimArray b)
itraversePrimArray :: (Int -> a -> f b) -> PrimArray a -> f (PrimArray b)
itraversePrimArray Int -> a -> f b
f = \ !PrimArray a
ary ->
let
!len :: Int
len = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
ary
go :: Int -> f (STA b)
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = STA b -> f (STA b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA b -> f (STA b)) -> STA b -> f (STA b)
forall a b. (a -> b) -> a -> b
$ (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b)
-> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary -> MutablePrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray (MutableByteArray# s -> MutablePrimArray s b
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary)
| a
x <- PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
ary Int
i
= (b -> STA b -> STA b) -> f b -> f (STA b) -> f (STA b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\b
b (STA forall s. MutableByteArray# s -> ST s (PrimArray b)
m) -> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b)
-> (forall s. MutableByteArray# s -> ST s (PrimArray b)) -> STA b
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary ->
MutablePrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s b
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
i b
b ST s () -> ST s (PrimArray b) -> ST s (PrimArray b)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MutableByteArray# s -> ST s (PrimArray b)
forall s. MutableByteArray# s -> ST s (PrimArray b)
m MutableByteArray# s
mary)
(Int -> a -> f b
f Int
i a
x) (Int -> f (STA b)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray b -> f (PrimArray b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray b
forall a. PrimArray a
emptyPrimArray
else Int -> STA b -> PrimArray b
forall a. Prim a => Int -> STA a -> PrimArray a
runSTA Int
len (STA b -> PrimArray b) -> f (STA b) -> f (PrimArray b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA b)
go Int
0
{-# INLINE itraversePrimArrayP #-}
itraversePrimArrayP :: (Prim a, Prim b, PrimMonad m)
=> (Int -> a -> m b)
-> PrimArray a
-> m (PrimArray b)
itraversePrimArrayP :: (Int -> a -> m b) -> PrimArray a -> m (PrimArray b)
itraversePrimArrayP Int -> a -> m b
f PrimArray a
arr = do
let !sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
arr
MutablePrimArray (PrimState m) b
marr <- Int -> m (MutablePrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go :: Int -> m ()
go !Int
ix
| Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = do
MutablePrimArray (PrimState m) b -> Int -> b -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray (PrimState m) b
marr Int
ix (b -> m ()) -> m b -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> a -> m b
f Int
ix (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
ix)
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> m ()
go Int
0
MutablePrimArray (PrimState m) b -> m (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray (PrimState m) b
marr
{-# INLINE generatePrimArray #-}
generatePrimArray :: Prim a
=> Int
-> (Int -> a)
-> PrimArray a
generatePrimArray :: Int -> (Int -> a) -> PrimArray a
generatePrimArray Int
len Int -> a
f = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s a
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
len
let go :: Int -> ST s ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then do
MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr Int
ix (Int -> a
f Int
ix)
Int -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> ST s ()
go Int
0
MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr
{-# INLINE replicatePrimArray #-}
replicatePrimArray :: Prim a
=> Int
-> a
-> PrimArray a
replicatePrimArray :: Int -> a -> PrimArray a
replicatePrimArray Int
len a
a = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s a
marr <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
len
MutablePrimArray (PrimState (ST s)) a -> Int -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> Int -> a -> m ()
setPrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr Int
0 Int
len a
a
MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
marr
{-# INLINE generatePrimArrayA #-}
generatePrimArrayA ::
(Applicative f, Prim a)
=> Int
-> (Int -> f a)
-> f (PrimArray a)
generatePrimArrayA :: Int -> (Int -> f a) -> f (PrimArray a)
generatePrimArrayA Int
len Int -> f a
f =
let
go :: Int -> f (STA a)
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = STA a -> f (STA a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA a -> f (STA a)) -> STA a -> f (STA a)
forall a b. (a -> b) -> a -> b
$ (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a)
-> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary -> MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray (MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary)
| Bool
otherwise
= (a -> STA a -> STA a) -> f a -> f (STA a) -> f (STA a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
b (STA forall s. MutableByteArray# s -> ST s (PrimArray a)
m) -> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a)
-> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary ->
MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
i a
b ST s () -> ST s (PrimArray a) -> ST s (PrimArray a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MutableByteArray# s -> ST s (PrimArray a)
forall s. MutableByteArray# s -> ST s (PrimArray a)
m MutableByteArray# s
mary)
(Int -> f a
f Int
i) (Int -> f (STA a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray a -> f (PrimArray a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray a
forall a. PrimArray a
emptyPrimArray
else Int -> STA a -> PrimArray a
forall a. Prim a => Int -> STA a -> PrimArray a
runSTA Int
len (STA a -> PrimArray a) -> f (STA a) -> f (PrimArray a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA a)
go Int
0
{-# INLINE replicatePrimArrayA #-}
replicatePrimArrayA ::
(Applicative f, Prim a)
=> Int
-> f a
-> f (PrimArray a)
replicatePrimArrayA :: Int -> f a -> f (PrimArray a)
replicatePrimArrayA Int
len f a
f =
let
go :: Int -> f (STA a)
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = STA a -> f (STA a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA a -> f (STA a)) -> STA a -> f (STA a)
forall a b. (a -> b) -> a -> b
$ (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a)
-> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary -> MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray (MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary)
| Bool
otherwise
= (a -> STA a -> STA a) -> f a -> f (STA a) -> f (STA a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
b (STA forall s. MutableByteArray# s -> ST s (PrimArray a)
m) -> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a.
(forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
STA ((forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a)
-> (forall s. MutableByteArray# s -> ST s (PrimArray a)) -> STA a
forall a b. (a -> b) -> a -> b
$ \MutableByteArray# s
mary ->
MutablePrimArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray (MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
mary) Int
i a
b ST s () -> ST s (PrimArray a) -> ST s (PrimArray a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MutableByteArray# s -> ST s (PrimArray a)
forall s. MutableByteArray# s -> ST s (PrimArray a)
m MutableByteArray# s
mary)
f a
f (Int -> f (STA a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then PrimArray a -> f (PrimArray a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrimArray a
forall a. PrimArray a
emptyPrimArray
else Int -> STA a -> PrimArray a
forall a. Prim a => Int -> STA a -> PrimArray a
runSTA Int
len (STA a -> PrimArray a) -> f (STA a) -> f (PrimArray a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA a)
go Int
0
traversePrimArray_ ::
(Applicative f, Prim a)
=> (a -> f b)
-> PrimArray a
-> f ()
traversePrimArray_ :: (a -> f b) -> PrimArray a -> f ()
traversePrimArray_ a -> f b
f PrimArray a
a = Int -> f ()
go Int
0 where
!sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
a
go :: Int -> f ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then a -> f b
f (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a Int
ix) f b -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> f ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
itraversePrimArray_ ::
(Applicative f, Prim a)
=> (Int -> a -> f b)
-> PrimArray a
-> f ()
itraversePrimArray_ :: (Int -> a -> f b) -> PrimArray a -> f ()
itraversePrimArray_ Int -> a -> f b
f PrimArray a
a = Int -> f ()
go Int
0 where
!sz :: Int
sz = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray PrimArray a
a
go :: Int -> f ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then Int -> a -> f b
f Int
ix (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
a Int
ix) f b -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> f ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
newtype IxSTA a = IxSTA {IxSTA a -> forall s. Int -> MutableByteArray# s -> ST s Int
_runIxSTA :: forall s. Int -> MutableByteArray# s -> ST s Int}
runIxSTA :: forall a. Prim a
=> Int
-> IxSTA a
-> PrimArray a
runIxSTA :: Int -> IxSTA a -> PrimArray a
runIxSTA !Int
szUpper = \ (IxSTA forall s. Int -> MutableByteArray# s -> ST s Int
m) -> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s a
ar :: MutablePrimArray s a <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
szUpper
Int
sz <- Int -> MutableByteArray# s -> ST s Int
forall s. Int -> MutableByteArray# s -> ST s Int
m Int
0 (MutablePrimArray s a -> MutableByteArray# s
forall s a. MutablePrimArray s a -> MutableByteArray# s
unMutablePrimArray MutablePrimArray s a
ar)
MutablePrimArray s a
ar' <- MutablePrimArray (PrimState (ST s)) a
-> Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
ar Int
sz
MutablePrimArray (PrimState (ST s)) a -> ST s (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
ar'
{-# INLINE runIxSTA #-}
newtype STA a = STA {STA a -> forall s. MutableByteArray# s -> ST s (PrimArray a)
_runSTA :: forall s. MutableByteArray# s -> ST s (PrimArray a)}
runSTA :: forall a. Prim a => Int -> STA a -> PrimArray a
runSTA :: Int -> STA a -> PrimArray a
runSTA !Int
sz = \ (STA forall s. MutableByteArray# s -> ST s (PrimArray a)
m) -> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (PrimArray a)) -> PrimArray a)
-> (forall s. ST s (PrimArray a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz ST s (MutablePrimArray s a)
-> (MutablePrimArray s a -> ST s (PrimArray a))
-> ST s (PrimArray a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ (MutablePrimArray s a
ar :: MutablePrimArray s a) -> MutableByteArray# s -> ST s (PrimArray a)
forall s. MutableByteArray# s -> ST s (PrimArray a)
m (MutablePrimArray s a -> MutableByteArray# s
forall s a. MutablePrimArray s a -> MutableByteArray# s
unMutablePrimArray MutablePrimArray s a
ar)
{-# INLINE runSTA #-}
unMutablePrimArray :: MutablePrimArray s a -> MutableByteArray# s
unMutablePrimArray :: MutablePrimArray s a -> MutableByteArray# s
unMutablePrimArray (MutablePrimArray MutableByteArray# s
m) = MutableByteArray# s
m
newPinnedPrimArray :: forall m a. (PrimMonad m, Prim a)
=> Int -> m (MutablePrimArray (PrimState m) a)
{-# INLINE newPinnedPrimArray #-}
newPinnedPrimArray :: Int -> m (MutablePrimArray (PrimState m) a)
newPinnedPrimArray (I# Int#
n#)
= (State# (PrimState m)
-> (# State# (PrimState m), MutablePrimArray (PrimState m) a #))
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case Int#
-> State# (PrimState m)
-> (# State# (PrimState m), MutableByteArray# (PrimState m) #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newPinnedByteArray# (Int#
n# Int# -> Int# -> Int#
*# a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)) State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, MutableByteArray# (PrimState m) -> MutablePrimArray (PrimState m) a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# (PrimState m)
arr# #))
newAlignedPinnedPrimArray :: forall m a. (PrimMonad m, Prim a)
=> Int -> m (MutablePrimArray (PrimState m) a)
{-# INLINE newAlignedPinnedPrimArray #-}
newAlignedPinnedPrimArray :: Int -> m (MutablePrimArray (PrimState m) a)
newAlignedPinnedPrimArray (I# Int#
n#)
= (State# (PrimState m)
-> (# State# (PrimState m), MutablePrimArray (PrimState m) a #))
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case Int#
-> Int#
-> State# (PrimState m)
-> (# State# (PrimState m), MutableByteArray# (PrimState m) #)
forall d.
Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
newAlignedPinnedByteArray# (Int#
n# Int# -> Int# -> Int#
*# a -> Int#
forall a. Prim a => a -> Int#
sizeOf# (a
forall a. HasCallStack => a
undefined :: a)) (a -> Int#
forall a. Prim a => a -> Int#
alignment# (a
forall a. HasCallStack => a
undefined :: a)) State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, MutableByteArray# (PrimState m) -> MutablePrimArray (PrimState m) a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# (PrimState m)
arr# #))
primArrayContents :: PrimArray a -> Ptr a
{-# INLINE primArrayContents #-}
primArrayContents :: PrimArray a -> Ptr a
primArrayContents (PrimArray ByteArray#
arr#) = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents# ByteArray#
arr#)
mutablePrimArrayContents :: MutablePrimArray s a -> Ptr a
{-# INLINE mutablePrimArrayContents #-}
mutablePrimArrayContents :: MutablePrimArray s a -> Ptr a
mutablePrimArrayContents (MutablePrimArray MutableByteArray# s
arr#)
= Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents# (MutableByteArray# s -> ByteArray#
unsafeCoerce# MutableByteArray# s
arr#))
clonePrimArray :: Prim a
=> PrimArray a
-> Int
-> Int
-> PrimArray a
{-# INLINE clonePrimArray #-}
clonePrimArray :: PrimArray a -> Int -> Int -> PrimArray a
clonePrimArray PrimArray a
src Int
off Int
n = (forall s. ST s (MutablePrimArray s a)) -> PrimArray a
forall a. (forall s. ST s (MutablePrimArray s a)) -> PrimArray a
runPrimArray ((forall s. ST s (MutablePrimArray s a)) -> PrimArray a)
-> (forall s. ST s (MutablePrimArray s a)) -> PrimArray a
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s a
dst <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
n
MutablePrimArray (PrimState (ST s)) a
-> Int -> PrimArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> PrimArray a -> Int -> Int -> m ()
copyPrimArray MutablePrimArray s a
MutablePrimArray (PrimState (ST s)) a
dst Int
0 PrimArray a
src Int
off Int
n
MutablePrimArray s a -> ST s (MutablePrimArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutablePrimArray s a
dst
cloneMutablePrimArray :: (PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> m (MutablePrimArray (PrimState m) a)
{-# INLINE cloneMutablePrimArray #-}
cloneMutablePrimArray :: MutablePrimArray (PrimState m) a
-> Int -> Int -> m (MutablePrimArray (PrimState m) a)
cloneMutablePrimArray MutablePrimArray (PrimState m) a
src Int
off Int
n = do
MutablePrimArray (PrimState m) a
dst <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
n
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArray MutablePrimArray (PrimState m) a
dst Int
0 MutablePrimArray (PrimState m) a
src Int
off Int
n
MutablePrimArray (PrimState m) a
-> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutablePrimArray (PrimState m) a
dst
#if MIN_VERSION_base(4,10,0) /* In new GHCs, runRW# is available. */
runPrimArray
:: (forall s. ST s (MutablePrimArray s a))
-> PrimArray a
runPrimArray :: (forall s. ST s (MutablePrimArray s a)) -> PrimArray a
runPrimArray forall s. ST s (MutablePrimArray s a)
m = ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ((forall s. ST s (MutablePrimArray s a)) -> ByteArray#
forall a. (forall s. ST s (MutablePrimArray s a)) -> ByteArray#
runPrimArray# forall s. ST s (MutablePrimArray s a)
m)
runPrimArray#
:: (forall s. ST s (MutablePrimArray s a))
-> ByteArray#
runPrimArray# :: (forall s. ST s (MutablePrimArray s a)) -> ByteArray#
runPrimArray# forall s. ST s (MutablePrimArray s a)
m = case (State# RealWorld -> (# State# RealWorld, ByteArray# #))
-> (# State# RealWorld, ByteArray# #)
forall o. (State# RealWorld -> o) -> o
runRW# ((State# RealWorld -> (# State# RealWorld, ByteArray# #))
-> (# State# RealWorld, ByteArray# #))
-> (State# RealWorld -> (# State# RealWorld, ByteArray# #))
-> (# State# RealWorld, ByteArray# #)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case ST RealWorld (MutablePrimArray RealWorld a)
-> State# RealWorld
-> (# State# RealWorld, MutablePrimArray RealWorld a #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST ST RealWorld (MutablePrimArray RealWorld a)
forall s. ST s (MutablePrimArray s a)
m State# RealWorld
s of { (# State# RealWorld
s', MutablePrimArray MutableByteArray# RealWorld
mary# #) ->
MutableByteArray# RealWorld
-> State# RealWorld -> (# State# RealWorld, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# RealWorld
mary# State# RealWorld
s'} of (# State# RealWorld
_, ByteArray#
ary# #) -> ByteArray#
ary#
unST :: ST s a -> State# s -> (# State# s, a #)
unST :: ST s a -> State# s -> (# State# s, a #)
unST (GHCST.ST State# s -> (# State# s, a #)
f) = State# s -> (# State# s, a #)
f
#else /* In older GHCs, runRW# is not available. */
runPrimArray
:: (forall s. ST s (MutablePrimArray s a))
-> PrimArray a
runPrimArray m = runST $ m >>= unsafeFreezePrimArray
#endif