Safe Haskell | None |
---|---|
Language | Haskell2010 |
Arrays of boxed elements. This mimics the API of Data.Primitive.Array
.
However, all functions that deal with indices are total and cannot cause
runtime crashes.
- data Vector n a
- data MutableVector n s a
- new :: PrimMonad m => Length n -> m (MutableVector n (PrimState m) a)
- replicate :: PrimMonad m => Length n -> a -> m (MutableVector n (PrimState m) a)
- index :: Vector n a -> Index n -> a
- read :: PrimMonad m => MutableVector n (PrimState m) a -> Index n -> m a
- write :: PrimMonad m => MutableVector n (PrimState m) a -> Index n -> a -> m ()
- length :: Vector n a -> Length n
- size :: MutableVector n s a -> Length n
- thaw :: PrimMonad m => Vector n a -> m (MutableVector n (PrimState m) a)
- unsafeFreeze :: PrimMonad m => MutableVector n (PrimState m) a -> m (Vector n a)
- forget :: Vector n a -> Array a
- with :: Array a -> (forall n. Vector n a -> b) -> b
- zipWith :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c
- reverse :: Vector n a -> Vector n a
- update :: Vector n a -> PrimVector m (Index n) -> Vector m a -> Vector n a
- accumulate :: (a -> b -> a) -> Vector n a -> PrimVector m (Index n) -> Vector m b -> Vector n a
- backpermute :: Vector n a -> PrimVector m (Index n) -> Vector m a
Types
data MutableVector n s a Source #
Primops
index :: Vector n a -> Index n -> a Source #
Index into a vector. Use this with functions from Data.Primitive.Index.Types to get a hold of indices that can be used.
>>>
let cities = ["Cairo","Atlanta","Tokyo"] :: Array String
>>>
with cities $ \v -> ascendM (\ix -> putStrLn ("I ❤️ " ++ index v ix)) (length v)
I ❤️ Cairo I ❤️ Atlanta I ❤️ Tokyo
size :: MutableVector n s a -> Length n Source #
Get the length of a mutable vector.
thaw :: PrimMonad m => Vector n a -> m (MutableVector n (PrimState m) a) Source #
Create a mutable array from a slice of an immutable array.
This operation makes a copy of the immutable array, so it is safe to use the immutable array afterward.
unsafeFreeze :: PrimMonad m => MutableVector n (PrimState m) a -> m (Vector n a) Source #
Freeze the mutable vector. The argument must not be reused after this function is called on it.
Array Interop
forget :: Vector n a -> Array a Source #
Discard the phantom length associated with an indexed vector.
with :: Array a -> (forall n. Vector n a -> b) -> b Source #
Pass an unindexed array to a function that operates on a vector of any length.
Functions
All of these can be safely written using the other primitives provided
by this module. They are provided here to demonstrate how to write index-safe
variants of functions from the vector
library without sacrificing performance.
zipWith :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c Source #
Zip two vectors of equal length together by applying the given function.
:: Vector n a | initial vector (of length |
-> PrimVector m (Index n) | vector of indices |
-> Vector m a | vector of values |
-> Vector n a |
O(m+n) For each pair (i,a)
from the index/value pair vectors,
replace the vector element at position i
by a
.
:: (a -> b -> a) | accumulating function |
-> Vector n a | initial vector (of length |
-> PrimVector m (Index n) | vector of indices |
-> Vector m b | vector of values |
-> Vector n a |
O(m+n) For each pair (i,a)
from the index/value pair vectors,
replace the vector element at position i
by f a b
.
backpermute :: Vector n a -> PrimVector m (Index n) -> Vector m a Source #
O(n) Yield the vector obtained by replacing each element i
of the
index vector by
.index
xs i