@@ -39,6 +39,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
40 , byteLength
40 , byteLength
41 , byteCapacity
41 , byteCapacity
43 -- * Random access
44 -- * Random access
44 , getIndexUnsafe
45 , getIndexUnsafe
@@ -55,6 +56,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
55 , snocWith
56 , snocWith
57 , snocLinear
58 , snocLinear
59 -- * Casting
61 -- * Casting
@@ -517,6 +519,19 @@ snocUnsafe :: forall m a. (MonadIO m, Storable a) =>
517snocUnsafe arr@Array{..} =
519snocUnsafe arr@Array{..} =
518 snocNewEnd (aEnd `plusPtr` sizeOf (undefined :: a)) arr
520 snocNewEnd (aEnd `plusPtr` sizeOf (undefined :: a)) arr
522+-- | Like 'snoc' but does not reallocate when pre-allocated array capacity
526+{-# INLINE snocMay #-}
527+snocMay :: forall m a. (MonadIO m, Storable a) =>
528+ Array a -> a -> m (Maybe (Array a))
529+snocMay arr@Array{..} x = liftIO $ do
530+ let newEnd = aEnd `plusPtr` sizeOf (undefined :: a)
532+ then Just <$> snocNewEnd newEnd arr x
520reallocWith :: forall m a. (MonadIO m , Storable a) =>
535reallocWith :: forall m a. (MonadIO m , Storable a) =>
522 -> (Int -> Int)
537 -> (Int -> Int)
@@ -839,6 +854,16 @@ byteCapacity Array{..} =
839 len = aBound `minusPtr` p
854 len = aBound `minusPtr` p
840 in assert (len >= 0) len
855 in assert (len >= 0) len
857+-- | The remaining capacity in the array for appending more elements without
861+{-# INLINE bytesFree #-}
862+bytesFree :: Array a -> Int
863+bytesFree Array{..} =
864+ let n = aBound `minusPtr` aEnd
865+ in assert (n >= 0) n
842-------------------------------------------------------------------------------
867-------------------------------------------------------------------------------
843-- Streams of arrays - Creation
868-- Streams of arrays - Creation
844-------------------------------------------------------------------------------
869-------------------------------------------------------------------------------