@@ -106,6 +106,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
106 -- * Stream of arrays
106 -- * Stream of arrays
107 , arraysOf
107 , arraysOf
108 , writeChunks
108 , writeChunks
110 -- * Utilities
111 -- * Utilities
111 , bytesToElemCount
112 , bytesToElemCount
@@ -1244,12 +1245,16 @@ writeChunks n = FL.many (writeN n) FL.toStreamK
1245-- /Caution! Do not use this on infinite streams./
1246-- /Caution! Do not use this on infinite streams./
1248+-- >>> f n = Array.appendWith (* 2) (Array.newArray n)
1249+-- >>> writeWith n = Fold.rmapM Array.rightSize (f n)
1250+-- >>> writeWith n = Fold.rmapM Array.fromArrayStreamK (Array.writeChunks n)
1247-- /Pre-release/
1252-- /Pre-release/
1248{-# INLINE_NORMAL writeWith #-}
1253{-# INLINE_NORMAL writeWith #-}
1249writeWith :: forall m a. (MonadIO m, Storable a)
1254writeWith :: forall m a. (MonadIO m, Storable a)
1250− => Int -> Int -> Fold m a (Array a)
1255+ => Int -> Fold m a (Array a)
1251−-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
1256+-- writeWith n = FL.rmapM rightSize $ appendWith (* 2) (newArray n)
1252−writeWith alignSize elemCount =
1257+writeWith elemCount =
1253 FL.rmapM extract $ FL.foldlM' step initial
1258 FL.rmapM extract $ FL.foldlM' step initial
@@ -1260,7 +1265,7 @@ writeWith alignSize elemCount =
1261 initial = do
1266 initial = do
1262 when (elemCount < 0) $ error "writeWith: elemCount is negative"
1267 when (elemCount < 0) $ error "writeWith: elemCount is negative"
1263− liftIO $ newArrayAligned alignSize elemCount
1268+ liftIO $ newArrayAligned (alignment (undefined :: a)) elemCount
1264 step arr@(Array start end bound) x
1269 step arr@(Array start end bound) x
1265 | end `plusPtr` sizeOf (undefined :: a) > bound = do
1270 | end `plusPtr` sizeOf (undefined :: a) > bound = do
1266 let p = unsafeForeignPtrToPtr start
1271 let p = unsafeForeignPtrToPtr start
@@ -1269,7 +1274,10 @@ writeWith alignSize elemCount =
1271 $ reallocAligned
1276 $ reallocAligned
1272− (sizeOf (undefined :: a)) alignSize newSize arr
1277+ (sizeOf (undefined :: a))
1278+ (alignment (undefined :: a))
1273 insertElem arr1 x
1281 insertElem arr1 x
1274 step arr x = insertElem arr x
1282 step arr x = insertElem arr x
1275 extract = liftIO . rightSize
1283 extract = liftIO . rightSize
@@ -1284,9 +1292,7 @@ writeWith alignSize elemCount =
1284-- @since 0.7.0
1292-- @since 0.7.0
1285{-# INLINE write #-}
1293{-# INLINE write #-}
1286write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
1294write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
1287−write = writeWith (alignment (undefined :: a))
1288− (bytesToElemCount (undefined :: a)
1295+write = writeWith (bytesToElemCount (undefined :: a) arrayChunkSize)
1289− (mkChunkSize 1024))
1291-------------------------------------------------------------------------------
1297-------------------------------------------------------------------------------
1292-- construct from streams, known size
1298-- construct from streams, known size
@@ -1402,9 +1408,9 @@ spliceUnsafe dst (src, srcLen) =
1402 return $ dst {aEnd = pdst `plusPtr` srcLen}
1408 return $ dst {aEnd = pdst `plusPtr` srcLen}
1404-- | @spliceWith sizer dst src@ mutates @dst@ to append @src@. If there is no
1410-- | @spliceWith sizer dst src@ mutates @dst@ to append @src@. If there is no
1405−-- reserved space available in the @dst@ it is reallocated to a size determined
1411+-- reserved space available in @dst@ it is reallocated to a size determined by
1406−-- by @sizer dstSize srcSize@ function, where @dstSize@ is the number of bytes
1412+-- the @sizer dstBytesn srcBytes@ function, where @dstBytes@ is the size of the
1407−-- in the first array and @srcSize@ is the number of bytes in the second array.
1413+-- first array and @srcBytes@ is the size of the second array, in bytes.
1409-- Note that the returned array may be a mutated version of first array.
1415-- Note that the returned array may be a mutated version of first array.
@@ -1412,7 +1418,11 @@ spliceUnsafe dst (src, srcLen) =
1412{-# INLINE spliceWith #-}
1418{-# INLINE spliceWith #-}
1413spliceWith :: forall m a. (MonadIO m, Storable a) =>
1419spliceWith :: forall m a. (MonadIO m, Storable a) =>
1414 (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
1420 (Int -> Int -> Int) -> Array a -> Array a -> m (Array a)
1415−spliceWith allocSize dst@(Array start end bound) src = do
1421+spliceWith sizer dst@(Array start end bound) src = do
1423+ let f = appendWith (`sizer` byteLength src) (return dst)
1424+ in D.fold f (toStreamD src)
1416 assert (end <= bound) (return ())
1426 assert (end <= bound) (return ())
1417 let srcLen = aEnd src `minusPtr` unsafeForeignPtrToPtr (aStart src)
1427 let srcLen = aEnd src `minusPtr` unsafeForeignPtrToPtr (aStart src)
@@ -1421,7 +1431,7 @@ spliceWith allocSize dst@(Array start end bound) src = do
1422 let oldStart = unsafeForeignPtrToPtr start
1432 let oldStart = unsafeForeignPtrToPtr start
1423 oldSize = end `minusPtr` oldStart
1433 oldSize = end `minusPtr` oldStart
1424− newSize = allocSize oldSize srcLen
1434+ newSize = sizer oldSize srcLen
1425 when (newSize < oldSize + srcLen)
1435 when (newSize < oldSize + srcLen)
1427 $ "splice: newSize is less than the total size "
1437 $ "splice: newSize is less than the total size "