Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
The
type is an RRB-Vector of elements of type Vector
aa
.
This module should be imported qualified, to avoid name clashes with the Prelude.
Performance
The worst case running time complexities are given, with \(n\) referring to the number of elements in the vector (or \(n_1\), \(n_2\), etc. for multiple vectors). Note that all logarithms are base 16, so the constant factor for \(O(\log n)\) operations is quite small.
Implementation
The implementation uses Relaxed-Radix-Balanced trees, as described by
- Nicolas Stucki, "Turning Relaxed Radix Balanced Vector from Theory into Practice for Scala Collections", January 2015.
Currently, a branching factor of 16 is used. The tree is strict in its spine, but lazy in its elements.
Synopsis
- data Vector a
- empty :: Vector a
- singleton :: a -> Vector a
- fromList :: [a] -> Vector a
- replicate :: Int -> a -> Vector a
- (<|) :: a -> Vector a -> Vector a
- (|>) :: Vector a -> a -> Vector a
- (><) :: Vector a -> Vector a -> Vector a
- viewl :: Vector a -> Maybe (a, Vector a)
- viewr :: Vector a -> Maybe (Vector a, a)
- lookup :: Int -> Vector a -> Maybe a
- index :: HasCallStack => Int -> Vector a -> a
- (!?) :: Vector a -> Int -> Maybe a
- (!) :: HasCallStack => Vector a -> Int -> a
- update :: Int -> a -> Vector a -> Vector a
- adjust :: Int -> (a -> a) -> Vector a -> Vector a
- adjust' :: Int -> (a -> a) -> Vector a -> Vector a
- take :: Int -> Vector a -> Vector a
- drop :: Int -> Vector a -> Vector a
- splitAt :: Int -> Vector a -> (Vector a, Vector a)
- insertAt :: Int -> a -> Vector a -> Vector a
- deleteAt :: Int -> Vector a -> Vector a
- findIndexL :: (a -> Bool) -> Vector a -> Maybe Int
- findIndexR :: (a -> Bool) -> Vector a -> Maybe Int
- findIndicesL :: (a -> Bool) -> Vector a -> [Int]
- findIndicesR :: (a -> Bool) -> Vector a -> [Int]
- module Data.Foldable.WithIndex
- module Data.Functor.WithIndex
- module Data.Traversable.WithIndex
- map :: (a -> b) -> Vector a -> Vector b
- map' :: (a -> b) -> Vector a -> Vector b
- reverse :: Vector a -> Vector a
- zip :: Vector a -> Vector b -> Vector (a, b)
- zipWith :: (a -> b -> c) -> Vector a -> Vector b -> Vector c
- unzip :: Vector (a, b) -> (Vector a, Vector b)
- unzipWith :: (a -> (b, c)) -> Vector a -> (Vector b, Vector c)
- sort :: Ord a => Vector a -> Vector a
- sortBy :: (a -> a -> Ordering) -> Vector a -> Vector a
- sortOn :: Ord b => (a -> b) -> Vector a -> Vector a
Documentation
A vector.
The instances are based on those of Seq
s, which are in turn based on those of lists.
Instances
Construction
singleton :: a -> Vector a Source #
\(O(1)\). A vector with a single element.
singleton x = fromList [x]
replicate :: Int -> a -> Vector a Source #
\(O(\log n)\). replicate n x
creates a vector of length n
with every element set to x
.
>>>
replicate 5 42
fromList [42,42,42,42,42]
Since: 0.1.1.0
Concatenation
(<|) :: a -> Vector a -> Vector a infixr 5 Source #
\(O(\log n)\). Add an element to the left end of the vector.
>>>
1 <| fromList [2, 3, 4]
fromList [1,2,3,4]
(|>) :: Vector a -> a -> Vector a infixl 5 Source #
\(O(\log n)\). Add an element to the right end of the vector.
>>>
fromList [1, 2, 3] |> 4
fromList [1,2,3,4]
(><) :: Vector a -> Vector a -> Vector a infixr 5 Source #
\(O(\log \max(n_1, n_2))\). Concatenates two vectors.
>>>
fromList [1, 2, 3] >< fromList [4, 5]
fromList [1,2,3,4,5]
Deconstruction
viewl :: Vector a -> Maybe (a, Vector a) Source #
\(O(\log n)\). The first element and the vector without the first element, or Nothing
if the vector is empty.
>>>
viewl (fromList [1, 2, 3])
Just (1,fromList [2,3])
viewr :: Vector a -> Maybe (Vector a, a) Source #
\(O(\log n)\). The vector without the last element and the last element, or Nothing
if the vector is empty.
>>>
viewr (fromList [1, 2, 3])
Just (fromList [1,2],3)
Indexing
lookup :: Int -> Vector a -> Maybe a Source #
\(O(\log n)\). The element at the index or Nothing
if the index is out of range.
index :: HasCallStack => Int -> Vector a -> a Source #
\(O(\log n)\). The element at the index. Calls error
if the index is out of range.
update :: Int -> a -> Vector a -> Vector a Source #
\(O(\log n)\). Update the element at the index with a new element. If the index is out of range, the original vector is returned.
adjust :: Int -> (a -> a) -> Vector a -> Vector a Source #
\(O(\log n)\). Adjust the element at the index by applying the function to it. If the index is out of range, the original vector is returned.
adjust' :: Int -> (a -> a) -> Vector a -> Vector a Source #
\(O(\log n)\). Like adjust
, but the result of the function is forced.
take :: Int -> Vector a -> Vector a Source #
\(O(\log n)\). The first i
elements of the vector.
If i
is negative, the empty vector is returned. If the vector contains less than i
elements, the whole vector is returned.
drop :: Int -> Vector a -> Vector a Source #
\(O(\log n)\). The vector without the first i
elements.
If i
is negative, the whole vector is returned. If the vector contains less than i
elements, the empty vector is returned.
splitAt :: Int -> Vector a -> (Vector a, Vector a) Source #
\(O(\log n)\). Split the vector at the given index.
splitAt n v = (take n v, drop n v)
insertAt :: Int -> a -> Vector a -> Vector a Source #
\(O(\log n)\). Insert an element at the given index, shifting the rest of the vector over. If the index is negative, add the element to the left end of the vector. If the index is bigger than or equal to the length of the vector, add the element to the right end of the vector.
deleteAt :: Int -> Vector a -> Vector a Source #
\(O(\log n)\). Delete the element at the given index. If the index is out of range, return the original vector.
findIndexL :: (a -> Bool) -> Vector a -> Maybe Int Source #
\(O(n)\). Find the first index from the left that satisfies the predicate.
Since: 0.2.1.0
findIndexR :: (a -> Bool) -> Vector a -> Maybe Int Source #
\(O(n)\). Find the first index from the right that satisfies the predicate.
Since: 0.2.1.0
findIndicesL :: (a -> Bool) -> Vector a -> [Int] Source #
\(O(n)\). Find the indices that satisfy the predicate, starting from the left.
Since: 0.2.1.0
findIndicesR :: (a -> Bool) -> Vector a -> [Int] Source #
\(O(n)\). Find the indices that satisfy the predicate, starting from the right.
Since: 0.2.1.0
With Index
Reexported from indexed-traversable.
module Data.Foldable.WithIndex
module Data.Functor.WithIndex
module Data.Traversable.WithIndex
Transformations
map :: (a -> b) -> Vector a -> Vector b Source #
\(O(n)\). Apply the function to every element.
>>>
map (+ 1) (fromList [1, 2, 3])
fromList [2,3,4]
map' :: (a -> b) -> Vector a -> Vector b Source #
\(O(n)\). Like map
, but the results of the function are forced.
Since: 0.2.0.0
reverse :: Vector a -> Vector a Source #
\(O(n)\). Reverse the vector.
>>>
reverse (fromList [1, 2, 3])
fromList [3,2,1]
Zipping and unzipping
zip :: Vector a -> Vector b -> Vector (a, b) Source #
\(O(\min(n_1, n_2))\). Take two vectors and return a vector of corresponding pairs. If one input is longer, excess elements are discarded from the right end.
unzip :: Vector (a, b) -> (Vector a, Vector b) Source #
\(O(n)\). Unzip a vector of pairs.
>>>
unzip (fromList [(1, "a"), (2, "b"), (3, "c")])
(fromList [1,2,3],fromList ["a","b","c"])
unzipWith :: (a -> (b, c)) -> Vector a -> (Vector b, Vector c) Source #
\(O(n)\). Unzip a vector with a function.
unzipWith f = unzip . map f
Since: 0.2.0.0
Sorting
Currently implemented using samsort.
sort :: Ord a => Vector a -> Vector a Source #
\(O(n \log n)\). Sort the vector in ascending order. The sort is stable, meaning the order of equal elements is preserved.
Since: 0.2.2.0
sortBy :: (a -> a -> Ordering) -> Vector a -> Vector a Source #
\(O(n \log n)\). Sort the vector in ascending order according to the specified comparison function. The sort is stable, meaning the order of equal elements is preserved.
Since: 0.2.2.0