{-# language BangPatterns #-}
{-# language MagicHash #-}
{-# language RankNTypes #-}
{-# language ScopedTypeVariables #-}
{-# language TypeFamilies #-}
{-# language UnboxedTuples #-}
module Data.Primitive.Unlifted.Array
(
UnliftedArray(..)
, MutableUnliftedArray(..)
, newUnliftedArray
, unsafeNewUnliftedArray
, sizeofUnliftedArray
, sizeofMutableUnliftedArray
, sameMutableUnliftedArray
, writeUnliftedArray
, readUnliftedArray
, indexUnliftedArray
, unsafeFreezeUnliftedArray
, freezeUnliftedArray
, thawUnliftedArray
, setUnliftedArray
, copyUnliftedArray
, copyMutableUnliftedArray
, cloneUnliftedArray
, cloneMutableUnliftedArray
, emptyUnliftedArray
, singletonUnliftedArray
, runUnliftedArray
, unliftedArrayToList
, unliftedArrayFromList
, unliftedArrayFromListN
, foldrUnliftedArray
, foldrUnliftedArray'
, foldlUnliftedArray
, foldlUnliftedArray'
, foldlUnliftedArrayM'
, traverseUnliftedArray_
, itraverseUnliftedArray_
, mapUnliftedArray
) where
import Control.Monad.Primitive (PrimMonad,PrimState,primitive,primitive_)
import Control.Monad.ST (ST)
import Data.Primitive.Unlifted.Class (PrimUnlifted)
import GHC.Exts (Int(I#),MutableArrayArray#,ArrayArray#,State#)
import qualified Data.List as L
import qualified Data.Primitive.Unlifted.Class as C
import qualified GHC.Exts as Exts
import qualified GHC.ST as ST
data MutableUnliftedArray s a
= MutableUnliftedArray (MutableArrayArray# s)
data UnliftedArray a
= UnliftedArray ArrayArray#
unsafeNewUnliftedArray
:: (PrimMonad m)
=> Int
-> m (MutableUnliftedArray (PrimState m) a)
{-# inline unsafeNewUnliftedArray #-}
unsafeNewUnliftedArray :: Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray (I# Int#
i#) = (State# (PrimState m)
-> (# State# (PrimState m),
MutableUnliftedArray (PrimState m) a #))
-> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m),
MutableUnliftedArray (PrimState m) a #))
-> m (MutableUnliftedArray (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m),
MutableUnliftedArray (PrimState m) a #))
-> m (MutableUnliftedArray (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case Int#
-> State# (PrimState m)
-> (# State# (PrimState m), MutableArrayArray# (PrimState m) #)
forall d. Int# -> State# d -> (# State# d, MutableArrayArray# d #)
Exts.newArrayArray# Int#
i# State# (PrimState m)
s of
(# State# (PrimState m)
s', MutableArrayArray# (PrimState m)
maa# #) -> (# State# (PrimState m)
s', MutableArrayArray# (PrimState m)
-> MutableUnliftedArray (PrimState m) a
forall s a. MutableArrayArray# s -> MutableUnliftedArray s a
MutableUnliftedArray MutableArrayArray# (PrimState m)
maa# #)
newUnliftedArray
:: (PrimMonad m, PrimUnlifted a)
=> Int
-> a
-> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray :: Int -> a -> m (MutableUnliftedArray (PrimState m) a)
newUnliftedArray Int
len a
v = do
MutableUnliftedArray (PrimState m) a
mua <- Int -> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray Int
len
MutableUnliftedArray (PrimState m) a -> a -> Int -> Int -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> a -> Int -> Int -> m ()
setUnliftedArray MutableUnliftedArray (PrimState m) a
mua a
v Int
0 Int
len
MutableUnliftedArray (PrimState m) a
-> m (MutableUnliftedArray (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutableUnliftedArray (PrimState m) a
mua
{-# inline newUnliftedArray #-}
setUnliftedArray
:: (PrimMonad m, PrimUnlifted a)
=> MutableUnliftedArray (PrimState m) a
-> a
-> Int
-> Int
-> m ()
{-# inline setUnliftedArray #-}
setUnliftedArray :: MutableUnliftedArray (PrimState m) a -> a -> Int -> Int -> m ()
setUnliftedArray MutableUnliftedArray (PrimState m) a
mua a
v Int
off Int
len = Int -> m ()
loop (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
loop :: Int -> m ()
loop Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
off = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray (PrimState m) a
mua Int
i a
v m () -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> m ()
loop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
sizeofUnliftedArray :: UnliftedArray e -> Int
{-# inline sizeofUnliftedArray #-}
sizeofUnliftedArray :: UnliftedArray e -> Int
sizeofUnliftedArray (UnliftedArray ArrayArray#
aa#) = Int# -> Int
I# (ArrayArray# -> Int#
Exts.sizeofArrayArray# ArrayArray#
aa#)
sizeofMutableUnliftedArray :: MutableUnliftedArray s e -> Int
{-# inline sizeofMutableUnliftedArray #-}
sizeofMutableUnliftedArray :: MutableUnliftedArray s e -> Int
sizeofMutableUnliftedArray (MutableUnliftedArray MutableArrayArray# s
maa#)
= Int# -> Int
I# (MutableArrayArray# s -> Int#
forall d. MutableArrayArray# d -> Int#
Exts.sizeofMutableArrayArray# MutableArrayArray# s
maa#)
writeUnliftedArray :: (PrimMonad m, PrimUnlifted a)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> a
-> m ()
{-# inline writeUnliftedArray #-}
writeUnliftedArray :: MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray (MutableUnliftedArray MutableArrayArray# (PrimState m)
arr) (I# Int#
ix) a
a =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (MutableArrayArray# (PrimState m)
-> Int# -> a -> State# (PrimState m) -> State# (PrimState m)
forall a s.
PrimUnlifted a =>
MutableArrayArray# s -> Int# -> a -> State# s -> State# s
C.writeUnliftedArray# MutableArrayArray# (PrimState m)
arr Int#
ix a
a)
readUnliftedArray :: (PrimMonad m, PrimUnlifted a)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> m a
{-# inline readUnliftedArray #-}
readUnliftedArray :: MutableUnliftedArray (PrimState m) a -> Int -> m a
readUnliftedArray (MutableUnliftedArray MutableArrayArray# (PrimState m)
arr) (I# Int#
ix) =
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (MutableArrayArray# (PrimState m)
-> Int# -> State# (PrimState m) -> (# State# (PrimState m), a #)
forall a s.
PrimUnlifted a =>
MutableArrayArray# s -> Int# -> State# s -> (# State# s, a #)
C.readUnliftedArray# MutableArrayArray# (PrimState m)
arr Int#
ix)
indexUnliftedArray :: PrimUnlifted a
=> UnliftedArray a
-> Int
-> a
{-# inline indexUnliftedArray #-}
indexUnliftedArray :: UnliftedArray a -> Int -> a
indexUnliftedArray (UnliftedArray ArrayArray#
arr) (I# Int#
ix) =
ArrayArray# -> Int# -> a
forall a. PrimUnlifted a => ArrayArray# -> Int# -> a
C.indexUnliftedArray# ArrayArray#
arr Int#
ix
unsafeFreezeUnliftedArray
:: (PrimMonad m)
=> MutableUnliftedArray (PrimState m) a
-> m (UnliftedArray a)
unsafeFreezeUnliftedArray :: MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray (MutableUnliftedArray MutableArrayArray# (PrimState m)
maa#)
= (State# (PrimState m)
-> (# State# (PrimState m), UnliftedArray a #))
-> m (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), UnliftedArray a #))
-> m (UnliftedArray a))
-> (State# (PrimState m)
-> (# State# (PrimState m), UnliftedArray a #))
-> m (UnliftedArray a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case MutableArrayArray# (PrimState m)
-> State# (PrimState m) -> (# State# (PrimState m), ArrayArray# #)
forall d.
MutableArrayArray# d -> State# d -> (# State# d, ArrayArray# #)
Exts.unsafeFreezeArrayArray# MutableArrayArray# (PrimState m)
maa# State# (PrimState m)
s of
(# State# (PrimState m)
s', ArrayArray#
aa# #) -> (# State# (PrimState m)
s', ArrayArray# -> UnliftedArray a
forall a. ArrayArray# -> UnliftedArray a
UnliftedArray ArrayArray#
aa# #)
{-# inline unsafeFreezeUnliftedArray #-}
sameMutableUnliftedArray
:: MutableUnliftedArray s a
-> MutableUnliftedArray s a
-> Bool
sameMutableUnliftedArray :: MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
sameMutableUnliftedArray (MutableUnliftedArray MutableArrayArray# s
maa1#) (MutableUnliftedArray MutableArrayArray# s
maa2#)
= Int# -> Bool
Exts.isTrue# (MutableArrayArray# s -> MutableArrayArray# s -> Int#
forall d. MutableArrayArray# d -> MutableArrayArray# d -> Int#
Exts.sameMutableArrayArray# MutableArrayArray# s
maa1# MutableArrayArray# s
maa2#)
{-# inline sameMutableUnliftedArray #-}
copyUnliftedArray
:: (PrimMonad m)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> UnliftedArray a
-> Int
-> Int
-> m ()
{-# inline copyUnliftedArray #-}
copyUnliftedArray :: MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray
(MutableUnliftedArray MutableArrayArray# (PrimState m)
dst) (I# Int#
doff)
(UnliftedArray ArrayArray#
src) (I# Int#
soff) (I# Int#
ln) =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ ((State# (PrimState m) -> State# (PrimState m)) -> m ())
-> (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayArray#
-> Int#
-> MutableArrayArray# (PrimState m)
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
ArrayArray#
-> Int#
-> MutableArrayArray# d
-> Int#
-> Int#
-> State# d
-> State# d
Exts.copyArrayArray# ArrayArray#
src Int#
soff MutableArrayArray# (PrimState m)
dst Int#
doff Int#
ln
copyMutableUnliftedArray
:: (PrimMonad m)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
{-# inline copyMutableUnliftedArray #-}
copyMutableUnliftedArray :: MutableUnliftedArray (PrimState m) a
-> Int
-> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutableUnliftedArray
(MutableUnliftedArray MutableArrayArray# (PrimState m)
dst) (I# Int#
doff)
(MutableUnliftedArray MutableArrayArray# (PrimState m)
src) (I# Int#
soff) (I# Int#
ln) =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ ((State# (PrimState m) -> State# (PrimState m)) -> m ())
-> (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall a b. (a -> b) -> a -> b
$ MutableArrayArray# (PrimState m)
-> Int#
-> MutableArrayArray# (PrimState m)
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d.
MutableArrayArray# d
-> Int#
-> MutableArrayArray# d
-> Int#
-> Int#
-> State# d
-> State# d
Exts.copyMutableArrayArray# MutableArrayArray# (PrimState m)
src Int#
soff MutableArrayArray# (PrimState m)
dst Int#
doff Int#
ln
freezeUnliftedArray
:: (PrimMonad m)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m (UnliftedArray a)
freezeUnliftedArray :: MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (UnliftedArray a)
freezeUnliftedArray MutableUnliftedArray (PrimState m) a
src Int
off Int
len = 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
len
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
off Int
len
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
unsafeFreezeUnliftedArray MutableUnliftedArray (PrimState m) a
dst
{-# inline freezeUnliftedArray #-}
thawUnliftedArray
:: (PrimMonad m)
=> UnliftedArray a
-> Int
-> Int
-> m (MutableUnliftedArray (PrimState m) a)
{-# inline thawUnliftedArray #-}
thawUnliftedArray :: UnliftedArray a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
thawUnliftedArray UnliftedArray a
src Int
off Int
len = 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
len
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray MutableUnliftedArray (PrimState m) a
dst Int
0 UnliftedArray a
src Int
off Int
len
MutableUnliftedArray (PrimState m) a
-> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutableUnliftedArray (PrimState m) a
dst
unsafeCreateUnliftedArray
:: Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
unsafeCreateUnliftedArray :: Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
unsafeCreateUnliftedArray !Int
n forall s. MutableUnliftedArray s a -> ST s ()
f = (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a.
(forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
runUnliftedArray ((forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a)
-> (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ do
MutableUnliftedArray s a
mary <- Int -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray Int
n
MutableUnliftedArray s a -> ST s ()
forall s. MutableUnliftedArray s a -> ST s ()
f MutableUnliftedArray s a
mary
MutableUnliftedArray s a -> ST s (MutableUnliftedArray s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutableUnliftedArray s a
mary
runUnliftedArray
:: (forall s. ST s (MutableUnliftedArray s a))
-> UnliftedArray a
{-# INLINE runUnliftedArray #-}
runUnliftedArray :: (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
runUnliftedArray forall s. ST s (MutableUnliftedArray s a)
m = ArrayArray# -> UnliftedArray a
forall a. ArrayArray# -> UnliftedArray a
UnliftedArray ((forall s. ST s (MutableUnliftedArray s a)) -> ArrayArray#
forall a.
(forall s. ST s (MutableUnliftedArray s a)) -> ArrayArray#
runUnliftedArray# forall s. ST s (MutableUnliftedArray s a)
m)
runUnliftedArray#
:: (forall s. ST s (MutableUnliftedArray s a))
-> ArrayArray#
runUnliftedArray# :: (forall s. ST s (MutableUnliftedArray s a)) -> ArrayArray#
runUnliftedArray# forall s. ST s (MutableUnliftedArray s a)
m = case (State# RealWorld -> (# State# RealWorld, ArrayArray# #))
-> (# State# RealWorld, ArrayArray# #)
forall o. (State# RealWorld -> o) -> o
Exts.runRW# ((State# RealWorld -> (# State# RealWorld, ArrayArray# #))
-> (# State# RealWorld, ArrayArray# #))
-> (State# RealWorld -> (# State# RealWorld, ArrayArray# #))
-> (# State# RealWorld, ArrayArray# #)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case ST RealWorld (MutableUnliftedArray RealWorld a)
-> State# RealWorld
-> (# State# RealWorld, MutableUnliftedArray RealWorld a #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST ST RealWorld (MutableUnliftedArray RealWorld a)
forall s. ST s (MutableUnliftedArray s a)
m State# RealWorld
s of { (# State# RealWorld
s', MutableUnliftedArray MutableArrayArray# RealWorld
mary# #) ->
MutableArrayArray# RealWorld
-> State# RealWorld -> (# State# RealWorld, ArrayArray# #)
forall d.
MutableArrayArray# d -> State# d -> (# State# d, ArrayArray# #)
Exts.unsafeFreezeArrayArray# MutableArrayArray# RealWorld
mary# State# RealWorld
s'} of (# State# RealWorld
_, ArrayArray#
ary# #) -> ArrayArray#
ary#
unST :: ST s a -> State# s -> (# State# s, a #)
unST :: ST s a -> State# s -> (# State# s, a #)
unST (ST.ST State# s -> (# State# s, a #)
f) = State# s -> (# State# s, a #)
f
cloneUnliftedArray
:: UnliftedArray a
-> Int
-> Int
-> UnliftedArray a
{-# inline cloneUnliftedArray #-}
cloneUnliftedArray :: UnliftedArray a -> Int -> Int -> UnliftedArray a
cloneUnliftedArray UnliftedArray a
src Int
off Int
len =
(forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a.
(forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
runUnliftedArray (UnliftedArray a
-> Int -> Int -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
UnliftedArray a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
thawUnliftedArray UnliftedArray a
src Int
off Int
len)
cloneMutableUnliftedArray
:: (PrimMonad m)
=> MutableUnliftedArray (PrimState m) a
-> Int
-> Int
-> m (MutableUnliftedArray (PrimState m) a)
{-# inline cloneMutableUnliftedArray #-}
cloneMutableUnliftedArray :: MutableUnliftedArray (PrimState m) a
-> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
cloneMutableUnliftedArray MutableUnliftedArray (PrimState m) a
src Int
off Int
len = 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
len
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
off Int
len
MutableUnliftedArray (PrimState m) a
-> m (MutableUnliftedArray (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return MutableUnliftedArray (PrimState m) a
dst
emptyUnliftedArray :: UnliftedArray a
emptyUnliftedArray :: UnliftedArray a
emptyUnliftedArray = (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a.
(forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
runUnliftedArray (Int -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray Int
0)
{-# NOINLINE emptyUnliftedArray #-}
singletonUnliftedArray :: PrimUnlifted a => a -> UnliftedArray a
{-# INLINE singletonUnliftedArray #-}
singletonUnliftedArray :: a -> UnliftedArray a
singletonUnliftedArray a
x = (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a.
(forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
runUnliftedArray ((forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a)
-> (forall s. ST s (MutableUnliftedArray s a)) -> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ do
MutableUnliftedArray s a
dst <- Int -> ST s (MutableUnliftedArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MutableUnliftedArray (PrimState m) a)
unsafeNewUnliftedArray Int
1
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
dst Int
0 a
x
MutableUnliftedArray s a -> ST s (MutableUnliftedArray s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MutableUnliftedArray s a
dst
concatUnliftedArray :: UnliftedArray a -> UnliftedArray a -> UnliftedArray a
{-# INLINE concatUnliftedArray #-}
concatUnliftedArray :: UnliftedArray a -> UnliftedArray a -> UnliftedArray a
concatUnliftedArray UnliftedArray a
x UnliftedArray a
y = Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
forall a.
Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
unsafeCreateUnliftedArray (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
y) ((forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a)
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
forall a b. (a -> b) -> a -> b
$ \MutableUnliftedArray s a
m -> do
MutableUnliftedArray (PrimState (ST s)) a
-> Int -> UnliftedArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m Int
0 UnliftedArray a
x Int
0 (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
x)
MutableUnliftedArray (PrimState (ST s)) a
-> Int -> UnliftedArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
MutableUnliftedArray (PrimState m) a
-> Int -> UnliftedArray a -> Int -> Int -> m ()
copyUnliftedArray MutableUnliftedArray s a
MutableUnliftedArray (PrimState (ST s)) a
m (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
x) UnliftedArray a
y Int
0 (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
y)
foldrUnliftedArray :: forall a b. PrimUnlifted a => (a -> b -> b) -> b -> UnliftedArray a -> b
{-# INLINE foldrUnliftedArray #-}
foldrUnliftedArray :: (a -> b -> b) -> b -> UnliftedArray a -> b
foldrUnliftedArray a -> b -> b
f b
z UnliftedArray a
arr = Int -> b
go Int
0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
go :: Int -> b
go !Int
i
| Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
i = a -> b -> b
f (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i) (Int -> b
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1))
| Bool
otherwise = b
z
{-# INLINE foldrUnliftedArray' #-}
foldrUnliftedArray' :: forall a b. PrimUnlifted a => (a -> b -> b) -> b -> UnliftedArray a -> b
foldrUnliftedArray' :: (a -> b -> b) -> b -> UnliftedArray a -> b
foldrUnliftedArray' a -> b -> b
f b
z0 UnliftedArray a
arr = Int -> b -> b
go (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) b
z0
where
go :: Int -> b -> b
go !Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = b
acc
| Bool
otherwise = Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (a -> b -> b
f (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i) b
acc)
{-# INLINE foldlUnliftedArray #-}
foldlUnliftedArray :: forall a b. PrimUnlifted a => (b -> a -> b) -> b -> UnliftedArray a -> b
foldlUnliftedArray :: (b -> a -> b) -> b -> UnliftedArray a -> b
foldlUnliftedArray b -> a -> b
f b
z UnliftedArray a
arr = Int -> b
go (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
go :: Int -> b
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = b
z
| Bool
otherwise = b -> a -> b
f (Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i)
{-# INLINE foldlUnliftedArray' #-}
foldlUnliftedArray' :: forall a b. PrimUnlifted a => (b -> a -> b) -> b -> UnliftedArray a -> b
foldlUnliftedArray' :: (b -> a -> b) -> b -> UnliftedArray a -> b
foldlUnliftedArray' b -> a -> b
f b
z0 UnliftedArray a
arr = Int -> b -> b
go Int
0 b
z0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
go :: Int -> b -> b
go !Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> a -> b
f b
acc (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i))
| Bool
otherwise = b
acc
{-# INLINE foldlUnliftedArrayM' #-}
foldlUnliftedArrayM' :: (PrimUnlifted a, Monad m)
=> (b -> a -> m b) -> b -> UnliftedArray a -> m b
foldlUnliftedArrayM' :: (b -> a -> m b) -> b -> UnliftedArray a -> m b
foldlUnliftedArrayM' b -> a -> m b
f b
z0 UnliftedArray a
arr = Int -> b -> m b
go Int
0 b
z0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
go :: Int -> b -> m b
go !Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = b -> a -> m b
f b
acc (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i) m b -> (b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> b -> m b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
acc
{-# INLINE traverseUnliftedArray_ #-}
traverseUnliftedArray_ :: (PrimUnlifted a, Applicative m)
=> (a -> m b) -> UnliftedArray a -> m ()
traverseUnliftedArray_ :: (a -> m b) -> UnliftedArray a -> m ()
traverseUnliftedArray_ a -> m b
f UnliftedArray a
arr = Int -> m ()
go Int
0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
go :: Int -> m ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = a -> m b
f (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i) m b -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> m ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE itraverseUnliftedArray_ #-}
itraverseUnliftedArray_ :: (PrimUnlifted a, Applicative m)
=> (Int -> a -> m b) -> UnliftedArray a -> m ()
itraverseUnliftedArray_ :: (Int -> a -> m b) -> UnliftedArray a -> m ()
itraverseUnliftedArray_ Int -> a -> m b
f UnliftedArray a
arr = Int -> m ()
go Int
0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
go :: Int -> m ()
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz = Int -> a -> m b
f Int
i (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
i) m b -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> m ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE mapUnliftedArray #-}
mapUnliftedArray :: (PrimUnlifted a, PrimUnlifted b)
=> (a -> b)
-> UnliftedArray a
-> UnliftedArray b
mapUnliftedArray :: (a -> b) -> UnliftedArray a -> UnliftedArray b
mapUnliftedArray a -> b
f UnliftedArray a
arr = Int
-> (forall s. MutableUnliftedArray s b -> ST s ())
-> UnliftedArray b
forall a.
Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
unsafeCreateUnliftedArray Int
sz ((forall s. MutableUnliftedArray s b -> ST s ())
-> UnliftedArray b)
-> (forall s. MutableUnliftedArray s b -> ST s ())
-> UnliftedArray b
forall a b. (a -> b) -> a -> b
$ \MutableUnliftedArray s b
marr -> do
let go :: Int -> ST s ()
go !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz
then do
let b :: b
b = a -> b
f (UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
arr Int
ix)
MutableUnliftedArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, PrimUnlifted a) =>
MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
writeUnliftedArray MutableUnliftedArray s b
MutableUnliftedArray (PrimState (ST s)) b
marr Int
ix b
b
Int -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Int -> ST s ()
go Int
0
where
!sz :: Int
sz = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
arr
{-# INLINE unliftedArrayToList #-}
unliftedArrayToList :: PrimUnlifted a => UnliftedArray a -> [a]
unliftedArrayToList :: UnliftedArray a -> [a]
unliftedArrayToList UnliftedArray a
xs = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
Exts.build (\a -> b -> b
c b
n -> (a -> b -> b) -> b -> UnliftedArray a -> b
forall a b.
PrimUnlifted a =>
(a -> b -> b) -> b -> UnliftedArray a -> b
foldrUnliftedArray a -> b -> b
c b
n UnliftedArray a
xs)
unliftedArrayFromList :: PrimUnlifted a => [a] -> UnliftedArray a
unliftedArrayFromList :: [a] -> UnliftedArray a
unliftedArrayFromList [a]
xs = Int -> [a] -> UnliftedArray a
forall a. PrimUnlifted a => Int -> [a] -> UnliftedArray a
unliftedArrayFromListN ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
L.length [a]
xs) [a]
xs
unliftedArrayFromListN :: forall a. PrimUnlifted a => Int -> [a] -> UnliftedArray a
unliftedArrayFromListN :: Int -> [a] -> UnliftedArray a
unliftedArrayFromListN Int
len [a]
vs = Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
forall a.
Int
-> (forall s. MutableUnliftedArray s a -> ST s ())
-> UnliftedArray a
unsafeCreateUnliftedArray Int
len forall s. MutableUnliftedArray s a -> ST s ()
run where
run :: forall s. MutableUnliftedArray s a -> ST s ()
run :: MutableUnliftedArray s a -> ST s ()
run MutableUnliftedArray s a
arr = do
let go :: [a] -> Int -> ST s ()
go :: [a] -> Int -> ST s ()
go [] !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len
then () -> ST s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"unliftedArrayFromListN" String
"list length less than specified size"
go (a
a : [a]
as) !Int
ix = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then do
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
arr Int
ix a
a
[a] -> Int -> ST s ()
go [a]
as (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"unliftedArrayFromListN" String
"list length greater than specified size"
[a] -> Int -> ST s ()
go [a]
vs Int
0
instance PrimUnlifted a => Exts.IsList (UnliftedArray a) where
type Item (UnliftedArray a) = a
fromList :: [Item (UnliftedArray a)] -> UnliftedArray a
fromList = [Item (UnliftedArray a)] -> UnliftedArray a
forall a. PrimUnlifted a => [a] -> UnliftedArray a
unliftedArrayFromList
fromListN :: Int -> [Item (UnliftedArray a)] -> UnliftedArray a
fromListN = Int -> [Item (UnliftedArray a)] -> UnliftedArray a
forall a. PrimUnlifted a => Int -> [a] -> UnliftedArray a
unliftedArrayFromListN
toList :: UnliftedArray a -> [Item (UnliftedArray a)]
toList = UnliftedArray a -> [Item (UnliftedArray a)]
forall a. PrimUnlifted a => UnliftedArray a -> [a]
unliftedArrayToList
instance PrimUnlifted a => Semigroup (UnliftedArray a) where
<> :: UnliftedArray a -> UnliftedArray a -> UnliftedArray a
(<>) = UnliftedArray a -> UnliftedArray a -> UnliftedArray a
forall a. UnliftedArray a -> UnliftedArray a -> UnliftedArray a
concatUnliftedArray
instance PrimUnlifted a => Monoid (UnliftedArray a) where
mempty :: UnliftedArray a
mempty = UnliftedArray a
forall a. UnliftedArray a
emptyUnliftedArray
instance (Show a, PrimUnlifted a) => Show (UnliftedArray a) where
showsPrec :: Int -> UnliftedArray a -> ShowS
showsPrec Int
p UnliftedArray a
a = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"fromListN " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
a) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => a -> ShowS
shows (UnliftedArray a -> [a]
forall a. PrimUnlifted a => UnliftedArray a -> [a]
unliftedArrayToList UnliftedArray a
a)
instance Eq (MutableUnliftedArray s a) where
== :: MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
(==) = MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
forall s a.
MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
sameMutableUnliftedArray
instance (Eq a, PrimUnlifted a) => Eq (UnliftedArray a) where
UnliftedArray a
aa1 == :: UnliftedArray a -> UnliftedArray a -> Bool
== UnliftedArray a
aa2 = UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
aa1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
aa2
Bool -> Bool -> Bool
&& Int -> Bool
loop (UnliftedArray a -> Int
forall e. UnliftedArray e -> Int
sizeofUnliftedArray UnliftedArray a
aa1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
loop :: Int -> Bool
loop Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
True
| Bool
otherwise = UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
aa1 Int
i a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== UnliftedArray a -> Int -> a
forall a. PrimUnlifted a => UnliftedArray a -> Int -> a
indexUnliftedArray UnliftedArray a
aa2 Int
i Bool -> Bool -> Bool
&& Int -> Bool
loop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
die :: String -> String -> a
die :: String -> String -> a
die String
fun String
problem = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Data.Primitive.UnliftedArray." String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
fun String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
problem