{-# LANGUAGE CPP #-}
{-# LANGUAGE UnboxedTuples #-}
module Streamly.Internal.Data.Array.Mut.Type
(
MutArray (..)
, MutableByteArray
, touch
, pin
, unpin
, nil
, newPinned
, newPinnedBytes
, newAlignedPinned
, new
, newArrayWith
, withNewArrayUnsafe
, ArrayUnsafe (..)
, writeNWithUnsafe
, writeNWith
, writeNUnsafe
, writeN
, writeNAligned
, writeWith
, write
, writeRevN
, fromListN
, fromList
, fromListRevN
, fromListRev
, fromStreamDN
, fromStreamD
, putIndex
, putIndexUnsafe
, putIndices
, modifyIndexUnsafe
, modifyIndex
, modifyIndices
, modify
, swapIndices
, unsafeSwapIndices
, snocWith
, snoc
, snocLinear
, snocMay
, snocUnsafe
, writeAppendNUnsafe
, writeAppendN
, writeAppendWith
, writeAppend
, reader
, readerRevWith
, readerRev
, toStreamDWith
, toStreamDRevWith
, toStreamKWith
, toStreamKRevWith
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toList
, producerWith
, producer
, getIndex
, getIndexUnsafe
, getIndices
, getIndicesD
, getIndexRev
, blockSize
, arrayChunkBytes
, allocBytesToElemCount
, realloc
, resize
, resizeExp
, rightSize
, length
, byteLength
, byteCapacity
, bytesFree
, strip
, reverse
, permute
, partitionBy
, shuffleBy
, divideBy
, mergeBy
, bubble
, cast
, castUnsafe
, asBytes
, asPtrUnsafe
, foldl'
, foldr
, cmp
, chunksOf
, arrayStreamKFromStreamD
, writeChunks
, flattenArrays
, flattenArraysRev
, fromArrayStreamK
, getSliceUnsafe
, getSlice
, splitAt
, breakOn
, spliceCopy
, spliceWith
, splice
, spliceExp
, spliceUnsafe
, putSliceUnsafe
, roundUpToPower2
, memcpy
, memcmp
, c_memchr
)
where
#include "assert.hs"
#include "inline.hs"
#include "ArrayMacros.h"
#include "MachDeps.h"
import Control.Monad (when, void)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Bits (shiftR, (.|.), (.&.))
import Data.Proxy (Proxy(..))
import Data.Word (Word8)
import Foreign.C.Types (CSize(..), CInt(..))
import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
import Streamly.Internal.Data.Unboxed
( MutableByteArray(..)
, Unbox
, getMutableByteArray#
, peekWith
, pokeWith
, sizeOf
, touch
)
import GHC.Base
( IO(..)
, Int(..)
, byteArrayContents#
, compareByteArrays#
, copyMutableByteArray#
)
import GHC.Base (noinline)
import GHC.Exts (unsafeCoerce#)
import GHC.Ptr (Ptr(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Producer.Type (Producer (..))
import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
import Streamly.Internal.Data.Stream.StreamK.Type (StreamK)
import Streamly.Internal.Data.SVar.Type (adaptState, defState)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
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
import qualified Streamly.Internal.Data.Unboxed as Unboxed
import qualified Prelude
import Prelude hiding
(length, foldr, read, unlines, splitAt, reverse, truncate)
#include "DocTestDataMutArray.hs"
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. Unbox a => a -> Int -> Int
bytesToElemCount :: forall a. Unbox a => a -> Int -> Int
bytesToElemCount a
_ Int
n = Int
n 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 = 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 (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 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ CInt
r forall a. Eq a => a -> a -> Bool
== CInt
0
data MutArray a =
#ifdef DEVBUILD
Unbox a =>
#endif
MutArray
{ forall a. MutArray a -> MutableByteArray
arrContents :: {-# UNPACK #-} !MutableByteArray
, forall a. MutArray a -> Int
arrStart :: {-# UNPACK #-} !Int
, forall a. MutArray a -> Int
arrEnd :: {-# UNPACK #-} !Int
, forall a. MutArray a -> Int
arrBound :: {-# UNPACK #-} !Int
}
{-# INLINE pin #-}
pin :: MutArray a -> IO (MutArray a)
pin :: forall a. MutArray a -> IO (MutArray a)
pin arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
MutableByteArray
contents <- MutableByteArray -> IO MutableByteArray
Unboxed.pin MutableByteArray
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray a
arr {arrContents :: MutableByteArray
arrContents = MutableByteArray
contents}
{-# INLINE unpin #-}
unpin :: MutArray a -> IO (MutArray a)
unpin :: forall a. MutArray a -> IO (MutArray a)
unpin arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
MutableByteArray
contents <- MutableByteArray -> IO MutableByteArray
Unboxed.unpin MutableByteArray
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray a
arr {arrContents :: MutableByteArray
arrContents = MutableByteArray
contents}
{-# INLINE newArrayWith #-}
newArrayWith :: forall m a. (MonadIO m, Unbox a)
=> (Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
newArrayWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
newArrayWith Int -> Int -> m MutableByteArray
alloc Int
alignSize Int
count = do
let size :: Int
size = forall a. Ord a => a -> a -> a
max (Int
count forall a. Num a => a -> a -> a
* SIZE_OF(a)) 0
MutableByteArray
contents <- Int -> Int -> m MutableByteArray
alloc Int
size Int
alignSize
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
contents
, arrStart :: Int
arrStart = Int
0
, arrEnd :: Int
arrEnd = Int
0
, arrBound :: Int
arrBound = Int
size
}
nil ::
#ifdef DEVBUILD
Unbox a =>
#endif
MutArray a
nil :: forall a. MutArray a
nil = forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
Unboxed.nil Int
0 Int
0 Int
0
{-# INLINE newPinnedBytes #-}
newPinnedBytes :: MonadIO m =>
#ifdef DEVBUILD
Unbox a =>
#endif
Int -> m (MutArray a)
newPinnedBytes :: forall (m :: * -> *) a. MonadIO m => Int -> m (MutArray a)
newPinnedBytes Int
bytes = do
MutableByteArray
contents <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> IO MutableByteArray
Unboxed.newPinnedBytes Int
bytes
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
contents
, arrStart :: Int
arrStart = Int
0
, arrEnd :: Int
arrEnd = Int
0
, arrBound :: Int
arrBound = Int
bytes
}
{-# INLINE newAlignedPinned #-}
newAlignedPinned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
newAlignedPinned :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> m (MutArray a)
newAlignedPinned =
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
newArrayWith (\Int
s Int
a -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> Int -> IO MutableByteArray
Unboxed.newAlignedPinnedBytes Int
s Int
a)
{-# INLINE newPinned #-}
newPinned :: forall m a. (MonadIO m, Unbox a) => Int -> m (MutArray a)
newPinned :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned =
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
newArrayWith
(\Int
s Int
_ -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> IO MutableByteArray
Unboxed.newPinnedBytes Int
s)
(forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"newPinned: alignSize is not used")
{-# INLINE new #-}
new :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
new :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
new =
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> m MutableByteArray) -> Int -> Int -> m (MutArray a)
newArrayWith
(\Int
s Int
_ -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> IO MutableByteArray
Unboxed.newUnpinnedBytes Int
s)
(forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"new: alignment is not used in unpinned arrays.")
{-# INLINE withNewArrayUnsafe #-}
withNewArrayUnsafe ::
(MonadIO m, Unbox a) => Int -> (Ptr a -> m ()) -> m (MutArray a)
withNewArrayUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> (Ptr a -> m ()) -> m (MutArray a)
withNewArrayUnsafe Int
count Ptr a -> m ()
f = do
MutArray a
arr <- forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned Int
count
forall (m :: * -> *) a b.
MonadIO m =>
MutArray a -> (Ptr a -> m b) -> m b
asPtrUnsafe MutArray a
arr
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> m ()
f Ptr a
p forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr
{-# INLINE putIndexUnsafe #-}
putIndexUnsafe :: forall m a. (MonadIO m, Unbox a)
=> Int -> MutArray a -> a -> m ()
putIndexUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndexUnsafe Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a
x = do
let index :: Int
index = Int
INDEX_OF(arrStart, i, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_VALID(index, arrEnd, a)) (return ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
index a
x
invalidIndex :: String -> Int -> a
invalidIndex :: forall a. [Char] -> Int -> a
invalidIndex [Char]
label Int
i =
forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
label forall a. [a] -> [a] -> [a]
++ [Char]
": invalid array index " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
i
{-# INLINE putIndex #-}
putIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m ()
putIndex :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndex Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a
x = do
let index :: Int
index = Int
INDEX_OF(arrStart,i,a)
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_VALID(index,arrEnd,a)
then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
index a
x
else forall a. [Char] -> Int -> a
invalidIndex [Char]
"putIndex" Int
i
{-# INLINE putIndices #-}
putIndices :: forall m a. (MonadIO m, Unbox a)
=> MutArray a -> Fold m (Int, a) ()
putIndices :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> Fold m (Int, a) ()
putIndices MutArray a
arr = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall {m :: * -> *}. MonadIO m => () -> (Int, a) -> m ()
step (forall (m :: * -> *) a. Monad m => a -> m a
return ())
where
step :: () -> (Int, a) -> m ()
step () (Int
i, a
x) = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndex Int
i MutArray a
arr a
x)
modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
Int -> MutArray a -> (a -> (a, b)) -> m b
modifyIndexUnsafe :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> (a -> (a, b)) -> m b
modifyIndexUnsafe Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a -> (a, b)
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let index :: Int
index = Int
INDEX_OF(arrStart,i,a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_NEXT(index,a) <= arrEnd) (return ())
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
index
let (a
x, b
res) = a -> (a, b)
f a
r
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
index a
x
forall (m :: * -> *) a. Monad m => a -> m a
return b
res
modifyIndex :: forall m a b. (MonadIO m, Unbox a) =>
Int -> MutArray a -> (a -> (a, b)) -> m b
modifyIndex :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> (a -> (a, b)) -> m b
modifyIndex Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a -> (a, b)
f = do
let index :: Int
index = Int
INDEX_OF(arrStart,i,a)
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_VALID(index,arrEnd,a)
then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
index
let (a
x, b
res) = a -> (a, b)
f a
r
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
index a
x
forall (m :: * -> *) a. Monad m => a -> m a
return b
res
else forall a. [Char] -> Int -> a
invalidIndex [Char]
"modifyIndex" Int
i
{-# INLINE modifyIndices #-}
modifyIndices :: forall m a . (MonadIO m, Unbox a)
=> MutArray a -> (Int -> a -> a) -> Fold m Int ()
modifyIndices :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> (Int -> a -> a) -> Fold m Int ()
modifyIndices MutArray a
arr Int -> a -> a
f = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall {m :: * -> *}. MonadIO m => () -> Int -> m ()
step m ()
initial
where
initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> Int -> m ()
step () Int
i =
let f1 :: a -> (a, ())
f1 a
x = (Int -> a -> a
f Int
i a
x, ())
in forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> (a -> (a, b)) -> m b
modifyIndex Int
i MutArray a
arr a -> (a, ())
f1
modify :: forall m a. (MonadIO m, Unbox a)
=> MutArray a -> (a -> a) -> m ()
modify :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> (a -> a) -> m ()
modify MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a -> a
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
Int -> IO ()
go Int
arrStart
where
go :: Int -> IO ()
go Int
i =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (INDEX_VALID(i,arrEnd,a)) $ do
r <- peekWith arrContents i
pokeWith arrContents i (f r)
go (INDEX_NEXT(i,a))
{-# INLINE swapArrayByteIndices #-}
swapArrayByteIndices ::
forall a. Unbox a
=> Proxy a
-> MutableByteArray
-> Int
-> Int
-> IO ()
swapArrayByteIndices :: forall a.
Unbox a =>
Proxy a -> MutableByteArray -> Int -> Int -> IO ()
swapArrayByteIndices Proxy a
_ MutableByteArray
arrContents Int
i1 Int
i2 = do
a
r1 <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
i1
a
r2 <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
i2
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
i1 (a
r2 :: a)
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
i2 (a
r1 :: a)
{-# INLINE unsafeSwapIndices #-}
unsafeSwapIndices :: forall m a. (MonadIO m, Unbox a)
=> Int -> Int -> MutArray a -> m ()
unsafeSwapIndices :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> MutArray a -> m ()
unsafeSwapIndices Int
i1 Int
i2 MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let t1 :: Int
t1 = Int
INDEX_OF(arrStart,i1,a)
t2 :: Int
t2 = Int
INDEX_OF(arrStart,i2,a)
forall a.
Unbox a =>
Proxy a -> MutableByteArray -> Int -> Int -> IO ()
swapArrayByteIndices (forall {k} (t :: k). Proxy t
Proxy :: Proxy a) MutableByteArray
arrContents Int
t1 Int
t2
swapIndices :: forall m a. (MonadIO m, Unbox a)
=> Int -> Int -> MutArray a -> m ()
swapIndices :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> MutArray a -> m ()
swapIndices Int
i1 Int
i2 MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let t1 :: Int
t1 = Int
INDEX_OF(arrStart,i1,a)
t2 :: Int
t2 = Int
INDEX_OF(arrStart,i2,a)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i1 forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| INDEX_INVALID(t1,arrEnd,a))
forall a b. (a -> b) -> a -> b
$ forall a. [Char] -> Int -> a
invalidIndex [Char]
"swapIndices" Int
i1
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i2 forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| INDEX_INVALID(t2,arrEnd,a))
forall a b. (a -> b) -> a -> b
$ forall a. [Char] -> Int -> a
invalidIndex [Char]
"swapIndices" Int
i2
forall a.
Unbox a =>
Proxy a -> MutableByteArray -> Int -> Int -> IO ()
swapArrayByteIndices (forall {k} (t :: k). Proxy t
Proxy :: Proxy a) MutableByteArray
arrContents Int
t1 Int
t2
blockSize :: Int
blockSize :: Int
blockSize = Int
4 forall a. Num a => a -> a -> a
* Int
1024
largeObjectThreshold :: Int
largeObjectThreshold :: Int
largeObjectThreshold = (Int
blockSize forall a. Num a => a -> a -> a
* Int
8) forall a. Integral a => a -> a -> a
`div` Int
10
{-# INLINE roundUpLargeArray #-}
roundUpLargeArray :: Int -> Int
roundUpLargeArray :: Int -> Int
roundUpLargeArray Int
size =
if Int
size forall a. Ord a => a -> a -> Bool
>= Int
largeObjectThreshold
then
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
blockSize forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& ((Int
blockSize forall a. Bits a => a -> a -> a
.&. (Int
blockSize forall a. Num a => a -> a -> a
- Int
1)) forall a. Eq a => a -> a -> Bool
== Int
0))
((Int
size forall a. Num a => a -> a -> a
+ Int
blockSize forall a. Num a => a -> a -> a
- Int
1) forall a. Bits a => a -> a -> a
.&. 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 forall a. Bits a => a -> a -> a
.&. (Int
n forall a. Num a => a -> a -> a
- Int
1) 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 forall a. Num a => a -> a -> a
+ Int
z6
#else
1 + z5
#endif
where
z0 :: Int
z0 = Int
n forall a. Num a => a -> a -> a
- Int
1
z1 :: Int
z1 = Int
z0 forall a. Bits a => a -> a -> a
.|. Int
z0 forall a. Bits a => a -> Int -> a
`shiftR` Int
1
z2 :: Int
z2 = Int
z1 forall a. Bits a => a -> a -> a
.|. Int
z1 forall a. Bits a => a -> Int -> a
`shiftR` Int
2
z3 :: Int
z3 = Int
z2 forall a. Bits a => a -> a -> a
.|. Int
z2 forall a. Bits a => a -> Int -> a
`shiftR` Int
4
z4 :: Int
z4 = Int
z3 forall a. Bits a => a -> a -> a
.|. Int
z3 forall a. Bits a => a -> Int -> a
`shiftR` Int
8
z5 :: Int
z5 = Int
z4 forall a. Bits a => a -> a -> a
.|. Int
z4 forall a. Bits a => a -> Int -> a
`shiftR` Int
16
z6 :: Int
z6 = Int
z5 forall a. Bits a => a -> a -> a
.|. Int
z5 forall a. Bits a => a -> Int -> a
`shiftR` Int
32
{-# INLINE allocBytesToBytes #-}
allocBytesToBytes :: forall a. Unbox a => a -> Int -> Int
allocBytesToBytes :: forall a. Unbox a => a -> Int -> Int
allocBytesToBytes a
_ Int
n = forall a. Ord a => a -> a -> a
max (Int -> Int
arrayPayloadSize Int
n) (SIZE_OF(a))
{-# INLINE allocBytesToElemCount #-}
allocBytesToElemCount :: Unbox a => a -> Int -> Int
allocBytesToElemCount :: forall a. Unbox a => a -> Int -> Int
allocBytesToElemCount a
x Int
bytes =
let n :: Int
n = forall a. Unbox a => a -> Int -> Int
bytesToElemCount a
x (forall a. Unbox a => a -> Int -> Int
allocBytesToBytes a
x Int
bytes)
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n 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 forall a. Num a => a -> a -> a
- (Int
size forall a. Integral a => a -> a -> a
`mod` Int
elemSize)
{-# NOINLINE reallocExplicit #-}
reallocExplicit :: Int -> Int -> MutArray a -> IO (MutArray a)
reallocExplicit :: forall a. Int -> Int -> MutArray a -> IO (MutArray a)
reallocExplicit Int
elemSize Int
newCapacityInBytes MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
assertM(Int
arrEnd forall a. Ord a => a -> a -> Bool
<= Int
arrBound)
let newCapMaxInBytes :: Int
newCapMaxInBytes = Int -> Int
roundUpLargeArray Int
newCapacityInBytes
MutableByteArray
contents <- Int -> IO MutableByteArray
Unboxed.newPinnedBytes Int
newCapMaxInBytes
let !(MutableByteArray MutableByteArray# RealWorld
mbarrFrom#) = MutableByteArray
arrContents
!(MutableByteArray MutableByteArray# RealWorld
mbarrTo#) = MutableByteArray
contents
let oldStart :: Int
oldStart = Int
arrStart
!(I# Int#
oldStartInBytes#) = Int
oldStart
oldSizeInBytes :: Int
oldSizeInBytes = Int
arrEnd forall a. Num a => a -> a -> a
- Int
oldStart
newCapInBytes :: Int
newCapInBytes = Int -> Int -> Int
roundDownTo Int
elemSize Int
newCapMaxInBytes
!newLenInBytes :: Int
newLenInBytes@(I# Int#
newLenInBytes#) = forall a. Ord a => a -> a -> a
min Int
oldSizeInBytes Int
newCapInBytes
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
oldSizeInBytes forall a. Integral a => a -> a -> a
`mod` Int
elemSize forall a. Eq a => a -> a -> Bool
== Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
newLenInBytes forall a. Ord a => a -> a -> Bool
>= Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
newLenInBytes forall a. Integral a => a -> a -> a
`mod` Int
elemSize forall a. Eq a => a -> a -> Bool
== Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s# -> (# forall d.
MutableByteArray# d
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyMutableByteArray# MutableByteArray# RealWorld
mbarrFrom# Int#
oldStartInBytes#
MutableByteArray# RealWorld
mbarrTo# Int#
0# Int#
newLenInBytes# State# RealWorld
s#, () #)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray
{ arrStart :: Int
arrStart = Int
0
, arrContents :: MutableByteArray
arrContents = MutableByteArray
contents
, arrEnd :: Int
arrEnd = Int
newLenInBytes
, arrBound :: Int
arrBound = Int
newCapInBytes
}
{-# INLINABLE realloc #-}
realloc :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
realloc :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
bytes MutArray a
arr = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Int -> Int -> MutArray a -> IO (MutArray a)
reallocExplicit (SIZE_OF(a)) bytes arr
reallocWith :: forall m a. (MonadIO m , Unbox a) =>
String
-> (Int -> Int)
-> Int
-> MutArray a
-> m (MutArray a)
reallocWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[Char] -> (Int -> Int) -> Int -> MutArray a -> m (MutArray a)
reallocWith [Char]
label Int -> Int
capSizer Int
minIncrBytes MutArray a
arr = do
let oldSizeBytes :: Int
oldSizeBytes = forall a. MutArray a -> Int
arrEnd MutArray a
arr forall a. Num a => a -> a -> a
- forall a. MutArray a -> Int
arrStart MutArray a
arr
newCapBytes :: Int
newCapBytes = Int -> Int
capSizer Int
oldSizeBytes
newSizeBytes :: Int
newSizeBytes = Int
oldSizeBytes forall a. Num a => a -> a -> a
+ Int
minIncrBytes
safeCapBytes :: Int
safeCapBytes = forall a. Ord a => a -> a -> a
max Int
newCapBytes Int
newSizeBytes
assertM(Int
safeCapBytes forall a. Ord a => a -> a -> Bool
>= Int
newSizeBytes Bool -> Bool -> Bool
|| forall a. (?callStack::CallStack) => [Char] -> a
error (forall a. Show a => a -> [Char]
badSize Int
newSizeBytes))
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
safeCapBytes MutArray a
arr
where
badSize :: a -> [Char]
badSize a
newSize =
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Char]
label
, [Char]
": new array size (in bytes) is less than required size "
, forall a. Show a => a -> [Char]
show a
newSize
, [Char]
". Please check the sizing function passed."
]
{-# INLINE resize #-}
resize :: forall m a. (MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
resize :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
resize Int
nElems arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
let req :: Int
req = SIZE_OF(a) * nElems
len :: Int
len = Int
arrEnd forall a. Num a => a -> a -> a
- Int
arrStart
if Int
req forall a. Ord a => a -> a -> Bool
< Int
len
then forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr
else forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
req MutArray a
arr
{-# INLINE resizeExp #-}
resizeExp :: forall m a. (MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
resizeExp :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
resizeExp Int
nElems arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
let req :: Int
req = Int -> Int
roundUpLargeArray (SIZE_OF(a) * nElems)
req1 :: Int
req1 =
if Int
req forall a. Ord a => a -> a -> Bool
> Int
largeObjectThreshold
then Int -> Int
roundUpToPower2 Int
req
else Int
req
len :: Int
len = Int
arrEnd forall a. Num a => a -> a -> a
- Int
arrStart
if Int
req1 forall a. Ord a => a -> a -> Bool
< Int
len
then forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr
else forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
req1 MutArray a
arr
{-# INLINE rightSize #-}
rightSize :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m (MutArray a)
rightSize :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> m (MutArray a)
rightSize arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
arrEnd forall a. Ord a => a -> a -> Bool
<= Int
arrBound) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let start :: Int
start = Int
arrStart
len :: Int
len = Int
arrEnd forall a. Num a => a -> a -> a
- Int
start
capacity :: Int
capacity = Int
arrBound forall a. Num a => a -> a -> a
- Int
start
target :: Int
target = Int -> Int
roundUpLargeArray Int
len
waste :: Int
waste = Int
arrBound forall a. Num a => a -> a -> a
- Int
arrEnd
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
target forall a. Ord a => a -> a -> Bool
>= Int
len) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
len forall a. Integral a => a -> a -> a
`mod` SIZE_OF(a) == 0) (return ())
if Int
target forall a. Ord a => a -> a -> Bool
< Int
capacity Bool -> Bool -> Bool
&& Int
len forall a. Ord a => a -> a -> Bool
< Int
3 forall a. Num a => a -> a -> a
* Int
waste
then forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
target MutArray a
arr
else forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr
{-# INLINE snocNewEnd #-}
snocNewEnd :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd Int
newEnd arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a
x = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
newEnd forall a. Ord a => a -> a -> Bool
<= Int
arrBound) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
arrEnd a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray a
arr {arrEnd :: Int
arrEnd = Int
newEnd}
{-# INLINE snocUnsafe #-}
snocUnsafe :: forall m a. (MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snocUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snocUnsafe arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd (INDEX_NEXT(arrEnd,a)) arr
{-# INLINE snocMay #-}
snocMay :: forall m a. (MonadIO m, Unbox a) =>
MutArray a -> a -> m (Maybe (MutArray a))
snocMay :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (Maybe (MutArray a))
snocMay arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} a
x = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let newEnd :: Int
newEnd = INDEX_NEXT(arrEnd,a)
if Int
newEnd forall a. Ord a => a -> a -> Bool
<= Int
arrBound
then forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd Int
newEnd MutArray a
arr a
x
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
{-# NOINLINE snocWithRealloc #-}
snocWithRealloc :: forall m a. (MonadIO m, Unbox a) =>
(Int -> Int)
-> MutArray a
-> a
-> m (MutArray a)
snocWithRealloc :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWithRealloc Int -> Int
sizer MutArray a
arr a
x = do
MutArray a
arr1 <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[Char] -> (Int -> Int) -> Int -> MutArray a -> m (MutArray a)
reallocWith [Char]
"snocWith" Int -> Int
sizer (SIZE_OF(a)) arr
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snocUnsafe MutArray a
arr1 a
x
{-# INLINE snocWith #-}
snocWith :: forall m a. (MonadIO m, Unbox a) =>
(Int -> Int)
-> MutArray a
-> a
-> m (MutArray a)
snocWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWith Int -> Int
allocSize MutArray a
arr a
x = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let newEnd :: Int
newEnd = INDEX_NEXT(arrEnd arr,a)
if Int
newEnd forall a. Ord a => a -> a -> Bool
<= forall a. MutArray a -> Int
arrBound MutArray a
arr
then forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd Int
newEnd MutArray a
arr a
x
else forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWithRealloc Int -> Int
allocSize MutArray a
arr a
x
{-# INLINE snocLinear #-}
snocLinear :: forall m a. (MonadIO m, Unbox a) => MutArray a -> a -> m (MutArray a)
snocLinear :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snocLinear = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWith (forall a. Num a => a -> a -> a
+ forall a. Unbox a => a -> Int -> Int
allocBytesToBytes (forall a. (?callStack::CallStack) => a
undefined :: a) Int
arrayChunkBytes)
{-# INLINE snoc #-}
snoc :: forall m a. (MonadIO m, Unbox a) => MutArray a -> a -> m (MutArray a)
snoc :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snoc = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWith Int -> Int
f
where
f :: Int -> Int
f Int
oldSize =
if Int -> Bool
isPower2 Int
oldSize
then Int
oldSize forall a. Num a => a -> a -> a
* Int
2
else Int -> Int
roundUpToPower2 Int
oldSize forall a. Num a => a -> a -> a
* Int
2
{-# INLINE_NORMAL getIndexUnsafe #-}
getIndexUnsafe :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
getIndexUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndexUnsafe Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
let index :: Int
index = Int
INDEX_OF(arrStart,i,a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_VALID(index,arrEnd,a)) (return ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
index
{-# INLINE getIndex #-}
getIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
getIndex :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndex Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
let index :: Int
index = Int
INDEX_OF(arrStart,i,a)
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& INDEX_VALID(index,arrEnd,a)
then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
index
else forall a. [Char] -> Int -> a
invalidIndex [Char]
"getIndex" Int
i
{-# INLINE getIndexRev #-}
getIndexRev :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
getIndexRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndexRev Int
i MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = do
let index :: Int
index = RINDEX_OF(forall a. Unbox a => Proxy a -> Int
arrEnd,i,a)
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
index forall a. Ord a => a -> a -> Bool
>= Int
arrStart
then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
index
else forall a. [Char] -> Int -> a
invalidIndex [Char]
"getIndexRev" Int
i
data GetIndicesState contents start end st =
GetIndicesState contents start end st
{-# INLINE getIndicesD #-}
getIndicesD :: (Monad m, Unbox a) =>
(forall b. IO b -> m b) -> D.Stream m Int -> Unfold m (MutArray a) a
getIndicesD :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Stream m Int -> Unfold m (MutArray a) a
getIndicesD forall b. IO b -> m b
liftio (D.Stream State StreamK m Int -> s -> m (Step s Int)
stepi s
sti) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {a}.
Unbox a =>
GetIndicesState MutableByteArray Int Int s
-> m (Step (GetIndicesState MutableByteArray Int Int s) a)
step forall {m :: * -> *} {a}.
Monad m =>
MutArray a -> m (GetIndicesState MutableByteArray Int Int s)
inject
where
inject :: MutArray a -> m (GetIndicesState MutableByteArray Int Int s)
inject (MutArray MutableByteArray
contents Int
start Int
end Int
_) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState MutableByteArray
contents Int
start Int
end s
sti
{-# INLINE_LATE step #-}
step :: GetIndicesState MutableByteArray Int Int s
-> m (Step (GetIndicesState MutableByteArray Int Int s) a)
step (GetIndicesState MutableByteArray
contents Int
start Int
end s
st) = do
Step s Int
r <- State StreamK m Int -> s -> m (Step s Int)
stepi 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 <- forall b. IO b -> m b
liftio forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndex Int
i (forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
start Int
end forall a. (?callStack::CallStack) => a
undefined)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState MutableByteArray
contents Int
start Int
end s
s)
D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall contents start end st.
contents
-> start -> end -> st -> GetIndicesState contents start end st
GetIndicesState MutableByteArray
contents Int
start Int
end s
s)
Step s Int
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
{-# INLINE getIndices #-}
getIndices :: (MonadIO m, Unbox a) => Stream m Int -> Unfold m (MutArray a) a
getIndices :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m Int -> Unfold m (MutArray a) a
getIndices = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Stream m Int -> Unfold m (MutArray a) a
getIndicesD forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE getSliceUnsafe #-}
getSliceUnsafe :: forall a. Unbox a
=> Int
-> Int
-> MutArray a
-> MutArray a
getSliceUnsafe :: forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSliceUnsafe Int
index Int
len (MutArray MutableByteArray
contents Int
start Int
e Int
_) =
let fp1 :: Int
fp1 = INDEX_OF(start,index,a)
end :: Int
end = Int
fp1 forall a. Num a => a -> a -> a
+ (Int
len forall a. Num a => a -> a -> a
* SIZE_OF(a))
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
index forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
len forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
end forall a. Ord a => a -> a -> Bool
<= Int
e)
(forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
fp1 Int
end Int
end)
{-# INLINE getSlice #-}
getSlice :: forall a. Unbox a =>
Int
-> Int
-> MutArray a
-> MutArray a
getSlice :: forall a. Unbox a => Int -> Int -> MutArray a -> MutArray a
getSlice Int
index Int
len (MutArray MutableByteArray
contents Int
start Int
e Int
_) =
let fp1 :: Int
fp1 = INDEX_OF(start,index,a)
end :: Int
end = Int
fp1 forall a. Num a => a -> a -> a
+ (Int
len forall a. Num a => a -> a -> a
* SIZE_OF(a))
in if Int
index forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
len forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
end forall a. Ord a => a -> a -> Bool
<= Int
e
then forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
fp1 Int
end Int
end
else forall a. (?callStack::CallStack) => [Char] -> a
error
forall a b. (a -> b) -> a -> b
$ [Char]
"getSlice: invalid slice, index "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
index forall a. [a] -> [a] -> [a]
++ [Char]
" length " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
len
{-# INLINE reverse #-}
reverse :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m ()
reverse :: forall (m :: * -> *) a. (MonadIO m, Unbox a) => MutArray a -> m ()
reverse MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let l :: Int
l = Int
arrStart
h :: Int
h = INDEX_PREV(arrEnd,a)
in Int -> Int -> IO ()
swap Int
l Int
h
where
swap :: Int -> Int -> IO ()
swap Int
l Int
h = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
l forall a. Ord a => a -> a -> Bool
< Int
h) forall a b. (a -> b) -> a -> b
$ do
forall a.
Unbox a =>
Proxy a -> MutableByteArray -> Int -> Int -> IO ()
swapArrayByteIndices (forall {k} (t :: k). Proxy t
Proxy :: Proxy a) MutableByteArray
arrContents Int
l Int
h
Int -> Int -> IO ()
swap (INDEX_NEXT(l,a)) (INDEX_PREV(h,aInt
))
{-# INLINE permute #-}
permute :: MutArray a -> m Bool
permute :: forall a (m :: * -> *). MutArray a -> m Bool
permute = forall a. (?callStack::CallStack) => a
undefined
{-# INLINE partitionBy #-}
partitionBy :: forall m a. (MonadIO m, Unbox a)
=> (a -> Bool) -> MutArray a -> m (MutArray a, MutArray a)
partitionBy :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(a -> Bool) -> MutArray a -> m (MutArray a, MutArray a)
partitionBy a -> Bool
f arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
if Int
arrStart forall a. Ord a => a -> a -> Bool
>= Int
arrEnd
then forall (m :: * -> *) a. Monad m => a -> m a
return (MutArray a
arr, MutArray a
arr)
else do
Int
ptr <- Int -> Int -> IO Int
go Int
arrStart (INDEX_PREV(arrEnd,a))
let pl :: MutArray a
pl = forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
arrContents Int
arrStart Int
ptr Int
ptr
pr :: MutArray a
pr = forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
arrContents Int
ptr Int
arrEnd Int
arrEnd
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. MutArray a
pl, forall a. MutArray a
pr)
where
moveHigh :: Int -> Int -> IO (Maybe (Int, a))
moveHigh Int
low Int
high = do
a
h <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
high
if a -> Bool
f a
h
then
let high1 :: Int
high1 = INDEX_PREV(high,a)
in if Int
low forall a. Eq a => a -> a -> Bool
== Int
high1
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else Int -> Int -> IO (Maybe (Int, a))
moveHigh Int
low Int
high1
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (Int
high, a
h))
go :: Int -> Int -> IO Int
go Int
low Int
high = do
a
l <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
low
if a -> Bool
f a
l
then
if Int
low forall a. Eq a => a -> a -> Bool
== Int
high
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
low
else do
Maybe (Int, a)
r <- Int -> Int -> IO (Maybe (Int, a))
moveHigh Int
low Int
high
case Maybe (Int, a)
r of
Maybe (Int, a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Int
low
Just (Int
high1, a
h) -> do
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
low a
h
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
high1 a
l
let low1 :: Int
low1 = INDEX_NEXT(low,a)
high2 :: Int
high2 = INDEX_PREV(high1,a)
if Int
low1 forall a. Ord a => a -> a -> Bool
<= Int
high2
then Int -> Int -> IO Int
go Int
low1 Int
high2
else forall (m :: * -> *) a. Monad m => a -> m a
return Int
low1
else do
let low1 :: Int
low1 = INDEX_NEXT(low,a)
if Int
low forall a. Eq a => a -> a -> Bool
== Int
high
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
low1
else Int -> Int -> IO Int
go Int
low1 Int
high
{-# INLINE shuffleBy #-}
shuffleBy :: (a -> a -> m Bool) -> MutArray a -> MutArray a -> m ()
shuffleBy :: forall a (m :: * -> *).
(a -> a -> m Bool) -> MutArray a -> MutArray a -> m ()
shuffleBy = forall a. (?callStack::CallStack) => a
undefined
{-# INLINABLE divideBy #-}
divideBy ::
Int -> (MutArray a -> m (MutArray a, MutArray a)) -> MutArray a -> m ()
divideBy :: forall a (m :: * -> *).
Int
-> (MutArray a -> m (MutArray a, MutArray a)) -> MutArray a -> m ()
divideBy = forall a. (?callStack::CallStack) => a
undefined
mergeBy :: Int -> (MutArray a -> MutArray a -> m ()) -> MutArray a -> m ()
mergeBy :: forall a (m :: * -> *).
Int -> (MutArray a -> MutArray a -> m ()) -> MutArray a -> m ()
mergeBy = forall a. (?callStack::CallStack) => a
undefined
{-# INLINE byteLength #-}
byteLength :: MutArray a -> Int
byteLength :: forall a. MutArray a -> Int
byteLength MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let len :: Int
len = Int
arrEnd forall a. Num a => a -> a -> a
- Int
arrStart
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
len forall a. Ord a => a -> a -> Bool
>= Int
0) Int
len
{-# INLINE length #-}
length :: forall a. Unbox a => MutArray a -> Int
length :: forall a. Unbox a => MutArray a -> Int
length MutArray a
arr =
let elemSize :: Int
elemSize = SIZE_OF(a)
blen :: Int
blen = forall a. MutArray a -> Int
byteLength MutArray a
arr
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
blen forall a. Integral a => a -> a -> a
`mod` Int
elemSize forall a. Eq a => a -> a -> Bool
== Int
0) (Int
blen forall a. Integral a => a -> a -> a
`div` Int
elemSize)
{-# INLINE byteCapacity #-}
byteCapacity :: MutArray a -> Int
byteCapacity :: forall a. MutArray a -> Int
byteCapacity MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let len :: Int
len = Int
arrBound forall a. Num a => a -> a -> a
- Int
arrStart
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
len forall a. Ord a => a -> a -> Bool
>= Int
0) Int
len
{-# INLINE bytesFree #-}
bytesFree :: MutArray a -> Int
bytesFree :: forall a. MutArray a -> Int
bytesFree MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let n :: Int
n = Int
arrBound forall a. Num a => a -> a -> a
- Int
arrEnd
in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n 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 chunksOf #-}
chunksOf :: forall m a. (MonadIO m, Unbox a)
=> Int -> D.Stream m a -> D.Stream m (MutArray a)
chunksOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (MutArray a)
chunksOf Int
n (D.Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {a} {a}.
State StreamK m a
-> GroupState s MutableByteArray Int Int Int
-> m (Step
(GroupState s MutableByteArray Int Int Int) (MutArray a))
step' (forall s contents start end bound.
s -> GroupState s contents start end bound
GroupStart s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> GroupState s MutableByteArray Int Int Int
-> m (Step
(GroupState s MutableByteArray Int Int Int) (MutArray a))
step' State StreamK m a
_ (GroupStart s
st) = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n forall a. Ord a => a -> a -> Bool
<= Int
0) forall a b. (a -> b) -> a -> b
$
forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Streamly.Internal.Data.MutArray.Mut.Type.chunksOf: "
forall a. [a] -> [a] -> [a]
++ [Char]
"the size of arrays [" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
n
forall a. [a] -> [a] -> [a]
++ [Char]
"] must be a natural number"
(MutArray MutableByteArray
contents Int
start Int
end Int
bound :: MutArray a) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
st MutableByteArray
contents Int
start Int
end Int
bound)
step' State StreamK m a
gst (GroupBuffer s
st MutableByteArray
contents Int
start Int
end Int
bound) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
D.Yield a
x s
s -> do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
contents Int
end a
x
let end1 :: Int
end1 = INDEX_NEXT(end,a)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Int
end1 forall a. Ord a => a -> a -> Bool
>= Int
bound
then forall s a. s -> Step s a
D.Skip
(forall s contents start end bound.
contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
-> GroupState s contents start end bound
GroupYield
MutableByteArray
contents Int
start Int
end1 Int
bound (forall s contents start end bound.
s -> GroupState s contents start end bound
GroupStart s
s))
else forall s a. s -> Step s a
D.Skip (forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
s MutableByteArray
contents Int
start Int
end1 Int
bound)
D.Skip s
s ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s contents start end bound.
s
-> contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
GroupBuffer s
s MutableByteArray
contents Int
start Int
end Int
bound)
Step s a
D.Stop ->
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s contents start end bound.
contents
-> start
-> end
-> bound
-> GroupState s contents start end bound
-> GroupState s contents start end bound
GroupYield MutableByteArray
contents Int
start Int
end Int
bound forall s contents start end bound.
GroupState s contents start end bound
GroupFinish)
step' State StreamK m a
_ (GroupYield MutableByteArray
contents Int
start Int
end Int
bound GroupState s MutableByteArray Int Int Int
next) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield (forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
start Int
end Int
bound) GroupState s MutableByteArray Int Int Int
next
step' State StreamK m a
_ GroupState s MutableByteArray Int Int Int
GroupFinish = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
{-# INLINE arrayStreamKFromStreamD #-}
arrayStreamKFromStreamD :: forall m a. (MonadIO m, Unbox a) =>
D.Stream m a -> m (StreamK m (MutArray a))
arrayStreamKFromStreamD :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (StreamK m (MutArray a))
arrayStreamKFromStreamD =
let n :: Int
n = forall a. Unbox a => a -> Int -> Int
allocBytesToElemCount (forall a. (?callStack::CallStack) => a
undefined :: a) Int
defaultChunkSize
in forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall (m :: * -> *) a. StreamK m a
K.nil forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (MutArray a)
chunksOf Int
n
data FlattenState s contents a =
OuterLoop s
| InnerLoop s contents !Int !Int
{-# INLINE_NORMAL flattenArrays #-}
flattenArrays :: forall m a. (MonadIO m, Unbox a)
=> D.Stream m (MutArray a) -> D.Stream m a
flattenArrays :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (MutArray a) -> Stream m a
flattenArrays (D.Stream State StreamK m (MutArray a) -> s -> m (Step s (MutArray a))
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {a} {m :: * -> *} {a} {a} {a}.
Unbox a =>
State StreamK m a
-> FlattenState s MutableByteArray a
-> m (Step (FlattenState s MutableByteArray a) a)
step' (forall s contents a. s -> FlattenState s contents a
OuterLoop s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> FlattenState s MutableByteArray a
-> m (Step (FlattenState s MutableByteArray a) a)
step' State StreamK m a
gst (OuterLoop s
st) = do
Step s (MutArray a)
r <- State StreamK m (MutArray a) -> s -> m (Step s (MutArray a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (MutArray a)
r of
D.Yield MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} s
s ->
forall s a. s -> Step s a
D.Skip (forall s contents a.
s -> contents -> Int -> Int -> FlattenState s contents a
InnerLoop s
s MutableByteArray
arrContents Int
arrStart Int
arrEnd)
D.Skip s
s -> forall s a. s -> Step s a
D.Skip (forall s contents a. s -> FlattenState s contents a
OuterLoop s
s)
Step s (MutArray a)
D.Stop -> forall s a. Step s a
D.Stop
step' State StreamK m a
_ (InnerLoop s
st MutableByteArray
_ Int
p Int
end) | forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
p forall a. Ord a => a -> a -> Bool
<= Int
end) (Int
p forall a. Eq a => a -> a -> Bool
== Int
end) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s contents a. s -> FlattenState s contents a
OuterLoop s
st
step' State StreamK m a
_ (InnerLoop s
st MutableByteArray
contents Int
p Int
end) = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
contents Int
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall s contents a.
s -> contents -> Int -> Int -> FlattenState s contents a
InnerLoop s
st MutableByteArray
contents (INDEX_NEXT(p,a)) end)
{-# INLINE_NORMAL flattenArraysRev #-}
flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
=> D.Stream m (MutArray a) -> D.Stream m a
flattenArraysRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (MutArray a) -> Stream m a
flattenArraysRev (D.Stream State StreamK m (MutArray a) -> s -> m (Step s (MutArray a))
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {a} {m :: * -> *} {a} {a} {a}.
Unbox a =>
State StreamK m a
-> FlattenState s MutableByteArray a
-> m (Step (FlattenState s MutableByteArray a) a)
step' (forall s contents a. s -> FlattenState s contents a
OuterLoop s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> FlattenState s MutableByteArray a
-> m (Step (FlattenState s MutableByteArray a) a)
step' State StreamK m a
gst (OuterLoop s
st) = do
Step s (MutArray a)
r <- State StreamK m (MutArray a) -> s -> m (Step s (MutArray a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (MutArray a)
r of
D.Yield MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} s
s ->
let p :: Int
p = INDEX_PREV(arrEnd,a)
in forall s a. s -> Step s a
D.Skip (forall s contents a.
s -> contents -> Int -> Int -> FlattenState s contents a
InnerLoop s
s MutableByteArray
arrContents Int
p Int
arrStart)
D.Skip s
s -> forall s a. s -> Step s a
D.Skip (forall s contents a. s -> FlattenState s contents a
OuterLoop s
s)
Step s (MutArray a)
D.Stop -> forall s a. Step s a
D.Stop
step' State StreamK m a
_ (InnerLoop s
st MutableByteArray
_ Int
p Int
start) | Int
p forall a. Ord a => a -> a -> Bool
< Int
start =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s contents a. s -> FlattenState s contents a
OuterLoop s
st
step' State StreamK m a
_ (InnerLoop s
st MutableByteArray
contents Int
p Int
start) = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
contents Int
p
let cur :: Int
cur = INDEX_PREV(p,a)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall s contents a.
s -> contents -> Int -> Int -> FlattenState s contents a
InnerLoop s
st MutableByteArray
contents Int
cur Int
start)
data ArrayUnsafe a = ArrayUnsafe
{-# UNPACK #-} !MutableByteArray
{-# UNPACK #-} !Int
{-# UNPACK #-} !Int
toArrayUnsafe :: MutArray a -> ArrayUnsafe a
toArrayUnsafe :: forall a. MutArray a -> ArrayUnsafe a
toArrayUnsafe (MutArray MutableByteArray
contents Int
start Int
end Int
_) = forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
start Int
end
fromArrayUnsafe ::
#ifdef DEVBUILD
Unbox a =>
#endif
ArrayUnsafe a -> MutArray a
fromArrayUnsafe :: forall a. ArrayUnsafe a -> MutArray a
fromArrayUnsafe (ArrayUnsafe MutableByteArray
contents Int
start Int
end) =
forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
start Int
end Int
end
{-# INLINE_NORMAL producerWith #-}
producerWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> Producer m (MutArray a) a
producerWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Producer m (MutArray a) a
producerWith forall b. IO b -> m b
liftio = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> (s -> m a) -> Producer m a b
Producer forall {a} {a} {a}.
Unbox a =>
ArrayUnsafe a -> m (Step (ArrayUnsafe a) a)
step (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MutArray a -> ArrayUnsafe a
toArrayUnsafe) forall {a}. ArrayUnsafe a -> m (MutArray a)
extract
where
{-# INLINE_LATE step #-}
step :: ArrayUnsafe a -> m (Step (ArrayUnsafe a) a)
step (ArrayUnsafe MutableByteArray
_ Int
cur Int
end)
| forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
cur forall a. Ord a => a -> a -> Bool
<= Int
end) (Int
cur forall a. Eq a => a -> a -> Bool
== Int
end) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step (ArrayUnsafe MutableByteArray
contents Int
cur Int
end) = do
!a
x <- forall b. IO b -> m b
liftio forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
contents Int
cur
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents (INDEX_NEXT(cur,a)) end)
extract :: ArrayUnsafe a -> m (MutArray a)
extract = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ArrayUnsafe a -> MutArray a
fromArrayUnsafe
{-# INLINE_NORMAL producer #-}
producer :: forall m a. (MonadIO m, Unbox a) => Producer m (MutArray a) a
producer :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Producer m (MutArray a) a
producer = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Producer m (MutArray a) a
producerWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE_NORMAL reader #-}
reader :: forall m a. (MonadIO m, Unbox a) => Unfold m (MutArray a) a
reader :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Unfold m (MutArray a) a
reader = forall (m :: * -> *) a b. Producer m a b -> Unfold m a b
Producer.simplify forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Producer m (MutArray a) a
producer
{-# INLINE_NORMAL readerRevWith #-}
readerRevWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> Unfold m (MutArray a) a
readerRevWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Unfold m (MutArray a) a
readerRevWith forall b. IO b -> m b
liftio = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {a} {a} {a}.
Unbox a =>
ArrayUnsafe a -> m (Step (ArrayUnsafe a) a)
step forall {m :: * -> *} {a} {a}.
Monad m =>
MutArray a -> m (ArrayUnsafe a)
inject
where
inject :: MutArray a -> m (ArrayUnsafe a)
inject (MutArray MutableByteArray
contents Int
start Int
end Int
_) =
let p :: Int
p = INDEX_PREV(end,a)
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
start Int
p
{-# INLINE_LATE step #-}
step :: ArrayUnsafe a -> m (Step (ArrayUnsafe a) a)
step (ArrayUnsafe MutableByteArray
_ Int
start Int
p) | Int
p forall a. Ord a => a -> a -> Bool
< Int
start = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step (ArrayUnsafe MutableByteArray
contents Int
start Int
p) = do
!a
x <- forall b. IO b -> m b
liftio forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
contents Int
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
start (INDEX_PREV(p,a)))
{-# INLINE_NORMAL readerRev #-}
readerRev :: forall m a. (MonadIO m, Unbox a) => Unfold m (MutArray a) a
readerRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Unfold m (MutArray a) a
readerRev = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Unfold m (MutArray a) a
readerRevWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE toList #-}
toList :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m [a]
toList :: forall (m :: * -> *) a. (MonadIO m, Unbox a) => MutArray a -> m [a]
toList MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall {a}. Unbox a => Int -> IO [a]
go Int
arrStart
where
go :: Int -> IO [a]
go Int
p | forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
p forall a. Ord a => a -> a -> Bool
<= Int
arrEnd) (Int
p forall a. Eq a => a -> a -> Bool
== Int
arrEnd) = forall (m :: * -> *) a. Monad m => a -> m a
return []
go Int
p = do
a
x <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
p
(:) a
x forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO [a]
go (INDEX_NEXT(p,a))
{-# INLINE_NORMAL toStreamDWith #-}
toStreamDWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> MutArray a -> D.Stream m a
toStreamDWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
toStreamDWith forall b. IO b -> m b
liftio MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {a} {p}. Unbox a => p -> Int -> m (Step Int a)
step Int
arrStart
where
{-# INLINE_LATE step #-}
step :: p -> Int -> m (Step Int a)
step p
_ Int
p | forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
p forall a. Ord a => a -> a -> Bool
<= Int
arrEnd) (Int
p forall a. Eq a => a -> a -> Bool
== Int
arrEnd) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step p
_ Int
p = forall b. IO b -> m b
liftio forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
r (INDEX_NEXT(p,a))
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
toStreamD :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> Stream m a
toStreamD = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
toStreamDWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE toStreamKWith #-}
toStreamKWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKWith forall b. IO b -> m b
liftio MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall {a}. Unbox a => Int -> StreamK m a
go Int
arrStart
where
go :: Int -> StreamK m a
go Int
p | forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
p forall a. Ord a => a -> a -> Bool
<= Int
arrEnd) (Int
p forall a. Eq a => a -> a -> Bool
== Int
arrEnd) = forall (m :: * -> *) a. StreamK m a
K.nil
| Bool
otherwise =
let elemM :: IO a
elemM = forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
p
in forall b. IO b -> m b
liftio IO a
elemM forall (m :: * -> *) a.
Monad m =>
m a -> StreamK m a -> StreamK m a
`K.consM` Int -> StreamK m a
go (INDEX_NEXT(p,a))
{-# INLINE toStreamK #-}
toStreamK :: forall m a. (MonadIO m, Unbox a) => MutArray a -> StreamK m a
toStreamK :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> StreamK m a
toStreamK = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE_NORMAL toStreamDRevWith #-}
toStreamDRevWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> MutArray a -> D.Stream m a
toStreamDRevWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
toStreamDRevWith forall b. IO b -> m b
liftio MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let p :: Int
p = INDEX_PREV(arrEnd,a)
in forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {a} {p}. Unbox a => p -> Int -> m (Step Int a)
step Int
p
where
{-# INLINE_LATE step #-}
step :: p -> Int -> m (Step Int a)
step p
_ Int
p | Int
p forall a. Ord a => a -> a -> Bool
< Int
arrStart = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step p
_ Int
p = forall b. IO b -> m b
liftio forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
r (INDEX_PREV(p,a))
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> D.Stream m a
toStreamDRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> Stream m a
toStreamDRev = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
toStreamDRevWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE toStreamKRevWith #-}
toStreamKRevWith ::
forall m a. (Monad m, Unbox a)
=> (forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKRevWith :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKRevWith forall b. IO b -> m b
liftio MutArray {Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let p :: Int
p = INDEX_PREV(arrEnd,a)
in forall {a}. Unbox a => Int -> StreamK m a
go Int
p
where
go :: Int -> StreamK m a
go Int
p | Int
p forall a. Ord a => a -> a -> Bool
< Int
arrStart = forall (m :: * -> *) a. StreamK m a
K.nil
| Bool
otherwise =
let elemM :: IO a
elemM = forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
p
in forall b. IO b -> m b
liftio IO a
elemM forall (m :: * -> *) a.
Monad m =>
m a -> StreamK m a -> StreamK m a
`K.consM` Int -> StreamK m a
go (INDEX_PREV(p,a))
{-# INLINE toStreamKRev #-}
toStreamKRev :: forall m a. (MonadIO m, Unbox a) => MutArray a -> StreamK m a
toStreamKRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> StreamK m a
toStreamKRev = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
toStreamKRevWith forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE_NORMAL foldl' #-}
foldl' :: (MonadIO m, Unbox a) => (b -> a -> b) -> b -> MutArray a -> m b
foldl' :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
(b -> a -> b) -> b -> MutArray a -> m b
foldl' b -> a -> b
f b
z MutArray a
arr = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
D.foldl' b -> a -> b
f b
z forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> Stream m a
toStreamD MutArray a
arr
{-# INLINE_NORMAL foldr #-}
foldr :: (MonadIO m, Unbox a) => (a -> b -> b) -> b -> MutArray a -> m b
foldr :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
(a -> b -> b) -> b -> MutArray a -> m b
foldr a -> b -> b
f b
z MutArray a
arr = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr a -> b -> b
f b
z forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> Stream m a
toStreamD MutArray a
arr
{-# INLINE_NORMAL writeAppendNUnsafe #-}
writeAppendNUnsafe :: forall m a. (MonadIO m, Unbox a) =>
Int
-> m (MutArray a)
-> Fold m a (MutArray a)
writeAppendNUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendNUnsafe Int
n m (MutArray a)
action =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. ArrayUnsafe a -> MutArray a
fromArrayUnsafe forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall {m :: * -> *} {a} {a} {a}.
(MonadIO m, Unbox a) =>
ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step m (ArrayUnsafe a)
initial
where
initial :: m (ArrayUnsafe a)
initial = do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
>= Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
arr :: MutArray a
arr@(MutArray MutableByteArray
_ Int
_ Int
end Int
bound) <- m (MutArray a)
action
let free :: Int
free = Int
bound forall a. Num a => a -> a -> a
- Int
end
needed :: Int
needed = Int
n forall a. Num a => a -> a -> a
* SIZE_OF(a)
MutArray a
arr1 <-
if Int
free forall a. Ord a => a -> a -> Bool
< Int
needed
then forall a. a -> a
noinline forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[Char] -> (Int -> Int) -> Int -> MutArray a -> m (MutArray a)
reallocWith [Char]
"writeAppendNUnsafeWith" (forall a. Num a => a -> a -> a
+ Int
needed) Int
needed MutArray a
arr
else forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> ArrayUnsafe a
toArrayUnsafe MutArray a
arr1
step :: ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step (ArrayUnsafe MutableByteArray
contents Int
start Int
end) a
x = do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
contents Int
end a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
start (INDEX_NEXT(end,a))
{-# INLINE_NORMAL writeAppendN #-}
writeAppendN :: forall m a. (MonadIO m, Unbox a) =>
Int -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendN Int
n m (MutArray a)
initial = forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendNUnsafe Int
n m (MutArray a)
initial)
{-# INLINE writeAppendWith #-}
writeAppendWith :: forall m a. (MonadIO m, Unbox a) =>
(Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendWith Int -> Int
sizer = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> MutArray a -> a -> m (MutArray a)
snocWith Int -> Int
sizer)
{-# INLINE writeAppend #-}
writeAppend :: forall m a. (MonadIO m, Unbox a) =>
m (MutArray a) -> Fold m a (MutArray a)
writeAppend :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
m (MutArray a) -> Fold m a (MutArray a)
writeAppend = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int) -> m (MutArray a) -> Fold m a (MutArray a)
writeAppendWith (forall a. Num a => a -> a -> a
* Int
2)
{-# INLINE_NORMAL writeNWithUnsafe #-}
writeNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
=> (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWithUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWithUnsafe Int -> m (MutArray a)
alloc Int
n = forall a. ArrayUnsafe a -> MutArray a
fromArrayUnsafe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall {m :: * -> *} {a} {a} {a}.
(MonadIO m, Unbox a) =>
ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step m (ArrayUnsafe a)
initial
where
initial :: m (ArrayUnsafe a)
initial = forall a. MutArray a -> ArrayUnsafe a
toArrayUnsafe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (MutArray a)
alloc (forall a. Ord a => a -> a -> a
max Int
n Int
0)
step :: ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step (ArrayUnsafe MutableByteArray
contents Int
start Int
end) a
x = do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
contents Int
end a
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
start (INDEX_NEXT(end,a))
{-# INLINE_NORMAL writeNUnsafe #-}
writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
=> Int -> Fold m a (MutArray a)
writeNUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeNUnsafe = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWithUnsafe forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned
{-# INLINE_NORMAL writeNWith #-}
writeNWith :: forall m a. (MonadIO m, Unbox a)
=> (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWith Int -> m (MutArray a)
alloc Int
n = forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWithUnsafe Int -> m (MutArray a)
alloc Int
n)
{-# INLINE_NORMAL writeN #-}
writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
writeN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeN = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWith forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned
{-# INLINE_NORMAL writeRevNWithUnsafe #-}
writeRevNWithUnsafe :: forall m a. (MonadIO m, Unbox a)
=> (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWithUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWithUnsafe Int -> m (MutArray a)
alloc Int
n = forall a. ArrayUnsafe a -> MutArray a
fromArrayUnsafe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall {m :: * -> *} {a} {a} {a}.
(MonadIO m, Unbox a) =>
ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step forall {a}. m (ArrayUnsafe a)
initial
where
toArrayUnsafeRev :: MutArray a -> ArrayUnsafe a
toArrayUnsafeRev (MutArray MutableByteArray
contents Int
_ Int
_ Int
bound) =
forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
bound Int
bound
initial :: m (ArrayUnsafe a)
initial = forall {a} {a}. MutArray a -> ArrayUnsafe a
toArrayUnsafeRev forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (MutArray a)
alloc (forall a. Ord a => a -> a -> a
max Int
n Int
0)
step :: ArrayUnsafe a -> a -> m (ArrayUnsafe a)
step (ArrayUnsafe MutableByteArray
contents Int
start Int
end) a
x = do
let ptr :: Int
ptr = INDEX_PREV(start,a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
contents Int
ptr a
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall a. MutableByteArray -> Int -> Int -> ArrayUnsafe a
ArrayUnsafe MutableByteArray
contents Int
ptr Int
end
{-# INLINE_NORMAL writeRevNWith #-}
writeRevNWith :: forall m a. (MonadIO m, Unbox a)
=> (Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWith Int -> m (MutArray a)
alloc Int
n = forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWithUnsafe Int -> m (MutArray a)
alloc Int
n)
{-# INLINE_NORMAL writeRevN #-}
writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
writeRevN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeRevN = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeRevNWith forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned
{-# INLINE_NORMAL writeNAligned #-}
writeNAligned :: forall m a. (MonadIO m, Unbox a)
=> Int -> Int -> Fold m a (MutArray a)
writeNAligned :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> Fold m a (MutArray a)
writeNAligned Int
align = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> m (MutArray a)) -> Int -> Fold m a (MutArray a)
writeNWith (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> m (MutArray a)
newAlignedPinned Int
align)
{-# INLINE_NORMAL writeChunks #-}
writeChunks :: (MonadIO m, Unbox a) =>
Int -> Fold m a (StreamK n (MutArray a))
writeChunks :: forall (m :: * -> *) a (n :: * -> *).
(MonadIO m, Unbox a) =>
Int -> Fold m a (StreamK n (MutArray a))
writeChunks Int
n = forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
FL.many (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeN Int
n) forall (m :: * -> *) a (n :: * -> *).
Monad m =>
Fold m a (StreamK n a)
FL.toStreamK
{-# INLINE_NORMAL writeWith #-}
writeWith :: forall m a. (MonadIO m, Unbox a)
=> Int -> Fold m a (MutArray a)
writeWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeWith Int
elemCount =
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Fold m a b -> Fold m a c
FL.rmapM MutArray a -> m (MutArray a)
extract forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
step m (MutArray a)
initial
where
initial :: m (MutArray a)
initial = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
elemCount forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$ forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"writeWith: elemCount is negative"
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned Int
elemCount
step :: MutArray a -> a -> m (MutArray a)
step arr :: MutArray a
arr@(MutArray MutableByteArray
_ Int
start Int
end Int
bound) a
x
| INDEX_NEXT(end,a) > bound = do
let oldSize = end - start
newSize = max (oldSize * 2) 1
arr1 <- liftIO $ reallocExplicit (SIZE_OF(a)) newSize arr
snocUnsafe arr1 x
step MutArray a
arr a
x = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> a -> m (MutArray a)
snocUnsafe MutArray a
arr a
x
extract :: MutArray a -> m (MutArray a)
extract = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> m (MutArray a)
rightSize
{-# INLINE write #-}
write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
write :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Fold m a (MutArray a)
write = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeWith (forall a. Unbox a => a -> Int -> Int
allocBytesToElemCount (forall a. (?callStack::CallStack) => a
undefined :: a) Int
arrayChunkBytes)
{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: forall m a. (MonadIO m, Unbox a)
=> Int -> D.Stream m a -> m (MutArray a)
fromStreamDN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> m (MutArray a)
fromStreamDN Int
limit Stream m a
str = do
(MutArray a
arr :: MutArray a) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
newPinned Int
limit
Int
end <- forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
D.foldlM' (forall {m :: * -> *} {a}.
(MonadIO m, Unbox a) =>
MutableByteArray -> Int -> a -> m Int
fwrite (forall a. MutArray a -> MutableByteArray
arrContents MutArray a
arr)) (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Int
arrEnd MutArray a
arr) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
Applicative m =>
Int -> Stream m a -> Stream m a
D.take Int
limit Stream m a
str
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray a
arr {arrEnd :: Int
arrEnd = Int
end}
where
fwrite :: MutableByteArray -> Int -> a -> m Int
fwrite MutableByteArray
arrContents Int
ptr a
x = do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
arrContents Int
ptr a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ INDEX_NEXT(ptr,a)
{-# INLINABLE fromListN #-}
fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
fromListN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
fromListN Int
n [a]
xs = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> m (MutArray a)
fromStreamDN Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs
{-# INLINE fromListRevN #-}
fromListRevN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
fromListRevN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
fromListRevN Int
n [a]
xs = forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
D.fold (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
writeRevN Int
n) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs
{-# INLINE arrayStreamKLength #-}
arrayStreamKLength :: (Monad m, Unbox a) => StreamK m (MutArray a) -> m Int
arrayStreamKLength :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
StreamK m (MutArray a) -> m Int
arrayStreamKLength StreamK m (MutArray a)
as = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> StreamK m a -> m b
K.foldl' forall a. Num a => a -> a -> a
(+) Int
0 (forall a b (m :: * -> *). (a -> b) -> StreamK m a -> StreamK m b
K.map forall a. Unbox a => MutArray a -> Int
length StreamK m (MutArray a)
as)
{-# INLINE fromArrayStreamK #-}
fromArrayStreamK :: (Unbox a, MonadIO m) =>
StreamK m (MutArray a) -> m (MutArray a)
fromArrayStreamK :: forall a (m :: * -> *).
(Unbox a, MonadIO m) =>
StreamK m (MutArray a) -> m (MutArray a)
fromArrayStreamK StreamK m (MutArray a)
as = do
Int
len <- forall (m :: * -> *) a.
(Monad m, Unbox a) =>
StreamK m (MutArray a) -> m Int
arrayStreamKLength StreamK m (MutArray a)
as
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> m (MutArray a)
fromStreamDN Int
len forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Unfold m (MutArray a) a
reader forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m (MutArray a)
as
{-# INLINE fromStreamD #-}
fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (MutArray a)
fromStreamD :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
fromStreamD Stream m a
m = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (StreamK m (MutArray a))
arrayStreamKFromStreamD Stream m a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a (m :: * -> *).
(Unbox a, MonadIO m) =>
StreamK m (MutArray a) -> m (MutArray a)
fromArrayStreamK
{-# INLINE fromList #-}
fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
fromList :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[a] -> m (MutArray a)
fromList [a]
xs = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
fromStreamD forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs
{-# INLINE fromListRev #-}
fromListRev :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
fromListRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[a] -> m (MutArray a)
fromListRev [a]
xs = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
fromListRevN (forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
xs) [a]
xs
{-# INLINE putSliceUnsafe #-}
putSliceUnsafe :: MonadIO m => MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
putSliceUnsafe :: forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
putSliceUnsafe MutArray a
src Int
srcStartBytes MutArray a
dst Int
dstStartBytes Int
lenBytes = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
assertM(Int
lenBytes forall a. Ord a => a -> a -> Bool
<= forall a. MutArray a -> Int
arrBound MutArray a
dst forall a. Num a => a -> a -> a
- Int
dstStartBytes)
assertM(Int
lenBytes forall a. Ord a => a -> a -> Bool
<= forall a. MutArray a -> Int
arrEnd MutArray a
src forall a. Num a => a -> a -> a
- Int
srcStartBytes)
let !(I# Int#
srcStartBytes#) = Int
srcStartBytes
!(I# Int#
dstStartBytes#) = Int
dstStartBytes
!(I# Int#
lenBytes#) = Int
lenBytes
let arrS# :: MutableByteArray# RealWorld
arrS# = MutableByteArray -> MutableByteArray# RealWorld
getMutableByteArray# (forall a. MutArray a -> MutableByteArray
arrContents MutArray a
src)
arrD# :: MutableByteArray# RealWorld
arrD# = MutableByteArray -> MutableByteArray# RealWorld
getMutableByteArray# (forall a. MutArray a -> MutableByteArray
arrContents MutArray a
dst)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s# -> (# forall d.
MutableByteArray# d
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyMutableByteArray#
MutableByteArray# RealWorld
arrS# Int#
srcStartBytes# MutableByteArray# RealWorld
arrD# Int#
dstStartBytes# Int#
lenBytes# State# RealWorld
s#
, () #)
{-# INLINE spliceCopy #-}
spliceCopy :: forall m a. MonadIO m =>
#ifdef DEVBUILD
Unbox a =>
#endif
MutArray a -> MutArray a -> m (MutArray a)
spliceCopy :: forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> MutArray a -> m (MutArray a)
spliceCopy MutArray a
arr1 MutArray a
arr2 = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let start1 :: Int
start1 = forall a. MutArray a -> Int
arrStart MutArray a
arr1
start2 :: Int
start2 = forall a. MutArray a -> Int
arrStart MutArray a
arr2
len1 :: Int
len1 = forall a. MutArray a -> Int
arrEnd MutArray a
arr1 forall a. Num a => a -> a -> a
- Int
start1
len2 :: Int
len2 = forall a. MutArray a -> Int
arrEnd MutArray a
arr2 forall a. Num a => a -> a -> a
- Int
start2
MutableByteArray
newArrContents <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> IO MutableByteArray
Unboxed.newPinnedBytes (Int
len1 forall a. Num a => a -> a -> a
+ Int
len2)
let len :: Int
len = Int
len1 forall a. Num a => a -> a -> a
+ Int
len2
newArr :: MutArray a
newArr = forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
newArrContents Int
0 Int
len Int
len
forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
putSliceUnsafe MutArray a
arr1 Int
start1 forall a. MutArray a
newArr Int
0 Int
len1
forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
putSliceUnsafe MutArray a
arr2 Int
start2 forall a. MutArray a
newArr Int
len1 Int
len2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. MutArray a
newArr
{-# INLINE spliceUnsafe #-}
spliceUnsafe :: MonadIO m =>
MutArray a -> MutArray a -> m (MutArray a)
spliceUnsafe :: forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> MutArray a -> m (MutArray a)
spliceUnsafe MutArray a
dst MutArray a
src =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let startSrc :: Int
startSrc = forall a. MutArray a -> Int
arrStart MutArray a
src
srcLen :: Int
srcLen = forall a. MutArray a -> Int
arrEnd MutArray a
src forall a. Num a => a -> a -> a
- Int
startSrc
endDst :: Int
endDst = forall a. MutArray a -> Int
arrEnd MutArray a
dst
assertM(Int
endDst forall a. Num a => a -> a -> a
+ Int
srcLen forall a. Ord a => a -> a -> Bool
<= forall a. MutArray a -> Int
arrBound MutArray a
dst)
forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> Int -> MutArray a -> Int -> Int -> m ()
putSliceUnsafe MutArray a
src Int
startSrc MutArray a
dst Int
endDst Int
srcLen
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ MutArray a
dst {arrEnd :: Int
arrEnd = Int
endDst forall a. Num a => a -> a -> a
+ Int
srcLen}
{-# INLINE spliceWith #-}
spliceWith :: forall m a. (MonadIO m, Unbox a) =>
(Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
spliceWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
spliceWith Int -> Int -> Int
sizer dst :: MutArray a
dst@(MutArray MutableByteArray
_ Int
start Int
end Int
bound) MutArray a
src = do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
end forall a. Ord a => a -> a -> Bool
<= Int
bound) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let srcBytes :: Int
srcBytes = forall a. MutArray a -> Int
arrEnd MutArray a
src forall a. Num a => a -> a -> a
- forall a. MutArray a -> Int
arrStart MutArray a
src
MutArray a
dst1 <-
if Int
end forall a. Num a => a -> a -> a
+ Int
srcBytes forall a. Ord a => a -> a -> Bool
>= Int
bound
then do
let dstBytes :: Int
dstBytes = Int
end forall a. Num a => a -> a -> a
- Int
start
newSizeInBytes :: Int
newSizeInBytes = Int -> Int -> Int
sizer Int
dstBytes Int
srcBytes
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
newSizeInBytes forall a. Ord a => a -> a -> Bool
< Int
dstBytes forall a. Num a => a -> a -> a
+ Int
srcBytes)
forall a b. (a -> b) -> a -> b
$ forall a. (?callStack::CallStack) => [Char] -> a
error
forall a b. (a -> b) -> a -> b
$ [Char]
"splice: newSize is less than the total size "
forall a. [a] -> [a] -> [a]
++ [Char]
"of arrays being appended. Please check the "
forall a. [a] -> [a] -> [a]
++ [Char]
"sizer function passed."
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m (MutArray a)
realloc Int
newSizeInBytes MutArray a
dst
else forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
dst
forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> MutArray a -> m (MutArray a)
spliceUnsafe MutArray a
dst1 MutArray a
src
{-# INLINE splice #-}
splice :: (MonadIO m, Unbox a) => MutArray a -> MutArray a -> m (MutArray a)
splice :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> MutArray a -> m (MutArray a)
splice = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
spliceWith forall a. Num a => a -> a -> a
(+)
{-# INLINE spliceExp #-}
spliceExp :: (MonadIO m, Unbox a) => MutArray a -> MutArray a -> m (MutArray a)
spliceExp :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> MutArray a -> m (MutArray a)
spliceExp = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(Int -> Int -> Int) -> MutArray a -> MutArray a -> m (MutArray a)
spliceWith (\Int
l1 Int
l2 -> forall a. Ord a => a -> a -> a
max (Int
l1 forall a. Num a => a -> a -> a
* Int
2) (Int
l1 forall a. Num a => a -> a -> a
+ Int
l2))
{-# INLINE breakOn #-}
breakOn :: MonadIO m
=> Word8 -> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
breakOn :: forall (m :: * -> *).
MonadIO m =>
Word8
-> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
breakOn Word8
sep arr :: MutArray Word8
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a b.
MonadIO m =>
MutArray a -> (Ptr a -> m b) -> m b
asPtrUnsafe MutArray Word8
arr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Ptr Word8
loc <- Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)
c_memchr Ptr Word8
p Word8
sep (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Int
byteLength MutArray Word8
arr)
let sepIndex :: Int
sepIndex = Ptr Word8
loc forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Ptr Word8
loc forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr
then (MutArray Word8
arr, forall a. Maybe a
Nothing)
else
( MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
arrContents
, arrStart :: Int
arrStart = Int
arrStart
, arrEnd :: Int
arrEnd = Int
arrStart forall a. Num a => a -> a -> a
+ Int
sepIndex
, arrBound :: Int
arrBound = Int
arrStart forall a. Num a => a -> a -> a
+ Int
sepIndex
}
, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
arrContents
, arrStart :: Int
arrStart = Int
arrStart forall a. Num a => a -> a -> a
+ (Int
sepIndex forall a. Num a => a -> a -> a
+ Int
1)
, arrEnd :: Int
arrEnd = Int
arrEnd
, arrBound :: Int
arrBound = Int
arrBound
}
)
splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
splitAt Int
i arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} =
let maxIndex :: Int
maxIndex = forall a. Unbox a => MutArray a -> Int
length MutArray a
arr forall a. Num a => a -> a -> a
- Int
1
in if Int
i forall a. Ord a => a -> a -> Bool
< Int
0
then forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"sliceAt: negative array index"
else if Int
i forall a. Ord a => a -> a -> Bool
> Int
maxIndex
then forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"sliceAt: specified array index " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
i
forall a. [a] -> [a] -> [a]
++ [Char]
" is beyond the maximum index " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
maxIndex
else let off :: Int
off = Int
i forall a. Num a => a -> a -> a
* SIZE_OF(a)
p :: Int
p = Int
arrStart forall a. Num a => a -> a -> a
+ Int
off
in ( MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
arrContents
, arrStart :: Int
arrStart = Int
arrStart
, arrEnd :: Int
arrEnd = Int
p
, arrBound :: Int
arrBound = Int
p
}
, MutArray
{ arrContents :: MutableByteArray
arrContents = MutableByteArray
arrContents
, arrStart :: Int
arrStart = Int
p
, arrEnd :: Int
arrEnd = Int
arrEnd
, arrBound :: Int
arrBound = Int
arrBound
}
)
castUnsafe ::
#ifdef DEVBUILD
Unbox b =>
#endif
MutArray a -> MutArray b
castUnsafe :: forall a b. MutArray a -> MutArray b
castUnsafe (MutArray MutableByteArray
contents Int
start Int
end Int
bound) =
forall a. MutableByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutableByteArray
contents Int
start Int
end Int
bound
asBytes :: MutArray a -> MutArray Word8
asBytes :: forall a. MutArray a -> MutArray Word8
asBytes = forall a b. MutArray a -> MutArray b
castUnsafe
cast :: forall a b. Unbox b => MutArray a -> Maybe (MutArray b)
cast :: forall a b. Unbox b => MutArray a -> Maybe (MutArray b)
cast MutArray a
arr =
let len :: Int
len = forall a. MutArray a -> Int
byteLength MutArray a
arr
r :: Int
r = Int
len forall a. Integral a => a -> a -> a
`mod` SIZE_OF(b)
in if Int
r forall a. Eq a => a -> a -> Bool
/= Int
0
then forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. MutArray a -> MutArray b
castUnsafe MutArray a
arr
asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
asPtrUnsafe :: forall (m :: * -> *) a b.
MonadIO m =>
MutArray a -> (Ptr a -> m b) -> m b
asPtrUnsafe MutArray a
arr Ptr a -> m b
f = do
let contents :: MutableByteArray
contents = forall a. MutArray a -> MutableByteArray
arrContents MutArray a
arr
!ptr :: Ptr a
ptr = forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents#
(unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# (MutableByteArray -> MutableByteArray# RealWorld
getMutableByteArray# MutableByteArray
contents)))
b
r <- Ptr a -> m b
f (forall a. Ptr a
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall a. MutArray a -> Int
arrStart MutArray a
arr)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ MutableByteArray -> IO ()
touch MutableByteArray
contents
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINE cmp #-}
cmp :: MonadIO m => MutArray a -> MutArray a -> m Ordering
cmp :: forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> MutArray a -> m Ordering
cmp MutArray a
arr1 MutArray a
arr2 =
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
forall a b. (a -> b) -> a -> b
$ do
let marr1 :: MutableByteArray# RealWorld
marr1 = MutableByteArray -> MutableByteArray# RealWorld
getMutableByteArray# (forall a. MutArray a -> MutableByteArray
arrContents MutArray a
arr1)
marr2 :: MutableByteArray# RealWorld
marr2 = MutableByteArray -> MutableByteArray# RealWorld
getMutableByteArray# (forall a. MutArray a -> MutableByteArray
arrContents MutArray a
arr2)
!(I# Int#
st1#) = forall a. MutArray a -> Int
arrStart MutArray a
arr1
!(I# Int#
st2#) = forall a. MutArray a -> Int
arrStart MutArray a
arr2
!(I# Int#
len#) = forall a. MutArray a -> Int
byteLength MutArray a
arr1
case forall a. Ord a => a -> a -> Ordering
compare (forall a. MutArray a -> Int
byteLength MutArray a
arr1) (forall a. MutArray a -> Int
byteLength MutArray a
arr2) of
Ordering
EQ -> do
Int
r <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s# ->
let res :: Int
res =
Int# -> Int
I#
(ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> Int#
compareByteArrays#
(unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# MutableByteArray# RealWorld
marr1)
Int#
st1#
(unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# MutableByteArray# RealWorld
marr2)
Int#
st2#
Int#
len#)
in (# State# RealWorld
s#, Int
res #)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> Ordering
compare Int
r Int
0
Ordering
x -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
x
{-# INLINE strip #-}
strip :: forall a m. (Unbox a, MonadIO m) =>
(a -> Bool) -> MutArray a -> m (MutArray a)
strip :: forall a (m :: * -> *).
(Unbox a, MonadIO m) =>
(a -> Bool) -> MutArray a -> m (MutArray a)
strip a -> Bool
eq arr :: MutArray a
arr@MutArray{Int
MutableByteArray
arrBound :: Int
arrEnd :: Int
arrStart :: Int
arrContents :: MutableByteArray
arrBound :: forall a. MutArray a -> Int
arrEnd :: forall a. MutArray a -> Int
arrStart :: forall a. MutArray a -> Int
arrContents :: forall a. MutArray a -> MutableByteArray
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Int
st <- Int -> IO Int
getStart Int
arrStart
Int
end <- Int -> Int -> IO Int
getLast Int
arrEnd Int
st
forall (m :: * -> *) a. Monad m => a -> m a
return MutArray a
arr {arrStart :: Int
arrStart = Int
st, arrEnd :: Int
arrEnd = Int
end, arrBound :: Int
arrBound = Int
end}
where
getStart :: Int -> IO Int
getStart Int
cur = do
if Int
cur forall a. Ord a => a -> a -> Bool
< Int
arrEnd
then do
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
cur
if a -> Bool
eq a
r
then Int -> IO Int
getStart (INDEX_NEXT(cur,a))
else forall (m :: * -> *) a. Monad m => a -> m a
return Int
cur
else forall (m :: * -> *) a. Monad m => a -> m a
return Int
cur
getLast :: Int -> Int -> IO Int
getLast Int
cur Int
low = do
if Int
cur forall a. Ord a => a -> a -> Bool
> Int
low
then do
let prev :: Int
prev = INDEX_PREV(cur,a)
a
r <- forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
arrContents Int
prev
if a -> Bool
eq a
r
then Int -> Int -> IO Int
getLast Int
prev Int
low
else forall (m :: * -> *) a. Monad m => a -> m a
return Int
cur
else forall (m :: * -> *) a. Monad m => a -> m a
return Int
cur
{-# INLINE bubble #-}
bubble :: (MonadIO m, Unbox a) => (a -> a -> Ordering) -> MutArray a -> m ()
bubble :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
(a -> a -> Ordering) -> MutArray a -> m ()
bubble a -> a -> Ordering
cmp0 MutArray a
arr =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
l forall a. Ord a => a -> a -> Bool
> Int
1) forall a b. (a -> b) -> a -> b
$ do
a
x <- forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndexUnsafe (Int
l forall a. Num a => a -> a -> a
- Int
1) MutArray a
arr
forall {m :: * -> *}. MonadIO m => a -> Int -> m ()
go a
x (Int
l forall a. Num a => a -> a -> a
- Int
2)
where
l :: Int
l = forall a. Unbox a => MutArray a -> Int
length MutArray a
arr
go :: a -> Int -> m ()
go a
x Int
i =
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
0
then do
a
x1 <- forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
getIndexUnsafe Int
i MutArray a
arr
case a
x a -> a -> Ordering
`cmp0` a
x1 of
Ordering
LT -> do
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndexUnsafe (Int
i forall a. Num a => a -> a -> a
+ Int
1) MutArray a
arr a
x1
a -> Int -> m ()
go a
x (Int
i forall a. Num a => a -> a -> a
- Int
1)
Ordering
_ -> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndexUnsafe (Int
i forall a. Num a => a -> a -> a
+ Int
1) MutArray a
arr a
x
else forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> a -> m ()
putIndexUnsafe (Int
i forall a. Num a => a -> a -> a
+ Int
1) MutArray a
arr a
x