Change the API of unsafeWriteIndex and add fromListIO

This commit is contained in:
Adithya Kumar 2021-07-26 16:14:42 +05:30 committed by Adithya Kumar
parent f197114988
commit 9bda2e67b9
2 changed files with 11 additions and 8 deletions

View File

@ -26,6 +26,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
-- * From containers
, fromList
, fromListN
, fromListIO
, fromStreamDN
, fromStreamD
@ -303,15 +304,12 @@ unsafeWithNewArray count f = do
-------------------------------------------------------------------------------
{-# INLINE unsafeWriteIndex #-}
unsafeWriteIndex :: forall a. Storable a => Array a -> Int -> a -> IO (Array a)
unsafeWriteIndex arr@Array {..} i x =
unsafeWriteIndex :: forall a. Storable a => Array a -> Int -> a -> IO ()
unsafeWriteIndex Array {..} i x =
unsafeWithForeignPtr aStart
$ \begin -> do
poke (begin `plusPtr` (i * sizeOf (undefined :: a))) x
return arr
-- XXX grow the array when we are beyond bound.
--
-- Internal routine for when the array is being created. Appends one item at
-- the end of the array. Useful when sequentially writing a stream to the
-- array.
@ -981,6 +979,11 @@ fromStreamD m = do
len <- K.foldl' (+) 0 (K.map length buffered)
fromStreamDN len $ D.unfoldMany read $ D.fromStreamK buffered
-- XXX Replace fromList with this
{-# INLINABLE fromListIO #-}
fromListIO :: Storable a => [a] -> IO (Array a)
fromListIO xs = fromStreamD $ D.fromList xs
-- | Create an 'Array' from a list. The list must be of finite size.
--
-- @since 0.7.0

View File

@ -190,9 +190,9 @@ testArraysOf =
unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
unsafeWriteIndex xs i x = do
let arr = MA.fromList xs
arr1 <- MA.unsafeWriteIndex arr i x
x1 <- MA.unsafeIndexIO arr1 i
arr <- MA.fromListIO xs
MA.unsafeWriteIndex arr i x
x1 <- MA.unsafeIndexIO arr i
return $ x1 == x
lastN :: Int -> [a] -> [a]