vector-0.1: Efficient Arrays

Portabilitynon-portable
Stabilityexperimental
Maintainerrl@cse.unsw.edu.au

Data.Vector.IVector

Contents

Description

Generic interface to pure vectors

Synopsis

Immutable vectors

class IVector v a whereSource

Class of immutable vectors.

Methods

vnew :: (forall mv m. MVector mv m a => m (mv a)) -> v aSource

Construct a pure vector from a monadic initialiser (not fusible!)

vlength :: v a -> IntSource

Length of the vector (not fusible!)

unsafeSlice :: v a -> Int -> Int -> v aSource

Yield a part of the vector without copying it. No range checks!

unsafeIndex :: v a -> Int -> (a -> b) -> bSource

Apply the given function to the element at the given position. This interface prevents us from being too lazy. Suppose we had

 unsafeIndex' :: v a -> Int -> a

instead. Now, if we wanted to copy a vector, we'd do something like

 copy mv v ... = ... unsafeWrite mv i (unsafeIndex' v i) ...

For lazy vectors, the indexing would not be evaluated which means that we would retain a reference to the original vector in each element we write. This would be bad!

With unsafeIndex, we can do

 copy mv v ... = ... unsafeIndex v i (unsafeWrite mv i) ...

which does not have this problem.

Instances

Length information

length :: IVector v a => v a -> IntSource

Construction

empty :: IVector v a => v aSource

Empty vector

singleton :: IVector v a => a -> v aSource

Vector with exaclty one element

cons :: IVector v a => a -> v a -> v aSource

Prepend an element

snoc :: IVector v a => v a -> a -> v aSource

Append an element

replicate :: IVector v a => Int -> a -> v aSource

Vector of the given length with the given value in each position

(++) :: IVector v a => v a -> v a -> v aSource

Concatenate two vectors

Accessing individual elements

(!) :: IVector v a => v a -> Int -> aSource

Indexing

head :: IVector v a => v a -> aSource

First element

last :: IVector v a => v a -> aSource

Last element

Subvectors

sliceSource

Arguments

:: IVector v a 
=> v a 
-> Int

starting index

-> Int

length

-> v a 

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

extractSource

Arguments

:: IVector v a 
=> v a 
-> Int

starting index

-> Int

length

-> v a 

Copy n elements starting at the given position to a new vector.

takeSlice :: IVector v a => Int -> v a -> v aSource

Yield the first n elements without copying.

take :: IVector v a => Int -> v a -> v aSource

Copy the first n elements to a new vector.

dropSlice :: IVector v a => Int -> v a -> v aSource

Yield all but the first n elements without copying.

drop :: IVector v a => Int -> v a -> v aSource

Copy all but the first n elements to a new vectors.

Permutations

(//) :: IVector v a => v a -> [(Int, a)] -> v aSource

Mapping and zipping

map :: (IVector v a, IVector v b) => (a -> b) -> v a -> v bSource

Map a function over a vector

zipWith :: (IVector v a, IVector v b, IVector v c) => (a -> b -> c) -> v a -> v b -> v cSource

Zip two vectors with the given function.

Comparisons

eq :: (IVector v a, Eq a) => v a -> v a -> BoolSource

cmp :: (IVector v a, Ord a) => v a -> v a -> OrderingSource

Filtering

filter :: IVector v a => (a -> Bool) -> v a -> v aSource

Drop elements which do not satisfy the predicate

takeWhileSlice :: IVector v a => (a -> Bool) -> v a -> v aSource

Yield the longest prefix of elements satisfying the predicate without copying.

takeWhile :: IVector v a => (a -> Bool) -> v a -> v aSource

Copy the longest prefix of elements satisfying the predicate to a new vector

dropWhileSlice :: IVector v a => (a -> Bool) -> v a -> v aSource

Drop the longest prefix of elements that satisfy the predicate without copying

dropWhile :: IVector v a => (a -> Bool) -> v a -> v aSource

Drop the longest prefix of elements that satisfy the predicate and copy the rest to a new vector.

Searching

elem :: (IVector v a, Eq a) => a -> v a -> BoolSource

Check whether the vector contains an element

notElem :: (IVector v a, Eq a) => a -> v a -> BoolSource

Inverse of elem

find :: IVector v a => (a -> Bool) -> v a -> Maybe aSource

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

findIndex :: IVector v a => (a -> Bool) -> v a -> Maybe IntSource

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

Folding

foldl :: IVector v b => (a -> b -> a) -> a -> v b -> aSource

Left fold

foldl1 :: IVector v a => (a -> a -> a) -> v a -> aSource

Lefgt fold on non-empty vectors

foldl' :: IVector v b => (a -> b -> a) -> a -> v b -> aSource

Left fold with strict accumulator

foldl1' :: IVector v a => (a -> a -> a) -> v a -> aSource

Left fold on non-empty vectors with strict accumulator

foldr :: IVector v a => (a -> b -> b) -> b -> v a -> bSource

Right fold

foldr1 :: IVector v a => (a -> a -> a) -> v a -> aSource

Right fold on non-empty vectors

Conversion to/from lists

toList :: IVector v a => v a -> [a]Source

Convert a vector to a list

fromList :: IVector v a => [a] -> v aSource

Convert a list to a vector

Conversion to/from Streams

stream :: IVector v a => v a -> Stream aSource

Convert a vector to a Stream

unstream :: IVector v a => Stream a -> v aSource

Create a vector from a Stream

MVector-based initialisation

new :: IVector v a => Mut a -> v aSource

Construct a pure vector from a monadic initialiser

Unsafe functions

Utility functions