on master help sign in
Make signatures consistent with other array modules (#2170)
48f647bRanjeet Ranjan
core/src/Streamly/Internal/Data/Array/Generic.hs+3−3
@@ -139,7 +139,7 @@ fromStreamDN limit str = do
139 marr <- liftIO $ MArray.newArray (max limit 0)
139 marr <- liftIO $ MArray.newArray (max limit 0)
140 i <-
140 i <-
141 D.foldlM'
141 D.foldlM'
142− (\i x -> i `seq` liftIO $ MArray.putIndexUnsafe marr i x >> return (i + 1))
142+ (\i x -> i `seq` liftIO $ MArray.putIndexUnsafe i x marr >> return (i + 1))
143 (return 0) $
143 (return 0) $
144 D.take limit str
144 D.take limit str
145 return $ unsafeFreeze $ marr { MArray.arrLen = i }
145 return $ unsafeFreeze $ marr { MArray.arrLen = i }
@@ -244,7 +244,7 @@ streamFold f arr = f (read arr)
244{-# INLINE getIndexUnsafe #-}
244{-# INLINE getIndexUnsafe #-}
245getIndexUnsafe :: Array a -> Int -> a
245getIndexUnsafe :: Array a -> Int -> a
246getIndexUnsafe arr i =
246getIndexUnsafe arr i =
247− unsafePerformIO $ MArray.getIndexUnsafe (unsafeThaw arr) i
247+ unsafePerformIO $ MArray.getIndexUnsafe i (unsafeThaw arr)
248
248
249{-# INLINE writeLastN #-}
249{-# INLINE writeLastN #-}
250writeLastN :: MonadIO m => Int -> Fold m a (Array a)
250writeLastN :: MonadIO m => Int -> Fold m a (Array a)
@@ -322,7 +322,7 @@ putIndices arr = FL.rmapM (\ _ -> return ()) (FL.foldlM' step initial)
322 initial = return $ unsafeThaw arr
322 initial = return $ unsafeThaw arr
323
323
324 step marr (i, x) = do
324 step marr (i, x) = do
325− MArray.putIndexUnsafe marr i x
325+ MArray.putIndexUnsafe i x marr
326 return marr
326 return marr
327
327
328-------------------------------------------------------------------------------
328-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+21−21
@@ -251,8 +251,8 @@ newArray n@(I# n#) =
251--
251--
252-- /Pre-release/
252-- /Pre-release/
253{-# INLINE putIndexUnsafe #-}
253{-# INLINE putIndexUnsafe #-}
254−putIndexUnsafe :: forall m a. MonadIO m => Array a -> Int -> a -> m ()
254+putIndexUnsafe :: forall m a. MonadIO m => Int -> a -> Array a -> m ()
255−putIndexUnsafe Array {..} i x =
255+putIndexUnsafe i x Array {..} =
256 liftIO
256 liftIO
257 $ IO
257 $ IO
258 $ \s# ->
258 $ \s# ->
@@ -268,14 +268,14 @@ invalidIndex label i =
268-- | /O(1)/ Write the given element at the given index in the array.
268-- | /O(1)/ Write the given element at the given index in the array.
269-- Performs in-place mutation of the array.
269-- Performs in-place mutation of the array.
270--
270--
271−-- >>> putIndex arr ix val = Array.modifyIndex arr ix (const (val, ()))
271+-- >>> putIndex ix val arr = Array.modifyIndex ix (const (val, ())) arr
272--
272--
273-- /Pre-release/
273-- /Pre-release/
274{-# INLINE putIndex #-}
274{-# INLINE putIndex #-}
275−putIndex :: MonadIO m => Array a -> Int -> a -> m ()
275+putIndex :: MonadIO m => Int -> a -> Array a -> m ()
276−putIndex arr@Array {..} i x =
276+putIndex i x arr@Array {..} =
277 if i >= 0 && i < arrLen
277 if i >= 0 && i < arrLen
278− then putIndexUnsafe arr i x
278+ then putIndexUnsafe i x arr
279 else invalidIndex "putIndex" i
279 else invalidIndex "putIndex" i
280
280
281-- | Modify a given index of an array using a modifier function without checking
281-- | Modify a given index of an array using a modifier function without checking
@@ -284,8 +284,8 @@ putIndex arr@Array {..} i x =
284-- Unsafe because it does not check the bounds of the array.
284-- Unsafe because it does not check the bounds of the array.
285--
285--
286-- /Pre-release/
286-- /Pre-release/
287−modifyIndexUnsafe :: MonadIO m => Array a -> Int -> (a -> (a, b)) -> m b
287+modifyIndexUnsafe :: MonadIO m => Int -> (a -> (a, b)) -> Array a -> m b
288−modifyIndexUnsafe Array {..} i f = do
288+modifyIndexUnsafe i f Array {..} = do
289 liftIO
289 liftIO
290 $ IO
290 $ IO
291 $ \s# ->
291 $ \s# ->
@@ -300,10 +300,10 @@ modifyIndexUnsafe Array {..} i f = do
300-- | Modify a given index of an array using a modifier function.
300-- | Modify a given index of an array using a modifier function.
301--
301--
302-- /Pre-release/
302-- /Pre-release/
303−modifyIndex :: MonadIO m => Array a -> Int -> (a -> (a, b)) -> m b
303+modifyIndex :: MonadIO m => Int -> (a -> (a, b)) -> Array a -> m b
304−modifyIndex arr@Array {..} i f = do
304+modifyIndex i f arr@Array {..} = do
305 if i >= 0 && i < arrLen
305 if i >= 0 && i < arrLen
306− then modifyIndexUnsafe arr i f
306+ then modifyIndexUnsafe i f arr
307 else invalidIndex "modifyIndex" i
307 else invalidIndex "modifyIndex" i
308
308
309-------------------------------------------------------------------------------
309-------------------------------------------------------------------------------
@@ -362,7 +362,7 @@ reallocWith label sizer reqSize arr = do
362snocUnsafe :: MonadIO m => Array a -> a -> m (Array a)
362snocUnsafe :: MonadIO m => Array a -> a -> m (Array a)
363snocUnsafe arr@Array {..} a = do
363snocUnsafe arr@Array {..} a = do
364 assert (arrStart + arrLen < arrTrueLen) (return ())
364 assert (arrStart + arrLen < arrTrueLen) (return ())
365− putIndexUnsafe arr arrLen a
365+ putIndexUnsafe arrLen a arr
366 return $ arr {arrLen = arrLen + 1}
366 return $ arr {arrLen = arrLen + 1}
367
367
368-- NOINLINE to move it out of the way and not pollute the instruction cache.
368-- NOINLINE to move it out of the way and not pollute the instruction cache.
@@ -417,8 +417,8 @@ snoc = snocWith (* 2)
417--
417--
418-- Unsafe because it does not check the bounds of the array.
418-- Unsafe because it does not check the bounds of the array.
419{-# INLINE_NORMAL getIndexUnsafe #-}
419{-# INLINE_NORMAL getIndexUnsafe #-}
420−getIndexUnsafe :: MonadIO m => Array a -> Int -> m a
420+getIndexUnsafe :: MonadIO m => Int -> Array a -> m a
421−getIndexUnsafe Array {..} n =
421+getIndexUnsafe n Array {..} =
422 liftIO
422 liftIO
423 $ IO
423 $ IO
424 $ \s# ->
424 $ \s# ->
@@ -428,10 +428,10 @@ getIndexUnsafe Array {..} n =
428-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
428-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
429--
429--
430{-# INLINE getIndex #-}
430{-# INLINE getIndex #-}
431−getIndex :: MonadIO m => Array a -> Int -> m a
431+getIndex :: MonadIO m => Int -> Array a -> m a
432−getIndex arr@Array {..} i =
432+getIndex i arr@Array {..} =
433 if i >= 0 && i < arrLen
433 if i >= 0 && i < arrLen
434− then getIndexUnsafe arr i
434+ then getIndexUnsafe i arr
435 else invalidIndex "getIndex" i
435 else invalidIndex "getIndex" i
436
436
437-------------------------------------------------------------------------------
437-------------------------------------------------------------------------------
@@ -484,7 +484,7 @@ getSlice index len arr@Array{..} =
484-- /Pre-release/
484-- /Pre-release/
485{-# INLINE toList #-}
485{-# INLINE toList #-}
486toList :: MonadIO m => Array a -> m [a]
486toList :: MonadIO m => Array a -> m [a]
487−toList arr@Array{..} = mapM (getIndexUnsafe arr) [0 .. (arrLen - 1)]
487+toList arr@Array{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
488
488
489-- | Use the 'read' unfold instead.
489-- | Use the 'read' unfold instead.
490--
490--
@@ -494,7 +494,7 @@ toList arr@Array{..} = mapM (getIndexUnsafe arr) [0 .. (arrLen - 1)]
494{-# INLINE_NORMAL toStreamD #-}
494{-# INLINE_NORMAL toStreamD #-}
495toStreamD :: MonadIO m => Array a -> D.Stream m a
495toStreamD :: MonadIO m => Array a -> D.Stream m a
496toStreamD arr@Array{..} =
496toStreamD arr@Array{..} =
497− D.mapM (getIndexUnsafe arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
497+ D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
498
498
499{-# INLINE toStreamK #-}
499{-# INLINE toStreamK #-}
500toStreamK :: MonadIO m => Array a -> K.Stream m a
500toStreamK :: MonadIO m => Array a -> K.Stream m a
@@ -505,7 +505,7 @@ toStreamK arr@Array{..} = K.unfoldrM step 0
505 step i
505 step i
506 | i == arrLen = return Nothing
506 | i == arrLen = return Nothing
507 | otherwise = do
507 | otherwise = do
508− x <- getIndexUnsafe arr i
508+ x <- getIndexUnsafe i arr
509 return $ Just (x, i + 1)
509 return $ Just (x, i + 1)
510
510
511
511
@@ -563,7 +563,7 @@ producerWith liftio = Producer step inject extract
563 step (arr, i)
563 step (arr, i)
564 | assert (arrLen arr >= 0) (i == arrLen arr) = return D.Stop
564 | assert (arrLen arr >= 0) (i == arrLen arr) = return D.Stop
565 step (arr, i) = do
565 step (arr, i) = do
566− x <- liftio $ getIndexUnsafe arr i
566+ x <- liftio $ getIndexUnsafe i arr
567 return $ D.Yield x (arr, i + 1)
567 return $ D.Yield x (arr, i + 1)
568
568
569-- | Resumable unfold of an array.
569-- | Resumable unfold of an array.
core/src/Streamly/Internal/Data/Ring.hs+1−1
@@ -40,7 +40,7 @@ createRing count = do
40{-# INLINE unsafeInsertRing #-}
40{-# INLINE unsafeInsertRing #-}
41unsafeInsertRing :: Ring a -> Int -> a -> IO ()
41unsafeInsertRing :: Ring a -> Int -> a -> IO ()
42unsafeInsertRing Ring{..} idx x = do
42unsafeInsertRing Ring{..} idx x = do
43− putIndexUnsafe arr (mod idx ringMax) x
43+ putIndexUnsafe (mod idx ringMax) x arr
44 ref <- readIORef ringHead
44 ref <- readIORef ringHead
45 if (ref+1) < ringMax
45 if (ref+1) < ringMax
46 then modifyIORef' ringHead ( + 1)
46 then modifyIORef' ringHead ( + 1)