vector-0.5: Efficient Arrays

Portabilitynon-portable
Stabilityexperimental
MaintainerRoman Leshchinskiy <rl@cse.unsw.edu.au>

Data.Vector.Storable

Contents

Description

Storable-based vectors.

Synopsis

Documentation

data Vector a Source

Storable-based vectors

Instances

Storable a => Vector Vector a 
(Storable a, Eq a) => Eq (Vector a) 
(Storable a, Ord a) => Ord (Vector a) 
(Show a, Storable a) => Show (Vector a) 

data MVector s a Source

Mutable Storable-based vectors

Constructors

MVector !Int !Int !(ForeignPtr a) 

Instances

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 Ptr a, for some a 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

empty :: Storable a => Vector aSource

Empty vector

singleton :: Storable a => a -> Vector aSource

Vector with exaclty one element

cons :: Storable a => a -> Vector a -> Vector aSource

Prepend an element

snoc :: Storable a => Vector a -> a -> Vector aSource

Append an element

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

(++) :: Storable a => Vector a -> Vector a -> Vector aSource

Concatenate two vectors

copy :: Storable a => Vector a -> Vector aSource

Create a copy of a vector. Useful when dealing with slices.

Accessing individual elements

(!) :: Storable a => Vector a -> Int -> aSource

Indexing

head :: Storable a => Vector a -> aSource

First element

last :: Storable a => Vector a -> aSource

Last element

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

headM :: (Storable a, Monad m) => Vector a -> m aSource

lastM :: (Storable a, Monad m) => Vector a -> m aSource

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

sliceSource

Arguments

:: Storable a 
=> Int

starting index

-> Int

length

-> Vector a 
-> Vector a 

Yield a part of the vector without copying it. Safer version of basicUnsafeSlice.

init :: Storable a => Vector a -> Vector aSource

Yield all but the last element without copying.

tail :: Storable a => Vector a -> Vector aSource

All but the first element (without copying).

take :: Storable a => Int -> Vector a -> Vector aSource

Yield the first n elements without copying.

drop :: Storable a => Int -> Vector a -> Vector aSource

Yield all but the first n elements without copying.

unsafeSliceSource

Arguments

:: Storable a 
=> Int

starting index

-> Int

length

-> Vector a 
-> Vector a 

Unsafely yield a part of the vector without copying it and without performing bounds checks.

Permutations

accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector aSource

accumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector aSource

(//) :: Storable a => Vector a -> [(Int, a)] -> Vector aSource

unsafeAccum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector aSource

unsafeAccumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector aSource

unsafeUpd :: Storable a => Vector a -> [(Int, a)] -> 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

concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector bSource

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

notElem :: (Storable a, Eq a) => a -> Vector a -> BoolSource

Inverse of elem

find :: Storable a => (a -> Bool) -> Vector a -> Maybe aSource

Yield Just the first element matching the predicate or Nothing if no such element exists.

findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe IntSource

Yield Just the index of the first element matching the predicate or Nothing if no such element exists.

findIndices :: Storable a => (a -> Bool) -> Vector a -> Vector IntSource

Yield the indices of elements satisfying the predicate

elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe IntSource

Yield Just the index of the first occurence of the given element or Nothing if the vector does not contain the element

elemIndices :: (Storable a, Eq a) => a -> Vector a -> Vector IntSource

Yield the indices of all occurences of the given element

Folding

foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> aSource

Left fold

foldl1 :: Storable a => (a -> a -> a) -> Vector a -> aSource

Lefgt fold on non-empty vectors

foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> aSource

Left fold with strict accumulator

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

foldr1 :: Storable a => (a -> a -> a) -> Vector a -> aSource

Right fold on non-empty vectors

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

all :: Storable a => (a -> Bool) -> Vector a -> BoolSource

any :: Storable a => (a -> Bool) -> Vector a -> BoolSource

sum :: (Storable a, Num a) => Vector a -> aSource

product :: (Storable a, Num a) => Vector a -> aSource

maximum :: (Storable a, Ord a) => Vector a -> aSource

maximumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> aSource

minimum :: (Storable a, Ord a) => Vector a -> aSource

minimumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> aSource

minIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> IntSource

maxIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> IntSource

Unfolding

unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector aSource

Scans

prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector aSource

Prefix scan

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

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

toList :: Storable a => Vector a -> [a]Source

Convert a vector to a list

fromList :: Storable a => [a] -> Vector aSource

Convert a list to a vector

Accessing the underlying memory

unsafeFromForeignPtrSource

Arguments

:: 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.

unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO bSource

Pass a pointer to the vector's data to the IO action. The data may not be modified through the 'Ptr.