{-# language BangPatterns #-}
{-# language FlexibleInstances #-}
{-# language LambdaCase #-}
{-# language MagicHash #-}
{-# language RankNTypes #-}
{-# language ScopedTypeVariables #-}
{-# language TypeFamilies #-}
{-# language TypeFamilyDependencies #-}
{-# language UnboxedTuples #-}
module Data.Primitive.Contiguous
(
size
, sizeMutable
, null
, index
, index#
, read
, indexM
, empty
, new
, singleton
, doubleton
, tripleton
, quadrupleton
, replicate
, replicateMutable
, generate
, generateM
, generateMutable
, iterateN
, iterateMutableN
, write
, run
, replicateMutableM
, generateMutableM
, iterateMutableNM
, create
, createT
, unfoldr
, unfoldrN
, unfoldrMutable
, enumFromN
, enumFromMutableN
, append
, insertAt
, insertSlicing
, replaceAt
, modifyAt
, modifyAt'
, modifyAtF
, modifyAtF'
, reverse
, reverseMutable
, reverseSlice
, resize
, map
, map'
, mapMutable
, mapMutable'
, imap
, imap'
, imapMutable
, imapMutable'
, modify
, modify'
, mapMaybe
, zip
, zipWith
, izipWith
, swap
, filter
, ifilter
, catMaybes
, lefts
, rights
, partitionEithers
, find
, findIndex
, elem
, maximum
, minimum
, maximumBy
, minimumBy
, equals
, equalsMutable
, same
, foldl
, foldl'
, foldr
, foldr'
, foldMap
, foldMap'
, foldlMap'
, ifoldl'
, ifoldr'
, ifoldlMap'
, ifoldlMap1'
, foldlM'
, ifoldlM'
, asum
, all
, any
, foldrZipWith
, ifoldrZipWith
, foldlZipWithM'
, ifoldlZipWithM'
, traverse
, traverse_
, itraverse
, itraverse_
, traverseP
, mapM
, forM
, mapM_
, forM_
, for
, for_
, sequence
, sequence_
, (<$)
, ap
, scanl
, scanl'
, iscanl
, iscanl'
, prescanl
, prescanl'
, iprescanl
, iprescanl'
, mapAccum'
, mapAccumLM'
, fromList
, fromListN
, fromListMutable
, fromListMutableN
, unsafeFromListN
, unsafeFromListReverseN
, unsafeFromListReverseMutableN
, toList
, toListMutable
, convert
, lift
, unlift
, clone
, cloneMutable
, copy
, copyMutable
, freeze
, thaw
, unsafeFreeze
, liftHashWithSalt
, rnf
, Contiguous(Mutable,Element)
, Always
, Array
, MutableArray
, SmallArray
, SmallMutableArray
, PrimArray
, MutablePrimArray
, UnliftedArray
, MutableUnliftedArray
) where
import Control.Monad.Primitive
import Data.Primitive hiding (fromList,fromListN)
import Data.Primitive.Unlifted.Array
import Prelude hiding (map,all,any,foldr,foldMap,traverse,read,filter,replicate,null,reverse,foldl,foldr,zip,zipWith,scanl,(<$),elem,maximum,minimum,mapM,mapM_,sequence,sequence_)
import Control.Applicative (liftA2)
import Control.DeepSeq (NFData)
import Control.Monad (when)
import Control.Monad.ST (runST,ST)
import Control.Monad.ST.Run (runPrimArrayST,runSmallArrayST,runUnliftedArrayST,runArrayST)
import Data.Bits (xor)
import Data.Coerce (coerce)
import Data.Kind (Type)
import Data.Primitive.Unlifted.Class (PrimUnlifted)
import Data.Semigroup (First(..))
import Data.Word (Word8)
import GHC.Base (build)
import GHC.Exts (MutableArrayArray#,ArrayArray#,Constraint,sizeofByteArray#,sizeofArray#,sizeofArrayArray#,unsafeCoerce#,sameMutableArrayArray#,isTrue#,dataToTag#,Int(..))
import qualified Control.Applicative as A
import qualified Control.DeepSeq as DS
import qualified Prelude
class Always a
instance Always a
class Contiguous (arr :: Type -> Type) where
type family Mutable arr = (r :: Type -> Type -> Type) | r -> arr
type family Element arr :: Type -> Constraint
empty :: arr a
null :: arr b -> Bool
new :: (PrimMonad m, Element arr b) => Int -> m (Mutable arr (PrimState m) b)
replicateMutable :: (PrimMonad m, Element arr b) => Int -> b -> m (Mutable arr (PrimState m) b)
index :: Element arr b => arr b -> Int -> b
index# :: Element arr b => arr b -> Int -> (# b #)
indexM :: (Element arr b, Monad m) => arr b -> Int -> m b
read :: (PrimMonad m, Element arr b) => Mutable arr (PrimState m) b -> Int -> m b
write :: (PrimMonad m, Element arr b) => Mutable arr (PrimState m) b -> Int -> b -> m ()
resize :: (PrimMonad m, Element arr b) => Mutable arr (PrimState m) b -> Int -> m (Mutable arr (PrimState m) b)
size :: Element arr b => arr b -> Int
sizeMutable :: (PrimMonad m, Element arr b) => Mutable arr (PrimState m) b -> m Int
unsafeFreeze :: PrimMonad m => Mutable arr (PrimState m) b -> m (arr b)
freeze :: (PrimMonad m, Element arr b)
=> Mutable arr (PrimState m) b
-> Int
-> Int
-> m (arr b)
thaw :: (PrimMonad m, Element arr b)
=> arr b
-> Int
-> Int
-> m (Mutable arr (PrimState m) b)
copy :: (PrimMonad m, Element arr b)
=> Mutable arr (PrimState m) b
-> Int
-> arr b
-> Int
-> Int
-> m ()
copyMutable :: (PrimMonad m, Element arr b)
=> Mutable arr (PrimState m) b
-> Int
-> Mutable arr (PrimState m) b
-> Int
-> Int
-> m ()
clone :: Element arr b
=> arr b
-> Int
-> Int
-> arr b
cloneMutable :: (PrimMonad m, Element arr b)
=> Mutable arr (PrimState m) b
-> Int
-> Int
-> m (Mutable arr (PrimState m) b)
insertSlicing :: Element arr b
=> arr b
-> Int
-> Int
-> Int
-> b
-> arr b
insertSlicing arr b
src Int
off Int
len0 Int
i b
x = (forall s. ST s (arr b)) -> arr b
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr b)) -> arr b)
-> (forall s. ST s (arr b)) -> arr b
forall a b. (a -> b) -> a -> b
$ do
Mutable arr s b
dst <- Int -> b -> ST s (Mutable arr (PrimState (ST s)) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> b -> m (Mutable arr (PrimState m) b)
replicateMutable (Int
len0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
x
Mutable arr (PrimState (ST s)) b
-> Int -> arr b -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy Mutable arr s b
Mutable arr (PrimState (ST s)) b
dst Int
0 arr b
src Int
off Int
i
Mutable arr (PrimState (ST s)) b
-> Int -> arr b -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy Mutable arr s b
Mutable arr (PrimState (ST s)) b
dst (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) arr b
src (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) (Int
len0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i)
Mutable arr (PrimState (ST s)) b -> ST s (arr b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr s b
Mutable arr (PrimState (ST s)) b
dst
{-# inline insertSlicing #-}
equals :: (Element arr b, Eq b) => arr b -> arr b -> Bool
equalsMutable :: Mutable arr s a -> Mutable arr s a -> Bool
unlift :: arr b -> ArrayArray#
lift :: ArrayArray# -> arr b
singleton :: Element arr a => a -> arr a
doubleton :: Element arr a => a -> a -> arr a
tripleton :: Element arr a => a -> a -> a -> arr a
quadrupleton :: Element arr a => a -> a -> a -> a -> arr a
rnf :: (NFData a, Element arr a) => arr a -> ()
run :: (forall s. ST s (arr a)) -> arr a
instance Contiguous SmallArray where
type Mutable SmallArray = SmallMutableArray
type Element SmallArray = Always
empty :: SmallArray a
empty = SmallArray a
forall a. Monoid a => a
mempty
new :: Int -> m (Mutable SmallArray (PrimState m) b)
new Int
n = Int -> b -> m (SmallMutableArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
n b
forall a. a
errorThunk
index :: SmallArray b -> Int -> b
index = SmallArray b -> Int -> b
forall a. SmallArray a -> Int -> a
indexSmallArray
indexM :: SmallArray b -> Int -> m b
indexM = SmallArray b -> Int -> m b
forall (m :: * -> *) a. Monad m => SmallArray a -> Int -> m a
indexSmallArrayM
index# :: SmallArray b -> Int -> (# b #)
index# = SmallArray b -> Int -> (# b #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray##
read :: Mutable SmallArray (PrimState m) b -> Int -> m b
read = Mutable SmallArray (PrimState m) b -> Int -> m b
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray
write :: Mutable SmallArray (PrimState m) b -> Int -> b -> m ()
write = Mutable SmallArray (PrimState m) b -> Int -> b -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray
null :: SmallArray b -> Bool
null SmallArray b
a = case SmallArray b -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray b
a of
Int
0 -> Bool
True
Int
_ -> Bool
False
freeze :: Mutable SmallArray (PrimState m) b
-> Int -> Int -> m (SmallArray b)
freeze = Mutable SmallArray (PrimState m) b
-> Int -> Int -> m (SmallArray b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
freezeSmallArray
size :: SmallArray b -> Int
size = SmallArray b -> Int
forall a. SmallArray a -> Int
sizeofSmallArray
sizeMutable :: Mutable SmallArray (PrimState m) b -> m Int
sizeMutable = (\Mutable SmallArray (PrimState m) b
x -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$! SmallMutableArray (PrimState m) b -> Int
forall s a. SmallMutableArray s a -> Int
sizeofSmallMutableArray SmallMutableArray (PrimState m) b
Mutable SmallArray (PrimState m) b
x)
unsafeFreeze :: Mutable SmallArray (PrimState m) b -> m (SmallArray b)
unsafeFreeze = Mutable SmallArray (PrimState m) b -> m (SmallArray b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray
thaw :: SmallArray b
-> Int -> Int -> m (Mutable SmallArray (PrimState m) b)
thaw = SmallArray b
-> Int -> Int -> m (Mutable SmallArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallArray a -> Int -> Int -> m (SmallMutableArray (PrimState m) a)
thawSmallArray
equals :: SmallArray b -> SmallArray b -> Bool
equals = SmallArray b -> SmallArray b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
equalsMutable :: Mutable SmallArray s a -> Mutable SmallArray s a -> Bool
equalsMutable = Mutable SmallArray s a -> Mutable SmallArray s a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
singleton :: a -> SmallArray a
singleton a
a = (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray s a
marr <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
1 a
forall a. a
errorThunk
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
marr Int
0 a
a
SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
marr
doubleton :: a -> a -> SmallArray a
doubleton a
a a
b = (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray s a
m <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
2 a
forall a. a
errorThunk
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
0 a
a
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
1 a
b
SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m
tripleton :: a -> a -> a -> SmallArray a
tripleton a
a a
b a
c = (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray s a
m <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
3 a
forall a. a
errorThunk
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
0 a
a
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
1 a
b
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
2 a
c
SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m
quadrupleton :: a -> a -> a -> a -> SmallArray a
quadrupleton a
a a
b a
c a
d = (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray s a
m <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
4 a
forall a. a
errorThunk
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
0 a
a
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
1 a
b
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
2 a
c
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m Int
3 a
d
SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
m
rnf :: SmallArray a -> ()
rnf !SmallArray a
ary =
let !sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then
let !(# a
x #) = SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
ix
in a -> ()
forall a. NFData a => a -> ()
DS.rnf a
x () -> () -> ()
`seq` Int -> ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else ()
in Int -> ()
go Int
0
clone :: SmallArray b -> Int -> Int -> SmallArray b
clone = SmallArray b -> Int -> Int -> SmallArray b
forall a. SmallArray a -> Int -> Int -> SmallArray a
cloneSmallArray
cloneMutable :: Mutable SmallArray (PrimState m) b
-> Int -> Int -> m (Mutable SmallArray (PrimState m) b)
cloneMutable = Mutable SmallArray (PrimState m) b
-> Int -> Int -> m (Mutable SmallArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> Int -> m (SmallMutableArray (PrimState m) a)
cloneSmallMutableArray
lift :: ArrayArray# -> SmallArray b
lift ArrayArray#
x = SmallArray# b -> SmallArray b
forall a. SmallArray# a -> SmallArray a
SmallArray (ArrayArray# -> SmallArray# b
unsafeCoerce# ArrayArray#
x)
unlift :: SmallArray b -> ArrayArray#
unlift (SmallArray SmallArray# b
x) = SmallArray# b -> ArrayArray#
unsafeCoerce# SmallArray# b
x
copy :: Mutable SmallArray (PrimState m) b
-> Int -> SmallArray b -> Int -> Int -> m ()
copy = Mutable SmallArray (PrimState m) b
-> Int -> SmallArray b -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray
copyMutable :: Mutable SmallArray (PrimState m) b
-> Int -> Mutable SmallArray (PrimState m) b -> Int -> Int -> m ()
copyMutable = Mutable SmallArray (PrimState m) b
-> Int -> Mutable SmallArray (PrimState m) b -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
copySmallMutableArray
replicateMutable :: Int -> b -> m (Mutable SmallArray (PrimState m) b)
replicateMutable = Int -> b -> m (Mutable SmallArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
replicateSmallMutableArray
resize :: Mutable SmallArray (PrimState m) b
-> Int -> m (Mutable SmallArray (PrimState m) b)
resize = Mutable SmallArray (PrimState m) b
-> Int -> m (Mutable SmallArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> m (SmallMutableArray (PrimState m) a)
resizeSmallArray
run :: (forall s. ST s (SmallArray a)) -> SmallArray a
run = (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s (SmallArray a)) -> SmallArray a
runSmallArrayST
{-# inline empty #-}
{-# inline null #-}
{-# inline new #-}
{-# inline replicateMutable #-}
{-# inline index #-}
{-# inline index# #-}
{-# inline indexM #-}
{-# inline read #-}
{-# inline write #-}
{-# inline resize #-}
{-# inline size #-}
{-# inline sizeMutable #-}
{-# inline unsafeFreeze #-}
{-# inline freeze #-}
{-# inline thaw #-}
{-# inline copy #-}
{-# inline copyMutable #-}
{-# inline clone #-}
{-# inline cloneMutable #-}
{-# inline equals #-}
{-# inline equalsMutable #-}
{-# inline unlift #-}
{-# inline lift #-}
{-# inline singleton #-}
{-# inline doubleton #-}
{-# inline tripleton #-}
{-# inline quadrupleton #-}
{-# inline rnf #-}
{-# inline run #-}
instance Contiguous PrimArray where
type Mutable PrimArray = MutablePrimArray
type Element PrimArray = Prim
empty :: PrimArray a
empty = PrimArray a
forall a. Monoid a => a
mempty
new :: Int -> m (Mutable PrimArray (PrimState m) b)
new = Int -> m (Mutable PrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray
replicateMutable :: Int -> b -> m (Mutable PrimArray (PrimState m) b)
replicateMutable = Int -> b -> m (Mutable PrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> a -> m (MutablePrimArray (PrimState m) a)
replicateMutablePrimArray
index :: PrimArray b -> Int -> b
index = PrimArray b -> Int -> b
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray
index# :: PrimArray b -> Int -> (# b #)
index# PrimArray b
arr Int
ix = (# PrimArray b -> Int -> b
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray b
arr Int
ix #)
indexM :: PrimArray b -> Int -> m b
indexM PrimArray b
arr Int
ix = b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PrimArray b -> Int -> b
forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray PrimArray b
arr Int
ix)
read :: Mutable PrimArray (PrimState m) b -> Int -> m b
read = Mutable PrimArray (PrimState m) b -> Int -> m b
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray
write :: Mutable PrimArray (PrimState m) b -> Int -> b -> m ()
write = Mutable PrimArray (PrimState m) b -> Int -> b -> m ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray
resize :: Mutable PrimArray (PrimState m) b
-> Int -> m (Mutable PrimArray (PrimState m) b)
resize = Mutable PrimArray (PrimState m) b
-> Int -> m (Mutable PrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray
size :: PrimArray b -> Int
size = PrimArray b -> Int
forall a. Prim a => PrimArray a -> Int
sizeofPrimArray
sizeMutable :: Mutable PrimArray (PrimState m) b -> m Int
sizeMutable = Mutable PrimArray (PrimState m) b -> m Int
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> m Int
getSizeofMutablePrimArray
freeze :: Mutable PrimArray (PrimState m) b -> Int -> Int -> m (PrimArray b)
freeze = Mutable PrimArray (PrimState m) b -> Int -> Int -> m (PrimArray b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
freezePrimArrayShim
unsafeFreeze :: Mutable PrimArray (PrimState m) b -> m (PrimArray b)
unsafeFreeze = Mutable PrimArray (PrimState m) b -> m (PrimArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
unsafeFreezePrimArray
thaw :: PrimArray b -> Int -> Int -> m (Mutable PrimArray (PrimState m) b)
thaw = PrimArray b -> Int -> Int -> m (Mutable PrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
PrimArray a -> Int -> Int -> m (MutablePrimArray (PrimState m) a)
thawPrimArray
copy :: Mutable PrimArray (PrimState m) b
-> Int -> PrimArray b -> Int -> Int -> m ()
copy = Mutable PrimArray (PrimState m) b
-> Int -> PrimArray b -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> PrimArray a -> Int -> Int -> m ()
copyPrimArray
copyMutable :: Mutable PrimArray (PrimState m) b
-> Int -> Mutable PrimArray (PrimState m) b -> Int -> Int -> m ()
copyMutable = Mutable PrimArray (PrimState m) b
-> Int -> Mutable PrimArray (PrimState m) b -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
copyMutablePrimArray
clone :: PrimArray b -> Int -> Int -> PrimArray b
clone = PrimArray b -> Int -> Int -> PrimArray b
forall a. Prim a => PrimArray a -> Int -> Int -> PrimArray a
clonePrimArrayShim
cloneMutable :: Mutable PrimArray (PrimState m) b
-> Int -> Int -> m (Mutable PrimArray (PrimState m) b)
cloneMutable = Mutable PrimArray (PrimState m) b
-> Int -> Int -> m (Mutable PrimArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a
-> Int -> Int -> m (MutablePrimArray (PrimState m) a)
cloneMutablePrimArrayShim
equals :: PrimArray b -> PrimArray b -> Bool
equals = PrimArray b -> PrimArray b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
unlift :: PrimArray b -> ArrayArray#
unlift (PrimArray ByteArray#
x) = ByteArray# -> ArrayArray#
unsafeCoerce# ByteArray#
x
lift :: ArrayArray# -> PrimArray b
lift ArrayArray#
x = ByteArray# -> PrimArray b
forall a. ByteArray# -> PrimArray a
PrimArray (ArrayArray# -> ByteArray#
unsafeCoerce# ArrayArray#
x)
null :: PrimArray b -> Bool
null (PrimArray ByteArray#
a) = case ByteArray# -> Int#
sizeofByteArray# ByteArray#
a of
Int#
0# -> Bool
True
Int#
_ -> Bool
False
equalsMutable :: Mutable PrimArray s a -> Mutable PrimArray s a -> Bool
equalsMutable = Mutable PrimArray s a -> Mutable PrimArray s a -> Bool
forall s a. MutablePrimArray s a -> MutablePrimArray s a -> Bool
sameMutablePrimArray
rnf :: PrimArray a -> ()
rnf (PrimArray !ByteArray#
_) = ()
singleton :: a -> PrimArray a
singleton a
a = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((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
1
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
0 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
doubleton :: a -> a -> PrimArray a
doubleton a
a a
b = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((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
m <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
2
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
m Int
0 a
a
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
m Int
1 a
b
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
m
tripleton :: a -> a -> a -> PrimArray a
tripleton a
a a
b a
c = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((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
m <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
3
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
m Int
0 a
a
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
m Int
1 a
b
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
m Int
2 a
c
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
m
quadrupleton :: a -> a -> a -> a -> PrimArray a
quadrupleton a
a a
b a
c a
d = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((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
m <- Int -> ST s (MutablePrimArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
4
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
m Int
0 a
a
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
m Int
1 a
b
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
m Int
2 a
c
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
m Int
3 a
d
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
m
insertSlicing :: PrimArray b -> Int -> Int -> Int -> b -> PrimArray b
insertSlicing PrimArray b
src Int
off Int
len0 Int
i b
x = (forall s. ST s (PrimArray b)) -> PrimArray b
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((forall s. ST s (PrimArray b)) -> PrimArray b)
-> (forall s. ST s (PrimArray b)) -> PrimArray b
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s b
dst <- Int -> ST s (Mutable PrimArray (PrimState (ST s)) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (Int
len0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Mutable PrimArray (PrimState (ST s)) b
-> Int -> PrimArray b -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy MutablePrimArray s b
Mutable PrimArray (PrimState (ST s)) b
dst Int
0 PrimArray b
src Int
off Int
i
Mutable PrimArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write MutablePrimArray s b
Mutable PrimArray (PrimState (ST s)) b
dst Int
i b
x
Mutable PrimArray (PrimState (ST s)) b
-> Int -> PrimArray b -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy MutablePrimArray s b
Mutable PrimArray (PrimState (ST s)) b
dst (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PrimArray b
src (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) (Int
len0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i)
Mutable PrimArray (PrimState (ST s)) b -> ST s (PrimArray b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze MutablePrimArray s b
Mutable PrimArray (PrimState (ST s)) b
dst
run :: (forall s. ST s (PrimArray a)) -> PrimArray a
run = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST
{-# inline empty #-}
{-# inline null #-}
{-# inline new #-}
{-# inline replicateMutable #-}
{-# inline index #-}
{-# inline index# #-}
{-# inline indexM #-}
{-# inline read #-}
{-# inline write #-}
{-# inline resize #-}
{-# inline size #-}
{-# inline sizeMutable #-}
{-# inline unsafeFreeze #-}
{-# inline freeze #-}
{-# inline thaw #-}
{-# inline copy #-}
{-# inline copyMutable #-}
{-# inline clone #-}
{-# inline cloneMutable #-}
{-# inline insertSlicing #-}
{-# inline equals #-}
{-# inline equalsMutable #-}
{-# inline unlift #-}
{-# inline lift #-}
{-# inline singleton #-}
{-# inline doubleton #-}
{-# inline tripleton #-}
{-# inline quadrupleton #-}
{-# inline rnf #-}
{-# inline run #-}
instance Contiguous Array where
type Mutable Array = MutableArray
type Element Array = Always
empty :: Array a
empty = Array a
forall a. Monoid a => a
mempty
new :: Int -> m (Mutable Array (PrimState m) b)
new Int
n = Int -> b -> m (MutableArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
n b
forall a. a
errorThunk
replicateMutable :: Int -> b -> m (Mutable Array (PrimState m) b)
replicateMutable = Int -> b -> m (Mutable Array (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray
index :: Array b -> Int -> b
index = Array b -> Int -> b
forall a. Array a -> Int -> a
indexArray
index# :: Array b -> Int -> (# b #)
index# = Array b -> Int -> (# b #)
forall a. Array a -> Int -> (# a #)
indexArray##
indexM :: Array b -> Int -> m b
indexM = Array b -> Int -> m b
forall (m :: * -> *) a. Monad m => Array a -> Int -> m a
indexArrayM
read :: Mutable Array (PrimState m) b -> Int -> m b
read = Mutable Array (PrimState m) b -> Int -> m b
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> m a
readArray
write :: Mutable Array (PrimState m) b -> Int -> b -> m ()
write = Mutable Array (PrimState m) b -> Int -> b -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray
resize :: Mutable Array (PrimState m) b
-> Int -> m (Mutable Array (PrimState m) b)
resize = Mutable Array (PrimState m) b
-> Int -> m (Mutable Array (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> m (MutableArray (PrimState m) a)
resizeArray
size :: Array b -> Int
size = Array b -> Int
forall a. Array a -> Int
sizeofArray
sizeMutable :: Mutable Array (PrimState m) b -> m Int
sizeMutable = (\Mutable Array (PrimState m) b
x -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$! MutableArray (PrimState m) b -> Int
forall s a. MutableArray s a -> Int
sizeofMutableArray MutableArray (PrimState m) b
Mutable Array (PrimState m) b
x)
freeze :: Mutable Array (PrimState m) b -> Int -> Int -> m (Array b)
freeze = Mutable Array (PrimState m) b -> Int -> Int -> m (Array b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> Int -> m (Array a)
freezeArray
unsafeFreeze :: Mutable Array (PrimState m) b -> m (Array b)
unsafeFreeze = Mutable Array (PrimState m) b -> m (Array b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray
thaw :: Array b -> Int -> Int -> m (Mutable Array (PrimState m) b)
thaw = Array b -> Int -> Int -> m (Mutable Array (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Array a -> Int -> Int -> m (MutableArray (PrimState m) a)
thawArray
copy :: Mutable Array (PrimState m) b
-> Int -> Array b -> Int -> Int -> m ()
copy = Mutable Array (PrimState m) b
-> Int -> Array b -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> Array a -> Int -> Int -> m ()
copyArray
copyMutable :: Mutable Array (PrimState m) b
-> Int -> Mutable Array (PrimState m) b -> Int -> Int -> m ()
copyMutable = Mutable Array (PrimState m) b
-> Int -> Mutable Array (PrimState m) b -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
copyMutableArray
clone :: Array b -> Int -> Int -> Array b
clone = Array b -> Int -> Int -> Array b
forall a. Array a -> Int -> Int -> Array a
cloneArray
cloneMutable :: Mutable Array (PrimState m) b
-> Int -> Int -> m (Mutable Array (PrimState m) b)
cloneMutable = Mutable Array (PrimState m) b
-> Int -> Int -> m (Mutable Array (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a
-> Int -> Int -> m (MutableArray (PrimState m) a)
cloneMutableArray
equals :: Array b -> Array b -> Bool
equals = Array b -> Array b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
unlift :: Array b -> ArrayArray#
unlift (Array Array# b
x) = Array# b -> ArrayArray#
unsafeCoerce# Array# b
x
lift :: ArrayArray# -> Array b
lift ArrayArray#
x = Array# b -> Array b
forall a. Array# a -> Array a
Array (ArrayArray# -> Array# b
unsafeCoerce# ArrayArray#
x)
null :: Array b -> Bool
null (Array Array# b
a) = case Array# b -> Int#
forall a. Array# a -> Int#
sizeofArray# Array# b
a of
Int#
0# -> Bool
True
Int#
_ -> Bool
False
equalsMutable :: Mutable Array s a -> Mutable Array s a -> Bool
equalsMutable = Mutable Array s a -> Mutable Array s a -> Bool
forall s a. MutableArray s a -> MutableArray s a -> Bool
sameMutableArray
rnf :: Array a -> ()
rnf !Array a
ary =
let !sz :: Int
sz = Array a -> Int
forall a. Array a -> Int
sizeofArray Array a
ary
go :: Int -> ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = ()
| Bool
otherwise =
let !(# a
x #) = Array a -> Int -> (# a #)
forall a. Array a -> Int -> (# a #)
indexArray## Array a
ary Int
i
in a -> ()
forall a. NFData a => a -> ()
DS.rnf a
x () -> () -> ()
`seq` Int -> ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Int -> ()
go Int
0
singleton :: a -> Array a
singleton a
a = (forall s. ST s (Array a)) -> Array a
forall a. (forall s. ST s (Array a)) -> Array a
runArrayST (Int -> a -> ST s (MutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
1 a
a ST s (MutableArray s a)
-> (MutableArray s a -> ST s (Array a)) -> ST s (Array a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MutableArray s a -> ST s (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray)
doubleton :: a -> a -> Array a
doubleton a
a a
b = (forall s. ST s (Array a)) -> Array a
forall a. (forall s. ST s (Array a)) -> Array a
runArrayST ((forall s. ST s (Array a)) -> Array a)
-> (forall s. ST s (Array a)) -> Array a
forall a b. (a -> b) -> a -> b
$ do
MutableArray s a
m <- Int -> a -> ST s (MutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
2 a
a
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
1 a
b
MutableArray (PrimState (ST s)) a -> ST s (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m
tripleton :: a -> a -> a -> Array a
tripleton a
a a
b a
c = (forall s. ST s (Array a)) -> Array a
forall a. (forall s. ST s (Array a)) -> Array a
runArrayST ((forall s. ST s (Array a)) -> Array a)
-> (forall s. ST s (Array a)) -> Array a
forall a b. (a -> b) -> a -> b
$ do
MutableArray s a
m <- Int -> a -> ST s (MutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
3 a
a
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
1 a
b
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
2 a
c
MutableArray (PrimState (ST s)) a -> ST s (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m
quadrupleton :: a -> a -> a -> a -> Array a
quadrupleton a
a a
b a
c a
d = (forall s. ST s (Array a)) -> Array a
forall a. (forall s. ST s (Array a)) -> Array a
runArrayST ((forall s. ST s (Array a)) -> Array a)
-> (forall s. ST s (Array a)) -> Array a
forall a b. (a -> b) -> a -> b
$ do
MutableArray s a
m <- Int -> a -> ST s (MutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
4 a
a
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
1 a
b
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
2 a
c
MutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Int -> a -> m ()
writeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m Int
3 a
d
MutableArray (PrimState (ST s)) a -> ST s (Array a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
unsafeFreezeArray MutableArray s a
MutableArray (PrimState (ST s)) a
m
run :: (forall s. ST s (Array a)) -> Array a
run = (forall s. ST s (Array a)) -> Array a
forall a. (forall s. ST s (Array a)) -> Array a
runArrayST
{-# inline empty #-}
{-# inline null #-}
{-# inline new #-}
{-# inline replicateMutable #-}
{-# inline index #-}
{-# inline index# #-}
{-# inline indexM #-}
{-# inline read #-}
{-# inline write #-}
{-# inline resize #-}
{-# inline size #-}
{-# inline sizeMutable #-}
{-# inline unsafeFreeze #-}
{-# inline freeze #-}
{-# inline thaw #-}
{-# inline copy #-}
{-# inline copyMutable #-}
{-# inline clone #-}
{-# inline cloneMutable #-}
{-# inline equals #-}
{-# inline equalsMutable #-}
{-# inline unlift #-}
{-# inline lift #-}
{-# inline singleton #-}
{-# inline doubleton #-}
{-# inline tripleton #-}
{-# inline quadrupleton #-}
{-# inline rnf #-}
{-# inline run #-}
instance Contiguous UnliftedArray where
type Mutable UnliftedArray = MutableUnliftedArray
type Element UnliftedArray = PrimUnlifted
empty :: UnliftedArray a
empty = UnliftedArray a
forall a. UnliftedArray a
emptyUnliftedArray
new :: Int -> m (Mutable UnliftedArray (PrimState m) b)
new = Int -> m (Mutable UnliftedArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray
replicateMutable :: Int -> b -> m (Mutable UnliftedArray (PrimState m) b)
replicateMutable = Int -> b -> m (Mutable UnliftedArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray
index :: UnliftedArray b -> Int -> b
index = UnliftedArray b -> Int -> b
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray
index# :: UnliftedArray b -> Int -> (# b #)
index# UnliftedArray b
arr Int
ix = (# UnliftedArray b -> Int -> b
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray b
arr Int
ix #)
indexM :: UnliftedArray b -> Int -> m b
indexM UnliftedArray b
arr Int
ix = b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UnliftedArray b -> Int -> b
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray b
arr Int
ix)
read :: Mutable UnliftedArray (PrimState m) b -> Int -> m b
read = Mutable UnliftedArray (PrimState m) b -> Int -> m b
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> m a
readUnliftedArray
write :: Mutable UnliftedArray (PrimState m) b -> Int -> b -> m ()
write = Mutable UnliftedArray (PrimState m) b -> Int -> b -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray
resize :: Mutable UnliftedArray (PrimState m) b
-> Int -> m (Mutable UnliftedArray (PrimState m) b)
resize = Mutable UnliftedArray (PrimState m) b
-> Int -> m (Mutable UnliftedArray (PrimState m) b)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a
-> Int -> m (MutableUnliftedArray (PrimState m) a)
resizeUnliftedArray
size :: UnliftedArray b -> Int
size = UnliftedArray b -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray
sizeMutable :: Mutable UnliftedArray (PrimState m) b -> m Int
sizeMutable = Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int)
-> (MutableUnliftedArray (PrimState m) b -> Int)
-> MutableUnliftedArray (PrimState m) b
-> m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutableUnliftedArray (PrimState m) b -> Int
forall s e. MutableUnliftedArray s e -> Int
sizeofMutableUnliftedArray
freeze :: Mutable UnliftedArray (PrimState m) b
-> Int -> Int -> m (UnliftedArray b)
freeze = Mutable UnliftedArray (PrimState m) b
-> Int -> Int -> m (UnliftedArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (UnliftedArray a)
freezeUnliftedArray
unsafeFreeze :: Mutable UnliftedArray (PrimState m) b -> m (UnliftedArray b)
unsafeFreeze = Mutable UnliftedArray (PrimState m) b -> m (UnliftedArray b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray
thaw :: UnliftedArray b
-> Int -> Int -> m (Mutable UnliftedArray (PrimState m) b)
thaw = UnliftedArray b
-> Int -> Int -> m (Mutable UnliftedArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
UnliftedArray a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
thawUnliftedArray
copy :: Mutable UnliftedArray (PrimState m) b
-> Int -> UnliftedArray b -> Int -> Int -> m ()
copy = Mutable UnliftedArray (PrimState m) b
-> Int -> UnliftedArray b -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray
copyMutable :: Mutable UnliftedArray (PrimState m) b
-> Int
-> Mutable UnliftedArray (PrimState m) b
-> Int
-> Int
-> m ()
copyMutable = Mutable UnliftedArray (PrimState m) b
-> Int
-> Mutable UnliftedArray (PrimState m) b
-> Int
-> Int
-> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutableUnliftedArray
clone :: UnliftedArray b -> Int -> Int -> UnliftedArray b
clone = UnliftedArray b -> Int -> Int -> UnliftedArray b
forall a. UnliftedArray a -> Int -> Int -> UnliftedArray a
cloneUnliftedArray
cloneMutable :: Mutable UnliftedArray (PrimState m) b
-> Int -> Int -> m (Mutable UnliftedArray (PrimState m) b)
cloneMutable = Mutable UnliftedArray (PrimState m) b
-> Int -> Int -> m (Mutable UnliftedArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
cloneMutableUnliftedArray
equals :: UnliftedArray b -> UnliftedArray b -> Bool
equals = UnliftedArray b -> UnliftedArray b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
unlift :: UnliftedArray b -> ArrayArray#
unlift (UnliftedArray ArrayArray#
x) = ArrayArray#
x
lift :: ArrayArray# -> UnliftedArray b
lift ArrayArray#
x = ArrayArray# -> UnliftedArray b
forall b. ArrayArray# -> UnliftedArray b
UnliftedArray ArrayArray#
x
null :: UnliftedArray b -> Bool
null (UnliftedArray ArrayArray#
a) = case ArrayArray# -> Int#
sizeofArrayArray# ArrayArray#
a of
Int#
0# -> Bool
True
Int#
_ -> Bool
False
equalsMutable :: Mutable UnliftedArray s a -> Mutable UnliftedArray s a -> Bool
equalsMutable = Mutable UnliftedArray s a -> Mutable UnliftedArray s a -> Bool
forall s a.
MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
sameMutableUnliftedArray
rnf :: UnliftedArray a -> ()
rnf !UnliftedArray a
ary =
let !sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
ary
go :: Int -> ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = ()
| Bool
otherwise =
let x :: a
x = UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
ary Int
i
in a -> ()
forall a. NFData a => a -> ()
DS.rnf a
x () -> () -> ()
`seq` Int -> ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Int -> ()
go Int
0
singleton :: a -> UnliftedArray a
singleton a
a = (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST (Int -> a -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
1 a
a ST s (MutableUnliftedArray s a)
-> (MutableUnliftedArray s a -> ST s (UnliftedArray a))
-> ST s (UnliftedArray a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MutableUnliftedArray s a -> ST s (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray)
doubleton :: a -> a -> UnliftedArray a
doubleton a
a a
b = (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST ((forall s. ST s (UnliftedArray a)) -> UnliftedArray a)
-> (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ do
MutableUnliftedArray s a
m <- Int -> a -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
2 a
a
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
1 a
b
MutableUnliftedArray (PrimState (ST s)) a -> ST s (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m
tripleton :: a -> a -> a -> UnliftedArray a
tripleton a
a a
b a
c = (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST ((forall s. ST s (UnliftedArray a)) -> UnliftedArray a)
-> (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ do
MutableUnliftedArray s a
m <- Int -> a -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
3 a
a
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
1 a
b
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
2 a
c
MutableUnliftedArray (PrimState (ST s)) a -> ST s (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m
quadrupleton :: a -> a -> a -> a -> UnliftedArray a
quadrupleton a
a a
b a
c a
d = (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST ((forall s. ST s (UnliftedArray a)) -> UnliftedArray a)
-> (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ do
MutableUnliftedArray s a
m <- Int -> a -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
4 a
a
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
1 a
b
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
2 a
c
MutableUnliftedArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
3 a
d
MutableUnliftedArray (PrimState (ST s)) a -> ST s (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m
run :: (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
run = (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
forall a. (forall s. ST s (UnliftedArray a)) -> UnliftedArray a
runUnliftedArrayST
{-# inline empty #-}
{-# inline null #-}
{-# inline new #-}
{-# inline replicateMutable #-}
{-# inline index #-}
{-# inline index# #-}
{-# inline indexM #-}
{-# inline read #-}
{-# inline write #-}
{-# inline resize #-}
{-# inline size #-}
{-# inline sizeMutable #-}
{-# inline unsafeFreeze #-}
{-# inline freeze #-}
{-# inline thaw #-}
{-# inline copy #-}
{-# inline copyMutable #-}
{-# inline clone #-}
{-# inline cloneMutable #-}
{-# inline equals #-}
{-# inline equalsMutable #-}
{-# inline unlift #-}
{-# inline lift #-}
{-# inline singleton #-}
{-# inline doubleton #-}
{-# inline tripleton #-}
{-# inline quadrupleton #-}
{-# inline rnf #-}
{-# inline run #-}
errorThunk :: a
errorThunk :: a
errorThunk = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Contiguous typeclass: unitialized element"
{-# noinline errorThunk #-}
freezePrimArrayShim :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
freezePrimArrayShim :: MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
freezePrimArrayShim !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
{-# inline freezePrimArrayShim #-}
resizeArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> m (MutableArray (PrimState m) a)
resizeArray :: MutableArray (PrimState m) a
-> Int -> m (MutableArray (PrimState m) a)
resizeArray !MutableArray (PrimState m) a
src !Int
sz = do
MutableArray (PrimState m) a
dst <- Int -> a -> m (MutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MutableArray (PrimState m) a)
newArray Int
sz a
forall a. a
errorThunk
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
dst Int
0 MutableArray (PrimState m) a
src Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
sz (MutableArray (PrimState m) a -> Int
forall s a. MutableArray s a -> Int
sizeofMutableArray MutableArray (PrimState m) a
src))
MutableArray (PrimState m) a -> m (MutableArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutableArray (PrimState m) a
dst
{-# inline resizeArray #-}
resizeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> m (SmallMutableArray (PrimState m) a)
resizeSmallArray :: SmallMutableArray (PrimState m) a
-> Int -> m (SmallMutableArray (PrimState m) a)
resizeSmallArray !SmallMutableArray (PrimState m) a
src !Int
sz = do
SmallMutableArray (PrimState m) a
dst <- Int -> a -> m (SmallMutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
sz a
forall a. a
errorThunk
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
dst Int
0 SmallMutableArray (PrimState m) a
src Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
sz (SmallMutableArray (PrimState m) a -> Int
forall s a. SmallMutableArray s a -> Int
sizeofSmallMutableArray SmallMutableArray (PrimState m) a
src))
SmallMutableArray (PrimState m) a
-> m (SmallMutableArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure SmallMutableArray (PrimState m) a
dst
{-# inline resizeSmallArray #-}
resizeUnliftedArray :: (PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> Int -> m (MutableUnliftedArray (PrimState m) a)
resizeUnliftedArray :: MutableUnliftedArray (PrimState m) a
-> Int -> m (MutableUnliftedArray (PrimState m) a)
resizeUnliftedArray !MutableUnliftedArray (PrimState m) a
src !Int
sz = do
MutableUnliftedArray (PrimState m) a
dst <- Int -> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray Int
sz
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
dst Int
0 MutableUnliftedArray (PrimState m) a
src Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
sz (MutableUnliftedArray (PrimState m) a -> Int
forall s e. MutableUnliftedArray s e -> Int
sizeofMutableUnliftedArray MutableUnliftedArray (PrimState m) a
src))
MutableUnliftedArray (PrimState m) a
-> m (MutableUnliftedArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutableUnliftedArray (PrimState m) a
dst
{-# inline resizeUnliftedArray #-}
append :: (Contiguous arr, Element arr a) => arr a -> arr a -> arr a
append :: arr a -> arr a -> arr a
append !arr a
a !arr a
b = (forall s. ST s (arr a)) -> arr a
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr a)) -> arr a)
-> (forall s. ST s (arr a)) -> arr a
forall a b. (a -> b) -> a -> b
$ do
let !szA :: Int
szA = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
a
let !szB :: Int
szB = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
b
Mutable arr s a
m <- Int -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (Int
szA Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
szB)
Mutable arr (PrimState (ST s)) a
-> Int -> arr a -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy Mutable arr s a
Mutable arr (PrimState (ST s)) a
m Int
0 arr a
a Int
0 Int
szA
Mutable arr (PrimState (ST s)) a
-> Int -> arr a -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy Mutable arr s a
Mutable arr (PrimState (ST s)) a
m Int
szA arr a
b Int
0 Int
szB
Mutable arr (PrimState (ST s)) a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr s a
Mutable arr (PrimState (ST s)) a
m
{-# inline append #-}
insertAt :: (Contiguous arr, Element arr a) => arr a -> Int -> a -> arr a
insertAt :: arr a -> Int -> a -> arr a
insertAt arr a
src Int
i a
x = arr a -> Int -> Int -> Int -> a -> arr a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> Int -> Int -> b -> arr b
insertSlicing arr a
src Int
0 (arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
src) Int
i a
x
replaceAt :: (Contiguous arr, Element arr a) => arr a -> Int -> a -> arr a
replaceAt :: arr a -> Int -> a -> arr a
replaceAt arr a
src Int
i a
x = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr s a)) -> arr a)
-> (forall s. ST s (Mutable arr s a)) -> arr a
forall a b. (a -> b) -> a -> b
$ do
Mutable arr s a
dst <- arr a -> Int -> Int -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
arr b -> Int -> Int -> m (Mutable arr (PrimState m) b)
thaw arr a
src Int
0 (arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
src)
Mutable arr (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr s a
Mutable arr (PrimState (ST s)) a
dst Int
i a
x
Mutable arr s a -> ST s (Mutable arr s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr s a
dst
{-# inline replaceAt #-}
modifyAt :: (Contiguous arr, Element arr a)
=> (a -> a) -> arr a -> Int -> arr a
modifyAt :: (a -> a) -> arr a -> Int -> arr a
modifyAt a -> a
f arr a
src Int
i = arr a -> Int -> a -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
arr a -> Int -> a -> arr a
replaceAt arr a
src Int
i (a -> arr a) -> a -> arr a
forall a b. (a -> b) -> a -> b
$ a -> a
f (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
src Int
i)
{-# inline modifyAt #-}
modifyAt' :: (Contiguous arr, Element arr a)
=> (a -> a) -> arr a -> Int -> arr a
modifyAt' :: (a -> a) -> arr a -> Int -> arr a
modifyAt' a -> a
f arr a
src Int
i = arr a -> Int -> a -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
arr a -> Int -> a -> arr a
replaceAt arr a
src Int
i (a -> arr a) -> a -> arr a
forall a b. (a -> b) -> a -> b
$! a -> a
f (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
src Int
i)
{-# inline modifyAt' #-}
modifyAtF :: (Contiguous arr, Element arr a, Functor f)
=> (a -> f a) -> arr a -> Int -> f (arr a)
modifyAtF :: (a -> f a) -> arr a -> Int -> f (arr a)
modifyAtF a -> f a
f arr a
src Int
i = arr a -> Int -> a -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
arr a -> Int -> a -> arr a
replaceAt arr a
src Int
i (a -> arr a) -> f a -> f (arr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
src Int
i)
{-# inline modifyAtF #-}
modifyAtF' :: (Contiguous arr, Element arr a, Monad f)
=> (a -> f a) -> arr a -> Int -> f (arr a)
modifyAtF' :: (a -> f a) -> arr a -> Int -> f (arr a)
modifyAtF' a -> f a
f arr a
src Int
i = do
!a
r <- a -> f a
f (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
src Int
i)
let !dst :: arr a
dst = arr a -> Int -> a -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
arr a -> Int -> a -> arr a
replaceAt arr a
src Int
i a
r
arr a -> f (arr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure arr a
dst
{-# inline modifyAtF' #-}
imap :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c) => (Int -> b -> c) -> arr1 b -> arr2 c
imap :: (Int -> b -> c) -> arr1 b -> arr2 c
imap Int -> b -> c
f arr1 b
a = (forall s. ST s (arr2 c)) -> arr2 c
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr2 c)) -> arr2 c)
-> (forall s. ST s (arr2 c)) -> arr2 c
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 s c
mb <- Int -> ST s (Mutable arr2 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a)
let go :: Int -> ST s ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
b
x <- arr1 b -> Int -> ST s b
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 b
a Int
i
Mutable arr2 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb Int
i (Int -> b -> c
f Int
i b
x)
Int -> ST s ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Int -> ST s ()
go Int
0
Mutable arr2 (PrimState (ST s)) c -> ST s (arr2 c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb
{-# inline imap #-}
imap' :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c) => (Int -> b -> c) -> arr1 b -> arr2 c
imap' :: (Int -> b -> c) -> arr1 b -> arr2 c
imap' Int -> b -> c
f arr1 b
a = (forall s. ST s (arr2 c)) -> arr2 c
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr2 c)) -> arr2 c)
-> (forall s. ST s (arr2 c)) -> arr2 c
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 s c
mb <- Int -> ST s (Mutable arr2 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a)
let go :: Int -> ST s ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
b
x <- arr1 b -> Int -> ST s b
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 b
a Int
i
let !b :: c
b = Int -> b -> c
f Int
i b
x
Mutable arr2 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb Int
i c
b
Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> ST s ()
go Int
0
Mutable arr2 (PrimState (ST s)) c -> ST s (arr2 c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb
{-# inline imap' #-}
map :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c) => (b -> c) -> arr1 b -> arr2 c
map :: (b -> c) -> arr1 b -> arr2 c
map b -> c
f arr1 b
a = (forall s. ST s (arr2 c)) -> arr2 c
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr2 c)) -> arr2 c)
-> (forall s. ST s (arr2 c)) -> arr2 c
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 s c
mb <- Int -> ST s (Mutable arr2 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a)
let go :: Int -> ST s ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
b
x <- arr1 b -> Int -> ST s b
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 b
a Int
i
Mutable arr2 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb Int
i (b -> c
f b
x)
Int -> ST s ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Int -> ST s ()
go Int
0
Mutable arr2 (PrimState (ST s)) c -> ST s (arr2 c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb
{-# inline map #-}
map' :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c) => (b -> c) -> arr1 b -> arr2 c
map' :: (b -> c) -> arr1 b -> arr2 c
map' b -> c
f arr1 b
a = (forall s. ST s (arr2 c)) -> arr2 c
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr2 c)) -> arr2 c)
-> (forall s. ST s (arr2 c)) -> arr2 c
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 s c
mb <- Int -> ST s (Mutable arr2 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a)
let go :: Int -> ST s ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
a = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
b
x <- arr1 b -> Int -> ST s b
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 b
a Int
i
let !b :: c
b = b -> c
f b
x
Mutable arr2 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb Int
i c
b
Int -> ST s ()
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Int -> ST s ()
go Int
0
Mutable arr2 (PrimState (ST s)) c -> ST s (arr2 c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
mb
{-# inline map' #-}
convert :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 b) => arr1 b -> arr2 b
convert :: arr1 b -> arr2 b
convert arr1 b
a = (b -> b) -> arr1 b -> arr2 b
forall (arr1 :: * -> *) b (arr2 :: * -> *) c.
(Contiguous arr1, Element arr1 b, Contiguous arr2,
Element arr2 c) =>
(b -> c) -> arr1 b -> arr2 c
map b -> b
forall a. a -> a
id arr1 b
a
{-# inline convert #-}
foldr :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b
{-# inline foldr #-}
foldr :: (a -> b -> b) -> b -> arr a -> b
foldr a -> b -> b
f b
z = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b
go !Int
ix = if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ix
then case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> a -> b -> b
f a
x (Int -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
else b
z
in Int -> b
go Int
0
foldr' :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b
foldr' :: (a -> b -> b) -> b -> arr a -> b
foldr' a -> b -> b
f !b
z = \arr a
arr ->
let go :: Int -> b -> b
go !Int
ix !b
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== -Int
1
then b
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> b -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (a -> b -> b
f a
x b
acc)
in Int -> b -> b
go (arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) b
z
{-# inline foldr' #-}
foldl :: (Contiguous arr, Element arr a) => (b -> a -> b) -> b -> arr a -> b
foldl :: (b -> a -> b) -> b -> arr a -> b
foldl b -> a -> b
f b
z = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> b
go !Int
ix b
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then b
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> b -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> a -> b
f b
acc a
x)
in Int -> b -> b
go Int
0 b
z
{-# inline foldl #-}
foldl' :: (Contiguous arr, Element arr a) => (b -> a -> b) -> b -> arr a -> b
foldl' :: (b -> a -> b) -> b -> arr a -> b
foldl' b -> a -> b
f !b
z = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> b
go !Int
ix !b
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then b
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> b -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> a -> b
f b
acc a
x)
in Int -> b -> b
go Int
0 b
z
{-# inline foldl' #-}
ifoldl' :: (Contiguous arr, Element arr a) => (b -> Int -> a -> b) -> b -> arr a -> b
ifoldl' :: (b -> Int -> a -> b) -> b -> arr a -> b
ifoldl' b -> Int -> a -> b
f !b
z = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> b
go !Int
ix !b
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then b
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> b -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> Int -> a -> b
f b
acc Int
ix a
x)
in Int -> b -> b
go Int
0 b
z
{-# inline ifoldl' #-}
ifoldr' :: (Contiguous arr, Element arr a) => (Int -> a -> b -> b) -> b -> arr a -> b
ifoldr' :: (Int -> a -> b -> b) -> b -> arr a -> b
ifoldr' Int -> a -> b -> b
f !b
z = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> b
go !Int
ix !b
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (-Int
1)
then b
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> b -> b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> a -> b -> b
f Int
ix a
x b
acc)
in Int -> b -> b
go (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) b
z
{-# inline ifoldr' #-}
foldMap :: (Contiguous arr, Element arr a, Monoid m) => (a -> m) -> arr a -> m
foldMap :: (a -> m) -> arr a -> m
foldMap a -> m
f = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> m
go !Int
ix = if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ix
then case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> m -> m -> m
forall a. Monoid a => a -> a -> a
mappend (a -> m
f a
x) (Int -> m
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
else m
forall a. Monoid a => a
mempty
in Int -> m
go Int
0
{-# inline foldMap #-}
foldMap' :: (Contiguous arr, Element arr a, Monoid m)
=> (a -> m) -> arr a -> m
foldMap' :: (a -> m) -> arr a -> m
foldMap' a -> m
f = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> m -> m
go !Int
ix !m
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then m
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix
of (# a
x #) -> Int -> m -> m
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
acc (a -> m
f a
x))
in Int -> m -> m
go Int
0 m
forall a. Monoid a => a
mempty
{-# inline foldMap' #-}
foldlMap' :: (Contiguous arr, Element arr a, Monoid m)
=> (a -> m) -> arr a -> m
foldlMap' :: (a -> m) -> arr a -> m
foldlMap' = (a -> m) -> arr a -> m
forall (arr :: * -> *) a m.
(Contiguous arr, Element arr a, Monoid m) =>
(a -> m) -> arr a -> m
foldMap'
{-# inline foldlMap' #-}
ifoldlMap' :: (Contiguous arr, Element arr a, Monoid m)
=> (Int -> a -> m)
-> arr a
-> m
ifoldlMap' :: (Int -> a -> m) -> arr a -> m
ifoldlMap' Int -> a -> m
f = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> m -> m
go !Int
ix !m
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then m
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> m -> m
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
acc (Int -> a -> m
f Int
ix a
x))
in Int -> m -> m
go Int
0 m
forall a. Monoid a => a
mempty
{-# inline ifoldlMap' #-}
ifoldlMap1' :: (Contiguous arr, Element arr a, Semigroup m)
=> (Int -> a -> m)
-> arr a
-> m
ifoldlMap1' :: (Int -> a -> m) -> arr a -> m
ifoldlMap1' Int -> a -> m
f = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> m -> m
go !Int
ix !m
acc = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then m
acc
else case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
(# a
x #) -> Int -> m -> m
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (m
acc m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Int -> a -> m
f Int
ix a
x)
!(# a
e0 #) = arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
0
in Int -> m -> m
go Int
1 (Int -> a -> m
f Int
0 a
e0)
{-# inline ifoldlMap1' #-}
foldlM' :: (Contiguous arr, Element arr a, Monad m) => (b -> a -> m b) -> b -> arr a -> m b
foldlM' :: (b -> a -> m b) -> b -> arr a -> m b
foldlM' b -> a -> m b
f b
z0 = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> m b
go !Int
ix !b
acc1 = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let (# a
x #) = arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix
b
acc2 <- b -> a -> m b
f b
acc1 a
x
Int -> b -> m b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
acc2
else b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
acc1
in Int -> b -> m b
go Int
0 b
z0
{-# inline foldlM' #-}
ifoldlM' :: (Contiguous arr, Element arr a, Monad m) => (b -> Int -> a -> m b) -> b -> arr a -> m b
ifoldlM' :: (b -> Int -> a -> m b) -> b -> arr a -> m b
ifoldlM' b -> Int -> a -> m b
f b
z0 = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> b -> m b
go !Int
ix !b
acc1 = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let (# a
x #) = arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix
b
acc2 <- b -> Int -> a -> m b
f b
acc1 Int
ix a
x
Int -> b -> m b
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
acc2
else b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
acc1
in Int -> b -> m b
go Int
0 b
z0
{-# inline ifoldlM' #-}
filter :: (Contiguous arr, Element arr a)
=> (a -> Bool)
-> arr a
-> arr a
filter :: (a -> Bool) -> arr a -> arr a
filter a -> Bool
p arr a
arr = (Int -> a -> Bool) -> arr a -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(Int -> a -> Bool) -> arr a -> arr a
ifilter ((a -> Bool) -> Int -> a -> Bool
forall a b. a -> b -> a
const a -> Bool
p) arr a
arr
{-# inline filter #-}
ifilter :: (Contiguous arr, Element arr a)
=> (Int -> a -> Bool)
-> arr a
-> arr a
ifilter :: (Int -> a -> Bool) -> arr a -> arr a
ifilter Int -> a -> Bool
p arr a
arr = (forall s. ST s (arr a)) -> arr a
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr a)) -> arr a)
-> (forall s. ST s (arr a)) -> arr a
forall a b. (a -> b) -> a -> b
$ do
MutablePrimArray s Word8
marr :: MutablePrimArray s Word8 <- Int -> ST s (MutablePrimArray (PrimState (ST s)) Word8)
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
sz
let go1 :: Int -> Int -> ST s Int
go1 :: Int -> Int -> ST s Int
go1 !Int
ix !Int
numTrue = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
a
atIx <- arr a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr a
arr Int
ix
let !keep :: Bool
keep = Int -> a -> Bool
p Int
ix a
atIx
let !keepTag :: Int
keepTag = Int# -> Int
I# (Bool -> Int#
forall a. a -> Int#
dataToTag# Bool
keep)
MutablePrimArray (PrimState (ST s)) Word8
-> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s Word8
MutablePrimArray (PrimState (ST s)) Word8
marr Int
ix (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
keepTag)
Int -> Int -> ST s Int
go1 (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
numTrue Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
keepTag)
else Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
numTrue
Int
numTrue <- Int -> Int -> ST s Int
go1 Int
0 Int
0
if Int
numTrue Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then arr a -> ST s (arr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure arr a
arr
else do
Mutable arr s a
marrTrues <- Int -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
numTrue
let go2 :: Int -> Int -> ST s ()
go2 !Int
ixSrc !Int
ixDst = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ixDst Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
numTrue) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
Word8
atIxKeep <- MutablePrimArray (PrimState (ST s)) Word8 -> Int -> ST s Word8
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray MutablePrimArray s Word8
MutablePrimArray (PrimState (ST s)) Word8
marr Int
ixSrc
if Word8 -> Bool
isTrue Word8
atIxKeep
then do
a
atIxVal <- arr a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr a
arr Int
ixSrc
Mutable arr (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr s a
Mutable arr (PrimState (ST s)) a
marrTrues Int
ixDst a
atIxVal
Int -> Int -> ST s ()
go2 (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 ()
go2 (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
ixDst
Int -> Int -> ST s ()
go2 Int
0 Int
0
Mutable arr (PrimState (ST s)) a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr s a
Mutable arr (PrimState (ST s)) a
marrTrues
where
!sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
{-# inline ifilter #-}
mapMaybe :: forall arr1 arr2 a b. (Contiguous arr1, Element arr1 a, Contiguous arr2, Element arr2 b)
=> (a -> Maybe b)
-> arr1 a
-> arr2 b
mapMaybe :: (a -> Maybe b) -> arr1 a -> arr2 b
mapMaybe a -> Maybe b
f arr1 a
arr = (forall s. ST s (arr2 b)) -> arr2 b
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr2 b)) -> arr2 b)
-> (forall s. ST s (arr2 b)) -> arr2 b
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr
let go :: Int -> Int -> [b] -> ST s ([b],Int)
go :: Int -> Int -> [b] -> ST s ([b], Int)
go !Int
ix !Int
numJusts ![b]
justs = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
a
atIx <- arr1 a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
arr Int
ix
case a -> Maybe b
f a
atIx of
Maybe b
Nothing -> Int -> Int -> [b] -> ST s ([b], Int)
forall s. Int -> Int -> [b] -> ST s ([b], Int)
go (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
numJusts [b]
justs
Just b
x -> Int -> Int -> [b] -> ST s ([b], Int)
forall s. Int -> Int -> [b] -> ST s ([b], Int)
go (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
numJustsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (b
xb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
justs)
else ([b], Int) -> ST s ([b], Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([b]
justs,Int
numJusts)
!([b]
bs,!Int
numJusts) <- Int -> Int -> [b] -> ST s ([b], Int)
forall s. Int -> Int -> [b] -> ST s ([b], Int)
go Int
0 Int
0 []
!Mutable arr2 s b
marr <- Int -> [b] -> ST s (Mutable arr2 (PrimState (ST s)) b)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
numJusts [b]
bs
Mutable arr2 (PrimState (ST s)) b -> ST s (arr2 b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s b
Mutable arr2 (PrimState (ST s)) b
marr
{-# inline mapMaybe #-}
{-# inline isTrue #-}
isTrue :: Word8 -> Bool
isTrue :: Word8 -> Bool
isTrue Word8
0 = Bool
False
isTrue Word8
_ = Bool
True
catMaybes :: (Contiguous arr, Element arr a, Element arr (Maybe a))
=> arr (Maybe a)
-> arr a
catMaybes :: arr (Maybe a) -> arr a
catMaybes = (Maybe a -> Maybe a) -> arr (Maybe a) -> arr a
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Element arr1 a, Contiguous arr2,
Element arr2 b) =>
(a -> Maybe b) -> arr1 a -> arr2 b
mapMaybe Maybe a -> Maybe a
forall a. a -> a
id
{-# inline catMaybes #-}
clonePrimArrayShim :: Prim a => PrimArray a -> Int -> Int -> PrimArray a
clonePrimArrayShim :: PrimArray a -> Int -> Int -> PrimArray a
clonePrimArrayShim !PrimArray a
arr !Int
off !Int
len = (forall s. ST s (PrimArray a)) -> PrimArray a
forall a. (forall s. ST s (PrimArray a)) -> PrimArray a
runPrimArrayST ((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 -> 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
marr Int
0 PrimArray a
arr Int
off Int
len
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 clonePrimArrayShim #-}
cloneMutablePrimArrayShim :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> Int -> m (MutablePrimArray (PrimState m) a)
cloneMutablePrimArrayShim :: MutablePrimArray (PrimState m) a
-> Int -> Int -> m (MutablePrimArray (PrimState m) a)
cloneMutablePrimArrayShim !MutablePrimArray (PrimState m) a
arr !Int
off !Int
len = 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
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
marr Int
0 MutablePrimArray (PrimState m) a
arr Int
off Int
len
MutablePrimArray (PrimState m) a
-> m (MutablePrimArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutablePrimArray (PrimState m) a
marr
{-# inline cloneMutablePrimArrayShim #-}
replicate :: (Contiguous arr, Element arr a) => Int -> a -> arr a
replicate :: Int -> a -> arr a
replicate Int
n a
x = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> a -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> b -> m (Mutable arr (PrimState m) b)
replicateMutable Int
n a
x)
{-# inline replicate #-}
replicateMutableM :: (PrimMonad m, Contiguous arr, Element arr a)
=> Int
-> m a
-> m (Mutable arr (PrimState m) a)
replicateMutableM :: Int -> m a -> m (Mutable arr (PrimState m) a)
replicateMutableM Int
len m a
act = do
Mutable arr (PrimState m) a
marr <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
len
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- m a
act
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
x
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
marr
{-# inline replicateMutableM #-}
replicateMutablePrimArray :: (PrimMonad m, Prim a)
=> Int
-> a
-> m (MutablePrimArray (PrimState m) a)
replicateMutablePrimArray :: Int -> a -> m (MutablePrimArray (PrimState m) a)
replicateMutablePrimArray Int
len a
a = 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
len
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 (PrimState m) a
marr Int
0 Int
len a
a
MutablePrimArray (PrimState m) a
-> m (MutablePrimArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutablePrimArray (PrimState m) a
marr
{-# inline replicateMutablePrimArray #-}
replicateSmallMutableArray :: (PrimMonad m)
=> Int
-> a
-> m (SmallMutableArray (PrimState m) a)
replicateSmallMutableArray :: Int -> a -> m (SmallMutableArray (PrimState m) a)
replicateSmallMutableArray Int
len a
a = do
SmallMutableArray (PrimState m) a
marr <- Int -> a -> m (SmallMutableArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
len a
forall a. a
errorThunk
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
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 Int
ix a
a
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
SmallMutableArray (PrimState m) a
-> m (SmallMutableArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure SmallMutableArray (PrimState m) a
marr
{-# inline replicateSmallMutableArray #-}
unsafeFromListN :: (Contiguous arr, Element arr a)
=> Int
-> [a]
-> arr a
unsafeFromListN :: Int -> [a] -> arr a
unsafeFromListN Int
n [a]
l = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> [a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListMutableN Int
n [a]
l)
{-# inline unsafeFromListN #-}
unsafeFromListMutableN :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> [a]
-> m (Mutable arr (PrimState m) a)
unsafeFromListMutableN :: Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListMutableN Int
n [a]
l = do
Mutable arr (PrimState m) a
m <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
n
let go :: Int -> [a] -> m (Mutable arr (PrimState m) a)
go !Int
_ [] = Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
m
go !Int
ix (a
x : [a]
xs) = do
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
m Int
ix a
x
Int -> [a] -> m (Mutable arr (PrimState m) a)
go (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [a]
xs
Int -> [a] -> m (Mutable arr (PrimState m) a)
go Int
0 [a]
l
{-# inline unsafeFromListMutableN #-}
unsafeFromListReverseMutableN :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> [a]
-> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN :: Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
n [a]
l = do
Mutable arr (PrimState m) a
m <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
n
let go :: Int -> [a] -> m (Mutable arr (PrimState m) a)
go !Int
_ [] = Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
m
go !Int
ix (a
x : [a]
xs) = do
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
m Int
ix a
x
Int -> [a] -> m (Mutable arr (PrimState m) a)
go (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
xs
Int -> [a] -> m (Mutable arr (PrimState m) a)
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [a]
l
{-# inline unsafeFromListReverseMutableN #-}
unsafeFromListReverseN :: (Contiguous arr, Element arr a)
=> Int
-> [a]
-> arr a
unsafeFromListReverseN :: Int -> [a] -> arr a
unsafeFromListReverseN Int
n [a]
l = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> [a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
n [a]
l)
{-# inline unsafeFromListReverseN #-}
mapMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> (a -> a)
-> Mutable arr (PrimState m) a
-> m ()
mapMutable :: (a -> a) -> Mutable arr (PrimState m) a -> m ()
mapMutable a -> a
f !Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix (a -> a
f a
a)
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
{-# inline mapMutable #-}
mapMutable' :: (PrimMonad m, Contiguous arr, Element arr a)
=> (a -> a)
-> Mutable arr (PrimState m) a
-> m ()
mapMutable' :: (a -> a) -> Mutable arr (PrimState m) a -> m ()
mapMutable' a -> a
f !Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
let !b :: a
b = a -> a
f a
a
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (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)
Int -> m ()
go Int
0
{-# inline mapMutable' #-}
imapMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> (Int -> a -> a)
-> Mutable arr (PrimState m) a
-> m ()
imapMutable :: (Int -> a -> a) -> Mutable arr (PrimState m) a -> m ()
imapMutable Int -> a -> a
f !Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix (Int -> a -> a
f Int
ix a
a)
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
{-# inline imapMutable #-}
imapMutable' :: (PrimMonad m, Contiguous arr, Element arr a)
=> (Int -> a -> a)
-> Mutable arr (PrimState m) a
-> m ()
imapMutable' :: (Int -> a -> a) -> Mutable arr (PrimState m) a -> m ()
imapMutable' Int -> a -> a
f !Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
let !b :: a
b = Int -> a -> a
f Int
ix a
a
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (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)
Int -> m ()
go Int
0
{-# inline imapMutable' #-}
traverseP :: (PrimMonad m, Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b)
=> (a -> m b)
-> arr1 a
-> m (arr2 b)
traverseP :: (a -> m b) -> arr1 a -> m (arr2 b)
traverseP a -> m b
f !arr1 a
arr = do
let !sz :: Int
sz = arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr
!Mutable arr2 (PrimState m) b
marr <- Int -> m (Mutable arr2 (PrimState m) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- arr1 a -> Int -> m a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
arr Int
ix
b
b <- a -> m b
f a
a
Mutable arr2 (PrimState m) b -> Int -> b -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 (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)
Int -> m ()
go Int
0
Mutable arr2 (PrimState m) b -> m (arr2 b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 (PrimState m) b
marr
{-# inline traverseP #-}
newtype STA v a = STA {STA v a -> forall s. Mutable v s a -> ST s (v a)
_runSTA :: forall s. Mutable v s a -> ST s (v a)}
runSTA :: (Contiguous v, Element v a) => Int -> STA v a -> v a
runSTA :: Int -> STA v a -> v a
runSTA !Int
sz (STA forall s. Mutable v s a -> ST s (v a)
m) = (forall s. ST s (v a)) -> v a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (v a)) -> v a) -> (forall s. ST s (v a)) -> v a
forall a b. (a -> b) -> a -> b
$ Int -> ST s (Mutable v (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz ST s (Mutable v s a) -> (Mutable v s a -> ST s (v a)) -> ST s (v a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Mutable v s a -> ST s (v a)
forall s. Mutable v s a -> ST s (v a)
m
{-# inline runSTA #-}
traverse ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Applicative f
)
=> (a -> f b)
-> arr1 a
-> f (arr2 b)
traverse :: (a -> f b) -> arr1 a -> f (arr2 b)
traverse a -> f b
f = (Int -> a -> f b) -> arr1 a -> f (arr2 b)
forall (arr1 :: * -> *) (arr2 :: * -> *) a b (f :: * -> *).
(Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b,
Applicative f) =>
(Int -> a -> f b) -> arr1 a -> f (arr2 b)
itraverse ((a -> f b) -> Int -> a -> f b
forall a b. a -> b -> a
const a -> f b
f)
{-# inline traverse #-}
traverse_ ::
(Contiguous arr, Element arr a, Applicative f)
=> (a -> f b)
-> arr a
-> f ()
traverse_ :: (a -> f b) -> arr a -> f ()
traverse_ a -> f b
f = (Int -> a -> f b) -> arr a -> f ()
forall (arr :: * -> *) a (f :: * -> *) b.
(Contiguous arr, Element arr a, Applicative f) =>
(Int -> a -> f b) -> arr a -> f ()
itraverse_ ((a -> f b) -> Int -> a -> f b
forall a b. a -> b -> a
const a -> f b
f)
itraverse ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Applicative f
)
=> (Int -> a -> f b)
-> arr1 a
-> f (arr2 b)
itraverse :: (Int -> a -> f b) -> arr1 a -> f (arr2 b)
itraverse Int -> a -> f b
f = \arr1 a
arr ->
let !sz :: Int
sz = arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr
go :: Int -> f (STA arr2 b)
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
then STA arr2 b -> f (STA arr2 b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((forall s. Mutable arr2 s b -> ST s (arr2 b)) -> STA arr2 b
forall (v :: * -> *) a.
(forall s. Mutable v s a -> ST s (v a)) -> STA v a
STA forall s. Mutable arr2 s b -> ST s (arr2 b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze)
else case arr1 a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr1 a
arr Int
ix of
(# a
x #) -> (b -> STA arr2 b -> STA arr2 b)
-> f b -> f (STA arr2 b) -> f (STA arr2 b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
(\b
b (STA forall s. Mutable arr2 s b -> ST s (arr2 b)
m) -> (forall s. Mutable arr2 s b -> ST s (arr2 b)) -> STA arr2 b
forall (v :: * -> *) a.
(forall s. Mutable v s a -> ST s (v a)) -> STA v a
STA ((forall s. Mutable arr2 s b -> ST s (arr2 b)) -> STA arr2 b)
-> (forall s. Mutable arr2 s b -> ST s (arr2 b)) -> STA arr2 b
forall a b. (a -> b) -> a -> b
$ \Mutable arr2 s b
marr -> do
Mutable arr2 (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s b
Mutable arr2 (PrimState (ST s)) b
marr Int
ix b
b
Mutable arr2 s b -> ST s (arr2 b)
forall s. Mutable arr2 s b -> ST s (arr2 b)
m Mutable arr2 s b
marr
)
(Int -> a -> f b
f Int
ix a
x)
(Int -> f (STA arr2 b)
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then arr2 b -> f (arr2 b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure arr2 b
forall (arr :: * -> *) a. Contiguous arr => arr a
empty
else Int -> STA arr2 b -> arr2 b
forall (v :: * -> *) a.
(Contiguous v, Element v a) =>
Int -> STA v a -> v a
runSTA Int
sz (STA arr2 b -> arr2 b) -> f (STA arr2 b) -> f (arr2 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA arr2 b)
go Int
0
{-# inline itraverse #-}
itraverse_ ::
(Contiguous arr, Element arr a, Applicative f)
=> (Int -> a -> f b)
-> arr a
-> f ()
itraverse_ :: (Int -> a -> f b) -> arr a -> f ()
itraverse_ Int -> a -> f b
f = \arr a
arr ->
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> f ()
go !Int
ix = Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
Int -> a -> f b
f Int
ix (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
arr 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)
in Int -> f ()
go Int
0
{-# inline itraverse_ #-}
for ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Applicative f
)
=> arr1 a
-> (a -> f b)
-> f (arr2 b)
for :: arr1 a -> (a -> f b) -> f (arr2 b)
for = ((a -> f b) -> arr1 a -> f (arr2 b))
-> arr1 a -> (a -> f b) -> f (arr2 b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> f b) -> arr1 a -> f (arr2 b)
forall (arr1 :: * -> *) (arr2 :: * -> *) a b (f :: * -> *).
(Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b,
Applicative f) =>
(a -> f b) -> arr1 a -> f (arr2 b)
traverse
{-# inline for #-}
for_ :: (Contiguous arr, Element arr a, Applicative f)
=> arr a
-> (a -> f b)
-> f ()
for_ :: arr a -> (a -> f b) -> f ()
for_ = ((a -> f b) -> arr a -> f ()) -> arr a -> (a -> f b) -> f ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> f b) -> arr a -> f ()
forall (arr :: * -> *) a (f :: * -> *) b.
(Contiguous arr, Element arr a, Applicative f) =>
(a -> f b) -> arr a -> f ()
traverse_
{-# inline for_ #-}
mapAccumLM' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 b
, Element arr2 c
, Monad m
) => (a -> b -> m (a, c)) -> a -> arr1 b -> m (a, arr2 c)
{-# inline mapAccumLM' #-}
mapAccumLM' :: (a -> b -> m (a, c)) -> a -> arr1 b -> m (a, arr2 c)
mapAccumLM' a -> b -> m (a, c)
f a
a0 arr1 b
src = Int -> [c] -> a -> m (a, arr2 c)
go Int
0 [] a
a0 where
!sz :: Int
sz = arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
src
go :: Int -> [c] -> a -> m (a, arr2 c)
go !Int
ix ![c]
xs !a
acc = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
(!a
acc',!c
x) <- a -> b -> m (a, c)
f a
acc (arr1 b -> Int -> b
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr1 b
src Int
ix)
Int -> [c] -> a -> m (a, arr2 c)
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (c
x c -> [c] -> [c]
forall a. a -> [a] -> [a]
: [c]
xs) a
acc'
else
let !xs' :: arr2 c
xs' = Int -> [c] -> arr2 c
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
Int -> [a] -> arr a
unsafeFromListReverseN Int
sz [c]
xs
in (a, arr2 c) -> m (a, arr2 c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
acc,arr2 c
xs')
mapAccum' :: forall arr1 arr2 a b c.
( Contiguous arr1
, Contiguous arr2
, Element arr1 b
, Element arr2 c
, Monoid a
) => (b -> (a, c)) -> arr1 b -> (a, arr2 c)
{-# inline mapAccum' #-}
mapAccum' :: (b -> (a, c)) -> arr1 b -> (a, arr2 c)
mapAccum' b -> (a, c)
f !arr1 b
src = (forall s. ST s (a, arr2 c)) -> (a, arr2 c)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (a, arr2 c)) -> (a, arr2 c))
-> (forall s. ST s (a, arr2 c)) -> (a, arr2 c)
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 s c
dst <- Int -> ST s (Mutable arr2 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
a
acc <- Int -> Mutable arr2 s c -> a -> ST s a
forall s. Int -> Mutable arr2 s c -> a -> ST s a
go Int
0 Mutable arr2 s c
dst a
forall a. Monoid a => a
mempty
arr2 c
dst' <- Mutable arr2 (PrimState (ST s)) c -> ST s (arr2 c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
dst
(a, arr2 c) -> ST s (a, arr2 c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
acc,arr2 c
dst')
where
!sz :: Int
sz = arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
src
go :: Int -> Mutable arr2 s c -> a -> ST s a
go :: Int -> Mutable arr2 s c -> a -> ST s a
go !Int
ix !Mutable arr2 s c
dst !a
accA = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let (!a
accB,!c
x) = b -> (a, c)
f (arr1 b -> Int -> b
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr1 b
src Int
ix)
Mutable arr2 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s c
Mutable arr2 (PrimState (ST s)) c
dst Int
ix c
x
Int -> Mutable arr2 s c -> a -> ST s a
forall s. Int -> Mutable arr2 s c -> a -> ST s a
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Mutable arr2 s c
dst (a
accA a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
accB)
else a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
accA
mapM ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Monad m
) => (a -> m b)
-> arr1 a
-> m (arr2 b)
mapM :: (a -> m b) -> arr1 a -> m (arr2 b)
mapM a -> m b
f arr1 a
arr =
let !sz :: Int
sz = arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr
in Int -> (Int -> m b) -> m (arr2 b)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, Monad m) =>
Int -> (Int -> m a) -> m (arr a)
generateM Int
sz ((Int -> m b) -> m (arr2 b)) -> (Int -> m b) -> m (arr2 b)
forall a b. (a -> b) -> a -> b
$ \Int
ix -> arr1 a -> Int -> m a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
arr Int
ix m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
f
{-# inline mapM #-}
mapM_ :: (Contiguous arr, Element arr a, Element arr b, Applicative f)
=> (a -> f b)
-> arr a
-> f ()
mapM_ :: (a -> f b) -> arr a -> f ()
mapM_ = (a -> f b) -> arr a -> f ()
forall (arr :: * -> *) a (f :: * -> *) b.
(Contiguous arr, Element arr a, Applicative f) =>
(a -> f b) -> arr a -> f ()
traverse_
{-# inline mapM_ #-}
forM ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Monad m
) => arr1 a
-> (a -> m b)
-> m (arr2 b)
forM :: arr1 a -> (a -> m b) -> m (arr2 b)
forM = ((a -> m b) -> arr1 a -> m (arr2 b))
-> arr1 a -> (a -> m b) -> m (arr2 b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> m b) -> arr1 a -> m (arr2 b)
forall (arr1 :: * -> *) (arr2 :: * -> *) a b (m :: * -> *).
(Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b,
Monad m) =>
(a -> m b) -> arr1 a -> m (arr2 b)
mapM
{-# inline forM #-}
forM_ :: (Contiguous arr, Element arr a, Element arr b, Applicative f)
=> arr a
-> (a -> f b)
-> f ()
forM_ :: arr a -> (a -> f b) -> f ()
forM_ = ((a -> f b) -> arr a -> f ()) -> arr a -> (a -> f b) -> f ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> f b) -> arr a -> f ()
forall (arr :: * -> *) a (f :: * -> *) b.
(Contiguous arr, Element arr a, Applicative f) =>
(a -> f b) -> arr a -> f ()
traverse_
{-# inline forM_ #-}
sequence ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 (f a)
, Element arr2 a
, Applicative f
) => arr1 (f a) -> f (arr2 a)
sequence :: arr1 (f a) -> f (arr2 a)
sequence = (f a -> f a) -> arr1 (f a) -> f (arr2 a)
forall (arr1 :: * -> *) (arr2 :: * -> *) a b (f :: * -> *).
(Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b,
Applicative f) =>
(a -> f b) -> arr1 a -> f (arr2 b)
traverse f a -> f a
forall a. a -> a
id
{-# inline sequence #-}
sequence_ ::
( Contiguous arr
, Element arr (f a)
, Applicative f
) => arr (f a) -> f ()
sequence_ :: arr (f a) -> f ()
sequence_ = (f a -> f () -> f ()) -> f () -> arr (f a) -> f ()
forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> arr a -> b
foldr f a -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) (() -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# inline sequence_ #-}
asum ::
( Contiguous arr
, Element arr (f a)
, A.Alternative f
) => arr (f a) -> f a
asum :: arr (f a) -> f a
asum = (f a -> f a -> f a) -> f a -> arr (f a) -> f a
forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> arr a -> b
foldr f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(A.<|>) f a
forall (f :: * -> *) a. Alternative f => f a
A.empty
{-# inline asum #-}
generate :: (Contiguous arr, Element arr a)
=> Int
-> (Int -> a)
-> arr a
generate :: Int -> (Int -> a) -> arr a
generate Int
len Int -> a
f = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> (Int -> a) -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> (Int -> a) -> m (Mutable arr (PrimState m) a)
generateMutable Int
len Int -> a
f)
{-# inline generate #-}
generateM :: (Contiguous arr, Element arr a, Monad m)
=> Int
-> (Int -> m a)
-> m (arr a)
generateM :: Int -> (Int -> m a) -> m (arr a)
generateM !Int
sz Int -> m a
f =
let go :: Int -> m (STA arr a)
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then (a -> STA arr a -> STA arr a)
-> m a -> m (STA arr a) -> m (STA arr a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
(\a
b (STA forall s. Mutable arr s a -> ST s (arr a)
m) -> (forall s. Mutable arr s a -> ST s (arr a)) -> STA arr a
forall (v :: * -> *) a.
(forall s. Mutable v s a -> ST s (v a)) -> STA v a
STA ((forall s. Mutable arr s a -> ST s (arr a)) -> STA arr a)
-> (forall s. Mutable arr s a -> ST s (arr a)) -> STA arr a
forall a b. (a -> b) -> a -> b
$ \Mutable arr s a
marr -> do
Mutable arr (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr s a
Mutable arr (PrimState (ST s)) a
marr Int
ix a
b
Mutable arr s a -> ST s (arr a)
forall s. Mutable arr s a -> ST s (arr a)
m Mutable arr s a
marr
)
(Int -> m a
f Int
ix)
(Int -> m (STA arr a)
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
else STA arr a -> m (STA arr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA arr a -> m (STA arr a)) -> STA arr a -> m (STA arr a)
forall a b. (a -> b) -> a -> b
$ (forall s. Mutable arr s a -> ST s (arr a)) -> STA arr a
forall (v :: * -> *) a.
(forall s. Mutable v s a -> ST s (v a)) -> STA v a
STA forall s. Mutable arr s a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze
in if Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then arr a -> m (arr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure arr a
forall (arr :: * -> *) a. Contiguous arr => arr a
empty
else Int -> STA arr a -> arr a
forall (v :: * -> *) a.
(Contiguous v, Element v a) =>
Int -> STA v a -> v a
runSTA Int
sz (STA arr a -> arr a) -> m (STA arr a) -> m (arr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (STA arr a)
go Int
0
generateMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> (Int -> a)
-> m (Mutable arr (PrimState m) a)
generateMutable :: Int -> (Int -> a) -> m (Mutable arr (PrimState m) a)
generateMutable Int
len Int -> a
f = Int -> (Int -> m a) -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> (Int -> m a) -> m (Mutable arr (PrimState m) a)
generateMutableM Int
len (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (Int -> a) -> Int -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f)
{-# inline generateMutable #-}
generateMutableM :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> (Int -> m a)
-> m (Mutable arr (PrimState m) a)
generateMutableM :: Int -> (Int -> m a) -> m (Mutable arr (PrimState m) a)
generateMutableM !Int
len Int -> m a
f = do
Mutable arr (PrimState m) a
marr <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
len
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- Int -> m a
f Int
ix
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
x
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
marr
{-# inline generateMutableM #-}
iterateN :: (Contiguous arr, Element arr a)
=> Int
-> (a -> a)
-> a
-> arr a
iterateN :: Int -> (a -> a) -> a -> arr a
iterateN Int
len a -> a
f a
z0 = (forall s. ST s (arr a)) -> arr a
forall a. (forall s. ST s a) -> a
runST (Int -> (a -> a) -> a -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> (a -> a) -> a -> m (Mutable arr (PrimState m) a)
iterateMutableN Int
len a -> a
f a
z0 ST s (Mutable arr s a)
-> (Mutable arr s a -> ST s (arr a)) -> ST s (arr a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Mutable arr s a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze)
{-# inline iterateN #-}
iterateMutableN :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> (a -> a)
-> a
-> m (Mutable arr (PrimState m) a)
iterateMutableN :: Int -> (a -> a) -> a -> m (Mutable arr (PrimState m) a)
iterateMutableN Int
len a -> a
f a
z0 = Int -> (a -> m a) -> a -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> (a -> m a) -> a -> m (Mutable arr (PrimState m) a)
iterateMutableNM Int
len (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f) a
z0
{-# inline iterateMutableN #-}
iterateMutableNM :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> (a -> m a)
-> a
-> m (Mutable arr (PrimState m) a)
iterateMutableNM :: Int -> (a -> m a) -> a -> m (Mutable arr (PrimState m) a)
iterateMutableNM !Int
len a -> m a
f a
z0 = do
Mutable arr (PrimState m) a
marr <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
len
let go :: Int -> a -> m ()
go !Int
ix !a
acc
| Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
z0 m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> a -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
z0
| Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
a
a <- a -> m a
f a
acc
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
a
Int -> a -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
a
Int -> a -> m ()
go Int
0 a
z0
Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
marr
{-# inline iterateMutableNM #-}
create :: (Contiguous arr, Element arr a)
=> (forall s. ST s (Mutable arr s a))
-> arr a
create :: (forall s. ST s (Mutable arr s a)) -> arr a
create forall s. ST s (Mutable arr s a)
x = (forall s. ST s (arr a)) -> arr a
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run (Mutable arr s a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze (Mutable arr s a -> ST s (arr a))
-> ST s (Mutable arr s a) -> ST s (arr a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ST s (Mutable arr s a)
forall s. ST s (Mutable arr s a)
x)
{-# inline create #-}
createT :: (Contiguous arr, Element arr a, Traversable f)
=> (forall s. ST s (f (Mutable arr s a)))
-> f (arr a)
createT :: (forall s. ST s (f (Mutable arr s a))) -> f (arr a)
createT forall s. ST s (f (Mutable arr s a))
p = (forall s. ST s (f (arr a))) -> f (arr a)
forall a. (forall s. ST s a) -> a
runST ((Mutable arr s a -> ST s (arr a))
-> f (Mutable arr s a) -> ST s (f (arr a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
Prelude.mapM Mutable arr s a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze (f (Mutable arr s a) -> ST s (f (arr a)))
-> ST s (f (Mutable arr s a)) -> ST s (f (arr a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ST s (f (Mutable arr s a))
forall s. ST s (f (Mutable arr s a))
p)
{-# inline createT #-}
unfoldr :: (Contiguous arr, Element arr a)
=> (b -> Maybe (a,b))
-> b
-> arr a
unfoldr :: (b -> Maybe (a, b)) -> b -> arr a
unfoldr b -> Maybe (a, b)
f b
z0 = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((b -> Maybe (a, b)) -> b -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *) b.
(Contiguous arr, Element arr a, PrimMonad m) =>
(b -> Maybe (a, b)) -> b -> m (Mutable arr (PrimState m) a)
unfoldrMutable b -> Maybe (a, b)
f b
z0)
{-# inline unfoldr #-}
unfoldrMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> (b -> Maybe (a,b))
-> b
-> m (Mutable arr (PrimState m) a)
unfoldrMutable :: (b -> Maybe (a, b)) -> b -> m (Mutable arr (PrimState m) a)
unfoldrMutable b -> Maybe (a, b)
f b
z0 = do
let go :: Int -> b -> [a] -> m (Int, [a])
go !Int
sz b
s ![a]
xs = case b -> Maybe (a, b)
f b
s of
Maybe (a, b)
Nothing -> (Int, [a]) -> m (Int, [a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
sz,[a]
xs)
Just (a
x,b
s') -> Int -> b -> [a] -> m (Int, [a])
go (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
s' (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs)
(Int
sz,[a]
xs) <- Int -> b -> [a] -> m (Int, [a])
go Int
0 b
z0 []
Int -> [a] -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
sz [a]
xs
{-# inline unfoldrMutable #-}
unfoldrN :: (Contiguous arr, Element arr a)
=> Int
-> (b -> Maybe (a, b))
-> b
-> arr a
unfoldrN :: Int -> (b -> Maybe (a, b)) -> b -> arr a
unfoldrN Int
maxSz b -> Maybe (a, b)
f b
z0 = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int
-> (b -> Maybe (a, b))
-> b
-> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *) b.
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> (b -> Maybe (a, b)) -> b -> m (Mutable arr (PrimState m) a)
unfoldrMutableN Int
maxSz b -> Maybe (a, b)
f b
z0)
{-# inline unfoldrN #-}
unfoldrMutableN :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> (b -> Maybe (a, b))
-> b
-> m (Mutable arr (PrimState m) a)
unfoldrMutableN :: Int -> (b -> Maybe (a, b)) -> b -> m (Mutable arr (PrimState m) a)
unfoldrMutableN !Int
maxSz b -> Maybe (a, b)
f b
z0 = do
Mutable arr (PrimState m) a
m <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
maxSz
let go :: Int -> b -> m Int
go !Int
ix b
s = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxSz
then case b -> Maybe (a, b)
f b
s of
Maybe (a, b)
Nothing -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
ix
Just (a
x,b
s') -> do
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
m Int
ix a
x
Int -> b -> m Int
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
s'
else Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
ix
Int
sz <- Int -> b -> m Int
go Int
0 b
z0
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
maxSz Int
sz of
Ordering
EQ -> Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
m
Ordering
GT -> Mutable arr (PrimState m) a
-> Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b
-> Int -> m (Mutable arr (PrimState m) b)
resize Mutable arr (PrimState m) a
m Int
sz
Ordering
LT -> [Char] -> m (Mutable arr (PrimState m) a)
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Primitive.Contiguous.unfoldrMutableN: internal error"
{-# inline unfoldrMutableN #-}
toList :: (Contiguous arr, Element arr a)
=> arr a
-> [a]
toList :: arr a -> [a]
toList arr a
arr = (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 -> arr a -> b
forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> arr a -> b
foldr a -> b -> b
c b
n arr a
arr)
{-# inline toList #-}
toListMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> Mutable arr (PrimState m) a
-> m [a]
toListMutable :: Mutable arr (PrimState m) a -> m [a]
toListMutable Mutable arr (PrimState m) a
marr = do
Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> [a] -> m [a]
go !Int
ix ![a]
acc = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do
a
x <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
Int -> [a] -> m [a]
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc)
else [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
acc
Int -> [a] -> m [a]
go (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) []
{-# inline toListMutable #-}
fromListMutableN :: (Contiguous arr, Element arr a, PrimMonad m)
=> Int
-> [a]
-> m (Mutable arr (PrimState m) a)
fromListMutableN :: Int -> [a] -> m (Mutable arr (PrimState m) a)
fromListMutableN Int
len [a]
vs = do
Mutable arr (PrimState m) a
marr <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
len
let go :: [a] -> Int -> m ()
go [] !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len
then () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
else [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Primitive.Contiguous.fromListN: 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
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
a
[a] -> Int -> m ()
go [a]
as (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Primitive.Contiguous.fromListN: list length greater than specified size."
[a] -> Int -> m ()
go [a]
vs Int
0
Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
marr
{-# inline fromListMutableN #-}
fromListMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> [a]
-> m (Mutable arr (PrimState m) a)
fromListMutable :: [a] -> m (Mutable arr (PrimState m) a)
fromListMutable [a]
xs = Int -> [a] -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
fromListMutableN ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) [a]
xs
{-# inline fromListMutable #-}
fromListN :: (Contiguous arr, Element arr a)
=> Int
-> [a]
-> arr a
fromListN :: Int -> [a] -> arr a
fromListN Int
len [a]
vs = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> [a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
fromListMutableN Int
len [a]
vs)
{-# inline fromListN #-}
fromList :: (Contiguous arr, Element arr a)
=> [a]
-> arr a
fromList :: [a] -> arr a
fromList [a]
vs = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ([a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
[a] -> m (Mutable arr (PrimState m) a)
fromListMutable [a]
vs)
{-# inline fromList #-}
modify :: (Contiguous arr, Element arr a, PrimMonad m)
=> (a -> a)
-> Mutable arr (PrimState m) a
-> m ()
modify :: (a -> a) -> Mutable arr (PrimState m) a -> m ()
modify a -> a
f Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix (a -> a
f a
x)
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
{-# inline modify #-}
modify' :: (Contiguous arr, Element arr a, PrimMonad m)
=> (a -> a)
-> Mutable arr (PrimState m) a
-> m ()
modify' :: (a -> a) -> Mutable arr (PrimState m) a -> m ()
modify' a -> a
f Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
let go :: Int -> m ()
go !Int
ix = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix
let !y :: a
y = a -> a
f a
x
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix a
y
Int -> m ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> m ()
go Int
0
{-# inline modify' #-}
enumFromN :: (Contiguous arr, Element arr a, Enum a)
=> a
-> Int
-> arr a
enumFromN :: a -> Int -> arr a
enumFromN a
z0 Int
sz = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (a -> Int -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m, Enum a) =>
a -> Int -> m (Mutable arr (PrimState m) a)
enumFromMutableN a
z0 Int
sz)
{-# inline enumFromN #-}
enumFromMutableN :: (Contiguous arr, Element arr a, PrimMonad m, Enum a)
=> a
-> Int
-> m (Mutable arr (PrimState m) a)
enumFromMutableN :: a -> Int -> m (Mutable arr (PrimState m) a)
enumFromMutableN a
z0 !Int
sz = do
Mutable arr (PrimState m) a
m <- Int -> m (Mutable arr (PrimState m) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
let go :: Int -> a -> m (Mutable arr (PrimState m) a)
go !Int
ix a
z = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
m Int
ix a
z
Int -> a -> m (Mutable arr (PrimState m) a)
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> a
forall a. Enum a => a -> a
succ a
z)
else Mutable arr (PrimState m) a -> m (Mutable arr (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr (PrimState m) a
m
Int -> a -> m (Mutable arr (PrimState m) a)
go Int
0 a
z0
{-# inline enumFromMutableN #-}
liftHashWithSalt :: (Contiguous arr, Element arr a)
=> (Int -> a -> Int)
-> Int
-> arr a
-> Int
liftHashWithSalt :: (Int -> a -> Int) -> Int -> arr a -> Int
liftHashWithSalt Int -> a -> Int
f Int
s0 arr a
arr = Int -> Int -> Int
go Int
0 Int
s0 where
sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> Int -> Int
go !Int
ix !Int
s = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then
let !(# a
x #) = arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix
in Int -> Int -> Int
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> a -> Int
f Int
s a
x)
else Int -> Int -> Int
hashIntWithSalt Int
s Int
ix
{-# inline liftHashWithSalt #-}
reverse :: (Contiguous arr, Element arr a)
=> arr a
-> arr a
reverse :: arr a -> arr a
reverse arr a
arr = (forall s. ST s (arr a)) -> arr a
forall (arr :: * -> *) a.
Contiguous arr =>
(forall s. ST s (arr a)) -> arr a
run ((forall s. ST s (arr a)) -> arr a)
-> (forall s. ST s (arr a)) -> arr a
forall a b. (a -> b) -> a -> b
$ do
Mutable arr s a
marr <- Int -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
Mutable arr (PrimState (ST s)) a
-> Int -> arr a -> Int -> Int -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> arr b -> Int -> Int -> m ()
copy Mutable arr s a
Mutable arr (PrimState (ST s)) a
marr Int
0 arr a
arr Int
0 Int
sz
Mutable arr (PrimState (ST s)) a -> ST s ()
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Mutable arr (PrimState m) a -> m ()
reverseMutable Mutable arr s a
Mutable arr (PrimState (ST s)) a
marr
Mutable arr (PrimState (ST s)) a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze Mutable arr s a
Mutable arr (PrimState (ST s)) a
marr
where
!sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
{-# inline reverse #-}
reverseMutable :: (Contiguous arr, Element arr a, PrimMonad m)
=> Mutable arr (PrimState m) a
-> m ()
reverseMutable :: Mutable arr (PrimState m) a -> m ()
reverseMutable Mutable arr (PrimState m) a
marr = do
!Int
sz <- Mutable arr (PrimState m) a -> m Int
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> m Int
sizeMutable Mutable arr (PrimState m) a
marr
Mutable arr (PrimState m) a -> Int -> Int -> m ()
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Mutable arr (PrimState m) a -> Int -> Int -> m ()
reverseSlice Mutable arr (PrimState m) a
marr Int
0 (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
{-# inline reverseMutable #-}
reverseSlice :: (Contiguous arr, Element arr a, PrimMonad m)
=> Mutable arr (PrimState m) a
-> Int
-> Int
-> m ()
reverseSlice :: Mutable arr (PrimState m) a -> Int -> Int -> m ()
reverseSlice !Mutable arr (PrimState m) a
marr !Int
start !Int
end = do
let go :: Int -> Int -> m ()
go !Int
s !Int
e = if Int
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
e
then () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
else do
a
tmp <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
s
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
s (a -> m ()) -> m a -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
e
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
e a
tmp
Int -> Int -> m ()
go (Int
sInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
Int -> Int -> m ()
go Int
start Int
end
{-# inline reverseSlice #-}
same :: Contiguous arr => arr a -> arr a -> Bool
same :: arr a -> arr a -> Bool
same arr a
a arr a
b = Int# -> Bool
isTrue# (MutableArrayArray# Any -> MutableArrayArray# Any -> Int#
forall d. MutableArrayArray# d -> MutableArrayArray# d -> Int#
sameMutableArrayArray# (ArrayArray# -> MutableArrayArray# s
unsafeCoerce# (arr a -> ArrayArray#
forall (arr :: * -> *) b. Contiguous arr => arr b -> ArrayArray#
unlift arr a
a) :: MutableArrayArray# s) (ArrayArray# -> MutableArrayArray# s
unsafeCoerce# (arr a -> ArrayArray#
forall (arr :: * -> *) b. Contiguous arr => arr b -> ArrayArray#
unlift arr a
b) :: MutableArrayArray# s))
hashIntWithSalt :: Int -> Int -> Int
hashIntWithSalt :: Int -> Int -> Int
hashIntWithSalt Int
salt Int
x = Int
salt Int -> Int -> Int
`combine` Int
x
{-# inline hashIntWithSalt #-}
combine :: Int -> Int -> Int
combine :: Int -> Int -> Int
combine Int
h1 Int
h2 = (Int
h1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
16777619) Int -> Int -> Int
forall a. Bits a => a -> a -> a
`xor` Int
h2
{-# inline combine #-}
elem :: (Contiguous arr, Element arr a, Eq a) => a -> arr a -> Bool
elem :: a -> arr a -> Bool
elem a
a !arr a
arr =
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> Bool
go !Int
ix
| Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
!(# a
x #) -> if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x
then Bool
True
else Int -> Bool
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Bool
False
in Int -> Bool
go Int
0
{-# inline elem #-}
maximum :: (Contiguous arr, Element arr a, Ord a) => arr a -> Maybe a
maximum :: arr a -> Maybe a
maximum = (a -> a -> Ordering) -> arr a -> Maybe a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(a -> a -> Ordering) -> arr a -> Maybe a
maximumBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# inline maximum #-}
minimum :: (Contiguous arr, Element arr a, Ord a) => arr a -> Maybe a
minimum :: arr a -> Maybe a
minimum = (a -> a -> Ordering) -> arr a -> Maybe a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(a -> a -> Ordering) -> arr a -> Maybe a
minimumBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# inline minimum #-}
maximumBy :: (Contiguous arr, Element arr a)
=> (a -> a -> Ordering)
-> arr a
-> Maybe a
maximumBy :: (a -> a -> Ordering) -> arr a -> Maybe a
maximumBy a -> a -> Ordering
f arr a
arr =
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> a -> a
go !Int
ix a
o = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
!(# a
x #) -> Int -> a -> a
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (case a -> a -> Ordering
f a
x a
o of { Ordering
GT -> a
x; Ordering
_ -> a
o; })
else a
o
in if Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Maybe a
forall a. Maybe a
Nothing
else a -> Maybe a
forall a. a -> Maybe a
Just (Int -> a -> a
go Int
0 (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
arr Int
0))
{-# inline maximumBy #-}
minimumBy :: (Contiguous arr, Element arr a)
=> (a -> a -> Ordering)
-> arr a
-> Maybe a
minimumBy :: (a -> a -> Ordering) -> arr a -> Maybe a
minimumBy a -> a -> Ordering
f arr a
arr =
let !sz :: Int
sz = arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
arr
go :: Int -> a -> a
go !Int
ix a
o = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then case arr a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr a
arr Int
ix of
!(# a
x #) -> Int -> a -> a
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (case a -> a -> Ordering
f a
x a
o of { Ordering
GT -> a
o; Ordering
_ -> a
x; })
else a
o
in if Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Maybe a
forall a. Maybe a
Nothing
else a -> Maybe a
forall a. a -> Maybe a
Just (Int -> a -> a
go Int
0 (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
arr Int
0))
{-# inline minimumBy #-}
find :: (Contiguous arr, Element arr a)
=> (a -> Bool)
-> arr a
-> Maybe a
find :: (a -> Bool) -> arr a -> Maybe a
find a -> Bool
p = Maybe (First a) -> Maybe a
coerce (Maybe (First a) -> Maybe a)
-> (arr a -> Maybe (First a)) -> arr a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a -> Maybe (First a)) -> arr a -> Maybe (First a)
forall (arr :: * -> *) a m.
(Contiguous arr, Element arr a, Monoid m) =>
(a -> m) -> arr a -> m
foldMap (\a
x -> if a -> Bool
p a
x then First a -> Maybe (First a)
forall a. a -> Maybe a
Just (a -> First a
forall a. a -> First a
First a
x) else Maybe (First a)
forall a. Maybe a
Nothing))
{-# inline find #-}
findIndex :: (Contiguous arr, Element arr a)
=> (a -> Bool)
-> arr a
-> Maybe Int
findIndex :: (a -> Bool) -> arr a -> Maybe Int
findIndex a -> Bool
p arr a
xs = Int -> Maybe Int
loop Int
0
where
loop :: Int -> Maybe Int
loop Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< arr a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr a
xs = if a -> Bool
p (arr a -> Int -> a
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> b
index arr a
xs Int
i) then Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i else Int -> Maybe Int
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Maybe Int
forall a. Maybe a
Nothing
{-# inline findIndex #-}
swap :: (Contiguous arr, Element arr a, PrimMonad m)
=> Mutable arr (PrimState m) a
-> Int
-> Int
-> m ()
swap :: Mutable arr (PrimState m) a -> Int -> Int -> m ()
swap !Mutable arr (PrimState m) a
marr !Int
ix1 !Int
ix2 = do
a
atIx1 <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix1
a
atIx2 <- Mutable arr (PrimState m) a -> Int -> m a
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> m b
read Mutable arr (PrimState m) a
marr Int
ix2
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix1 a
atIx2
Mutable arr (PrimState m) a -> Int -> a -> m ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr (PrimState m) a
marr Int
ix2 a
atIx1
{-# inline swap #-}
lefts :: forall arr a b.
( Contiguous arr
, Element arr a
, Element arr (Either a b)
) => arr (Either a b)
-> arr a
lefts :: arr (Either a b) -> arr a
lefts !arr (Either a b)
arr = (forall s. ST s (Mutable arr s a)) -> arr a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr s a)) -> arr a)
-> (forall s. ST s (Mutable arr s a)) -> arr a
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = arr (Either a b) -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr (Either a b)
arr
go :: Int -> [a] -> Int -> ST s (Int, [a])
go :: Int -> [a] -> Int -> ST s (Int, [a])
go !Int
ix ![a]
as !Int
acc = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
arr (Either a b) -> Int -> ST s (Either a b)
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr (Either a b)
arr Int
ix ST s (Either a b)
-> (Either a b -> ST s (Int, [a])) -> ST s (Int, [a])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left a
a -> Int -> [a] -> Int -> ST s (Int, [a])
forall s. Int -> [a] -> Int -> ST s (Int, [a])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as) (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Right b
_ -> Int -> [a] -> Int -> ST s (Int, [a])
forall s. Int -> [a] -> Int -> ST s (Int, [a])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
as Int
acc
else (Int, [a]) -> ST s (Int, [a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
acc, [a]
as)
(Int
len, [a]
as) <- Int -> [a] -> Int -> ST s (Int, [a])
forall s. Int -> [a] -> Int -> ST s (Int, [a])
go Int
0 [] Int
0
Int -> [a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
len [a]
as
{-# inline lefts #-}
rights :: forall arr a b.
( Contiguous arr
, Element arr b
, Element arr (Either a b)
) => arr (Either a b)
-> arr b
rights :: arr (Either a b) -> arr b
rights !arr (Either a b)
arr = (forall s. ST s (Mutable arr s b)) -> arr b
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr s b)) -> arr b)
-> (forall s. ST s (Mutable arr s b)) -> arr b
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = arr (Either a b) -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr (Either a b)
arr
go :: Int -> [b] -> Int -> ST s (Int, [b])
go :: Int -> [b] -> Int -> ST s (Int, [b])
go !Int
ix ![b]
bs !Int
acc = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
arr (Either a b) -> Int -> ST s (Either a b)
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr (Either a b)
arr Int
ix ST s (Either a b)
-> (Either a b -> ST s (Int, [b])) -> ST s (Int, [b])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left a
_ -> Int -> [b] -> Int -> ST s (Int, [b])
forall s. Int -> [b] -> Int -> ST s (Int, [b])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [b]
bs Int
acc
Right b
b -> Int -> [b] -> Int -> ST s (Int, [b])
forall s. Int -> [b] -> Int -> ST s (Int, [b])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b
bb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
bs) (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else (Int, [b]) -> ST s (Int, [b])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
acc, [b]
bs)
(Int
len, [b]
bs) <- Int -> [b] -> Int -> ST s (Int, [b])
forall s. Int -> [b] -> Int -> ST s (Int, [b])
go Int
0 [] Int
0
Int -> [b] -> ST s (Mutable arr (PrimState (ST s)) b)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
len [b]
bs
{-# inline rights #-}
partitionEithers :: forall arr a b.
( Contiguous arr
, Element arr a
, Element arr b
, Element arr (Either a b)
) => arr (Either a b)
-> (arr a, arr b)
partitionEithers :: arr (Either a b) -> (arr a, arr b)
partitionEithers !arr (Either a b)
arr = (forall s. ST s (arr a, arr b)) -> (arr a, arr b)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (arr a, arr b)) -> (arr a, arr b))
-> (forall s. ST s (arr a, arr b)) -> (arr a, arr b)
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = arr (Either a b) -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr (Either a b)
arr
go :: Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
go :: Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
go !Int
ix ![a]
as ![b]
bs !Int
accA !Int
accB = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
arr (Either a b) -> Int -> ST s (Either a b)
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr (Either a b)
arr Int
ix ST s (Either a b)
-> (Either a b -> ST s (Int, Int, [a], [b]))
-> ST s (Int, Int, [a], [b])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left a
a -> Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
forall s.
Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as) [b]
bs (Int
accA Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
accB
Right b
b -> Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
forall s.
Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
as (b
bb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
bs) Int
accA (Int
accB Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else (Int, Int, [a], [b]) -> ST s (Int, Int, [a], [b])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
accA, Int
accB, [a]
as, [b]
bs)
(Int
lenA, Int
lenB, [a]
as, [b]
bs) <- Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
forall s.
Int -> [a] -> [b] -> Int -> Int -> ST s (Int, Int, [a], [b])
go Int
0 [] [] Int
0 Int
0
arr a
arrA <- Mutable arr s a -> ST s (arr a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze (Mutable arr s a -> ST s (arr a))
-> ST s (Mutable arr s a) -> ST s (arr a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> [a] -> ST s (Mutable arr (PrimState (ST s)) a)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
lenA [a]
as
arr b
arrB <- Mutable arr s b -> ST s (arr b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m) =>
Mutable arr (PrimState m) b -> m (arr b)
unsafeFreeze (Mutable arr s b -> ST s (arr b))
-> ST s (Mutable arr s b) -> ST s (arr b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> [b] -> ST s (Mutable arr (PrimState (ST s)) b)
forall (arr :: * -> *) a (m :: * -> *).
(Contiguous arr, Element arr a, PrimMonad m) =>
Int -> [a] -> m (Mutable arr (PrimState m) a)
unsafeFromListReverseMutableN Int
lenB [b]
bs
(arr a, arr b) -> ST s (arr a, arr b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (arr a
arrA, arr b
arrB)
{-# inline partitionEithers #-}
scanl ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (b -> a -> b)
-> b
-> arr1 a
-> arr2 b
scanl :: (b -> a -> b) -> b -> arr1 a -> arr2 b
scanl b -> a -> b
f = (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
(Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iscanl ((b -> a -> b) -> Int -> b -> a -> b
forall a b. a -> b -> a
const b -> a -> b
f)
{-# inline scanl #-}
iscanl ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
iscanl :: (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iscanl Int -> b -> a -> b
f b
q arr1 a
as = Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
as Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> b -> a -> b
f b
q arr1 a
as
{-# inline iscanl #-}
scanl' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (b -> a -> b)
-> b
-> arr1 a
-> arr2 b
scanl' :: (b -> a -> b) -> b -> arr1 a -> arr2 b
scanl' b -> a -> b
f = (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
(Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iscanl' ((b -> a -> b) -> Int -> b -> a -> b
forall a b. a -> b -> a
const b -> a -> b
f)
{-# inline scanl' #-}
iscanl' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
iscanl' :: (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iscanl' Int -> b -> a -> b
f !b
q arr1 a
as = Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl' (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
as Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> b -> a -> b
f b
q arr1 a
as
{-# inline iscanl' #-}
internalScanl ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => Int
-> (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
internalScanl :: Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl !Int
sz Int -> b -> a -> b
f !b
q arr1 a
as = (forall s. ST s (Mutable arr2 s b)) -> arr2 b
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr2 s b)) -> arr2 b)
-> (forall s. ST s (Mutable arr2 s b)) -> arr2 b
forall a b. (a -> b) -> a -> b
$ do
!Mutable arr2 s b
marr <- Int -> ST s (Mutable arr2 (PrimState (ST s)) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
let go :: Int -> b -> ST s ()
go !Int
ix b
acc = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s b
Mutable arr2 (PrimState (ST s)) b
marr Int
ix b
acc
a
x <- arr1 a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
as Int
ix
Int -> b -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> b -> a -> b
f Int
ix b
acc a
x)
Int -> b -> ST s ()
go Int
0 b
q
Mutable arr2 s b -> ST s (Mutable arr2 s b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr2 s b
marr
{-# inline internalScanl #-}
internalScanl' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => Int
-> (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
internalScanl' :: Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl' !Int
sz Int -> b -> a -> b
f !b
q arr1 a
as = (forall s. ST s (Mutable arr2 s b)) -> arr2 b
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr2 s b)) -> arr2 b)
-> (forall s. ST s (Mutable arr2 s b)) -> arr2 b
forall a b. (a -> b) -> a -> b
$ do
!Mutable arr2 s b
marr <- Int -> ST s (Mutable arr2 (PrimState (ST s)) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
let go :: Int -> b -> ST s ()
go !Int
ix !b
acc = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
Mutable arr2 (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr2 s b
Mutable arr2 (PrimState (ST s)) b
marr Int
ix b
acc
a
x <- arr1 a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
as Int
ix
Int -> b -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> b -> a -> b
f Int
ix b
acc a
x)
Int -> b -> ST s ()
go Int
0 b
q
Mutable arr2 s b -> ST s (Mutable arr2 s b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr2 s b
marr
{-# inline internalScanl' #-}
prescanl ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (b -> a -> b)
-> b
-> arr1 a
-> arr2 b
prescanl :: (b -> a -> b) -> b -> arr1 a -> arr2 b
prescanl b -> a -> b
f = (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
(Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iprescanl ((b -> a -> b) -> Int -> b -> a -> b
forall a b. a -> b -> a
const b -> a -> b
f)
{-# inline prescanl #-}
iprescanl ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
iprescanl :: (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iprescanl Int -> b -> a -> b
f b
q arr1 a
as = Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
as) Int -> b -> a -> b
f b
q arr1 a
as
{-# inline iprescanl #-}
prescanl' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (b -> a -> b)
-> b
-> arr1 a
-> arr2 b
prescanl' :: (b -> a -> b) -> b -> arr1 a -> arr2 b
prescanl' b -> a -> b
f = (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
(Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iprescanl ((b -> a -> b) -> Int -> b -> a -> b
forall a b. a -> b -> a
const b -> a -> b
f)
{-# inline prescanl' #-}
iprescanl' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (Int -> b -> a -> b)
-> b
-> arr1 a
-> arr2 b
iprescanl' :: (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
iprescanl' Int -> b -> a -> b
f !b
q arr1 a
as = Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
forall (arr1 :: * -> *) (arr2 :: * -> *) a b.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
Int -> (Int -> b -> a -> b) -> b -> arr1 a -> arr2 b
internalScanl' (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
as) Int -> b -> a -> b
f b
q arr1 a
as
{-# inline iprescanl' #-}
zipWith ::
( Contiguous arr1
, Contiguous arr2
, Contiguous arr3
, Element arr1 a
, Element arr2 b
, Element arr3 c
) => (a -> b -> c)
-> arr1 a
-> arr2 b
-> arr3 c
zipWith :: (a -> b -> c) -> arr1 a -> arr2 b -> arr3 c
zipWith a -> b -> c
f = (Int -> a -> b -> c) -> arr1 a -> arr2 b -> arr3 c
forall (arr1 :: * -> *) (arr2 :: * -> *) (arr3 :: * -> *) a b c.
(Contiguous arr1, Contiguous arr2, Contiguous arr3, Element arr1 a,
Element arr2 b, Element arr3 c) =>
(Int -> a -> b -> c) -> arr1 a -> arr2 b -> arr3 c
izipWith (\Int
_ a
a b
b -> a -> b -> c
f a
a b
b)
{-# inline zipWith #-}
izipWith ::
( Contiguous arr1
, Contiguous arr2
, Contiguous arr3
, Element arr1 a
, Element arr2 b
, Element arr3 c
) => (Int -> a -> b -> c)
-> arr1 a
-> arr2 b
-> arr3 c
izipWith :: (Int -> a -> b -> c) -> arr1 a -> arr2 b -> arr3 c
izipWith Int -> a -> b -> c
f arr1 a
as arr2 b
bs = (forall s. ST s (Mutable arr3 s c)) -> arr3 c
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr3 s c)) -> arr3 c)
-> (forall s. ST s (Mutable arr3 s c)) -> arr3 c
forall a b. (a -> b) -> a -> b
$ do
let !sz :: Int
sz = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
as) (arr2 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr2 b
bs)
!Mutable arr3 s c
marr <- Int -> ST s (Mutable arr3 (PrimState (ST s)) c)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new Int
sz
let go :: Int -> ST s ()
go !Int
ix = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
a <- arr1 a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 a
as Int
ix
b
b <- arr2 b -> Int -> ST s b
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr2 b
bs Int
ix
let !g :: c
g = Int -> a -> b -> c
f Int
ix a
a b
b
Mutable arr3 (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr3 s c
Mutable arr3 (PrimState (ST s)) c
marr Int
ix c
g
Int -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> ST s ()
go Int
0
Mutable arr3 s c -> ST s (Mutable arr3 s c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr3 s c
marr
{-# inline izipWith #-}
foldrZipWith ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (a -> b -> c -> c)
-> c
-> arr1 a
-> arr2 b
-> c
foldrZipWith :: (a -> b -> c -> c) -> c -> arr1 a -> arr2 b -> c
foldrZipWith a -> b -> c -> c
f = (Int -> a -> b -> c -> c) -> c -> arr1 a -> arr2 b -> c
forall (arr1 :: * -> *) (arr2 :: * -> *) a b c.
(Contiguous arr1, Contiguous arr2, Element arr1 a,
Element arr2 b) =>
(Int -> a -> b -> c -> c) -> c -> arr1 a -> arr2 b -> c
ifoldrZipWith (\Int
_ a
x b
y c
c -> a -> b -> c -> c
f a
x b
y c
c)
{-# inline foldrZipWith #-}
foldlZipWithM' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Monad m
) => (c -> a -> b -> m c)
-> c
-> arr1 a
-> arr2 b
-> m c
foldlZipWithM' :: (c -> a -> b -> m c) -> c -> arr1 a -> arr2 b -> m c
foldlZipWithM' c -> a -> b -> m c
f = (Int -> c -> a -> b -> m c) -> c -> arr1 a -> arr2 b -> m c
forall (arr1 :: * -> *) (arr2 :: * -> *) a b (m :: * -> *) c.
(Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b,
Monad m) =>
(Int -> c -> a -> b -> m c) -> c -> arr1 a -> arr2 b -> m c
ifoldlZipWithM' (\Int
_ c
x a
y b
c -> c -> a -> b -> m c
f c
x a
y b
c)
{-# inline foldlZipWithM' #-}
ifoldrZipWith ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
) => (Int -> a -> b -> c -> c)
-> c
-> arr1 a
-> arr2 b
-> c
ifoldrZipWith :: (Int -> a -> b -> c -> c) -> c -> arr1 a -> arr2 b -> c
ifoldrZipWith Int -> a -> b -> c -> c
f c
z = \arr1 a
arr1 arr2 b
arr2 ->
let !sz :: Int
sz = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr1) (arr2 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr2 b
arr2)
go :: Int -> c
go !Int
ix = if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ix
then case arr1 a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr1 a
arr1 Int
ix of
(# a
x #) -> case arr2 b -> Int -> (# b #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr2 b
arr2 Int
ix of
(# b
y #) -> Int -> a -> b -> c -> c
f Int
ix a
x b
y (Int -> c
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
else c
z
in Int -> c
go Int
0
{-# inline ifoldrZipWith #-}
ifoldlZipWithM' ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 a
, Element arr2 b
, Monad m
) => (Int -> c -> a -> b -> m c)
-> c
-> arr1 a
-> arr2 b
-> m c
ifoldlZipWithM' :: (Int -> c -> a -> b -> m c) -> c -> arr1 a -> arr2 b -> m c
ifoldlZipWithM' Int -> c -> a -> b -> m c
f c
z = \arr1 a
arr1 arr2 b
arr2 ->
let !sz :: Int
sz = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (arr1 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 a
arr1) (arr2 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr2 b
arr2)
go :: Int -> c -> m c
go !Int
ix !c
acc = if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ix
then case arr1 a -> Int -> (# a #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr1 a
arr1 Int
ix of
(# a
x #) -> case arr2 b -> Int -> (# b #)
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int -> (# b #)
index# arr2 b
arr2 Int
ix of
(# b
y #) -> do
c
acc' <- Int -> c -> a -> b -> m c
f Int
ix c
acc a
x b
y
Int -> c -> m c
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) c
acc'
else c -> m c
forall (f :: * -> *) a. Applicative f => a -> f a
pure c
acc
in Int -> c -> m c
go Int
0 c
z
{-# inline ifoldlZipWithM' #-}
zip ::
( Contiguous arr1
, Contiguous arr2
, Contiguous arr3
, Element arr1 a
, Element arr2 b
, Element arr3 (a, b)
) => arr1 a
-> arr2 b
-> arr3 (a, b)
zip :: arr1 a -> arr2 b -> arr3 (a, b)
zip = (a -> b -> (a, b)) -> arr1 a -> arr2 b -> arr3 (a, b)
forall (arr1 :: * -> *) (arr2 :: * -> *) (arr3 :: * -> *) a b c.
(Contiguous arr1, Contiguous arr2, Contiguous arr3, Element arr1 a,
Element arr2 b, Element arr3 c) =>
(a -> b -> c) -> arr1 a -> arr2 b -> arr3 c
zipWith (,)
{-# inline zip #-}
(<$) ::
( Contiguous arr1
, Contiguous arr2
, Element arr1 b
, Element arr2 a
) => a -> arr1 b -> arr2 a
a
a <$ :: a -> arr1 b -> arr2 a
<$ arr1 b
barr = (forall s. ST s (Mutable arr2 s a)) -> arr2 a
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create (Int -> a -> ST s (Mutable arr2 (PrimState (ST s)) a)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> b -> m (Mutable arr (PrimState m) b)
replicateMutable (arr1 b -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 b
barr) a
a)
{-# inline (<$) #-}
ap ::
( Contiguous arr1
, Contiguous arr2
, Contiguous arr3
, Element arr1 (a -> b)
, Element arr2 a
, Element arr3 b
) => arr1 (a -> b) -> arr2 a -> arr3 b
ap :: arr1 (a -> b) -> arr2 a -> arr3 b
ap arr1 (a -> b)
fs arr2 a
xs = (forall s. ST s (Mutable arr3 s b)) -> arr3 b
forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(forall s. ST s (Mutable arr s a)) -> arr a
create ((forall s. ST s (Mutable arr3 s b)) -> arr3 b)
-> (forall s. ST s (Mutable arr3 s b)) -> arr3 b
forall a b. (a -> b) -> a -> b
$ do
Mutable arr3 s b
marr <- Int -> ST s (Mutable arr3 (PrimState (ST s)) b)
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Int -> m (Mutable arr (PrimState m) b)
new (Int
szfs Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
szxs)
let go1 :: Int -> ST s ()
go1 !Int
ix = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szfs) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a -> b
f <- arr1 (a -> b) -> Int -> ST s (a -> b)
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr1 (a -> b)
fs Int
ix
Int -> (a -> b) -> Int -> ST s ()
go2 (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
szxs) a -> b
f Int
0
Int -> ST s ()
go1 (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
go2 :: Int -> (a -> b) -> Int -> ST s ()
go2 !Int
off a -> b
f !Int
j = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szxs) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- arr2 a -> Int -> ST s a
forall (arr :: * -> *) b (m :: * -> *).
(Contiguous arr, Element arr b, Monad m) =>
arr b -> Int -> m b
indexM arr2 a
xs Int
j
Mutable arr3 (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (arr :: * -> *) (m :: * -> *) b.
(Contiguous arr, PrimMonad m, Element arr b) =>
Mutable arr (PrimState m) b -> Int -> b -> m ()
write Mutable arr3 s b
Mutable arr3 (PrimState (ST s)) b
marr (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
j) (a -> b
f a
x)
Int -> (a -> b) -> Int -> ST s ()
go2 Int
off a -> b
f (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int -> ST s ()
go1 Int
0
Mutable arr3 s b -> ST s (Mutable arr3 s b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Mutable arr3 s b
marr
where
!szfs :: Int
szfs = arr1 (a -> b) -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr1 (a -> b)
fs
!szxs :: Int
szxs = arr2 a -> Int
forall (arr :: * -> *) b.
(Contiguous arr, Element arr b) =>
arr b -> Int
size arr2 a
xs
{-# inline ap #-}
all :: (Contiguous arr, Element arr a) => (a -> Bool) -> arr a -> Bool
all :: (a -> Bool) -> arr a -> Bool
all a -> Bool
f = (a -> Bool -> Bool) -> Bool -> arr a -> Bool
forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> arr a -> b
foldr (\a
x Bool
acc -> a -> Bool
f a
x Bool -> Bool -> Bool
&& Bool
acc) Bool
True
{-# inline all #-}
any :: (Contiguous arr, Element arr a) => (a -> Bool) -> arr a -> Bool
any :: (a -> Bool) -> arr a -> Bool
any a -> Bool
f = (a -> Bool -> Bool) -> Bool -> arr a -> Bool
forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> arr a -> b
foldr (\a
x Bool
acc -> a -> Bool
f a
x Bool -> Bool -> Bool
|| Bool
acc) Bool
False
{-# inline any #-}