on master help sign in
Release Data.Array.Generic.Mut (#2161)
d7ab86aRanjeet Ranjan
core/src/Streamly/Data/Array/Generic/Mut.hsnew file+38
@@ -0,0 +1,38 @@
1+-- |
2+-- Module : Streamly.Data.Array.Generic.Mut
3+-- Copyright : (c) 2020 Composewell Technologies
4+-- License : BSD3-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : experimental
7+-- Portability : GHC
8+--
9+-- Mutable arrays for all types (unconstrained).
10+--
11+module Streamly.Data.Array.Generic.Mut
12+(
13+ -- * Type
14+ Array
15+
16+ -- * Construction
17+ , writeN
18+
19+ -- * Appending elements
20+ , new
21+ , snoc
22+
23+ -- * Conversion
24+ , toList
25+
26+ -- * Unfolds
27+ , reader
28+
29+ -- * Random reads
30+ , getIndex
31+
32+ -- * Inplace mutation
33+ , putIndex
34+ , modifyIndex
35+ )
36+where
37+
38+import Streamly.Internal.Data.Array.Generic.Mut.Type
core/src/Streamly/Internal/Data/Array/Generic.hs+4−4
@@ -99,7 +99,7 @@ unsafeThaw (Array cont# arrS arrL) = MArray.Array cont# arrS arrL arrL
99{-# NOINLINE nil #-}
99{-# NOINLINE nil #-}
100nil :: Array a
100nil :: Array a
101nil = unsafePerformIO $ do
101nil = unsafePerformIO $ do
102− marr <- MArray.newArray 0
102+ marr <- MArray.new 0
103 return $ unsafeFreeze marr
103 return $ unsafeFreeze marr
104
104
105-------------------------------------------------------------------------------
105-------------------------------------------------------------------------------
@@ -115,7 +115,7 @@ write :: MonadIO m => Fold m a (Array a)
115write = Fold step initial extract
115write = Fold step initial extract
116 where
116 where
117 initial = do
117 initial = do
118− marr <- liftIO $ MArray.newArray 0
118+ marr <- liftIO $ MArray.new 0
119 return $ FL.Partial (Tuple3' marr 0 0)
119 return $ FL.Partial (Tuple3' marr 0 0)
120 step (Tuple3' marr i capacity) x
120 step (Tuple3' marr i capacity) x
121 | i == capacity =
121 | i == capacity =
@@ -136,7 +136,7 @@ write = Fold step initial extract
136{-# INLINE_NORMAL fromStreamDN #-}
136{-# INLINE_NORMAL fromStreamDN #-}
137fromStreamDN :: MonadIO m => Int -> D.Stream m a -> m (Array a)
137fromStreamDN :: MonadIO m => Int -> D.Stream m a -> m (Array a)
138fromStreamDN limit str = do
138fromStreamDN limit str = do
139− marr <- liftIO $ MArray.newArray (max limit 0)
139+ marr <- liftIO $ MArray.new (max limit 0)
140 i <-
140 i <-
141 D.foldlM'
141 D.foldlM'
142 (\i x -> i `seq` liftIO $ MArray.putIndexUnsafe i x marr >> return (i + 1))
142 (\i x -> i `seq` liftIO $ MArray.putIndexUnsafe i x marr >> return (i + 1))
@@ -263,7 +263,7 @@ writeLastN n
263 return $ FL.Partial $ Tuple' rb (rh + 1)
263 return $ FL.Partial $ Tuple' rb (rh + 1)
264
264
265 done (Tuple' rb rh) = do
265 done (Tuple' rb rh) = do
266− arr' <- liftIO $ MArray.newArray (min rh n)
266+ arr' <- liftIO $ MArray.new (min rh n)
267 ref <- liftIO $ readIORef $ RB.ringHead rb
267 ref <- liftIO $ readIORef $ RB.ringHead rb
268 if rh < n
268 if rh < n
269 then
269 then
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+8−8
@@ -19,7 +19,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
19 -- , nil
19 -- , nil
20
20
21 -- *** Uninitialized Arrays
21 -- *** Uninitialized Arrays
22− , newArray
22+ , new
23 -- , newArrayWith
23 -- , newArrayWith
24
24
25 -- *** From streams
25 -- *** From streams
@@ -228,12 +228,12 @@ bottomElement =
228
228
229-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
229-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
230-- that we do not have to write undefined or error in the whole array.
230-- that we do not have to write undefined or error in the whole array.
231−-- | @newArray count@ allocates an empty array that can hold 'count' items.
231+-- | @new count@ allocates an empty array that can hold 'count' items.
232--
232--
233-- /Pre-release/
233-- /Pre-release/
234−{-# INLINE newArray #-}
234+{-# INLINE new #-}
235−newArray :: forall m a. MonadIO m => Int -> m (Array a)
235+new :: forall m a. MonadIO m => Int -> m (Array a)
236−newArray n@(I# n#) =
236+new n@(I# n#) =
237 liftIO
237 liftIO
238 $ IO
238 $ IO
239 $ \s# ->
239 $ \s# ->
@@ -317,7 +317,7 @@ modifyIndex i f arr@Array {..} = do
317--
317--
318realloc :: MonadIO m => Int -> Array a -> m (Array a)
318realloc :: MonadIO m => Int -> Array a -> m (Array a)
319realloc n arr = do
319realloc n arr = do
320− arr1 <- newArray n
320+ arr1 <- new n
321 let !newLen@(I# newLen#) = min n (arrLen arr)
321 let !newLen@(I# newLen#) = min n (arrLen arr)
322 !(I# arrS#) = arrStart arr
322 !(I# arrS#) = arrStart arr
323 !(I# arr1S#) = arrStart arr1
323 !(I# arr1S#) = arrStart arr1
@@ -526,7 +526,7 @@ writeNUnsafe n = Fold step initial return
526
526
527 where
527 where
528
528
529− initial = FL.Partial <$> newArray (max n 0)
529+ initial = FL.Partial <$> new (max n 0)
530
530
531 step arr x = FL.Partial <$> snocUnsafe arr x
531 step arr x = FL.Partial <$> snocUnsafe arr x
532
532
@@ -602,6 +602,6 @@ putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
602clone :: MonadIO m => Array a -> m (Array a)
602clone :: MonadIO m => Array a -> m (Array a)
603clone src = liftIO $ do
603clone src = liftIO $ do
604 let len = arrLen src
604 let len = arrLen src
605− dst <- newArray len
605+ dst <- new len
606 putSliceUnsafe src 0 dst 0 len
606 putSliceUnsafe src 0 dst 0 len
607 return dst
607 return dst
core/src/Streamly/Internal/Data/Ring.hs+2−2
@@ -16,7 +16,7 @@ module Streamly.Internal.Data.Ring
16import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef, IORef)
16import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef, IORef)
17import Streamly.Internal.Data.Array.Generic.Mut.Type
17import Streamly.Internal.Data.Array.Generic.Mut.Type
18 ( Array(..)
18 ( Array(..)
19− , newArray
19+ , new
20 , putIndexUnsafe
20 , putIndexUnsafe
21 )
21 )
22
22
@@ -29,7 +29,7 @@ data Ring a = Ring
29{-# INLINE createRing #-}
29{-# INLINE createRing #-}
30createRing :: Int -> IO (Ring a)
30createRing :: Int -> IO (Ring a)
31createRing count = do
31createRing count = do
32− arr' <- newArray count
32+ arr' <- new count
33 head' <- newIORef 0
33 head' <- newIORef 0
34 return (Ring
34 return (Ring
35 { arr = arr'
35 { arr = arr'
core/streamly-core.cabal+1
@@ -381,6 +381,7 @@ library
381 -- NOTE: these must be added to streamly.cabal as well
381 -- NOTE: these must be added to streamly.cabal as well
382 , Streamly.Console.Stdio
382 , Streamly.Console.Stdio
383 , Streamly.Data.Array.Generic
383 , Streamly.Data.Array.Generic
384+ , Streamly.Data.Array.Generic.Mut
384 , Streamly.Data.Array
385 , Streamly.Data.Array
385 , Streamly.Data.Array.Mut
386 , Streamly.Data.Array.Mut
386 , Streamly.Data.Fold
387 , Streamly.Data.Fold