Safe Haskell | None |
---|---|
Language | Haskell2010 |
Rebase.Data.Vector.Generic.Base
Synopsis
- type family Mutable (v :: Type -> Type) = (mv :: Type -> Type -> Type) | mv -> v
- class MVector (Mutable v) a => Vector (v :: Type -> Type) a where
- basicUnsafeFreeze :: Mutable v s a -> ST s (v a)
- basicUnsafeThaw :: v a -> ST s (Mutable v s a)
- basicLength :: v a -> Int
- basicUnsafeSlice :: Int -> Int -> v a -> v a
- basicUnsafeIndexM :: v a -> Int -> Box a
- basicUnsafeCopy :: Mutable v s a -> v a -> ST s ()
- elemseq :: v a -> a -> b -> b
Documentation
type family Mutable (v :: Type -> Type) = (mv :: Type -> Type -> Type) | mv -> v #
Mutable v s a
is the mutable version of the immutable vector type v a
with
the state token s
. It is injective on GHC 8 and newer.
Instances
type Mutable Array # | |
Defined in Data.Vector.Generic.Base | |
type Mutable PrimArray # | |
Defined in Data.Vector.Generic.Base | |
type Mutable SmallArray # | |
Defined in Data.Vector.Generic.Base | |
type Mutable Vector # | |
Defined in Data.Vector | |
type Mutable Vector # | |
Defined in Data.Vector.Primitive | |
type Mutable Vector # | |
Defined in Data.Vector.Storable | |
type Mutable Vector # | |
Defined in Data.Vector.Strict | |
type Mutable Vector # | |
Defined in Data.Vector.Unboxed.Base |
class MVector (Mutable v) a => Vector (v :: Type -> Type) a where #
Class of immutable vectors. Every immutable vector is associated with its
mutable version through the Mutable
type family. Methods of this class
should not be used directly. Instead, Data.Vector.Generic and other
Data.Vector
modules provide safe and fusible wrappers.
Minimum complete implementation:
Minimal complete definition
basicUnsafeFreeze, basicUnsafeThaw, basicLength, basicUnsafeSlice, basicUnsafeIndexM
Methods
basicUnsafeFreeze :: Mutable v s a -> ST s (v a) #
Assumed complexity: O(1)
Unsafely convert a mutable vector to its immutable version without copying. The mutable vector may not be used after this operation.
basicUnsafeThaw :: v a -> ST s (Mutable v s a) #
Assumed complexity: O(1)
Unsafely convert an immutable vector to its mutable version without copying. The immutable vector may not be used after this operation.
basicLength :: v a -> Int #
Assumed complexity: O(1)
Yield the length of the vector.
Assumed complexity: O(1)
Yield a slice of the vector without copying it. No range checks are performed.
basicUnsafeIndexM :: v a -> Int -> Box a #
Assumed complexity: O(1)
Yield the element at the given position in a monad. No range checks are performed.
The monad allows us to be strict in the vector if we want. Suppose we had
unsafeIndex :: v a -> Int -> a
instead. Now, if we wanted to copy a vector, we'd do something like
copy mv v ... = ... unsafeWrite mv i (unsafeIndex v i) ...
For lazy vectors, the indexing would not be evaluated, which means that we would retain a reference to the original vector in each element we write. This is not what we want!
With basicUnsafeIndexM
, we can do
copy mv v ... = ... case basicUnsafeIndexM v i of Box x -> unsafeWrite mv i x ...
which does not have this problem, because indexing (but not the returned element!) is evaluated immediately.
basicUnsafeCopy :: Mutable v s a -> v a -> ST s () #
Assumed complexity: O(n)
Copy an immutable vector into a mutable one. The two vectors must have the same length, but this is not checked.
Instances of Vector
should redefine this method if they wish to support
an efficient block copy operation.
Default definition: copying based on basicUnsafeIndexM
and
basicUnsafeWrite
.
elemseq :: v a -> a -> b -> b #
Evaluate a
as far as storing it in a vector would and yield b
.
The v a
argument only fixes the type and is not touched. This method is
only used for optimisation purposes. Thus, it is safe for instances of
Vector
to evaluate a
less than it would be when stored in a vector,
although this might result in suboptimal code.
elemseq v x y = (singleton x `asTypeOf` v) `seq` y
Default definition: a
is not evaluated at all.