Copyright | (C) 2013 Amgen Inc. |
---|---|
Safe Haskell | None |
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).
- data Vector s (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, ElemRep s ty ~ a)
- fromSEXP :: VECTOR s ty a => SEXP s ty -> Vector s ty a
- unsafeFromSEXP :: VECTOR s ty a => SEXP s ty -> Vector s ty a
- toSEXP :: VECTOR s ty a => Vector s ty a -> SEXP s ty
- unsafeToSEXP :: VECTOR s ty a => Vector s ty a -> SEXP s ty
- length :: VECTOR s ty a => Vector s ty a -> Int
- null :: VECTOR s ty a => Vector s ty a -> Bool
- (!) :: VECTOR s ty a => Vector s ty a -> Int -> a
- (!?) :: VECTOR s ty a => Vector s ty a -> Int -> Maybe a
- head :: VECTOR s ty a => Vector s ty a -> a
- last :: VECTOR s ty a => Vector s ty a -> a
- unsafeIndex :: VECTOR s ty a => Vector s ty a -> Int -> a
- unsafeHead :: VECTOR s ty a => Vector s ty a -> a
- unsafeLast :: VECTOR s ty a => Vector s ty a -> a
- indexM :: (VECTOR s ty a, Monad m) => Vector s ty a -> Int -> m a
- headM :: (VECTOR s ty a, Monad m) => Vector s ty a -> m a
- lastM :: (VECTOR s ty a, Monad m) => Vector s ty a -> m a
- unsafeIndexM :: (VECTOR s ty a, Monad m) => Vector s ty a -> Int -> m a
- unsafeHeadM :: (VECTOR s ty a, Monad m) => Vector s ty a -> m a
- unsafeLastM :: (VECTOR s ty a, Monad m) => Vector s ty a -> m a
- slice :: VECTOR s ty a => Int -> Int -> Vector s ty a -> Vector s ty a
- init :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- take :: VECTOR s ty a => Int -> Vector s ty a -> Vector s ty a
- drop :: VECTOR s ty a => Int -> Vector s ty a -> Vector s ty a
- tail :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- splitAt :: VECTOR s ty a => Int -> Vector s ty a -> (Vector s ty a, Vector s ty a)
- unsafeTail :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- unsafeSlice :: VECTOR s ty a => Int -> Int -> Vector s ty a -> Vector s ty a
- unsafeDrop :: VECTOR s ty a => Int -> Vector s ty a -> Vector s ty a
- unsafeTake :: VECTOR s ty a => Int -> Vector s ty a -> Vector s ty a
- unsafeInit :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- empty :: VECTOR s ty a => Vector s ty a
- singleton :: VECTOR s ty a => a -> Vector s ty a
- replicate :: VECTOR s ty a => Int -> a -> Vector s ty a
- generate :: VECTOR s ty a => Int -> (Int -> a) -> Vector s ty a
- iterateN :: VECTOR s ty a => Int -> (a -> a) -> a -> Vector s ty a
- replicateM :: (Monad m, VECTOR s ty a) => Int -> m a -> m (Vector s ty a)
- generateM :: (Monad m, VECTOR s ty a) => Int -> (Int -> m a) -> m (Vector s ty a)
- create :: VECTOR s ty a => (forall r. ST r (MVector r ty a)) -> Vector s ty a
- unfoldr :: VECTOR s ty a => (b -> Maybe (a, b)) -> b -> Vector s ty a
- unfoldrN :: VECTOR s ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector s ty a
- constructN :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s ty a
- constructrN :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s ty a
- enumFromN :: (VECTOR s ty a, Num a) => a -> Int -> Vector s ty a
- enumFromStepN :: (VECTOR s ty a, Num a) => a -> a -> Int -> Vector s ty a
- enumFromTo :: (VECTOR s ty a, Enum a) => a -> a -> Vector s ty a
- enumFromThenTo :: (VECTOR s ty a, Enum a) => a -> a -> a -> Vector s ty a
- cons :: VECTOR s ty a => a -> Vector s ty a -> Vector s ty a
- snoc :: VECTOR s ty a => Vector s ty a -> a -> Vector s ty a
- (++) :: VECTOR s ty a => Vector s ty a -> Vector s ty a -> Vector s ty a
- concat :: VECTOR s ty a => [Vector s ty a] -> Vector s ty a
- force :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- (//) :: VECTOR s ty a => Vector s ty a -> [(Int, a)] -> Vector s ty a
- unsafeUpd :: VECTOR s ty a => Vector s ty a -> [(Int, a)] -> Vector s ty a
- accum :: VECTOR s ty a => (a -> b -> a) -> Vector s ty a -> [(Int, b)] -> Vector s ty a
- unsafeAccum :: VECTOR s ty a => (a -> b -> a) -> Vector s ty a -> [(Int, b)] -> Vector s ty a
- reverse :: VECTOR s ty a => Vector s ty a -> Vector s ty a
- map :: (VECTOR s ty a, VECTOR s ty b) => (a -> b) -> Vector s ty a -> Vector s ty b
- imap :: (VECTOR s ty a, VECTOR s ty b) => (Int -> a -> b) -> Vector s ty a -> Vector s ty b
- concatMap :: (VECTOR s tya a, VECTOR s tyb b) => (a -> Vector s tyb b) -> Vector s tya a -> Vector s tyb b
- mapM :: (Monad m, VECTOR s ty a, VECTOR s ty b) => (a -> m b) -> Vector s ty a -> m (Vector s ty b)
- mapM_ :: (Monad m, VECTOR s ty a) => (a -> m b) -> Vector s ty a -> m ()
- forM :: (Monad m, VECTOR s ty a, VECTOR s ty b) => Vector s ty a -> (a -> m b) -> m (Vector s ty b)
- forM_ :: (Monad m, VECTOR s ty a) => Vector s ty a -> (a -> m b) -> m ()
- zipWith :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c) => (a -> b -> c) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c
- zipWith3 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d) => (a -> b -> c -> d) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d
- zipWith4 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e) => (a -> b -> c -> d -> e) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e
- zipWith5 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f) => (a -> b -> c -> d -> e -> f) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f
- zipWith6 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f, VECTOR s tyg g) => (a -> b -> c -> d -> e -> f -> g) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f -> Vector s tyg g
- izipWith :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c) => (Int -> a -> b -> c) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c
- izipWith3 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d) => (Int -> a -> b -> c -> d) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d
- izipWith4 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e) => (Int -> a -> b -> c -> d -> e) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e
- izipWith5 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f
- izipWith6 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f, VECTOR s tyg g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f -> Vector s tyg g
- zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c) => (a -> b -> m c) -> Vector (Region m) tya a -> Vector (Region m) tyb b -> m (Vector (Region m) tyc c)
- zipWithM_ :: (Monad m, VECTOR s tya a, VECTOR s tyb b) => (a -> b -> m c) -> Vector s tya a -> Vector s tyb b -> m ()
- filter :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a
- ifilter :: VECTOR s ty a => (Int -> a -> Bool) -> Vector s ty a -> Vector s ty a
- filterM :: (Monad m, VECTOR s ty a) => (a -> m Bool) -> Vector s ty a -> m (Vector s ty a)
- takeWhile :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a
- dropWhile :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a
- partition :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a)
- unstablePartition :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a)
- span :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a)
- break :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a)
- elem :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Bool
- notElem :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Bool
- find :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Maybe a
- findIndex :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Maybe Int
- elemIndex :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Maybe Int
- foldl :: VECTOR s ty b => (a -> b -> a) -> a -> Vector s ty b -> a
- foldl1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a
- foldl' :: VECTOR s ty b => (a -> b -> a) -> a -> Vector s ty b -> a
- foldl1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a
- foldr :: VECTOR s ty a => (a -> b -> b) -> b -> Vector s ty a -> b
- foldr1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a
- foldr' :: VECTOR s ty a => (a -> b -> b) -> b -> Vector s ty a -> b
- foldr1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a
- ifoldl :: VECTOR s ty b => (a -> Int -> b -> a) -> a -> Vector s ty b -> a
- ifoldl' :: VECTOR s ty b => (a -> Int -> b -> a) -> a -> Vector s ty b -> a
- ifoldr :: VECTOR s ty a => (Int -> a -> b -> b) -> b -> Vector s ty a -> b
- ifoldr' :: VECTOR s ty a => (Int -> a -> b -> b) -> b -> Vector s ty a -> b
- all :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Bool
- any :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Bool
- sum :: (VECTOR s ty a, Num a) => Vector s ty a -> a
- product :: (VECTOR s ty a, Num a) => Vector s ty a -> a
- maximum :: (VECTOR s ty a, Ord a) => Vector s ty a -> a
- maximumBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> a
- minimum :: (VECTOR s ty a, Ord a) => Vector s ty a -> a
- minimumBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> a
- minIndex :: (VECTOR s ty a, Ord a) => Vector s ty a -> Int
- minIndexBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> Int
- maxIndex :: (VECTOR s ty a, Ord a) => Vector s ty a -> Int
- maxIndexBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> Int
- foldM :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m a
- foldM' :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m a
- fold1M :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m a
- fold1M' :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m a
- foldM_ :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m ()
- foldM'_ :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m ()
- fold1M_ :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m ()
- fold1M'_ :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m ()
- prescanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- prescanl' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- postscanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- postscanl' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- scanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- scanl' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a
- scanl1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a
- scanl1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a
- prescanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- prescanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- postscanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- postscanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- scanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- scanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b
- scanr1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a
- scanr1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a
- toList :: VECTOR s ty a => Vector s ty a -> [a]
- fromList :: forall s ty a. VECTOR s ty a => [a] -> Vector s ty a
- fromListN :: forall s ty a. VECTOR s ty a => Int -> [a] -> Vector s ty a
- freeze :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> m (Vector (Region m) ty a)
- thaw :: (MonadR m, VECTOR (Region m) ty a) => Vector (Region m) ty a -> m (MVector (Region m) ty a)
- copy :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> Vector (Region m) ty a -> m ()
- unsafeFreeze :: (VECTOR (Region m) ty a, MonadR m) => MVector (Region m) ty a -> m (Vector (Region m) ty a)
- unsafeThaw :: (MonadR m, VECTOR (Region m) ty a) => Vector (Region m) ty a -> m (MVector (Region m) ty a)
- unsafeCopy :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> Vector (Region m) ty a -> m ()
- toString :: Vector s Char Word8 -> String
- toByteString :: Vector s Char Word8 -> ByteString
Documentation
data Vector s (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, ElemRep s ty ~ a) Source #
Constraint synonym for all operations on vectors.
unsafeFromSEXP :: VECTOR s ty a => SEXP s ty -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> SEXP s ty Source #
O(n) Yield a (mutable) copy of the vector as a SEXP
.
unsafeToSEXP :: VECTOR s ty a => Vector s 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 :: VECTOR s ty a => Vector s ty a -> Int -> a Source #
O(1) Unsafe indexing without bounds checking
unsafeHead :: VECTOR s ty a => Vector s ty a -> a Source #
O(1) First element without checking if the vector is empty
unsafeLast :: VECTOR s ty a => Vector s ty a -> a Source #
O(1) Last element without checking if the vector is empty
Monadic indexing
indexM :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s ty a Source #
O(N) Yield all but the last element, this operation will copy an array. The vector may not be empty.
take :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s ty a Source #
O(N) Copy all but the first element. The vector may not be empty.
unsafeTail :: VECTOR s ty a => Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> a -> Vector s ty a Source #
O(n) Vector of the given length with the same value in each position
generate :: VECTOR s ty a => Int -> (Int -> a) -> Vector s ty a Source #
O(n) Construct a vector of the given length by applying the function to each index
iterateN :: VECTOR s ty a => Int -> (a -> a) -> a -> Vector s ty a Source #
O(n) Apply function n times to value. Zeroth element is original value.
Monadic initialisation
replicateM :: (Monad m, VECTOR s ty a) => Int -> m a -> m (Vector s ty a) Source #
O(n) Execute the monadic action the given number of times and store the results in a vector.
generateM :: (Monad m, VECTOR s ty a) => Int -> (Int -> m a) -> m (Vector s ty a) Source #
O(n) Construct a vector of the given length by applying the monadic action to each index
create :: VECTOR s ty a => (forall r. ST r (MVector r ty a)) -> Vector s 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 :: VECTOR s ty a => (b -> Maybe (a, b)) -> b -> Vector s ty a Source #
O(n) Construct a Vector s 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 :: VECTOR s ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector s 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 :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s 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 :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s 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 :: (VECTOR s ty a, Num a) => a -> Int -> Vector s 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 :: (VECTOR s ty a, Num a) => a -> a -> Int -> Vector s 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 :: (VECTOR s ty a, Enum a) => a -> a -> Vector s 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 :: (VECTOR s ty a, Enum a) => a -> a -> a -> Vector s 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
(++) :: VECTOR s ty a => Vector s ty a -> Vector s ty a -> Vector s ty a infixr 5 Source #
O(m+n) Concatenate two vectors
concat :: VECTOR s ty a => [Vector s ty a] -> Vector s ty a Source #
O(n) Concatenate all vectors in the list
Restricting memory usage
force :: VECTOR s ty a => Vector s ty a -> Vector s 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
:: VECTOR s ty a | |
=> Vector s ty a | initial vector (of length |
-> [(Int, a)] | list of index/value pairs (of length |
-> Vector s 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 :: VECTOR s ty a => Vector s ty a -> [(Int, a)] -> Vector s ty a Source #
Same as (//
) but without bounds checking.
Accumulations
:: VECTOR s ty a | |
=> (a -> b -> a) | accumulating function |
-> Vector s ty a | initial vector (of length |
-> [(Int, b)] | list of index/value pairs (of length |
-> Vector s 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 :: VECTOR s ty a => (a -> b -> a) -> Vector s ty a -> [(Int, b)] -> Vector s ty a Source #
Same as accum
but without bounds checking.
Permutations
Safe destructive updates
Elementwise operations
Mapping
map :: (VECTOR s ty a, VECTOR s ty b) => (a -> b) -> Vector s ty a -> Vector s ty b Source #
O(n) Map a function over a vector
imap :: (VECTOR s ty a, VECTOR s ty b) => (Int -> a -> b) -> Vector s ty a -> Vector s ty b Source #
O(n) Apply a function to every element of a Vector s ty and its index
concatMap :: (VECTOR s tya a, VECTOR s tyb b) => (a -> Vector s tyb b) -> Vector s tya a -> Vector s tyb b Source #
Map a function over a Vector s ty and concatenate the results.
Monadic mapping
mapM :: (Monad m, VECTOR s ty a, VECTOR s ty b) => (a -> m b) -> Vector s ty a -> m (Vector s ty b) Source #
O(n) Apply the monadic action to all elements of the vector, yielding a vector of results
mapM_ :: (Monad m, VECTOR s ty a) => (a -> m b) -> Vector s ty a -> m () Source #
O(n) Apply the monadic action to all elements of a Vector s ty and ignore the results
forM :: (Monad m, VECTOR s ty a, VECTOR s ty b) => Vector s ty a -> (a -> m b) -> m (Vector s 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, VECTOR s ty a) => Vector s ty a -> (a -> m b) -> m () Source #
O(n) Apply the monadic action to all elements of a Vector s ty and ignore the
results. Equivalent to flip
.mapM_
Zipping
zipWith :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c) => (a -> b -> c) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c Source #
O(min(m,n)) Zip two vectors with the given function.
zipWith3 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d) => (a -> b -> c -> d) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d Source #
Zip three vectors with the given function.
zipWith4 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e) => (a -> b -> c -> d -> e) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e Source #
zipWith5 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f) => (a -> b -> c -> d -> e -> f) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f Source #
zipWith6 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f, VECTOR s tyg g) => (a -> b -> c -> d -> e -> f -> g) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f -> Vector s tyg g Source #
izipWith :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c) => (Int -> a -> b -> c) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c Source #
O(min(m,n)) Zip two vectors with a function that also takes the elements' indices.
izipWith3 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d) => (Int -> a -> b -> c -> d) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d Source #
Zip three vectors and their indices with the given function.
izipWith4 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e) => (Int -> a -> b -> c -> d -> e) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e Source #
izipWith5 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f Source #
izipWith6 :: (VECTOR s tya a, VECTOR s tyb b, VECTOR s tyc c, VECTOR s tyd d, VECTOR s tye e, VECTOR s tyf f, VECTOR s tyg g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector s tya a -> Vector s tyb b -> Vector s tyc c -> Vector s tyd d -> Vector s tye e -> Vector s tyf f -> Vector s tyg g Source #
Monadic zipping
zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c) => (a -> b -> m c) -> Vector (Region m) tya a -> Vector (Region m) tyb b -> m (Vector (Region m) tyc c) Source #
O(min(m,n)) Zip the two vectors with the monadic action and yield a vector of results
zipWithM_ :: (Monad m, VECTOR s tya a, VECTOR s tyb b) => (a -> b -> m c) -> Vector s tya a -> Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a Source #
O(n) Drop elements that do not satisfy the predicate
ifilter :: VECTOR s ty a => (Int -> a -> Bool) -> Vector s ty a -> Vector s ty a Source #
O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices
filterM :: (Monad m, VECTOR s ty a) => (a -> m Bool) -> Vector s ty a -> m (Vector s ty a) Source #
O(n) Drop elements that do not satisfy the monadic predicate
takeWhile :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a Source #
O(n) Yield the longest prefix of elements satisfying the predicate with copying.
dropWhile :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a Source #
O(n) Drop the longest prefix of elements that satisfy the predicate with copying.
Partitioning
partition :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a) Source #
O(n) Split the vector into the longest prefix of elements that satisfy the predicate and the rest with copying.
break :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Bool infix 4 Source #
O(n) Check if the vector contains an element
notElem :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Bool infix 4 Source #
O(n) Check if the vector does not contain an element (inverse of elem
)
find :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Maybe a Source #
O(n) Yield Just
the first element matching the predicate or Nothing
if no such element exists.
findIndex :: VECTOR s ty a => (a -> Bool) -> Vector s 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 :: (VECTOR s ty a, Eq a) => a -> Vector s 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 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a Source #
O(n) Left fold on non-empty vectors
foldl' :: VECTOR s ty b => (a -> b -> a) -> a -> Vector s ty b -> a Source #
O(n) Left fold with strict accumulator
foldl1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a Source #
O(n) Left fold on non-empty vectors with strict accumulator
foldr1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a Source #
O(n) Right fold on non-empty vectors
foldr' :: VECTOR s ty a => (a -> b -> b) -> b -> Vector s ty a -> b Source #
O(n) Right fold with a strict accumulator
foldr1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> a Source #
O(n) Right fold on non-empty vectors with strict accumulator
ifoldl :: VECTOR s ty b => (a -> Int -> b -> a) -> a -> Vector s ty b -> a Source #
O(n) Left fold (function applied to each element and its index)
ifoldl' :: VECTOR s ty b => (a -> Int -> b -> a) -> a -> Vector s ty b -> a Source #
O(n) Left fold with strict accumulator (function applied to each element and its index)
ifoldr :: VECTOR s ty a => (Int -> a -> b -> b) -> b -> Vector s ty a -> b Source #
O(n) Right fold (function applied to each element and its index)
ifoldr' :: VECTOR s ty a => (Int -> a -> b -> b) -> b -> Vector s ty a -> b Source #
O(n) Right fold with strict accumulator (function applied to each element and its index)
Specialised folds
all :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Bool Source #
O(n) Check if all elements satisfy the predicate.
any :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Bool Source #
O(n) Check if any element satisfies the predicate.
product :: (VECTOR s ty a, Num a) => Vector s ty a -> a Source #
O(n) Compute the produce of the elements
maximum :: (VECTOR s ty a, Ord a) => Vector s ty a -> a Source #
O(n) Yield the maximum element of the vector. The vector may not be empty.
maximumBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> a Source #
O(n) Yield the maximum element of the Vector s ty according to the given comparison function. The vector may not be empty.
minimum :: (VECTOR s ty a, Ord a) => Vector s ty a -> a Source #
O(n) Yield the minimum element of the vector. The vector may not be empty.
minimumBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> a Source #
O(n) Yield the minimum element of the Vector s ty according to the given comparison function. The vector may not be empty.
minIndex :: (VECTOR s ty a, Ord a) => Vector s ty a -> Int Source #
O(n) Yield the index of the minimum element of the vector. The vector may not be empty.
minIndexBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> Int Source #
O(n) Yield the index of the minimum element of the Vector s ty according to the given comparison function. The vector may not be empty.
maxIndex :: (VECTOR s ty a, Ord a) => Vector s ty a -> Int Source #
O(n) Yield the index of the maximum element of the vector. The vector may not be empty.
maxIndexBy :: VECTOR s ty a => (a -> a -> Ordering) -> Vector s ty a -> Int Source #
O(n) Yield the index of the maximum element of the Vector s ty according to the given comparison function. The vector may not be empty.
Monadic folds
foldM :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m a Source #
O(n) Monadic fold
foldM' :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m a Source #
O(n) Monadic fold with strict accumulator
fold1M :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m a Source #
O(n) Monadic fold over non-empty vectors
fold1M' :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m a Source #
O(n) Monadic fold over non-empty vectors with strict accumulator
foldM_ :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m () Source #
O(n) Monadic fold that discards the result
foldM'_ :: (Monad m, VECTOR s ty b) => (a -> b -> m a) -> a -> Vector s ty b -> m () Source #
O(n) Monadic fold with strict accumulator that discards the result
fold1M_ :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m () Source #
O(n) Monadic fold over non-empty vectors that discards the result
fold1M'_ :: (Monad m, VECTOR s ty a) => (a -> a -> m a) -> Vector s ty a -> m () Source #
O(n) Monadic fold over non-empty vectors with strict accumulator that discards the result
Prefix sums (scans)
prescanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #
prescanl' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #
O(n) Prescan with strict accumulator
postscanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #
postscanl' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #
O(n) Scan with strict accumulator
scanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s 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' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #
O(n) Haskell-style scan with strict accumulator
scanl1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s 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' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a Source #
O(n) Scan over a non-empty vector with a strict accumulator
prescanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
prescanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
O(n) Right-to-left prescan with strict accumulator
postscanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
O(n) Right-to-left scan
postscanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
O(n) Right-to-left scan with strict accumulator
scanr :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
O(n) Right-to-left Haskell-style scan
scanr' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> b) -> b -> Vector s ty a -> Vector s ty b Source #
O(n) Right-to-left Haskell-style scan with strict accumulator
scanr1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a Source #
O(n) Right-to-left scan over a non-empty vector
scanr1' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a Source #
O(n) Right-to-left scan over a non-empty vector with a strict accumulator
Conversions
Lists
fromList :: forall s ty a. VECTOR s ty a => [a] -> Vector s ty a Source #
O(n) Convert a list to a vector
Mutable vectors
freeze :: (MonadR m, VECTOR (Region m) ty a) => MVector (Region m) ty a -> m (Vector (Region m) ty a) Source #
O(n) Yield an immutable copy of the mutable vector.
thaw :: (MonadR m, VECTOR (Region m) ty a) => Vector (Region m) 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) => MVector (Region m) ty a -> Vector (Region m) 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) => MVector (Region m) ty a -> m (Vector (Region m) 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) => Vector (Region m) 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) => MVector (Region m) ty a -> Vector (Region m) 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 s Char Word8 -> ByteString Source #
O(n) Convert a character vector into a strict ByteString
.