Skip to content

Latest commit

 

History

History
113 lines (70 loc) · 2.58 KB

File metadata and controls

113 lines (70 loc) · 2.58 KB

Module Data.Array.ST

Helper functions for working with mutable arrays using the ST effect.

This module can be used when performance is important and mutation is a local effect.

STArray

data STArray :: * -> * -> *

A reference to a mutable array.

The first type parameter represents the memory region which the array belongs to. The second type parameter defines the type of elements of the mutable array.

The runtime representation of a value of type STArray h a is the same as that of [a], except that mutation is allowed.

Assoc

type Assoc a = { value :: a, index :: Int }

An element and its index

runSTArray

runSTArray :: forall a r. (forall h. Eff (st :: ST h | r) (STArray h a)) -> Eff r (Array a)

Freeze a mutable array, creating an immutable array. Use this function as you would use runST to freeze a mutable reference.

The rank-2 type prevents the reference from escaping the scope of runSTArray.

emptySTArray

emptySTArray :: forall a h r. Eff (st :: ST h | r) (STArray h a)

Create an empty mutable array.

thaw

thaw :: forall a h r. Array a -> Eff (st :: ST h | r) (STArray h a)

Create a mutable copy of an immutable array.

freeze

freeze :: forall a h r. STArray h a -> Eff (st :: ST h | r) (Array a)

Create an immutable copy of a mutable array.

peekSTArray

peekSTArray :: forall a h r. STArray h a -> Int -> Eff (st :: ST h | r) (Maybe a)

Read the value at the specified index in a mutable array.

pokeSTArray

pokeSTArray :: forall a h r. STArray h a -> Int -> a -> Eff (st :: ST h | r) Boolean

Change the value at the specified index in a mutable array.

pushSTArray

pushSTArray :: forall a h r. STArray h a -> a -> Eff (st :: ST h | r) Int

Append an element to the end of a mutable array.

pushAllSTArray

pushAllSTArray :: forall a h r. STArray h a -> Array a -> Eff (st :: ST h | r) Int

Append the values in an immutable array to the end of a mutable array.

spliceSTArray

spliceSTArray :: forall a h r. STArray h a -> Int -> Int -> Array a -> Eff (st :: ST h | r) (Array a)

Remove and/or insert elements from/into a mutable array at the specified index.

toAssocArray

toAssocArray :: forall a h r. STArray h a -> Eff (st :: ST h | r) (Array (Assoc a))

Create an immutable copy of a mutable array, where each element is labelled with its index in the original array.