ArrayRef-0.1.3: Unboxed references, dynamic arrays and moreSource codeContentsIndex
Data.ArrayBZ.Internals.MArray
PortabilityGHC/Hugs
Stabilityexperimental
MaintainerBulat Ziganshin <Bulat.Ziganshin@gmail.com>
Description
Mutable arrays: class and general algorithms Freeze/Thaw: mutable-immutable arrays conversion
Synopsis
arrEleBottom :: a
class Monad m => HasMutableBounds a m where
getBounds :: Ix i => a i e -> m (i, i)
class (Monad m, HasMutableBounds a m) => MArray a e m where
newArray :: Ix i => (i, i) -> e -> m (a i e)
newArray_ :: Ix i => (i, i) -> m (a i e)
unsafeRead :: Ix i => a i e -> Int -> m e
unsafeWrite :: Ix i => a i e -> Int -> e -> m ()
readArray :: Ix i => a i e -> i -> m e
writeArray :: Ix i => a i e -> i -> e -> m ()
newListArray :: (MArray a e m, Ix i) => (i, i) -> [e] -> m (a i e)
getIndices :: (MArray a e m, Ix i) => a i e -> m [i]
getElems :: (MArray a e m, Ix i) => a i e -> m [e]
getAssocs :: (MArray a e m, Ix i) => a i e -> m [(i, e)]
mapArray :: (MArray a e' m, MArray a e m, Ix i) => (e' -> e) -> a i e' -> m (a i e)
mapIndices :: (MArray a e m, Ix i, Ix j) => (i, i) -> (i -> j) -> a j e -> m (a i e)
freeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e)
unsafeFreeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e)
thaw :: (Ix i, IArray a e, MArray b e m) => a i e -> m (b i e)
unsafeThaw :: (Ix i, IArray a e, MArray b e m) => a i e -> m (b i e)
Documentation
arrEleBottom :: aSource
Value used to initialize undefined array elements
class Monad m => HasMutableBounds a m whereSource
Class of array types with mutable bounds
Methods
getBounds :: Ix i => a i e -> m (i, i)Source
Get the current bounds of an array
show/hide Instances
class (Monad m, HasMutableBounds a m) => MArray a e m whereSource

Class of mutable array types.

An array type has the form (a i e) where a is the array type constructor (kind * -> * -> *), i is the index type (a member of the class Ix), and e is the element type.

The MArray class is parameterised over both a and e (so that instances specialised to certain element types can be defined, in the same way as for IArray), and also over the type of the monad, m, in which the mutable array will be manipulated.

Methods
newArray :: Ix i => (i, i) -> e -> m (a i e)Source
Builds a new array, with every element initialised to the supplied value.
newArray_ :: Ix i => (i, i) -> m (a i e)Source
Builds a new array, with every element initialised to undefined.
unsafeRead :: Ix i => a i e -> Int -> m eSource
unsafeWrite :: Ix i => a i e -> Int -> e -> m ()Source
readArray :: Ix i => a i e -> i -> m eSource
writeArray :: Ix i => a i e -> i -> e -> m ()Source
show/hide Instances
Storable e => MArray StorableArray e IO
Storable e => MArray StorableArray e IO
(STorIO m s, Unboxed e) => MArray (UnboxedMutableArray s) e m
STorIO m s => MArray (BoxedMutableArray s) e m
Unboxed e => MArray (STUArray s) e (ST s)
Unboxed e => MArray (STUArray s) e (ST s)
MArray (STArray s) e (ST s)
MArray (STArray s) e (ST s)
(MArray a e m, Ref m r) => MArray (Dynamic r a) e m
newListArray :: (MArray a e m, Ix i) => (i, i) -> [e] -> m (a i e)Source
Constructs a mutable array from a list of initial elements. The list gives the elements of the array in ascending order beginning with the lowest index.
getIndices :: (MArray a e m, Ix i) => a i e -> m [i]Source
Return a list of all the indexes of a mutable array
getElems :: (MArray a e m, Ix i) => a i e -> m [e]Source
Return a list of all the elements of a mutable array
getAssocs :: (MArray a e m, Ix i) => a i e -> m [(i, e)]Source
Return a list of all the associations of a mutable array, in index order.
mapArray :: (MArray a e' m, MArray a e m, Ix i) => (e' -> e) -> a i e' -> m (a i e)Source
Constructs a new array derived from the original array by applying a function to each of the elements.
mapIndices :: (MArray a e m, Ix i, Ix j) => (i, i) -> (i -> j) -> a j e -> m (a i e)Source
Constructs a new array derived from the original array by applying a function to each of the indices.
freeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e)Source
Converts a mutable array (any instance of MArray) to an immutable array (any instance of IArray) by taking a complete copy of it.
unsafeFreeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e)Source

Converts an mutable array into an immutable array. The implementation may either simply cast the array from one type to the other without copying the array, or it may take a full copy of the array.

Note that because the array is possibly not copied, any subsequent modifications made to the mutable version of the array may be shared with the immutable version. It is safe to use, therefore, if the mutable version is never modified after the freeze operation.

The non-copying implementation is supported between certain pairs of array types only; one constraint is that the array types must have identical representations. In GHC, The following pairs of array types have a non-copying O(1) implementation of unsafeFreeze. Because the optimised versions are enabled by specialisations, you will need to compile with optimisation (-O) to get them.

  • Data.Array.IO.IOUArray -> Data.Array.Unboxed.UArray
  • Data.Array.ST.STUArray -> Data.Array.Unboxed.UArray
  • Data.Array.IO.IOArray -> Data.Array.Array
  • Data.Array.ST.STArray -> Data.Array.Array
thaw :: (Ix i, IArray a e, MArray b e m) => a i e -> m (b i e)Source
Converts an immutable array (any instance of IArray) into a mutable array (any instance of MArray) by taking a complete copy of it.
unsafeThaw :: (Ix i, IArray a e, MArray b e m) => a i e -> m (b i e)Source

Converts an immutable array into a mutable array. The implementation may either simply cast the array from one type to the other without copying the array, or it may take a full copy of the array.

Note that because the array is possibly not copied, any subsequent modifications made to the mutable version of the array may be shared with the immutable version. It is only safe to use, therefore, if the immutable array is never referenced again in this thread, and there is no possibility that it can be also referenced in another thread. If you use an unsafeThawwriteunsafeFreeze sequence in a multi-threaded setting, then you must ensure that this sequence is atomic with respect to other threads, or a garbage collector crash may result (because the write may be writing to a frozen array).

The non-copying implementation is supported between certain pairs of array types only; one constraint is that the array types must have identical representations. In GHC, The following pairs of array types have a non-copying O(1) implementation of unsafeThaw. Because the optimised versions are enabled by specialisations, you will need to compile with optimisation (-O) to get them.

  • Data.Array.Unboxed.UArray -> Data.Array.IO.IOUArray
  • Data.Array.Unboxed.UArray -> Data.Array.ST.STUArray
  • Data.Array.Array -> Data.Array.IO.IOArray
  • Data.Array.Array -> Data.Array.ST.STArray
Produced by Haddock version 2.4.2