Portability | non-portable |
---|---|
Stability | experimental |
Maintainer | Roman Leshchinskiy <rl@cse.unsw.edu.au> |
Storable
-based vectors.
- data Vector a
- data MVector s a = MVector !Int !Int !(ForeignPtr a)
- class Storable a
- length :: Storable a => Vector a -> Int
- null :: Storable a => Vector a -> Bool
- empty :: Storable a => Vector a
- singleton :: Storable a => a -> Vector a
- cons :: Storable a => a -> Vector a -> Vector a
- snoc :: Storable a => Vector a -> a -> Vector a
- replicate :: Storable a => Int -> a -> Vector a
- generate :: Storable a => Int -> (Int -> a) -> Vector a
- (++) :: Storable a => Vector a -> Vector a -> Vector a
- copy :: Storable a => Vector a -> Vector a
- (!) :: Storable a => Vector a -> Int -> a
- head :: Storable a => Vector a -> a
- last :: Storable a => Vector a -> a
- indexM :: (Storable a, Monad m) => Vector a -> Int -> m a
- headM :: (Storable a, Monad m) => Vector a -> m a
- lastM :: (Storable a, Monad m) => Vector a -> m a
- unsafeIndex :: Storable a => Vector a -> Int -> a
- unsafeHead :: Storable a => Vector a -> a
- unsafeLast :: Storable a => Vector a -> a
- unsafeIndexM :: (Storable a, Monad m) => Vector a -> Int -> m a
- unsafeHeadM :: (Storable a, Monad m) => Vector a -> m a
- unsafeLastM :: (Storable a, Monad m) => Vector a -> m a
- slice :: Storable a => Int -> Int -> Vector a -> Vector a
- init :: Storable a => Vector a -> Vector a
- tail :: Storable a => Vector a -> Vector a
- take :: Storable a => Int -> Vector a -> Vector a
- drop :: Storable a => Int -> Vector a -> Vector a
- unsafeSlice :: Storable a => Int -> Int -> Vector a -> Vector a
- unsafeInit :: Storable a => Vector a -> Vector a
- unsafeTail :: Storable a => Vector a -> Vector a
- unsafeTake :: Storable a => Int -> Vector a -> Vector a
- unsafeDrop :: Storable a => Int -> Vector a -> Vector a
- accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a
- accumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
- (//) :: Storable a => Vector a -> [(Int, a)] -> Vector a
- update_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a
- backpermute :: Storable a => Vector a -> Vector Int -> Vector a
- reverse :: Storable a => Vector a -> Vector a
- unsafeAccum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a
- unsafeAccumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
- unsafeUpd :: Storable a => Vector a -> [(Int, a)] -> Vector a
- unsafeUpdate_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a
- unsafeBackpermute :: Storable a => Vector a -> Vector Int -> Vector a
- map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b
- imap :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
- concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b
- zipWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c
- zipWith3 :: (Storable a, Storable b, Storable c, Storable d) => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
- zipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e) => (a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
- zipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => (a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f
- zipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => (a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g
- izipWith :: (Storable a, Storable b, Storable c) => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
- izipWith3 :: (Storable a, Storable b, Storable c, Storable d) => (Int -> a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
- izipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e) => (Int -> a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
- izipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f
- izipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g
- filter :: Storable a => (a -> Bool) -> Vector a -> Vector a
- ifilter :: Storable a => (Int -> a -> Bool) -> Vector a -> Vector a
- takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
- dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
- partition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
- unstablePartition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
- span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
- break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
- elem :: (Storable a, Eq a) => a -> Vector a -> Bool
- notElem :: (Storable a, Eq a) => a -> Vector a -> Bool
- find :: Storable a => (a -> Bool) -> Vector a -> Maybe a
- findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int
- findIndices :: Storable a => (a -> Bool) -> Vector a -> Vector Int
- elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe Int
- elemIndices :: (Storable a, Eq a) => a -> Vector a -> Vector Int
- foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> a
- foldl1 :: Storable a => (a -> a -> a) -> Vector a -> a
- foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> a
- foldl1' :: Storable a => (a -> a -> a) -> Vector a -> a
- foldr :: Storable a => (a -> b -> b) -> b -> Vector a -> b
- foldr1 :: Storable a => (a -> a -> a) -> Vector a -> a
- foldr' :: Storable a => (a -> b -> b) -> b -> Vector a -> b
- foldr1' :: Storable a => (a -> a -> a) -> Vector a -> a
- ifoldl :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a
- ifoldl' :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a
- ifoldr :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
- ifoldr' :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
- all :: Storable a => (a -> Bool) -> Vector a -> Bool
- any :: Storable a => (a -> Bool) -> Vector a -> Bool
- and :: Vector Bool -> Bool
- or :: Vector Bool -> Bool
- sum :: (Storable a, Num a) => Vector a -> a
- product :: (Storable a, Num a) => Vector a -> a
- maximum :: (Storable a, Ord a) => Vector a -> a
- maximumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a
- minimum :: (Storable a, Ord a) => Vector a -> a
- minimumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a
- minIndex :: (Storable a, Ord a) => Vector a -> Int
- minIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int
- maxIndex :: (Storable a, Ord a) => Vector a -> Int
- maxIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int
- unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector a
- prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
- scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a
- scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a
- prescanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- prescanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- postscanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- postscanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- scanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
- scanr1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a
- scanr1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a
- enumFromN :: (Storable a, Num a) => a -> Int -> Vector a
- enumFromStepN :: (Storable a, Num a) => a -> a -> Int -> Vector a
- enumFromTo :: (Storable a, Enum a) => a -> a -> Vector a
- enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector a
- toList :: Storable a => Vector a -> [a]
- fromList :: Storable a => [a] -> Vector a
- unsafeFromForeignPtr :: ForeignPtr a -> Int -> Int -> Vector a
- unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int)
- unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b
Documentation
Storable
-based vectors
Mutable Storable
-based vectors
MVector !Int !Int !(ForeignPtr a) |
class Storable a
The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.
Memory addresses are represented as values of type
, for some
Ptr
aa
which is an instance of class Storable
. The type argument to
Ptr
helps provide some valuable type safety in FFI code (you can't
mix pointers of different types without an explicit cast), while
helping the Haskell type system figure out which marshalling method is
needed for a given pointer.
All marshalling between Haskell and a foreign language ultimately
boils down to translating Haskell data structures into the binary
representation of a corresponding data structure of the foreign
language and vice versa. To code this marshalling in Haskell, it is
necessary to manipulate primitive data types stored in unstructured
memory blocks. The class Storable
facilitates this manipulation on
all types for which it is instantiated, which are the standard basic
types of Haskell, the fixed size Int
types (Int8
, Int16
,
Int32
, Int64
), the fixed size Word
types (Word8
, Word16
,
Word32
, Word64
), StablePtr
, all types from Foreign.C.Types,
as well as Ptr
.
Minimal complete definition: sizeOf
, alignment
, one of peek
,
peekElemOff
and peekByteOff
, and one of poke
, pokeElemOff
and
pokeByteOff
.
Length information
Construction
replicate :: Storable a => Int -> a -> Vector aSource
Vector of the given length with the given value in each position
generate :: Storable a => Int -> (Int -> a) -> Vector aSource
Generate a vector of the given length by applying the function to each index
copy :: Storable a => Vector a -> Vector aSource
Create a copy of a vector. Useful when dealing with slices.
Accessing individual elements
indexM :: (Storable a, Monad m) => Vector a -> Int -> m aSource
Monadic indexing which can be strict in the vector while remaining lazy in the element
unsafeIndex :: Storable a => Vector a -> Int -> aSource
Unsafe indexing without bounds checking
unsafeHead :: Storable a => Vector a -> aSource
Yield the first element of a vector without checking if the vector is empty
unsafeLast :: Storable a => Vector a -> aSource
Yield the last element of a vector without checking if the vector is empty
unsafeIndexM :: (Storable a, Monad m) => Vector a -> Int -> m aSource
Unsafe monadic indexing without bounds checks
unsafeHeadM :: (Storable a, Monad m) => Vector a -> m aSource
unsafeLastM :: (Storable a, Monad m) => Vector a -> m aSource
Subvectors
Yield a part of the vector without copying it. Safer version of
basicUnsafeSlice
.
drop :: Storable a => Int -> Vector a -> Vector aSource
Yield all but the first n
elements without copying.
Unsafely yield a part of the vector without copying it and without performing bounds checks.
unsafeInit :: Storable a => Vector a -> Vector aSource
unsafeTail :: Storable a => Vector a -> Vector aSource
Permutations
accumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector aSource
unsafeAccumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector aSource
Mapping
map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector bSource
Map a function over a vector
imap :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector bSource
Apply a function to every index/value pair
Zipping and unzipping
zipWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector cSource
Zip two vectors with the given function.
zipWith3 :: (Storable a, Storable b, Storable c, Storable d) => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector dSource
Zip three vectors with the given function.
zipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e) => (a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector eSource
zipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => (a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector fSource
zipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => (a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector gSource
izipWith :: (Storable a, Storable b, Storable c) => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector cSource
Zip two vectors and their indices with the given function.
izipWith3 :: (Storable a, Storable b, Storable c, Storable d) => (Int -> a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector dSource
Zip three vectors and their indices with the given function.
izipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e) => (Int -> a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector eSource
izipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector fSource
izipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector gSource
Filtering
filter :: Storable a => (a -> Bool) -> Vector a -> Vector aSource
Drop elements which do not satisfy the predicate
ifilter :: Storable a => (Int -> a -> Bool) -> Vector a -> Vector aSource
Drop elements that do not satisfy the predicate (applied to values and their indices)
takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector aSource
Yield the longest prefix of elements satisfying the predicate.
dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector aSource
Drop the longest prefix of elements that satisfy the predicate.
partition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)Source
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 :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)Source
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.
span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)Source
Split the vector into the longest prefix of elements that satisfy the predicate and the rest.
break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)Source
Split the vector into the longest prefix of elements that do not satisfy the predicate and the rest.
Searching
elem :: (Storable a, Eq a) => a -> Vector a -> BoolSource
Check whether the vector contains an element
findIndices :: Storable a => (a -> Bool) -> Vector a -> Vector IntSource
Yield the indices of elements satisfying the predicate
elemIndices :: (Storable a, Eq a) => a -> Vector a -> Vector IntSource
Yield the indices of all occurences of the given element
Folding
foldl1' :: Storable a => (a -> a -> a) -> Vector a -> aSource
Left fold on non-empty vectors with strict accumulator
foldr' :: Storable a => (a -> b -> b) -> b -> Vector a -> bSource
Right fold with a strict accumulator
foldr1' :: Storable a => (a -> a -> a) -> Vector a -> aSource
Right fold on non-empty vectors with strict accumulator
ifoldl :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> aSource
Left fold (function applied to each element and its index)
ifoldl' :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> aSource
Left fold with strict accumulator (function applied to each element and its index)
ifoldr :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> bSource
Right fold (function applied to each element and its index)
ifoldr' :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> bSource
Right fold with strict accumulator (function applied to each element and its index)
Specialised folds
Unfolding
Scans
prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource
Prefix scan with strict accumulator
postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource
Suffix scan
postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource
Suffix scan with strict accumulator
scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource
Haskell-style scan
scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource
Haskell-style scan with strict accumulator
scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector aSource
Scan over a non-empty Vector
with a strict accumulator
prescanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Prefix right-to-left scan
prescanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Prefix right-to-left scan with strict accumulator
postscanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Suffix right-to-left scan
postscanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Suffix right-to-left scan with strict accumulator
scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Haskell-style right-to-left scan
scanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector bSource
Haskell-style right-to-left scan with strict accumulator
scanr1 :: Storable a => (a -> a -> a) -> Vector a -> Vector aSource
Right-to-left scan over a non-empty vector
scanr1' :: Storable a => (a -> a -> a) -> Vector a -> Vector aSource
Right-to-left scan over a non-empty vector with a strict accumulator
Enumeration
enumFromN :: (Storable a, Num a) => a -> Int -> Vector aSource
Yield a vector of the given length containing the values x
, x+1
etc.
This operation is usually more efficient than enumFromTo
.
enumFromStepN :: (Storable a, Num a) => a -> a -> Int -> Vector aSource
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
.
enumFromTo :: (Storable a, Enum a) => a -> a -> Vector aSource
Enumerate values from x
to y
.
WARNING: This operation can be very inefficient. If at all possible, use
enumFromN
instead.
enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector aSource
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.
Conversion to/from lists
Accessing the underlying memory
:: ForeignPtr a | pointer |
-> Int | offset |
-> Int | length |
-> Vector a |
Create a vector from a ForeignPtr
with an offset and a length. The data
may not be modified through the ForeignPtr
afterwards.
unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int)Source
Yield the underlying ForeignPtr
together with the offset to the data
and its length. The data may not be modified through the ForeignPtr
.