@@ -60,7 +60,7 @@ module Streamly.Internal.Data.Array.Foreign
60 , A.writeN -- drop new
60 , A.writeN -- drop new
61 , A.writeNAligned
61 , A.writeNAligned
62 , A.write -- full buffer
62 , A.write -- full buffer
63− -- , writeLastN -- drop old (ring buffer)
63+ , writeLastN
65 -- * Elimination
65 -- * Elimination
@@ -78,7 +78,7 @@ module Streamly.Internal.Data.Array.Foreign
81− , readIndex
81+ , getIndex
82 , A.unsafeIndex
82 , A.unsafeIndex
83 -- , readIndices
83 -- , readIndices
84 -- , readRanges
84 -- , readRanges
@@ -130,15 +130,12 @@ module Streamly.Internal.Data.Array.Foreign
131 , unsafeCast
131 , unsafeCast
133− , asByteArray
133+ , asBytes
134 , asCString
134 , asCString
136 -- * Folding Arrays
136 -- * Folding Arrays
137 , streamFold
137 , streamFold
140− -- * Folds with Array as the container
@@ -273,34 +270,43 @@ unsafeRead = Unfold step inject
273null :: Storable a => Array a -> Bool
270null :: Storable a => Array a -> Bool
274null arr = length arr == 0
271null arr = length arr == 0
276−-- | > last arr = readIndex arr (length arr - 1)
273+-- | > last arr = getIndex arr (length arr - 1)
278-- /Pre-release/
275-- /Pre-release/
279{-# INLINE last #-}
276{-# INLINE last #-}
280last :: Storable a => Array a -> Maybe a
277last :: Storable a => Array a -> Maybe a
281−last arr = readIndex arr (length arr - 1)
278+last arr = getIndex arr (length arr - 1)
283-------------------------------------------------------------------------------
280-------------------------------------------------------------------------------
284-- Folds with Array as the container
281-- Folds with Array as the container
285-------------------------------------------------------------------------------
282-------------------------------------------------------------------------------
287−-- | Take last 'n' elements from the stream and discard the rest.
284+-- | @writeLastN n@ folds a maximum of @n@ elements from the end of the input
285+-- stream to an 'Array'.
288−{-# INLINE lastN #-}
288+{-# INLINE writeLastN #-}
289−lastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
289+writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
290−lastN n
290+writeLastN n
291 | n <= 0 = fmap (const mempty) FL.drain
291 | n <= 0 = fmap (const mempty) FL.drain
292 | otherwise = A.unsafeFreeze <$> Fold step initial done
292 | otherwise = A.unsafeFreeze <$> Fold step initial done
294 step (Tuple3' rb rh i) a = do
296 step (Tuple3' rb rh i) a = do
295 rh1 <- liftIO $ RB.unsafeInsert rb rh a
297 rh1 <- liftIO $ RB.unsafeInsert rb rh a
296 return $ FL.Partial $ Tuple3' rb rh1 (i + 1)
298 return $ FL.Partial $ Tuple3' rb rh1 (i + 1)
297 initial =
300 initial =
298 let f (a, b) = FL.Partial $ Tuple3' a b (0 :: Int)
301 let f (a, b) = FL.Partial $ Tuple3' a b (0 :: Int)
299 in fmap f $ liftIO $ RB.new n
302 in fmap f $ liftIO $ RB.new n
300 done (Tuple3' rb rh i) = do
304 done (Tuple3' rb rh i) = do
301 arr <- liftIO $ MA.newArray n
305 arr <- liftIO $ MA.newArray n
302 foldFunc i rh snoc' arr rb
306 foldFunc i rh snoc' arr rb
303 snoc' b a = liftIO $ MA.unsafeSnoc b a
308 snoc' b a = liftIO $ MA.unsafeSnoc b a
304 foldFunc i
310 foldFunc i
305 | i < n = RB.unsafeFoldRingM
311 | i < n = RB.unsafeFoldRingM
306 | otherwise = RB.unsafeFoldRingFullM
312 | otherwise = RB.unsafeFoldRingFullM
@@ -425,10 +431,10 @@ foldbWith level f = undefined
426-- | /O(1)/ Lookup the element at the given index, starting from 0.
432-- | /O(1)/ Lookup the element at the given index, starting from 0.
428−-- /Pre-release/
434+-- @since 0.8.0
429−{-# INLINE readIndex #-}
435+{-# INLINE getIndex #-}
430−readIndex :: Storable a => Array a -> Int -> Maybe a
436+getIndex :: Storable a => Array a -> Int -> Maybe a
431−readIndex arr i =
437+getIndex arr i =
432 if i < 0 || i > length arr - 1
438 if i < 0 || i > length arr - 1
433 then Nothing
439 then Nothing
434 else A.unsafeInlineIO $
440 else A.unsafeInlineIO $
@@ -543,18 +549,18 @@ unsafeCast ::
543 Array a -> Array b
549 Array a -> Array b
544unsafeCast (Array start end) = Array (castForeignPtr start) (castPtr end)
550unsafeCast (Array start end) = Array (castForeignPtr start) (castPtr end)
546−-- | Cast an array into a Word8 array
552+-- | Cast an @Array a@ into an @Array Word8@.
548−-- /Pre-release/
554+-- @since 0.8.0
550−asByteArray :: Array a -> Array Word8
556+asBytes :: Array a -> Array Word8
551−asByteArray = unsafeCast
557+asBytes = unsafeCast
553-- | Cast an array having elements of type @a@ into an array having elements of
559-- | Cast an array having elements of type @a@ into an array having elements of
554-- type @b@. The length of the array should be a multiple of the size of the
560-- type @b@. The length of the array should be a multiple of the size of the
555-- target element otherwise 'Nothing' is returned.
561-- target element otherwise 'Nothing' is returned.
557−-- /Pre-release/
563+-- @since 0.8.0
559cast :: forall a b. (Storable b) => Array a -> Maybe (Array b)
565cast :: forall a b. (Storable b) => Array a -> Maybe (Array b)
560cast arr =
566cast arr =
@@ -584,7 +590,7 @@ asPtr Array{..} act = do
585asCString :: Array a -> (CString -> IO b) -> IO b
591asCString :: Array a -> (CString -> IO b) -> IO b
586asCString arr act = do
592asCString arr act = do
587− let Array{..} = asByteArray arr <> A.fromList [0]
593+ let Array{..} = asBytes arr <> A.fromList [0]
588 withForeignPtr aStart $ \ptr -> act (castPtr ptr)
594 withForeignPtr aStart $ \ptr -> act (castPtr ptr)
590-------------------------------------------------------------------------------
596-------------------------------------------------------------------------------