Copyright | (c) 2020 Composewell Technologies |
---|---|
License | BSD3-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Unconstrained version of Streamly.Data.MutArray module.
See the Streamly.Data.MutArray module for documentation.
Synopsis
- data MutArray a
- new :: MonadIO m => Int -> m (MutArray a)
- fromListN :: MonadIO m => Int -> [a] -> m (MutArray a)
- fromList :: MonadIO m => [a] -> m (MutArray a)
- writeN :: MonadIO m => Int -> Fold m a (MutArray a)
- write :: MonadIO m => Fold m a (MutArray a)
- snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
- putIndex :: MonadIO m => Int -> MutArray a -> a -> m ()
- putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m ()
- modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
- modifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
- getIndex :: MonadIO m => Int -> MutArray a -> m (Maybe a)
- getIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a
- toList :: MonadIO m => MutArray a -> m [a]
- read :: MonadIO m => MutArray a -> Stream m a
- readRev :: MonadIO m => MutArray a -> Stream m a
- reader :: MonadIO m => Unfold m (MutArray a) a
- length :: MutArray a -> Int
Setup
To execute the code examples provided in this module in ghci, please run the following commands first.
>>>
:m
>>>
import qualified Streamly.Data.Fold as Fold
>>>
import qualified Streamly.Data.MutArray.Generic as MutArray
>>>
import qualified Streamly.Data.Stream as Stream
For APIs that have not been released yet.
>>>
import Streamly.Internal.Data.MutArray.Generic as MutArray
Type
Construction
new :: MonadIO m => Int -> m (MutArray a) Source #
new count
allocates a zero length array that can be extended to hold
up to count
items without reallocating.
Pre-release
writeN :: MonadIO m => Int -> Fold m a (MutArray a) Source #
writeN n
folds a maximum of n
elements from the input stream to an
Array
.
>>>
writeN n = Fold.take n (MutArray.writeNUnsafe n)
Pre-release
write :: MonadIO m => Fold m a (MutArray a) Source #
Fold the whole input to a single array.
Same as writeWith
using an initial array size of arrayChunkSize
bytes
rounded up to the element size.
Caution! Do not use this on infinite streams.
Appending elements
snoc :: MonadIO m => MutArray a -> a -> m (MutArray a) Source #
The array is mutated to append an additional element to it. If there is no reserved space available in the array then it is reallocated to double the original size.
This is useful to reduce allocations when appending unknown number of elements.
Note that the returned array may be a mutated version of the original array.
>>>
snoc = MutArray.snocWith (* 2)
Performs O(n * log n) copies to grow, but is liberal with memory allocation.
Pre-release
Inplace mutation
putIndex :: MonadIO m => Int -> MutArray a -> a -> m () Source #
O(1) Write the given element at the given index in the array. Performs in-place mutation of the array.
>>>
putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))
Pre-release
putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m () Source #
Write the given element to the given index of the array. Does not check if the index is out of bounds of the array.
Pre-release
modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b Source #
Modify a given index of an array using a modifier function.
Pre-release
modifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b Source #
Modify a given index of an array using a modifier function without checking the bounds.
Unsafe because it does not check the bounds of the array.
Pre-release
Random reads
getIndex :: MonadIO m => Int -> MutArray a -> m (Maybe a) Source #
O(1) Lookup the element at the given index. Index starts from 0.
getIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a Source #
Return the element at the specified index without checking the bounds.
Unsafe because it does not check the bounds of the array.
Conversion
Streams
read :: MonadIO m => MutArray a -> Stream m a Source #
Generates a stream from the elements of a MutArray
.
>>>
read = Stream.unfold MutArray.reader