on master help sign in
Add snocMay and bytesFree
33b7aafHarendra Kumar
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+25
@@ -39,6 +39,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
39 , length
39 , length
40 , byteLength
40 , byteLength
41 , byteCapacity
41 , byteCapacity
42+ , bytesFree
42
43
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
56 , snoc
57 , snoc
57 , snocLinear
58 , snocLinear
59+ , snocMay
58
60
59 -- * Casting
61 -- * Casting
60 , cast
62 , cast
@@ -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
519
521
522+-- | Like 'snoc' but does not reallocate when pre-allocated array capacity
523+-- becomes full.
524+--
525+-- /Internal/
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)
531+ if newEnd <= aBound
532+ then Just <$> snocNewEnd newEnd arr x
533+ else return Nothing
534+
520reallocWith :: forall m a. (MonadIO m , Storable a) =>
535reallocWith :: forall m a. (MonadIO m , Storable a) =>
521 String
536 String
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
841
856
857+-- | The remaining capacity in the array for appending more elements without
858+-- reallocation.
859+--
860+-- /Pre-release/
861+{-# INLINE bytesFree #-}
862+bytesFree :: Array a -> Int
863+bytesFree Array{..} =
864+ let n = aBound `minusPtr` aEnd
865+ in assert (n >= 0) n
866+
842-------------------------------------------------------------------------------
867-------------------------------------------------------------------------------
843-- Streams of arrays - Creation
868-- Streams of arrays - Creation
844-------------------------------------------------------------------------------
869-------------------------------------------------------------------------------