HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebasement-0.0.16Haskell2010

Basement.UArray

An unboxed array of primitive types

All the cells in the array are in one chunk of contiguous memory.

  • 1 type
  • 1 class
  • 80 values
  • Packagebasement-0.0.16
  • Exports82
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceUArray.hs
datadata UArray ty
#

An array of type built on top of GHC primitive.

The elements need to have fixed sized and the representation is a packed contiguous array in memory that can easily be passed to foreign interface

Constructors

Instances18IsList, Eq, Data, Ord, Show, Semigroup, …
classclass Eq ty => PrimType ty where
#

Represent the accessor for types that can be stored in the UArray and MUArray.

Types need to be a instance of storable and have fixed sized.

Associated types

  • type family PrimSize ty :: Nat

    type level size of the given ty

Methods

Instances20PrimType, …

methods

2 declarations
valueunsafeCopyAtRO
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)

    destination array

  3. -> Offset ty

    offset at destination

  4. -> UArray ty

    source array

  5. -> Offset ty

    offset at source

  6. -> CountOf ty

    number of elements to copy

  7. -> prim ()
#

Copy n sequential elements from the specified offset in a source array to the specified position in a destination array.

This function does not check bounds. Accessing invalid memory can return unpredictable and invalid values.

internal methods

7 declarations
valuerecast :: (PrimType a, PrimType b) => UArray a -> UArray b
#

Recast an array of type a to an array of b

a and b need to have the same size otherwise this raise an async exception

valueunsafeRecast :: (PrimType a, PrimType b) => UArray a -> UArray b
#

Unsafely recast an UArray containing a to an UArray containing b

The offset and size are converted from units of a to units of b, but no check are performed to make sure this is compatible.

use recast if unsure.

valuefreeze
  1. :: (PrimType ty, PrimMonad prim)
  2. => MUArray ty (PrimState prim)
  3. -> prim (UArray ty)
#

Freeze a MUArray into a UArray by copying all the content is a pristine new buffer

The MUArray in parameter can be still be used after the call without changing the resulting frozen data.

valuethaw
  1. :: (PrimMonad prim, PrimType ty)
  2. => UArray ty
  3. -> prim (MUArray ty (PrimState prim))
#

Thaw an array to a mutable array.

the array is not modified, instead a new mutable array is created and every values is copied, before returning the mutable array.

Creation

13 declarations
valuevFromListN :: PrimType ty => CountOf ty -> [ty] -> UArray ty
#

Make an array from a list of elements with a size hint.

The list should be of the same size as the hint, as otherwise:

  • The length of the list is smaller than the hint: the array allocated is of the size of the hint, but is sliced to only represent the valid bits

  • The length of the list is bigger than the hint: The allocated array is the size of the hint, and the list is truncated to fit.

valuenew
  1. :: (PrimMonad prim, PrimType ty)
  2. => CountOf ty
  3. -> prim (MUArray ty (PrimState prim))
#

Create a new mutable array of size @n.

When memory for a new array is allocated, we decide if that memory region should be pinned (will not be copied around by GC) or unpinned (can be moved around by GC) depending on its size.

You can change the threshold value used by setting the environment variable HS_FOUNDATION_UARRAY_UNPINNED_MAX.

valuecreate
  1. :: PrimType ty
  2. => CountOf ty

    the size of the array

  3. -> (Offset ty -> ty)

    the function that set the value at the index

  4. -> UArray ty

    the array created

#

Create a new array of size n by settings each cells through the function f.

valuecopyToPtr
  1. :: (PrimType ty, PrimMonad prim)
  2. => UArray ty

    the source array to copy

  3. -> Ptr ty

    The destination address where the copy is going to start

  4. -> prim ()
#

Copy all the block content to the memory starting at the destination address

valuewithPtr
  1. :: (PrimMonad prim, PrimType ty)
  2. => UArray ty
  3. -> Ptr ty -> prim a
  4. -> prim a
#

Get a Ptr pointing to the data in the UArray.

Since a UArray is immutable, this Ptr shouldn't be to use to modify the contents

If the UArray is pinned, then its address is returned as is, however if it's unpinned, a pinned copy of the UArray is made before getting the address.

valuewithMutablePtr
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Ptr ty -> prim a
  4. -> prim a
#

Create a pointer on the beginning of the mutable array and call a function f.

The mutable buffer can be mutated by the f function and the change will be reflected in the mutable array

If the mutable array is unpinned, a trampoline buffer is created and the data is only copied when f return.

valuetoBlock :: PrimType ty => UArray ty -> Block ty
#

Create a Block from a UArray.

Note that because of the slice, the destination block is re-allocated and copied, unless the slice point at the whole array

accessors

7 declarations
valueupdate :: PrimType ty => UArray ty -> [(Offset ty, ty)] -> UArray ty
#

update an array by creating a new array with the updates.

the operation copy the previous array, modify it in place, then freeze it.

valueunsafeIndex :: PrimType ty => UArray ty -> Offset ty -> ty
#

Return the element at a specific index from an array without bounds checking.

Reading from invalid memory can return unpredictable and invalid values. use index if unsure.

valueunsafeRead
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Offset ty
  4. -> prim ty
#

read from a cell in a mutable array without bounds checking.

Reading from invalid memory can return unpredictable and invalid values. use read if unsure.

valueunsafeWrite
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Offset ty
  4. -> ty
  5. -> prim ()
#

write to a cell in a mutable array without bounds checking.

Writing with invalid bounds will corrupt memory and your program will become unreliable. use write if unsure.

Functions

51 declarations
valueindex :: PrimType ty => UArray ty -> Offset ty -> ty
#

Return the element at a specific index from an array.

If the index @n is out of bounds, an error is raised.

valuetake :: CountOf ty -> UArray ty -> UArray ty
#

Take a count of elements from the array and create an array with just those elements

valuedrop :: CountOf ty -> UArray ty -> UArray ty
#

Drop a count of elements from the array and return the new array minus those dropped elements

valuesplitAt :: CountOf ty -> UArray ty -> (UArray ty, UArray ty)
#

Split an array into two, with a count of at most N elements in the first one and the remaining in the other.

valuerevSplitAt :: CountOf ty -> UArray ty -> (UArray ty, UArray ty)
#

Split an array at the N element from the end, and return the last N elements in the first part of the tuple, and whatever first elements remaining in the second

valuebreakEnd
  1. :: PrimType ty
  2. => ty -> Bool
  3. -> UArray ty
  4. -> (UArray ty, UArray ty)
#

Similar to break but start the search of the breakpoint from the end

breakEnd (> 0) [1,2,3,0,0,0]

([1,2,3], [0,0,0])

Similar to breakElem specialized to split on linefeed

it either returns: * Left. no line has been found, and whether the last character is a CR * Right, a line has been found with an optional CR, and it returns the array of bytes on the left of the CR/LF, and the the array of bytes on the right of the LF.

valuefromForeignPtr :: PrimType ty => (ForeignPtr ty, Int, Int) -> UArray ty
#

Create a foreign UArray from foreign memory and given offset/size

No check are performed to make sure this is valid, so this is unsafe.

This is particularly useful when dealing with foreign memory and ByteString