Copyright | (C) 2013 Amgen Inc. |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Vectors that can be passed to and from R with no copying at all. These
vectors are an instance of Data.Vector.Storable, where the memory is
allocated from the R heap, in such a way that they can be converted to
a SEXP
through simple pointer arithmetic (see toSEXP
) in constant time.
The main difference between Data.Vector.SEXP and Data.Vector.Storable is that the former uses a header-prefixed data layout (the header immediately precedes the payload of the vector). This means that no additional pointer dereferencing is needed to reach the vector data. The trade-off is that most slicing operations are O(N) instead of O(1).
If you make heavy use of slicing, then it's best to convert to
a Data.Vector.Storable vector first, using unsafeToStorable
.
Note that since unstream
relies on slicing operations, it will still be an
O(N) operation but it will copy vector data twice (instead of once).
Synopsis
- data Vector (ty :: SEXPTYPE) a = Vector {
- vectorBase :: !(ForeignSEXP ty)
- vectorOffset :: !Int32
- vectorLength :: !Int32
- data MVector s ty a = MVector {
- mvectorBase :: !(SEXP s ty)
- mvectorOffset :: !Int32
- mvectorLength :: !Int32
- type family ElemRep s (a :: SEXPTYPE) where ...
- type VECTOR s ty a = (Storable a, IsVector ty, SingI ty)
- type SVECTOR ty a = (Storable a, IsVector ty, SingI ty, ElemRep V ty ~ a)
- fromSEXP :: SVECTOR ty a => SEXP s ty -> Vector ty a
- unsafeFromSEXP :: SVECTOR ty a => SEXP s ty -> Vector ty a
- toSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty
- unsafeToSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty
- length :: SVECTOR ty a => Vector ty a -> Int
- null :: SVECTOR ty a => Vector ty a -> Bool
- (!) :: SVECTOR ty a => Vector ty a -> Int -> a
- (!?) :: SVECTOR ty a => Vector ty a -> Int -> Maybe a
- head :: SVECTOR ty a => Vector ty a -> a
- last :: SVECTOR ty a => Vector ty a -> a
- unsafeIndex :: SVECTOR ty a => Vector ty a -> Int -> a
- unsafeHead :: SVECTOR ty a => Vector ty a -> a
- unsafeLast :: SVECTOR ty a => Vector ty a -> a
- indexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a
- headM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
- lastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
- unsafeIndexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a
- unsafeHeadM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
- unsafeLastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a
- slice :: SVECTOR ty a => Int -> Int -> Vector ty a -> Vector ty a
- init :: SVECTOR ty a => Vector ty a -> Vector ty a
- take :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
- drop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
- tail :: SVECTOR ty a => Vector ty a -> Vector ty a
- splitAt :: SVECTOR ty a => Int -> Vector ty a -> (Vector ty a, Vector ty a)
- unsafeTail :: SVECTOR ty a => Vector ty a -> Vector ty a
- unsafeSlice :: SVECTOR ty a => Int -> Int -> Vector ty a -> Vector ty a
- unsafeDrop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
- unsafeTake :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a
- unsafeInit :: SVECTOR ty a => Vector ty a -> Vector ty a
- empty :: SVECTOR ty a => Vector ty a
- singleton :: SVECTOR ty a => a -> Vector ty a
- replicate :: SVECTOR ty a => Int -> a -> Vector ty a
- generate :: SVECTOR ty a => Int -> (Int -> a) -> Vector ty a
- iterateN :: SVECTOR ty a => Int -> (a -> a) -> a -> Vector ty a
- replicateM :: (Monad m, SVECTOR ty a) => Int -> m a -> m (Vector ty a)
- generateM :: (Monad m, SVECTOR ty a) => Int -> (Int -> m a) -> m (Vector ty a)
- create :: SVECTOR ty a => (forall r. ST r (MVector r ty a)) -> Vector ty a
- unfoldr :: SVECTOR ty a => (b -> Maybe (a, b)) -> b -> Vector ty a
- unfoldrN :: SVECTOR ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector ty a
- constructN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a
- constructrN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a
- enumFromN :: (SVECTOR ty a, Num a) => a -> Int -> Vector ty a
- enumFromStepN :: (SVECTOR ty a, Num a) => a -> a -> Int -> Vector ty a
- enumFromTo :: (SVECTOR ty a, Enum a) => a -> a -> Vector ty a
- enumFromThenTo :: (SVECTOR ty a, Enum a) => a -> a -> a -> Vector ty a
- cons :: SVECTOR ty a => a -> Vector ty a -> Vector ty a
- snoc :: SVECTOR ty a => Vector ty a -> a -> Vector ty a
- (++) :: SVECTOR ty a => Vector ty a -> Vector ty a -> Vector ty a
- concat :: SVECTOR ty a => [Vector ty a] -> Vector ty a
- force :: SVECTOR ty a => Vector ty a -> Vector ty a
- (//) :: SVECTOR ty a => Vector ty a -> [(Int, a)] -> Vector ty a
- unsafeUpd :: SVECTOR ty a => Vector ty a -> [(Int, a)] -> Vector ty a
- accum :: SVECTOR ty a => (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
- unsafeAccum :: SVECTOR ty a => (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a
- reverse :: SVECTOR ty a => Vector ty a -> Vector ty a
- map :: (SVECTOR ty a, SVECTOR ty b) => (a -> b) -> Vector ty a -> Vector ty b
- imap :: (SVECTOR ty a, SVECTOR ty b) => (Int -> a -> b) -> Vector ty a -> Vector ty b
- concatMap :: (SVECTOR tya a, SVECTOR tyb b) => (a -> Vector tyb b) -> Vector tya a -> Vector tyb b
- mapM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => (a -> m b) -> Vector ty a -> m (Vector ty b)
- mapM_ :: (Monad m, SVECTOR ty a) => (a -> m b) -> Vector ty a -> m ()
- forM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => Vector ty a -> (a -> m b) -> m (Vector ty b)
- forM_ :: (Monad m, SVECTOR ty a) => Vector ty a -> (a -> m b) -> m ()
- zipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
- zipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
- zipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
- zipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f
- zipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g
- izipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (Int -> a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c
- izipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (Int -> a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d
- izipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (Int -> a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e
- izipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f
- izipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g
- zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c, ElemRep V tya ~ a, ElemRep V tyb ~ b, ElemRep V tyc ~ c) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m (Vector tyc c)
- zipWithM_ :: (Monad m, SVECTOR tya a, SVECTOR tyb b) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m ()
- filter :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
- ifilter :: SVECTOR ty a => (Int -> a -> Bool) -> Vector ty a -> Vector ty a
- filterM :: (Monad m, SVECTOR ty a) => (a -> m Bool) -> Vector ty a -> m (Vector ty a)
- takeWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
- dropWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a
- partition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
- unstablePartition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
- span :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
- break :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a)
- elem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool
- notElem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool
- find :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe a
- findIndex :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe Int
- elemIndex :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Maybe Int
- foldl :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a
- foldl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
- foldl' :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a
- foldl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
- foldr :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b
- foldr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
- foldr' :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b
- foldr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a
- ifoldl :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a
- ifoldl' :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a
- ifoldr :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b
- ifoldr' :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b
- all :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool
- any :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool
- sum :: (SVECTOR ty a, Num a) => Vector ty a -> a
- product :: (SVECTOR ty a, Num a) => Vector ty a -> a
- maximum :: (SVECTOR ty a, Ord a) => Vector ty a -> a
- maximumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a
- minimum :: (SVECTOR ty a, Ord a) => Vector ty a -> a
- minimumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a
- minIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int
- minIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int
- maxIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int
- maxIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int
- foldM :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a
- foldM' :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a
- fold1M :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a
- fold1M' :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a
- foldM_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m ()
- foldM'_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m ()
- fold1M_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m ()
- fold1M'_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m ()
- prescanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- prescanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- postscanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- postscanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- scanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- scanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a
- scanl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
- scanl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
- prescanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- prescanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- postscanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- postscanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- scanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- scanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b
- scanr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
- scanr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a
- toList :: SVECTOR ty a => Vector ty a -> [a]
- fromList :: forall ty a. SVECTOR ty a => [a] -> Vector ty a
- fromListN :: forall ty a. SVECTOR ty a => Int -> [a] -> Vector ty a
- freeze :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a)
- thaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a)
- copy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m ()
- unsafeFreeze :: (VECTOR (Region m) ty a, MonadR m, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a)
- unsafeThaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a)
- unsafeCopy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m ()
- toString :: Vector 'Char Word8 -> String
- toByteString :: Vector 'Char Word8 -> ByteString
- unsafeWithByteString :: NFData a => Vector 'Char Word8 -> (ByteString -> IO a) -> a
Documentation
data Vector (ty :: SEXPTYPE) a Source #
Immutable vectors. The second type paramater is a phantom parameter
reflecting at the type level the tag of the vector when viewed as a SEXP
.
The tag of the vector and the representation type are related via ElemRep
.
Vector | |
|
Mutable R vector. Represented in memory with the same header as SEXP
nodes. The second type parameter is phantom, reflecting at the type level the
tag of the vector when viewed as a SEXP
. The tag of the vector and the
representation type are related via ElemRep
.
MVector | |
|
type family ElemRep s (a :: SEXPTYPE) where ... Source #
Function from R types to the types of the representations of each element in the vector.
type VECTOR s ty a = (Storable a, IsVector ty, SingI ty) Source #
Constraint synonym for all operations on vectors.
type SVECTOR ty a = (Storable a, IsVector ty, SingI ty, ElemRep V ty ~ a) Source #
Constraint synonym for all operations on vectors.
unsafeFromSEXP :: SVECTOR ty a => SEXP s ty -> Vector ty a Source #
O(1) Unsafe convert a mutable SEXP
to an immutable vector without
copying. The mutable vector must not be used after this operation, lest one
runs the risk of breaking referential transparency.
toSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty Source #
O(n) Yield a (mutable) copy of the vector as a SEXP
.
unsafeToSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty Source #
O(1) Unsafely convert an immutable vector to a (mutable) SEXP
without
copying. The immutable vector must not be used after this operation.
Accessors
Length information
Indexing
unsafeIndex :: SVECTOR ty a => Vector ty a -> Int -> a Source #
O(1) Unsafe indexing without bounds checking
unsafeHead :: SVECTOR ty a => Vector ty a -> a Source #
O(1) First element without checking if the vector is empty
unsafeLast :: SVECTOR ty a => Vector ty a -> a Source #
O(1) Last element without checking if the vector is empty
Monadic indexing
indexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a Source #
O(1) Indexing in a monad.
The monad allows operations to be strict in the vector when necessary. Suppose vector copying is implemented like this:
copy mv v = ... write mv i (v ! i) ...
For lazy vectors, v ! i
would not be evaluated which means that mv
would unnecessarily retain a reference to v
in each element written.
With indexM
, copying can be implemented like this instead:
copy mv v = ... do x <- indexM v i write mv i x
Here, no references to v
are retained because indexing (but not the
elements) is evaluated eagerly.
headM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #
O(1) First element of a vector in a monad. See indexM
for an
explanation of why this is useful.
lastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #
O(1) Last element of a vector in a monad. See indexM
for an
explanation of why this is useful.
unsafeIndexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a Source #
O(1) Indexing in a monad without bounds checks. See indexM
for an
explanation of why this is useful.
unsafeHeadM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #
O(1) First element in a monad without checking for empty vectors.
See indexM
for an explanation of why this is useful.
unsafeLastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #
O(1) Last element in a monad without checking for empty vectors.
See indexM
for an explanation of why this is useful.
Extracting subvectors (slicing)
O(N) Yield a slice of the vector with copying it. The vector must
contain at least i+n
elements.
init :: SVECTOR ty a => Vector ty a -> Vector ty a Source #
O(N) Yield all but the last element, this operation will copy an array. The vector may not be empty.
take :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #
O(N) Yield at the first n
elements with copying. The vector may
contain less than n
elements in which case it is returned unchanged.
drop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #
O(N) Yield all but the first n
elements with copying. The vector may
contain less than n
elements in which case an empty vector is returned.
tail :: SVECTOR ty a => Vector ty a -> Vector ty a Source #
O(N) Copy all but the first element. The vector may not be empty.
unsafeTail :: SVECTOR ty a => Vector ty a -> Vector ty a Source #
O(N) Yield all but the first element with copying. The vector may not be empty but this is not checked.
O(N) Yield a slice of the vector with copying. The vector must
contain at least i+n
elements but this is not checked.
unsafeDrop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #
O(N) Yield all but the first n
elements with copying. The vector
must contain at least n
elements but this is not checked.
unsafeTake :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #
O(N) Yield the first n
elements with copying. The vector must
contain at least n
elements but this is not checked.
unsafeInit :: SVECTOR ty a => Vector ty a -> Vector ty a Source #
O(N) Yield all but the last element with copying. The vector may not be empty but this is not checked.
Construction
Initialisation
replicate :: SVECTOR ty a => Int -> a -> Vector ty a Source #
O(n) Vector of the given length with the same value in each position
generate :: SVECTOR ty a => Int -> (Int -> a) -> Vector ty a Source #
O(n) Construct a vector of the given length by applying the function to each index
iterateN :: SVECTOR ty a => Int -> (a -> a) -> a -> Vector ty a Source #
O(n) Apply function n times to value. Zeroth element is original value.
Monadic initialisation
replicateM :: (Monad m, SVECTOR ty a) => Int -> m a -> m (Vector ty a) Source #
O(n) Execute the monadic action the given number of times and store the results in a vector.
generateM :: (Monad m, SVECTOR ty a) => Int -> (Int -> m a) -> m (Vector ty a) Source #
O(n) Construct a vector of the given length by applying the monadic action to each index
create :: SVECTOR ty a => (forall r. ST r (MVector r ty a)) -> Vector ty a Source #
Execute the monadic action and freeze the resulting vector.
create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a
,b
>
Unfolding
unfoldr :: SVECTOR ty a => (b -> Maybe (a, b)) -> b -> Vector ty a Source #
O(n) Construct a Vector ty by repeatedly applying the generator function
to a seed. The generator function yields Just
the next element and the
new seed or Nothing
if there are no more elements.
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 = <10,9,8,7,6,5,4,3,2,1>
unfoldrN :: SVECTOR ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector ty a Source #
O(n) Construct a vector with at most n
by repeatedly applying the
generator function to the a seed. The generator function yields Just
the
next element and the new seed or Nothing
if there are no more elements.
unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8>
constructN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a Source #
O(n) Construct a vector with n
elements by repeatedly applying the
generator function to the already constructed part of the vector.
constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in f <a,b,c>
constructrN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a Source #
O(n) Construct a vector with n
elements from right to left by
repeatedly applying the generator function to the already constructed part
of the vector.
constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in f <c,b,a>
Enumeration
enumFromN :: (SVECTOR ty a, Num a) => a -> Int -> Vector ty a Source #
O(n) Yield a vector of the given length containing the values x
, x+1
etc. This operation is usually more efficient than enumFromTo
.
enumFromN 5 3 = <5,6,7>
enumFromStepN :: (SVECTOR ty a, Num a) => a -> a -> Int -> Vector ty a Source #
O(n) Yield a vector of the given length containing the values x
, x+y
,
x+y+y
etc. This operations is usually more efficient than enumFromThenTo
.
enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4>
enumFromTo :: (SVECTOR ty a, Enum a) => a -> a -> Vector ty a Source #
O(n) Enumerate values from x
to y
.
WARNING: This operation can be very inefficient. If at all possible, use
enumFromN
instead.
enumFromThenTo :: (SVECTOR ty a, Enum a) => a -> a -> a -> Vector ty a Source #
O(n) Enumerate values from x
to y
with a specific step z
.
WARNING: This operation can be very inefficient. If at all possible, use
enumFromStepN
instead.
Concatenation
(++) :: SVECTOR ty a => Vector ty a -> Vector ty a -> Vector ty a infixr 5 Source #
O(m+n) Concatenate two vectors
concat :: SVECTOR ty a => [Vector ty a] -> Vector ty a Source #
O(n) Concatenate all vectors in the list
Restricting memory usage
force :: SVECTOR ty a => Vector ty a -> Vector ty a Source #
O(n) Yield the argument but force it not to retain any extra memory, possibly by copying it.
This is especially useful when dealing with slices. For example:
force (slice 0 2 <huge vector>)
Here, the slice retains a reference to the huge vector. Forcing it creates a copy of just the elements that belong to the slice and allows the huge vector to be garbage collected.
Modifying vectors
Bulk updates
:: SVECTOR ty a | |
=> Vector ty a | initial vector (of length |
-> [(Int, a)] | list of index/value pairs (of length |
-> Vector ty a |
O(m+n) For each pair (i,a)
from the list, replace the vector
element at position i
by a
.
<5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>
unsafeUpd :: SVECTOR ty a => Vector ty a -> [(Int, a)] -> Vector ty a Source #
Same as (//
) but without bounds checking.
Accumulations
:: SVECTOR ty a | |
=> (a -> b -> a) | accumulating function |
-> Vector ty a | initial vector (of length |
-> [(Int, b)] | list of index/value pairs (of length |
-> Vector ty a |
O(m+n) For each pair (i,b)
from the list, replace the vector element
a
at position i
by f a b
.
accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
unsafeAccum :: SVECTOR ty a => (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a Source #
Same as accum
but without bounds checking.
Permutations
Safe destructive updates
Elementwise operations
Mapping
map :: (SVECTOR ty a, SVECTOR ty b) => (a -> b) -> Vector ty a -> Vector ty b Source #
O(n) Map a function over a vector
imap :: (SVECTOR ty a, SVECTOR ty b) => (Int -> a -> b) -> Vector ty a -> Vector ty b Source #
O(n) Apply a function to every element of a Vector ty and its index
concatMap :: (SVECTOR tya a, SVECTOR tyb b) => (a -> Vector tyb b) -> Vector tya a -> Vector tyb b Source #
Map a function over a Vector ty and concatenate the results.
Monadic mapping
mapM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => (a -> m b) -> Vector ty a -> m (Vector ty b) Source #
O(n) Apply the monadic action to all elements of the vector, yielding a vector of results
mapM_ :: (Monad m, SVECTOR ty a) => (a -> m b) -> Vector ty a -> m () Source #
O(n) Apply the monadic action to all elements of a Vector ty and ignore the results
forM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => Vector ty a -> (a -> m b) -> m (Vector ty b) Source #
O(n) Apply the monadic action to all elements of the vector, yielding a
vector of results. Equvalent to flip
.mapM
forM_ :: (Monad m, SVECTOR ty a) => Vector ty a -> (a -> m b) -> m () Source #
O(n) Apply the monadic action to all elements of a Vector ty and ignore the
results. Equivalent to flip
.mapM_
Zipping
zipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c Source #
O(min(m,n)) Zip two vectors with the given function.
zipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d Source #
Zip three vectors with the given function.
zipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e Source #
zipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f Source #
zipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g Source #
izipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (Int -> a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c Source #
O(min(m,n)) Zip two vectors with a function that also takes the elements' indices.
izipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (Int -> a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d Source #
Zip three vectors and their indices with the given function.
izipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (Int -> a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e Source #
izipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f Source #
izipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g Source #
Monadic zipping
zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c, ElemRep V tya ~ a, ElemRep V tyb ~ b, ElemRep V tyc ~ c) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m (Vector tyc c) Source #
O(min(m,n)) Zip the two vectors with the monadic action and yield a vector of results
zipWithM_ :: (Monad m, SVECTOR tya a, SVECTOR tyb b) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m () Source #
O(min(m,n)) Zip the two vectors with the monadic action and ignore the results
Working with predicates
Filtering
filter :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #
O(n) Drop elements that do not satisfy the predicate
ifilter :: SVECTOR ty a => (Int -> a -> Bool) -> Vector ty a -> Vector ty a Source #
O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices
filterM :: (Monad m, SVECTOR ty a) => (a -> m Bool) -> Vector ty a -> m (Vector ty a) Source #
O(n) Drop elements that do not satisfy the monadic predicate
takeWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #
O(n) Yield the longest prefix of elements satisfying the predicate with copying.
dropWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #
O(n) Drop the longest prefix of elements that satisfy the predicate with copying.
Partitioning
partition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #
O(n) Split the vector in two parts, the first one containing those
elements that satisfy the predicate and the second one those that don't. The
relative order of the elements is preserved at the cost of a sometimes
reduced performance compared to unstablePartition
.
unstablePartition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #
O(n) Split the vector in two parts, the first one containing those
elements that satisfy the predicate and the second one those that don't.
The order of the elements is not preserved but the operation is often
faster than partition
.
span :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #
O(n) Split the vector into the longest prefix of elements that satisfy the predicate and the rest with copying.
break :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #
O(n) Split the vector into the longest prefix of elements that do not satisfy the predicate and the rest with copying.
Searching
elem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool infix 4 Source #
O(n) Check if the vector contains an element
notElem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool infix 4 Source #
O(n) Check if the vector does not contain an element (inverse of elem
)
find :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe a Source #
O(n) Yield Just
the first element matching the predicate or Nothing
if no such element exists.
findIndex :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe Int Source #
O(n) Yield Just
the index of the first element matching the predicate
or Nothing
if no such element exists.
elemIndex :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Maybe Int Source #
O(n) Yield Just
the index of the first occurence of the given element or
Nothing
if the vector does not contain the element. This is a specialised
version of findIndex
.
Folding
foldl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #
O(n) Left fold on non-empty vectors
foldl' :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a Source #
O(n) Left fold with strict accumulator
foldl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #
O(n) Left fold on non-empty vectors with strict accumulator
foldr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #
O(n) Right fold on non-empty vectors
foldr' :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b Source #
O(n) Right fold with a strict accumulator
foldr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #
O(n) Right fold on non-empty vectors with strict accumulator
ifoldl :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a Source #
O(n) Left fold (function applied to each element and its index)
ifoldl' :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a Source #
O(n) Left fold with strict accumulator (function applied to each element and its index)
ifoldr :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b Source #
O(n) Right fold (function applied to each element and its index)
ifoldr' :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b Source #
O(n) Right fold with strict accumulator (function applied to each element and its index)
Specialised folds
all :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool Source #
O(n) Check if all elements satisfy the predicate.
any :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool Source #
O(n) Check if any element satisfies the predicate.
product :: (SVECTOR ty a, Num a) => Vector ty a -> a Source #
O(n) Compute the produce of the elements
maximum :: (SVECTOR ty a, Ord a) => Vector ty a -> a Source #
O(n) Yield the maximum element of the vector. The vector may not be empty.
maximumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a Source #
O(n) Yield the maximum element of the Vector ty according to the given comparison function. The vector may not be empty.
minimum :: (SVECTOR ty a, Ord a) => Vector ty a -> a Source #
O(n) Yield the minimum element of the vector. The vector may not be empty.
minimumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a Source #
O(n) Yield the minimum element of the Vector ty according to the given comparison function. The vector may not be empty.
minIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int Source #
O(n) Yield the index of the minimum element of the vector. The vector may not be empty.
minIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int Source #
O(n) Yield the index of the minimum element of the Vector ty according to the given comparison function. The vector may not be empty.
maxIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int Source #
O(n) Yield the index of the maximum element of the vector. The vector may not be empty.
maxIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int Source #
O(n) Yield the index of the maximum element of the Vector ty according to the given comparison function. The vector may not be empty.
Monadic folds
foldM :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a Source #
O(n) Monadic fold
foldM' :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a Source #
O(n) Monadic fold with strict accumulator
fold1M :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a Source #
O(n) Monadic fold over non-empty vectors
fold1M' :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a Source #
O(n) Monadic fold over non-empty vectors with strict accumulator
foldM_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m () Source #
O(n) Monadic fold that discards the result
foldM'_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m () Source #
O(n) Monadic fold with strict accumulator that discards the result
fold1M_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m () Source #
O(n) Monadic fold over non-empty vectors that discards the result
fold1M'_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m () Source #
O(n) Monadic fold over non-empty vectors with strict accumulator that discards the result
Prefix sums (scans)
prescanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
prescanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
O(n) Prescan with strict accumulator
postscanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
postscanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
O(n) Scan with strict accumulator
scanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
O(n) Haskell-style scan
scanl f z <x1,...,xn> = <y1,...,y(n+1)> where y1 = z yi = f y(i-1) x(i-1)
Example: scanl (+) 0 <1,2,3,4> = <0,1,3,6,10>
scanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #
O(n) Haskell-style scan with strict accumulator
scanl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #
O(n) Scan over a non-empty vector
scanl f <x1,...,xn> = <y1,...,yn> where y1 = x1 yi = f y(i-1) xi
scanl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #
O(n) Scan over a non-empty vector with a strict accumulator
prescanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
prescanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
O(n) Right-to-left prescan with strict accumulator
postscanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
O(n) Right-to-left scan
postscanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
O(n) Right-to-left scan with strict accumulator
scanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
O(n) Right-to-left Haskell-style scan
scanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #
O(n) Right-to-left Haskell-style scan with strict accumulator
scanr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #
O(n) Right-to-left scan over a non-empty vector
scanr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #
O(n) Right-to-left scan over a non-empty vector with a strict accumulator
Conversions
Lists
Mutable vectors
freeze :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a) Source #
O(n) Yield an immutable copy of the mutable vector.
thaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a) Source #
O(n) Yield a mutable copy of the immutable vector.
copy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m () Source #
O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length.
unsafeFreeze :: (VECTOR (Region m) ty a, MonadR m, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a) Source #
O(1) Unsafe convert a mutable vector to an immutable one with copying. The mutable vector may not be used after this operation.
unsafeThaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a) Source #
O(1) Unsafely convert an immutable vector to a mutable one with copying. The immutable vector may not be used after this operation.
unsafeCopy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m () Source #
O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length. This is not checked.
SEXP specific helpers.
toByteString :: Vector 'Char Word8 -> ByteString Source #
O(n) Convert a character vector into a strict ByteString
.
unsafeWithByteString :: NFData a => Vector 'Char Word8 -> (ByteString -> IO a) -> a Source #
This function is unsafe and ByteString should not be used outside of the function. Any change to bytestring will be reflected in the source vector, thus breaking referencial transparancy.