on master help sign in
Implement clone in MutArray
commitc4b717ec8e896836956ca7313380727cac74b735 authorAdithya Kumar <adithya@composewell.com> dateFri 18 Aug 2023, 17:55 (3 years ago) parent9e8c35d
1 file changed+31
core/src/Streamly/Internal/Data/MutArray/Type.hs+31
core/src/Streamly/Internal/Data/MutArray/Type.hs+31
@@ -204,6 +204,10 @@ module Streamly.Internal.Data.MutArray.Type
204204 , splitAt -- XXX should be able to express using getSlice
205205 , breakOn
206206
207+ -- ** Cloning arrays
208+ , clone
209+ , pinnedClone
210+
207211 -- ** Appending arrays
208212 , spliceCopy
209213 , spliceWith
@@ -2085,10 +2089,37 @@ pinnedFromList xs = fromStreamDAs Pinned $ D.fromList xs
20852089fromListRev :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
20862090fromListRev xs = fromListRevN (Prelude.length xs) xs
20872091
2092+-------------------------------------------------------------------------------
2093+-- Cloning
2094+-------------------------------------------------------------------------------
2095+
2096+{-# INLINE cloneAs #-}
2097+cloneAs :: MonadIO m => PinnedState -> MutArray a -> m (MutArray a)
2098+cloneAs ps src =
2099+ liftIO $ do
2100+ let startSrc = arrStart src
2101+ srcLen = arrEnd src - startSrc
2102+ newArrDst = srcLen
2103+ newArr <- newBytesAs ps srcLen
2104+ putSliceUnsafe src startSrc newArr 0 srcLen
2105+ return $ newArr {arrEnd = newArrDst}
2106+
2107+{-# INLINE clone #-}
2108+clone :: MonadIO m => MutArray a -> m (MutArray a)
2109+clone = cloneAs Unpinned
2110+
2111+{-# INLINE pinnedClone #-}
2112+pinnedClone :: MonadIO m => MutArray a -> m (MutArray a)
2113+pinnedClone = cloneAs Pinned
2114+
20882115-------------------------------------------------------------------------------
20892116-- Combining
20902117-------------------------------------------------------------------------------
20912118
2119+-- XXX Change the signature of this function to
2120+-- putSliceUnsafe :: MonadIO m => MutableByteArray -> Int -> MutableByteArray -> Int -> Int -> m ()
2121+-- We don't use the relative indexes but the absolute ones. So we should MutableByteArray to remove any confusion.
2122+
20922123-- | Put a sub range of a source array into a subrange of a destination array.
20932124-- This is not safe as it does not check the bounds.
20942125{-# INLINE putSliceUnsafe #-}