on master help sign in
Release Data.Array.Generic.Mut (#2161)
commitd7ab86ab0029bfe984e926cd94b7964b872ce745 authorRanjeet Ranjan <ranjeetr@composewell.com> dateMon 12 Dec 2022, 02:58 (4 years ago) parent48f647b
5 files changed+53−14
core/src/Streamly/Data/Array/Generic/Mut.hs+38 core/src/Streamly/Internal/Data/Array/Generic.hs+4−4 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+8−8 core/src/Streamly/Internal/Data/Ring.hs+2−2 core/streamly-core.cabal+1
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
9999{-# NOINLINE nil #-}
100100nil :: Array a
101101nil = unsafePerformIO $ do
102− marr <- MArray.newArray 0
102+ marr <- MArray.new 0
103103 return $ unsafeFreeze marr
104104
105105-------------------------------------------------------------------------------
@@ -115,7 +115,7 @@ write :: MonadIO m => Fold m a (Array a)
115115write = Fold step initial extract
116116 where
117117 initial = do
118− marr <- liftIO $ MArray.newArray 0
118+ marr <- liftIO $ MArray.new 0
119119 return $ FL.Partial (Tuple3' marr 0 0)
120120 step (Tuple3' marr i capacity) x
121121 | i == capacity =
@@ -136,7 +136,7 @@ write = Fold step initial extract
136136{-# INLINE_NORMAL fromStreamDN #-}
137137fromStreamDN :: MonadIO m => Int -> D.Stream m a -> m (Array a)
138138fromStreamDN limit str = do
139− marr <- liftIO $ MArray.newArray (max limit 0)
139+ marr <- liftIO $ MArray.new (max limit 0)
140140 i <-
141141 D.foldlM'
142142 (\i x -> i `seq` liftIO $ MArray.putIndexUnsafe i x marr >> return (i + 1))
@@ -263,7 +263,7 @@ writeLastN n
263263 return $ FL.Partial $ Tuple' rb (rh + 1)
264264
265265 done (Tuple' rb rh) = do
266− arr' <- liftIO $ MArray.newArray (min rh n)
266+ arr' <- liftIO $ MArray.new (min rh n)
267267 ref <- liftIO $ readIORef $ RB.ringHead rb
268268 if rh < n
269269 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
1919 -- , nil
2020
2121 -- *** Uninitialized Arrays
22− , newArray
22+ , new
2323 -- , newArrayWith
2424
2525 -- *** From streams
@@ -228,12 +228,12 @@ bottomElement =
228228
229229-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
230230-- 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.
232232--
233233-- /Pre-release/
234−{-# INLINE newArray #-}
235−newArray :: forall m a. MonadIO m => Int -> m (Array a)
236−newArray n@(I# n#) =
234+{-# INLINE new #-}
235+new :: forall m a. MonadIO m => Int -> m (Array a)
236+new n@(I# n#) =
237237 liftIO
238238 $ IO
239239 $ \s# ->
@@ -317,7 +317,7 @@ modifyIndex i f arr@Array {..} = do
317317--
318318realloc :: MonadIO m => Int -> Array a -> m (Array a)
319319realloc n arr = do
320− arr1 <- newArray n
320+ arr1 <- new n
321321 let !newLen@(I# newLen#) = min n (arrLen arr)
322322 !(I# arrS#) = arrStart arr
323323 !(I# arr1S#) = arrStart arr1
@@ -526,7 +526,7 @@ writeNUnsafe n = Fold step initial return
526526
527527 where
528528
529− initial = FL.Partial <$> newArray (max n 0)
529+ initial = FL.Partial <$> new (max n 0)
530530
531531 step arr x = FL.Partial <$> snocUnsafe arr x
532532
@@ -602,6 +602,6 @@ putSliceUnsafe src srcStart dst dstStart len = liftIO $ do
602602clone :: MonadIO m => Array a -> m (Array a)
603603clone src = liftIO $ do
604604 let len = arrLen src
605− dst <- newArray len
605+ dst <- new len
606606 putSliceUnsafe src 0 dst 0 len
607607 return dst
core/src/Streamly/Internal/Data/Ring.hs+2−2
@@ -16,7 +16,7 @@ module Streamly.Internal.Data.Ring
1616import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef, IORef)
1717import Streamly.Internal.Data.Array.Generic.Mut.Type
1818 ( Array(..)
19− , newArray
19+ , new
2020 , putIndexUnsafe
2121 )
2222
@@ -29,7 +29,7 @@ data Ring a = Ring
2929{-# INLINE createRing #-}
3030createRing :: Int -> IO (Ring a)
3131createRing count = do
32− arr' <- newArray count
32+ arr' <- new count
3333 head' <- newIORef 0
3434 return (Ring
3535 { arr = arr'
core/streamly-core.cabal+1
@@ -381,6 +381,7 @@ library
381381 -- NOTE: these must be added to streamly.cabal as well
382382 , Streamly.Console.Stdio
383383 , Streamly.Data.Array.Generic
384+ , Streamly.Data.Array.Generic.Mut
384385 , Streamly.Data.Array
385386 , Streamly.Data.Array.Mut
386387 , Streamly.Data.Fold