on master help sign in
Update docs and expose doctest setup for MutArray
commit68cb9729d39a3b47bd8596c79cda2db7645a5a33 authorHarendra Kumar <harendra@composewell.com> dateSun 12 Mar 2023, 00:16 (4 years ago) parent303143a
8 files changed+83−57
core/src/DocTestDataMutArray.hs+10 core/src/DocTestDataMutArrayGeneric.hs+10 core/src/Streamly/Data/MutArray.hs+18−6 core/src/Streamly/Data/MutArray/Generic.hs+9 core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+6−12 core/src/Streamly/Internal/Data/Array/Mut.hs+2−16 core/src/Streamly/Internal/Data/Array/Mut/Type.hs+26−23 core/streamly-core.cabal+2
core/src/DocTestDataMutArray.hsnew file+10
@@ -0,0 +1,10 @@
1+{- $setup
2+>>> :m
3+>>> import qualified Streamly.Data.Fold as Fold
4+>>> import qualified Streamly.Data.MutArray as MutArray
5+>>> import qualified Streamly.Data.Stream as Stream
6+
7+For APIs that have not been released yet.
8+
9+>>> import Streamly.Internal.Data.Array.Mut as MutArray
10+-}
core/src/DocTestDataMutArrayGeneric.hsnew file+10
@@ -0,0 +1,10 @@
1+{- $setup
2+>>> :m
3+>>> import qualified Streamly.Data.Fold as Fold
4+>>> import qualified Streamly.Data.MutArray.Generic as MutArray
5+>>> import qualified Streamly.Data.Stream as Stream
6+
7+For APIs that have not been released yet.
8+
9+>>> import Streamly.Internal.Data.Array.Generic.Mut.Type as MutArray
10+-}
core/src/Streamly/Data/MutArray.hs+18−6
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12-- |
23-- Module : Streamly.Data.MutArray
34-- Copyright : (c) 2022 Composewell Technologies
@@ -7,16 +8,25 @@
78-- Stability : released
89-- Portability : GHC
910--
10−-- Mutable version of "Streamly.Data.Array". Please refer to that module for
11−-- general documentation. The contents of mutable ararys can be modified
12−-- in-place.
11+-- This module provides a mutable version of "Streamly.Data.Array". The
12+-- contents of a mutable array can be modified in-place. For general
13+-- documentation, please refer to the original module.
1314--
14−-- See "Streamly.Data.MutArray.Generic" for mutable arrays that work for boxed
15−-- types i.e. not requiring the 'Unbox' constraint.
15+-- Please refer to "Streamly.Internal.Data.Array.Mut" for functions that have
16+-- not yet been released.
17+--
18+-- For mutable arrays that work on boxed types, not requiring the 'Unbox'
19+-- constraint, please refer to "Streamly.Data.MutArray.Generic".
1620
1721module Streamly.Data.MutArray
1822 (
19− -- * Types
23+ -- * Setup
24+ -- | To execute the code examples provided in this module in ghci, please
25+ -- run the following commands first.
26+ --
27+ -- $setup
28+
29+ -- * Mutable Array Type
2030 MutArray
2131
2232 -- * Construction
@@ -67,3 +77,5 @@ where
6777import Prelude hiding (length, read)
6878import Streamly.Internal.Data.Array.Mut
6979import Streamly.Internal.Data.Unboxed (Unbox (..))
80+
81+#include "DocTestDataMutArray.hs"
core/src/Streamly/Data/MutArray/Generic.hs+9
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12-- |
23-- Module : Streamly.Data.MutArray.Generic
34-- Copyright : (c) 2020 Composewell Technologies
@@ -12,6 +13,12 @@
1213--
1314module Streamly.Data.MutArray.Generic
1415(
16+ -- * Setup
17+ -- | To execute the code examples provided in this module in ghci, please
18+ -- run the following commands first.
19+ --
20+ -- $setup
21+
1522 -- * Type
1623 MutArray
1724
@@ -38,3 +45,5 @@ module Streamly.Data.MutArray.Generic
3845where
3946
4047import Streamly.Internal.Data.Array.Generic.Mut.Type
48+
49+#include "DocTestDataMutArrayGeneric.hs"
core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs+6−12
@@ -1,5 +1,5 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE UnboxedTuples #-}
2−
33-- |
44-- Module : Streamly.Internal.Data.Array.Generic.Mut.Type
55-- Copyright : (c) 2020 Composewell Technologies
@@ -188,13 +188,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
188188
189189import Prelude hiding (read, length)
190190
191−-- $setup
192−-- >>> :m
193−-- >>> import qualified Streamly.Internal.Data.Array.Generic.Mut.Type as Array
194−-- >>> import qualified Streamly.Internal.Data.Stream as Stream
195−-- >>> import qualified Streamly.Internal.Data.Stream.StreamD as StreamD
196−-- >>> import qualified Streamly.Internal.Data.Fold as Fold
197−-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
191+#include "DocTestDataMutArrayGeneric.hs"
198192
199193-------------------------------------------------------------------------------
200194-- MutArray Data Type
@@ -256,7 +250,7 @@ new n@(I# n#) =
256250-- |
257251-- Definition:
258252--
259−-- >>> nil = Array.new 0
253+-- >>> nil = MutArray.new 0
260254{-# INLINE nil #-}
261255nil :: MonadIO m => m (MutArray a)
262256nil = new 0
@@ -288,7 +282,7 @@ invalidIndex label i =
288282-- | /O(1)/ Write the given element at the given index in the array.
289283-- Performs in-place mutation of the array.
290284--
291−-- >>> putIndex ix arr val = Array.modifyIndex ix arr (const (val, ()))
285+-- >>> putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))
292286--
293287-- /Pre-release/
294288{-# INLINE putIndex #-}
@@ -434,7 +428,7 @@ snocWith sizer arr@MutArray {..} x = do
434428--
435429-- Note that the returned array may be a mutated version of the original array.
436430--
437−-- >>> snoc = Array.snocWith (* 2)
431+-- >>> snoc = MutArray.snocWith (* 2)
438432--
439433-- Performs O(n * log n) copies to grow, but is liberal with memory allocation.
440434--
@@ -593,7 +587,7 @@ writeNUnsafe n = Fold step initial return
593587-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
594588-- 'Array'.
595589--
596−-- >>> writeN n = Fold.take n (Array.writeNUnsafe n)
590+-- >>> writeN n = Fold.take n (MutArray.writeNUnsafe n)
597591--
598592-- /Pre-release/
599593{-# INLINE_NORMAL writeN #-}
core/src/Streamly/Internal/Data/Array/Mut.hs+2−16
@@ -1,5 +1,3 @@
1−#include "inline.hs"
2−
31-- |
42-- Module : Streamly.Internal.Data.Array.Mut
53-- Copyright : (c) 2020 Composewell Technologies
@@ -8,20 +6,6 @@
86-- Stability : experimental
97-- Portability : GHC
108--
11−-- Unboxed pinned mutable array type for 'Unboxed' types with an option to use
12−-- foreign (non-GHC) memory allocators. Fulfils the following goals:
13−--
14−-- * Random access (array)
15−-- * Efficient storage (unboxed)
16−-- * Performance (unboxed access)
17−-- * Performance - in-place operations (mutable)
18−-- * Performance - GC (pinned, mutable)
19−-- * interfacing with OS (pinned)
20−-- * Fragmentation control (foreign allocators)
21−--
22−-- Stream and Fold APIs allow easy, efficient and convenient operations on
23−-- arrays.
24−
259module Streamly.Internal.Data.Array.Mut
2610 (
2711 module Streamly.Internal.Data.Array.Mut.Type
@@ -32,6 +16,8 @@ module Streamly.Internal.Data.Array.Mut
3216 )
3317where
3418
19+#include "inline.hs"
20+
3521import Control.Monad.IO.Class (MonadIO(..))
3622import Streamly.Internal.Data.Unboxed (Unbox)
3723import Streamly.Internal.Data.Stream.StreamD (Stream)
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+26−23
@@ -1,12 +1,26 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE UnboxedTuples #-}
23-- |
3−-- Module : Streamly.Internal.Data.MutArray.Mut.Type
4+-- Module : Streamly.Internal.Data.Array.Mut.Type
45-- Copyright : (c) 2020 Composewell Technologies
56-- License : BSD3-3-Clause
67-- Maintainer : streamly@composewell.com
78-- Stability : experimental
89-- Portability : GHC
910--
11+-- Pinned and unpinned mutable array for 'Unboxed' types. Fulfils the following
12+-- goals:
13+--
14+-- * Random access (array)
15+-- * Efficient storage (unboxed)
16+-- * Performance (unboxed access)
17+-- * Performance - in-place operations (mutable)
18+-- * Performance - GC (pinned, mutable)
19+-- * interfacing with OS (pinned)
20+--
21+-- Stream and Fold APIs allow easy, efficient and convenient operations on
22+-- arrays.
23+--
1024-- Mutable arrays and file system files are quite similar, they can grow and
1125-- their content is mutable. Therefore, both have similar APIs as well. We
1226-- strive to keep the API consistent for both. Ideally, you should be able to
@@ -205,10 +219,6 @@ module Streamly.Internal.Data.Array.Mut.Type
205219 )
206220where
207221
208−-- Notes:
209−-- When we use a purely lazy Monad like Identity, we need to force ordering of
210−-- some actions for correctness.
211−
212222#include "assert.hs"
213223#include "inline.hs"
214224#include "ArrayMacros.h"
@@ -259,13 +269,7 @@ import qualified Prelude
259269import Prelude hiding
260270 (length, foldr, read, unlines, splitAt, reverse, truncate)
261271
262−-- $setup
263−-- >>> :m
264−-- >>> import qualified Streamly.Internal.Data.Array.Mut.Type as MutArray
265−-- >>> import qualified Streamly.Internal.Data.Stream as Stream
266−-- >>> import qualified Streamly.Internal.Data.Stream.StreamD as StreamD
267−-- >>> import qualified Streamly.Internal.Data.Fold as Fold
268−-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
272+#include "DocTestDataMutArray.hs"
269273
270274-------------------------------------------------------------------------------
271275-- Foreign helpers
@@ -305,14 +309,12 @@ memcmp p1 p2 len = do
305309
306310-- $arrayNotes
307311--
308−-- We can use an 'Unboxed' constraint in the MutArray type and the constraint can
309−-- be automatically provided to a function that pattern matches on the MutArray
310−-- type. However, it has huge performance cost, so we do not use it.
312+-- We can use an 'Unboxed' constraint in the MutArray type and the constraint
313+-- can be automatically provided to a function that pattern matches on the
314+-- MutArray type. However, it has huge performance cost, so we do not use it.
311315-- Investigate a GHC improvement possiblity.
312316
313−-- XXX Rename the fields to better names.
314−
315−-- | An unboxed, pinned mutable array. An array is created with a given length
317+-- | An unboxed mutable array. An array is created with a given length
316318-- and capacity. Length is the number of valid elements in the array. Capacity
317319-- is the maximum number of elements that the array can be expanded to without
318320-- having to reallocate the memory.
@@ -1520,7 +1522,8 @@ toListFB c n MutArray{..} = go arrStart
15201522
15211523-- XXX Monadic foldr/build fusion?
15221524-- Reference: https://www.researchgate.net/publication/220676509_Monadic_augment_and_generalised_short_cut_fusion
1523−-- | Convert an 'MutArray' into a list.
1525+
1526+-- | Convert a 'MutArray' into a list.
15241527--
15251528{-# INLINE toList #-}
15261529toList :: forall m a. (MonadIO m, Unbox a) => MutArray a -> m [a]
@@ -1813,7 +1816,7 @@ writeRevN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
18131816writeRevN = writeRevNWith newPinned
18141817
18151818-- | @writeNAligned align n@ folds a maximum of @n@ elements from the input
1816−-- stream to an 'MutArray' aligned to the given size.
1819+-- stream to a 'MutArray' aligned to the given size.
18171820--
18181821-- >>> writeNAligned align = MutArray.writeNWith (MutArray.newAlignedPinned align)
18191822-- >>> writeNAligned align n = MutArray.writeAppendN n (MutArray.newAlignedPinned align n)
@@ -1911,7 +1914,7 @@ write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
19111914
19121915-- | Use the 'writeN' fold instead.
19131916--
1914−-- >>> fromStreamDN n = StreamD.fold (MutArray.writeN n)
1917+-- >>> fromStreamDN n = Stream.fold (MutArray.writeN n)
19151918--
19161919{-# INLINE_NORMAL fromStreamDN #-}
19171920fromStreamDN :: forall m a. (MonadIO m, Unbox a)
@@ -1928,7 +1931,7 @@ fromStreamDN limit str = do
19281931 liftIO $ pokeWith arrContents ptr x
19291932 return $ INDEX_NEXT(ptr,a)
19301933
1931−-- | Create an 'MutArray' from the first N elements of a list. The array is
1934+-- | Create a 'MutArray' from the first N elements of a list. The array is
19321935-- allocated to size N, if the list terminates before N elements then the
19331936-- array may hold less than N elements.
19341937--
@@ -1978,7 +1981,7 @@ fromArrayStreamK as = do
19781981fromStreamD :: (MonadIO m, Unbox a) => D.Stream m a -> m (MutArray a)
19791982fromStreamD m = arrayStreamKFromStreamD m >>= fromArrayStreamK
19801983
1981−-- | Create an 'MutArray' from a list. The list must be of finite size.
1984+-- | Create a 'MutArray' from a list. The list must be of finite size.
19821985--
19831986{-# INLINE fromList #-}
19841987fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
core/streamly-core.cabal+2
@@ -40,6 +40,8 @@ extra-source-files:
4040 -- doctest include files
4141 src/DocTestDataArray.hs
4242 src/DocTestDataFold.hs
43+ src/DocTestDataMutArray.hs
44+ src/DocTestDataMutArrayGeneric.hs
4345
4446 -- This is duplicated
4547 src/Streamly/Internal/Data/Array/ArrayMacros.h