| storablevector-0.2.1: Fast, packed, strict storable arrays with a list interface like ByteString | Source code | Contents | Index |
|
|
|
|
|
Description |
A time and space-efficient implementation of vectors using
packed arrays, suitable for high performance use, both in terms
of large data quantities, or high speed requirements. Vectors
are encoded as strict arrays, held in a ForeignPtr,
and can be passed between C and Haskell with little effort.
This module is intended to be imported qualified, to avoid name
clashes with Prelude functions. eg.
import qualified Data.StorableVector as V
Original GHC implementation by Bryan O'Sullivan. Rewritten to use
UArray by Simon Marlow. Rewritten to support slices and use
ForeignPtr by David Roundy. Polished and extended by Don Stewart.
Generalized to any Storable value by Spencer Janssen.
Chunky lazy stream and mutable access in ST monad by Henning Thieleman.
|
|
Synopsis |
|
data Vector a | | empty :: Storable a => Vector a | | singleton :: Storable a => a -> Vector a | | pack :: Storable a => [a] -> Vector a | | unpack :: Storable a => Vector a -> [a] | | packWith :: Storable b => (a -> b) -> [a] -> Vector b | | unpackWith :: Storable a => (a -> b) -> Vector a -> [b] | | cons :: Storable a => a -> Vector a -> Vector a | | snoc :: Storable a => Vector a -> a -> Vector a | | append :: Storable a => Vector a -> Vector a -> Vector a | | head :: Storable a => Vector a -> a | | last :: Storable a => Vector a -> a | | tail :: Storable a => Vector a -> Vector a | | init :: Vector a -> Vector a | | null :: Vector a -> Bool | | length :: Vector a -> Int | | viewL :: Storable a => Vector a -> Maybe (a, Vector a) | | viewR :: Storable a => Vector a -> Maybe (Vector a, a) | | map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b | | reverse :: Storable a => Vector a -> Vector a | | intersperse :: Storable a => a -> Vector a -> Vector a | | transpose :: Storable a => [Vector a] -> [Vector a] | | foldl :: Storable a => (b -> a -> b) -> b -> Vector a -> b | | foldl' :: Storable a => (b -> a -> b) -> b -> Vector a -> b | | foldl1 :: Storable a => (a -> a -> a) -> Vector a -> 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 | | concat :: Storable a => [Vector a] -> Vector a | | concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b | | any :: Storable a => (a -> Bool) -> Vector a -> Bool | | all :: Storable a => (a -> Bool) -> Vector a -> Bool | | maximum :: (Storable a, Ord a) => Vector a -> a | | minimum :: (Storable a, Ord a) => Vector a -> a | | scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a | | scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a | | scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b | | scanr1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a | | mapAccumL :: (Storable a, Storable b) => (acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b) | | mapAccumR :: (Storable a, Storable b) => (acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b) | | mapIndexed :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b | | replicate :: Storable a => Int -> a -> Vector a | | unfoldr :: Storable b => (a -> Maybe (b, a)) -> a -> Vector b | | unfoldrN :: Storable b => Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a) | | take :: Storable a => Int -> Vector a -> Vector a | | drop :: Storable a => Int -> Vector a -> Vector a | | splitAt :: Storable a => Int -> Vector a -> (Vector a, Vector a) | | takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a | | dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a | | span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) | | spanEnd :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) | | break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) | | breakEnd :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) | | group :: (Storable a, Eq a) => Vector a -> [Vector a] | | groupBy :: Storable a => (a -> a -> Bool) -> Vector a -> [Vector a] | | inits :: Storable a => Vector a -> [Vector a] | | tails :: Storable a => Vector a -> [Vector a] | | split :: (Storable a, Eq a) => a -> Vector a -> [Vector a] | | splitWith :: Storable a => (a -> Bool) -> Vector a -> [Vector a] | | tokens :: Storable a => (a -> Bool) -> Vector a -> [Vector a] | | join :: Storable a => Vector a -> [Vector a] -> Vector a | | isPrefixOf :: (Storable a, Eq a) => Vector a -> Vector a -> Bool | | isSuffixOf :: (Storable a, Eq a) => Vector a -> Vector a -> Bool | | 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 | | filter :: Storable a => (a -> Bool) -> Vector a -> Vector a | | index :: Storable a => Vector a -> Int -> a | | elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe Int | | elemIndices :: (Storable a, Eq a) => a -> Vector a -> [Int] | | elemIndexEnd :: (Storable a, Eq a) => a -> Vector a -> Maybe Int | | findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int | | findIndices :: Storable a => (a -> Bool) -> Vector a -> [Int] | | count :: (Storable a, Eq a) => a -> Vector a -> Int | | findIndexOrEnd :: Storable a => (a -> Bool) -> Vector a -> Int | | zip :: (Storable a, Storable b) => Vector a -> Vector b -> [(a, b)] | | zipWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c | | unzip :: (Storable a, Storable b) => [(a, b)] -> (Vector a, Vector b) | | copy :: Storable a => Vector a -> Vector a | | hGet :: Storable a => Handle -> Int -> IO (Vector a) | | hPut :: Storable a => Handle -> Vector a -> IO () | | readFile :: Storable a => FilePath -> IO (Vector a) | | writeFile :: Storable a => FilePath -> Vector a -> IO () | | appendFile :: Storable a => FilePath -> Vector a -> IO () |
|
|
|
The Vector type
|
|
|
A space-efficient representation of a vector, supporting many efficient
operations.
Instances of Eq, Ord, Read, Show, Data, Typeable
| Instances | |
|
|
Introducing and eliminating Vectors
|
|
|
O(1) The empty Vector
|
|
|
O(1) Construct a Vector containing a single element
|
|
|
O(n) Convert a '[a]' into a 'Vector a'.
|
|
|
O(n) Converts a 'Vector a' to a '[a]'.
|
|
|
O(n) Convert a list into a Vector using a conversion function
|
|
|
O(n) Convert a Vector into a list using a conversion function
|
|
Basic interface
|
|
|
O(n) cons is analogous to (:) for lists, but of different
complexity, as it requires a memcpy.
|
|
|
O(n) Append an element to the end of a Vector
|
|
|
O(n) Append two Vectors
|
|
|
O(1) Extract the first element of a Vector, which must be non-empty.
An exception will be thrown in the case of an empty Vector.
|
|
|
O(1) Extract the last element of a Vector, which must be finite and non-empty.
An exception will be thrown in the case of an empty Vector.
|
|
|
O(1) Extract the elements after the head of a Vector, which must be non-empty.
An exception will be thrown in the case of an empty Vector.
|
|
|
O(1) Return all the elements of a Vector except the last one.
An exception will be thrown in the case of an empty Vector.
|
|
|
O(1) Test whether a Vector is empty.
|
|
|
O(1) length returns the length of a Vector as an Int.
|
|
|
|
|
|
Transformating Vectors
|
|
|
O(n) map f xs is the Vector obtained by applying f to each
element of xs.
|
|
|
O(n) reverse xs efficiently returns the elements of xs in reverse order.
|
|
|
O(n) The intersperse function takes a element and a
Vector and `intersperses' that element between the elements of
the Vector. It is analogous to the intersperse function on
Lists.
|
|
|
The transpose function transposes the rows and columns of its
Vector argument.
|
|
Reducing Vectors (folds)
|
|
|
foldl, applied to a binary operator, a starting value (typically
the left-identity of the operator), and a Vector, reduces the
Vector using the binary operator, from left to right.
This function is subject to array fusion.
|
|
|
'foldl\'' is like foldl, but strict in the accumulator.
Though actually foldl is also strict in the accumulator.
|
|
|
foldl1 is a variant of foldl that has no starting value
argument, and thus must be applied to non-empty Vectors.
This function is subject to array fusion.
An exception will be thrown in the case of an empty Vector.
|
|
|
'foldl1\'' is like foldl1, but strict in the accumulator.
An exception will be thrown in the case of an empty Vector.
|
|
|
foldr, applied to a binary operator, a starting value
(typically the right-identity of the operator), and a Vector,
reduces the Vector using the binary operator, from right to left.
|
|
|
foldr1 is a variant of foldr that has no starting value argument,
and thus must be applied to non-empty Vectors
An exception will be thrown in the case of an empty Vector.
|
|
Special folds
|
|
|
O(n) Concatenate a list of Vectors.
|
|
|
Map a function over a Vector and concatenate the results
|
|
|
O(n) Applied to a predicate and a Vector, any determines if
any element of the Vector satisfies the predicate.
|
|
|
O(n) Applied to a predicate and a Vector, all determines
if all elements of the Vector satisfy the predicate.
|
|
|
O(n) maximum returns the maximum value from a Vector
This function will fuse.
An exception will be thrown in the case of an empty Vector.
|
|
|
O(n) minimum returns the minimum value from a Vector
This function will fuse.
An exception will be thrown in the case of an empty Vector.
|
|
Building Vectors
|
|
Scans
|
|
|
scanl is similar to foldl, but returns a list of successive
reduced values from the left. This function will fuse.
scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
Note that
last (scanl f z xs) == foldl f z xs.
|
|
|
scanl1 is a variant of scanl that has no starting value argument.
This function will fuse.
scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
|
|
|
scanr is the right-to-left dual of scanl.
|
|
|
scanr1 is a variant of scanr that has no starting value argument.
|
|
Accumulating maps
|
|
|
The mapAccumL function behaves like a combination of map and
foldl; it applies a function to each element of a Vector,
passing an accumulating parameter from left to right, and returning a
final value of this accumulator together with the new list.
|
|
|
The mapAccumR function behaves like a combination of map and
foldr; it applies a function to each element of a Vector,
passing an accumulating parameter from right to left, and returning a
final value of this accumulator together with the new Vector.
|
|
|
O(n) map functions, provided with the index at each position
|
|
Unfolding Vectors
|
|
|
O(n) replicate n x is a Vector of length n with x
the value of every element.
|
|
|
O(n), where n is the length of the result. The unfoldr
function is analogous to the List 'unfoldr'. unfoldr builds a
Vector from a seed value. The function takes the element and
returns Nothing if it is done producing the 'Vector or returns
Just (a,b), in which case, a is the next element in the Vector,
and b is the seed value for further production.
Examples:
unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0
== pack [0, 1, 2, 3, 4, 5]
|
|
|
O(n) Like unfoldr, unfoldrN builds a Vector from a seed
value. However, the length of the result is limited by the first
argument to unfoldrN. This function is more efficient than unfoldr
when the maximum length of the result is known.
The following equation relates unfoldrN and unfoldr:
fst (unfoldrN n f s) == take n (unfoldr f s)
|
|
Substrings
|
|
Breaking strings
|
|
|
O(1) take n, applied to a Vector xs, returns the prefix
of xs of length n, or xs itself if n > length xs.
|
|
|
O(1) drop n xs returns the suffix of xs after the first n
elements, or [] if n > length xs.
|
|
|
O(1) splitAt n xs is equivalent to (take n xs, drop n xs).
|
|
|
takeWhile, applied to a predicate p and a Vector xs,
returns the longest prefix (possibly empty) of xs of elements that
satisfy p.
|
|
|
dropWhile p xs returns the suffix remaining after takeWhile p xs.
|
|
|
span p xs breaks the Vector into two segments. It is
equivalent to (takeWhile p xs, dropWhile p xs)
|
|
|
spanEnd behaves like span but from the end of the Vector.
We have
spanEnd (not.isSpace) "x y z" == ("x y ","z")
and
spanEnd (not . isSpace) ps
==
let (x,y) = span (not.isSpace) (reverse ps) in (reverse y, reverse x)
|
|
|
break p is equivalent to span (not . p).
|
|
|
breakEnd behaves like break but from the end of the Vector
breakEnd p == spanEnd (not.p)
|
|
|
The group function takes a Vector and returns a list of
Vectors such that the concatenation of the result is equal to the
argument. Moreover, each sublist in the result contains only equal
elements. For example,
group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]
It is a special case of groupBy, which allows the programmer to
supply their own equality test. It is about 40% faster than
groupBy (==)
|
|
|
The groupBy function is the non-overloaded version of group.
|
|
|
O(n) Return all initial segments of the given Vector, shortest first.
|
|
|
O(n) Return all final segments of the given Vector, longest first.
|
|
Breaking into many substrings
|
|
|
O(n) Break a Vector into pieces separated by the
argument, consuming the delimiter. I.e.
split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
split 'a' "aXaXaXa" == ["","X","X","X"]
split 'x' "x" == ["",""]
and
join [c] . split c == id
split == splitWith . (==)
As for all splitting functions in this library, this function does
not copy the substrings, it just constructs new Vectors that
are slices of the original.
|
|
|
O(n) Splits a Vector into components delimited by
separators, where the predicate returns True for a separator element.
The resulting components do not contain the separators. Two adjacent
separators result in an empty component in the output. eg.
splitWith (=='a') "aabbaca" == ["","","bb","c",""]
splitWith (=='a') [] == []
|
|
|
Like splitWith, except that sequences of adjacent separators are
treated as a single separator. eg.
tokens (=='a') "aabbaca" == ["bb","c"]
|
|
Joining strings
|
|
|
O(n) The join function takes a Vector and a list of
Vectors and concatenates the list after interspersing the first
argument between each element of the list.
|
|
Predicates
|
|
|
O(n) The isPrefixOf function takes two Vector and returns True
iff the first is a prefix of the second.
|
|
|
O(n) The isSuffixOf function takes two Vectors and returns True
iff the first is a suffix of the second.
The following holds:
isSuffixOf x y == reverse x `isPrefixOf` reverse y
|
|
Searching Vectors
|
|
Searching by equality
|
|
|
O(n) elem is the Vector membership predicate.
|
|
|
O(n) notElem is the inverse of elem
|
|
Searching with a predicate
|
|
|
O(n) The find function takes a predicate and a Vector,
and returns the first element in matching the predicate, or Nothing
if there is no such element.
find f p = case findIndex f p of Just n -> Just (p ! n) ; _ -> Nothing
|
|
|
O(n) filter, applied to a predicate and a Vector,
returns a Vector containing those elements that satisfy the
predicate. This function is subject to array fusion.
|
|
Indexing Vectors
|
|
|
O(1) Vector index (subscript) operator, starting from 0.
|
|
|
O(n) The elemIndex function returns the index of the first
element in the given Vector which is equal to the query
element, or Nothing if there is no such element.
This implementation uses memchr(3).
|
|
|
O(n) The elemIndices function extends elemIndex, by returning
the indices of all elements equal to the query element, in ascending order.
This implementation uses memchr(3).
|
|
|
O(n) The elemIndexEnd function returns the last index of the
element in the given Vector which is equal to the query
element, or Nothing if there is no such element. The following
holds:
elemIndexEnd c xs ==
(-) (length xs - 1) `fmap` elemIndex c (reverse xs)
|
|
|
The findIndex function takes a predicate and a Vector and
returns the index of the first element in the Vector
satisfying the predicate.
|
|
|
The findIndices function extends findIndex, by returning the
indices of all elements satisfying the predicate, in ascending order.
|
|
|
count returns the number of times its argument appears in the Vector
count = length . elemIndices
But more efficiently than using length on the intermediate list.
|
|
|
findIndexOrEnd is a variant of findIndex, that returns the length
of the string if no element is found, rather than Nothing.
|
|
Zipping and unzipping Vectors
|
|
|
O(n) zip takes two Vectors and returns a list of
corresponding pairs of elements. If one input Vector is short,
excess elements of the longer Vector are discarded. This is
equivalent to a pair of unpack operations.
|
|
|
zipWith generalises zip by zipping with the function given as
the first argument, instead of a tupling function. For example,
zipWith (+) is applied to two Vectors to produce the list of
corresponding sums.
|
|
|
O(n) unzip transforms a list of pairs of elements into a pair of
Vectors. Note that this performs two pack operations.
|
|
|
O(n) Make a copy of the Vector with its own storage.
This is mainly useful to allow the rest of the data pointed
to by the Vector to be garbage collected, for example
if a large string has been read in, and only a small part of it
is needed in the rest of the program.
|
|
IO
|
|
|
Read a Vector directly from the specified Handle. This
is far more efficient than reading the characters into a list
and then using pack.
|
|
|
Outputs a Vector to the specified Handle.
|
|
|
Read an entire file strictly into a Vector. This is far more
efficient than reading the characters into a String and then using
pack. It also may be more efficient than opening the file and
reading it using hGet. Files are read using 'binary mode' on Windows.
|
|
|
Write a Vector to a file.
|
|
|
Append a Vector to a file.
|
|
Produced by Haddock version 2.4.2 |