{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
module Z.Data.Array (
Arr(..)
, RealWorld
, Array(..)
, MutableArray(..)
, SmallArray(..)
, SmallMutableArray(..)
, uninitialized
, PrimArray(..)
, MutablePrimArray(..)
, Prim(..)
, newPinnedPrimArray, newAlignedPinnedPrimArray
, copyPrimArrayToPtr, copyMutablePrimArrayToPtr, copyPtrToMutablePrimArray
, primArrayContents, mutablePrimArrayContents, withPrimArrayContents, withMutablePrimArrayContents
, isPrimArrayPinned, isMutablePrimArrayPinned
, UnliftedArray(..)
, MutableUnliftedArray(..)
, PrimUnlifted(..)
, ArrayException(..)
, Cast
, castArray
, castMutableArray
) where
import Control.Exception (ArrayException (..), throw)
import Control.Monad.Primitive
import Data.Primitive.Array
import Data.Primitive.ByteArray
import Data.Primitive.PrimArray
import Data.Primitive.Ptr (copyPtrToMutablePrimArray)
import Data.Primitive.SmallArray
import Data.Primitive.Types
import GHC.Prim
import GHC.Types
import Z.Data.Array.Cast
import Z.Data.Array.UnliftedArray
uninitialized :: a
uninitialized :: a
uninitialized = ArrayException -> a
forall a e. Exception e => e -> a
throw (String -> ArrayException
UndefinedElement String
"Data.Array.uninitialized")
class Arr (arr :: * -> * ) a where
type MArr arr = (mar :: * -> * -> *) | mar -> arr
newArr :: (PrimMonad m, PrimState m ~ s) => Int -> m (MArr arr s a)
newArrWith :: (PrimMonad m, PrimState m ~ s) => Int -> a -> m (MArr arr s a)
readArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> m a
writeArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> a -> m ()
setArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> Int -> a -> m ()
indexArr :: arr a -> Int -> a
indexArr' :: arr a -> Int -> (# a #)
indexArrM :: (Monad m) => arr a -> Int -> m a
freezeArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> Int -> m (arr a)
thawArr :: (PrimMonad m, PrimState m ~ s) => arr a -> Int -> Int -> m (MArr arr s a)
unsafeFreezeArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> m (arr a)
unsafeThawArr :: (PrimMonad m, PrimState m ~ s) => arr a -> m (MArr arr s a)
copyArr :: (PrimMonad m, PrimState m ~ s)
=> MArr arr s a
-> Int
-> arr a
-> Int
-> Int
-> m ()
copyMutableArr :: (PrimMonad m, PrimState m ~ s)
=> MArr arr s a
-> Int
-> MArr arr s a
-> Int
-> Int
-> m ()
moveArr :: (PrimMonad m, PrimState m ~ s)
=> MArr arr s a
-> Int
-> MArr arr s a
-> Int
-> Int
-> m ()
cloneArr :: arr a -> Int -> Int -> arr a
cloneMutableArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> Int -> m (MArr arr s a)
resizeMutableArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> m (MArr arr s a)
shrinkMutableArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> Int -> m ()
sameMutableArr :: MArr arr s a -> MArr arr s a -> Bool
sizeofArr :: arr a -> Int
sizeofMutableArr :: (PrimMonad m, PrimState m ~ s) => MArr arr s a -> m Int
sameArr :: arr a -> arr a -> Bool
instance Arr Array a where
type MArr Array = MutableArray
newArr :: Int -> m (MArr Array s a)
newArr Int
n = Int -> a -> m (MutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
n a
forall a. a
uninitialized
{-# INLINE newArr #-}
newArrWith :: Int -> a -> m (MArr Array s a)
newArrWith = Int -> a -> m (MArr Array s a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray
{-# INLINE newArrWith #-}
readArr :: MArr Array s a -> Int -> m a
readArr = MArr Array s a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> m a
readArray
{-# INLINE readArr #-}
writeArr :: MArr Array s a -> Int -> a -> m ()
writeArr = MArr Array s a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray
{-# INLINE writeArr #-}
setArr :: MArr Array s a -> Int -> Int -> a -> m ()
setArr MArr Array s a
marr Int
s Int
l a
x = Int -> m ()
go Int
s
where
!sl :: Int
sl = Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sl = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = MutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray (PrimState m) a
MArr Array s a
marr Int
i a
x m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
{-# INLINE setArr #-}
indexArr :: Array a -> Int -> a
indexArr = Array a -> Int -> a
forall a. Array a -> Int -> a
indexArray
{-# INLINE indexArr #-}
indexArr' :: Array a -> Int -> (# a #)
indexArr' (Array Array# a
arr#) (I# Int#
i#) = Array# a -> Int# -> (# a #)
forall a. Array# a -> Int# -> (# a #)
indexArray# Array# a
arr# Int#
i#
{-# INLINE indexArr' #-}
indexArrM :: Array a -> Int -> m a
indexArrM = Array a -> Int -> m a
forall (m :: * -> *) a. Monad m => Array a -> Int -> m a
indexArrayM
{-# INLINE indexArrM #-}
freezeArr :: MArr Array s a -> Int -> Int -> m (Array a)
freezeArr = MArr Array s a -> Int -> Int -> m (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> Int -> m (Array a)
freezeArray
{-# INLINE freezeArr #-}
thawArr :: Array a -> Int -> Int -> m (MArr Array s a)
thawArr = Array a -> Int -> Int -> m (MArr Array s a)
forall (m :: * -> *) a.
PrimMonad m =>
Array a -> Int -> Int -> m (MutableArray (PrimState m) a)
thawArray
{-# INLINE thawArr #-}
unsafeFreezeArr :: MArr Array s a -> m (Array a)
unsafeFreezeArr = MArr Array s a -> m (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray
{-# INLINE unsafeFreezeArr #-}
unsafeThawArr :: Array a -> m (MArr Array s a)
unsafeThawArr = Array a -> m (MArr Array s a)
forall (m :: * -> *) a.
PrimMonad m =>
Array a -> m (MutableArray (PrimState m) a)
unsafeThawArray
{-# INLINE unsafeThawArr #-}
copyArr :: MArr Array s a -> Int -> Array a -> Int -> Int -> m ()
copyArr = MArr Array s a -> Int -> Array a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> Array a -> Int -> Int -> m ()
copyArray
{-# INLINE copyArr #-}
copyMutableArr :: MArr Array s a -> Int -> MArr Array s a -> Int -> Int -> m ()
copyMutableArr = MArr Array s a -> Int -> MArr Array s a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
copyMutableArray
{-# INLINE copyMutableArr #-}
moveArr :: MArr Array s a -> Int -> MArr Array s a -> Int -> Int -> m ()
moveArr MArr Array s a
marr1 Int
s1 MArr Array s a
marr2 Int
s2 Int
l
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| MutableArray s a -> MutableArray s a -> Bool
forall s a. MutableArray s a -> MutableArray s a -> Bool
sameMutableArray MutableArray s a
MArr Array s a
marr1 MutableArray s a
MArr Array s a
marr2 =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
s1 Int
s2 of
Ordering
LT ->
let !d :: Int
d = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s1
!s2l :: Int
s2l = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
s2l = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- MutableArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> m a
readArray MutableArray (PrimState m) a
MArr Array s a
marr2 Int
i
MutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray (PrimState m) a
MArr Array s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Int -> m ()
go Int
s2
Ordering
EQ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ordering
GT ->
let !d :: Int
d = Int
s1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s2
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
s2 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- MutableArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> m a
readArray MutableArray (PrimState m) a
MArr Array s a
marr2 Int
i
MutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray (PrimState m) a
MArr Array s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
in Int -> m ()
go (Int
s2Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
copyMutableArray MutableArray (PrimState m) a
MArr Array s a
marr1 Int
s1 MutableArray (PrimState m) a
MArr Array s a
marr2 Int
s2 Int
l
{-# INLINE moveArr #-}
cloneArr :: Array a -> Int -> Int -> Array a
cloneArr = Array a -> Int -> Int -> Array a
forall a. Array a -> Int -> Int -> Array a
cloneArray
{-# INLINE cloneArr #-}
cloneMutableArr :: MArr Array s a -> Int -> Int -> m (MArr Array s a)
cloneMutableArr = MArr Array s a -> Int -> Int -> m (MArr Array s a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> Int -> m (MutableArray (PrimState m) a)
cloneMutableArray
{-# INLINE cloneMutableArr #-}
resizeMutableArr :: MArr Array s a -> Int -> m (MArr Array s a)
resizeMutableArr MArr Array s a
marr Int
n = do
MutableArray s a
marr' <- Int -> a -> m (MutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
n a
forall a. a
uninitialized
MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
copyMutableArray MutableArray s a
MutableArray (PrimState m) a
marr' Int
0 MutableArray (PrimState m) a
MArr Array s a
marr Int
0 (MutableArray s a -> Int
forall s a. MutableArray s a -> Int
sizeofMutableArray MutableArray s a
MArr Array s a
marr)
MutableArray s a -> m (MutableArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutableArray s a
marr'
{-# INLINE resizeMutableArr #-}
shrinkMutableArr :: MArr Array s a -> Int -> m ()
shrinkMutableArr MArr Array s a
_ Int
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE shrinkMutableArr #-}
sameMutableArr :: MArr Array s a -> MArr Array s a -> Bool
sameMutableArr = MArr Array s a -> MArr Array s a -> Bool
forall s a. MutableArray s a -> MutableArray s a -> Bool
sameMutableArray
{-# INLINE sameMutableArr #-}
sizeofArr :: Array a -> Int
sizeofArr = Array a -> Int
forall a. Array a -> Int
sizeofArray
{-# INLINE sizeofArr #-}
sizeofMutableArr :: MArr Array s a -> m Int
sizeofMutableArr = Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int)
-> (MutableArray s a -> Int) -> MutableArray s a -> m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutableArray s a -> Int
forall s a. MutableArray s a -> Int
sizeofMutableArray
{-# INLINE sizeofMutableArr #-}
sameArr :: Array a -> Array a -> Bool
sameArr (Array Array# a
arr1#) (Array Array# a
arr2#) = Int# -> Bool
isTrue# (
MutableArray# Any Any -> MutableArray# Any Any -> Int#
forall d a. MutableArray# d a -> MutableArray# d a -> Int#
sameMutableArray# (Array# a -> MutableArray# Any Any
unsafeCoerce# Array# a
arr1#) (Array# a -> MutableArray# Any Any
unsafeCoerce# Array# a
arr2#))
{-# INLINE sameArr #-}
instance Arr SmallArray a where
type MArr SmallArray = SmallMutableArray
newArr :: Int -> m (MArr SmallArray s a)
newArr Int
n = Int -> a -> m (SmallMutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
n a
forall a. a
uninitialized
{-# INLINE newArr #-}
newArrWith :: Int -> a -> m (MArr SmallArray s a)
newArrWith = Int -> a -> m (MArr SmallArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray
{-# INLINE newArrWith #-}
readArr :: MArr SmallArray s a -> Int -> m a
readArr = MArr SmallArray s a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray
{-# INLINE readArr #-}
writeArr :: MArr SmallArray s a -> Int -> a -> m ()
writeArr = MArr SmallArray s a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray
{-# INLINE writeArr #-}
setArr :: MArr SmallArray s a -> Int -> Int -> a -> m ()
setArr MArr SmallArray s a
marr Int
s Int
l a
x = Int -> m ()
go Int
s
where
!sl :: Int
sl = Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sl = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = SmallMutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr Int
i a
x m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
{-# INLINE setArr #-}
indexArr :: SmallArray a -> Int -> a
indexArr = SmallArray a -> Int -> a
forall a. SmallArray a -> Int -> a
indexSmallArray
{-# INLINE indexArr #-}
indexArr' :: SmallArray a -> Int -> (# a #)
indexArr' (SmallArray SmallArray# a
arr#) (I# Int#
i#) = SmallArray# a -> Int# -> (# a #)
forall a. SmallArray# a -> Int# -> (# a #)
indexSmallArray# SmallArray# a
arr# Int#
i#
{-# INLINE indexArr' #-}
indexArrM :: SmallArray a -> Int -> m a
indexArrM = SmallArray a -> Int -> m a
forall (m :: * -> *) a. Monad m => SmallArray a -> Int -> m a
indexSmallArrayM
{-# INLINE indexArrM #-}
freezeArr :: MArr SmallArray s a -> Int -> Int -> m (SmallArray a)
freezeArr = MArr SmallArray s a -> Int -> Int -> m (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
freezeSmallArray
{-# INLINE freezeArr #-}
thawArr :: SmallArray a -> Int -> Int -> m (MArr SmallArray s a)
thawArr = SmallArray a -> Int -> Int -> m (MArr SmallArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallArray a -> Int -> Int -> m (SmallMutableArray (PrimState m) a)
thawSmallArray
{-# INLINE thawArr #-}
unsafeFreezeArr :: MArr SmallArray s a -> m (SmallArray a)
unsafeFreezeArr = MArr SmallArray s a -> m (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray
{-# INLINE unsafeFreezeArr #-}
unsafeThawArr :: SmallArray a -> m (MArr SmallArray s a)
unsafeThawArr = SmallArray a -> m (MArr SmallArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallArray a -> m (SmallMutableArray (PrimState m) a)
unsafeThawSmallArray
{-# INLINE unsafeThawArr #-}
copyArr :: MArr SmallArray s a -> Int -> SmallArray a -> Int -> Int -> m ()
copyArr = MArr SmallArray s a -> Int -> SmallArray a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray
{-# INLINE copyArr #-}
copyMutableArr :: MArr SmallArray s a
-> Int -> MArr SmallArray s a -> Int -> Int -> m ()
copyMutableArr = MArr SmallArray s a
-> Int -> MArr SmallArray s a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
copySmallMutableArray
{-# INLINE copyMutableArr #-}
moveArr :: MArr SmallArray s a
-> Int -> MArr SmallArray s a -> Int -> Int -> m ()
moveArr MArr SmallArray s a
marr1 Int
s1 MArr SmallArray s a
marr2 Int
s2 Int
l
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| MArr SmallArray s a -> MArr SmallArray s a -> Bool
forall (arr :: * -> *) a s.
Arr arr a =>
MArr arr s a -> MArr arr s a -> Bool
sameMutableArr MArr SmallArray s a
marr1 MArr SmallArray s a
marr2 =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
s1 Int
s2 of
Ordering
LT ->
let !d :: Int
d = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s1
!s2l :: Int
s2l = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
s2l = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- SmallMutableArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr2 Int
i
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Int -> m ()
go Int
s2
Ordering
EQ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ordering
GT ->
let !d :: Int
d = Int
s1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s2
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
s2 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- SmallMutableArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr2 Int
i
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
in Int -> m ()
go (Int
s2Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
copySmallMutableArray SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr1 Int
s1 SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr2 Int
s2 Int
l
{-# INLINE moveArr #-}
cloneArr :: SmallArray a -> Int -> Int -> SmallArray a
cloneArr = SmallArray a -> Int -> Int -> SmallArray a
forall a. SmallArray a -> Int -> Int -> SmallArray a
cloneSmallArray
{-# INLINE cloneArr #-}
cloneMutableArr :: MArr SmallArray s a -> Int -> Int -> m (MArr SmallArray s a)
cloneMutableArr = MArr SmallArray s a -> Int -> Int -> m (MArr SmallArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> Int -> m (SmallMutableArray (PrimState m) a)
cloneSmallMutableArray
{-# INLINE cloneMutableArr #-}
resizeMutableArr :: MArr SmallArray s a -> Int -> m (MArr SmallArray s a)
resizeMutableArr MArr SmallArray s a
marr Int
n = do
SmallMutableArray s a
marr' <- Int -> a -> m (SmallMutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
n a
forall a. a
uninitialized
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
copySmallMutableArray SmallMutableArray s a
SmallMutableArray (PrimState m) a
marr' Int
0 SmallMutableArray (PrimState m) a
MArr SmallArray s a
marr Int
0 (SmallMutableArray s a -> Int
forall s a. SmallMutableArray s a -> Int
sizeofSmallMutableArray SmallMutableArray s a
MArr SmallArray s a
marr)
SmallMutableArray s a -> m (SmallMutableArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return SmallMutableArray s a
marr'
{-# INLINE resizeMutableArr #-}
shrinkMutableArr :: MArr SmallArray s a -> Int -> m ()
shrinkMutableArr (SmallMutableArray smarr#) (I# Int#
n#) = (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (
SmallMutableArray# s a -> Int# -> State# s -> State# s
forall d a. SmallMutableArray# d a -> Int# -> State# d -> State# d
shrinkSmallMutableArray# SmallMutableArray# s a
smarr# Int#
n#)
{-# INLINE shrinkMutableArr #-}
sameMutableArr :: MArr SmallArray s a -> MArr SmallArray s a -> Bool
sameMutableArr (SmallMutableArray smarr1#) (SmallMutableArray smarr2#) =
Int# -> Bool
isTrue# (SmallMutableArray# s a -> SmallMutableArray# s a -> Int#
forall d a.
SmallMutableArray# d a -> SmallMutableArray# d a -> Int#
sameSmallMutableArray# SmallMutableArray# s a
smarr1# SmallMutableArray# s a
smarr2#)
{-# INLINE sameMutableArr #-}
sizeofArr :: SmallArray a -> Int
sizeofArr = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray
{-# INLINE sizeofArr #-}
sizeofMutableArr :: MArr SmallArray s a -> m Int
sizeofMutableArr = Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int)
-> (SmallMutableArray s a -> Int) -> SmallMutableArray s a -> m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SmallMutableArray s a -> Int
forall s a. SmallMutableArray s a -> Int
sizeofSmallMutableArray
{-# INLINE sizeofMutableArr #-}
sameArr :: SmallArray a -> SmallArray a -> Bool
sameArr (SmallArray SmallArray# a
arr1#) (SmallArray SmallArray# a
arr2#) = Int# -> Bool
isTrue# (
SmallMutableArray# Any Any -> SmallMutableArray# Any Any -> Int#
forall d a.
SmallMutableArray# d a -> SmallMutableArray# d a -> Int#
sameSmallMutableArray# (SmallArray# a -> SmallMutableArray# Any Any
unsafeCoerce# SmallArray# a
arr1#) (SmallArray# a -> SmallMutableArray# Any Any
unsafeCoerce# SmallArray# a
arr2#))
{-# INLINE sameArr #-}
instance Prim a => Arr PrimArray a where
type MArr PrimArray = MutablePrimArray
newArr :: Int -> m (MArr PrimArray s a)
newArr = Int -> m (MArr PrimArray s a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray
{-# INLINE newArr #-}
newArrWith :: Int -> a -> m (MArr PrimArray s a)
newArrWith Int
n a
x = do
MutablePrimArray s a
marr <- 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 -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> Int -> a -> m ()
setPrimArray MutablePrimArray s a
MutablePrimArray (PrimState m) a
marr Int
0 Int
n a
x
MutablePrimArray s a -> m (MutablePrimArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutablePrimArray s a
marr
{-# INLINE newArrWith #-}
readArr :: MArr PrimArray s a -> Int -> m a
readArr = MArr PrimArray s a -> Int -> m a
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray
{-# INLINE readArr #-}
writeArr :: MArr PrimArray s a -> Int -> a -> m ()
writeArr = MArr PrimArray s a -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray
{-# INLINE writeArr #-}
setArr :: MArr PrimArray s a -> Int -> Int -> a -> m ()
setArr = MArr PrimArray s a -> Int -> Int -> a -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> Int -> a -> m ()
setPrimArray
{-# INLINE setArr #-}
indexArr :: PrimArray a -> Int -> a
indexArr = PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray
{-# INLINE indexArr #-}
indexArr' :: PrimArray a -> Int -> (# a #)
indexArr' PrimArray a
arr Int
i = (# PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i #)
{-# INLINE indexArr' #-}
indexArrM :: PrimArray a -> Int -> m a
indexArrM PrimArray a
arr Int
i = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (PrimArray a -> Int -> a
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray a
arr Int
i)
{-# INLINE indexArrM #-}
freezeArr :: MArr PrimArray s a -> Int -> Int -> m (PrimArray a)
freezeArr = MArr PrimArray s a -> Int -> Int -> m (PrimArray a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
freezePrimArray
{-# INLINE freezeArr #-}
thawArr :: PrimArray a -> Int -> Int -> m (MArr PrimArray s a)
thawArr PrimArray a
arr Int
s Int
l = do
MutablePrimArray s a
marr' <- Int -> m (MutablePrimArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
l
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 s a
MutablePrimArray (PrimState m) a
marr' Int
0 PrimArray a
arr Int
s Int
l
MutablePrimArray s a -> m (MutablePrimArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutablePrimArray s a
marr'
{-# INLINE thawArr #-}
unsafeFreezeArr :: MArr PrimArray s a -> m (PrimArray a)
unsafeFreezeArr = MArr PrimArray s a -> m (PrimArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray
{-# INLINE unsafeFreezeArr #-}
unsafeThawArr :: PrimArray a -> m (MArr PrimArray s a)
unsafeThawArr = PrimArray a -> m (MArr PrimArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
PrimArray a -> m (MutablePrimArray (PrimState m) a)
unsafeThawPrimArray
{-# INLINE unsafeThawArr #-}
copyArr :: MArr PrimArray s a -> Int -> PrimArray a -> Int -> Int -> m ()
copyArr = MArr PrimArray s 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
{-# INLINE copyArr #-}
copyMutableArr :: MArr PrimArray s a
-> Int -> MArr PrimArray s a -> Int -> Int -> m ()
copyMutableArr = MArr PrimArray s a
-> Int -> MArr PrimArray s a -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArray
{-# INLINE copyMutableArr #-}
moveArr :: MArr PrimArray s a
-> Int -> MArr PrimArray s a -> Int -> Int -> m ()
moveArr (MutablePrimArray dst) Int
doff (MutablePrimArray src) Int
soff Int
n =
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
moveByteArray (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
dst) (Int
doffInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
siz) (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
src) (Int
soffInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
siz) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
siz)
where siz :: Int
siz = a -> Int
forall a. Prim a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)
{-# INLINE moveArr #-}
cloneArr :: PrimArray a -> Int -> Int -> PrimArray a
cloneArr = PrimArray a -> Int -> Int -> PrimArray a
forall a. Prim a => PrimArray a -> Int -> Int -> PrimArray a
clonePrimArray
{-# INLINE cloneArr #-}
cloneMutableArr :: MArr PrimArray s a -> Int -> Int -> m (MArr PrimArray s a)
cloneMutableArr = MArr PrimArray s a -> Int -> Int -> m (MArr PrimArray s a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> Int -> m (MutablePrimArray (PrimState m) a)
cloneMutablePrimArray
{-# INLINE cloneMutableArr #-}
resizeMutableArr :: MArr PrimArray s a -> Int -> m (MArr PrimArray s a)
resizeMutableArr = MArr PrimArray s a -> Int -> m (MArr PrimArray s a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray
{-# INLINE resizeMutableArr #-}
shrinkMutableArr :: MArr PrimArray s a -> Int -> m ()
shrinkMutableArr = MArr PrimArray s a -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> Int -> m ()
shrinkMutablePrimArray
{-# INLINE shrinkMutableArr #-}
sameMutableArr :: MArr PrimArray s a -> MArr PrimArray s a -> Bool
sameMutableArr = MArr PrimArray s a -> MArr PrimArray s a -> Bool
forall s a. MutablePrimArray s a -> MutablePrimArray s a -> Bool
sameMutablePrimArray
{-# INLINE sameMutableArr #-}
sizeofArr :: PrimArray a -> Int
sizeofArr = PrimArray a -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray
{-# INLINE sizeofArr #-}
sizeofMutableArr :: MArr PrimArray s a -> m Int
sizeofMutableArr = MArr PrimArray s a -> m Int
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> m Int
getSizeofMutablePrimArray
{-# INLINE sizeofMutableArr #-}
sameArr :: PrimArray a -> PrimArray a -> Bool
sameArr (PrimArray ByteArray#
ba1#) (PrimArray ByteArray#
ba2#) =
Int# -> Bool
isTrue# (MutableByteArray# Any -> MutableByteArray# Any -> Int#
forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# (ByteArray# -> MutableByteArray# Any
unsafeCoerce# ByteArray#
ba1#) (ByteArray# -> MutableByteArray# Any
unsafeCoerce# ByteArray#
ba2#))
{-# INLINE sameArr #-}
instance PrimUnlifted a => Arr UnliftedArray a where
type MArr UnliftedArray = MutableUnliftedArray
newArr :: Int -> m (MArr UnliftedArray s a)
newArr = Int -> m (MArr UnliftedArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray
{-# INLINE newArr #-}
newArrWith :: Int -> a -> m (MArr UnliftedArray s a)
newArrWith = Int -> a -> m (MArr UnliftedArray s a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray
{-# INLINE newArrWith #-}
readArr :: MArr UnliftedArray s a -> Int -> m a
readArr = MArr UnliftedArray s a -> Int -> m a
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> m a
readUnliftedArray
{-# INLINE readArr #-}
writeArr :: MArr UnliftedArray s a -> Int -> a -> m ()
writeArr = MArr UnliftedArray s a -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray
{-# INLINE writeArr #-}
setArr :: MArr UnliftedArray s a -> Int -> Int -> a -> m ()
setArr = MArr UnliftedArray s a -> Int -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> Int -> a -> m ()
setUnliftedArray
{-# INLINE setArr #-}
indexArr :: UnliftedArray a -> Int -> a
indexArr = UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray
{-# INLINE indexArr #-}
indexArr' :: UnliftedArray a -> Int -> (# a #)
indexArr' UnliftedArray a
arr Int
i = (# UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i #)
{-# INLINE indexArr' #-}
indexArrM :: UnliftedArray a -> Int -> m a
indexArrM UnliftedArray a
arr Int
i = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i)
{-# INLINE indexArrM #-}
freezeArr :: MArr UnliftedArray s a -> Int -> Int -> m (UnliftedArray a)
freezeArr = MArr UnliftedArray s a -> Int -> Int -> m (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (UnliftedArray a)
freezeUnliftedArray
{-# INLINE freezeArr #-}
thawArr :: UnliftedArray a -> Int -> Int -> m (MArr UnliftedArray s a)
thawArr = UnliftedArray a -> Int -> Int -> m (MArr UnliftedArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
UnliftedArray a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
thawUnliftedArray
{-# INLINE thawArr #-}
unsafeFreezeArr :: MArr UnliftedArray s a -> m (UnliftedArray a)
unsafeFreezeArr = MArr UnliftedArray s a -> m (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray
{-# INLINE unsafeFreezeArr #-}
unsafeThawArr :: UnliftedArray a -> m (MArr UnliftedArray s a)
unsafeThawArr (UnliftedArray ArrayArray#
arr#) = (State# (PrimState m)
-> (# State# (PrimState m), MutableUnliftedArray s a #))
-> m (MutableUnliftedArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ( \ State# (PrimState m)
s0# ->
let !(# State# s
s1#, MutableArray# s Any
marr# #) = Array# Any -> State# s -> (# State# s, MutableArray# s Any #)
forall a d.
Array# a -> State# d -> (# State# d, MutableArray# d a #)
unsafeThawArray# (ArrayArray# -> Array# Any
unsafeCoerce# ArrayArray#
arr#) State# s
State# (PrimState m)
s0#
in (# State# s
State# (PrimState m)
s1#, MutableArrayArray# s -> MutableUnliftedArray s a
forall s a. MutableArrayArray# s -> MutableUnliftedArray s a
MutableUnliftedArray (MutableArray# s Any -> MutableArrayArray# s
unsafeCoerce# MutableArray# s Any
marr#) #)
)
{-# INLINE unsafeThawArr #-}
copyArr :: MArr UnliftedArray s a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyArr = MArr UnliftedArray s a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray
{-# INLINE copyArr #-}
copyMutableArr :: MArr UnliftedArray s a
-> Int -> MArr UnliftedArray s a -> Int -> Int -> m ()
copyMutableArr = MArr UnliftedArray s a
-> Int -> MArr UnliftedArray s a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutableUnliftedArray
{-# INLINE copyMutableArr #-}
moveArr :: MArr UnliftedArray s a
-> Int -> MArr UnliftedArray s a -> Int -> Int -> m ()
moveArr MArr UnliftedArray s a
marr1 Int
s1 MArr UnliftedArray s a
marr2 Int
s2 Int
l
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
forall s a.
MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
sameMutableUnliftedArray MutableUnliftedArray s a
MArr UnliftedArray s a
marr1 MutableUnliftedArray s a
MArr UnliftedArray s a
marr2 =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
s1 Int
s2 of
Ordering
LT ->
let !d :: Int
d = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s1
!s2l :: Int
s2l = Int
s2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
s2l = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- MutableUnliftedArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> m a
readUnliftedArray MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr2 Int
i
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Int -> m ()
go Int
s2
Ordering
EQ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ordering
GT ->
let !d :: Int
d = Int
s1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s2
go :: Int -> m ()
go !Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
s2 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do a
x <- MutableUnliftedArray (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> m a
readUnliftedArray MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr2 Int
i
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr1 (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
d) a
x
Int -> m ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
in Int -> m ()
go (Int
s2Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutableUnliftedArray MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr1 Int
s1 MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr2 Int
s2 Int
l
{-# INLINE moveArr #-}
cloneArr :: UnliftedArray a -> Int -> Int -> UnliftedArray a
cloneArr = UnliftedArray a -> Int -> Int -> UnliftedArray a
forall a. UnliftedArray a -> Int -> Int -> UnliftedArray a
cloneUnliftedArray
{-# INLINE cloneArr #-}
cloneMutableArr :: MArr UnliftedArray s a -> Int -> Int -> m (MArr UnliftedArray s a)
cloneMutableArr = MArr UnliftedArray s a -> Int -> Int -> m (MArr UnliftedArray s a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
cloneMutableUnliftedArray
{-# INLINE cloneMutableArr #-}
resizeMutableArr :: MArr UnliftedArray s a -> Int -> m (MArr UnliftedArray s a)
resizeMutableArr MArr UnliftedArray s a
marr Int
n = do
MutableUnliftedArray s a
marr' <- Int -> a -> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
n a
forall a. a
uninitialized
MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutableUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState m) a
marr' Int
0 MutableUnliftedArray (PrimState m) a
MArr UnliftedArray s a
marr Int
0 (MutableUnliftedArray s a -> Int
forall s e. MutableUnliftedArray s e -> Int
sizeofMutableUnliftedArray MutableUnliftedArray s a
MArr UnliftedArray s a
marr)
MutableUnliftedArray s a -> m (MutableUnliftedArray s a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutableUnliftedArray s a
marr'
{-# INLINE resizeMutableArr #-}
shrinkMutableArr :: MArr UnliftedArray s a -> Int -> m ()
shrinkMutableArr MArr UnliftedArray s a
_ Int
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE shrinkMutableArr #-}
sameMutableArr :: MArr UnliftedArray s a -> MArr UnliftedArray s a -> Bool
sameMutableArr = MArr UnliftedArray s a -> MArr UnliftedArray s a -> Bool
forall s a.
MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
sameMutableUnliftedArray
{-# INLINE sameMutableArr #-}
sizeofArr :: UnliftedArray a -> Int
sizeofArr = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray
{-# INLINE sizeofArr #-}
sizeofMutableArr :: MArr UnliftedArray s a -> m Int
sizeofMutableArr = Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int)
-> (MutableUnliftedArray s a -> Int)
-> MutableUnliftedArray s a
-> m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutableUnliftedArray s a -> Int
forall s e. MutableUnliftedArray s e -> Int
sizeofMutableUnliftedArray
{-# INLINE sizeofMutableArr #-}
sameArr :: UnliftedArray a -> UnliftedArray a -> Bool
sameArr (UnliftedArray ArrayArray#
arr1#) (UnliftedArray ArrayArray#
arr2#) = Int# -> Bool
isTrue# (
MutableArrayArray# Any -> MutableArrayArray# Any -> Int#
forall d. MutableArrayArray# d -> MutableArrayArray# d -> Int#
sameMutableArrayArray# (ArrayArray# -> MutableArrayArray# Any
unsafeCoerce# ArrayArray#
arr1#) (ArrayArray# -> MutableArrayArray# Any
unsafeCoerce# ArrayArray#
arr2#))
{-# INLINE sameArr #-}
withPrimArrayContents :: PrimArray a -> (Ptr a -> IO b) -> IO b
{-# INLINE withPrimArrayContents #-}
withPrimArrayContents :: PrimArray a -> (Ptr a -> IO b) -> IO b
withPrimArrayContents (PrimArray ByteArray#
ba#) Ptr a -> IO b
f = do
let addr# :: Addr#
addr# = ByteArray# -> Addr#
byteArrayContents# ByteArray#
ba#
ptr :: Ptr a
ptr = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
addr#
b
b <- Ptr a -> IO b
f Ptr a
ptr
(State# (PrimState IO) -> State# (PrimState IO)) -> IO ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (ByteArray# -> State# RealWorld -> State# RealWorld
forall a. a -> State# RealWorld -> State# RealWorld
touch# ByteArray#
ba#)
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
withMutablePrimArrayContents :: MutablePrimArray RealWorld a -> (Ptr a -> IO b) -> IO b
{-# INLINE withMutablePrimArrayContents #-}
withMutablePrimArrayContents :: MutablePrimArray RealWorld a -> (Ptr a -> IO b) -> IO b
withMutablePrimArrayContents (MutablePrimArray MutableByteArray# RealWorld
mba#) Ptr a -> IO b
f = do
let addr# :: Addr#
addr# = ByteArray# -> Addr#
byteArrayContents# (MutableByteArray# RealWorld -> ByteArray#
unsafeCoerce# MutableByteArray# RealWorld
mba#)
ptr :: Ptr a
ptr = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
addr#
b
b <- Ptr a -> IO b
f Ptr a
ptr
(State# (PrimState IO) -> State# (PrimState IO)) -> IO ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableByteArray# RealWorld -> State# RealWorld -> State# RealWorld
forall a. a -> State# RealWorld -> State# RealWorld
touch# MutableByteArray# RealWorld
mba#)
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
castArray :: (Arr arr a, Cast a b) => arr a -> arr b
castArray :: arr a -> arr b
castArray = arr a -> arr b
unsafeCoerce#
castMutableArray :: (Arr arr a, Cast a b) => MArr arr s a -> MArr arr s b
castMutableArray :: MArr arr s a -> MArr arr s b
castMutableArray = MArr arr s a -> MArr arr s b
unsafeCoerce#