on master help sign in
implement overlay in Array APIs
020be24Ranjeet Kumar Ranjan
src/Streamly/Internal/Data/Array.hs+19
@@ -46,6 +46,7 @@ module Streamly.Internal.Data.Array
46 -- * Random Access
46 -- * Random Access
47 , getIndexUnsafe
47 , getIndexUnsafe
48 , strip
48 , strip
49+ , putIndices
49 )
50 )
50where
51where
51
52
@@ -54,6 +55,7 @@ import Control.DeepSeq (NFData(..))
54#endif
55#endif
55import Control.Monad (when)
56import Control.Monad (when)
56import Control.Monad.IO.Class (liftIO, MonadIO)
57import Control.Monad.IO.Class (liftIO, MonadIO)
58+import Control.Monad.Primitive (PrimMonad)
57import Data.Functor.Identity (runIdentity)
59import Data.Functor.Identity (runIdentity)
58import Data.IORef
60import Data.IORef
59import GHC.Base (Int(..))
61import GHC.Base (Int(..))
@@ -313,3 +315,20 @@ strip p arr =
313 if p (indexArray arr idx) then getIndexR (idx - 1) else idx
315 if p (indexArray arr idx) then getIndexR (idx - 1) else idx
314
316
315 getIndexL idx = if p (indexArray arr idx) then getIndexL (idx + 1) else idx
317 getIndexL idx = if p (indexArray arr idx) then getIndexL (idx + 1) else idx
318+
319+-- | Write an input stream of (index, value) pairs to an array. Throws an
320+-- error if any index is out of bounds.
321+--
322+-- /Pre-release/
323+{-# INLINE putIndices #-}
324+putIndices :: PrimMonad m
325+ => Array a -> Fold m (Int, a) ()
326+putIndices arr = FL.rmapM (\ _ -> return ()) (FL.foldlM' step initial)
327+
328+ where
329+
330+ initial = unsafeThawArray arr
331+
332+ step marr (i, x) = do
333+ writeArray marr i x
334+ return marr