@@ -44,6 +44,7 @@ module Streamly.Internal.Data.Array.Type
44 , fromByteStr#
44 , fromByteStr#
45 , fromByteStr
45 , fromByteStr
46 , fromPtrN
46 , fromPtrN
48 -- ** Split
49 -- ** Split
@@ -117,6 +118,7 @@ import GHC.Ptr (Ptr(..))
117import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
118import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
118import Streamly.Internal.Data.Fold.Type (Fold(..))
119import Streamly.Internal.Data.Fold.Type (Fold(..))
119import Streamly.Internal.Data.Stream.Type (Stream)
120import Streamly.Internal.Data.Stream.Type (Stream)
121+import Streamly.Internal.Data.StreamK.Type (StreamK)
120import Streamly.Internal.Data.Unbox (Unbox(..))
122import Streamly.Internal.Data.Unbox (Unbox(..))
121import Streamly.Internal.Data.Unfold.Type (Unfold(..))
123import Streamly.Internal.Data.Unfold.Type (Unfold(..))
122import Text.Read (readPrec)
124import Text.Read (readPrec)
@@ -262,9 +264,9 @@ isPinned = MA.isPinned . unsafeThaw
262-- would make a copy on every splice operation, instead use the
264-- would make a copy on every splice operation, instead use the
263-- 'fromArrayStreamK' operation to combine n immutable arrays.
265-- 'fromArrayStreamK' operation to combine n immutable arrays.
264{-# INLINE splice #-}
266{-# INLINE splice #-}
265−splice :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
267+splice :: MonadIO m => Array a -> Array a -> m (Array a)
266splice arr1 arr2 =
268splice arr1 arr2 =
267− unsafeFreeze <$> MA.splice (unsafeThaw arr1) (unsafeThaw arr2)
269+ unsafeFreeze <$> MA.spliceCopy (unsafeThaw arr1) (unsafeThaw arr2)
269-- | Create an 'Array' from the first N elements of a list. The array is
271-- | Create an 'Array' from the first N elements of a list. The array is
270-- allocated to size N, if the list terminates before N elements then the
272-- allocated to size N, if the list terminates before N elements then the
@@ -663,6 +665,16 @@ fromByteStr# addr = unsafePerformIO $ fmap unsafeFreeze (MA.fromByteStr# addr)
663fromByteStr :: Ptr Word8 -> Array Word8
665fromByteStr :: Ptr Word8 -> Array Word8
664fromByteStr (Ptr addr#) = fromByteStr# addr#
666fromByteStr (Ptr addr#) = fromByteStr# addr#
668+-- XXX implement fromArrayStream/fromArrayList instead?
670+-- | Convert an array stream to an array. Note that this requires peak memory
671+-- that is double the size of the array stream.
673+{-# INLINE fromArrayStreamK #-}
674+fromArrayStreamK :: (Unbox a, MonadIO m) => StreamK m (Array a) -> m (Array a)
675+fromArrayStreamK stream =
676+ fmap unsafeFreeze $ MA.fromArrayStreamK $ fmap unsafeThaw stream
666-------------------------------------------------------------------------------
678-------------------------------------------------------------------------------
667-- Instances
679-- Instances
668-------------------------------------------------------------------------------
680-------------------------------------------------------------------------------
@@ -815,6 +827,12 @@ instance Foldable Array where
815-- Semigroup and Monoid
827-- Semigroup and Monoid
816-------------------------------------------------------------------------------
828-------------------------------------------------------------------------------
830+-- XXX Deprecate and remove the Semigroup and Monoid instances because of
831+-- potential misuse chances.
833+-- | This should not be used for combining many or N arrays as it would copy
834+-- the two arrays everytime to a new array. For coalescing multiple arrays use
835+-- 'fromArrayStreamK' instead.
818instance Unbox a => Semigroup (Array a) where
836instance Unbox a => Semigroup (Array a) where
819 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
837 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2