vector-0.12.2.0: Efficient Arrays
Copyright (c) Roman Leshchinskiy 2008-2010 BSD-style Roman Leshchinskiy experimental non-portable None Haskell2010

Data.Vector

Description

A library for boxed vectors (that is, polymorphic arrays capable of holding any Haskell value). The vectors come in two flavours:

• mutable
• immutable

and support a rich interface of both list-like operations, and bulk array operations.

For unboxed arrays, use Data.Vector.Unboxed

Synopsis

# Boxed vectors

data Vector a Source #

Boxed vectors, supporting efficient slicing.

#### Instances

Instances details
True
False
>>> V.any even $V.fromList [3, 2, 13 :: Int] True >>> V.any even (V.empty :: V.Vector Int) False O(n) Check if all elements are True #### Examples Expand >>> import qualified Data.Vector as V >>> V.and$ V.fromList [True, False]
False
>>> V.and V.empty
True

O(n) Check if any element is True

#### Examples

Expand
>>> import qualified Data.Vector as V
>>> V.or $V.fromList [True, False] True >>> V.or V.empty False sum :: Num a => Vector a -> a Source # O(n) Compute the sum of the elements #### Examples Expand >>> import qualified Data.Vector as V >>> V.sum$ V.fromList [300,20,1 :: Int]
321
>>> V.sum (V.empty :: V.Vector Int)
0

product :: Num a => Vector a -> a Source #

O(n) Compute the produce of the elements

#### Examples

Expand
>>> import qualified Data.Vector as V
>>> V.product $V.fromList [1,2,3,4 :: Int] 24 >>> V.product (V.empty :: V.Vector Int) 1 maximum :: Ord a => Vector a -> a Source # O(n) Yield the maximum element of the vector. The vector may not be empty. #### Examples Expand >>> import qualified Data.Vector as V >>> V.maximum$ V.fromList [2.0, 1.0]
2.0

maximumBy :: (a -> a -> Ordering) -> Vector a -> a Source #

O(n) Yield the maximum element of the vector according to the given comparison function. The vector may not be empty.

minimum :: Ord a => Vector a -> a Source #

O(n) Yield the minimum element of the vector. The vector may not be empty.

#### Examples

Expand
>>> import qualified Data.Vector as V
>>> V.minimum \$ V.fromList [2.0, 1.0]
1.0

minimumBy :: (a -> a -> Ordering) -> Vector a -> a Source #

O(n) Yield the minimum element of the vector according to the given comparison function. The vector may not be empty.

minIndex :: Ord a => Vector a -> Int Source #

O(n) Yield the index of the minimum element of the vector. The vector may not be empty.

minIndexBy :: (a -> a -> Ordering) -> Vector a -> Int Source #

O(n) Yield the index of the minimum element of the vector according to the given comparison function. The vector may not be empty.

maxIndex :: Ord a => Vector a -> Int Source #

O(n) Yield the index of the maximum element of the vector. The vector may not be empty.

maxIndexBy :: (a -> a -> Ordering) -> Vector a -> Int Source #

O(n) Yield the index of the maximum element of the vector according to the given comparison function. The vector may not be empty.

foldM :: Monad m => (a -> b -> m a) -> a -> Vector b -> m a Source #

ifoldM :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m a Source #

O(n) Monadic fold (action applied to each element and its index)

foldM' :: Monad m => (a -> b -> m a) -> a -> Vector b -> m a Source #

O(n) Monadic fold with strict accumulator

ifoldM' :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m a Source #

O(n) Monadic fold with strict accumulator (action applied to each element and its index)

fold1M :: Monad m => (a -> a -> m a) -> Vector a -> m a Source #

O(n) Monadic fold over non-empty vectors

fold1M' :: Monad m => (a -> a -> m a) -> Vector a -> m a Source #

O(n) Monadic fold over non-empty vectors with strict accumulator

foldM_ :: Monad m => (a -> b -> m a) -> a -> Vector b -> m () Source #

O(n) Monadic fold that discards the result

ifoldM_ :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m () Source #

O(n) Monadic fold that discards the result (action applied to each element and its index)

foldM'_ :: Monad m => (a -> b -> m a) -> a -> Vector b -> m () Source #

O(n) Monadic fold with strict accumulator that discards the result

ifoldM'_ :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m () Source #

O(n) Monadic fold with strict accumulator that discards the result (action applied to each element and its index)

fold1M_ :: Monad m => (a -> a -> m a) -> Vector a -> m () Source #

O(n) Monadic fold over non-empty vectors that discards the result

fold1M'_ :: Monad m => (a -> a -> m a) -> Vector a -> m () Source #

O(n) Monadic fold over non-empty vectors with strict accumulator that discards the result

sequence :: Monad m => Vector (m a) -> m (Vector a) Source #

Evaluate each action and collect the results

sequence_ :: Monad m => Vector (m a) -> m () Source #

Evaluate each action and discard the results

# Prefix sums (scans)

prescanl :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Prescan

prescanl f z = init . scanl f z

Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6>

prescanl' :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Prescan with strict accumulator

postscanl :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Scan

postscanl f z = tail . scanl f z

Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10>

postscanl' :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Scan with strict accumulator

scanl :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

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' :: (a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Haskell-style scan with strict accumulator

scanl1 :: (a -> a -> a) -> Vector a -> Vector 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' :: (a -> a -> a) -> Vector a -> Vector a Source #

O(n) Scan over a non-empty vector with a strict accumulator

iscanl :: (Int -> a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Scan over a vector with its index

Since: 0.12.0.0

iscanl' :: (Int -> a -> b -> a) -> a -> Vector b -> Vector a Source #

O(n) Scan over a vector (strictly) with its index

Since: 0.12.0.0

prescanr :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left prescan

prescanr f z = reverse . prescanl (flip f) z . reverse

prescanr' :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left prescan with strict accumulator

postscanr :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left scan

postscanr' :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left scan with strict accumulator

scanr :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left Haskell-style scan

scanr' :: (a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left Haskell-style scan with strict accumulator

scanr1 :: (a -> a -> a) -> Vector a -> Vector a Source #

O(n) Right-to-left scan over a non-empty vector

scanr1' :: (a -> a -> a) -> Vector a -> Vector a Source #

O(n) Right-to-left scan over a non-empty vector with a strict accumulator

iscanr :: (Int -> a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left scan over a vector with its index

Since: 0.12.0.0

iscanr' :: (Int -> a -> b -> b) -> b -> Vector a -> Vector b Source #

O(n) Right-to-left scan over a vector (strictly) with its index

Since: 0.12.0.0

## Comparisons

eqBy :: (a -> b -> Bool) -> Vector a -> Vector b -> Bool Source #

O(n) Check if two vectors are equal using supplied equality predicate.

Since: 0.12.2.0

cmpBy :: (a -> b -> Ordering) -> Vector a -> Vector b -> Ordering Source #

O(n) Compare two vectors using supplied comparison function for vector elements. Comparison works same as for lists.

cmpBy compare == compare

Since: 0.12.2.0

# Conversions

## Lists

toList :: Vector a -> [a] Source #

O(n) Convert a vector to a list

fromList :: [a] -> Vector a Source #

O(n) Convert a list to a vector

fromListN :: Int -> [a] -> Vector a Source #

O(n) Convert the first n elements of a list to a vector

fromListN n xs = fromList (take n xs)

## Arrays

fromArray :: Array a -> Vector a Source #

O(1) Convert an array to a vector.

Since: 0.12.2.0

toArray :: Vector a -> Array a Source #

O(n) Convert a vector to an array.

Since: 0.12.2.0

## Other vector types

convert :: (Vector v a, Vector w a) => v a -> w a Source #

O(n) Convert different vector types

## Mutable vectors

freeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) Source #

O(n) Yield an immutable copy of the mutable vector.

thaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) Source #

O(n) Yield a mutable copy of the immutable vector.

copy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m () Source #

O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length.

unsafeFreeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) Source #

O(1) Unsafe convert a mutable vector to an immutable one without copying. The mutable vector may not be used after this operation.

unsafeThaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) Source #

O(1) Unsafely convert an immutable vector to a mutable one without copying. The immutable vector may not be used after this operation.

unsafeCopy :: PrimMonad m => MVector (PrimState m) a -> Vector 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.