HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Type

1 declaration
datadata MutArray a
#

Constructors

  • MutArray
    • arrContents# :: MutableArray# RealWorld a

      The internal contents of the array representing the entire array.

    • arrStart :: !Int

      The starting index of this slice.

    • arrLen :: !Int

      The length of this slice.

    • arrTrueLen :: !Int

      This is the true length of the array. Coincidentally, this also represents the first index beyond the maximum acceptable index of the array. This is specific to the array contents itself and not dependent on the slice. This value should not change and is shared across all the slices.

Constructing and Writing

0 declarations

Construction

valuenil :: MonadIO m => m (MutArray a)
#

Definition:

Example1 expression
nil = MutArray.new 0

Uninitialized Arrays

valueemptyOf :: MonadIO m => Int -> m (MutArray a)
#

emptyOf count allocates a zero length array that can be extended to hold up to count items without reallocating.

Pre-release

From streams

valueunsafeCreateOf :: MonadIO m => Int -> Fold m a (MutArray a)
#

Like createOf 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

valuecreateOf :: MonadIO m => Int -> Fold m a (MutArray a)
#

createOf n folds a maximum of n elements from the input stream to an Array.

Example1 expression
createOf n = Fold.take n (MutArray.unsafeCreateOf n)

Pre-release

valuecreateWith :: MonadIO m => Int -> Fold m a (MutArray a)
#

createWith minCount folds the whole input to a single array. The array starts at a size big enough to hold minCount elements, the size is doubled every time the array needs to be grown.

Caution! Do not use this on infinite streams.

Pre-release

valuecreate :: MonadIO m => Fold m a (MutArray a)
#

Fold the whole input to a single array.

Same as createWith using an initial array size of arrayChunkSize bytes rounded up to the element size.

Caution! Do not use this on infinite streams.

valuefromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)
#

Create a MutArray from the first n elements of a stream. The array is allocated to size n, if the stream terminates before n elements then the array may hold less than n elements.

From containers

Random writes

5 declarations
valueputIndex :: MonadIO m => Int -> MutArray a -> a -> m ()
#

O(1) Write the given element at the given index in the array. Performs in-place mutation of the array.

Example1 expression
putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))

Pre-release

valueputIndexUnsafe :: MonadIO m => Int -> MutArray a -> a -> m ()
#

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

valueputIndices :: MonadIO m => MutArray a -> Fold m (Int, a) ()
#

Write an input stream of (index, value) pairs to an array. Throws an error if any index is out of bounds.

Pre-release

valuemodifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
#

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

valuemodifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
#

Modify a given index of an array using a modifier function.

Pre-release

Growing and Shrinking

0 declarations

Reallocation

valuerealloc :: MonadIO m => Int -> MutArray a -> m (MutArray a)
#

Reallocates the array according to the new size. This is a safe function that always creates a new array and copies the old array into the new one. If the reallocated size is less than the original array it results in a truncated version of the original array.

valueuninit :: MonadIO m => MutArray a -> Int -> m (MutArray a)
#

Make the uninitialized memory in the array available for use extending it by the supplied length beyond the current length of the array. The array may be reallocated.

Appending elements

valuesnocWith :: MonadIO m => (Int -> Int) -> MutArray a -> a -> m (MutArray a)
#

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

valuesnoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
#

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.

Example1 expression
snoc = MutArray.snocWith (* 2)

Performs O(n * log n) copies to grow, but is liberal with memory allocation.

Pre-release

valuesnocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
#

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

0 declarations

Unfolds

To containers

valueread :: MonadIO m => MutArray a -> Stream m a
#

Generates a stream from the elements of a MutArray.

Example1 expression
read = Stream.unfold MutArray.reader

Random reads

valuegetIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a
#

Return the element at the specified index without checking the bounds.

Unsafe because it does not check the bounds of the array.

Size

1 declaration

In-place Mutation Algorithms

1 declaration

Folding

2 declarations
valuecmp :: (MonadIO m, Ord a) => MutArray a -> MutArray a -> m Ordering
#

Compare the length of the arrays. If the length is equal, compare the lexicographical ordering of two underlying byte arrays otherwise return the result of length comparison.

Pre-release

Arrays of arrays

0 declarations

Operations dealing with multiple arrays, streams of arrays or multidimensional array representations.

Construct from streams

valuechunksOf :: MonadIO m => Int -> Stream m a -> Stream m (MutArray a)
#

chunksOf n stream groups the input stream into a stream of arrays of size n.

chunksOf n = foldMany (MutArray.writeN n)

Pre-release

Eliminate to streams

Construct from arrays

valuegetSliceUnsafe
  1. :: Int

    from index

  2. -> Int

    length of the slice

  3. -> MutArray a
  4. -> MutArray a
#

O(1) Slice an array in constant time.

Unsafe: The bounds of the slice are not checked.

Unsafe

Pre-release

valuegetSlice
  1. :: Int

    from index

  2. -> Int

    length of the slice

  3. -> MutArray a
  4. -> MutArray a
#

O(1) Slice an array in constant time. Throws an error if the slice extends out of the array bounds.

Pre-release

Appending arrays

Deprecated

5 declarations