HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulestreamly-core-0.3.1Haskell2010

Streamly.Internal.Data.MutArray

  • 4 types
  • 266 values

MutArray.Type module

244 declarations
valueallocBytesToElemCount :: Unbox a => a -> Int -> Int
#

Given an Unboxed type (unused first arg) and real allocation size (including overhead), return how many elements of that type will completely fit in it, returns at least 1.

valueappend :: (MonadIO m, Unbox a) => m (MutArray a) -> Fold m a (MutArray a)
#

Deprecated. Please use append2 instead.

append action mutates the array generated by action to append the input stream. If there is no reserved space available in the array it is reallocated to double the size and aligned to power of 2.

Note that the returned array may be a mutated version of original array.

Example1 expression
append = Fold.foldlM' MutArray.snoc
valueappend2 :: (MonadIO m, Unbox a) => MutArray a -> Fold m a (MutArray a)
#

Fold append2 arr mutates the array arr to append the input stream. If there is no reserved space available in the array it is reallocated to double the size and aligned to power of 2.

Note that the returned array may be a mutated version of original array.

Example1 expression
append2 arr = Fold.foldlM' MutArray.snoc (pure arr)
valueappendGrowBy
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> Fold m a (MutArray a)
#

appendGrowBy arr mutates the array arr to append the input stream. If there is no reserved space available in the array it is reallocated to add space for the min number of elements supplied and align to block size if the array becomes larger than largeObjectThreshold.

Note that the returned array may be a mutated version of original array.

Example1 expression
appendGrowBy n arr = Fold.foldlM' (MutArray.snocGrowBy n) (pure arr)
valueappendMax
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> Fold m a (MutArray a)
#

Allocates space for n additional elements. The fold terminates after appending n elements. If less than n elements are supplied then the space for the remaining elements is guaranteed to be reserved.

Example1 expression
appendMax n arr = Fold.take n (MutArray.unsafeAppendMax n arr)
valueappendN
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> m (MutArray a)
  4. -> Fold m a (MutArray a)
#

Deprecated. Please use appendMax instead.

Append n elements to an existing array. Any free space left in the array after appending n elements is lost.

Example1 expression
appendN n initial = Fold.take n (MutArray.unsafeAppendN n initial)
valueappendWith
  1. :: (MonadIO m, Unbox a)
  2. => Int -> Int
  3. -> m (MutArray a)
  4. -> Fold m a (MutArray a)
#

appendWith sizer action mutates the array generated by action to append the input stream. If there is no reserved space available in the array it is reallocated to a size in bytes determined by sizer oldSize, where oldSize is the current size of the array in bytes. If the sizer returns less than or equal to the current size then the size is incremented by one element.

Note that the returned array may be a mutated version of original array.

Example1 expression
appendWith sizer = Fold.foldlM' (MutArray.snocWith sizer)

Pre-release

valuearrayChunkBytes :: Int
#

The default chunk size by which the array creation routines increase the size of the array when the array is grown linearly.

valuebreakAt :: Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
#

Create two slices of an array without copying the original array. The specified index i is the first index of the second slice.

valuebreakEndBy
  1. :: (MonadIO m, Unbox a)
  2. => a -> Bool
  3. -> MutArray a
  4. -> m (MutArray a, MutArray a)
#
Example4 expressions
arr <- MutArray.fromList "hello world"(a,b) <- MutArray.breakEndBy (== ' ') arrMutArray.toList a"hello "MutArray.toList b"world"
valuebreakEndBy_
  1. :: (MonadIO m, Unbox a)
  2. => a -> Bool
  3. -> MutArray a
  4. -> m (MutArray a, MutArray a)
#

Break the array into two slices when the predicate succeeds. The array element matching the predicate is dropped. If the predicate never succeeds the second array is empty.

Example4 expressions
arr <- MutArray.fromList "hello world"(a,b) <- MutArray.breakEndBy_ (== ' ') arrMutArray.toList a"hello"MutArray.toList b"world"

Pre-release

valuebubble :: (MonadIO m, Unbox a) => (a -> a -> Ordering) -> MutArray a -> m ()
#

Given an array sorted in ascending order except the last element being out of order, use bubble sort to place the last element at the right place such that the array remains sorted in ascending order.

Pre-release

valuebuildChunks
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Fold m a (StreamK n (MutArray a))
#

Buffer a stream into a stream of arrays.

Example1 expression
buildChunks n = Fold.many (MutArray.createOf n) Fold.toStreamK

Breaking an array into an array stream can be useful to consume a large array sequentially such that memory of the array is released incrementatlly.

See also: arrayStreamKFromStreamD.

Unimplemented

valuebyteCapacity :: MutArray a -> Int
#

Get the total capacity of an array. An array may have space reserved beyond the current used length of the array.

Pre-release

valuebyteCmp :: MonadIO m => MutArray a -> MutArray a -> m Ordering
#

Byte compare two arrays. 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.

Unsafe: Note that the Unbox instance of sum types with constructors of different sizes may leave some memory uninitialized which can make byte comparison unreliable.

Pre-release

valuebytesFree :: MutArray a -> Int
#

The remaining capacity in the array for appending more elements without reallocation.

Pre-release

valuecast :: Unbox b => MutArray a -> Maybe (MutArray b)
#

Cast an array having elements of type a into an array having elements of type b. The length of the array should be a multiple of the size of the target element otherwise Nothing is returned.

valuecastUnsafe :: MutArray a -> MutArray b
#

Deprecated. Please use unsafeCast instead.

Cast an array having elements of type a into an array having elements of type b. The array size must be a multiple of the size of type b otherwise accessing the last element of the array may result into a crash or a random value.

Pre-release

valuechunksEndBy
  1. :: (MonadIO m, Unbox a)
  2. => a -> Bool
  3. -> Stream m a
  4. -> Stream m (MutArray a)
#

Create arrays from the input stream using a predicate to find the end of the chunk. When the predicate matches, the chunk ends, the matching element is included in the chunk.

Definition:

Example1 expression
chunksEndBy p = Stream.foldMany (Fold.takeEndBy p MutArray.create)
valuechunksOf
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Stream m a
  4. -> Stream m (MutArray a)
#

chunksOf n stream groups the elements in the input stream into arrays of n elements each.

Same as the following but may be more efficient:

Example1 expression
chunksOf n = Stream.foldMany (MutArray.createOf n)

Pre-release

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

Clone the elements of a MutArray. Does not clone the reserve capacity.

To clone a slice of MutArray you can create a slice with "unsafeSliceOffLen" and then use "clone".

The new MutArray is unpinned in nature. Use "clone'" to clone the MutArray in pinned memory.

valuecompactGE
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Stream m (MutArray a)
  4. -> Stream m (MutArray a)
#

Deprecated. Please use compactMin instead.

compactMin n stream coalesces adjacent arrays in the stream until the compacted array size becomes greater than or equal to n.

Example1 expression
compactMin n = Stream.foldMany (MutArray.createCompactMin n)
valuecompactLeAs
  1. :: (MonadIO m, Unbox a)
  2. => PinnedState
  3. -> Int
  4. -> Stream m (MutArray a)
  5. -> Stream m (MutArray a)
#

This mutates the first array (if it has space) to append values from the second one. This would work for immutable arrays as well because an immutable array never has additional space so a new array is allocated instead of mutating it.

valuecompactMin
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Stream m (MutArray a)
  4. -> Stream m (MutArray a)
#

compactMin n stream coalesces adjacent arrays in the stream until the compacted array size becomes greater than or equal to n.

Example1 expression
compactMin n = Stream.foldMany (MutArray.createCompactMin n)
valueconcat :: (MonadIO m, Unbox a) => Stream m (MutArray a) -> Stream m a
#

Same as the following but may be more efficient due to better fusion:

Example1 expression
concat = Stream.unfoldEach MutArray.reader
valuecreate :: (MonadIO m, Unbox a) => Fold m a (MutArray a)
#

Fold the whole input to a single array.

Same as 'createMinOf using an initial array size of arrayChunkBytes bytes rounded up to the element size. If the array is expected to be smaller than arrayChunkBytes then use createMinOf to avoid wasting memory.

Caution! Do not use this on infinite streams.

valuecreateCompactMax
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Parser (MutArray a) m (MutArray a)
#

Parser createCompactMax maxElems coalesces adjacent arrays in the input stream only if the combined size would be less than or equal to maxElems elements. Note that it won't split an array if the original array is already larger than maxElems.

maxElems must be greater than 0.

Generates unpinned arrays irrespective of the pinning status of input arrays.

Note that a fold compacting to less than or equal to a given size is not possible, as folds cannot backtrack.

Internal

valuecreateCompactMin
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Fold m (MutArray a) (MutArray a)
#

Fold createCompactMin minElems coalesces adjacent arrays in the input stream until the size becomes greater than or equal to minElems.

Generates unpinned arrays irrespective of the pinning status of input arrays.

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

createMinOf count 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.

The array capacity is guaranteed to be at least count.

Caution! Do not use this on infinite streams.

Example3 expressions
f n = MutArray.appendWith (* 2) (MutArray.emptyOf n)createWith n = Fold.rmapM MutArray.rightSize (f n)createWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)

Pre-release

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

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

The array capacity is guranteed to be at least n.

Example3 expressions
createOf = MutArray.createWithOf MutArray.emptyOfcreateOf n = Fold.take n (MutArray.unsafeCreateOf n)createOf n = MutArray.appendMax n MutArray.empty
valuecreateOfWith
  1. :: (MonadIO m, Unbox a)
  2. => Int -> m (MutArray a)
  3. -> Int
  4. -> Fold m a (MutArray a)
#

Deprecated. Please use createWithOf instead.

createWithOf alloc n folds a maximum of n elements into an array allocated using the alloc function.

The array capacity is guranteed to be at least n.

Example2 expressions
createWithOf alloc n = Fold.take n (MutArray.unsafeCreateWithOf alloc n)createWithOf alloc n = MutArray.appendN (alloc n) n
valuecreateWith :: (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
#

Deprecated. Please use createMinOf instead.

createMinOf count 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.

The array capacity is guaranteed to be at least count.

Caution! Do not use this on infinite streams.

Example3 expressions
f n = MutArray.appendWith (* 2) (MutArray.emptyOf n)createWith n = Fold.rmapM MutArray.rightSize (f n)createWith n = Fold.rmapM MutArray.fromChunksK (MutArray.buildChunks n)

Pre-release

valuecreateWithOf
  1. :: (MonadIO m, Unbox a)
  2. => Int -> m (MutArray a)
  3. -> Int
  4. -> Fold m a (MutArray a)
#

createWithOf alloc n folds a maximum of n elements into an array allocated using the alloc function.

The array capacity is guranteed to be at least n.

Example2 expressions
createWithOf alloc n = Fold.take n (MutArray.unsafeCreateWithOf alloc n)createWithOf alloc n = MutArray.appendN (alloc n) n
valuedivideBy
  1. :: Int
  2. -> MutArray a -> m (MutArray a, MutArray a)
  3. -> MutArray a
  4. -> m ()
#

divideBy level partition array performs a top down hierarchical recursive partitioning fold of items in the container using the given function as the partition function. Level indicates the level in the tree where the fold would stop.

This performs a quick sort if the partition function is 'partitionBy (< pivot)'.

Unimplemented

valuedropAround
  1. :: (Unbox a, MonadIO m)
  2. => a -> Bool
  3. -> MutArray a
  4. -> m (MutArray a)
#

Strip elements which match the predicate, from both ends.

Example3 expressions
arr <- MutArray.fromList "   hello world    "a <- MutArray.dropAround (== ' ') arrMutArray.toList a"hello world"

Pre-release

valuedropWhile
  1. :: (Unbox a, MonadIO m)
  2. => a -> Bool
  3. -> MutArray a
  4. -> m (MutArray a)
#

Strip elements which match the predicate, from the start of the array.

Example3 expressions
arr <- MutArray.fromList "    hello world"a <- MutArray.dropWhile (== ' ') arrMutArray.toList a"hello world"

Pre-release

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

Allocates an unpinned array of zero length but growable to the specified capacity without reallocation.

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

Allocates a pinned array of zero length but growable to the specified capacity without reallocation.

valueemptyWithAligned
  1. :: (MonadIO m, Unbox a)
  2. => Int -> Int -> IO MutByteArray
  3. -> Int
  4. -> Int
  5. -> m (MutArray a)
#

emptyWithAligned allocator alignment count allocates a new array of zero length and with a capacity to hold count elements, using allocator size alignment as the memory allocator function.

Alignment must be greater than or equal to machine word size and a power of 2.

Alignment is ignored if the allocator allocates unpinned memory.

Pre-release

valuefCompactGE
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Fold m (MutArray a) (MutArray a)
#

Deprecated. Please use createCompactMin instead.

Fold createCompactMin minElems coalesces adjacent arrays in the input stream until the size becomes greater than or equal to minElems.

Generates unpinned arrays irrespective of the pinning status of input arrays.

valuefold :: (MonadIO m, Unbox a) => Fold m a b -> MutArray a -> m b
#

Fold an array using a Fold.

For example:

Example1 expression
findIndex eq = MutArray.fold (Fold.findIndex eq)

Pre-release

valuefoldRev :: (MonadIO m, Unbox a) => Fold m a b -> MutArray a -> m b
#

Fold an arary starting from end up to beginning.

For example:

Example1 expression
findIndexRev eq = MutArray.foldRev (Fold.findIndex eq)
valuefromCString# :: MonadIO m => Addr# -> m (MutArray Word8)
#

fromCString# addr copies a C string consisting of bytes and terminated by a null byte, into a Word8 array. The null byte is not copied.

Example1 expression
MutArray.fromCString# "hello"#

Unsafe:

The caller has to ensure that:

  1. the addr is pinned and alive during the call.

  2. the pointer passed is valid up to the point where null byte is found.

valuefromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
#

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

valuefromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8)
#

fromPtrN len addr copies len bytes from addr into an array.

Unsafe:

The caller has to ensure that:

  1. the pointer is pinned and alive during the call.

  2. the pointer passed is valid up to the given length.

valuefromStream :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
#

Create an Array from a stream. This is useful when we want to create a single array from a stream of unknown size. createOf is at least twice as efficient when the size is already known.

Note that if the input stream is too large memory allocation for the array may fail. When the stream size is not known, chunksOf followed by processing of indvidual arrays in the resulting stream should be preferred.

Pre-release

valuefromStreamD :: (MonadIO m, Unbox a) => Stream m a -> m (MutArray a)
#

Deprecated. Please use fromStream instead.

We could take the approach of doubling the memory allocation on each overflow. This would result in more or less the same amount of copying as in the chunking approach. However, if we have to shrink in the end then it may result in an extra copy of the entire data.

Example1 expression
fromStreamD = StreamD.fold MutArray.create
valuefromW16CString# :: MonadIO m => Addr# -> m (MutArray Word16)
#

fromW16CString# addr copies a C string consisting of 16-bit wide chars and terminated by a 16-bit null char, into a Word16 array. The null character is not copied.

Useful for copying UTF16 strings on Windows.

Unsafe:

The caller has to ensure that:

  1. the addr is pinned and alive during the call.

  2. the pointer passed is valid up to the point where null Word16 is found.

valuegetIndexRev :: (MonadIO m, Unbox a) => Int -> MutArray a -> m a
#

O(1) Lookup the element at the given index from the end of the array. Index starts from 0.

Slightly faster than computing the forward index and using getIndex.

valuegetIndexUnsafe :: (MonadIO m, Unbox a) => Int -> MutArray a -> m a
#

Deprecated. Please use unsafeGetIndex instead.

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

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

valuegetSlice
  1. :: Unbox a
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> MutArray a
  5. -> MutArray a
#

Deprecated. Please use sliceOffLen instead.

O(1) Get a reference to a slice from a mutable array. Throws an error if the slice extends out of the array bounds.

The capacity of the slice is the same as its length i.e. it does not have any unused or reserved space at the end.

The slice shares the same underlying mutable array when created. However, if the slice or the original array is reallocated by growing or shrinking then it will be copied to new memory and they will no longer share the same memory.

Pre-release

valuegetSliceUnsafe
  1. :: Unbox a
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> MutArray a
  5. -> MutArray a
#

Deprecated. Please use unsafeSliceOffLen instead.

O(1) Slice an array in constant time.

Unsafe: The bounds of the slice are not checked.

Unsafe

Pre-release

valuegrow :: (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
#

Deprecated. Please use growTo instead.

growTo newCapacity array changes the total capacity of the array so that it is enough to hold the specified number of elements. Nothing is done if the specified capacity is less than the length of the array.

If the capacity is more than largeObjectThreshold then it is rounded up to the block size (4K).

Nothing is done if the requested capacity is <= 0.

Pre-release

valuegrowBy :: (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
#

Like growTo but specifies the required reserve (unused) capacity rather than the total capacity. Increases the reserve capacity, if required, to at least the given amount.

Nothing is done if the requested capacity is <= 0.

valuegrowTo :: (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
#

growTo newCapacity array changes the total capacity of the array so that it is enough to hold the specified number of elements. Nothing is done if the specified capacity is less than the length of the array.

If the capacity is more than largeObjectThreshold then it is rounded up to the block size (4K).

Nothing is done if the requested capacity is <= 0.

Pre-release

valueindexReader
  1. :: (MonadIO m, Unbox a)
  2. => Stream m Int
  3. -> Unfold m (MutArray a) a
#

Given an unfold that generates array indices, read the elements on those indices from the supplied MutArray. An error is thrown if an index is out of bounds.

Pre-release

valuelCompactGE
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Fold m (MutArray a) ()
  4. -> Fold m (MutArray a) ()
#

Deprecated. Please use scanCompactMin instead.

Like compactGE but for transforming folds instead of stream.

> lCompactGE n = Fold.many (MutArray.fCompactGE n)

Generates unpinned arrays irrespective of the pinning status of input arrays.

valuelength :: Unbox a => MutArray a -> Int
#

O(1) Get the used length of the array i.e. the number of elements in the array.

Note that byteLength is less expensive than this operation, as length involves a costly division operation.

valuemergeBy :: Int -> (MutArray a -> MutArray a -> m ()) -> MutArray a -> m ()
#

mergeBy level merge array performs a pairwise bottom up fold recursively merging the pairs using the supplied merge function. Level indicates the level in the tree where the fold would stop.

This performs a random shuffle if the merge function is random. If we stop at level 0 and repeatedly apply the function then we can do a bubble sort.

Unimplemented

valuemodify :: (MonadIO m, Unbox a) => MutArray a -> (a -> a) -> m ()
#

Modify each element of an array using the supplied modifier function.

This is an in-place equivalent of an immutable map operation.

Pre-release

valuemodifyIndexUnsafe
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> a -> (a, b)
  5. -> m b
#

Deprecated. Please use unsafeModifyIndex instead.

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

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

Pre-release

valuenewArrayWith
  1. :: (MonadIO m, Unbox a)
  2. => Int -> Int -> IO MutByteArray
  3. -> Int
  4. -> Int
  5. -> m (MutArray a)
#

Deprecated. Please use emptyWithAligned instead.

emptyWithAligned allocator alignment count allocates a new array of zero length and with a capacity to hold count elements, using allocator size alignment as the memory allocator function.

Alignment must be greater than or equal to machine word size and a power of 2.

Alignment is ignored if the allocator allocates unpinned memory.

Pre-release

valuenil :: MutArray a
#

Deprecated. Please use empty instead.

valuepCompactLE
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Parser (MutArray a) m (MutArray a)
#

Deprecated. Please use createCompactMax instead.

Parser createCompactMax maxElems coalesces adjacent arrays in the input stream only if the combined size would be less than or equal to maxElems elements. Note that it won't split an array if the original array is already larger than maxElems.

maxElems must be greater than 0.

Generates unpinned arrays irrespective of the pinning status of input arrays.

Note that a fold compacting to less than or equal to a given size is not possible, as folds cannot backtrack.

Internal

valuepeek
  1. :: (MonadIO m, Unbox a)
  2. => MutArray Word8
  3. -> m (Maybe a, MutArray Word8)
#

Create a Haskell value from its unboxed representation from the head of a byte array, return the value and the remaining array.

Like uncons except that the value is deserialized from the byte array.

Note: If you are deserializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive an Unbox instance of the entire record.

valuepeekUncons
  1. :: (MonadIO m, Unbox a)
  2. => MutArray Word8
  3. -> m (Maybe a, MutArray Word8)
#

Deprecated. Please use peek instead.

Create a Haskell value from its unboxed representation from the head of a byte array, return the value and the remaining array.

Like uncons except that the value is deserialized from the byte array.

Note: If you are deserializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive an Unbox instance of the entire record.

valuepeekUnconsUnsafe
  1. :: (MonadIO m, Unbox a)
  2. => MutArray Word8
  3. -> m (a, MutArray Word8)
#

Deprecated. Please use unsafePeek instead.

Really really unsafe, create a Haskell value from an unboxed byte array, does not check if the array is big enough, may return garbage or if you are lucky may cause a segfault.

Internal

valuepermute :: MutArray a -> m Bool
#

Generate the next permutation of the sequence, returns False if this is the last permutation.

Unimplemented

valuepin :: MutArray a -> IO (MutArray a)
#

Return a copy of the array in pinned memory if unpinned, else return the original array.

valuepinnedEmptyOf :: (MonadIO m, Unbox a) => Int -> m (MutArray a)
#

Deprecated. Please use emptyOf' instead.

Allocates a pinned array of zero length but growable to the specified capacity without reallocation.

valuepinnedNewAligned :: (MonadIO m, Unbox a) => Int -> Int -> m (MutArray a)
#

Deprecated. Please use emptyOf' to create a Word8 array and cast it accordingly.

Like emptyWithAligned but using an allocator is a pinned memory allocator and the alignment is dictated by the Unboxed instance of the type.

Internal

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

Deprecated. Please use emptyOf' to create a Word8 array and cast it accordingly.

Allocates a pinned empty array that with a reserved capacity of bytes. The memory of the array is uninitialized and the allocation is aligned as per the Unboxed instance of the type.

pinnedNewBytes = (unsafeCast :: Array Word8 -> a) . emptyOf'

Pre-release

valuepoke :: (MonadIO m, Unbox a) => MutArray Word8 -> a -> m (MutArray Word8)
#

Unbox a Haskell type and append the resulting bytes to a mutable byte array. The array is grown exponentially when more space is needed.

Like snoc except that the value is unboxed to the byte array.

Note: If you are serializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive an Unbox instance of the entire record.

valuepokeAppend
  1. :: (MonadIO m, Unbox a)
  2. => MutArray Word8
  3. -> a
  4. -> m (MutArray Word8)
#

Deprecated. Please use poke instead.

Unbox a Haskell type and append the resulting bytes to a mutable byte array. The array is grown exponentially when more space is needed.

Like snoc except that the value is unboxed to the byte array.

Note: If you are serializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive an Unbox instance of the entire record.

valueputIndex :: (MonadIO m, Unbox a) => 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.

Example3 expressions
putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))f = MutArray.putIndicesputIndex ix arr val = Stream.fold (f arr) (Stream.fromPure (ix, val))
valueputIndexUnsafe :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m ()
#

Deprecated. Please use unsafePutIndex instead.

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, Unbox a) => 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

valuerangeBy :: (a -> a -> Ordering) -> MutArray a -> IO (Maybe (a, a))
#

Find the minimum and maximum elements in the array using the provided comparison function.

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

Deprecated. Please use reallocBytes instead.

realloc newCapacity array reallocates the array to the specified capacity in bytes.

If the new size is less than the original array the array gets truncated. If the new size is not a multiple of array element size then it is rounded down to multiples of array size. If the new size is more than largeObjectThreshold then it is rounded up to the block size (4K).

If the original array is pinned, the newly allocated array is also pinned.

valuereallocBytes :: (MonadIO m, Unbox a) => Int -> MutArray a -> m (MutArray a)
#

realloc newCapacity array reallocates the array to the specified capacity in bytes.

If the new size is less than the original array the array gets truncated. If the new size is not a multiple of array element size then it is rounded down to multiples of array size. If the new size is more than largeObjectThreshold then it is rounded up to the block size (4K).

If the original array is pinned, the newly allocated array is also pinned.

valuereallocBytesWith
  1. :: (MonadIO m, Unbox a)
  2. => String
  3. -> Int -> Int
  4. -> Int
  5. -> MutArray a
  6. -> m (MutArray a)
#

reallocBytesWith label capSizer minIncrBytes array. The label is used in error messages and the capSizer is used to determine the capacity of the new array in bytes given the current byte length of the array.

valuerevDropWhile
  1. :: (Unbox a, MonadIO m)
  2. => a -> Bool
  3. -> MutArray a
  4. -> m (MutArray a)
#

Strip elements which match the predicate, from the end of the array.

Example3 expressions
arr <- MutArray.fromList "hello world    "a <- MutArray.revDropWhile (== ' ') arrMutArray.toList a"hello world"

Pre-release

valuereverse :: (MonadIO m, Unbox a) => MutArray a -> m ()
#

You may not need to reverse an array because you can consume it in reverse using readerRev. To reverse large arrays you can read in reverse and write to another array. However, in-place reverse can be useful to take adavantage of cache locality and when you do not want to allocate additional memory.

valuerightSize :: (MonadIO m, Unbox a) => MutArray a -> m (MutArray a)
#

Resize the allocated memory to drop any reserved free space at the end of the array and reallocate it to reduce wastage.

Up to 25% wastage is allowed to avoid reallocations. If the capacity is more than largeObjectThreshold then free space up to the blockSize is retained.

Pre-release

valueshuffleBy :: (a -> a -> m Bool) -> MutArray a -> MutArray a -> m ()
#

Shuffle corresponding elements from two arrays using a shuffle function. If the shuffle function returns False then do nothing otherwise swap the elements. This can be used in a bottom up fold to shuffle or reorder the elements.

Unimplemented

valuesliceOffLen
  1. :: Unbox a
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> MutArray a
  5. -> MutArray a
#

O(1) Get a reference to a slice from a mutable array. Throws an error if the slice extends out of the array bounds.

The capacity of the slice is the same as its length i.e. it does not have any unused or reserved space at the end.

The slice shares the same underlying mutable array when created. However, if the slice or the original array is reallocated by growing or shrinking then it will be copied to new memory and they will no longer share the same memory.

Pre-release

valuesnoc :: (MonadIO m, Unbox a) => 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 and aligned to a power of 2.

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.

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

valuesnocGrowBy
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> a
  5. -> 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 grow it by adding space for the requested number of elements, the new size is rounded up to blockSize when the size becomes more than largeObjectThreshold. If the size specified is <= 0 then the array is grown by one element.

Note that the returned array may be a mutated version of the original array.

Performs O(n^2) copies to grow but is thrifty on memory compared to snoc.

Pre-release

valuesnocLinear :: (MonadIO m, Unbox a) => MutArray a -> a -> m (MutArray a)
#

Deprecated. Please use snocGrowBy instead. snocLinear ~ snocGrowBy (1024 / sizeOf (Proxy :: Proxy a) + 1)

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 grow it by arrayChunkBytes rounded up to blockSize when the size becomes more than largeObjectThreshold.

Note that the returned array may be a mutated version of the original array.

Performs O(n^2) copies to grow but is thrifty on memory.

Pre-release

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

Deprecated. Please use unsafeSnoc instead.

Really really unsafe, appends the element into the first array, may cause silent data corruption or if you are lucky a segfault if the first array does not have enough space to append the element.

Internal

valuesnocWith
  1. :: (MonadIO m, Unbox a)
  2. => Int -> Int
  3. -> MutArray a
  4. -> a
  5. -> m (MutArray a)
#

snocWith sizer arr elem mutates arr to append elem. The used 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 oldSizeBytes function, where oldSizeBytes is the original size of the array in bytes. The sizer function should return a capacity more than or equal to the current used size. If the capacity returned is less than or equal to the current used size, the array is still grown by one element.

If the new array size is more than largeObjectThreshold then it is rounded up to blockSize.

Note that the returned array may be a mutated version of the original array.

Pre-release

valuesplice :: (MonadIO m, Unbox a) => MutArray a -> MutArray a -> m (MutArray a)
#

The first array is extended in-place to append the second array. If there is no reserved space available in the first array then a new allocation of exact required size is done.

Note that the returned array may be an extended version of first array, referring to the same memory as the original array.

Example1 expression
splice = MutArray.spliceWith (+)

If the original array is pinned the spliced array is also pinned.

Pre-release

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

Copy two arrays into a newly allocated array. If the first array is pinned the spliced array is also pinned.

Note: If you freeze and splice it will create a new array.

valuespliceExp
  1. :: (MonadIO m, Unbox a)
  2. => MutArray a
  3. -> MutArray a
  4. -> m (MutArray a)
#

Like append but the growth of the array is exponential. Whenever a new allocation is required the previous array size is at least doubled.

This is useful to reduce allocations when folding many arrays together.

Note that the returned array may be a mutated version of first array.

Example1 expression
spliceExp = MutArray.spliceWith (\l1 l2 -> max (l1 * 2) (l1 + l2))

Pre-release

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

Deprecated. Please use unsafeSplice instead.

Really really unsafe, appends the second array into the first array. If the first array does not have enough space it may cause silent data corruption or if you are lucky a segfault.

valuespliceWith
  1. :: (MonadIO m, Unbox a)
  2. => Int -> Int -> Int
  3. -> MutArray a
  4. -> MutArray a
  5. -> m (MutArray a)
#

spliceWith sizer dst src mutates dst to append src. If there is no reserved space available in dst it is reallocated to a size determined by the sizer dstBytes srcBytes function, where dstBytes is the size of the first array and srcBytes is the size of the second array, in bytes.

Note that the returned array may be a mutated version of first array.

Pre-release

valuesplitAt :: Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
#

Deprecated. Please use breakAt instead.

Create two slices of an array without copying the original array. The specified index i is the first index of the second slice.

valuesplitOn
  1. :: (MonadIO m, Unbox a)
  2. => a -> Bool
  3. -> MutArray a
  4. -> Stream m (MutArray a)
#

Deprecated. Please use splitEndBy_ instead.

Generate a stream of array slices using a predicate. The array element matching the predicate is dropped.

Pre-release

valuestrip :: (Unbox a, MonadIO m) => (a -> Bool) -> MutArray a -> m (MutArray a)
#

Deprecated. Please use dropAround instead.

Strip elements which match the predicate, from both ends.

Example3 expressions
arr <- MutArray.fromList "   hello world    "a <- MutArray.dropAround (== ' ') arrMutArray.toList a"hello world"

Pre-release

valueunpin :: MutArray a -> IO (MutArray a)
#

Return a copy of the array in unpinned memory if pinned, else return the original array.

valueunsafeAppendMax
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> Fold m a (MutArray a)
#

unsafeAppendMax n arr appends up to n input items to the supplied array.

Unsafe: Do not drive the fold beyond n elements, it will lead to memory corruption or segfault.

Internal

valueunsafeAppendN
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> m (MutArray a)
  4. -> Fold m a (MutArray a)
#

Deprecated. Please use unsafeAppendMax instead.

unsafeAppendN n arr appends up to n input items to the supplied array.

Unsafe: Do not drive the fold beyond n elements, it will lead to memory corruption or segfault.

Any free space left in the array after appending n elements is lost.

Internal

Append specified number of bytes from a given pointer to the MutArray.

Unsafe:

The caller has to ensure that:

  1. the MutArray is valid up to the given length.

  2. the source pointer is pinned and alive during the call.

  3. the pointer passed is valid up to the given length.

valueunsafeAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> IO b) -> m b
#

unsafeAsPtr arr f, f is a function used as f ptr len where ptr is a pointer to the beginning of array and len is the byte-length of the array.

Unsafe WARNING:

  1. The array must be pinned, otherwise it will lead to memory corruption.

  2. The user must not use the pointer beyond the supplied length.

Pre-release

valueunsafeBreakAt :: Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
#

Like breakAt but does not check whether the index is valid.

Example1 expression
unsafeBreakAt i arr = (MutArray.unsafeSliceOffLen 0 i arr, MutArray.unsafeSliceOffLen i (MutArray.length arr - i) arr)
valueunsafeCast :: MutArray a -> MutArray b
#

Cast an array having elements of type a into an array having elements of type b. The array size must be a multiple of the size of type b otherwise accessing the last element of the array may result into a crash or a random value.

Pre-release

valueunsafeCreateOf :: (MonadIO m, Unbox a) => 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.

Example1 expression
unsafeCreateOf = MutArray.unsafeCreateWithOf MutArray.emptyOf

unsafeCreateWithPtr' capacity populator creates a pinned array of capacity bytes and invokes the populator function to populate it. populator ptr len gets the pointer to the array and MUST return the amount of the capacity populated in bytes.

Unsafe because the populator is allowed to use the pointer only up to specified length. In other words, bytes populated MUST be less than or equal to the total capacity.

valueunsafeGetIndex :: (MonadIO m, Unbox a) => 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.

valueunsafeModifyIndex
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> MutArray a
  4. -> a -> (a, b)
  5. -> m b
#

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

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

Pre-release

valueunsafePeek
  1. :: (MonadIO m, Unbox a)
  2. => MutArray Word8
  3. -> m (a, MutArray Word8)
#

Really really unsafe, create a Haskell value from an unboxed byte array, does not check if the array is big enough, may return garbage or if you are lucky may cause a segfault.

Internal

valueunsafePinnedAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> m b) -> m b
#

Deprecated. Pin the array and then use unsafeAsPtr.

NOTE: this is deprecated because it can lead to accidental problems if the user tries to use it to mutate the array because it does not return the new array after pinning.

valueunsafePutIndex :: (MonadIO m, Unbox a) => 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

valueunsafeSnoc :: (MonadIO m, Unbox a) => 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 first array does not have enough space to append the element.

Internal

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

Really really unsafe, appends the second array into the first array. If the first array does not have enough space it may cause silent data corruption or if you are lucky a segfault.

valueunsafeSwapIndices
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Int
  4. -> MutArray a
  5. -> m ()
#

Swap the elements at two indices without validating the indices.

Unsafe: This could result in memory corruption if indices are not valid.

Pre-release

valuevacate :: MutArray a -> MutArray a
#

Reset the array end position to start, thus truncating the array to 0 length, making it empty. The capacity of the array remains unchanged. The array refers to the same memory as before.

valueblockSize :: Int
#

The page or block size used by the GHC allocator. Allocator allocates at least a block and then allocates smaller allocations from within a block.

datadata MutArray a
#

An unboxed mutable array. An array is created with a given length and capacity. Length is the number of valid elements in the array. Capacity is the maximum number of elements that the array can be expanded to without having to reallocate the memory.

The elements in the array can be mutated in-place without changing the reference (constructor). However, the length of the array cannot be mutated in-place. A new array reference is generated when the length changes. When the length is increased (upto the maximum reserved capacity of the array), the array is not reallocated and the new reference uses the same underlying memory as the old one.

Several routines in this module allow the programmer to control the capacity of the array. The programmer can control the trade-off between memory usage and performance impact due to reallocations when growing or shrinking the array.

Constructors

MutArray module

8 declarations
valueindexerFromLen
  1. :: (Monad m, Unbox a)
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> Unfold m (MutArray a) (Int, Int)
#

Generate a stream of array slice descriptors ((index, len)) of specified length from an array, starting from the supplied array index. The last slice may be shorter than the requested length depending on the array length.

Pre-release

valuesplitterFromLen
  1. :: (Monad m, Unbox a)
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> Unfold m (MutArray a) (MutArray a)
#

Generate a stream of slices of specified length from an array, starting from the supplied array index. The last slice may be shorter than the requested length depending on the array length.

Pre-release

valuecompactMax
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Stream m (MutArray a)
  4. -> Stream m (MutArray a)
#

compactLE maxElems coalesces adjacent arrays in the input stream only if the combined size would be less than or equal to maxElems elements. Note that it won't split an array if the original array is already larger than maxElems.

maxElems must be greater than 0.

Generates unpinned arrays irrespective of the pinning status of input arrays.

Serialization

4 declarations
valueserialize
  1. :: (MonadIO m, Serialize a)
  2. => MutArray Word8
  3. -> a
  4. -> m (MutArray Word8)
#

Serialize the supplied Haskell value at the end of the mutable array, growing the array size. If there is no reserve capacity left in the array the array is reallocated to double the current size.

Like snoc except that the value is serialized to the byte array.

Note: If you are serializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive an Serialize instance of the entire record.

Unstable API

valuedeserialize
  1. :: (MonadIO m, Serialize a)
  2. => MutArray Word8
  3. -> m (a, MutArray Word8)
#

Deserialize a Haskell value from the beginning of a mutable array. The deserialized value is removed from the array and the remaining array is returned.

Like uncons except that the value is deserialized from the byte array.

Note: If you are deserializing a large number of small fields, and the types are statically known, then it may be more efficient to declare a record of those fields and derive Serialize instance of the entire record.

Unstable API

valueserializePtrN :: MutArray Word8 -> Ptr a -> Int -> m (MutArray Word8)
#

Serializes a (Ptr, len) pair in the same way as an array. The serialized value can be de-serialized as an array or consumed as a pointer using deserializePtrN.

The Ptr must be pinned or the existence of the Ptr must be ensured by the user of this API.

Unimplemented

valuedeserializePtrN
  1. :: MutArray Word8
  2. -> Ptr a -> Int -> m b
  3. -> m (a, MutArray Word8)
#

Consume a serialized array or (Ptr, length) from the MutArray using an IO action that consumes the pointer directly.

WARNING! The array must be a pinned array.

Unimplemented

Deprecated

14 declarations
valueslicerFromLen
  1. :: (Monad m, Unbox a)
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> Unfold m (MutArray a) (MutArray a)
#

Deprecated. Please use splitterFromLen instead.

Generate a stream of slices of specified length from an array, starting from the supplied array index. The last slice may be shorter than the requested length depending on the array length.

Pre-release

valuesliceIndexerFromLen
  1. :: (Monad m, Unbox a)
  2. => Int

    from index

  3. -> Int

    length of the slice

  4. -> Unfold m (MutArray a) (Int, Int)
#

Deprecated. Please use indexerFromLen instead.

Generate a stream of array slice descriptors ((index, len)) of specified length from an array, starting from the supplied array index. The last slice may be shorter than the requested length depending on the array length.

Pre-release

valuecompactLE
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Stream m (MutArray a)
  4. -> Stream m (MutArray a)
#

Deprecated. Please use compactMax instead.

compactLE maxElems coalesces adjacent arrays in the input stream only if the combined size would be less than or equal to maxElems elements. Note that it won't split an array if the original array is already larger than maxElems.

maxElems must be greater than 0.

Generates unpinned arrays irrespective of the pinning status of input arrays.

typetype IORef = IORef
#

Deprecated. Use IORef from MutByteArray module.