Copyright | (c) 2020 Composewell Technologies |
---|---|
License | BSD3-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- data Array a = Array {
- arrContents# :: MutableArray# RealWorld a
- arrStart :: !Int
- arrLen :: !Int
- arrTrueLen :: !Int
- newArray :: forall m a. MonadIO m => Int -> m (Array a)
- writeNUnsafe :: MonadIO m => Int -> Fold m a (Array a)
- writeN :: MonadIO m => Int -> Fold m a (Array a)
- putIndex :: MonadIO m => Array a -> Int -> a -> m ()
- putIndexUnsafe :: forall m a. MonadIO m => Array a -> Int -> a -> m ()
- modifyIndexUnsafe :: MonadIO m => Array a -> Int -> (a -> (a, b)) -> m b
- modifyIndex :: MonadIO m => Array a -> Int -> (a -> (a, b)) -> m b
- snocWith :: MonadIO m => (Int -> Int) -> Array a -> a -> m (Array a)
- snoc :: MonadIO m => Array a -> a -> m (Array a)
- snocUnsafe :: MonadIO m => Array a -> a -> m (Array a)
- read :: MonadIO m => Unfold m (Array a) a
- toStreamD :: MonadIO m => Array a -> Stream m a
- toStreamK :: MonadIO m => Array a -> Stream m a
- toList :: MonadIO m => Array a -> m [a]
- producer :: MonadIO m => Producer m (Array a) a
- getIndex :: MonadIO m => Array a -> Int -> m a
- getIndexUnsafe :: MonadIO m => Array a -> Int -> m a
- getSliceUnsafe :: Int -> Int -> Array a -> Array a
- getSlice :: Int -> Int -> Array a -> Array a
Type
Array | |
|
Constructing and Writing
Construction
Uninitialized Arrays
newArray :: forall m a. MonadIO m => Int -> m (Array a) Source #
newArray count
allocates an empty array that can hold count
items.
Pre-release
From streams
writeNUnsafe :: MonadIO m => Int -> Fold m a (Array a) Source #
Like writeN
but does not check the array bounds when writing. The fold
driver must not call the step function more than n
times otherwise it will
corrupt the memory and crash. This function exists mainly because any
conditional in the step function blocks fusion causing 10x performance
slowdown.
Pre-release
writeN :: MonadIO m => Int -> Fold m a (Array a) Source #
writeN n
folds a maximum of n
elements from the input stream to an
Array
.
>>>
writeN n = Fold.take n (Array.writeNUnsafe n)
Pre-release
From containers
Random writes
putIndex :: MonadIO m => Array a -> Int -> a -> m () Source #
O(1) Write the given element at the given index in the array. Performs in-place mutation of the array.
>>>
putIndex arr ix val = Array.modifyIndex arr ix (const (val, ()))
Pre-release
putIndexUnsafe :: forall m a. MonadIO m => Array a -> Int -> 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
modifyIndexUnsafe :: MonadIO m => Array a -> Int -> (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
modifyIndex :: MonadIO m => Array a -> Int -> (a -> (a, b)) -> m b Source #
Modify a given index of an array using a modifier function.
Pre-release
Growing and Shrinking
Appending elements
snocWith :: MonadIO m => (Int -> Int) -> Array a -> a -> m (Array a) Source #
snocWith sizer arr elem
mutates arr
to append elem
. The length of
the array increases by 1.
If there is no reserved space available in arr
it is reallocated to a size
in bytes determined by the sizer oldSize
function, where oldSize
is the
original size of the array.
Note that the returned array may be a mutated version of the original array.
Pre-release
snoc :: MonadIO m => Array a -> a -> m (Array 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 = Array.snocWith (* 2)
Performs O(n * log n) copies to grow, but is liberal with memory allocation.
Pre-release
snocUnsafe :: MonadIO m => Array a -> a -> m (Array a) Source #
Really really unsafe, appends the element into the first array, may cause silent data corruption or if you are lucky a segfault if the index is out of bounds.
Internal
Appending streams
Truncation
Eliminating and Reading
To streams
To containers
toStreamD :: MonadIO m => Array a -> Stream m a Source #
Use the read
unfold instead.
toStreamD = D.unfold read
We can try this if the unfold has any performance issues.
Random reads
getIndex :: MonadIO m => Array a -> Int -> m a Source #
O(1) Lookup the element at the given index. Index starts from 0.
getIndexUnsafe :: MonadIO m => Array a -> Int -> 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.
In-place Mutation Algorithms
Folding
Arrays of arrays
Operations dealing with multiple arrays, streams of arrays or multidimensional array representations.
Construct from streams
Eliminate to streams
Construct from arrays
O(1) Slice an array in constant time.
Unsafe: The bounds of the slice are not checked.
Unsafe
Pre-release
O(1) Slice an array in constant time. Throws an error if the slice extends out of the array bounds.
Pre-release