on master help sign in
Implement putIndices
commitb5bf22bf4c3d5f1d5b0e39a44d5dbfb0b97d0b26 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 27 Dec 2021, 20:00 (5 years ago) parent7b8d5c0
1 file changed+17−8
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+17−8
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+17−8
@@ -547,14 +547,6 @@ withNewArrayUnsafe count f = do
547547-- Random writes
548548-------------------------------------------------------------------------------
549549
550−-- | Write an input stream of (index, value) pairs to an array. Throws an
551−-- error if any index is out of bounds.
552−--
553−-- /Unimplemented/
554−{-# INLINE putIndices #-}
555−putIndices :: Array a -> Fold m (Int, a) ()
556−putIndices = undefined
557−
558550-- | Write the given element to the given index of the array. Does not check if
559551-- the index is out of bounds of the array.
560552--
@@ -597,6 +589,23 @@ putIndex arr i x =
597589 unsafeWithArrayContents (arrContents arr) (arrStart arr)
598590 $ \p -> putIndexPtr p (aEnd arr) i x
599591
592+-- | Write an input stream of (index, value) pairs to an array. Throws an
593+-- error if any index is out of bounds.
594+--
595+-- /Pre-release/
596+{-# INLINE putIndices #-}
597+putIndices :: forall m a. (MonadIO m, Storable a)
598+ => Array a -> Fold m (Int, a) ()
599+putIndices Array{..} = FL.mkFoldM step initial extract
600+
601+ where
602+
603+ initial = return $ FL.Partial ()
604+
605+ step () (i, x) = FL.Partial <$> liftIO (putIndexPtr arrStart aEnd i x)
606+
607+ extract () = liftIO $ touch arrContents
608+
600609-- | Modify a given index of an array using a modifier function.
601610--
602611-- /Pre-release/