transients-0: Transients

Copyright(c) Edward Kmett 2015
LicenseBSD-style
MaintainerEdward Kmett <ekmett@gmail.com>
Portabilitynon-portable
Safe HaskellUnsafe
LanguageHaskell2010

Data.Transient.Primitive.SmallArray

Description

Small primitive boxed arrays

Synopsis

Documentation

data SmallMutableArray s a Source

Mutable boxed arrays associated with a primitive state token.

Instances

newSmallArray :: PrimMonad m => Int -> a -> m (SmallMutableArray (PrimState m) a) Source

Create a new mutable array of the specified size and initialise all elements with the given value.

readSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> m a Source

Read a value from the array at the given index.

writeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> a -> m () Source

Write a value to the array at the given index.

indexSmallArray :: SmallArray a -> Int -> a Source

Read a value from the immutable array at the given index.

indexSmallArrayM :: Monad m => SmallArray a -> Int -> m a Source

Monadically read a value from the immutable array at the given index. This allows us to be strict in the array while remaining lazy in the read element which is very useful for collective operations. Suppose we want to copy an array. We could do something like this:

copy marr arr ... = do ...
                       writeSmallArray marr i (indexSmallArray arr i) ...
                       ...

But since primitive arrays are lazy, the calls to indexSmallArray will not be evaluated. Rather, marr will be filled with thunks each of which would retain a reference to arr. This is definitely not what we want!

With indexSmallArrayM, we can instead write

copy marr arr ... = do ...
                       x <- indexSmallArrayM arr i
                       writeSmallArray marr i x
                       ...

Now, indexing is executed immediately although the returned element is still not evaluated.

unsafeFreezeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> m (SmallArray a) Source

Convert a mutable array to an immutable one without copying. The array should not be modified after the conversion.

unsafeThawSmallArray :: PrimMonad m => SmallArray a -> m (SmallMutableArray (PrimState m) a) Source

Convert an immutable array to an mutable one without copying. The immutable array should not be used after the conversion.

sameSmallMutableArray :: SmallMutableArray s a -> SmallMutableArray s a -> Bool Source

Check whether the two arrays refer to the same memory block.

copySmallArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

destination array

-> Int

offset into destination array

-> SmallArray a

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of an immutable array to a mutable array.

copySmallMutableArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

destination array

-> Int

offset into destination array

-> SmallMutableArray (PrimState m) a

source array

-> Int

offset into source array

-> Int

number of elements to copy

-> m () 

Copy a slice of a mutable array to another array. The two arrays may not be the same.

cloneSmallArray Source

Arguments

:: SmallArray a

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> SmallArray a 

Return a newly allocated SmallArray with the specified subrange of the provided SmallArray. The provided SmallArray should contain the full subrange specified by the two Ints, but this is not checked.

cloneSmallMutableArray Source

Arguments

:: PrimMonad m 
=> SmallMutableArray (PrimState m) a

source array

-> Int

offset into destination array

-> Int

number of elements to copy

-> m (SmallMutableArray (PrimState m) a) 

Return a newly allocated SmallMutableArray. with the specified subrange of the provided SmallMutableArray. The provided SmallMutableArray should contain the full subrange specified by the two Ints, but this is not checked.

casSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> a -> a -> m (Int, a) Source

Perform an unsafe, machine-level atomic compare and swap on an element within an array.