{-# LANGUAGE UnboxedTuples #-}
module Streamly.Internal.Data.Array.Foreign.Mut.Type
(
Array (..)
, ArrayContents
, arrayToFptrContents
, fptrToArrayContents
, nilArrayContents
, touch
, nil
, newArray
, newArrayAligned
, newArrayAlignedUnmanaged
, newArrayWith
, withNewArrayUnsafe
, ArrayUnsafe (..)
, writeNWithUnsafe
, writeNWith
, writeNUnsafe
, writeN
, writeNAligned
, writeNAlignedUnmanaged
, writeWith
, write
, fromForeignPtrUnsafe
, fromListN
, fromList
, fromStreamDN
, fromStreamD
, putIndex
, putIndexUnsafe
, putIndices
, modifyIndexUnsafe
, modifyIndex
, modifyIndices
, modify
, swapIndices
, unsafeSwapIndices
, snocWith
, snoc
, snocLinear
, snocMay
, snocUnsafe
, appendNUnsafe
, appendN
, appendWith
, append
, ReadUState(..)
, read
, readRev
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toList
, producer
, getIndex
, getIndexUnsafe
, getIndices
, getIndicesD
, getIndexRev
, blockSize
, arrayChunkBytes
, allocBytesToElemCount
, realloc
, resize
, resizeExp
, rightSize
, length
, byteLength
, byteCapacity
, bytesFree
, reverse
, permute
, partitionBy
, shuffleBy
, divideBy
, mergeBy
, cast
, castUnsafe
, asBytes
, asPtrUnsafe
, foldl'
, foldr
, cmp
, arraysOf
, arrayStreamKFromStreamD
, writeChunks
, flattenArrays
, flattenArraysRev
, fromArrayStreamK
, getSliceUnsafe
, getSlice
, splitAt
, breakOn
, spliceCopy
, spliceWith
, splice
, spliceExp
, roundUpToPower2
, memcpy
, memcmp
, c_memchr
)
where
#include "inline.hs"
#include "ArrayMacros.h"
#include "MachDeps.h"
#ifdef USE_C_MALLOC
#define USE_FOREIGN_PTR
#endif
import Control.Exception (assert)
import Control.DeepSeq (NFData(..))
import Control.Monad (when, void)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Bits (shiftR, (.|.), (.&.))
#if __GLASGOW_HASKELL__ < 808
import Data.Semigroup (Semigroup(..))
#endif
import Data.Word (Word8)
import Foreign.C.Types (CSize(..), CInt(..))
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
#ifndef USE_FOREIGN_PTR
import Foreign.Marshal.Alloc (mallocBytes)
#endif
import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
import Foreign.Storable (Storable(..))
import GHC.Base
( touch#, IO(..), byteArrayContents#
, Int(..), newAlignedPinnedByteArray#
)
#ifndef USE_FOREIGN_PTR
import GHC.Base (RealWorld, MutableByteArray#)
#endif
#if __GLASGOW_HASKELL__ < 802
#define noinline
#else
import GHC.Base (noinline)
#endif
import GHC.Exts (unsafeCoerce#)
import GHC.ForeignPtr (ForeignPtr(..), ForeignPtrContents(..))
#ifdef USE_C_MALLOC
import GHC.ForeignPtr (mallocForeignPtrAlignedBytes)
#endif
import GHC.Ptr (Ptr(..))
import Streamly.Internal.BaseCompat
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Producer.Type (Producer (..))
import Streamly.Internal.Data.Stream.Serial (SerialT(..))
import Streamly.Internal.Data.SVar.Type (adaptState, defState)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
import System.IO.Unsafe (unsafePerformIO)
#ifdef DEVBUILD
import qualified Data.Foldable as F
#endif
import qualified Streamly.Internal.Data.Fold.Type as FL
import qualified Streamly.Internal.Data.Producer as Producer
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
#ifdef USE_FOREIGN_PTR
import qualified Streamly.Internal.Foreign.Malloc as Malloc
#endif
import Prelude hiding
(length, foldr, read, unlines, splitAt, reverse, truncate)
foreign import ccall unsafe "string.h memcpy" c_memcpy
:: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)
foreign import ccall unsafe "string.h memchr" c_memchr
:: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)
foreign import ccall unsafe "string.h memcmp" c_memcmp
:: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
{-# INLINE bytesToElemCount #-}
bytesToElemCount :: forall a. Storable a => a -> Int -> Int
bytesToElemCount :: a -> Int -> Int
bytesToElemCount a
_ Int
n = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` SIZE_OF(a)
memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy Ptr Word8
dst Ptr Word8
src Int
len = IO (Ptr Word8) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)
c_memcpy Ptr Word8
dst Ptr Word8
src (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len))
{-# INLINE memcmp #-}
memcmp :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
memcmp :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
memcmp Ptr Word8
p1 Ptr Word8
p2 Int
len = do
CInt
r <- Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
c_memcmp Ptr Word8
p1 Ptr Word8
p2 (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> IO Bool) -> Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0
#ifdef USE_FOREIGN_PTR
newtype ArrayContents = ArrayContents ForeignPtrContents
#define UNPACKIF
#else
data ArrayContents = ArrayContents !(MutableByteArray# RealWorld)
#define UNPACKIF {-# UNPACK #-}
#endif
{-# INLINE touch #-}
touch :: ArrayContents -> IO ()
touch :: ArrayContents -> IO ()
touch (ArrayContents MutableByteArray# RealWorld
contents) =
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case MutableByteArray# RealWorld -> State# RealWorld -> State# RealWorld
forall a. a -> State# RealWorld -> State# RealWorld
touch# MutableByteArray# RealWorld
contents State# RealWorld
s of State# RealWorld
s' -> (# State# RealWorld
s', () #)
fptrToArrayContents :: ForeignPtrContents -> ArrayContents
arrayToFptrContents :: ArrayContents -> ForeignPtrContents
#ifdef USE_FOREIGN_PTR
fptrToArrayContents = ArrayContents
arrayToFptrContents (ArrayContents contents) = contents
#else
fptrToArrayContents :: ForeignPtrContents -> ArrayContents
fptrToArrayContents (PlainPtr MutableByteArray# RealWorld
mbarr) = MutableByteArray# RealWorld -> ArrayContents
ArrayContents MutableByteArray# RealWorld
mbarr
fptrToArrayContents ForeignPtrContents
_ = [Char] -> ArrayContents
forall a. HasCallStack => [Char] -> a
error [Char]
"Unsupported foreign ptr"
arrayToFptrContents :: ArrayContents -> ForeignPtrContents
arrayToFptrContents (ArrayContents MutableByteArray# RealWorld
contents) = MutableByteArray# RealWorld -> ForeignPtrContents
PlainPtr MutableByteArray# RealWorld
contents
#endif
data Array a =
#ifdef DEVBUILD
Storable a =>
#endif
Array
{ Array a -> ArrayContents
arrContents :: UNPACKIF !ArrayContents
, Array a -> Ptr a
arrStart :: {-# UNPACK #-} !(Ptr a)
, Array a -> Ptr a
aEnd :: {-# UNPACK #-} !(Ptr a)
, Array a -> Ptr a
aBound :: {-# UNPACK #-} !(Ptr a)
}
{-# INLINE newArrayWith #-}
newArrayWith :: forall m a. (MonadIO m, Storable a)
=> (Int -> Int -> m (ArrayContents, Ptr a)) -> Int -> Int -> m (Array a)
newArrayWith :: (Int -> Int -> m (ArrayContents, Ptr a))
-> Int -> Int -> m (Array a)
newArrayWith Int -> Int -> m (ArrayContents, Ptr a)
alloc Int
alignSize Int
count = do
let size :: Int
size = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a)) 0
(ArrayContents
contents, Ptr a
p) <- Int -> Int -> m (ArrayContents, Ptr a)
alloc Int
size Int
alignSize
Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a)) -> Array a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
contents
, arrStart :: Ptr a
arrStart = Ptr a
p
, aEnd :: Ptr a
aEnd = Ptr a
p
, aBound :: Ptr a
aBound = Ptr a
p Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size
}
newAlignedArrayContents :: Int -> Int -> IO (ArrayContents, Ptr a)
#ifdef USE_C_MALLOC
newAlignedArrayContents size align = do
(ForeignPtr addr contents) <- mallocForeignPtrAlignedBytes size align
return (ArrayContents contents, Ptr addr)
#else
newAlignedArrayContents :: Int -> Int -> IO (ArrayContents, Ptr a)
newAlignedArrayContents Int
size Int
_align | Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 =
[Char] -> IO (ArrayContents, Ptr a)
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"newAlignedArrayContents: size must be >= 0"
newAlignedArrayContents (I# Int#
size) (I# Int#
align) = (State# RealWorld
-> (# State# RealWorld, (ArrayContents, Ptr a) #))
-> IO (ArrayContents, Ptr a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld
-> (# State# RealWorld, (ArrayContents, Ptr a) #))
-> IO (ArrayContents, Ptr a))
-> (State# RealWorld
-> (# State# RealWorld, (ArrayContents, Ptr a) #))
-> IO (ArrayContents, Ptr a)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Int#
-> Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall d.
Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
newAlignedPinnedByteArray# Int#
size Int#
align State# RealWorld
s of
(# State# RealWorld
s', MutableByteArray# RealWorld
mbarr# #) ->
let p :: Ptr a
p = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents# (MutableByteArray# RealWorld -> ByteArray#
unsafeCoerce# MutableByteArray# RealWorld
mbarr#))
#ifdef USE_FOREIGN_PTR
c = ArrayContents (PlainPtr mbarr#)
#else
c :: ArrayContents
c = MutableByteArray# RealWorld -> ArrayContents
ArrayContents MutableByteArray# RealWorld
mbarr#
#endif
in (# State# RealWorld
s', (ArrayContents
c, Ptr a
forall a. Ptr a
p) #)
#endif
{-# NOINLINE nilArrayContents #-}
nilArrayContents :: ArrayContents
nilArrayContents :: ArrayContents
nilArrayContents =
(ArrayContents, Ptr Any) -> ArrayContents
forall a b. (a, b) -> a
fst ((ArrayContents, Ptr Any) -> ArrayContents)
-> (ArrayContents, Ptr Any) -> ArrayContents
forall a b. (a -> b) -> a -> b
$ IO (ArrayContents, Ptr Any) -> (ArrayContents, Ptr Any)
forall a. IO a -> a
unsafePerformIO (IO (ArrayContents, Ptr Any) -> (ArrayContents, Ptr Any))
-> IO (ArrayContents, Ptr Any) -> (ArrayContents, Ptr Any)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> IO (ArrayContents, Ptr Any)
forall a. Int -> Int -> IO (ArrayContents, Ptr a)
newAlignedArrayContents Int
0 Int
0
nil ::
#ifdef DEVBUILD
Storable a =>
#endif
Array a
nil :: Array a
nil = ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
nilArrayContents Ptr a
forall a. Ptr a
nullPtr Ptr a
forall a. Ptr a
nullPtr Ptr a
forall a. Ptr a
nullPtr
{-# INLINE fromForeignPtrUnsafe #-}
fromForeignPtrUnsafe ::
#ifdef DEVBUILD
Storable a =>
#endif
ForeignPtr a -> Ptr a -> Ptr a -> Array a
fromForeignPtrUnsafe :: ForeignPtr a -> Ptr a -> Ptr a -> Array a
fromForeignPtrUnsafe (ForeignPtr Addr#
start ForeignPtrContents
_) Ptr a
_ Ptr a
_
| Addr# -> Ptr Any
forall a. Addr# -> Ptr a
Ptr Addr#
start Ptr Any -> Ptr Any -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Any
forall a. Ptr a
nullPtr = Array a
forall a. Array a
nil
fromForeignPtrUnsafe fp :: ForeignPtr a
fp@(ForeignPtr Addr#
start ForeignPtrContents
contents) Ptr a
end Ptr a
bound =
Bool -> Array a -> Array a
forall a. HasCallStack => Bool -> a -> a
assert (ForeignPtr a -> Ptr a
forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr a
fp Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
end Bool -> Bool -> Bool
&& Ptr a
end Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
bound)
(ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array (ForeignPtrContents -> ArrayContents
fptrToArrayContents ForeignPtrContents
contents) (Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
start) Ptr a
end Ptr a
bound)
{-# INLINE newArrayAlignedUnmanaged #-}
newArrayAlignedUnmanaged :: forall m a. (MonadIO m, Storable a) =>
Int -> Int -> m (Array a)
#ifdef USE_FOREIGN_PTR
newArrayAlignedUnmanaged = do
newArrayWith mallocForeignPtrAlignedUnmanagedBytes
where
mallocForeignPtrAlignedUnmanagedBytes size align = do
ForeignPtr addr contents <-
liftIO $ Malloc.mallocForeignPtrAlignedUnmanagedBytes size align
return (ArrayContents contents, Ptr addr)
#else
newArrayAlignedUnmanaged :: Int -> Int -> m (Array a)
newArrayAlignedUnmanaged Int
_align Int
count = do
let size :: Int
size = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a)) 0
Ptr a
p <- IO (Ptr a) -> m (Ptr a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Ptr a) -> m (Ptr a)) -> IO (Ptr a) -> m (Ptr a)
forall a b. (a -> b) -> a -> b
$ Int -> IO (Ptr a)
forall a. Int -> IO (Ptr a)
mallocBytes Int
size
Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a)) -> Array a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
nilArrayContents
, arrStart :: Ptr a
arrStart = Ptr a
p
, aEnd :: Ptr a
aEnd = Ptr a
p
, aBound :: Ptr a
aBound = Ptr a
p Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size
}
#endif
{-# INLINE newArrayAligned #-}
newArrayAligned :: (MonadIO m, Storable a) => Int -> Int -> m (Array a)
newArrayAligned :: Int -> Int -> m (Array a)
newArrayAligned = (Int -> Int -> m (ArrayContents, Ptr a))
-> Int -> Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int -> m (ArrayContents, Ptr a))
-> Int -> Int -> m (Array a)
newArrayWith (\Int
s Int
a -> IO (ArrayContents, Ptr a) -> m (ArrayContents, Ptr a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ArrayContents, Ptr a) -> m (ArrayContents, Ptr a))
-> IO (ArrayContents, Ptr a) -> m (ArrayContents, Ptr a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> IO (ArrayContents, Ptr a)
forall a. Int -> Int -> IO (ArrayContents, Ptr a)
newAlignedArrayContents Int
s Int
a)
{-# INLINE newArray #-}
newArray :: forall m a. (MonadIO m, Storable a) => Int -> m (Array a)
newArray :: Int -> m (Array a)
newArray = Int -> Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> m (Array a)
newArrayAligned (a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined :: a))
{-# INLINE withNewArrayUnsafe #-}
withNewArrayUnsafe ::
(MonadIO m, Storable a) => Int -> (Ptr a -> m ()) -> m (Array a)
withNewArrayUnsafe :: Int -> (Ptr a -> m ()) -> m (Array a)
withNewArrayUnsafe Int
count Ptr a -> m ()
f = do
Array a
arr <- Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray Int
count
Array a -> (Ptr a -> m (Array a)) -> m (Array a)
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr
((Ptr a -> m (Array a)) -> m (Array a))
-> (Ptr a -> m (Array a)) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> m ()
f Ptr a
p m () -> m (Array a) -> m (Array a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr
{-# INLINE putIndexUnsafe #-}
putIndexUnsafe :: forall m a. (MonadIO m, Storable a)
=> Int -> a -> Array a -> m ()
putIndexUnsafe :: Int -> a -> Array a -> m ()
putIndexUnsafe Int
i a
x arr :: Array a
arr@(Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..}) =
Array a -> (Ptr a -> m ()) -> m ()
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(ptr,i,a)
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_VALID(elemPtr,aEndInt
,a)) (return ())
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
forall a. Ptr a
elemPtr a
x
invalidIndex :: String -> Int -> a
invalidIndex :: [Char] -> Int -> a
invalidIndex [Char]
label Int
i =
[Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
label [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": invalid array index " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i
{-# INLINE putIndexPtr #-}
putIndexPtr :: forall m a. (MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> a -> m ()
putIndexPtr :: Ptr a -> Ptr a -> Int -> a -> m ()
putIndexPtr Ptr a
start Ptr a
end Int
i a
x = do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(start,i,a)
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_VALID(elemPtr,Int -> Int -> Int
forall a. Ord a => a -> a -> a
end,Int
a)
then IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
forall a. Ptr a
elemPtr a
x
else [Char] -> Int -> m ()
forall a. [Char] -> Int -> a
invalidIndex [Char]
"putIndexPtr" Int
i
{-# INLINE putIndex #-}
putIndex :: (MonadIO m, Storable a) => Int -> a -> Array a -> m ()
putIndex :: Int -> a -> Array a -> m ()
putIndex Int
i a
x Array a
arr =
Array a -> (Ptr a -> m ()) -> m ()
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr
((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> Ptr a -> Int -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> a -> m ()
putIndexPtr Ptr a
p (Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr) Int
i a
x
{-# INLINE putIndices #-}
putIndices :: forall m a. (MonadIO m, Storable a)
=> Array a -> Fold m (Int, a) ()
putIndices :: Array a -> Fold m (Int, a) ()
putIndices Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = (() -> (Int, a) -> m (Step () ()))
-> m (Step () ()) -> (() -> m ()) -> Fold m (Int, a) ()
forall s a (m :: * -> *) b.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
FL.mkFoldM () -> (Int, a) -> m (Step () ())
forall (f :: * -> *) b.
MonadIO f =>
() -> (Int, a) -> f (Step () b)
step m (Step () ())
forall b. m (Step () b)
initial () -> m ()
forall (m :: * -> *). MonadIO m => () -> m ()
extract
where
initial :: m (Step () b)
initial = Step () b -> m (Step () b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step () b -> m (Step () b)) -> Step () b -> m (Step () b)
forall a b. (a -> b) -> a -> b
$ () -> Step () b
forall s b. s -> Step s b
FL.Partial ()
step :: () -> (Int, a) -> f (Step () b)
step () (Int
i, a
x) = () -> Step () b
forall s b. s -> Step s b
FL.Partial (() -> Step () b) -> f () -> f (Step () b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO () -> f ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr a -> Ptr a -> Int -> a -> IO ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> a -> m ()
putIndexPtr Ptr a
arrStart Ptr a
aEnd Int
i a
x)
extract :: () -> m ()
extract () = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
arrContents
modifyIndexUnsafe :: forall m a b. (MonadIO m, Storable a) =>
Int -> (a -> (a, b)) -> Array a -> m b
modifyIndexUnsafe :: Int -> (a -> (a, b)) -> Array a -> m b
modifyIndexUnsafe Int
i a -> (a, b)
f arr :: Array a
arr@(Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..}) = do
IO b -> m b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO b -> m b) -> IO b -> m b
forall a b. (a -> b) -> a -> b
$ Array a -> (Ptr a -> IO b) -> IO b
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(ptr,i,a)
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_NEXT(elemPtr,a) <= aEnd) (return ())
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
forall a. Ptr a
elemPtr
let (a
x, b
res) = a -> (a, b)
f a
r
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
forall a. Ptr a
elemPtr a
x
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
res
{-# INLINE modifyIndexPtr #-}
modifyIndexPtr :: forall m a b. (MonadIO m, Storable a) =>
Int -> (a -> (a, b)) -> Ptr a -> Ptr a -> m b
modifyIndexPtr :: Int -> (a -> (a, b)) -> Ptr a -> Ptr a -> m b
modifyIndexPtr Int
i a -> (a, b)
f Ptr a
start Ptr a
end = IO b -> m b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO b -> m b) -> IO b -> m b
forall a b. (a -> b) -> a -> b
$ do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(start,i,a)
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_VALID(elemPtr,Int -> Int -> Int
forall a. Ord a => a -> a -> a
end,Int
a)
then do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
forall a. Ptr a
elemPtr
let (a
x, b
res) = a -> (a, b)
f a
r
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
forall a. Ptr a
elemPtr a
x
b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
res
else [Char] -> Int -> IO b
forall a. [Char] -> Int -> a
invalidIndex [Char]
"modifyIndex" Int
i
modifyIndex :: forall m a b. (MonadIO m, Storable a) =>
Int -> (a -> (a, b)) -> Array a -> m b
modifyIndex :: Int -> (a -> (a, b)) -> Array a -> m b
modifyIndex Int
i a -> (a, b)
f arr :: Array a
arr@(Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..}) = do
IO b -> m b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO b -> m b) -> IO b -> m b
forall a b. (a -> b) -> a -> b
$ Array a -> (Ptr a -> IO b) -> IO b
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
Int -> (a -> (a, b)) -> Ptr a -> Ptr a -> IO b
forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Int -> (a -> (a, b)) -> Ptr a -> Ptr a -> m b
modifyIndexPtr Int
i a -> (a, b)
f Ptr a
ptr Ptr a
aEnd
{-# INLINE modifyIndices #-}
modifyIndices :: forall m a. (MonadIO m, Storable a)
=> (a -> a) -> Array a -> Fold m Int ()
modifyIndices :: (a -> a) -> Array a -> Fold m Int ()
modifyIndices a -> a
f Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = (() -> Int -> m (Step () ()))
-> m (Step () ()) -> (() -> m ()) -> Fold m Int ()
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold () -> Int -> m (Step () ())
forall (f :: * -> *) b. MonadIO f => () -> Int -> f (Step () b)
step m (Step () ())
forall b. m (Step () b)
initial () -> m ()
forall (m :: * -> *). MonadIO m => () -> m ()
extract
where
initial :: m (Step () b)
initial = Step () b -> m (Step () b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step () b -> m (Step () b)) -> Step () b -> m (Step () b)
forall a b. (a -> b) -> a -> b
$ () -> Step () b
forall s b. s -> Step s b
FL.Partial ()
step :: () -> Int -> f (Step () b)
step () Int
i =
let f1 :: a -> (a, ())
f1 a
x = (a -> a
f a
x, ())
in () -> Step () b
forall s b. s -> Step s b
FL.Partial (() -> Step () b) -> f () -> f (Step () b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO () -> f ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> (a -> (a, ())) -> Ptr a -> Ptr a -> IO ()
forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Int -> (a -> (a, b)) -> Ptr a -> Ptr a -> m b
modifyIndexPtr Int
i a -> (a, ())
f1 Ptr a
arrStart Ptr a
aEnd)
extract :: () -> m ()
extract () = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
arrContents
modify :: forall m a. (MonadIO m, Storable a)
=> (a -> a) -> Array a -> m ()
modify :: (a -> a) -> Array a -> m ()
modify a -> a
f arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$
Array a -> (Ptr a -> IO ()) -> IO ()
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr Ptr a -> IO ()
go
where
go :: Ptr a -> IO ()
go Ptr a
ptr =
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PTR_VALID(ptr,aEndInt
,a)) $ do
r <- peek ptr
poke ptr (f r)
go (PTR_NEXT(ptr,a))
{-# INLINE swapPtrs #-}
swapPtrs :: Storable a => Ptr a -> Ptr a -> IO ()
swapPtrs :: Ptr a -> Ptr a -> IO ()
swapPtrs Ptr a
ptr1 Ptr a
ptr2 = do
a
r1 <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr1
a
r2 <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr2
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr1 a
r2
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr2 a
r1
{-# INLINE unsafeSwapIndices #-}
unsafeSwapIndices :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Array a -> m ()
unsafeSwapIndices :: Int -> Int -> Array a -> m ()
unsafeSwapIndices Int
i1 Int
i2 Array a
arr = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Array a -> (Ptr a -> IO ()) -> IO ()
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
let ptr1 :: Ptr b
ptr1 = PTR_INDEX(ptr,i1,a)
ptr2 :: Ptr b
ptr2 = PTR_INDEX(ptr,i2,a)
Ptr a -> Ptr a -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> IO ()
swapPtrs Ptr a
forall a. Ptr a
ptr1 (Ptr a
forall a. Ptr a
ptr2 :: Ptr a)
swapIndices :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Array a -> m ()
swapIndices :: Int -> Int -> Array a -> m ()
swapIndices Int
i1 Int
i2 Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
let ptr1 :: Ptr b
ptr1 = PTR_INDEX(arrStart,i1,a)
ptr2 :: Ptr b
ptr2 = PTR_INDEX(arrStart,i2,a)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| PTR_INVALID(ptr1,aEnd,a))
(IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> IO ()
forall a. [Char] -> Int -> a
invalidIndex [Char]
"swapIndices" Int
i1
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| PTR_INVALID(ptr2,aEnd,a))
(IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> IO ()
forall a. [Char] -> Int -> a
invalidIndex [Char]
"swapIndices" Int
i2
Ptr a -> Ptr a -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> IO ()
swapPtrs Ptr a
forall a. Ptr a
ptr1 (Ptr a
forall a. Ptr a
ptr2 :: Ptr a)
blockSize :: Int
blockSize :: Int
blockSize = Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024
largeObjectThreshold :: Int
largeObjectThreshold :: Int
largeObjectThreshold = (Int
blockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
10
{-# INLINE roundUpLargeArray #-}
roundUpLargeArray :: Int -> Int
roundUpLargeArray :: Int -> Int
roundUpLargeArray Int
size =
if Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
largeObjectThreshold
then
Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert
(Int
blockSize Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& ((Int
blockSize Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. (Int
blockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0))
((Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
blockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int -> Int
forall a. Num a => a -> a
negate Int
blockSize)
else Int
size
{-# INLINE isPower2 #-}
isPower2 :: Int -> Bool
isPower2 :: Int -> Bool
isPower2 Int
n = Int
n Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
{-# INLINE roundUpToPower2 #-}
roundUpToPower2 :: Int -> Int
roundUpToPower2 :: Int -> Int
roundUpToPower2 Int
n =
#if WORD_SIZE_IN_BITS == 64
Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
z6
#else
1 + z5
#endif
where
z0 :: Int
z0 = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
z1 :: Int
z1 = Int
z0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z0 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
1
z2 :: Int
z2 = Int
z1 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z1 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
2
z3 :: Int
z3 = Int
z2 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z2 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
4
z4 :: Int
z4 = Int
z3 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z3 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
8
z5 :: Int
z5 = Int
z4 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z4 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
16
z6 :: Int
z6 = Int
z5 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
z5 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
32
{-# INLINE allocBytesToBytes #-}
allocBytesToBytes :: forall a. Storable a => a -> Int -> Int
allocBytesToBytes :: a -> Int -> Int
allocBytesToBytes a
_ Int
n = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int -> Int
arrayPayloadSize Int
n) (SIZE_OF(a))
{-# INLINE allocBytesToElemCount #-}
allocBytesToElemCount :: Storable a => a -> Int -> Int
allocBytesToElemCount :: a -> Int -> Int
allocBytesToElemCount a
x Int
bytes =
let n :: Int
n = a -> Int -> Int
forall a. Storable a => a -> Int -> Int
bytesToElemCount a
x (a -> Int -> Int
forall a. Storable a => a -> Int -> Int
allocBytesToBytes a
x Int
bytes)
in Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1) Int
n
arrayChunkBytes :: Int
arrayChunkBytes :: Int
arrayChunkBytes = Int
1024
{-# INLINE roundDownTo #-}
roundDownTo :: Int -> Int -> Int
roundDownTo :: Int -> Int -> Int
roundDownTo Int
elemSize Int
size = Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
size Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
elemSize)
{-# NOINLINE reallocAligned #-}
reallocAligned :: Int -> Int -> Int -> Array a -> IO (Array a)
reallocAligned :: Int -> Int -> Int -> Array a -> IO (Array a)
reallocAligned Int
elemSize Int
alignSize Int
newCapacity Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = do
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
aEnd Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aBound) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
let newCapMax :: Int
newCapMax = Int -> Int
roundUpLargeArray Int
newCapacity
(ArrayContents
contents, Ptr a
pNew) <- Int -> Int -> IO (ArrayContents, Ptr a)
forall a. Int -> Int -> IO (ArrayContents, Ptr a)
newAlignedArrayContents Int
newCapMax Int
alignSize
let oldStart :: Ptr a
oldStart = Ptr a
arrStart
oldSize :: Int
oldSize = Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
oldStart
newCap :: Int
newCap = Int -> Int -> Int
roundDownTo Int
elemSize Int
newCapMax
newLen :: Int
newLen = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
oldSize Int
newCap
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
oldSize Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
elemSize Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
newLen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
newLen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
elemSize Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
pNew) (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
oldStart) Int
newLen
ArrayContents -> IO ()
touch ArrayContents
arrContents
Array a -> IO (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> IO (Array a)) -> Array a -> IO (Array a)
forall a b. (a -> b) -> a -> b
$ Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrStart :: Ptr a
arrStart = Ptr a
pNew
, arrContents :: ArrayContents
arrContents = ArrayContents
contents
, aEnd :: Ptr a
aEnd = Ptr a
pNew Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
newLen
, aBound :: Ptr a
aBound = Ptr a
pNew Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
newCap
}
{-# INLINABLE realloc #-}
realloc :: forall m a. (MonadIO m, Storable a) => Int -> Array a -> m (Array a)
realloc :: Int -> Array a -> m (Array a)
realloc Int
n Array a
arr =
IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int -> Array a -> IO (Array a)
forall a. Int -> Int -> Int -> Array a -> IO (Array a)
reallocAligned (SIZE_OF(a)) (alignment (undefined :: a)) n arr
reallocWith :: forall m a. (MonadIO m , Storable a) =>
String
-> (Int -> Int)
-> Int
-> Array a
-> m (Array a)
reallocWith :: [Char] -> (Int -> Int) -> Int -> Array a -> m (Array a)
reallocWith [Char]
label Int -> Int
capSizer Int
minIncr Array a
arr = do
let oldSize :: Int
oldSize = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr
newCap :: Int
newCap = Int -> Int
capSizer Int
oldSize
newSize :: Int
newSize = Int
oldSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
minIncr
safeCap :: Int
safeCap = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
newCap Int
newSize
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
newCap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
newSize Bool -> Bool -> Bool
|| [Char] -> Bool
forall a. HasCallStack => [Char] -> a
error (Int -> [Char]
forall a. Show a => a -> [Char]
badSize Int
newSize)) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Int -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
realloc Int
safeCap Array a
arr
where
badSize :: a -> [Char]
badSize a
newSize = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Char]
label
, [Char]
": new array size is less than required size "
, a -> [Char]
forall a. Show a => a -> [Char]
show a
newSize
, [Char]
". Please check the sizing function passed."
]
{-# INLINE resize #-}
resize :: forall m a. (MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
resize :: Int -> Array a -> m (Array a)
resize Int
n arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = do
let req :: Int
req = SIZE_OF(a) * n
len :: Int
len = Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
arrStart
if Int
req Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr
else Int -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
realloc Int
req Array a
arr
{-# INLINE resizeExp #-}
resizeExp :: forall m a. (MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
resizeExp :: Int -> Array a -> m (Array a)
resizeExp Int
n arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = do
let req :: Int
req = Int -> Int
roundUpLargeArray (SIZE_OF(a) * n)
req1 :: Int
req1 =
if Int
req Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
largeObjectThreshold
then Int -> Int
roundUpToPower2 Int
req
else Int
req
len :: Int
len = Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
arrStart
if Int
req1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr
else Int -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
realloc Int
req1 Array a
arr
{-# INLINE rightSize #-}
rightSize :: forall m a. (MonadIO m, Storable a) => Array a -> m (Array a)
rightSize :: Array a -> m (Array a)
rightSize arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
aEnd Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aBound) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
let start :: Ptr a
start = Ptr a
arrStart
len :: Int
len = Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
start
capacity :: Int
capacity = Ptr a
aBound Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
start
target :: Int
target = Int -> Int
roundUpLargeArray Int
len
waste :: Int
waste = Ptr a
aBound Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
aEnd
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
target Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` SIZE_OF(a) == 0) (return ())
if Int
target Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
capacity Bool -> Bool -> Bool
&& Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
waste
then Int -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
realloc Int
target Array a
arr
else Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr
{-# INLINE snocNewEnd #-}
snocNewEnd :: (MonadIO m, Storable a) => Ptr a -> Array a -> a -> m (Array a)
snocNewEnd :: Ptr a -> Array a -> a -> m (Array a)
snocNewEnd Ptr a
newEnd arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} a
x = IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ do
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
newEnd Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aBound) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
aEnd a
x
ArrayContents -> IO ()
touch ArrayContents
arrContents
Array a -> IO (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> IO (Array a)) -> Array a -> IO (Array a)
forall a b. (a -> b) -> a -> b
$ Array a
arr {aEnd :: Ptr a
aEnd = Ptr a
newEnd}
{-# INLINE snocUnsafe #-}
snocUnsafe :: forall m a. (MonadIO m, Storable a) =>
Array a -> a -> m (Array a)
snocUnsafe :: Array a -> a -> m (Array a)
snocUnsafe arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = Ptr a -> Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Array a -> a -> m (Array a)
snocNewEnd (PTR_NEXT(aEnd,a)) arr
{-# INLINE snocMay #-}
snocMay :: forall m a. (MonadIO m, Storable a) =>
Array a -> a -> m (Maybe (Array a))
snocMay :: Array a -> a -> m (Maybe (Array a))
snocMay arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} a
x = IO (Maybe (Array a)) -> m (Maybe (Array a))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe (Array a)) -> m (Maybe (Array a)))
-> IO (Maybe (Array a)) -> m (Maybe (Array a))
forall a b. (a -> b) -> a -> b
$ do
let newEnd :: Ptr b
newEnd = PTR_NEXT(aEnd,a)
if Ptr a
forall a. Ptr a
newEnd Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aBound
then Array a -> Maybe (Array a)
forall a. a -> Maybe a
Just (Array a -> Maybe (Array a))
-> IO (Array a) -> IO (Maybe (Array a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr a -> Array a -> a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Array a -> a -> m (Array a)
snocNewEnd Ptr a
forall a. Ptr a
newEnd Array a
arr a
x
else Maybe (Array a) -> IO (Maybe (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Array a)
forall a. Maybe a
Nothing
{-# NOINLINE snocWithRealloc #-}
snocWithRealloc :: forall m a. (MonadIO m, Storable a) =>
(Int -> Int)
-> Array a
-> a
-> m (Array a)
snocWithRealloc :: (Int -> Int) -> Array a -> a -> m (Array a)
snocWithRealloc Int -> Int
sizer Array a
arr a
x = do
Array a
arr1 <- IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ [Char] -> (Int -> Int) -> Int -> Array a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
[Char] -> (Int -> Int) -> Int -> Array a -> m (Array a)
reallocWith [Char]
"snocWith" Int -> Int
sizer (SIZE_OF(a)) arr
Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> a -> m (Array a)
snocUnsafe Array a
arr1 a
x
{-# INLINE snocWith #-}
snocWith :: forall m a. (MonadIO m, Storable a) =>
(Int -> Int)
-> Array a
-> a
-> m (Array a)
snocWith :: (Int -> Int) -> Array a -> a -> m (Array a)
snocWith Int -> Int
allocSize Array a
arr a
x = IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ do
let newEnd :: Ptr b
newEnd = PTR_NEXT(aEnd arr,a)
if Ptr a
forall a. Ptr a
newEnd Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Array a -> Ptr a
forall a. Array a -> Ptr a
aBound Array a
arr
then Ptr a -> Array a -> a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Array a -> a -> m (Array a)
snocNewEnd Ptr a
forall a. Ptr a
newEnd Array a
arr a
x
else (Int -> Int) -> Array a -> a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int) -> Array a -> a -> m (Array a)
snocWithRealloc Int -> Int
allocSize Array a
arr a
x
{-# INLINE snocLinear #-}
snocLinear :: forall m a. (MonadIO m, Storable a) => Array a -> a -> m (Array a)
snocLinear :: Array a -> a -> m (Array a)
snocLinear = (Int -> Int) -> Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int) -> Array a -> a -> m (Array a)
snocWith (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ a -> Int -> Int
forall a. Storable a => a -> Int -> Int
allocBytesToBytes (a
forall a. HasCallStack => a
undefined :: a) Int
arrayChunkBytes)
{-# INLINE snoc #-}
snoc :: forall m a. (MonadIO m, Storable a) => Array a -> a -> m (Array a)
snoc :: Array a -> a -> m (Array a)
snoc = (Int -> Int) -> Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int) -> Array a -> a -> m (Array a)
snocWith Int -> Int
f
where
f :: Int -> Int
f Int
oldSize =
if Int -> Bool
isPower2 Int
oldSize
then Int
oldSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
else Int -> Int
roundUpToPower2 Int
oldSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
{-# INLINE_NORMAL getIndexUnsafe #-}
getIndexUnsafe :: forall m a. (MonadIO m, Storable a) => Int -> Array a -> m a
getIndexUnsafe :: Int -> Array a -> m a
getIndexUnsafe Int
i arr :: Array a
arr@(Array {Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..}) =
Array a -> (Ptr a -> m a) -> m a
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr ((Ptr a -> m a) -> m a) -> (Ptr a -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(ptr,i,a)
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_VALID(elemPtr,aEndInt
,a)) (return ())
IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
forall a. Ptr a
elemPtr
{-# INLINE getIndexPtr #-}
getIndexPtr :: forall m a. (MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> m a
getIndexPtr :: Ptr a -> Ptr a -> Int -> m a
getIndexPtr Ptr a
start Ptr a
end Int
i = do
let elemPtr :: Ptr b
elemPtr = PTR_INDEX(start,i,a)
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& PTR_VALID(elemPtr,Int -> Int -> Int
forall a. Ord a => a -> a -> a
end,Int
a)
then IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
forall a. Ptr a
elemPtr
else [Char] -> Int -> m a
forall a. [Char] -> Int -> a
invalidIndex [Char]
"getIndexPtr" Int
i
{-# INLINE getIndex #-}
getIndex :: (MonadIO m, Storable a) => Int -> Array a -> m a
getIndex :: Int -> Array a -> m a
getIndex Int
i Array a
arr =
Array a -> (Ptr a -> m a) -> m a
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr
((Ptr a -> m a) -> m a) -> (Ptr a -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> Ptr a -> Int -> m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> m a
getIndexPtr Ptr a
p (Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr) Int
i
{-# INLINE getIndexPtrRev #-}
getIndexPtrRev :: forall m a. (MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> m a
getIndexPtrRev :: Ptr a -> Ptr a -> Int -> m a
getIndexPtrRev Ptr a
start Ptr a
end Int
i = do
let elemPtr :: Ptr b
elemPtr = PTR_RINDEX(end,i,a)
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Ptr a
forall a. Ptr a
elemPtr Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
start
then IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
forall a. Ptr a
elemPtr
else [Char] -> Int -> m a
forall a. [Char] -> Int -> a
invalidIndex [Char]
"getIndexPtrRev" Int
i
{-# INLINE getIndexRev #-}
getIndexRev :: (MonadIO m, Storable a) => Int -> Array a -> m a
getIndexRev :: Int -> Array a -> m a
getIndexRev Int
i Array a
arr =
Array a -> (Ptr a -> m a) -> m a
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array a
arr
((Ptr a -> m a) -> m a) -> (Ptr a -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> Ptr a -> Int -> m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> m a
getIndexPtrRev Ptr a
p (Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr) Int
i
data GetIndicesState contents start end st =
GetIndicesState contents start end st
{-# INLINE getIndicesD #-}
getIndicesD :: (Monad m, Storable a) =>
(forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (Array a) a
getIndicesD :: (forall b. IO b -> m b) -> Stream m Int -> Unfold m (Array a) a
getIndicesD forall b. IO b -> m b
liftio (D.Stream State Stream m Int -> s -> m (Step s Int)
stepi s
sti) = (GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a))
-> (Array a -> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s))
-> Unfold m (Array a) a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall a.
Storable a =>
GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
step Array a -> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s)
forall (m :: * -> *) a a.
Monad m =>
Array a -> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s)
inject
where
inject :: Array a -> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s)
inject (Array ArrayContents
contents Ptr a
start (Ptr Addr#
end) Ptr a
_) =
GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s))
-> GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (GetIndicesState ArrayContents (Ptr a) (Ptr a) s)
forall a b. (a -> b) -> a -> b
$ ArrayContents
-> Ptr a
-> Ptr a
-> s
-> GetIndicesState ArrayContents (Ptr a) (Ptr a) s
forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState ArrayContents
contents Ptr a
start (Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
end) s
sti
{-# INLINE_LATE step #-}
step :: GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
step (GetIndicesState ArrayContents
contents Ptr a
start Ptr a
end s
st) = do
Step s Int
r <- State Stream m Int -> s -> m (Step s Int)
stepi State Stream m Int
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s Int
r of
D.Yield Int
i s
s -> do
a
x <- IO a -> m a
forall b. IO b -> m b
liftio (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> Ptr a -> Int -> IO a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> Ptr a -> Int -> m a
getIndexPtr Ptr a
start Ptr a
end Int
i
Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a))
-> Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall a b. (a -> b) -> a -> b
$ a
-> GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
forall s a. a -> s -> Step s a
D.Yield a
x (ArrayContents
-> Ptr a
-> Ptr a
-> s
-> GetIndicesState ArrayContents (Ptr a) (Ptr a) s
forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState ArrayContents
contents Ptr a
start Ptr a
end s
s)
D.Skip s
s -> Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a))
-> Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall a b. (a -> b) -> a -> b
$ GetIndicesState ArrayContents (Ptr a) (Ptr a) s
-> Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
forall s a. s -> Step s a
D.Skip (ArrayContents
-> Ptr a
-> Ptr a
-> s
-> GetIndicesState ArrayContents (Ptr a) (Ptr a) s
forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState ArrayContents
contents Ptr a
start Ptr a
end s
s)
Step s Int
D.Stop -> do
IO () -> m ()
forall b. IO b -> m b
liftio (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
contents
Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
-> m (Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (GetIndicesState ArrayContents (Ptr a) (Ptr a) s) a
forall s a. Step s a
D.Stop
{-# INLINE getIndices #-}
getIndices :: (MonadIO m, Storable a) => SerialT m Int -> Unfold m (Array a) a
getIndices :: SerialT m Int -> Unfold m (Array a) a
getIndices (SerialT Stream m Int
stream) = (forall b. IO b -> m b) -> Stream m Int -> Unfold m (Array a) a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
(forall b. IO b -> m b) -> Stream m Int -> Unfold m (Array a) a
getIndicesD forall b. IO b -> m b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Stream m Int -> Unfold m (Array a) a)
-> Stream m Int -> Unfold m (Array a) a
forall a b. (a -> b) -> a -> b
$ Stream m Int -> Stream m Int
forall (m :: * -> *) a. Applicative m => Stream m a -> Stream m a
D.fromStreamK Stream m Int
stream
{-# INLINE getSliceUnsafe #-}
getSliceUnsafe :: forall a. Storable a
=> Int
-> Int
-> Array a
-> Array a
getSliceUnsafe :: Int -> Int -> Array a -> Array a
getSliceUnsafe Int
index Int
len (Array ArrayContents
contents Ptr a
start Ptr a
e Ptr a
_) =
let fp1 :: Ptr b
fp1 = PTR_INDEX(start,index,a)
end :: Ptr b
end = Ptr Any
forall a. Ptr a
fp1 Ptr Any -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a))
in Bool -> Array a -> Array a
forall a. HasCallStack => Bool -> a -> a
assert
(Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Ptr a
forall a. Ptr a
end Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
e)
(ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
forall a. Ptr a
fp1 Ptr a
forall a. Ptr a
end Ptr a
forall a. Ptr a
end)
{-# INLINE getSlice #-}
getSlice :: forall a. Storable a =>
Int
-> Int
-> Array a
-> Array a
getSlice :: Int -> Int -> Array a -> Array a
getSlice Int
index Int
len (Array ArrayContents
contents Ptr a
start Ptr a
e Ptr a
_) =
let fp1 :: Ptr b
fp1 = PTR_INDEX(start,index,a)
end :: Ptr b
end = Ptr Any
forall a. Ptr a
fp1 Ptr Any -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a))
in if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Ptr a
forall a. Ptr a
end Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
e
then ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
forall a. Ptr a
fp1 Ptr a
forall a. Ptr a
end Ptr a
forall a. Ptr a
end
else [Char] -> Array a
forall a. HasCallStack => [Char] -> a
error
([Char] -> Array a) -> [Char] -> Array a
forall a b. (a -> b) -> a -> b
$ [Char]
"getSlice: invalid slice, index "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
index [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" length " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
len
{-# INLINE reverse #-}
reverse :: forall m a. (MonadIO m, Storable a) => Array a -> m ()
reverse :: Array a -> m ()
reverse Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
let l :: Ptr a
l = Ptr a
arrStart
h :: Ptr b
h = PTR_PREV(aEnd,a)
in Ptr a -> Ptr a -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> IO ()
swap Ptr a
l Ptr a
forall a. Ptr a
h
where
swap :: Ptr b -> Ptr b -> IO ()
swap Ptr b
l Ptr b
h = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Ptr b
l Ptr b -> Ptr b -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr b
h) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Ptr b -> Ptr b -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> IO ()
swapPtrs Ptr b
l Ptr b
h
Ptr b -> Ptr b -> IO ()
swap (PTR_NEXT(l,a)) (PTR_PREV(h,a))
{-# INLINE permute #-}
permute :: Array a -> m Bool
permute :: Array a -> m Bool
permute = Array a -> m Bool
forall a. HasCallStack => a
undefined
{-# INLINE partitionBy #-}
partitionBy :: forall m a. (MonadIO m, Storable a)
=> (a -> Bool) -> Array a -> m (Array a, Array a)
partitionBy :: (a -> Bool) -> Array a -> m (Array a, Array a)
partitionBy a -> Bool
f arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO (Array a, Array a) -> m (Array a, Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a, Array a) -> m (Array a, Array a))
-> IO (Array a, Array a) -> m (Array a, Array a)
forall a b. (a -> b) -> a -> b
$ do
if Ptr a
arrStart Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
aEnd
then (Array a, Array a) -> IO (Array a, Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a
arr, Array a
arr)
else do
Ptr a
ptr <- Ptr a -> Ptr a -> IO (Ptr a)
go Ptr a
arrStart (PTR_PREV(aEnd,a))
let pl :: Array a
pl = ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
arrContents Ptr a
arrStart Ptr a
ptr Ptr a
ptr
pr :: Array a
pr = ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
arrContents Ptr a
ptr Ptr a
aEnd Ptr a
aEnd
(Array a, Array a) -> IO (Array a, Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a
pl, Array a
pr)
where
moveHigh :: Ptr b -> Ptr a -> IO (Maybe (Ptr a, a))
moveHigh Ptr b
low Ptr a
high = do
a
h <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
high
if a -> Bool
f a
h
then
let high1 :: Ptr b
high1 = PTR_PREV(high,a)
in if Ptr b
low Ptr b -> Ptr b -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr b
forall a. Ptr a
high1
then Maybe (Ptr a, a) -> IO (Maybe (Ptr a, a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Ptr a, a)
forall a. Maybe a
Nothing
else Ptr b -> Ptr a -> IO (Maybe (Ptr a, a))
moveHigh Ptr b
low Ptr a
forall a. Ptr a
high1
else Maybe (Ptr a, a) -> IO (Maybe (Ptr a, a))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Ptr a, a) -> Maybe (Ptr a, a)
forall a. a -> Maybe a
Just (Ptr a
high, a
h))
go :: Ptr a -> Ptr a -> IO (Ptr a)
go Ptr a
low Ptr a
high = do
a
l <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
low
if a -> Bool
f a
l
then
if Ptr a
low Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
high
then Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
low
else do
Maybe (Ptr a, a)
r <- Ptr a -> Ptr a -> IO (Maybe (Ptr a, a))
forall b. Ptr b -> Ptr a -> IO (Maybe (Ptr a, a))
moveHigh Ptr a
low Ptr a
high
case Maybe (Ptr a, a)
r of
Maybe (Ptr a, a)
Nothing -> Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
low
Just (Ptr a
high1, a
h) -> do
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
low a
h
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
high1 a
l
let low1 :: Ptr b
low1 = PTR_NEXT(low,a)
high2 :: Ptr b
high2 = PTR_PREV(high1,a)
if Ptr Any
forall a. Ptr a
low1 Ptr Any -> Ptr Any -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr Any
forall a. Ptr a
high2
then Ptr a -> Ptr a -> IO (Ptr a)
go Ptr a
forall a. Ptr a
low1 Ptr a
forall a. Ptr a
high2
else Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
forall a. Ptr a
low1
else do
let low1 :: Ptr b
low1 = PTR_NEXT(low,a)
if Ptr a
low Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
high
then Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
forall a. Ptr a
low1
else Ptr a -> Ptr a -> IO (Ptr a)
go Ptr a
forall a. Ptr a
low1 Ptr a
high
{-# INLINE shuffleBy #-}
shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m ()
shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m ()
shuffleBy = (a -> a -> m Bool) -> Array a -> Array a -> m ()
forall a. HasCallStack => a
undefined
{-# INLINABLE divideBy #-}
divideBy ::
Int -> (Array a -> m (Array a, Array a)) -> Array a -> m ()
divideBy :: Int -> (Array a -> m (Array a, Array a)) -> Array a -> m ()
divideBy = Int -> (Array a -> m (Array a, Array a)) -> Array a -> m ()
forall a. HasCallStack => a
undefined
mergeBy :: Int -> (Array a -> Array a -> m ()) -> Array a -> m ()
mergeBy :: Int -> (Array a -> Array a -> m ()) -> Array a -> m ()
mergeBy = Int -> (Array a -> Array a -> m ()) -> Array a -> m ()
forall a. HasCallStack => a
undefined
{-# INLINE byteLength #-}
byteLength :: Array a -> Int
byteLength :: Array a -> Int
byteLength Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let len :: Int
len = Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
arrStart
in Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) Int
len
{-# INLINE length #-}
length :: forall a. Storable a => Array a -> Int
length :: Array a -> Int
length Array a
arr =
let elemSize :: Int
elemSize = SIZE_OF(a)
blen :: Int
blen = Array a -> Int
forall a. Array a -> Int
byteLength Array a
arr
in Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert (Int
blen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
elemSize Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (Int
blen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
elemSize)
{-# INLINE byteCapacity #-}
byteCapacity :: Array a -> Int
byteCapacity :: Array a -> Int
byteCapacity Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let len :: Int
len = Ptr a
aBound Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
arrStart
in Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) Int
len
{-# INLINE bytesFree #-}
bytesFree :: Array a -> Int
bytesFree :: Array a -> Int
bytesFree Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let n :: Int
n = Ptr a
aBound Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
aEnd
in Bool -> Int -> Int
forall a. HasCallStack => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) Int
n
data GroupState s contents start end bound
= GroupStart s
| GroupBuffer s contents start end bound
| GroupYield
contents start end bound (GroupState s contents start end bound)
| GroupFinish
{-# INLINE_NORMAL arraysOf #-}
arraysOf :: forall m a. (MonadIO m, Storable a)
=> Int -> D.Stream m a -> D.Stream m (Array a)
arraysOf :: Int -> Stream m a -> Stream m (Array a)
arraysOf Int
n (D.Stream State Stream m a -> s -> m (Step s a)
step s
state) =
(State Stream m (Array a)
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Stream m (Array a)
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array a)
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a.
State Stream m a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
step' (s -> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
s -> GroupState s contents start end bound
GroupStart s
state)
where
{-# INLINE_LATE step' #-}
step' :: State Stream m a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
step' State Stream m a
_ (GroupStart s
st) = do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Streamly.Internal.Data.Array.Foreign.Mut.Type.arraysOf: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"the size of arrays [" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"] must be a natural number"
Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound <- IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray Int
n
Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall a b. (a -> b) -> a -> b
$ GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. s -> Step s a
D.Skip (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Ptr a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
st ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound)
step' State Stream m a
gst (GroupBuffer s
st ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound) = do
Step s a
r <- State Stream m a -> s -> m (Step s a)
step (State Stream m a -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
case Step s a
r of
D.Yield a
x s
s -> do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
end a
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayContents -> IO ()
touch ArrayContents
contents
let end1 :: Ptr b
end1 = PTR_NEXT(end,a)
Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall a b. (a -> b) -> a -> b
$
if Ptr a
forall a. Ptr a
end1 Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
bound
then GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. s -> Step s a
D.Skip
(ArrayContents
-> Ptr a
-> Ptr a
-> Ptr a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
-> GroupState s contents start end bound
GroupYield
ArrayContents
contents Ptr a
start Ptr a
forall a. Ptr a
end1 Ptr a
bound (s -> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
s -> GroupState s contents start end bound
GroupStart s
s))
else GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. s -> Step s a
D.Skip (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Ptr a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
s ArrayContents
contents Ptr a
start Ptr a
forall a. Ptr a
end1 Ptr a
bound)
D.Skip s
s ->
Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall a b. (a -> b) -> a -> b
$ GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. s -> Step s a
D.Skip (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Ptr a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
s ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound)
Step s a
D.Stop ->
Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall a b. (a -> b) -> a -> b
$ GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. s -> Step s a
D.Skip (ArrayContents
-> Ptr a
-> Ptr a
-> Ptr a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
-> GroupState s contents start end bound
GroupYield ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
forall s contents start end bound.
GroupState s contents start end bound
GroupFinish)
step' State Stream m a
_ (GroupYield ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
next) =
Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)))
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall a b. (a -> b) -> a -> b
$ Array a
-> GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
-> Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. a -> s -> Step s a
D.Yield (ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound) GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
next
step' State Stream m a
_ GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)
GroupFinish = Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
-> m (Step
(GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step (GroupState s ArrayContents (Ptr a) (Ptr a) (Ptr a)) (Array a)
forall s a. Step s a
D.Stop
{-# INLINE arrayStreamKFromStreamD #-}
arrayStreamKFromStreamD :: forall m a. (MonadIO m, Storable a) =>
D.Stream m a -> m (K.Stream m (Array a))
arrayStreamKFromStreamD :: Stream m a -> m (Stream m (Array a))
arrayStreamKFromStreamD =
let n :: Int
n = a -> Int -> Int
forall a. Storable a => a -> Int -> Int
allocBytesToElemCount (a
forall a. HasCallStack => a
undefined :: a) Int
defaultChunkSize
in (Array a -> Stream m (Array a) -> Stream m (Array a))
-> Stream m (Array a)
-> Stream m (Array a)
-> m (Stream m (Array a))
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr Array a -> Stream m (Array a) -> Stream m (Array a)
forall a (m :: * -> *). a -> Stream m a -> Stream m a
K.cons Stream m (Array a)
forall (m :: * -> *) a. Stream m a
K.nil (Stream m (Array a) -> m (Stream m (Array a)))
-> (Stream m a -> Stream m (Array a))
-> Stream m a
-> m (Stream m (Array a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Stream m a -> Stream m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
arraysOf Int
n
data FlattenState s contents a =
OuterLoop s
| InnerLoop s contents !(Ptr a) !(Ptr a)
{-# INLINE_NORMAL flattenArrays #-}
flattenArrays :: forall m a. (MonadIO m, Storable a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArrays :: Stream m (Array a) -> Stream m a
flattenArrays (D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = (State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a))
-> FlattenState s ArrayContents a -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a.
State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
step' (s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
state)
where
{-# INLINE_LATE step' #-}
step' :: State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
step' State Stream m a
gst (OuterLoop s
st) = do
Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step (State Stream m a -> State Stream m (Array a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ case Step s (Array a)
r of
D.Yield Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} s
s ->
FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> FlattenState s ArrayContents a
forall s contents a.
s -> contents -> Ptr a -> Ptr a -> FlattenState s contents a
InnerLoop s
s ArrayContents
arrContents Ptr a
arrStart Ptr a
aEnd)
D.Skip s
s -> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
s)
Step s (Array a)
D.Stop -> Step (FlattenState s ArrayContents a) a
forall s a. Step s a
D.Stop
step' State Stream m a
_ (InnerLoop s
st ArrayContents
_ Ptr a
p Ptr a
end) | Bool -> Bool -> Bool
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
end) (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
end) =
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a)
-> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall a b. (a -> b) -> a -> b
$ s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
st
step' State Stream m a
_ (InnerLoop s
st ArrayContents
contents Ptr a
p Ptr a
end) = do
a
x <- IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
contents
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ a
-> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. a -> s -> Step s a
D.Yield a
x (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> FlattenState s ArrayContents a
forall s contents a.
s -> contents -> Ptr a -> Ptr a -> FlattenState s contents a
InnerLoop s
st ArrayContents
contents (PTR_NEXT(p,a)) end)
{-# INLINE_NORMAL flattenArraysRev #-}
flattenArraysRev :: forall m a. (MonadIO m, Storable a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArraysRev :: Stream m (Array a) -> Stream m a
flattenArraysRev (D.Stream State Stream m (Array a) -> s -> m (Step s (Array a))
step s
state) = (State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a))
-> FlattenState s ArrayContents a -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a.
State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
step' (s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
state)
where
{-# INLINE_LATE step' #-}
step' :: State Stream m a
-> FlattenState s ArrayContents a
-> m (Step (FlattenState s ArrayContents a) a)
step' State Stream m a
gst (OuterLoop s
st) = do
Step s (Array a)
r <- State Stream m (Array a) -> s -> m (Step s (Array a))
step (State Stream m a -> State Stream m (Array a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ case Step s (Array a)
r of
D.Yield Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} s
s ->
let p :: Ptr b
p = PTR_PREV(aEnd,a)
in FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> FlattenState s ArrayContents a
forall s contents a.
s -> contents -> Ptr a -> Ptr a -> FlattenState s contents a
InnerLoop s
s ArrayContents
arrContents Ptr a
forall a. Ptr a
p Ptr a
arrStart)
D.Skip s
s -> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
s)
Step s (Array a)
D.Stop -> Step (FlattenState s ArrayContents a) a
forall s a. Step s a
D.Stop
step' State Stream m a
_ (InnerLoop s
st ArrayContents
_ Ptr a
p Ptr a
start) | Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr a
start =
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. s -> Step s a
D.Skip (FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a)
-> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall a b. (a -> b) -> a -> b
$ s -> FlattenState s ArrayContents a
forall s contents a. s -> FlattenState s contents a
OuterLoop s
st
step' State Stream m a
_ (InnerLoop s
st ArrayContents
contents Ptr a
p Ptr a
start) = do
a
x <- IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
contents
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
let cur :: Ptr b
cur = PTR_PREV(p,a)
Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a))
-> Step (FlattenState s ArrayContents a) a
-> m (Step (FlattenState s ArrayContents a) a)
forall a b. (a -> b) -> a -> b
$ a
-> FlattenState s ArrayContents a
-> Step (FlattenState s ArrayContents a) a
forall s a. a -> s -> Step s a
D.Yield a
x (s
-> ArrayContents
-> Ptr a
-> Ptr a
-> FlattenState s ArrayContents a
forall s contents a.
s -> contents -> Ptr a -> Ptr a -> FlattenState s contents a
InnerLoop s
st ArrayContents
contents Ptr a
forall a. Ptr a
cur Ptr a
start)
data ReadUState a = ReadUState
UNPACKIF !ArrayContents
!(Ptr a)
!(Ptr a)
toReadUState :: Array a -> ReadUState a
toReadUState :: Array a -> ReadUState a
toReadUState (Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
_) = ArrayContents -> Ptr a -> Ptr a -> ReadUState a
forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
end Ptr a
start
{-# INLINE_NORMAL producer #-}
producer :: forall m a. (MonadIO m, Storable a) => Producer m (Array a) a
producer :: Producer m (Array a) a
producer = (ReadUState a -> m (Step (ReadUState a) a))
-> (Array a -> m (ReadUState a))
-> (ReadUState a -> m (Array a))
-> Producer m (Array a) a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> (s -> m a) -> Producer m a b
Producer ReadUState a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
ReadUState a -> m (Step (ReadUState a) a)
step (ReadUState a -> m (ReadUState a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ReadUState a -> m (ReadUState a))
-> (Array a -> ReadUState a) -> Array a -> m (ReadUState a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> ReadUState a
forall a. Array a -> ReadUState a
toReadUState) ReadUState a -> m (Array a)
forall (m :: * -> *) a. Monad m => ReadUState a -> m (Array a)
extract
where
{-# INLINE_LATE step #-}
step :: ReadUState a -> m (Step (ReadUState a) a)
step (ReadUState ArrayContents
contents Ptr a
end Ptr a
cur)
| Bool -> Bool -> Bool
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
cur Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
end) (Ptr a
cur Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
end) = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
contents
Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ReadUState a) a
forall s a. Step s a
D.Stop
step (ReadUState ArrayContents
contents Ptr a
end Ptr a
cur) = do
!a
x <- IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
cur
Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ReadUState a) a -> m (Step (ReadUState a) a))
-> Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall a b. (a -> b) -> a -> b
$ a -> ReadUState a -> Step (ReadUState a) a
forall s a. a -> s -> Step s a
D.Yield a
x (ArrayContents -> Ptr a -> Ptr a -> ReadUState a
forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
end (PTR_NEXT(cur,a)))
extract :: ReadUState a -> m (Array a)
extract (ReadUState ArrayContents
contents Ptr a
end Ptr a
cur) = Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a)) -> Array a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
cur Ptr a
end Ptr a
end
{-# INLINE_NORMAL read #-}
read :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
read :: Unfold m (Array a) a
read = Producer m (Array a) a -> Unfold m (Array a) a
forall (m :: * -> *) a b. Producer m a b -> Unfold m a b
Producer.simplify Producer m (Array a) a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Producer m (Array a) a
producer
{-# INLINE_NORMAL readRev #-}
readRev :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
readRev :: Unfold m (Array a) a
readRev = (ReadUState a -> m (Step (ReadUState a) a))
-> (Array a -> m (ReadUState a)) -> Unfold m (Array a) a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold ReadUState a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
ReadUState a -> m (Step (ReadUState a) a)
step Array a -> m (ReadUState a)
forall (m :: * -> *) a. Monad m => Array a -> m (ReadUState a)
inject
where
inject :: Array a -> m (ReadUState a)
inject (Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
_) =
let p :: Ptr b
p = PTR_PREV(end,a)
in ReadUState a -> m (ReadUState a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ReadUState a -> m (ReadUState a))
-> ReadUState a -> m (ReadUState a)
forall a b. (a -> b) -> a -> b
$ ArrayContents -> Ptr a -> Ptr a -> ReadUState a
forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
start Ptr a
forall a. Ptr a
p
{-# INLINE_LATE step #-}
step :: ReadUState a -> m (Step (ReadUState a) a)
step (ReadUState ArrayContents
contents Ptr a
start Ptr a
p) | Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr a
start = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
contents
Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ReadUState a) a
forall s a. Step s a
D.Stop
step (ReadUState ArrayContents
contents Ptr a
start Ptr a
p) = do
a
x <- IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ReadUState a) a -> m (Step (ReadUState a) a))
-> Step (ReadUState a) a -> m (Step (ReadUState a) a)
forall a b. (a -> b) -> a -> b
$ a -> ReadUState a -> Step (ReadUState a) a
forall s a. a -> s -> Step s a
D.Yield a
x (ArrayContents -> Ptr a -> Ptr a -> ReadUState a
forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
start (PTR_PREV(p,a)))
{-# INLINE toList #-}
toList :: forall m a. (MonadIO m, Storable a) => Array a -> m [a]
toList :: Array a -> m [a]
toList Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO [a] -> m [a]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [a] -> m [a]) -> IO [a] -> m [a]
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO [a]
go Ptr a
arrStart
where
go :: Ptr a -> IO [a]
go Ptr a
p | Bool -> Bool -> Bool
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = [a] -> IO [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
go Ptr a
p = do
a
x <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
(:) a
x ([a] -> [a]) -> IO [a] -> IO [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr a -> IO [a]
go (PTR_NEXT(p,a))
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
toStreamD :: Array a -> Stream m a
toStreamD Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = (State Stream m a -> Ptr a -> m (Step (Ptr a) a))
-> Ptr a -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a -> Ptr a -> m (Step (Ptr a) a)
forall (m :: * -> *) p b.
MonadIO m =>
p -> Ptr a -> m (Step (Ptr b) a)
step Ptr a
arrStart
where
{-# INLINE_LATE step #-}
step :: p -> Ptr a -> m (Step (Ptr b) a)
step p
_ Ptr a
p | Bool -> Bool -> Bool
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = Step (Ptr b) a -> m (Step (Ptr b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Ptr b) a
forall s a. Step s a
D.Stop
step p
_ Ptr a
p = IO (Step (Ptr b) a) -> m (Step (Ptr b) a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Step (Ptr b) a) -> m (Step (Ptr b) a))
-> IO (Step (Ptr b) a) -> m (Step (Ptr b) a)
forall a b. (a -> b) -> a -> b
$ do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
Step (Ptr b) a -> IO (Step (Ptr b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Ptr b) a -> IO (Step (Ptr b) a))
-> Step (Ptr b) a -> IO (Step (Ptr b) a)
forall a b. (a -> b) -> a -> b
$ a -> Ptr b -> Step (Ptr b) a
forall s a. a -> s -> Step s a
D.Yield a
r (PTR_NEXT(p,a))
{-# INLINE toStreamK #-}
toStreamK :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
toStreamK :: Array a -> Stream m a
toStreamK Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = Ptr a -> Stream m a
forall (m :: * -> *). MonadIO m => Ptr a -> Stream m a
go Ptr a
arrStart
where
go :: Ptr a -> Stream m a
go Ptr a
p | Bool -> Bool -> Bool
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = Stream m a
forall (m :: * -> *) a. Stream m a
K.nil
| Bool
otherwise =
let elemM :: IO a
elemM = do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
in IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
elemM m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`K.consM` Ptr a -> Stream m a
go (PTR_NEXT(p,a))
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
toStreamDRev :: Array a -> Stream m a
toStreamDRev Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let p :: Ptr b
p = PTR_PREV(aEnd,a)
in (State Stream m a -> Ptr a -> m (Step (Ptr a) a))
-> Ptr a -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a -> Ptr a -> m (Step (Ptr a) a)
forall (m :: * -> *) p b.
MonadIO m =>
p -> Ptr a -> m (Step (Ptr b) a)
step Ptr a
forall a. Ptr a
p
where
{-# INLINE_LATE step #-}
step :: p -> Ptr a -> m (Step (Ptr b) a)
step p
_ Ptr a
p | Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr a
arrStart = Step (Ptr b) a -> m (Step (Ptr b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Ptr b) a
forall s a. Step s a
D.Stop
step p
_ Ptr a
p = IO (Step (Ptr b) a) -> m (Step (Ptr b) a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Step (Ptr b) a) -> m (Step (Ptr b) a))
-> IO (Step (Ptr b) a) -> m (Step (Ptr b) a)
forall a b. (a -> b) -> a -> b
$ do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
Step (Ptr b) a -> IO (Step (Ptr b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Ptr b) a -> IO (Step (Ptr b) a))
-> Step (Ptr b) a -> IO (Step (Ptr b) a)
forall a b. (a -> b) -> a -> b
$ a -> Ptr b -> Step (Ptr b) a
forall s a. a -> s -> Step s a
D.Yield a
r (PTR_PREV(p,a))
{-# INLINE toStreamKRev #-}
toStreamKRev :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
toStreamKRev :: Array a -> Stream m a
toStreamKRev Array {Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let p :: Ptr b
p = PTR_PREV(aEnd,a)
in Ptr a -> Stream m a
forall (m :: * -> *). MonadIO m => Ptr a -> Stream m a
go Ptr a
forall a. Ptr a
p
where
go :: Ptr a -> Stream m a
go Ptr a
p | Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr a
arrStart = Stream m a
forall (m :: * -> *) a. Stream m a
K.nil
| Bool
otherwise =
let elemM :: IO a
elemM = do
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
in IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
elemM m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`K.consM` Ptr a -> Stream m a
go (PTR_PREV(p,a))
{-# INLINE_NORMAL foldl' #-}
foldl' :: (MonadIO m, Storable a) => (b -> a -> b) -> b -> Array a -> m b
foldl' :: (b -> a -> b) -> b -> Array a -> m b
foldl' b -> a -> b
f b
z Array a
arr = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
D.foldl' b -> a -> b
f b
z (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ Array a -> Stream m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr
{-# INLINE_NORMAL foldr #-}
foldr :: (MonadIO m, Storable a) => (a -> b -> b) -> b -> Array a -> m b
foldr :: (a -> b -> b) -> b -> Array a -> m b
foldr a -> b -> b
f b
z Array a
arr = (a -> b -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr a -> b -> b
f b
z (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ Array a -> Stream m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr
data ArrayUnsafe a = ArrayUnsafe
UNPACKIF !ArrayContents
{-# UNPACK #-} !(Ptr a)
{-# UNPACK #-} !(Ptr a)
toArrayUnsafe :: Array a -> ArrayUnsafe a
toArrayUnsafe :: Array a -> ArrayUnsafe a
toArrayUnsafe (Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
_) =
ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
forall a. ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
ArrayUnsafe ArrayContents
contents Ptr a
start Ptr a
end
fromArrayUnsafe ::
#ifdef DEVBUILD
Storable a =>
#endif
ArrayUnsafe a -> Array a
fromArrayUnsafe :: ArrayUnsafe a -> Array a
fromArrayUnsafe (ArrayUnsafe ArrayContents
contents Ptr a
start Ptr a
end) =
ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
end
{-# INLINE_NORMAL appendNUnsafe #-}
appendNUnsafe :: forall m a. (MonadIO m, Storable a) =>
m (Array a)
-> Int
-> Fold m a (Array a)
appendNUnsafe :: m (Array a) -> Int -> Fold m a (Array a)
appendNUnsafe m (Array a)
action Int
n =
(ArrayUnsafe a -> Array a)
-> Fold m a (ArrayUnsafe a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ArrayUnsafe a -> Array a
forall a. ArrayUnsafe a -> Array a
fromArrayUnsafe (Fold m a (ArrayUnsafe a) -> Fold m a (Array a))
-> Fold m a (ArrayUnsafe a) -> Fold m a (Array a)
forall a b. (a -> b) -> a -> b
$ (ArrayUnsafe a -> a -> m (ArrayUnsafe a))
-> m (ArrayUnsafe a) -> Fold m a (ArrayUnsafe a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' ArrayUnsafe a -> a -> m (ArrayUnsafe a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step m (ArrayUnsafe a)
initial
where
initial :: m (ArrayUnsafe a)
initial = do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
arr :: Array a
arr@(Array ArrayContents
_ Ptr a
_ Ptr a
end Ptr a
bound) <- m (Array a)
action
let free :: Int
free = Ptr a
bound Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
end
needed :: Int
needed = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a)
Array a
arr1 <-
if Int
free Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
needed
then ([Char] -> (Int -> Int) -> Int -> Array a -> m (Array a))
-> [Char] -> (Int -> Int) -> Int -> Array a -> m (Array a)
forall a. a -> a
noinline [Char] -> (Int -> Int) -> Int -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
[Char] -> (Int -> Int) -> Int -> Array a -> m (Array a)
reallocWith [Char]
"appendNUnsafeWith" (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed) Int
needed Array a
arr
else Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr
ArrayUnsafe a -> m (ArrayUnsafe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ArrayUnsafe a -> m (ArrayUnsafe a))
-> ArrayUnsafe a -> m (ArrayUnsafe a)
forall a b. (a -> b) -> a -> b
$ Array a -> ArrayUnsafe a
forall a. Array a -> ArrayUnsafe a
toArrayUnsafe Array a
arr1
step :: ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step (ArrayUnsafe ArrayContents
contents Ptr a
start Ptr a
end) a
x = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
end a
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayContents -> IO ()
touch ArrayContents
contents
ArrayUnsafe a -> m (ArrayUnsafe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ArrayUnsafe a -> m (ArrayUnsafe a))
-> ArrayUnsafe a -> m (ArrayUnsafe a)
forall a b. (a -> b) -> a -> b
$ ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
forall a. ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
ArrayUnsafe ArrayContents
contents Ptr a
start (PTR_NEXT(end,a))
{-# INLINE_NORMAL appendN #-}
appendN :: forall m a. (MonadIO m, Storable a) =>
m (Array a) -> Int -> Fold m a (Array a)
appendN :: m (Array a) -> Int -> Fold m a (Array a)
appendN m (Array a)
initial Int
n = Int -> Fold m a (Array a) -> Fold m a (Array a)
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n (m (Array a) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
m (Array a) -> Int -> Fold m a (Array a)
appendNUnsafe m (Array a)
initial Int
n)
{-# INLINE appendWith #-}
appendWith :: forall m a. (MonadIO m, Storable a) =>
(Int -> Int) -> m (Array a) -> Fold m a (Array a)
appendWith :: (Int -> Int) -> m (Array a) -> Fold m a (Array a)
appendWith Int -> Int
sizer = (Array a -> a -> m (Array a)) -> m (Array a) -> Fold m a (Array a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' ((Int -> Int) -> Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int) -> Array a -> a -> m (Array a)
snocWith Int -> Int
sizer)
{-# INLINE append #-}
append :: forall m a. (MonadIO m, Storable a) =>
m (Array a) -> Fold m a (Array a)
append :: m (Array a) -> Fold m a (Array a)
append = (Int -> Int) -> m (Array a) -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int) -> m (Array a) -> Fold m a (Array a)
appendWith (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
{-# INLINE_NORMAL writeNWithUnsafe #-}
writeNWithUnsafe :: forall m a. (MonadIO m, Storable a)
=> (Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWithUnsafe :: (Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWithUnsafe Int -> m (Array a)
alloc Int
n = (ArrayUnsafe a -> a -> m (Step (ArrayUnsafe a) (Array a)))
-> m (Step (ArrayUnsafe a) (Array a))
-> (ArrayUnsafe a -> m (Array a))
-> Fold m a (Array a)
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold ArrayUnsafe a -> a -> m (Step (ArrayUnsafe a) (Array a))
forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
ArrayUnsafe a -> a -> m (Step (ArrayUnsafe a) b)
step m (Step (ArrayUnsafe a) (Array a))
forall b. m (Step (ArrayUnsafe a) b)
initial (Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a))
-> (ArrayUnsafe a -> Array a) -> ArrayUnsafe a -> m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ArrayUnsafe a -> Array a
forall a. ArrayUnsafe a -> Array a
fromArrayUnsafe)
where
initial :: m (Step (ArrayUnsafe a) b)
initial = ArrayUnsafe a -> Step (ArrayUnsafe a) b
forall s b. s -> Step s b
FL.Partial (ArrayUnsafe a -> Step (ArrayUnsafe a) b)
-> (Array a -> ArrayUnsafe a) -> Array a -> Step (ArrayUnsafe a) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> ArrayUnsafe a
forall a. Array a -> ArrayUnsafe a
toArrayUnsafe (Array a -> Step (ArrayUnsafe a) b)
-> m (Array a) -> m (Step (ArrayUnsafe a) b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (Array a)
alloc (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
n Int
0)
step :: ArrayUnsafe a -> a -> m (Step (ArrayUnsafe a) b)
step (ArrayUnsafe ArrayContents
contents Ptr a
start Ptr a
end) a
x = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
end a
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayContents -> IO ()
touch ArrayContents
contents
Step (ArrayUnsafe a) b -> m (Step (ArrayUnsafe a) b)
forall (m :: * -> *) a. Monad m => a -> m a
return
(Step (ArrayUnsafe a) b -> m (Step (ArrayUnsafe a) b))
-> Step (ArrayUnsafe a) b -> m (Step (ArrayUnsafe a) b)
forall a b. (a -> b) -> a -> b
$ ArrayUnsafe a -> Step (ArrayUnsafe a) b
forall s b. s -> Step s b
FL.Partial
(ArrayUnsafe a -> Step (ArrayUnsafe a) b)
-> ArrayUnsafe a -> Step (ArrayUnsafe a) b
forall a b. (a -> b) -> a -> b
$ ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
forall a. ArrayContents -> Ptr a -> Ptr a -> ArrayUnsafe a
ArrayUnsafe ArrayContents
contents Ptr a
start (PTR_NEXT(end,a))
{-# INLINE_NORMAL writeNUnsafe #-}
writeNUnsafe :: forall m a. (MonadIO m, Storable a)
=> Int -> Fold m a (Array a)
writeNUnsafe :: Int -> Fold m a (Array a)
writeNUnsafe = (Int -> m (Array a)) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWithUnsafe Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray
{-# INLINE_NORMAL writeNWith #-}
writeNWith :: forall m a. (MonadIO m, Storable a)
=> (Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWith :: (Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWith Int -> m (Array a)
alloc Int
n = Int -> Fold m a (Array a) -> Fold m a (Array a)
forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n ((Int -> m (Array a)) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWithUnsafe Int -> m (Array a)
alloc Int
n)
{-# INLINE_NORMAL writeN #-}
writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
writeN :: Int -> Fold m a (Array a)
writeN = (Int -> m (Array a)) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWith Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray
{-# INLINE_NORMAL writeNAligned #-}
writeNAligned :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Fold m a (Array a)
writeNAligned :: Int -> Int -> Fold m a (Array a)
writeNAligned Int
align = (Int -> m (Array a)) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWith (Int -> Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> m (Array a)
newArrayAligned Int
align)
{-# INLINE_NORMAL writeNAlignedUnmanaged #-}
writeNAlignedUnmanaged :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Fold m a (Array a)
writeNAlignedUnmanaged :: Int -> Int -> Fold m a (Array a)
writeNAlignedUnmanaged Int
align = (Int -> m (Array a)) -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> m (Array a)) -> Int -> Fold m a (Array a)
writeNWith (Int -> Int -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> m (Array a)
newArrayAlignedUnmanaged Int
align)
{-# INLINE_NORMAL writeChunks #-}
writeChunks :: (MonadIO m, Storable a) =>
Int -> Fold m a (K.Stream n (Array a))
writeChunks :: Int -> Fold m a (Stream n (Array a))
writeChunks Int
n = Fold m a (Array a)
-> Fold m (Array a) (Stream n (Array a))
-> Fold m a (Stream n (Array a))
forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
FL.many (Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeN Int
n) Fold m (Array a) (Stream n (Array a))
forall (m :: * -> *) a (n :: * -> *).
Monad m =>
Fold m a (Stream n a)
FL.toStreamK
{-# INLINE_NORMAL writeWith #-}
writeWith :: forall m a. (MonadIO m, Storable a)
=> Int -> Fold m a (Array a)
writeWith :: Int -> Fold m a (Array a)
writeWith Int
elemCount =
(Array a -> m (Array a))
-> Fold m a (Array a) -> Fold m a (Array a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
FL.rmapM Array a -> m (Array a)
extract (Fold m a (Array a) -> Fold m a (Array a))
-> Fold m a (Array a) -> Fold m a (Array a)
forall a b. (a -> b) -> a -> b
$ (Array a -> a -> m (Array a)) -> m (Array a) -> Fold m a (Array a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> a -> m (Array a)
step m (Array a)
initial
where
insertElem :: Array a -> a -> m (Array a)
insertElem (Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound) a
x = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
end a
x
Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a)) -> Array a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents Ptr a
start (PTR_NEXT(end,a)) bound
initial :: m (Array a)
initial = do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
elemCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"writeWith: elemCount is negative"
IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> m (Array a)
newArrayAligned (a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined :: a)) Int
elemCount
step :: Array a -> a -> m (Array a)
step arr :: Array a
arr@(Array ArrayContents
_ Ptr a
start Ptr a
end Ptr a
bound) a
x
| PTR_NEXT(end,a) > bound = do
let oldSize = end `minusPtr` start
newSize = max (oldSize * 2) 1
arr1 <-
liftIO
$ reallocAligned
(SIZE_OF(a))
(alignment (undefined :: a))
newSize
arr
insertElem arr1 x
step Array a
arr a
x = Array a -> a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> a -> m (Array a)
insertElem Array a
arr a
x
extract :: Array a -> m (Array a)
extract = IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a))
-> (Array a -> IO (Array a)) -> Array a -> m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> m (Array a)
rightSize
{-# INLINE write #-}
write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
write :: Fold m a (Array a)
write = Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeWith (a -> Int -> Int
forall a. Storable a => a -> Int -> Int
allocBytesToElemCount (a
forall a. HasCallStack => a
undefined :: a) Int
arrayChunkBytes)
{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: forall m a. (MonadIO m, Storable a)
=> Int -> D.Stream m a -> m (Array a)
fromStreamDN :: Int -> Stream m a -> m (Array a)
fromStreamDN Int
limit Stream m a
str = do
Array a
arr <- IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray Int
limit
Ptr a
end <- (Ptr a -> a -> m (Ptr a)) -> m (Ptr a) -> Stream m a -> m (Ptr a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
D.foldlM' Ptr a -> a -> m (Ptr a)
forall (m :: * -> *) a b.
(MonadIO m, Storable a) =>
Ptr a -> a -> m (Ptr b)
fwrite (Ptr a -> m (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr a -> m (Ptr a)) -> Ptr a -> m (Ptr a)
forall a b. (a -> b) -> a -> b
$ Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr) (Stream m a -> m (Ptr a)) -> Stream m a -> m (Ptr a)
forall a b. (a -> b) -> a -> b
$ Int -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Applicative m =>
Int -> Stream m a -> Stream m a
D.take Int
limit Stream m a
str
Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> m (Array a)) -> Array a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Array a
arr {aEnd :: Ptr a
aEnd = Ptr a
end}
where
fwrite :: Ptr a -> a -> m (Ptr b)
fwrite Ptr a
ptr a
x = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr a
x
Ptr b -> m (Ptr b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr b -> m (Ptr b)) -> Ptr b -> m (Ptr b)
forall a b. (a -> b) -> a -> b
$ PTR_NEXT(ptr,a)
{-# INLINABLE fromListN #-}
fromListN :: (MonadIO m, Storable a) => Int -> [a] -> m (Array a)
fromListN :: Int -> [a] -> m (Array a)
fromListN Int
n [a]
xs = Int -> Stream m a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> m (Array a)
fromStreamDN Int
n (Stream m a -> m (Array a)) -> Stream m a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ [a] -> Stream m a
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs
{-# INLINE arrayStreamKLength #-}
arrayStreamKLength :: (Monad m, Storable a) => K.Stream m (Array a) -> m Int
arrayStreamKLength :: Stream m (Array a) -> m Int
arrayStreamKLength Stream m (Array a)
as = (Int -> Int -> Int) -> Int -> Stream m Int -> m Int
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
K.foldl' Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Int
0 ((Array a -> Int) -> Stream m (Array a) -> Stream m Int
forall a b (m :: * -> *). (a -> b) -> Stream m a -> Stream m b
K.map Array a -> Int
forall a. Storable a => Array a -> Int
length Stream m (Array a)
as)
{-# INLINE fromArrayStreamK #-}
fromArrayStreamK :: (Storable a, MonadIO m) =>
K.Stream m (Array a) -> m (Array a)
fromArrayStreamK :: Stream m (Array a) -> m (Array a)
fromArrayStreamK Stream m (Array a)
as = do
Int
len <- Stream m (Array a) -> m Int
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Stream m (Array a) -> m Int
arrayStreamKLength Stream m (Array a)
as
Int -> Stream m a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> m (Array a)
fromStreamDN Int
len (Stream m a -> m (Array a)) -> Stream m a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Unfold m (Array a) a -> Stream m (Array a) -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany Unfold m (Array a) a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Unfold m (Array a) a
read (Stream m (Array a) -> Stream m a)
-> Stream m (Array a) -> Stream m a
forall a b. (a -> b) -> a -> b
$ Stream m (Array a) -> Stream m (Array a)
forall (m :: * -> *) a. Applicative m => Stream m a -> Stream m a
D.fromStreamK Stream m (Array a)
as
{-# INLINE fromStreamD #-}
fromStreamD :: (MonadIO m, Storable a) => D.Stream m a -> m (Array a)
fromStreamD :: Stream m a -> m (Array a)
fromStreamD Stream m a
m = Stream m a -> m (Stream m (Array a))
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Stream m (Array a))
arrayStreamKFromStreamD Stream m a
m m (Stream m (Array a))
-> (Stream m (Array a) -> m (Array a)) -> m (Array a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream m (Array a) -> m (Array a)
forall a (m :: * -> *).
(Storable a, MonadIO m) =>
Stream m (Array a) -> m (Array a)
fromArrayStreamK
{-# INLINE fromList #-}
fromList :: (MonadIO m, Storable a) => [a] -> m (Array a)
fromList :: [a] -> m (Array a)
fromList [a]
xs = Stream m a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Array a)
fromStreamD (Stream m a -> m (Array a)) -> Stream m a -> m (Array a)
forall a b. (a -> b) -> a -> b
$ [a] -> Stream m a
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs
{-# INLINE spliceCopy #-}
spliceCopy :: (MonadIO m, Storable a) => Array a -> Array a -> m (Array a)
spliceCopy :: Array a -> Array a -> m (Array a)
spliceCopy Array a
arr1 Array a
arr2 = do
let src1 :: Ptr a
src1 = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr1
src2 :: Ptr a
src2 = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr2
len1 :: Int
len1 = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr1 Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
src1
len2 :: Int
len2 = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr2 Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
src2
Array a
arr <- IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> m (Array a)
newArray (Int
len1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len2)
let dst :: Ptr a
dst = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
dst) (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
src1) Int
len1
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
arr1)
Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (Ptr Any -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr (Ptr a
dst Ptr a -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
len1)) (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
src2) Int
len2
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
arr2)
Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
arr { aEnd :: Ptr a
aEnd = Ptr a
dst Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
len1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len2) }
{-# INLINE spliceUnsafe #-}
spliceUnsafe :: MonadIO m => Array a -> (Array a, Int) -> m (Array a)
spliceUnsafe :: Array a -> (Array a, Int) -> m (Array a)
spliceUnsafe Array a
dst (Array a
src, Int
srcLen) =
IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ do
let psrc :: Ptr a
psrc = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
src
let pdst :: Ptr a
pdst = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
dst
Bool -> IO () -> IO ()
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
pdst Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
srcLen Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Array a -> Ptr a
forall a. Array a -> Ptr a
aBound Array a
dst) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Ptr Word8 -> Ptr Word8 -> Int -> IO ()
memcpy (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
pdst) (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
psrc) Int
srcLen
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
src)
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
dst)
Array a -> IO (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> IO (Array a)) -> Array a -> IO (Array a)
forall a b. (a -> b) -> a -> b
$ Array a
dst {aEnd :: Ptr a
aEnd = Ptr a
pdst Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
srcLen}
{-# INLINE spliceWith #-}
spliceWith :: forall m a. (MonadIO m, Storable a) =>
(Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
spliceWith :: (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
spliceWith Int -> Int -> Int
sizer dst :: Array a
dst@(Array ArrayContents
_ Ptr a
start Ptr a
end Ptr a
bound) Array a
src = do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
end Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr a
bound) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
let srcLen :: Int
srcLen = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
src Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
src
Array a
dst1 <-
if Ptr a
end Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
srcLen Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
bound
then do
let oldSize :: Int
oldSize = Ptr a
end Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
start
newSize :: Int
newSize = Int -> Int -> Int
sizer Int
oldSize Int
srcLen
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
newSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
oldSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
srcLen)
(m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error
([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"splice: newSize is less than the total size "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"of arrays being appended. Please check the "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"newSize function passed."
IO (Array a) -> m (Array a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array a) -> m (Array a)) -> IO (Array a) -> m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> Array a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m (Array a)
realloc Int
newSize Array a
dst
else Array a -> m (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array a
dst
Array a -> (Array a, Int) -> m (Array a)
forall (m :: * -> *) a.
MonadIO m =>
Array a -> (Array a, Int) -> m (Array a)
spliceUnsafe Array a
dst1 (Array a
src, Int
srcLen)
{-# INLINE splice #-}
splice :: (MonadIO m, Storable a) => Array a -> Array a -> m (Array a)
splice :: Array a -> Array a -> m (Array a)
splice = (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
spliceWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+)
{-# INLINE spliceExp #-}
spliceExp :: (MonadIO m, Storable a) => Array a -> Array a -> m (Array a)
spliceExp :: Array a -> Array a -> m (Array a)
spliceExp = (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
(Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
spliceWith (\Int
l1 Int
l2 -> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
l1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Int
l1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l2))
{-# INLINE breakOn #-}
breakOn :: MonadIO m
=> Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn :: Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn Word8
sep arr :: Array Word8
arr@Array{Ptr Word8
ArrayContents
aBound :: Ptr Word8
aEnd :: Ptr Word8
arrStart :: Ptr Word8
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = IO (Array Word8, Maybe (Array Word8))
-> m (Array Word8, Maybe (Array Word8))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8, Maybe (Array Word8))
-> m (Array Word8, Maybe (Array Word8)))
-> IO (Array Word8, Maybe (Array Word8))
-> m (Array Word8, Maybe (Array Word8))
forall a b. (a -> b) -> a -> b
$ do
let p :: Ptr Word8
p = Ptr Word8
arrStart
Ptr Word8
loc <- Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)
c_memchr Ptr Word8
p Word8
sep (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CSize) -> Int -> CSize
forall a b. (a -> b) -> a -> b
$ Ptr Word8
aEnd Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
p)
(Array Word8, Maybe (Array Word8))
-> IO (Array Word8, Maybe (Array Word8))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Array Word8, Maybe (Array Word8))
-> IO (Array Word8, Maybe (Array Word8)))
-> (Array Word8, Maybe (Array Word8))
-> IO (Array Word8, Maybe (Array Word8))
forall a b. (a -> b) -> a -> b
$
if Ptr Word8
loc Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
forall a. Ptr a
nullPtr
then (Array Word8
arr, Maybe (Array Word8)
forall a. Maybe a
Nothing)
else
( Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
arrContents
, arrStart :: Ptr Word8
arrStart = Ptr Word8
arrStart
, aEnd :: Ptr Word8
aEnd = Ptr Word8
loc
, aBound :: Ptr Word8
aBound = Ptr Word8
loc
}
, Array Word8 -> Maybe (Array Word8)
forall a. a -> Maybe a
Just (Array Word8 -> Maybe (Array Word8))
-> Array Word8 -> Maybe (Array Word8)
forall a b. (a -> b) -> a -> b
$ Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
arrContents
, arrStart :: Ptr Word8
arrStart = Ptr Word8
arrStart Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Ptr Word8
loc Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
, aEnd :: Ptr Word8
aEnd = Ptr Word8
aEnd
, aBound :: Ptr Word8
aBound = Ptr Word8
aBound
}
)
splitAt :: forall a. Storable a => Int -> Array a -> (Array a, Array a)
splitAt :: Int -> Array a -> (Array a, Array a)
splitAt Int
i arr :: Array a
arr@Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} =
let maxIndex :: Int
maxIndex = Array a -> Int
forall a. Storable a => Array a -> Int
length Array a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
in if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then [Char] -> (Array a, Array a)
forall a. HasCallStack => [Char] -> a
error [Char]
"sliceAt: negative array index"
else if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxIndex
then [Char] -> (Array a, Array a)
forall a. HasCallStack => [Char] -> a
error ([Char] -> (Array a, Array a)) -> [Char] -> (Array a, Array a)
forall a b. (a -> b) -> a -> b
$ [Char]
"sliceAt: specified array index " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is beyond the maximum index " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
maxIndex
else let off :: Int
off = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* SIZE_OF(a)
p :: Ptr b
p = Ptr a
arrStart Ptr a -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off
in ( Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
arrContents
, arrStart :: Ptr a
arrStart = Ptr a
arrStart
, aEnd :: Ptr a
aEnd = Ptr a
forall a. Ptr a
p
, aBound :: Ptr a
aBound = Ptr a
forall a. Ptr a
p
}
, Array :: forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
arrContents
, arrStart :: Ptr a
arrStart = Ptr a
arrStart Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off
, aEnd :: Ptr a
aEnd = Ptr a
aEnd
, aBound :: Ptr a
aBound = Ptr a
aBound
}
)
castUnsafe ::
#ifdef DEVBUILD
Storable b =>
#endif
Array a -> Array b
castUnsafe :: Array a -> Array b
castUnsafe (Array ArrayContents
contents Ptr a
start Ptr a
end Ptr a
bound) =
ArrayContents -> Ptr b -> Ptr b -> Ptr b -> Array b
forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
Array ArrayContents
contents (Ptr a -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr a
start) (Ptr a -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr a
end) (Ptr a -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr a
bound)
asBytes :: Array a -> Array Word8
asBytes :: Array a -> Array Word8
asBytes = Array a -> Array Word8
forall a b. Array a -> Array b
castUnsafe
cast :: forall a b. Storable b => Array a -> Maybe (Array b)
cast :: Array a -> Maybe (Array b)
cast Array a
arr =
let len :: Int
len = Array a -> Int
forall a. Array a -> Int
byteLength Array a
arr
r :: Int
r = Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` SIZE_OF(b)
in if Int
r Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0
then Maybe (Array b)
forall a. Maybe a
Nothing
else Array b -> Maybe (Array b)
forall a. a -> Maybe a
Just (Array b -> Maybe (Array b)) -> Array b -> Maybe (Array b)
forall a b. (a -> b) -> a -> b
$ Array a -> Array b
forall a b. Array a -> Array b
castUnsafe Array a
arr
asPtrUnsafe :: MonadIO m => Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe :: Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array{Ptr a
ArrayContents
aBound :: Ptr a
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} Ptr a -> m b
f = do
b
r <- Ptr a -> m b
f Ptr a
arrStart
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
arrContents
b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINE cmp #-}
cmp :: MonadIO m => Array a -> Array a -> m Bool
cmp :: Array a -> Array a -> m Bool
cmp Array a
arr1 Array a
arr2 =
IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ do
let ptr1 :: Ptr a
ptr1 = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr1
let ptr2 :: Ptr a
ptr2 = Array a -> Ptr a
forall a. Array a -> Ptr a
arrStart Array a
arr2
let len1 :: Int
len1 = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr1 Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
ptr1
let len2 :: Int
len2 = Array a -> Ptr a
forall a. Array a -> Ptr a
aEnd Array a
arr2 Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
ptr2
if Int
len1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len2
then
if Ptr a
ptr1 Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
ptr2
then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else do
Bool
r <- Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
memcmp (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr1) (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr2) Int
len1
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
arr1)
ArrayContents -> IO ()
touch (Array a -> ArrayContents
forall a. Array a -> ArrayContents
arrContents Array a
arr2)
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
r
else Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
instance NFData (Array a) where
{-# INLINE rnf #-}
rnf :: Array a -> ()
rnf Array {} = ()