-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Efficient Arrays -- -- An efficient implementation of Int-indexed arrays (both mutable and -- immutable), with a powerful loop optimisation framework . -- -- It is structured as follows: -- --
-- fromListN n xs = fromList (take n xs) --fromListN :: Int -> [a] -> Bundle v a unsafeFromList :: Size -> [a] -> Bundle v a -- | Convert a pure stream to a monadic stream lift :: Monad m => Bundle Id v a -> Bundle m v a fromVector :: Vector v a => v a -> Bundle v a reVector :: Bundle u a -> Bundle v a fromVectors :: Vector v a => [v a] -> Bundle v a concatVectors :: Vector v a => Bundle u (v a) -> Bundle v a -- | Apply a monadic action to each element of the stream, producing a -- monadic stream of results mapM :: Monad m => (a -> m b) -> Bundle v a -> Bundle m v b -- | Apply a monadic action to each element of the stream mapM_ :: Monad m => (a -> m b) -> Bundle v a -> m () zipWithM :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> Bundle m v c zipWithM_ :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> m () -- | Yield a monadic stream of elements that satisfy the monadic predicate filterM :: Monad m => (a -> m Bool) -> Bundle v a -> Bundle m v a -- | O(n) Apply monadic function to each element of a bundle and -- discard elements returning Nothing. mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Bundle v a -> Bundle m v b -- | Monadic fold foldM :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a -- | Monadic fold over non-empty stream fold1M :: Monad m => (a -> a -> m a) -> Bundle v a -> m a -- | Monadic fold with strict accumulator foldM' :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a -- | Monad fold over non-empty stream with strict accumulator fold1M' :: Monad m => (a -> a -> m a) -> Bundle v a -> m a -- | Check if two Bundles are equal eq :: Eq a => Bundle v a -> Bundle v a -> Bool -- | Lexicographically compare two Bundles cmp :: Ord a => Bundle v a -> Bundle v a -> Ordering eqBy :: (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool cmpBy :: (a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Vector.Fusion.Bundle.Monadic.Bundle Data.Vector.Fusion.Util.Id v a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Vector.Fusion.Bundle.Monadic.Bundle Data.Vector.Fusion.Util.Id v a) instance Data.Functor.Classes.Eq1 (Data.Vector.Fusion.Bundle.Monadic.Bundle Data.Vector.Fusion.Util.Id v) instance Data.Functor.Classes.Ord1 (Data.Vector.Fusion.Bundle.Monadic.Bundle Data.Vector.Fusion.Util.Id v) -- | Generic interface to mutable vectors module Data.Vector.Generic.Mutable -- | Class of mutable vectors parametrised with a primitive state token. class MVector v a -- | Length of the mutable vector. This method should not be called -- directly, use length instead. basicLength :: MVector v a => v s a -> Int -- | Yield a part of the mutable vector without copying it. This method -- should not be called directly, use unsafeSlice instead. basicUnsafeSlice :: MVector v a => Int -> Int -> v s a -> v s a -- | Check whether two vectors overlap. This method should not be called -- directly, use overlaps instead. basicOverlaps :: MVector v a => v s a -> v s a -> Bool -- | Create a mutable vector of the given length. This method should not be -- called directly, use unsafeNew instead. basicUnsafeNew :: (MVector v a, PrimMonad m) => Int -> m (v (PrimState m) a) -- | Initialize a vector to a standard value. This is intended to be called -- as part of the safe new operation (and similar operations), to -- properly blank the newly allocated memory if necessary. -- -- Vectors that are necessarily initialized as part of creation may -- implement this as a no-op. basicInitialize :: (MVector v a, PrimMonad m) => v (PrimState m) a -> m () -- | Create a mutable vector of the given length and fill it with an -- initial value. This method should not be called directly, use -- replicate instead. basicUnsafeReplicate :: (MVector v a, PrimMonad m) => Int -> a -> m (v (PrimState m) a) -- | Yield the element at the given position. This method should not be -- called directly, use unsafeRead instead. basicUnsafeRead :: (MVector v a, PrimMonad m) => v (PrimState m) a -> Int -> m a -- | Replace the element at the given position. This method should not be -- called directly, use unsafeWrite instead. basicUnsafeWrite :: (MVector v a, PrimMonad m) => v (PrimState m) a -> Int -> a -> m () -- | Reset all elements of the vector to some undefined value, clearing all -- references to external objects. This is usually a noop for unboxed -- vectors. This method should not be called directly, use clear -- instead. basicClear :: (MVector v a, PrimMonad m) => v (PrimState m) a -> m () -- | Set all elements of the vector to the given value. This method should -- not be called directly, use set instead. basicSet :: (MVector v a, PrimMonad m) => v (PrimState m) a -> a -> m () -- | Copy a vector. The two vectors may not overlap. This method should not -- be called directly, use unsafeCopy instead. basicUnsafeCopy :: (MVector v a, PrimMonad m) => v (PrimState m) a -> v (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors may overlap. This -- method should not be called directly, use unsafeMove instead. basicUnsafeMove :: (MVector v a, PrimMonad m) => v (PrimState m) a -> v (PrimState m) a -> m () -- | Grow a vector by the given number of elements. Allocates a new vector -- and copies all of the elements over starting at 0 index. This method -- should not be called directly, use grow/unsafeGrow -- instead. basicUnsafeGrow :: (MVector v a, PrimMonad m) => v (PrimState m) a -> Int -> m (v (PrimState m) a) -- | Length of the mutable vector. length :: MVector v a => v s a -> Int -- | Check whether the vector is empty null :: MVector v a => v s a -> Bool -- | Yield a part of the mutable vector without copying it. The vector must -- contain at least i+n elements. slice :: MVector v a => Int -> Int -> v s a -> v s a -- | Drop last element of the mutable vector without making a copy. If -- vector is empty exception is thrown. init :: MVector v a => v s a -> v s a -- | Drop first element of the mutable vector without making a copy. If -- vector is empty exception is thrown. tail :: MVector v a => v s a -> v s a -- | Take n first elements of the mutable vector without making a -- copy. For negative n empty vector is returned. If n -- is larger than vector's length empty vector is returned, take :: MVector v a => Int -> v s a -> v s a -- | Drop n first element of the mutable vector without making a -- copy. For negative n vector is returned unchanged and if -- n is larger than vector's length empty vector is returned. drop :: MVector v a => Int -> v s a -> v s a splitAt :: MVector v a => Int -> v s a -> (v s a, v s a) -- | Yield a part of the mutable vector without copying it. No bounds -- checks are performed. unsafeSlice :: MVector v a => Int -> Int -> v s a -> v s a -- | Same as init but doesn't do range checks. unsafeInit :: MVector v a => v s a -> v s a -- | Same as tail but doesn't do range checks. unsafeTail :: MVector v a => v s a -> v s a -- | Unsafe variant of take. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeTake :: MVector v a => Int -> v s a -> v s a -- | Unsafe variant of drop. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeDrop :: MVector v a => Int -> v s a -> v s a -- | Check whether two vectors overlap. overlaps :: MVector v a => v s a -> v s a -> Bool -- | Create a mutable vector of the given length. new :: (PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a) -- | Create a mutable vector of the given length. The vector content should -- be presumed uninitialized. However exact semantics depends on vector -- implementation. For example unboxed and storable vectors will create -- vector filled with whatever underlying memory buffer happens to -- contain, while boxed vector's elements are initialized to bottoms -- which will throw exception when evaluated. unsafeNew :: (PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with an initial value. replicate :: (PrimMonad m, MVector v a) => Int -> a -> m (v (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with values produced by repeatedly executing the -- monadic action. replicateM :: (PrimMonad m, MVector v a) => Int -> m a -> m (v (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- function to each index. generate :: (PrimMonad m, MVector v a) => Int -> (Int -> a) -> m (v (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- monadic function to each index. Iteration starts at index 0. generateM :: (PrimMonad m, MVector v a) => Int -> (Int -> m a) -> m (v (PrimState m) a) -- | Create a copy of a mutable vector. clone :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m (v (PrimState m) a) -- | Grow a vector by the given number of elements. The number must not be -- negative otherwise error is thrown. Semantics of this function is -- exactly the same as unsafeGrow, except that it will initialize -- the newly allocated memory first. -- -- It is important to note that mutating the returned vector will not -- affect the vector that was used as a source. In other words it does -- not, nor will it ever have the semantics of realloc from C. -- --
-- grow mv 0 === clone mv --grow :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m (v (PrimState m) a) -- | Grow a vector by allocating a new mutable vector of the same size plus -- the the given number of elements and copying all the data over to the -- new vector starting at its beginning. The newly allocated memory is -- not initialized and the extra space at the end will likely contain -- garbage data or uninitialzed error. Use unsafeGrowFront to make -- the extra space available in the front of the new vector. -- -- It is important to note that mutating the returned vector will not -- affect elements of the vector that was used as a source. In other -- words it does not, nor will it ever have the semantics of -- realloc from C. Keep in mind, however, that values themselves -- can be of a mutable type (eg. Ptr), in which case it would be -- possible to affect values stored in both vectors. -- --
-- unsafeGrow mv 0 === clone mv --unsafeGrow :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m (v (PrimState m) a) -- | Same as grow, except that it copies data towards the end of the -- newly allocated vector making extra space available at the beginning. growFront :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m (v (PrimState m) a) -- | Same as unsafeGrow, except that it copies data towards the end -- of the newly allocated vector making extra space available at the -- beginning. unsafeGrowFront :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m (v (PrimState m) a) -- | Reset all elements of the vector to some undefined value, clearing all -- references to external objects. This is usually a noop for unboxed -- vectors. clear :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m () -- | Yield the element at the given position. read :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a -- | Replace the element at the given position. write :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. modify :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. modifyM :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. swap :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. exchange :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m a -- | Yield the element at the given position. No bounds checks are -- performed. unsafeRead :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a -- | Replace the element at the given position. No bounds checks are -- performed. unsafeWrite :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. No bounds checks are -- performed. unsafeModify :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. No -- bounds checks are performed. unsafeModifyM :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. No bounds checks are -- performed. unsafeSwap :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. -- No bounds checks are performed. unsafeExchange :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m a -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. mapM_ :: (PrimMonad m, MVector v a) => (a -> m b) -> v (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. imapM_ :: (PrimMonad m, MVector v a) => (Int -> a -> m b) -> v (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. It's same as the flip mapM_. forM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. It's same as the flip -- imapM_. iforM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (Int -> a -> m b) -> m () -- | O(n) Pure left fold. foldl :: (PrimMonad m, MVector v a) => (b -> a -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator. foldl' :: (PrimMonad m, MVector v a) => (b -> a -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic fold. foldM :: (PrimMonad m, MVector v a) => (b -> a -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator. foldM' :: (PrimMonad m, MVector v a) => (b -> a -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure right fold. foldr :: (PrimMonad m, MVector v a) => (a -> b -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator. foldr' :: (PrimMonad m, MVector v a) => (a -> b -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic right fold. foldrM :: (PrimMonad m, MVector v a) => (a -> b -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator. foldrM' :: (PrimMonad m, MVector v a) => (a -> b -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure left fold (function applied to each element and its -- index). ifoldl :: (PrimMonad m, MVector v a) => (b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator (function applied -- to each element and its index). ifoldl' :: (PrimMonad m, MVector v a) => (b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic fold (action applied to each element and its -- index). ifoldM :: (PrimMonad m, MVector v a) => (b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index). ifoldM' :: (PrimMonad m, MVector v a) => (b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure right fold (function applied to each element and its -- index). ifoldr :: (PrimMonad m, MVector v a) => (Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator (function applied -- to each element and its index). ifoldr' :: (PrimMonad m, MVector v a) => (Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic right fold (action applied to each element and its -- index). ifoldrM :: (PrimMonad m, MVector v a) => (Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator (action applied -- to each element and its index). ifoldrM' :: (PrimMonad m, MVector v a) => (Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b -- | Compute the next (lexicographically) permutation of given vector -- in-place. Returns False when input is the last permutation nextPermutation :: (PrimMonad m, Ord e, MVector v e) => v (PrimState m) e -> m Bool -- | Set all elements of the vector to the given value. set :: (PrimMonad m, MVector v a) => v (PrimState m) a -> a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. copy :: (PrimMonad m, MVector v a) => v (PrimState m) a -> v (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length. -- -- If the vectors do not overlap, then this is equivalent to copy. -- Otherwise, the copying is performed as if the source vector were -- copied to a temporary vector and then the temporary vector was copied -- to the target vector. move :: (PrimMonad m, MVector v a) => v (PrimState m) a -> v (PrimState m) a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. This is not checked. unsafeCopy :: (PrimMonad m, MVector v a) => v (PrimState m) a -> v (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length, but this is not checked. -- -- If the vectors do not overlap, then this is equivalent to -- unsafeCopy. Otherwise, the copying is performed as if the -- source vector were copied to a temporary vector and then the temporary -- vector was copied to the target vector. unsafeMove :: (PrimMonad m, MVector v a) => v (PrimState m) a -> v (PrimState m) a -> m () mstream :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a mstreamR :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a -- | Create a new mutable vector and fill it with elements from the -- Bundle. The vector will grow exponentially if the maximum size -- of the Bundle is unknown. unstream :: (PrimMonad m, MVector v a) => Bundle u a -> m (v (PrimState m) a) -- | Create a new mutable vector and fill it with elements from the -- Bundle from right to left. The vector will grow exponentially -- if the maximum size of the Bundle is unknown. unstreamR :: (PrimMonad m, MVector v a) => Bundle u a -> m (v (PrimState m) a) -- | Create a new mutable vector and fill it with elements from the -- Bundle. The vector will grow exponentially if the maximum size -- of the Bundle is unknown. vunstream :: (PrimMonad m, Vector v a) => Bundle v a -> m (Mutable v (PrimState m) a) -- | Create a new mutable vector and fill it with elements from the monadic -- stream. The vector will grow exponentially if the maximum size of the -- stream is unknown. munstream :: (PrimMonad m, MVector v a) => MBundle m u a -> m (v (PrimState m) a) -- | Create a new mutable vector and fill it with elements from the monadic -- stream from right to left. The vector will grow exponentially if the -- maximum size of the stream is unknown. munstreamR :: (PrimMonad m, MVector v a) => MBundle m u a -> m (v (PrimState m) a) transform :: (PrimMonad m, MVector v a) => (Stream m a -> Stream m a) -> v (PrimState m) a -> m (v (PrimState m) a) transformR :: (PrimMonad m, MVector v a) => (Stream m a -> Stream m a) -> v (PrimState m) a -> m (v (PrimState m) a) fill :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a -> m (v (PrimState m) a) fillR :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a -> m (v (PrimState m) a) unsafeAccum :: (PrimMonad m, MVector v a) => (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m () accum :: (PrimMonad m, MVector v a) => (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m () unsafeUpdate :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Bundle u (Int, a) -> m () update :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Bundle u (Int, a) -> m () reverse :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m () unstablePartition :: forall m v a. (PrimMonad m, MVector v a) => (a -> Bool) -> v (PrimState m) a -> m Int unstablePartitionBundle :: (PrimMonad m, MVector v a) => (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a) partitionBundle :: (PrimMonad m, MVector v a) => (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a) partitionWithBundle :: (PrimMonad m, MVector v a, MVector v b, MVector v c) => (a -> Either b c) -> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c) -- | Purely functional interface to initialisation of mutable vectors module Data.Vector.Generic.New data New v a New :: (forall s. ST s (Mutable v s a)) -> New v a create :: (forall s. ST s (Mutable v s a)) -> New v a run :: New v a -> ST s (Mutable v s a) runPrim :: PrimMonad m => New v a -> m (Mutable v (PrimState m) a) apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a modifyWithBundle :: (forall s. Mutable v s a -> Bundle u b -> ST s ()) -> New v a -> Bundle u b -> New v a unstream :: Vector v a => Bundle v a -> New v a transform :: Vector v a => (forall m. Monad m => Stream m a -> Stream m a) -> (Size -> Size) -> New v a -> New v a unstreamR :: Vector v a => Bundle v a -> New v a transformR :: Vector v a => (forall m. Monad m => Stream m a -> Stream m a) -> (Size -> Size) -> New v a -> New v a slice :: Vector v a => Int -> Int -> New v a -> New v a init :: Vector v a => New v a -> New v a tail :: Vector v a => New v a -> New v a take :: Vector v a => Int -> New v a -> New v a drop :: Vector v a => Int -> New v a -> New v a unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a unsafeInit :: Vector v a => New v a -> New v a unsafeTail :: Vector v a => New v a -> New v a -- | Generic interface to pure vectors. module Data.Vector.Generic -- | Class of immutable vectors. Every immutable vector is associated with -- its mutable version through the Mutable type family. Methods of -- this class should not be used directly. Instead, -- Data.Vector.Generic and other Data.Vector modules provide safe -- and fusible wrappers. -- -- Minimum complete implementation: -- --
-- 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 is not what we want! -- -- With basicUnsafeIndexM, we can do -- --
-- copy mv v ... = ... case basicUnsafeIndexM v i of -- Box x -> unsafeWrite mv i x ... ---- -- which does not have this problem because indexing (but not the -- returned element!) is evaluated immediately. basicUnsafeIndexM :: (Vector v a, Monad m) => v a -> Int -> m a -- | Assumed complexity: O(n) -- -- Copy an immutable vector into a mutable one. The two vectors must have -- the same length but this is not checked. -- -- Instances of Vector should redefine this method if they wish to -- support an efficient block copy operation. -- -- Default definition: copying basic on basicUnsafeIndexM and -- basicUnsafeWrite. basicUnsafeCopy :: (Vector v a, PrimMonad m) => Mutable v (PrimState m) a -> v a -> m () -- | Evaluate a as far as storing it in a vector would and yield -- b. The v a argument only fixes the type and is not -- touched. The method is only used for optimisation purposes. Thus, it -- is safe for instances of Vector to evaluate a less -- than it would be when stored in a vector although this might result in -- suboptimal code. -- --
-- elemseq v x y = (singleton x `asTypeOf` v) `seq` y ---- -- Default defintion: a is not evaluated at all elemseq :: Vector v a => v a -> a -> b -> b -- | Mutable v s a is the mutable version of the pure vector type -- v a with the state token s. It is injective on GHC 8 -- and newer. type family Mutable (v :: * -> *) = (mv :: * -> * -> *) | mv -> v -- | O(1) Yield the length of the vector length :: Vector v a => v a -> Int -- | O(1) Test whether a vector is empty null :: Vector v a => v a -> Bool -- | O(1) Indexing (!) :: Vector v a => v a -> Int -> a infixl 9 ! -- | O(1) Safe indexing (!?) :: Vector v a => v a -> Int -> Maybe a infixl 9 !? -- | O(1) First element head :: Vector v a => v a -> a -- | O(1) Last element last :: Vector v a => v a -> a -- | O(1) Unsafe indexing without bounds checking unsafeIndex :: Vector v a => v a -> Int -> a -- | O(1) First element without checking if the vector is empty unsafeHead :: Vector v a => v a -> a -- | O(1) Last element without checking if the vector is empty unsafeLast :: Vector v a => v a -> a -- | O(1) Indexing in a monad. -- -- The monad allows operations to be strict in the vector when necessary. -- Suppose vector copying is implemented like this: -- --
-- copy mv v = ... write mv i (v ! i) ... ---- -- For lazy vectors, v ! i would not be evaluated which means -- that mv would unnecessarily retain a reference to v -- in each element written. -- -- With indexM, copying can be implemented like this instead: -- --
-- copy mv v = ... do -- x <- indexM v i -- write mv i x ---- -- Here, no references to v are retained because indexing (but -- not the elements) is evaluated eagerly. indexM :: (Vector v a, Monad m) => v a -> Int -> m a -- | O(1) First element of a vector in a monad. See indexM -- for an explanation of why this is useful. headM :: (Vector v a, Monad m) => v a -> m a -- | O(1) Last element of a vector in a monad. See indexM for -- an explanation of why this is useful. lastM :: (Vector v a, Monad m) => v a -> m a -- | O(1) Indexing in a monad without bounds checks. See -- indexM for an explanation of why this is useful. unsafeIndexM :: (Vector v a, Monad m) => v a -> Int -> m a -- | O(1) First element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeHeadM :: (Vector v a, Monad m) => v a -> m a -- | O(1) Last element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeLastM :: (Vector v a, Monad m) => v a -> m a -- | O(1) Yield a slice of the vector without copying it. The vector -- must contain at least i+n elements. slice :: Vector v a => Int -> Int -> v a -> v a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty. init :: Vector v a => v a -> v a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty. tail :: Vector v a => v a -> v a -- | O(1) Yield the first n elements without copying. The -- vector may contain less than n elements in which case it is -- returned unchanged. take :: Vector v a => Int -> v a -> v a -- | O(1) Yield all but the first n elements without -- copying. The vector may contain less than n elements in which -- case an empty vector is returned. drop :: Vector v a => Int -> v a -> v a -- | O(1) Yield the first n elements paired with the -- remainder without copying. -- -- Note that splitAt n v is equivalent to -- (take n v, drop n v) but slightly more -- efficient. splitAt :: Vector v a => Int -> v a -> (v a, v a) -- | O(1) Yield the head and tail of the vector, or -- Nothing if empty. uncons :: Vector v a => v a -> Maybe (a, v a) -- | O(1) Yield the last and init of the vector, or -- Nothing if empty. unsnoc :: Vector v a => v a -> Maybe (v a, a) -- | O(1) Yield a slice of the vector without copying. The vector -- must contain at least i+n elements but this is not checked. unsafeSlice :: Vector v a => Int -> Int -> v a -> v a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty but this is not checked. unsafeInit :: Vector v a => v a -> v a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty but this is not checked. unsafeTail :: Vector v a => v a -> v a -- | O(1) Yield the first n elements without copying. The -- vector must contain at least n elements but this is not -- checked. unsafeTake :: Vector v a => Int -> v a -> v a -- | O(1) Yield all but the first n elements without -- copying. The vector must contain at least n elements but this -- is not checked. unsafeDrop :: Vector v a => Int -> v a -> v a -- | O(1) Empty vector empty :: Vector v a => v a -- | O(1) Vector with exactly one element singleton :: forall v a. Vector v a => a -> v a -- | O(n) Vector of the given length with the same value in each -- position replicate :: forall v a. Vector v a => Int -> a -> v a -- | O(n) Construct a vector of the given length by applying the -- function to each index generate :: Vector v a => Int -> (Int -> a) -> v a -- | O(n) Apply function <math> times to an initial value, -- producing a vector of length <math>. Zeroth element will contain -- the initial value, that's why there is one less function application -- than the number of elements in the produced vector. -- -- <math> iterateN :: Vector v a => Int -> (a -> a) -> a -> v a -- | O(n) Execute the monadic action the given number of times and -- store the results in a vector. replicateM :: (Monad m, Vector v a) => Int -> m a -> m (v a) -- | O(n) Construct a vector of the given length by applying the -- monadic action to each index generateM :: (Monad m, Vector v a) => Int -> (Int -> m a) -> m (v a) -- | O(n) Apply monadic function <math> times to an initial -- value, producing a vector of length <math>. Zeroth element will -- contain the initial value, that's why there is one less function -- application than the number of elements in the produced vector. -- -- For non-monadic version see iterateN iterateNM :: (Monad m, Vector v a) => Int -> (a -> m a) -> a -> m (v a) -- | Execute the monadic action and freeze the resulting vector. -- --
-- create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>
--
create :: Vector v a => (forall s. ST s (Mutable v s a)) -> v a
-- | Execute the monadic action and freeze the resulting vectors.
createT :: (Traversable f, Vector v a) => (forall s. ST s (f (Mutable v s a))) -> f (v a)
-- | O(n) Construct a vector by repeatedly applying the generator
-- function to a seed. The generator function yields Just the next
-- element and the new seed or Nothing if there are no more
-- elements.
--
-- -- unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 -- = <10,9,8,7,6,5,4,3,2,1> --unfoldr :: Vector v a => (b -> Maybe (a, b)) -> b -> v a -- | O(n) Construct a vector with at most n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields Just the next element and the new seed or -- Nothing if there are no more elements. -- --
-- unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8> --unfoldrN :: Vector v a => Int -> (b -> Maybe (a, b)) -> b -> v a -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields the next element and the new seed. -- --
-- unfoldrExactN 3 (\n -> (n,n-1)) 10 = <10,9,8> --unfoldrExactN :: Vector v a => Int -> (b -> (a, b)) -> b -> v a -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrM :: (Monad m, Vector v a) => (b -> m (Maybe (a, b))) -> b -> m (v a) -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrNM :: (Monad m, Vector v a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (v a) -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the monadic generator function to a seed. The -- generator function yields the next element and the new seed. unfoldrExactNM :: (Monad m, Vector v a) => Int -> (b -> m (a, b)) -> b -> m (v a) -- | O(n) Construct a vector with n elements by repeatedly -- applying the generator function to the already constructed part of the -- vector. -- --
-- constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in <a,b,c> --constructN :: forall v a. Vector v a => Int -> (v a -> a) -> v a -- | O(n) Construct a vector with n elements from right to -- left by repeatedly applying the generator function to the already -- constructed part of the vector. -- --
-- constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in <c,b,a> --constructrN :: forall v a. Vector v a => Int -> (v a -> a) -> v a -- | O(n) Yield a vector of the given length containing the values -- x, x+1 etc. This operation is usually more efficient -- than enumFromTo. -- --
-- enumFromN 5 3 = <5,6,7> --enumFromN :: (Vector v a, Num a) => a -> Int -> v a -- | O(n) 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. -- --
-- enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4> --enumFromStepN :: forall v a. (Vector v a, Num a) => a -> a -> Int -> v a -- | O(n) Enumerate values from x to y. -- -- WARNING: This operation can be very inefficient. If at all -- possible, use enumFromN instead. enumFromTo :: (Vector v a, Enum a) => a -> a -> v a -- | O(n) 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. enumFromThenTo :: (Vector v a, Enum a) => a -> a -> a -> v a -- | O(n) Prepend an element cons :: forall v a. Vector v a => a -> v a -> v a -- | O(n) Append an element snoc :: forall v a. Vector v a => v a -> a -> v a -- | O(m+n) Concatenate two vectors (++) :: Vector v a => v a -> v a -> v a infixr 5 ++ -- | O(n) Concatenate all vectors in the list concat :: Vector v a => [v a] -> v a -- | O(n) Concatenate all vectors in the non-empty list concatNE :: Vector v a => NonEmpty (v a) -> v a -- | O(n) Yield the argument but force it not to retain any extra -- memory, possibly by copying it. -- -- This is especially useful when dealing with slices. For example: -- --
-- force (slice 0 2 <huge vector>) ---- -- Here, the slice retains a reference to the huge vector. Forcing it -- creates a copy of just the elements that belong to the slice and -- allows the huge vector to be garbage collected. force :: Vector v a => v a -> v a -- | O(m+n) For each pair (i,a) from the list, replace the -- vector element at position i by a. -- --
-- <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7> --(//) :: Vector v a => v a -> [(Int, a)] -> v a -- | O(m+n) For each pair (i,a) from the vector of -- index/value pairs, replace the vector element at position i -- by a. -- --
-- update <5,9,2,7> <(2,1),(0,3),(2,8)> = <3,9,8,7> --update :: (Vector v a, Vector v (Int, a)) => v a -> v (Int, a) -> v a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value a from the value vector, replace -- the element of the initial vector at position i by -- a. -- --
-- update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7> ---- -- This function is useful for instances of Vector that cannot -- store pairs. Otherwise, update is probably more convenient. -- --
-- update_ xs is ys = update xs (zip is ys) --update_ :: (Vector v a, Vector v Int) => v a -> v Int -> v a -> v a -- | Same as (//) but without bounds checking. unsafeUpd :: Vector v a => v a -> [(Int, a)] -> v a -- | Same as update but without bounds checking. unsafeUpdate :: (Vector v a, Vector v (Int, a)) => v a -> v (Int, a) -> v a -- | Same as update_ but without bounds checking. unsafeUpdate_ :: (Vector v a, Vector v Int) => v a -> v Int -> v a -> v a -- | O(m+n) For each pair (i,b) from the list, replace the -- vector element a at position i by f a b. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.accum (+) (V.fromList [1000.0,2000.0,3000.0]) [(2,4),(1,6),(0,3),(1,10)] -- [1003.0,2016.0,3004.0] --accum :: Vector v a => (a -> b -> a) -> v a -> [(Int, b)] -> v a -- | O(m+n) For each pair (i,b) from the vector of pairs, -- replace the vector element a at position i by f -- a b. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.accumulate (+) (V.fromList [1000.0,2000.0,3000.0]) (V.fromList [(2,4),(1,6),(0,3),(1,10)]) -- [1003.0,2016.0,3004.0] --accumulate :: (Vector v a, Vector v (Int, b)) => (a -> b -> a) -> v a -> v (Int, b) -> v a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value b from the the value vector, -- replace the element of the initial vector at position i by -- f a b. -- --
-- accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4> ---- -- This function is useful for instances of Vector that cannot -- store pairs. Otherwise, accumulate is probably more convenient: -- --
-- accumulate_ f as is bs = accumulate f as (zip is bs) --accumulate_ :: (Vector v a, Vector v Int, Vector v b) => (a -> b -> a) -> v a -> v Int -> v b -> v a -- | Same as accum but without bounds checking. unsafeAccum :: Vector v a => (a -> b -> a) -> v a -> [(Int, b)] -> v a -- | Same as accumulate but without bounds checking. unsafeAccumulate :: (Vector v a, Vector v (Int, b)) => (a -> b -> a) -> v a -> v (Int, b) -> v a -- | Same as accumulate_ but without bounds checking. unsafeAccumulate_ :: (Vector v a, Vector v Int, Vector v b) => (a -> b -> a) -> v a -> v Int -> v b -> v a -- | O(n) Reverse a vector reverse :: Vector v a => v a -> v a -- | O(n) Yield the vector obtained by replacing each element -- i of the index vector by xs!i. This is -- equivalent to map (xs!) is but is often much -- more efficient. -- --
-- backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a> --backpermute :: (Vector v a, Vector v Int) => v a -> v Int -> v a -- | Same as backpermute but without bounds checking. unsafeBackpermute :: (Vector v a, Vector v Int) => v a -> v Int -> v a -- | Apply a destructive operation to a vector. The operation will be -- performed in place if it is safe to do so and will modify a copy of -- the vector otherwise. -- --
-- modify (\v -> write v 0 'x') (replicate 3 'a') = <'x','a','a'> --modify :: Vector v a => (forall s. Mutable v s a -> ST s ()) -> v a -> v a -- | O(n) Pair each element in a vector with its index indexed :: (Vector v a, Vector v (Int, a)) => v a -> v (Int, a) -- | O(n) Map a function over a vector map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b -- | O(n) Apply a function to every element of a vector and its -- index imap :: (Vector v a, Vector v b) => (Int -> a -> b) -> v a -> v b -- | Map a function over a vector and concatenate the results. concatMap :: (Vector v a, Vector v b) => (a -> v b) -> v a -> v b -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results mapM :: (Monad m, Vector v a, Vector v b) => (a -> m b) -> v a -> m (v b) -- | O(n) Apply the monadic action to every element of a vector and -- its index, yielding a vector of results imapM :: (Monad m, Vector v a, Vector v b) => (Int -> a -> m b) -> v a -> m (v b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results mapM_ :: (Monad m, Vector v a) => (a -> m b) -> v a -> m () -- | O(n) Apply the monadic action to every element of a vector and -- its index, ignoring the results imapM_ :: (Monad m, Vector v a) => (Int -> a -> m b) -> v a -> m () -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results. Equivalent to flip mapM. forM :: (Monad m, Vector v a, Vector v b) => v a -> (a -> m b) -> m (v b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results. Equivalent to flip mapM_. forM_ :: (Monad m, Vector v a) => v a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to all elements of the vector and -- their indices, yielding a vector of results. Equivalent to flip -- imapM. iforM :: (Monad m, Vector v a, Vector v b) => v a -> (Int -> a -> m b) -> m (v b) -- | O(n) Apply the monadic action to all elements of the vector and -- their indices and ignore the results. Equivalent to flip -- imapM_. iforM_ :: (Monad m, Vector v a) => v a -> (Int -> a -> m b) -> m () -- | O(min(m,n)) Zip two vectors with the given function. zipWith :: (Vector v a, Vector v b, Vector v c) => (a -> b -> c) -> v a -> v b -> v c -- | Zip three vectors with the given function. zipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d) => (a -> b -> c -> d) -> v a -> v b -> v c -> v d zipWith4 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e) => (a -> b -> c -> d -> e) -> v a -> v b -> v c -> v d -> v e zipWith5 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f) => (a -> b -> c -> d -> e -> f) -> v a -> v b -> v c -> v d -> v e -> v f zipWith6 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f, Vector v g) => (a -> b -> c -> d -> e -> f -> g) -> v a -> v b -> v c -> v d -> v e -> v f -> v g -- | O(min(m,n)) Zip two vectors with a function that also takes the -- elements' indices. izipWith :: (Vector v a, Vector v b, Vector v c) => (Int -> a -> b -> c) -> v a -> v b -> v c izipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d) => (Int -> a -> b -> c -> d) -> v a -> v b -> v c -> v d izipWith4 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e) => (Int -> a -> b -> c -> d -> e) -> v a -> v b -> v c -> v d -> v e izipWith5 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f) => (Int -> a -> b -> c -> d -> e -> f) -> v a -> v b -> v c -> v d -> v e -> v f izipWith6 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f, Vector v g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> v a -> v b -> v c -> v d -> v e -> v f -> v g -- | O(min(m,n)) Zip two vectors zip :: (Vector v a, Vector v b, Vector v (a, b)) => v a -> v b -> v (a, b) zip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => v a -> v b -> v c -> v (a, b, c) zip4 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v (a, b, c, d)) => v a -> v b -> v c -> v d -> v (a, b, c, d) zip5 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v (a, b, c, d, e)) => v a -> v b -> v c -> v d -> v e -> v (a, b, c, d, e) zip6 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f, Vector v (a, b, c, d, e, f)) => v a -> v b -> v c -> v d -> v e -> v f -> v (a, b, c, d, e, f) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- yield a vector of results zipWithM :: (Monad m, Vector v a, Vector v b, Vector v c) => (a -> b -> m c) -> v a -> v b -> m (v c) -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and yield a vector of results izipWithM :: (Monad m, Vector v a, Vector v b, Vector v c) => (Int -> a -> b -> m c) -> v a -> v b -> m (v c) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- ignore the results zipWithM_ :: (Monad m, Vector v a, Vector v b) => (a -> b -> m c) -> v a -> v b -> m () -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and ignore the results izipWithM_ :: (Monad m, Vector v a, Vector v b) => (Int -> a -> b -> m c) -> v a -> v b -> m () -- | O(min(m,n)) Unzip a vector of pairs. unzip :: (Vector v a, Vector v b, Vector v (a, b)) => v (a, b) -> (v a, v b) unzip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => v (a, b, c) -> (v a, v b, v c) unzip4 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v (a, b, c, d)) => v (a, b, c, d) -> (v a, v b, v c, v d) unzip5 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v (a, b, c, d, e)) => v (a, b, c, d, e) -> (v a, v b, v c, v d, v e) unzip6 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v e, Vector v f, Vector v (a, b, c, d, e, f)) => v (a, b, c, d, e, f) -> (v a, v b, v c, v d, v e, v f) -- | O(n) Drop elements that do not satisfy the predicate filter :: Vector v a => (a -> Bool) -> v a -> v a -- | O(n) Drop elements that do not satisfy the predicate which is -- applied to values and their indices ifilter :: Vector v a => (Int -> a -> Bool) -> v a -> v a -- | O(n) Drop elements that do not satisfy the monadic predicate filterM :: (Monad m, Vector v a) => (a -> m Bool) -> v a -> m (v a) -- | O(n) Drop repeated adjacent elements. uniq :: (Vector v a, Eq a) => v a -> v a -- | O(n) Drop elements when predicate returns Nothing mapMaybe :: (Vector v a, Vector v b) => (a -> Maybe b) -> v a -> v b -- | O(n) Drop elements when predicate, applied to index and value, -- returns Nothing imapMaybe :: (Vector v a, Vector v b) => (Int -> a -> Maybe b) -> v a -> v b -- | O(n) Apply monadic function to each element of vector and -- discard elements returning Nothing. mapMaybeM :: (Monad m, Vector v a, Vector v b) => (a -> m (Maybe b)) -> v a -> m (v b) -- | O(n) Apply monadic function to each element of vector and its -- index. Discards elements returning Nothing. imapMaybeM :: (Monad m, Vector v a, Vector v b) => (Int -> a -> m (Maybe b)) -> v a -> m (v b) -- | O(n) Yield the longest prefix of elements satisfying the -- predicate. Current implementation is not copy-free, unless the result -- vector is fused away. takeWhile :: Vector v a => (a -> Bool) -> v a -> v a -- | O(n) Drop the longest prefix of elements that satisfy the -- predicate without copying. dropWhile :: Vector v a => (a -> Bool) -> v a -> v a -- | O(n) 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. partition :: Vector v a => (a -> Bool) -> v a -> (v a, v a) -- | O(n) Split the vector into two parts, the first one containing -- the Left elements and the second containing the -- Right elements. The relative order of the elements is -- preserved. partitionWith :: (Vector v a, Vector v b, Vector v c) => (a -> Either b c) -> v a -> (v b, v c) -- | O(n) 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 but the -- operation is often faster than partition. unstablePartition :: Vector v a => (a -> Bool) -> v a -> (v a, v a) -- | O(n) Split the vector into the longest prefix of elements that -- satisfy the predicate and the rest without copying. span :: Vector v a => (a -> Bool) -> v a -> (v a, v a) -- | O(n) Split the vector into the longest prefix of elements that -- do not satisfy the predicate and the rest without copying. break :: Vector v a => (a -> Bool) -> v a -> (v a, v a) -- | O(n) Check if the vector contains an element elem :: (Vector v a, Eq a) => a -> v a -> Bool infix 4 `elem` -- | O(n) Check if the vector does not contain an element (inverse -- of elem) notElem :: (Vector v a, Eq a) => a -> v a -> Bool infix 4 `notElem` -- | O(n) Yield Just the first element matching the predicate -- or Nothing if no such element exists. find :: Vector v a => (a -> Bool) -> v a -> Maybe a -- | O(n) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: Vector v a => (a -> Bool) -> v a -> Maybe Int -- | O(n) Yield Just the index of the last element -- matching the predicate or Nothing if no such element exists. findIndexR :: Vector v a => (a -> Bool) -> v a -> Maybe Int -- | O(n) Yield the indices of elements satisfying the predicate in -- ascending order. findIndices :: (Vector v a, Vector v Int) => (a -> Bool) -> v a -> v Int -- | O(n) Yield Just the index of the first occurence of the -- given element or Nothing if the vector does not contain the -- element. This is a specialised version of findIndex. elemIndex :: (Vector v a, Eq a) => a -> v a -> Maybe Int -- | O(n) Yield the indices of all occurences of the given element -- in ascending order. This is a specialised version of -- findIndices. elemIndices :: (Vector v a, Vector v Int, Eq a) => a -> v a -> v Int -- | O(n) Left fold foldl :: Vector v b => (a -> b -> a) -> a -> v b -> a -- | O(n) Left fold on non-empty vectors foldl1 :: Vector v a => (a -> a -> a) -> v a -> a -- | O(n) Left fold with strict accumulator foldl' :: Vector v b => (a -> b -> a) -> a -> v b -> a -- | O(n) Left fold on non-empty vectors with strict accumulator foldl1' :: Vector v a => (a -> a -> a) -> v a -> a -- | O(n) Right fold foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b -- | O(n) Right fold on non-empty vectors foldr1 :: Vector v a => (a -> a -> a) -> v a -> a -- | O(n) Right fold with a strict accumulator foldr' :: Vector v a => (a -> b -> b) -> b -> v a -> b -- | O(n) Right fold on non-empty vectors with strict accumulator foldr1' :: Vector v a => (a -> a -> a) -> v a -> a -- | O(n) Left fold (function applied to each element and its index) ifoldl :: Vector v b => (a -> Int -> b -> a) -> a -> v b -> a -- | O(n) Left fold with strict accumulator (function applied to -- each element and its index) ifoldl' :: Vector v b => (a -> Int -> b -> a) -> a -> v b -> a -- | O(n) Right fold (function applied to each element and its -- index) ifoldr :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b -- | O(n) Right fold with strict accumulator (function applied to -- each element and its index) ifoldr' :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b -- | O(n) Map each element of the structure to a monoid, and combine -- the results. It uses same implementation as corresponding method of -- Foldable type cless. Note it's implemented in terms of -- foldr and won't fuse with functions that traverse vector from -- left to right (map, generate, etc.). foldMap :: (Monoid m, Vector v a) => (a -> m) -> v a -> m -- | O(n) foldMap which is strict in accumulator. It uses -- same implementation as corresponding method of Foldable type -- class. Note it's implemented in terms of foldl' so it fuses in -- most contexts. foldMap' :: (Monoid m, Vector v a) => (a -> m) -> v a -> m -- | O(n) Check if all elements satisfy the predicate. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.all even $ V.fromList [2, 4, 12 :: Int] -- True -- -- >>> V.all even $ V.fromList [2, 4, 13 :: Int] -- False -- -- >>> V.all even (V.empty :: V.Vector Int) -- True --all :: Vector v a => (a -> Bool) -> v a -> Bool -- | O(n) Check if any element satisfies the predicate. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.any even $ V.fromList [1, 3, 7 :: Int] -- False -- -- >>> V.any even $ V.fromList [3, 2, 13 :: Int] -- True -- -- >>> V.any even (V.empty :: V.Vector Int) -- False --any :: Vector v a => (a -> Bool) -> v a -> Bool -- | O(n) Check if all elements are True -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.and $ V.fromList [True, False] -- False -- -- >>> V.and V.empty -- True --and :: Vector v Bool => v Bool -> Bool -- | O(n) Check if any element is True -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.or $ V.fromList [True, False] -- True -- -- >>> V.or V.empty -- False --or :: Vector v Bool => v Bool -> Bool -- | O(n) Compute the sum of the elements -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.sum $ V.fromList [300,20,1 :: Int] -- 321 -- -- >>> V.sum (V.empty :: V.Vector Int) -- 0 --sum :: (Vector v a, Num a) => v a -> a -- | O(n) Compute the produce of the elements -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.product $ V.fromList [1,2,3,4 :: Int] -- 24 -- -- >>> V.product (V.empty :: V.Vector Int) -- 1 --product :: (Vector v a, Num a) => v a -> a -- | O(n) Yield the maximum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.maximum $ V.fromList [2.0, 1.0] -- 2.0 --maximum :: (Vector v a, Ord a) => v a -> a -- | O(n) Yield the maximum element of the vector according to the -- given comparison function. The vector may not be empty. maximumBy :: Vector v a => (a -> a -> Ordering) -> v a -> a -- | O(n) Yield the minimum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.minimum $ V.fromList [2.0, 1.0] -- 1.0 --minimum :: (Vector v a, Ord a) => v a -> a -- | O(n) Yield the minimum element of the vector according to the -- given comparison function. The vector may not be empty. minimumBy :: Vector v a => (a -> a -> Ordering) -> v a -> a -- | O(n) Yield the index of the minimum element of the vector. The -- vector may not be empty. minIndex :: (Vector v a, Ord a) => v a -> Int -- | O(n) Yield the index of the minimum element of the vector -- according to the given comparison function. The vector may not be -- empty. minIndexBy :: Vector v a => (a -> a -> Ordering) -> v a -> Int -- | O(n) Yield the index of the maximum element of the vector. The -- vector may not be empty. maxIndex :: (Vector v a, Ord a) => v a -> Int -- | O(n) Yield the index of the maximum element of the vector -- according to the given comparison function. The vector may not be -- empty. maxIndexBy :: Vector v a => (a -> a -> Ordering) -> v a -> Int -- | O(n) Monadic fold foldM :: (Monad m, Vector v b) => (a -> b -> m a) -> a -> v b -> m a -- | O(n) Monadic fold (action applied to each element and its -- index) ifoldM :: (Monad m, Vector v b) => (a -> Int -> b -> m a) -> a -> v b -> m a -- | O(n) Monadic fold with strict accumulator foldM' :: (Monad m, Vector v b) => (a -> b -> m a) -> a -> v b -> m a -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index) ifoldM' :: (Monad m, Vector v b) => (a -> Int -> b -> m a) -> a -> v b -> m a -- | O(n) Monadic fold over non-empty vectors fold1M :: (Monad m, Vector v a) => (a -> a -> m a) -> v a -> m a -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator fold1M' :: (Monad m, Vector v a) => (a -> a -> m a) -> v a -> m a -- | O(n) Monadic fold that discards the result foldM_ :: (Monad m, Vector v b) => (a -> b -> m a) -> a -> v b -> m () -- | O(n) Monadic fold that discards the result (action applied to -- each element and its index) ifoldM_ :: (Monad m, Vector v b) => (a -> Int -> b -> m a) -> a -> v b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result foldM'_ :: (Monad m, Vector v b) => (a -> b -> m a) -> a -> v b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result (action applied to each element and its index) ifoldM'_ :: (Monad m, Vector v b) => (a -> Int -> b -> m a) -> a -> v b -> m () -- | O(n) Monadic fold over non-empty vectors that discards the -- result fold1M_ :: (Monad m, Vector v a) => (a -> a -> m a) -> v a -> m () -- | O(n) Monad fold over non-empty vectors with strict accumulator -- that discards the result fold1M'_ :: (Monad m, Vector v a) => (a -> a -> m a) -> v a -> m () -- | Evaluate each action and collect the results sequence :: (Monad m, Vector v a, Vector v (m a)) => v (m a) -> m (v a) -- | Evaluate each action and discard the results sequence_ :: (Monad m, Vector v (m a)) => v (m a) -> m () -- | O(n) Prescan -- --
-- prescanl f z = init . scanl f z ---- -- Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6> prescanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Prescan with strict accumulator prescanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Scan -- --
-- postscanl f z = tail . scanl f z ---- -- Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10> postscanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Scan with strict accumulator postscanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Haskell-style scan -- --
-- 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 :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Haskell-style scan with strict accumulator scanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a -- | O(n) Scan over a non-empty vector -- --
-- scanl f <x1,...,xn> = <y1,...,yn> -- where y1 = x1 -- yi = f y(i-1) xi --scanl1 :: Vector v a => (a -> a -> a) -> v a -> v a -- | O(n) Scan over a non-empty vector with a strict accumulator scanl1' :: Vector v a => (a -> a -> a) -> v a -> v a -- | O(n) Scan over a vector with its index iscanl :: (Vector v a, Vector v b) => (Int -> a -> b -> a) -> a -> v b -> v a -- | O(n) Scan over a vector (strictly) with its index iscanl' :: (Vector v a, Vector v b) => (Int -> a -> b -> a) -> a -> v b -> v a -- | O(n) Right-to-left prescan -- --
-- prescanr f z = reverse . prescanl (flip f) z . reverse --prescanr :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left prescan with strict accumulator prescanr' :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left scan postscanr :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left scan with strict accumulator postscanr' :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left Haskell-style scan scanr :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left Haskell-style scan with strict accumulator scanr' :: (Vector v a, Vector v b) => (a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left scan over a non-empty vector scanr1 :: Vector v a => (a -> a -> a) -> v a -> v a -- | O(n) Right-to-left scan over a non-empty vector with a strict -- accumulator scanr1' :: Vector v a => (a -> a -> a) -> v a -> v a -- | O(n) Right-to-left scan over a vector with its index iscanr :: (Vector v a, Vector v b) => (Int -> a -> b -> b) -> b -> v a -> v b -- | O(n) Right-to-left scan over a vector (strictly) with its index iscanr' :: (Vector v a, Vector v b) => (Int -> a -> b -> b) -> b -> v a -> v b -- | O(n) Convert a vector to a list toList :: Vector v a => v a -> [a] -- | O(n) Convert a list to a vector fromList :: Vector v a => [a] -> v a -- | O(n) Convert the first n elements of a list to a -- vector -- --
-- fromListN n xs = fromList (take n xs) ---- --
-- >>> import qualified Data.Vector as V -- -- >>> V.fromListN 3 [1,2,3,4,5::Int] -- [1,2,3] -- -- >>> V.fromListN 3 [1::Int] -- [1] --fromListN :: Vector v a => Int -> [a] -> v a -- | O(n) Convert different vector types convert :: (Vector v a, Vector w a) => v a -> w a -- | O(n) Yield an immutable copy of the mutable vector. freeze :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m (v a) -- | O(n) Yield a mutable copy of the immutable vector. thaw :: (PrimMonad m, Vector v a) => v a -> m (Mutable v (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. copy :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> v a -> m () -- | O(1) Unsafe convert a mutable vector to an immutable one -- without copying. The mutable vector may not be used after this -- operation. unsafeFreeze :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m (v a) -- | O(1) Unsafely convert an immutable vector to a mutable one -- without copying. Note that this is very dangerous function and -- generally it's only safe to read from resulting vector. In which case -- immutable vector could be used safely as well. -- -- Problem with mutation happens because GHC has a lot of freedom to -- introduce sharing. As a result mutable vectors produced by -- unsafeThaw may or may not share same underlying buffer. For -- example: -- --
-- foo = do -- let vec = V.generate 10 id -- mvec <- V.unsafeThaw vec -- do_something mvec ---- -- Here GHC could lift vec outside of foo which means all calls -- to do_something will use same buffer with possibly disastrous -- results. Whether such aliasing happens or not depends on program in -- question, optimization levels, and GHC flags. -- -- All in all attempts to modify vector after unsafeThaw falls out of -- domain of software engineering and into realm of black magic, dark -- rituals, and unspeakable horrors. Only advice that could be given is: -- "don't attempt to mutate vector after unsafeThaw unless you know how -- to prevent GHC from aliasing buffers accidentally. We don't" unsafeThaw :: (PrimMonad m, Vector v a) => v a -> m (Mutable v (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. This is not checked. unsafeCopy :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> v a -> m () -- | O(1) Convert a vector to a Bundle stream :: Vector v a => v a -> Bundle v a -- | O(n) Construct a vector from a Bundle unstream :: Vector v a => Bundle v a -> v a -- | Load monadic stream bundle into a newly allocated vector. This -- function goes through a list, so prefer using unstream, unless -- you need to be in a monad. unstreamM :: (Monad m, Vector v a) => MBundle m u a -> m (v a) -- | O(1) Convert a vector to a Bundle, proceeding from right -- to left streamR :: Vector v a => v a -> Bundle u a -- | O(n) Construct a vector from a Bundle, proceeding from -- right to left unstreamR :: Vector v a => Bundle v a -> v a -- | Construct a vector from a monadic initialiser. new :: Vector v a => New v a -> v a -- | Convert a vector to an initialiser which, when run, produces a copy of -- the vector. clone :: Vector v a => v a -> New v a -- | O(n) Check if two vectors are equal. All Vector -- instances are also instances of Eq and it is usually more -- appropriate to use those. This function is primarily intended for -- implementing Eq instances for new vector types. eq :: (Vector v a, Eq a) => v a -> v a -> Bool -- | O(n) Compare two vectors lexicographically. All Vector -- instances are also instances of Ord and it is usually more -- appropriate to use those. This function is primarily intended for -- implementing Ord instances for new vector types. cmp :: (Vector v a, Ord a) => v a -> v a -> Ordering -- | O(n) Check if two vectors are equal using supplied equality -- predicate. eqBy :: (Vector v a, Vector v b) => (a -> b -> Bool) -> v a -> v b -> Bool -- | O(n) Compare two vectors using supplied comparison function for -- vector elements. Comparison works same as for lists. -- --
-- cmpBy compare == cmp --cmpBy :: (Vector v a, Vector v b) => (a -> b -> Ordering) -> v a -> v b -> Ordering -- | Generic definition of showsPrec showsPrec :: (Vector v a, Show a) => Int -> v a -> ShowS -- | Generic definition of readPrec readPrec :: (Vector v a, Read a) => ReadPrec (v a) liftShowsPrec :: Vector v a => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> v a -> ShowS -- | Note: uses ReadS liftReadsPrec :: Vector v a => (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (v a) -- | Generic definion of gfoldl that views a Vector as a -- list. gfoldl :: (Vector v a, Data a) => (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> v a -> c (v a) gunfold :: (Vector v a, Data a) => (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (v a) dataCast :: (Vector v a, Data a, Typeable v, Typeable t) => (forall d. Data d => c (t d)) -> Maybe (c (v a)) mkVecType :: String -> DataType mkVecConstr :: String -> Constr mkType :: String -> DataType -- | Mutable boxed vectors. module Data.Vector.Mutable -- | Mutable boxed vectors keyed on the monad they live in (IO or -- ST s). data MVector s a MVector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !MutableArray s a -> MVector s a type IOVector = MVector RealWorld type STVector s = MVector s -- | Length of the mutable vector. length :: MVector s a -> Int -- | Check whether the vector is empty null :: MVector s a -> Bool -- | Yield a part of the mutable vector without copying it. The vector must -- contain at least i+n elements. slice :: Int -> Int -> MVector s a -> MVector s a -- | Drop last element of the mutable vector without making a copy. If -- vector is empty exception is thrown. init :: MVector s a -> MVector s a -- | Drop first element of the mutable vector without making a copy. If -- vector is empty exception is thrown. tail :: MVector s a -> MVector s a -- | Take n first elements of the mutable vector without making a -- copy. For negative n empty vector is returned. If n -- is larger than vector's length empty vector is returned, take :: Int -> MVector s a -> MVector s a -- | Drop n first element of the mutable vector without making a -- copy. For negative n vector is returned unchanged and if -- n is larger than vector's length empty vector is returned. drop :: Int -> MVector s a -> MVector s a splitAt :: Int -> MVector s a -> (MVector s a, MVector s a) -- | Yield a part of the mutable vector without copying it. No bounds -- checks are performed. unsafeSlice :: Int -> Int -> MVector s a -> MVector s a -- | Same as init but doesn't do range checks. unsafeInit :: MVector s a -> MVector s a -- | Same as tail but doesn't do range checks. unsafeTail :: MVector s a -> MVector s a -- | Unsafe variant of take. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeTake :: Int -> MVector s a -> MVector s a -- | Unsafe variant of drop. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeDrop :: Int -> MVector s a -> MVector s a -- | Check whether two vectors overlap. overlaps :: MVector s a -> MVector s a -> Bool -- | Create a mutable vector of the given length. new :: PrimMonad m => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length. The vector elements are -- set to bottom so accessing them will cause an exception. unsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with an initial value. replicate :: PrimMonad m => Int -> a -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with values produced by repeatedly executing the -- monadic action. replicateM :: PrimMonad m => Int -> m a -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- function to each index. generate :: PrimMonad m => Int -> (Int -> a) -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- monadic function to each index. Iteration starts at index 0. generateM :: PrimMonad m => Int -> (Int -> m a) -> m (MVector (PrimState m) a) -- | Create a copy of a mutable vector. clone :: PrimMonad m => MVector (PrimState m) a -> m (MVector (PrimState m) a) -- | Grow a boxed vector by the given number of elements. The number must -- be non-negative. Same semantics as in grow for generic vector. -- It differs from grow functions for unpacked vectors, however, -- in that only pointers to values are copied over, therefore values -- themselves will be shared between two vectors. This is an important -- distinction to know about during memory usage analysis and in case -- when values themselves are of a mutable type, eg. IORef or -- another mutable vector. -- --
-- >>> import qualified Data.Vector as V -- -- >>> import qualified Data.Vector.Mutable as MV -- -- >>> mv <- V.thaw $ V.fromList ([10, 20, 30] :: [Integer]) -- -- >>> mv' <- MV.grow mv 2 ---- -- The two extra elements at the end of the newly allocated vector will -- be uninitialized and will result in an error if evaluated, so me must -- overwrite them with new values first: -- --
-- >>> MV.write mv' 3 999 -- -- >>> MV.write mv' 4 777 -- -- >>> V.freeze mv' -- [10,20,30,999,777] ---- -- It is important to note that the source mutable vector is not affected -- when the newly allocated one is mutated. -- --
-- >>> MV.write mv' 2 888 -- -- >>> V.freeze mv' -- [10,20,888,999,777] -- -- >>> V.freeze mv -- [10,20,30] --grow :: PrimMonad m => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Grow a vector by the given number of elements. The number must be -- non-negative but this is not checked. Same semantics as in -- unsafeGrow for generic vector. unsafeGrow :: PrimMonad m => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Reset all elements of the vector to some undefined value, clearing all -- references to external objects. This is usually a noop for unboxed -- vectors. clear :: PrimMonad m => MVector (PrimState m) a -> m () -- | Yield the element at the given position. read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. modify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. modifyM :: PrimMonad m => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. exchange :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m a -- | Yield the element at the given position. No bounds checks are -- performed. unsafeRead :: PrimMonad m => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. No bounds checks are -- performed. unsafeWrite :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. No bounds checks are -- performed. unsafeModify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. No -- bounds checks are performed. unsafeModifyM :: PrimMonad m => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. No bounds checks are -- performed. unsafeSwap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. -- No bounds checks are performed. unsafeExchange :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m a -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. mapM_ :: PrimMonad m => (a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. imapM_ :: PrimMonad m => (Int -> a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. It's same as the flip mapM_. forM_ :: PrimMonad m => MVector (PrimState m) a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. It's same as the flip -- imapM_. iforM_ :: PrimMonad m => MVector (PrimState m) a -> (Int -> a -> m b) -> m () -- | O(n) Pure left fold. foldl :: PrimMonad m => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator. foldl' :: PrimMonad m => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold. foldM :: PrimMonad m => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator. foldM' :: PrimMonad m => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold. foldr :: PrimMonad m => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator. foldr' :: PrimMonad m => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold. foldrM :: PrimMonad m => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator. foldrM' :: PrimMonad m => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold (function applied to each element and its -- index). ifoldl :: PrimMonad m => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator (function applied -- to each element and its index). ifoldl' :: PrimMonad m => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold (action applied to each element and its -- index). ifoldM :: PrimMonad m => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index). ifoldM' :: PrimMonad m => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold (function applied to each element and its -- index). ifoldr :: PrimMonad m => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator (function applied -- to each element and its index). ifoldr' :: PrimMonad m => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold (action applied to each element and its -- index). ifoldrM :: PrimMonad m => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator (action applied -- to each element and its index). ifoldrM' :: PrimMonad m => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | Compute the next (lexicographically) permutation of given vector -- in-place. Returns False when input is the last permutation nextPermutation :: (PrimMonad m, Ord e) => MVector (PrimState m) e -> m Bool -- | Set all elements of the vector to the given value. set :: PrimMonad m => MVector (PrimState m) a -> a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. copy :: PrimMonad m => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length. -- -- If the vectors do not overlap, then this is equivalent to copy. -- Otherwise, the copying is performed as if the source vector were -- copied to a temporary vector and then the temporary vector was copied -- to the target vector. move :: PrimMonad m => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. This is not checked. unsafeCopy :: PrimMonad m => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length, but this is not checked. -- -- If the vectors do not overlap, then this is equivalent to -- unsafeCopy. Otherwise, the copying is performed as if the -- source vector were copied to a temporary vector and then the temporary -- vector was copied to the target vector. unsafeMove :: PrimMonad m => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | O(n) Make a copy of a mutable array to a new mutable vector. fromMutableArray :: PrimMonad m => MutableArray (PrimState m) a -> m (MVector (PrimState m) a) -- | O(n) Make a copy of a mutable vector into a new mutable array. toMutableArray :: PrimMonad m => MVector (PrimState m) a -> m (MutableArray (PrimState m) a) instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Mutable.MVector a -- | A library for boxed vectors (that is, polymorphic arrays capable of -- holding any Haskell value). The vectors come in two flavours: -- --
-- copy mv v = ... write mv i (v ! i) ... ---- -- For lazy vectors, v ! i would not be evaluated which means -- that mv would unnecessarily retain a reference to v -- in each element written. -- -- With indexM, copying can be implemented like this instead: -- --
-- copy mv v = ... do -- x <- indexM v i -- write mv i x ---- -- Here, no references to v are retained because indexing (but -- not the elements) is evaluated eagerly. indexM :: Monad m => Vector a -> Int -> m a -- | O(1) First element of a vector in a monad. See indexM -- for an explanation of why this is useful. headM :: Monad m => Vector a -> m a -- | O(1) Last element of a vector in a monad. See indexM for -- an explanation of why this is useful. lastM :: Monad m => Vector a -> m a -- | O(1) Indexing in a monad without bounds checks. See -- indexM for an explanation of why this is useful. unsafeIndexM :: Monad m => Vector a -> Int -> m a -- | O(1) First element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeHeadM :: Monad m => Vector a -> m a -- | O(1) Last element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeLastM :: Monad m => Vector a -> m a -- | O(1) Yield a slice of the vector without copying it. The vector -- must contain at least i+n elements. slice :: Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty. init :: Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty. tail :: Vector a -> Vector a -- | O(1) Yield at the first n elements without copying. -- The vector may contain less than n elements in which case it -- is returned unchanged. take :: Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector may contain less than n elements in which -- case an empty vector is returned. drop :: Int -> Vector a -> Vector a -- | O(1) Yield the first n elements paired with the -- remainder without copying. -- -- Note that splitAt n v is equivalent to -- (take n v, drop n v) but slightly more -- efficient. splitAt :: Int -> Vector a -> (Vector a, Vector a) -- | O(1) Yield the head and tail of the vector, or -- Nothing if empty. uncons :: Vector a -> Maybe (a, Vector a) -- | O(1) Yield the last and init of the vector, or -- Nothing if empty. unsnoc :: Vector a -> Maybe (Vector a, a) -- | O(1) Yield a slice of the vector without copying. The vector -- must contain at least i+n elements but this is not checked. unsafeSlice :: Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty but this is not checked. unsafeInit :: Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty but this is not checked. unsafeTail :: Vector a -> Vector a -- | O(1) Yield the first n elements without copying. The -- vector must contain at least n elements but this is not -- checked. unsafeTake :: Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector must contain at least n elements but this -- is not checked. unsafeDrop :: Int -> Vector a -> Vector a -- | O(1) Empty vector empty :: Vector a -- | O(1) Vector with exactly one element singleton :: a -> Vector a -- | O(n) Vector of the given length with the same value in each -- position replicate :: Int -> a -> Vector a -- | O(n) Construct a vector of the given length by applying the -- function to each index generate :: Int -> (Int -> a) -> Vector a -- | O(n) Apply function <math> times to an initial value, -- producing a vector of length <math>. Zeroth element will contain -- the initial value, that's why there is one less function application -- than the number of elements in the produced vector. -- -- <math> -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.iterateN 0 undefined undefined :: V.Vector String -- [] -- -- >>> V.iterateN 4 (\x -> x <> x) "Hi" -- ["Hi","HiHi","HiHiHiHi","HiHiHiHiHiHiHiHi"] --iterateN :: Int -> (a -> a) -> a -> Vector a -- | O(n) Execute the monadic action the given number of times and -- store the results in a vector. replicateM :: Monad m => Int -> m a -> m (Vector a) -- | O(n) Construct a vector of the given length by applying the -- monadic action to each index generateM :: Monad m => Int -> (Int -> m a) -> m (Vector a) -- | O(n) Apply monadic function <math> times to an initial -- value, producing a vector of length <math>. Zeroth element will -- contain the initial value, that's why there is one less function -- application than the number of elements in the produced vector. -- -- For non-monadic version see iterateN iterateNM :: Monad m => Int -> (a -> m a) -> a -> m (Vector a) -- | Execute the monadic action and freeze the resulting vector. -- --
-- create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>
--
create :: (forall s. ST s (MVector s a)) -> Vector a
-- | Execute the monadic action and freeze the resulting vectors.
createT :: Traversable f => (forall s. ST s (f (MVector s a))) -> f (Vector a)
-- | O(n) Construct a vector by repeatedly applying the generator
-- function to a seed. The generator function yields Just the next
-- element and the new seed or Nothing if there are no more
-- elements.
--
-- -- unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 -- = <10,9,8,7,6,5,4,3,2,1> --unfoldr :: (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with at most n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields Just the next element and the new seed or -- Nothing if there are no more elements. -- --
-- unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8> --unfoldrN :: Int -> (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields the next element and the new seed. -- --
-- unfoldrExactN 3 (\n -> (n,n-1)) 10 = <10,9,8> --unfoldrExactN :: Int -> (b -> (a, b)) -> b -> Vector a -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrNM :: Monad m => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the monadic generator function to a seed. The -- generator function yields the next element and the new seed. unfoldrExactNM :: Monad m => Int -> (b -> m (a, b)) -> b -> m (Vector a) -- | O(n) Construct a vector with n elements by repeatedly -- applying the generator function to the already constructed part of the -- vector. -- --
-- constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in <a,b,c> --constructN :: Int -> (Vector a -> a) -> Vector a -- | O(n) Construct a vector with n elements from right to -- left by repeatedly applying the generator function to the already -- constructed part of the vector. -- --
-- constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in <c,b,a> --constructrN :: Int -> (Vector a -> a) -> Vector a -- | O(n) Yield a vector of the given length containing the values -- x, x+1 etc. This operation is usually more efficient -- than enumFromTo. -- --
-- enumFromN 5 3 = <5,6,7> --enumFromN :: Num a => a -> Int -> Vector a -- | O(n) 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. -- --
-- enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4> --enumFromStepN :: Num a => a -> a -> Int -> Vector a -- | O(n) Enumerate values from x to y. -- -- WARNING: This operation can be very inefficient. If at all -- possible, use enumFromN instead. enumFromTo :: Enum a => a -> a -> Vector a -- | O(n) 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. enumFromThenTo :: Enum a => a -> a -> a -> Vector a -- | O(n) Prepend an element cons :: a -> Vector a -> Vector a -- | O(n) Append an element snoc :: Vector a -> a -> Vector a -- | O(m+n) Concatenate two vectors (++) :: Vector a -> Vector a -> Vector a infixr 5 ++ -- | O(n) Concatenate all vectors in the list concat :: [Vector a] -> Vector a -- | O(n) Yield the argument but force it not to retain any extra -- memory, possibly by copying it. -- -- This is especially useful when dealing with slices. For example: -- --
-- force (slice 0 2 <huge vector>) ---- -- Here, the slice retains a reference to the huge vector. Forcing it -- creates a copy of just the elements that belong to the slice and -- allows the huge vector to be garbage collected. force :: Vector a -> Vector a -- | O(m+n) For each pair (i,a) from the list, replace the -- vector element at position i by a. -- --
-- <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7> --(//) :: Vector a -> [(Int, a)] -> Vector a -- | O(m+n) For each pair (i,a) from the vector of -- index/value pairs, replace the vector element at position i -- by a. -- --
-- update <5,9,2,7> <(2,1),(0,3),(2,8)> = <3,9,8,7> --update :: Vector a -> Vector (Int, a) -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value a from the value vector, replace -- the element of the initial vector at position i by -- a. -- --
-- update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7> ---- -- The function update provides the same functionality and is -- usually more convenient. -- --
-- update_ xs is ys = update xs (zip is ys) --update_ :: Vector a -> Vector Int -> Vector a -> Vector a -- | Same as (//) but without bounds checking. unsafeUpd :: Vector a -> [(Int, a)] -> Vector a -- | Same as update but without bounds checking. unsafeUpdate :: Vector a -> Vector (Int, a) -> Vector a -- | Same as update_ but without bounds checking. unsafeUpdate_ :: Vector a -> Vector Int -> Vector a -> Vector a -- | O(m+n) For each pair (i,b) from the list, replace the -- vector element a at position i by f a b. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.accum (+) (V.fromList [1000.0,2000.0,3000.0]) [(2,4),(1,6),(0,3),(1,10)] -- [1003.0,2016.0,3004.0] --accum :: (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | O(m+n) For each pair (i,b) from the vector of pairs, -- replace the vector element a at position i by f -- a b. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.accumulate (+) (V.fromList [1000.0,2000.0,3000.0]) (V.fromList [(2,4),(1,6),(0,3),(1,10)]) -- [1003.0,2016.0,3004.0] --accumulate :: (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value b from the the value vector, -- replace the element of the initial vector at position i by -- f a b. -- --
-- accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4> ---- -- The function accumulate provides the same functionality and is -- usually more convenient. -- --
-- accumulate_ f as is bs = accumulate f as (zip is bs) --accumulate_ :: (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | Same as accum but without bounds checking. unsafeAccum :: (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | Same as accumulate but without bounds checking. unsafeAccumulate :: (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a -- | Same as accumulate_ but without bounds checking. unsafeAccumulate_ :: (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | O(n) Reverse a vector reverse :: Vector a -> Vector a -- | O(n) Yield the vector obtained by replacing each element -- i of the index vector by xs!i. This is -- equivalent to map (xs!) is but is often much -- more efficient. -- --
-- backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a> --backpermute :: Vector a -> Vector Int -> Vector a -- | Same as backpermute but without bounds checking. unsafeBackpermute :: Vector a -> Vector Int -> Vector a -- | Apply a destructive operation to a vector. The operation will be -- performed in place if it is safe to do so and will modify a copy of -- the vector otherwise. -- --
-- modify (\v -> write v 0 'x') (replicate 3 'a') = <'x','a','a'> --modify :: (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a -- | O(n) Pair each element in a vector with its index indexed :: Vector a -> Vector (Int, a) -- | O(n) Map a function over a vector map :: (a -> b) -> Vector a -> Vector b -- | O(n) Apply a function to every element of a vector and its -- index imap :: (Int -> a -> b) -> Vector a -> Vector b -- | Map a function over a vector and concatenate the results. concatMap :: (a -> Vector b) -> Vector a -> Vector b -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results mapM :: Monad m => (a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to every element of a vector and -- its index, yielding a vector of results imapM :: Monad m => (Int -> a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results mapM_ :: Monad m => (a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to every element of a vector and -- its index, ignoring the results imapM_ :: Monad m => (Int -> a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results. Equivalent to flip mapM. forM :: Monad m => Vector a -> (a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results. Equivalent to flip mapM_. forM_ :: Monad m => Vector a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to all elements of the vector and -- their indices, yielding a vector of results. Equivalent to flip -- imapM. iforM :: Monad m => Vector a -> (Int -> a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of the vector and -- their indices and ignore the results. Equivalent to flip -- imapM_. iforM_ :: Monad m => Vector a -> (Int -> a -> m b) -> m () -- | O(min(m,n)) Zip two vectors with the given function. zipWith :: (a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors with the given function. zipWith3 :: (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d zipWith4 :: (a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e zipWith5 :: (a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f zipWith6 :: (a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | O(min(m,n)) Zip two vectors with a function that also takes the -- elements' indices. izipWith :: (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors and their indices with the given function. izipWith3 :: (Int -> a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d izipWith4 :: (Int -> a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e izipWith5 :: (Int -> a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f izipWith6 :: (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | Elementwise pairing of array elements. zip :: Vector a -> Vector b -> Vector (a, b) -- | zip together three vectors into a vector of triples zip3 :: Vector a -> Vector b -> Vector c -> Vector (a, b, c) zip4 :: Vector a -> Vector b -> Vector c -> Vector d -> Vector (a, b, c, d) zip5 :: Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector (a, b, c, d, e) zip6 :: Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector (a, b, c, d, e, f) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- yield a vector of results zipWithM :: Monad m => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and yield a vector of results izipWithM :: Monad m => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- ignore the results zipWithM_ :: Monad m => (a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and ignore the results izipWithM_ :: Monad m => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(min(m,n)) Unzip a vector of pairs. unzip :: Vector (a, b) -> (Vector a, Vector b) unzip3 :: Vector (a, b, c) -> (Vector a, Vector b, Vector c) unzip4 :: Vector (a, b, c, d) -> (Vector a, Vector b, Vector c, Vector d) unzip5 :: Vector (a, b, c, d, e) -> (Vector a, Vector b, Vector c, Vector d, Vector e) unzip6 :: Vector (a, b, c, d, e, f) -> (Vector a, Vector b, Vector c, Vector d, Vector e, Vector f) -- | O(n) Drop elements that do not satisfy the predicate filter :: (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the predicate which is -- applied to values and their indices ifilter :: (Int -> a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the monadic predicate filterM :: Monad m => (a -> m Bool) -> Vector a -> m (Vector a) -- | O(n) Drop repeated adjacent elements. uniq :: Eq a => Vector a -> Vector a -- | O(n) Drop elements when predicate returns Nothing mapMaybe :: (a -> Maybe b) -> Vector a -> Vector b -- | O(n) Drop elements when predicate, applied to index and value, -- returns Nothing imapMaybe :: (Int -> a -> Maybe b) -> Vector a -> Vector b -- | O(n) Apply monadic function to each element of vector and -- discard elements returning Nothing. mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Apply monadic function to each element of vector and its -- index. Discards elements returning Nothing. imapMaybeM :: Monad m => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Return a Vector of all the Just values. catMaybes :: Vector (Maybe a) -> Vector a -- | O(n) Yield the longest prefix of elements satisfying the -- predicate. Current implementation is not copy-free, unless the result -- vector is fused away. takeWhile :: (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop the longest prefix of elements that satisfy the -- predicate without copying. dropWhile :: (a -> Bool) -> Vector a -> Vector a -- | O(n) 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. partition :: (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) 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 but the -- operation is often faster than partition. unstablePartition :: (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into two parts, the first one containing -- the Left elements and the second containing the -- Right elements. The relative order of the elements is -- preserved. partitionWith :: (a -> Either b c) -> Vector a -> (Vector b, Vector c) -- | O(n) Split the vector into the longest prefix of elements that -- satisfy the predicate and the rest without copying. span :: (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into the longest prefix of elements that -- do not satisfy the predicate and the rest without copying. break :: (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Check if the vector contains an element elem :: Eq a => a -> Vector a -> Bool infix 4 `elem` -- | O(n) Check if the vector does not contain an element (inverse -- of elem) notElem :: Eq a => a -> Vector a -> Bool infix 4 `notElem` -- | O(n) Yield Just the first element matching the predicate -- or Nothing if no such element exists. find :: (a -> Bool) -> Vector a -> Maybe a -- | O(n) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: (a -> Bool) -> Vector a -> Maybe Int -- | O(n) Yield the indices of elements satisfying the predicate in -- ascending order. findIndices :: (a -> Bool) -> Vector a -> Vector Int -- | O(n) Yield Just the index of the first occurence of the -- given element or Nothing if the vector does not contain the -- element. This is a specialised version of findIndex. elemIndex :: Eq a => a -> Vector a -> Maybe Int -- | O(n) Yield the indices of all occurences of the given element -- in ascending order. This is a specialised version of -- findIndices. elemIndices :: Eq a => a -> Vector a -> Vector Int -- | O(n) Left fold foldl :: (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors foldl1 :: (a -> a -> a) -> Vector a -> a -- | O(n) Left fold with strict accumulator foldl' :: (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors with strict accumulator foldl1' :: (a -> a -> a) -> Vector a -> a -- | O(n) Right fold foldr :: (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors foldr1 :: (a -> a -> a) -> Vector a -> a -- | O(n) Right fold with a strict accumulator foldr' :: (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors with strict accumulator foldr1' :: (a -> a -> a) -> Vector a -> a -- | O(n) Left fold (function applied to each element and its index) ifoldl :: (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold with strict accumulator (function applied to -- each element and its index) ifoldl' :: (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Right fold (function applied to each element and its -- index) ifoldr :: (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold with strict accumulator (function applied to -- each element and its index) ifoldr' :: (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Map each element of the structure to a monoid, and combine -- the results. It uses same implementation as corresponding method of -- Foldable type cless. Note it's implemented in terms of -- foldr and won't fuse with functions that traverse vector from -- left to right (map, generate, etc.). foldMap :: Monoid m => (a -> m) -> Vector a -> m -- | O(n) foldMap which is strict in accumulator. It uses -- same implementation as corresponding method of Foldable type -- class. Note it's implemented in terms of foldl' so it fuses in -- most contexts. foldMap' :: Monoid m => (a -> m) -> Vector a -> m -- | O(n) Check if all elements satisfy the predicate. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.all even $ V.fromList [2, 4, 12 :: Int] -- True -- -- >>> V.all even $ V.fromList [2, 4, 13 :: Int] -- False -- -- >>> V.all even (V.empty :: V.Vector Int) -- True --all :: (a -> Bool) -> Vector a -> Bool -- | O(n) Check if any element satisfies the predicate. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.any even $ V.fromList [1, 3, 7 :: Int] -- False -- -- >>> V.any even $ V.fromList [3, 2, 13 :: Int] -- True -- -- >>> V.any even (V.empty :: V.Vector Int) -- False --any :: (a -> Bool) -> Vector a -> Bool -- | O(n) Check if all elements are True -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.and $ V.fromList [True, False] -- False -- -- >>> V.and V.empty -- True --and :: Vector Bool -> Bool -- | O(n) Check if any element is True -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.or $ V.fromList [True, False] -- True -- -- >>> V.or V.empty -- False --or :: Vector Bool -> Bool -- | O(n) Compute the sum of the elements -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.sum $ V.fromList [300,20,1 :: Int] -- 321 -- -- >>> V.sum (V.empty :: V.Vector Int) -- 0 --sum :: Num a => Vector a -> a -- | O(n) Compute the produce of the elements -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.product $ V.fromList [1,2,3,4 :: Int] -- 24 -- -- >>> V.product (V.empty :: V.Vector Int) -- 1 --product :: Num a => Vector a -> a -- | O(n) Yield the maximum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.maximum $ V.fromList [2.0, 1.0] -- 2.0 --maximum :: Ord a => Vector a -> a -- | O(n) Yield the maximum element of the vector according to the -- given comparison function. The vector may not be empty. maximumBy :: (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the minimum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector as V -- -- >>> V.minimum $ V.fromList [2.0, 1.0] -- 1.0 --minimum :: Ord a => Vector a -> a -- | O(n) Yield the minimum element of the vector according to the -- given comparison function. The vector may not be empty. minimumBy :: (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the index of the minimum element of the vector. The -- vector may not be empty. minIndex :: Ord a => Vector a -> Int -- | O(n) Yield the index of the minimum element of the vector -- according to the given comparison function. The vector may not be -- empty. minIndexBy :: (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector. The -- vector may not be empty. maxIndex :: Ord a => Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector -- according to the given comparison function. The vector may not be -- empty. maxIndexBy :: (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Monadic fold foldM :: Monad m => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold (action applied to each element and its -- index) ifoldM :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator foldM' :: Monad m => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index) ifoldM' :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold over non-empty vectors fold1M :: Monad m => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator fold1M' :: Monad m => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold that discards the result foldM_ :: Monad m => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold that discards the result (action applied to -- each element and its index) ifoldM_ :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result foldM'_ :: Monad m => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result (action applied to each element and its index) ifoldM'_ :: Monad m => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold over non-empty vectors that discards the -- result fold1M_ :: Monad m => (a -> a -> m a) -> Vector a -> m () -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator that discards the result fold1M'_ :: Monad m => (a -> a -> m a) -> Vector a -> m () -- | Evaluate each action and collect the results sequence :: Monad m => Vector (m a) -> m (Vector a) -- | Evaluate each action and discard the results sequence_ :: Monad m => Vector (m a) -> m () -- | 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 -- | O(n) Prescan with strict accumulator prescanl' :: (a -> b -> a) -> a -> Vector b -> Vector a -- | 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 -- | O(n) Scan with strict accumulator postscanl' :: (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan -- --
-- 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 -- | O(n) Haskell-style scan with strict accumulator scanl' :: (a -> b -> a) -> a -> Vector b -> Vector a -- | 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 -- | O(n) Scan over a non-empty vector with a strict accumulator scanl1' :: (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a vector with its index iscanl :: (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a vector (strictly) with its index iscanl' :: (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Right-to-left prescan -- --
-- prescanr f z = reverse . prescanl (flip f) z . reverse --prescanr :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left prescan with strict accumulator prescanr' :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan postscanr :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan with strict accumulator postscanr' :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan scanr :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan with strict accumulator scanr' :: (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a non-empty vector scanr1 :: (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a non-empty vector with a strict -- accumulator scanr1' :: (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a vector with its index iscanr :: (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a vector (strictly) with its index iscanr' :: (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Check if two vectors are equal using supplied equality -- predicate. eqBy :: (a -> b -> Bool) -> Vector a -> Vector b -> Bool -- | O(n) Compare two vectors using supplied comparison function for -- vector elements. Comparison works same as for lists. -- --
-- cmpBy compare == compare --cmpBy :: (a -> b -> Ordering) -> Vector a -> Vector b -> Ordering -- | O(n) Convert a vector to a list toList :: Vector a -> [a] -- | O(n) Convert a list to a vector fromList :: [a] -> Vector a -- | O(n) Convert the first n elements of a list to a -- vector -- --
-- fromListN n xs = fromList (take n xs) --fromListN :: Int -> [a] -> Vector a -- | O(1) Convert an array to a vector. fromArray :: Array a -> Vector a -- | O(n) Convert a vector to an array. toArray :: Vector a -> Array a -- | O(n) Convert different vector types convert :: (Vector v a, Vector w a) => v a -> w a -- | O(n) Yield an immutable copy of the mutable vector. freeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) -- | O(n) Yield a mutable copy of the immutable vector. thaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. copy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m () -- | O(1) Unsafe convert a mutable vector to an immutable one -- without copying. The mutable vector may not be used after this -- operation. unsafeFreeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) -- | O(1) Unsafely convert an immutable vector to a mutable one -- without copying. Note that this is very dangerous function and -- generally it's only safe to read from resulting vector. In which case -- immutable vector could be used safely as well. -- -- Problem with mutation happens because GHC has a lot of freedom to -- introduce sharing. As a result mutable vectors produced by -- unsafeThaw may or may not share same underlying buffer. For -- example: -- --
-- foo = do -- let vec = V.generate 10 id -- mvec <- V.unsafeThaw vec -- do_something mvec ---- -- Here GHC could lift vec outside of foo which means all calls -- to do_something will use same buffer with possibly disastrous -- results. Whether such aliasing happens or not depends on program in -- question, optimization levels, and GHC flags. -- -- All in all attempts to modify vector after unsafeThaw falls out of -- domain of software engineering and into realm of black magic, dark -- rituals, and unspeakable horrors. Only advice that could be given is: -- "don't attempt to mutate vector after unsafeThaw unless you know how -- to prevent GHC from aliasing buffers accidentally. We don't" unsafeThaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. This is not checked. unsafeCopy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m () instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Vector.Vector a) instance Control.DeepSeq.NFData1 Data.Vector.Vector instance GHC.Show.Show a => GHC.Show.Show (Data.Vector.Vector a) instance GHC.Read.Read a => GHC.Read.Read (Data.Vector.Vector a) instance Data.Functor.Classes.Show1 Data.Vector.Vector instance Data.Functor.Classes.Read1 Data.Vector.Vector instance GHC.Exts.IsList (Data.Vector.Vector a) instance Data.Data.Data a => Data.Data.Data (Data.Vector.Vector a) instance Data.Vector.Generic.Base.Vector Data.Vector.Vector a instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Vector.Vector a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Vector.Vector a) instance Data.Functor.Classes.Eq1 Data.Vector.Vector instance Data.Functor.Classes.Ord1 Data.Vector.Vector instance GHC.Base.Semigroup (Data.Vector.Vector a) instance GHC.Base.Monoid (Data.Vector.Vector a) instance GHC.Base.Functor Data.Vector.Vector instance GHC.Base.Monad Data.Vector.Vector instance Control.Monad.Fail.MonadFail Data.Vector.Vector instance GHC.Base.MonadPlus Data.Vector.Vector instance Control.Monad.Zip.MonadZip Data.Vector.Vector instance Control.Monad.Fix.MonadFix Data.Vector.Vector instance GHC.Base.Applicative Data.Vector.Vector instance GHC.Base.Alternative Data.Vector.Vector instance Data.Foldable.Foldable Data.Vector.Vector instance Data.Traversable.Traversable Data.Vector.Vector -- | Mutable primitive vectors. module Data.Vector.Primitive.Mutable -- | Mutable vectors of primitive types. data MVector s a -- | offset, length, underlying mutable byte array MVector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !MutableByteArray s -> MVector s a type IOVector = MVector RealWorld type STVector s = MVector s -- | Class of types supporting primitive array operations. This includes -- interfacing with GC-managed memory (functions suffixed with -- ByteArray#) and interfacing with unmanaged memory (functions -- suffixed with Addr#). Endianness is platform-dependent. class Prim a -- | Length of the mutable vector. length :: Prim a => MVector s a -> Int -- | Check whether the vector is empty null :: Prim a => MVector s a -> Bool -- | Yield a part of the mutable vector without copying it. The vector must -- contain at least i+n elements. slice :: Prim a => Int -> Int -> MVector s a -> MVector s a -- | Drop last element of the mutable vector without making a copy. If -- vector is empty exception is thrown. init :: Prim a => MVector s a -> MVector s a -- | Drop first element of the mutable vector without making a copy. If -- vector is empty exception is thrown. tail :: Prim a => MVector s a -> MVector s a -- | Take n first elements of the mutable vector without making a -- copy. For negative n empty vector is returned. If n -- is larger than vector's length empty vector is returned, take :: Prim a => Int -> MVector s a -> MVector s a -- | Drop n first element of the mutable vector without making a -- copy. For negative n vector is returned unchanged and if -- n is larger than vector's length empty vector is returned. drop :: Prim a => Int -> MVector s a -> MVector s a splitAt :: Prim a => Int -> MVector s a -> (MVector s a, MVector s a) -- | Yield a part of the mutable vector without copying it. No bounds -- checks are performed. unsafeSlice :: Prim a => Int -> Int -> MVector s a -> MVector s a -- | Same as init but doesn't do range checks. unsafeInit :: Prim a => MVector s a -> MVector s a -- | Same as tail but doesn't do range checks. unsafeTail :: Prim a => MVector s a -> MVector s a -- | Unsafe variant of take. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeTake :: Prim a => Int -> MVector s a -> MVector s a -- | Unsafe variant of drop. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeDrop :: Prim a => Int -> MVector s a -> MVector s a -- | Check whether two vectors overlap. overlaps :: Prim a => MVector s a -> MVector s a -> Bool -- | Create a mutable vector of the given length. new :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length. The vector content is -- uninitialized, which means it is filled with whatever underlying -- memory buffer happens to contain. unsafeNew :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with an initial value. replicate :: (PrimMonad m, Prim a) => Int -> a -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with values produced by repeatedly executing the -- monadic action. replicateM :: (PrimMonad m, Prim a) => Int -> m a -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- function to each index. generate :: (PrimMonad m, Prim a) => Int -> (Int -> a) -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- monadic function to each index. Iteration starts at index 0. generateM :: (PrimMonad m, Prim a) => Int -> (Int -> m a) -> m (MVector (PrimState m) a) -- | Create a copy of a mutable vector. clone :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> m (MVector (PrimState m) a) -- | Grow a primitive vector by the given number of elements. The number -- must be non-negative. Same semantics as in grow for generic -- vector. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> import qualified Data.Vector.Primitive.Mutable as MVP -- -- >>> mv <- VP.thaw $ VP.fromList ([10, 20, 30] :: [Int]) -- -- >>> mv' <- MVP.grow mv 2 ---- -- Extra memory at the end of the newly allocated vector is initialized -- to 0 bytes, which for Prim instance will usually correspond to -- some default value for a particular type, eg. 0 for -- Int, NUL for Char, etc. However, if -- unsafeGrow was used instead this would not have been guaranteed -- and some garbage would be there instead: -- --
-- >>> VP.freeze mv' -- [10,20,30,0,0] ---- -- Having the extra space we can write new values in there: -- --
-- >>> MVP.write mv' 3 999 -- -- >>> VP.freeze mv' -- [10,20,30,999,0] ---- -- It is important to note that the source mutable vector is not affected -- when the newly allocated one is mutated. -- --
-- >>> MVP.write mv' 2 888 -- -- >>> VP.freeze mv' -- [10,20,888,999,0] -- -- >>> VP.freeze mv -- [10,20,30] --grow :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Grow a vector by the given number of elements. The number must be -- non-negative but this is not checked. Same semantics as in -- unsafeGrow for generic vector. unsafeGrow :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Reset all elements of the vector to some undefined value, clearing all -- references to external objects. This is usually a noop for unboxed -- vectors. clear :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> m () -- | Yield the element at the given position. read :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. write :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. modify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. modifyM :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. swap :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. exchange :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m a -- | Yield the element at the given position. No bounds checks are -- performed. unsafeRead :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. No bounds checks are -- performed. unsafeWrite :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. No bounds checks are -- performed. unsafeModify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. No -- bounds checks are performed. unsafeModifyM :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. No bounds checks are -- performed. unsafeSwap :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. -- No bounds checks are performed. unsafeExchange :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m a -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. mapM_ :: (PrimMonad m, Prim a) => (a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. imapM_ :: (PrimMonad m, Prim a) => (Int -> a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. It's same as the flip mapM_. forM_ :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. It's same as the flip -- imapM_. iforM_ :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (Int -> a -> m b) -> m () -- | O(n) Pure left fold. foldl :: (PrimMonad m, Prim a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator. foldl' :: (PrimMonad m, Prim a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold. foldM :: (PrimMonad m, Prim a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator. foldM' :: (PrimMonad m, Prim a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold. foldr :: (PrimMonad m, Prim a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator. foldr' :: (PrimMonad m, Prim a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold. foldrM :: (PrimMonad m, Prim a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator. foldrM' :: (PrimMonad m, Prim a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold (function applied to each element and its -- index). ifoldl :: (PrimMonad m, Prim a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator (function applied -- to each element and its index). ifoldl' :: (PrimMonad m, Prim a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold (action applied to each element and its -- index). ifoldM :: (PrimMonad m, Prim a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index). ifoldM' :: (PrimMonad m, Prim a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold (function applied to each element and its -- index). ifoldr :: (PrimMonad m, Prim a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator (function applied -- to each element and its index). ifoldr' :: (PrimMonad m, Prim a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold (action applied to each element and its -- index). ifoldrM :: (PrimMonad m, Prim a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator (action applied -- to each element and its index). ifoldrM' :: (PrimMonad m, Prim a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | Compute the next (lexicographically) permutation of given vector -- in-place. Returns False when input is the last permutation nextPermutation :: (PrimMonad m, Ord e, Prim e) => MVector (PrimState m) e -> m Bool -- | Set all elements of the vector to the given value. set :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. copy :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length. -- -- If the vectors do not overlap, then this is equivalent to copy. -- Otherwise, the copying is performed as if the source vector were -- copied to a temporary vector and then the temporary vector was copied -- to the target vector. move :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. This is not checked. unsafeCopy :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length, but this is not checked. -- -- If the vectors do not overlap, then this is equivalent to -- unsafeCopy. Otherwise, the copying is performed as if the -- source vector were copied to a temporary vector and then the temporary -- vector was copied to the target vector. unsafeMove :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () instance Control.DeepSeq.NFData (Data.Vector.Primitive.Mutable.MVector s a) instance Control.DeepSeq.NFData1 (Data.Vector.Primitive.Mutable.MVector s) instance Data.Primitive.Types.Prim a => Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Primitive.Mutable.MVector a -- | Unboxed vectors of primitive types. The use of this module is not -- recommended except in very special cases. Adaptive unboxed vectors -- defined in Data.Vector.Unboxed are significantly more flexible -- at no performance cost. module Data.Vector.Primitive -- | Unboxed vectors of primitive types data Vector a -- | offset, length, underlying byte array Vector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !ByteArray -> Vector a -- | Mutable vectors of primitive types. data MVector s a -- | offset, length, underlying mutable byte array MVector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !MutableByteArray s -> MVector s a -- | Class of types supporting primitive array operations. This includes -- interfacing with GC-managed memory (functions suffixed with -- ByteArray#) and interfacing with unmanaged memory (functions -- suffixed with Addr#). Endianness is platform-dependent. class Prim a -- | O(1) Yield the length of the vector length :: Prim a => Vector a -> Int -- | O(1) Test whether a vector is empty null :: Prim a => Vector a -> Bool -- | O(1) Indexing (!) :: Prim a => Vector a -> Int -> a -- | O(1) Safe indexing (!?) :: Prim a => Vector a -> Int -> Maybe a -- | O(1) First element head :: Prim a => Vector a -> a -- | O(1) Last element last :: Prim a => Vector a -> a -- | O(1) Unsafe indexing without bounds checking unsafeIndex :: Prim a => Vector a -> Int -> a -- | O(1) First element without checking if the vector is empty unsafeHead :: Prim a => Vector a -> a -- | O(1) Last element without checking if the vector is empty unsafeLast :: Prim a => Vector a -> a -- | O(1) Indexing in a monad. -- -- The monad allows operations to be strict in the vector when necessary. -- Suppose vector copying is implemented like this: -- --
-- copy mv v = ... write mv i (v ! i) ... ---- -- For lazy vectors, v ! i would not be evaluated which means -- that mv would unnecessarily retain a reference to v -- in each element written. -- -- With indexM, copying can be implemented like this instead: -- --
-- copy mv v = ... do -- x <- indexM v i -- write mv i x ---- -- Here, no references to v are retained because indexing (but -- not the elements) is evaluated eagerly. indexM :: (Prim a, Monad m) => Vector a -> Int -> m a -- | O(1) First element of a vector in a monad. See indexM -- for an explanation of why this is useful. headM :: (Prim a, Monad m) => Vector a -> m a -- | O(1) Last element of a vector in a monad. See indexM for -- an explanation of why this is useful. lastM :: (Prim a, Monad m) => Vector a -> m a -- | O(1) Indexing in a monad without bounds checks. See -- indexM for an explanation of why this is useful. unsafeIndexM :: (Prim a, Monad m) => Vector a -> Int -> m a -- | O(1) First element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeHeadM :: (Prim a, Monad m) => Vector a -> m a -- | O(1) Last element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeLastM :: (Prim a, Monad m) => Vector a -> m a -- | O(1) Yield a slice of the vector without copying it. The vector -- must contain at least i+n elements. slice :: Prim a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty. init :: Prim a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty. tail :: Prim a => Vector a -> Vector a -- | O(1) Yield at the first n elements without copying. -- The vector may contain less than n elements in which case it -- is returned unchanged. take :: Prim a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector may contain less than n elements in which -- case an empty vector is returned. drop :: Prim a => Int -> Vector a -> Vector a -- | O(1) Yield the first n elements paired with the -- remainder without copying. -- -- Note that splitAt n v is equivalent to -- (take n v, drop n v) but slightly more -- efficient. splitAt :: Prim a => Int -> Vector a -> (Vector a, Vector a) -- | O(1) Yield the head and tail of the vector, or -- Nothing if empty. uncons :: Prim a => Vector a -> Maybe (a, Vector a) -- | O(1) Yield the last and init of the vector, or -- Nothing if empty. unsnoc :: Prim a => Vector a -> Maybe (Vector a, a) -- | O(1) Yield a slice of the vector without copying. The vector -- must contain at least i+n elements but this is not checked. unsafeSlice :: Prim a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty but this is not checked. unsafeInit :: Prim a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty but this is not checked. unsafeTail :: Prim a => Vector a -> Vector a -- | O(1) Yield the first n elements without copying. The -- vector must contain at least n elements but this is not -- checked. unsafeTake :: Prim a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector must contain at least n elements but this -- is not checked. unsafeDrop :: Prim a => Int -> Vector a -> Vector a -- | O(1) Empty vector empty :: Prim a => Vector a -- | O(1) Vector with exactly one element singleton :: Prim a => a -> Vector a -- | O(n) Vector of the given length with the same value in each -- position replicate :: Prim a => Int -> a -> Vector a -- | O(n) Construct a vector of the given length by applying the -- function to each index generate :: Prim a => Int -> (Int -> a) -> Vector a -- | O(n) Apply function <math> times to an initial value, -- producing a vector of length <math>. Zeroth element will contain -- the initial value, that's why there is one less function application -- than the number of elements in the produced vector. -- -- <math> -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.iterateN 0 undefined undefined :: VP.Vector Int -- [] -- -- >>> VP.iterateN 26 succ 'a' -- "abcdefghijklmnopqrstuvwxyz" --iterateN :: Prim a => Int -> (a -> a) -> a -> Vector a -- | O(n) Execute the monadic action the given number of times and -- store the results in a vector. replicateM :: (Monad m, Prim a) => Int -> m a -> m (Vector a) -- | O(n) Construct a vector of the given length by applying the -- monadic action to each index generateM :: (Monad m, Prim a) => Int -> (Int -> m a) -> m (Vector a) -- | O(n) Apply monadic function <math> times to an initial -- value, producing a vector of length <math>. Zeroth element will -- contain the initial value, that's why there is one less function -- application than the number of elements in the produced vector. -- -- For non-monadic version see iterateN iterateNM :: (Monad m, Prim a) => Int -> (a -> m a) -> a -> m (Vector a) -- | Execute the monadic action and freeze the resulting vector. -- --
-- create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>
--
create :: Prim a => (forall s. ST s (MVector s a)) -> Vector a
-- | Execute the monadic action and freeze the resulting vectors.
createT :: (Traversable f, Prim a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
-- | O(n) Construct a vector by repeatedly applying the generator
-- function to a seed. The generator function yields Just the next
-- element and the new seed or Nothing if there are no more
-- elements.
--
-- -- unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 -- = <10,9,8,7,6,5,4,3,2,1> --unfoldr :: Prim a => (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with at most n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields Just the next element and the new seed or -- Nothing if there are no more elements. -- --
-- unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8> --unfoldrN :: Prim a => Int -> (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields the next element and the new seed. -- --
-- unfoldrExactN 3 (\n -> (n,n-1)) 10 = <10,9,8> --unfoldrExactN :: Prim a => Int -> (b -> (a, b)) -> b -> Vector a -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrM :: (Monad m, Prim a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrNM :: (Monad m, Prim a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the monadic generator function to a seed. The -- generator function yields the next element and the new seed. unfoldrExactNM :: (Monad m, Prim a) => Int -> (b -> m (a, b)) -> b -> m (Vector a) -- | O(n) Construct a vector with n elements by repeatedly -- applying the generator function to the already constructed part of the -- vector. -- --
-- constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in <a,b,c> --constructN :: Prim a => Int -> (Vector a -> a) -> Vector a -- | O(n) Construct a vector with n elements from right to -- left by repeatedly applying the generator function to the already -- constructed part of the vector. -- --
-- constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in <c,b,a> --constructrN :: Prim a => Int -> (Vector a -> a) -> Vector a -- | O(n) Yield a vector of the given length containing the values -- x, x+1 etc. This operation is usually more efficient -- than enumFromTo. -- --
-- enumFromN 5 3 = <5,6,7> --enumFromN :: (Prim a, Num a) => a -> Int -> Vector a -- | O(n) 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. -- --
-- enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4> --enumFromStepN :: (Prim a, Num a) => a -> a -> Int -> Vector a -- | O(n) Enumerate values from x to y. -- -- WARNING: This operation can be very inefficient. If at all -- possible, use enumFromN instead. enumFromTo :: (Prim a, Enum a) => a -> a -> Vector a -- | O(n) 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. enumFromThenTo :: (Prim a, Enum a) => a -> a -> a -> Vector a -- | O(n) Prepend an element cons :: Prim a => a -> Vector a -> Vector a -- | O(n) Append an element snoc :: Prim a => Vector a -> a -> Vector a -- | O(m+n) Concatenate two vectors (++) :: Prim a => Vector a -> Vector a -> Vector a infixr 5 ++ -- | O(n) Concatenate all vectors in the list concat :: Prim a => [Vector a] -> Vector a -- | O(n) Yield the argument but force it not to retain any extra -- memory, possibly by copying it. -- -- This is especially useful when dealing with slices. For example: -- --
-- force (slice 0 2 <huge vector>) ---- -- Here, the slice retains a reference to the huge vector. Forcing it -- creates a copy of just the elements that belong to the slice and -- allows the huge vector to be garbage collected. force :: Prim a => Vector a -> Vector a -- | O(m+n) For each pair (i,a) from the list, replace the -- vector element at position i by a. -- --
-- <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7> --(//) :: Prim a => Vector a -> [(Int, a)] -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value a from the value vector, replace -- the element of the initial vector at position i by -- a. -- --
-- update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7> --update_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a -- | Same as (//) but without bounds checking. unsafeUpd :: Prim a => Vector a -> [(Int, a)] -> Vector a -- | Same as update_ but without bounds checking. unsafeUpdate_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a -- | O(m+n) For each pair (i,b) from the list, replace the -- vector element a at position i by f a b. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.accum (+) (VP.fromList [1000.0,2000.0,3000.0]) [(2,4),(1,6),(0,3),(1,10)] -- [1003.0,2016.0,3004.0] --accum :: Prim a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value b from the the value vector, -- replace the element of the initial vector at position i by -- f a b. -- --
-- accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4> --accumulate_ :: (Prim a, Prim b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | Same as accum but without bounds checking. unsafeAccum :: Prim a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | Same as accumulate_ but without bounds checking. unsafeAccumulate_ :: (Prim a, Prim b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | O(n) Reverse a vector reverse :: Prim a => Vector a -> Vector a -- | O(n) Yield the vector obtained by replacing each element -- i of the index vector by xs!i. This is -- equivalent to map (xs!) is but is often much -- more efficient. -- --
-- backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a> --backpermute :: Prim a => Vector a -> Vector Int -> Vector a -- | Same as backpermute but without bounds checking. unsafeBackpermute :: Prim a => Vector a -> Vector Int -> Vector a -- | Apply a destructive operation to a vector. The operation will be -- performed in place if it is safe to do so and will modify a copy of -- the vector otherwise. -- --
-- modify (\v -> write v 0 'x') (replicate 3 'a') = <'x','a','a'> --modify :: Prim a => (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a -- | O(n) Map a function over a vector map :: (Prim a, Prim b) => (a -> b) -> Vector a -> Vector b -- | O(n) Apply a function to every element of a vector and its -- index imap :: (Prim a, Prim b) => (Int -> a -> b) -> Vector a -> Vector b -- | Map a function over a vector and concatenate the results. concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results mapM :: (Monad m, Prim a, Prim b) => (a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to every element of a vector and -- its index, yielding a vector of results imapM :: (Monad m, Prim a, Prim b) => (Int -> a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results mapM_ :: (Monad m, Prim a) => (a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to every element of a vector and -- its index, ignoring the results imapM_ :: (Monad m, Prim a) => (Int -> a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results. Equivalent to flip mapM. forM :: (Monad m, Prim a, Prim b) => Vector a -> (a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results. Equivalent to flip mapM_. forM_ :: (Monad m, Prim a) => Vector a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to all elements of the vector and -- their indices, yielding a vector of results. Equivalent to flip -- imapM. iforM :: (Monad m, Prim a, Prim b) => Vector a -> (Int -> a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of the vector and -- their indices and ignore the results. Equivalent to flip -- imapM_. iforM_ :: (Monad m, Prim a) => Vector a -> (Int -> a -> m b) -> m () -- | O(min(m,n)) Zip two vectors with the given function. zipWith :: (Prim a, Prim b, Prim c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors with the given function. zipWith3 :: (Prim a, Prim b, Prim c, Prim d) => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d zipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e) => (a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e zipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => (a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f zipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => (a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | O(min(m,n)) Zip two vectors with a function that also takes the -- elements' indices. izipWith :: (Prim a, Prim b, Prim c) => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors and their indices with the given function. izipWith3 :: (Prim a, Prim b, Prim c, Prim d) => (Int -> a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d izipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e) => (Int -> a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e izipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f izipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | O(min(m,n)) Zip the two vectors with the monadic action and -- yield a vector of results zipWithM :: (Monad m, Prim a, Prim b, Prim c) => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and yield a vector of results izipWithM :: (Monad m, Prim a, Prim b, Prim c) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- ignore the results zipWithM_ :: (Monad m, Prim a, Prim b) => (a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and ignore the results izipWithM_ :: (Monad m, Prim a, Prim b) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(n) Drop elements that do not satisfy the predicate filter :: Prim a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the predicate which is -- applied to values and their indices ifilter :: Prim a => (Int -> a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the monadic predicate filterM :: (Monad m, Prim a) => (a -> m Bool) -> Vector a -> m (Vector a) -- | O(n) Drop repeated adjacent elements. uniq :: (Prim a, Eq a) => Vector a -> Vector a -- | O(n) Drop elements when predicate returns Nothing mapMaybe :: (Prim a, Prim b) => (a -> Maybe b) -> Vector a -> Vector b -- | O(n) Drop elements when predicate, applied to index and value, -- returns Nothing imapMaybe :: (Prim a, Prim b) => (Int -> a -> Maybe b) -> Vector a -> Vector b -- | O(n) Apply monadic function to each element of vector and -- discard elements returning Nothing. mapMaybeM :: (Monad m, Prim a, Prim b) => (a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Apply monadic function to each element of vector and its -- index. Discards elements returning Nothing. imapMaybeM :: (Monad m, Prim a, Prim b) => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Yield the longest prefix of elements satisfying the -- predicate. Current implementation is not copy-free, unless the result -- vector is fused away. takeWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop the longest prefix of elements that satisfy the -- predicate without copying. dropWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a -- | O(n) 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. partition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) 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 but the -- operation is often faster than partition. unstablePartition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into two parts, the first one containing -- the Left elements and the second containing the -- Right elements. The relative order of the elements is -- preserved. partitionWith :: (Prim a, Prim b, Prim c) => (a -> Either b c) -> Vector a -> (Vector b, Vector c) -- | O(n) Split the vector into the longest prefix of elements that -- satisfy the predicate and the rest without copying. span :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into the longest prefix of elements that -- do not satisfy the predicate and the rest without copying. break :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Check if the vector contains an element elem :: (Prim a, Eq a) => a -> Vector a -> Bool infix 4 `elem` -- | O(n) Check if the vector does not contain an element (inverse -- of elem) notElem :: (Prim a, Eq a) => a -> Vector a -> Bool infix 4 `notElem` -- | O(n) Yield Just the first element matching the predicate -- or Nothing if no such element exists. find :: Prim a => (a -> Bool) -> Vector a -> Maybe a -- | O(n) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: Prim a => (a -> Bool) -> Vector a -> Maybe Int -- | O(n) Yield the indices of elements satisfying the predicate in -- ascending order. findIndices :: Prim a => (a -> Bool) -> Vector a -> Vector Int -- | O(n) Yield Just the index of the first occurence of the -- given element or Nothing if the vector does not contain the -- element. This is a specialised version of findIndex. elemIndex :: (Prim a, Eq a) => a -> Vector a -> Maybe Int -- | O(n) Yield the indices of all occurences of the given element -- in ascending order. This is a specialised version of -- findIndices. elemIndices :: (Prim a, Eq a) => a -> Vector a -> Vector Int -- | O(n) Left fold foldl :: Prim b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors foldl1 :: Prim a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold with strict accumulator foldl' :: Prim b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors with strict accumulator foldl1' :: Prim a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold foldr :: Prim a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors foldr1 :: Prim a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold with a strict accumulator foldr' :: Prim a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors with strict accumulator foldr1' :: Prim a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold (function applied to each element and its index) ifoldl :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold with strict accumulator (function applied to -- each element and its index) ifoldl' :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Right fold (function applied to each element and its -- index) ifoldr :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold with strict accumulator (function applied to -- each element and its index) ifoldr' :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Map each element of the structure to a monoid, and combine -- the results. It uses same implementation as corresponding method of -- Foldable type cless. Note it's implemented in terms of -- foldr and won't fuse with functions that traverse vector from -- left to right (map, generate, etc.). foldMap :: (Monoid m, Prim a) => (a -> m) -> Vector a -> m -- | O(n) foldMap which is strict in accumulator. It uses -- same implementation as corresponding method of Foldable type -- class. Note it's implemented in terms of foldl' so it fuses in -- most contexts. foldMap' :: (Monoid m, Prim a) => (a -> m) -> Vector a -> m -- | O(n) Check if all elements satisfy the predicate. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.all even $ VP.fromList [2, 4, 12 :: Int] -- True -- -- >>> VP.all even $ VP.fromList [2, 4, 13 :: Int] -- False -- -- >>> VP.all even (VP.empty :: VP.Vector Int) -- True --all :: Prim a => (a -> Bool) -> Vector a -> Bool -- | O(n) Check if any element satisfies the predicate. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.any even $ VP.fromList [1, 3, 7 :: Int] -- False -- -- >>> VP.any even $ VP.fromList [3, 2, 13 :: Int] -- True -- -- >>> VP.any even (VP.empty :: VP.Vector Int) -- False --any :: Prim a => (a -> Bool) -> Vector a -> Bool -- | O(n) Compute the sum of the elements -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.sum $ VP.fromList [300,20,1 :: Int] -- 321 -- -- >>> VP.sum (VP.empty :: VP.Vector Int) -- 0 --sum :: (Prim a, Num a) => Vector a -> a -- | O(n) Compute the produce of the elements -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.product $ VP.fromList [1,2,3,4 :: Int] -- 24 -- -- >>> VP.product (VP.empty :: VP.Vector Int) -- 1 --product :: (Prim a, Num a) => Vector a -> a -- | O(n) Yield the maximum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.maximum $ VP.fromList [2.0, 1.0] -- 2.0 --maximum :: (Prim a, Ord a) => Vector a -> a -- | O(n) Yield the maximum element of the vector according to the -- given comparison function. The vector may not be empty. maximumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the minimum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.minimum $ VP.fromList [2.0, 1.0] -- 1.0 --minimum :: (Prim a, Ord a) => Vector a -> a -- | O(n) Yield the minimum element of the vector according to the -- given comparison function. The vector may not be empty. minimumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the index of the minimum element of the vector. The -- vector may not be empty. minIndex :: (Prim a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the minimum element of the vector -- according to the given comparison function. The vector may not be -- empty. minIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector. The -- vector may not be empty. maxIndex :: (Prim a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector -- according to the given comparison function. The vector may not be -- empty. maxIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Monadic fold foldM :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold (action applied to each element and its -- index) ifoldM :: (Monad m, Prim b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator foldM' :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index) ifoldM' :: (Monad m, Prim b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold over non-empty vectors fold1M :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator fold1M' :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold that discards the result foldM_ :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold that discards the result (action applied to -- each element and its index) ifoldM_ :: (Monad m, Prim b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result foldM'_ :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result (action applied to each element and its index) ifoldM'_ :: (Monad m, Prim b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold over non-empty vectors that discards the -- result fold1M_ :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator that discards the result fold1M'_ :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Prescan -- --
-- prescanl f z = init . scanl f z ---- -- Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6> prescanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Prescan with strict accumulator prescanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan -- --
-- postscanl f z = tail . scanl f z ---- -- Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10> postscanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan with strict accumulator postscanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan -- --
-- 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 :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan with strict accumulator scanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a non-empty vector -- --
-- scanl f <x1,...,xn> = <y1,...,yn> -- where y1 = x1 -- yi = f y(i-1) xi --scanl1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a non-empty vector with a strict accumulator scanl1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a vector with its index iscanl :: (Prim a, Prim b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a vector (strictly) with its index iscanl' :: (Prim a, Prim b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Right-to-left prescan -- --
-- prescanr f z = reverse . prescanl (flip f) z . reverse --prescanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left prescan with strict accumulator prescanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan postscanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan with strict accumulator postscanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan scanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan with strict accumulator scanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a non-empty vector scanr1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a non-empty vector with a strict -- accumulator scanr1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a vector with its index iscanr :: (Prim a, Prim b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a vector (strictly) with its index iscanr' :: (Prim a, Prim b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Check if two vectors are equal using supplied equality -- predicate. eqBy :: (Prim a, Prim b) => (a -> b -> Bool) -> Vector a -> Vector b -> Bool -- | O(n) Compare two vectors using supplied comparison function for -- vector elements. Comparison works same as for lists. -- --
-- cmpBy compare == compare --cmpBy :: (Prim a, Prim b) => (a -> b -> Ordering) -> Vector a -> Vector b -> Ordering -- | O(n) Convert a vector to a list toList :: Prim a => Vector a -> [a] -- | O(n) Convert a list to a vector fromList :: Prim a => [a] -> Vector a -- | O(n) Convert the first n elements of a list to a -- vector -- --
-- fromListN n xs = fromList (take n xs) ---- --
-- >>> import qualified Data.Vector.Primitive as VP -- -- >>> VP.fromListN 3 [1,2,3,4,5::Int] -- [1,2,3] -- -- >>> VP.fromListN 3 [1::Int] -- [1] --fromListN :: Prim a => Int -> [a] -> Vector a -- | O(n) Convert different vector types convert :: (Vector v a, Vector w a) => v a -> w a -- | O(n) Yield an immutable copy of the mutable vector. freeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(n) Yield a mutable copy of the immutable vector. thaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () -- | O(1) Unsafe convert a mutable vector to an immutable one -- without copying. The mutable vector may not be used after this -- operation. unsafeFreeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(1) Unsafely convert an immutable vector to a mutable one -- without copying. Note that this is very dangerous function and -- generally it's only safe to read from resulting vector. In which case -- immutable vector could be used safely as well. -- -- Problem with mutation happens because GHC has a lot of freedom to -- introduce sharing. As a result mutable vectors produced by -- unsafeThaw may or may not share same underlying buffer. For -- example: -- --
-- foo = do -- let vec = V.generate 10 id -- mvec <- V.unsafeThaw vec -- do_something mvec ---- -- Here GHC could lift vec outside of foo which means all calls -- to do_something will use same buffer with possibly disastrous -- results. Whether such aliasing happens or not depends on program in -- question, optimization levels, and GHC flags. -- -- All in all attempts to modify vector after unsafeThaw falls out of -- domain of software engineering and into realm of black magic, dark -- rituals, and unspeakable horrors. Only advice that could be given is: -- "don't attempt to mutate vector after unsafeThaw unless you know how -- to prevent GHC from aliasing buffers accidentally. We don't" unsafeThaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. This is not checked. unsafeCopy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () instance Control.DeepSeq.NFData (Data.Vector.Primitive.Vector a) instance Control.DeepSeq.NFData1 Data.Vector.Primitive.Vector instance (GHC.Show.Show a, Data.Primitive.Types.Prim a) => GHC.Show.Show (Data.Vector.Primitive.Vector a) instance (GHC.Read.Read a, Data.Primitive.Types.Prim a) => GHC.Read.Read (Data.Vector.Primitive.Vector a) instance (Data.Data.Data a, Data.Primitive.Types.Prim a) => Data.Data.Data (Data.Vector.Primitive.Vector a) instance Data.Primitive.Types.Prim a => Data.Vector.Generic.Base.Vector Data.Vector.Primitive.Vector a instance (Data.Primitive.Types.Prim a, GHC.Classes.Eq a) => GHC.Classes.Eq (Data.Vector.Primitive.Vector a) instance (Data.Primitive.Types.Prim a, GHC.Classes.Ord a) => GHC.Classes.Ord (Data.Vector.Primitive.Vector a) instance Data.Primitive.Types.Prim a => GHC.Base.Semigroup (Data.Vector.Primitive.Vector a) instance Data.Primitive.Types.Prim a => GHC.Base.Monoid (Data.Vector.Primitive.Vector a) instance Data.Primitive.Types.Prim a => GHC.Exts.IsList (Data.Vector.Primitive.Vector a) -- | Ugly internal utility functions for implementing -- Storable-based vectors. module Data.Vector.Storable.Internal getPtr :: ForeignPtr a -> Ptr a setPtr :: ForeignPtr a -> Ptr a -> ForeignPtr a updPtr :: (Ptr a -> Ptr a) -> ForeignPtr a -> ForeignPtr a -- | A compatibility wrapper for unsafeWithForeignPtr provided by -- GHC 9.0.1 and later. -- -- Only to be used when the continuation is known not to unconditionally -- diverge lest unsoundness can result. unsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b -- | Mutable vectors based on Storable. module Data.Vector.Storable.Mutable -- | Mutable Storable-based vectors data MVector s a MVector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !ForeignPtr a -> MVector s a type IOVector = MVector RealWorld type STVector s = MVector s -- | 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. class Storable a -- | Length of the mutable vector. length :: Storable a => MVector s a -> Int -- | Check whether the vector is empty null :: Storable a => MVector s a -> Bool -- | Yield a part of the mutable vector without copying it. The vector must -- contain at least i+n elements. slice :: Storable a => Int -> Int -> MVector s a -> MVector s a -- | Drop last element of the mutable vector without making a copy. If -- vector is empty exception is thrown. init :: Storable a => MVector s a -> MVector s a -- | Drop first element of the mutable vector without making a copy. If -- vector is empty exception is thrown. tail :: Storable a => MVector s a -> MVector s a -- | Take n first elements of the mutable vector without making a -- copy. For negative n empty vector is returned. If n -- is larger than vector's length empty vector is returned, take :: Storable a => Int -> MVector s a -> MVector s a -- | Drop n first element of the mutable vector without making a -- copy. For negative n vector is returned unchanged and if -- n is larger than vector's length empty vector is returned. drop :: Storable a => Int -> MVector s a -> MVector s a splitAt :: Storable a => Int -> MVector s a -> (MVector s a, MVector s a) -- | Yield a part of the mutable vector without copying it. No bounds -- checks are performed. unsafeSlice :: Storable a => Int -> Int -> MVector s a -> MVector s a -- | Same as init but doesn't do range checks. unsafeInit :: Storable a => MVector s a -> MVector s a -- | Same as tail but doesn't do range checks. unsafeTail :: Storable a => MVector s a -> MVector s a -- | Unsafe variant of take. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeTake :: Storable a => Int -> MVector s a -> MVector s a -- | Unsafe variant of drop. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeDrop :: Storable a => Int -> MVector s a -> MVector s a -- | Check whether two vectors overlap. overlaps :: Storable a => MVector s a -> MVector s a -> Bool -- | Create a mutable vector of the given length. new :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length. The vector content is -- uninitialized, which means it is filled with whatever underlying -- memory buffer happens to contain. unsafeNew :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with an initial value. replicate :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with values produced by repeatedly executing the -- monadic action. replicateM :: (PrimMonad m, Storable a) => Int -> m a -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- function to each index. generate :: (PrimMonad m, Storable a) => Int -> (Int -> a) -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- monadic function to each index. Iteration starts at index 0. generateM :: (PrimMonad m, Storable a) => Int -> (Int -> m a) -> m (MVector (PrimState m) a) -- | Create a copy of a mutable vector. clone :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> m (MVector (PrimState m) a) -- | Grow a storable vector by the given number of elements. The number -- must be non-negative. Same semantics as in grow for generic -- vector. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> import qualified Data.Vector.Storable.Mutable as MVS -- -- >>> mv <- VS.thaw $ VS.fromList ([10, 20, 30] :: [Int]) -- -- >>> mv' <- MVS.grow mv 2 ---- -- Extra memory at the end of the newly allocated vector is initialized -- to 0 bytes, which for Storable instance will usually correspond -- to some default value for a particular type, eg. 0 for -- Int, False for Bool, etc. However, if -- unsafeGrow was used instead this would not have been guaranteed -- and some garbage would be there instead: -- --
-- >>> VS.freeze mv' -- [10,20,30,0,0] ---- -- Having the extra space we can write new values in there: -- --
-- >>> MVS.write mv' 3 999 -- -- >>> VS.freeze mv' -- [10,20,30,999,0] ---- -- It is important to note that the source mutable vector is not affected -- when the newly allocated one is mutated. -- --
-- >>> MVS.write mv' 2 888 -- -- >>> VS.freeze mv' -- [10,20,888,999,0] -- -- >>> VS.freeze mv -- [10,20,30] --grow :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Grow a vector by the given number of elements. The number must be -- non-negative but this is not checked. Same semantics as in -- unsafeGrow for generic vector. unsafeGrow :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a) -- | Reset all elements of the vector to some undefined value, clearing all -- references to external objects. This is usually a noop for unboxed -- vectors. clear :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> m () -- | Yield the element at the given position. read :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. write :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. modify :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. modifyM :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. swap :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. exchange :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m a -- | Yield the element at the given position. No bounds checks are -- performed. unsafeRead :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a -- | Replace the element at the given position. No bounds checks are -- performed. unsafeWrite :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m () -- | Modify the element at the given position. No bounds checks are -- performed. unsafeModify :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m () -- | Modify the element at the given position using a monadic function. No -- bounds checks are performed. unsafeModifyM :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m () -- | Swap the elements at the given positions. No bounds checks are -- performed. unsafeSwap :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m () -- | Replace the element at the given position and return the old element. -- No bounds checks are performed. unsafeExchange :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m a -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. mapM_ :: (PrimMonad m, Storable a) => (a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. imapM_ :: (PrimMonad m, Storable a) => (Int -> a -> m b) -> MVector (PrimState m) a -> m () -- | O(n) Apply the monadic action to every element of the vector, -- discarding the results. It's same as the flip mapM_. forM_ :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to every element of the vector -- and its index, discarding the results. It's same as the flip -- imapM_. iforM_ :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (Int -> a -> m b) -> m () -- | O(n) Pure left fold. foldl :: (PrimMonad m, Storable a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator. foldl' :: (PrimMonad m, Storable a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold. foldM :: (PrimMonad m, Storable a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator. foldM' :: (PrimMonad m, Storable a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold. foldr :: (PrimMonad m, Storable a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator. foldr' :: (PrimMonad m, Storable a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold. foldrM :: (PrimMonad m, Storable a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator. foldrM' :: (PrimMonad m, Storable a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold (function applied to each element and its -- index). ifoldl :: (PrimMonad m, Storable a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure left fold with strict accumulator (function applied -- to each element and its index). ifoldl' :: (PrimMonad m, Storable a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold (action applied to each element and its -- index). ifoldM :: (PrimMonad m, Storable a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index). ifoldM' :: (PrimMonad m, Storable a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold (function applied to each element and its -- index). ifoldr :: (PrimMonad m, Storable a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Pure right fold with strict accumulator (function applied -- to each element and its index). ifoldr' :: (PrimMonad m, Storable a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold (action applied to each element and its -- index). ifoldrM :: (PrimMonad m, Storable a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | O(n) Monadic right fold with strict accumulator (action applied -- to each element and its index). ifoldrM' :: (PrimMonad m, Storable a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b -- | Compute the next (lexicographically) permutation of given vector -- in-place. Returns False when input is the last permutation nextPermutation :: (PrimMonad m, Storable e, Ord e) => MVector (PrimState m) e -> m Bool -- | Set all elements of the vector to the given value. set :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. copy :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length. -- -- If the vectors do not overlap, then this is equivalent to copy. -- Otherwise, the copying is performed as if the source vector were -- copied to a temporary vector and then the temporary vector was copied -- to the target vector. move :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Copy a vector. The two vectors must have the same length and may not -- overlap. This is not checked. unsafeCopy :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | Move the contents of a vector. The two vectors must have the same -- length, but this is not checked. -- -- If the vectors do not overlap, then this is equivalent to -- unsafeCopy. Otherwise, the copying is performed as if the -- source vector were copied to a temporary vector and then the temporary -- vector was copied to the target vector. unsafeMove :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m () -- | O(1) Unsafely cast a mutable vector from one element type to -- another. The operation just changes the type of the underlying pointer -- and does not modify the elements. -- -- The resulting vector contains as many elements as can fit into the -- underlying memory block. unsafeCast :: forall a b s. (Storable a, Storable b) => MVector s a -> MVector s b -- | Create a mutable vector from a ForeignPtr with an offset and a -- length. -- -- Modifying data through the ForeignPtr afterwards is unsafe if -- the vector could have been frozen before the modification. -- -- If your offset is 0 it is more efficient to use -- unsafeFromForeignPtr0. unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> MVector s a -- | O(1) Create a mutable vector from a ForeignPtr and a -- length. -- -- It is assumed the pointer points directly to the data (no offset). Use -- unsafeFromForeignPtr if you need to specify an offset. -- -- Modifying data through the ForeignPtr afterwards is unsafe if -- the vector could have been frozen before the modification. unsafeFromForeignPtr0 :: Storable a => ForeignPtr a -> Int -> MVector s a -- | Yield the underlying ForeignPtr together with the offset to the -- data and its length. Modifying the data through the ForeignPtr -- is unsafe if the vector could have frozen before the modification. unsafeToForeignPtr :: Storable a => MVector s a -> (ForeignPtr a, Int, Int) -- | O(1) Yield the underlying ForeignPtr together with its -- length. -- -- You can assume the pointer points directly to the data (no offset). -- -- Modifying the data through the ForeignPtr is unsafe if the -- vector could have frozen before the modification. unsafeToForeignPtr0 :: Storable a => MVector s a -> (ForeignPtr a, Int) -- | Pass a pointer to the vector's data to the IO action. Modifying data -- through the pointer is unsafe if the vector could have been frozen -- before the modification. unsafeWith :: Storable a => IOVector a -> (Ptr a -> IO b) -> IO b instance Control.DeepSeq.NFData (Data.Vector.Storable.Mutable.MVector s a) instance Control.DeepSeq.NFData1 (Data.Vector.Storable.Mutable.MVector s) instance Foreign.Storable.Storable a => Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Storable.Mutable.MVector a -- | Storable-based vectors. module Data.Vector.Storable -- | Storable-based vectors data Vector a -- | Mutable Storable-based vectors data MVector s a MVector :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !ForeignPtr a -> MVector s 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. class Storable a -- | O(1) Yield the length of the vector length :: Storable a => Vector a -> Int -- | O(1) Test whether a vector is empty null :: Storable a => Vector a -> Bool -- | O(1) Indexing (!) :: Storable a => Vector a -> Int -> a -- | O(1) Safe indexing (!?) :: Storable a => Vector a -> Int -> Maybe a -- | O(1) First element head :: Storable a => Vector a -> a -- | O(1) Last element last :: Storable a => Vector a -> a -- | O(1) Unsafe indexing without bounds checking unsafeIndex :: Storable a => Vector a -> Int -> a -- | O(1) First element without checking if the vector is empty unsafeHead :: Storable a => Vector a -> a -- | O(1) Last element without checking if the vector is empty unsafeLast :: Storable a => Vector a -> a -- | O(1) Indexing in a monad. -- -- The monad allows operations to be strict in the vector when necessary. -- Suppose vector copying is implemented like this: -- --
-- copy mv v = ... write mv i (v ! i) ... ---- -- For lazy vectors, v ! i would not be evaluated which means -- that mv would unnecessarily retain a reference to v -- in each element written. -- -- With indexM, copying can be implemented like this instead: -- --
-- copy mv v = ... do -- x <- indexM v i -- write mv i x ---- -- Here, no references to v are retained because indexing (but -- not the elements) is evaluated eagerly. indexM :: (Storable a, Monad m) => Vector a -> Int -> m a -- | O(1) First element of a vector in a monad. See indexM -- for an explanation of why this is useful. headM :: (Storable a, Monad m) => Vector a -> m a -- | O(1) Last element of a vector in a monad. See indexM for -- an explanation of why this is useful. lastM :: (Storable a, Monad m) => Vector a -> m a -- | O(1) Indexing in a monad without bounds checks. See -- indexM for an explanation of why this is useful. unsafeIndexM :: (Storable a, Monad m) => Vector a -> Int -> m a -- | O(1) First element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeHeadM :: (Storable a, Monad m) => Vector a -> m a -- | O(1) Last element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeLastM :: (Storable a, Monad m) => Vector a -> m a -- | O(1) Yield a slice of the vector without copying it. The vector -- must contain at least i+n elements. slice :: Storable a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty. init :: Storable a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty. tail :: Storable a => Vector a -> Vector a -- | O(1) Yield at the first n elements without copying. -- The vector may contain less than n elements in which case it -- is returned unchanged. take :: Storable a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector may contain less than n elements in which -- case an empty vector is returned. drop :: Storable a => Int -> Vector a -> Vector a -- | O(1) Yield the first n elements paired with the -- remainder without copying. -- -- Note that splitAt n v is equivalent to -- (take n v, drop n v) but slightly more -- efficient. splitAt :: Storable a => Int -> Vector a -> (Vector a, Vector a) -- | O(1) Yield the head and tail of the vector, or -- Nothing if empty. uncons :: Storable a => Vector a -> Maybe (a, Vector a) -- | O(1) Yield the last and init of the vector, or -- Nothing if empty. unsnoc :: Storable a => Vector a -> Maybe (Vector a, a) -- | O(1) Yield a slice of the vector without copying. The vector -- must contain at least i+n elements but this is not checked. unsafeSlice :: Storable a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty but this is not checked. unsafeInit :: Storable a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty but this is not checked. unsafeTail :: Storable a => Vector a -> Vector a -- | O(1) Yield the first n elements without copying. The -- vector must contain at least n elements but this is not -- checked. unsafeTake :: Storable a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector must contain at least n elements but this -- is not checked. unsafeDrop :: Storable a => Int -> Vector a -> Vector a -- | O(1) Empty vector empty :: Storable a => Vector a -- | O(1) Vector with exactly one element singleton :: Storable a => a -> Vector a -- | O(n) Vector of the given length with the same value in each -- position replicate :: Storable a => Int -> a -> Vector a -- | O(n) Construct a vector of the given length by applying the -- function to each index generate :: Storable a => Int -> (Int -> a) -> Vector a -- | O(n) Apply function <math> times to an initial value, -- producing a vector of length <math>. Zeroth element will contain -- the initial value, that's why there is one less function application -- than the number of elements in the produced vector. -- -- <math> -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.iterateN 0 undefined undefined :: VS.Vector Int -- [] -- -- >>> VS.iterateN 26 succ 'a' -- "abcdefghijklmnopqrstuvwxyz" --iterateN :: Storable a => Int -> (a -> a) -> a -> Vector a -- | O(n) Execute the monadic action the given number of times and -- store the results in a vector. replicateM :: (Monad m, Storable a) => Int -> m a -> m (Vector a) -- | O(n) Construct a vector of the given length by applying the -- monadic action to each index generateM :: (Monad m, Storable a) => Int -> (Int -> m a) -> m (Vector a) -- | O(n) Apply monadic function <math> times to an initial -- value, producing a vector of length <math>. Zeroth element will -- contain the initial value, that's why there is one less function -- application than the number of elements in the produced vector. -- -- For non-monadic version see iterateN iterateNM :: (Monad m, Storable a) => Int -> (a -> m a) -> a -> m (Vector a) -- | Execute the monadic action and freeze the resulting vector. -- --
-- create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>
--
create :: Storable a => (forall s. ST s (MVector s a)) -> Vector a
-- | Execute the monadic action and freeze the resulting vectors.
createT :: (Traversable f, Storable a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
-- | O(n) Construct a vector by repeatedly applying the generator
-- function to a seed. The generator function yields Just the next
-- element and the new seed or Nothing if there are no more
-- elements.
--
-- -- unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 -- = <10,9,8,7,6,5,4,3,2,1> --unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with at most n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields Just the next element and the new seed or -- Nothing if there are no more elements. -- --
-- unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8> --unfoldrN :: Storable a => Int -> (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields the next element and the new seed. -- --
-- unfoldrExactN 3 (\n -> (n,n-1)) 10 = <10,9,8> --unfoldrExactN :: Storable a => Int -> (b -> (a, b)) -> b -> Vector a -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrM :: (Monad m, Storable a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrNM :: (Monad m, Storable a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the monadic generator function to a seed. The -- generator function yields the next element and the new seed. unfoldrExactNM :: (Monad m, Storable a) => Int -> (b -> m (a, b)) -> b -> m (Vector a) -- | O(n) Construct a vector with n elements by repeatedly -- applying the generator function to the already constructed part of the -- vector. -- --
-- constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in <a,b,c> --constructN :: Storable a => Int -> (Vector a -> a) -> Vector a -- | O(n) Construct a vector with n elements from right to -- left by repeatedly applying the generator function to the already -- constructed part of the vector. -- --
-- constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in <c,b,a> --constructrN :: Storable a => Int -> (Vector a -> a) -> Vector a -- | O(n) Yield a vector of the given length containing the values -- x, x+1 etc. This operation is usually more efficient -- than enumFromTo. -- --
-- enumFromN 5 3 = <5,6,7> --enumFromN :: (Storable a, Num a) => a -> Int -> Vector a -- | O(n) 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. -- --
-- enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4> --enumFromStepN :: (Storable a, Num a) => a -> a -> Int -> Vector a -- | O(n) Enumerate values from x to y. -- -- WARNING: This operation can be very inefficient. If at all -- possible, use enumFromN instead. enumFromTo :: (Storable a, Enum a) => a -> a -> Vector a -- | O(n) 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. enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector a -- | O(n) Prepend an element cons :: Storable a => a -> Vector a -> Vector a -- | O(n) Append an element snoc :: Storable a => Vector a -> a -> Vector a -- | O(m+n) Concatenate two vectors (++) :: Storable a => Vector a -> Vector a -> Vector a infixr 5 ++ -- | O(n) Concatenate all vectors in the list concat :: Storable a => [Vector a] -> Vector a -- | O(n) Yield the argument but force it not to retain any extra -- memory, possibly by copying it. -- -- This is especially useful when dealing with slices. For example: -- --
-- force (slice 0 2 <huge vector>) ---- -- Here, the slice retains a reference to the huge vector. Forcing it -- creates a copy of just the elements that belong to the slice and -- allows the huge vector to be garbage collected. force :: Storable a => Vector a -> Vector a -- | O(m+n) For each pair (i,a) from the list, replace the -- vector element at position i by a. -- --
-- <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7> --(//) :: Storable a => Vector a -> [(Int, a)] -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value a from the value vector, replace -- the element of the initial vector at position i by -- a. -- --
-- update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7> --update_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a -- | Same as (//) but without bounds checking. unsafeUpd :: Storable a => Vector a -> [(Int, a)] -> Vector a -- | Same as update_ but without bounds checking. unsafeUpdate_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a -- | O(m+n) For each pair (i,b) from the list, replace the -- vector element a at position i by f a b. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.accum (+) (VS.fromList [1000.0,2000.0,3000.0]) [(2,4),(1,6),(0,3),(1,10)] -- [1003.0,2016.0,3004.0] --accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value b from the the value vector, -- replace the element of the initial vector at position i by -- f a b. -- --
-- accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4> --accumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | Same as accum but without bounds checking. unsafeAccum :: Storable a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | Same as accumulate_ but without bounds checking. unsafeAccumulate_ :: (Storable a, Storable b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | O(n) Reverse a vector reverse :: Storable a => Vector a -> Vector a -- | O(n) Yield the vector obtained by replacing each element -- i of the index vector by xs!i. This is -- equivalent to map (xs!) is but is often much -- more efficient. -- --
-- backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a> --backpermute :: Storable a => Vector a -> Vector Int -> Vector a -- | Same as backpermute but without bounds checking. unsafeBackpermute :: Storable a => Vector a -> Vector Int -> Vector a -- | Apply a destructive operation to a vector. The operation will be -- performed in place if it is safe to do so and will modify a copy of -- the vector otherwise. -- --
-- modify (\v -> write v 0 'x') (replicate 3 'a') = <'x','a','a'> --modify :: Storable a => (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a -- | O(n) Map a function over a vector map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b -- | O(n) Apply a function to every element of a vector and its -- index imap :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b -- | Map a function over a vector and concatenate the results. concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results mapM :: (Monad m, Storable a, Storable b) => (a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to every element of a vector and -- its index, yielding a vector of results imapM :: (Monad m, Storable a, Storable b) => (Int -> a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results mapM_ :: (Monad m, Storable a) => (a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to every element of a vector and -- its index, ignoring the results imapM_ :: (Monad m, Storable a) => (Int -> a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results. Equivalent to flip mapM. forM :: (Monad m, Storable a, Storable b) => Vector a -> (a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results. Equivalent to flip mapM_. forM_ :: (Monad m, Storable a) => Vector a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to all elements of the vector and -- their indices, yielding a vector of results. Equivalent to flip -- imapM. iforM :: (Monad m, Storable a, Storable b) => Vector a -> (Int -> a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of the vector and -- their indices and ignore the results. Equivalent to flip -- imapM_. iforM_ :: (Monad m, Storable a) => Vector a -> (Int -> a -> m b) -> m () -- | O(min(m,n)) Zip two vectors with the given function. zipWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three 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 d 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 e 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 f 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 g -- | O(min(m,n)) Zip two vectors with a function that also takes the -- elements' indices. izipWith :: (Storable a, Storable b, Storable c) => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three 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 d 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 e 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 f 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 g -- | O(min(m,n)) Zip the two vectors with the monadic action and -- yield a vector of results zipWithM :: (Monad m, Storable a, Storable b, Storable c) => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and yield a vector of results izipWithM :: (Monad m, Storable a, Storable b, Storable c) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- ignore the results zipWithM_ :: (Monad m, Storable a, Storable b) => (a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and ignore the results izipWithM_ :: (Monad m, Storable a, Storable b) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(n) Drop elements that do not satisfy the predicate filter :: Storable a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the predicate which is -- applied to values and their indices ifilter :: Storable a => (Int -> a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the monadic predicate filterM :: (Monad m, Storable a) => (a -> m Bool) -> Vector a -> m (Vector a) -- | O(n) Drop repeated adjacent elements. uniq :: (Storable a, Eq a) => Vector a -> Vector a -- | O(n) Drop elements when predicate returns Nothing mapMaybe :: (Storable a, Storable b) => (a -> Maybe b) -> Vector a -> Vector b -- | O(n) Drop elements when predicate, applied to index and value, -- returns Nothing imapMaybe :: (Storable a, Storable b) => (Int -> a -> Maybe b) -> Vector a -> Vector b -- | O(n) Apply monadic function to each element of vector and -- discard elements returning Nothing. mapMaybeM :: (Monad m, Storable a, Storable b) => (a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Apply monadic function to each element of vector and its -- index. Discards elements returning Nothing. imapMaybeM :: (Monad m, Storable a, Storable b) => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Yield the longest prefix of elements satisfying the -- predicate. Current implementation is not copy-free, unless the result -- vector is fused away. takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop the longest prefix of elements that satisfy the -- predicate without copying. dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a -- | O(n) 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. partition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) 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 but the -- operation is often faster than partition. unstablePartition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into two parts, the first one containing -- the Left elements and the second containing the -- Right elements. The relative order of the elements is -- preserved. partitionWith :: (Storable a, Storable b, Storable c) => (a -> Either b c) -> Vector a -> (Vector b, Vector c) -- | O(n) Split the vector into the longest prefix of elements that -- satisfy the predicate and the rest without copying. span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into the longest prefix of elements that -- do not satisfy the predicate and the rest without copying. break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Check if the vector contains an element elem :: (Storable a, Eq a) => a -> Vector a -> Bool infix 4 `elem` -- | O(n) Check if the vector does not contain an element (inverse -- of elem) notElem :: (Storable a, Eq a) => a -> Vector a -> Bool infix 4 `notElem` -- | O(n) Yield Just the first element matching the predicate -- or Nothing if no such element exists. find :: Storable a => (a -> Bool) -> Vector a -> Maybe a -- | O(n) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int -- | O(n) Yield the indices of elements satisfying the predicate in -- ascending order. findIndices :: Storable a => (a -> Bool) -> Vector a -> Vector Int -- | O(n) Yield Just the index of the first occurence of the -- given element or Nothing if the vector does not contain the -- element. This is a specialised version of findIndex. elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe Int -- | O(n) Yield the indices of all occurences of the given element -- in ascending order. This is a specialised version of -- findIndices. elemIndices :: (Storable a, Eq a) => a -> Vector a -> Vector Int -- | O(n) Left fold foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors foldl1 :: Storable a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold with strict accumulator foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors with strict accumulator foldl1' :: Storable a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold foldr :: Storable a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors foldr1 :: Storable a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold with a strict accumulator foldr' :: Storable a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors with strict accumulator foldr1' :: Storable a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold (function applied to each element and its index) ifoldl :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold with strict accumulator (function applied to -- each element and its index) ifoldl' :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Right fold (function applied to each element and its -- index) ifoldr :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold with strict accumulator (function applied to -- each element and its index) ifoldr' :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Map each element of the structure to a monoid, and combine -- the results. It uses same implementation as corresponding method of -- Foldable type cless. Note it's implemented in terms of -- foldr and won't fuse with functions that traverse vector from -- left to right (map, generate, etc.). foldMap :: (Monoid m, Storable a) => (a -> m) -> Vector a -> m -- | O(n) foldMap which is strict in accumulator. It uses -- same implementation as corresponding method of Foldable type -- class. Note it's implemented in terms of foldl' so it fuses in -- most contexts. foldMap' :: (Monoid m, Storable a) => (a -> m) -> Vector a -> m -- | O(n) Check if all elements satisfy the predicate. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.all even $ VS.fromList [2, 4, 12 :: Int] -- True -- -- >>> VS.all even $ VS.fromList [2, 4, 13 :: Int] -- False -- -- >>> VS.all even (VS.empty :: VS.Vector Int) -- True --all :: Storable a => (a -> Bool) -> Vector a -> Bool -- | O(n) Check if any element satisfies the predicate. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.any even $ VS.fromList [1, 3, 7 :: Int] -- False -- -- >>> VS.any even $ VS.fromList [3, 2, 13 :: Int] -- True -- -- >>> VS.any even (VS.empty :: VS.Vector Int) -- False --any :: Storable a => (a -> Bool) -> Vector a -> Bool -- | O(n) Check if all elements are True -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.and $ VS.fromList [True, False] -- False -- -- >>> VS.and VS.empty -- True --and :: Vector Bool -> Bool -- | O(n) Check if any element is True -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.or $ VS.fromList [True, False] -- True -- -- >>> VS.or VS.empty -- False --or :: Vector Bool -> Bool -- | O(n) Compute the sum of the elements -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.sum $ VS.fromList [300,20,1 :: Int] -- 321 -- -- >>> VS.sum (VS.empty :: VS.Vector Int) -- 0 --sum :: (Storable a, Num a) => Vector a -> a -- | O(n) Compute the produce of the elements -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.product $ VS.fromList [1,2,3,4 :: Int] -- 24 -- -- >>> VS.product (VS.empty :: VS.Vector Int) -- 1 --product :: (Storable a, Num a) => Vector a -> a -- | O(n) Yield the maximum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.maximum $ VS.fromList [2.0, 1.0] -- 2.0 --maximum :: (Storable a, Ord a) => Vector a -> a -- | O(n) Yield the maximum element of the vector according to the -- given comparison function. The vector may not be empty. maximumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the minimum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.minimum $ VS.fromList [2.0, 1.0] -- 1.0 --minimum :: (Storable a, Ord a) => Vector a -> a -- | O(n) Yield the minimum element of the vector according to the -- given comparison function. The vector may not be empty. minimumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the index of the minimum element of the vector. The -- vector may not be empty. minIndex :: (Storable a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the minimum element of the vector -- according to the given comparison function. The vector may not be -- empty. minIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector. The -- vector may not be empty. maxIndex :: (Storable a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector -- according to the given comparison function. The vector may not be -- empty. maxIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Monadic fold foldM :: (Monad m, Storable b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold (action applied to each element and its -- index) ifoldM :: (Monad m, Storable b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator foldM' :: (Monad m, Storable b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index) ifoldM' :: (Monad m, Storable b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold over non-empty vectors fold1M :: (Monad m, Storable a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator fold1M' :: (Monad m, Storable a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold that discards the result foldM_ :: (Monad m, Storable b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold that discards the result (action applied to -- each element and its index) ifoldM_ :: (Monad m, Storable b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result foldM'_ :: (Monad m, Storable b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result (action applied to each element and its index) ifoldM'_ :: (Monad m, Storable b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold over non-empty vectors that discards the -- result fold1M_ :: (Monad m, Storable a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator that discards the result fold1M'_ :: (Monad m, Storable a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Prescan -- --
-- prescanl f z = init . scanl f z ---- -- Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6> prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Prescan with strict accumulator prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan -- --
-- postscanl f z = tail . scanl f z ---- -- Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10> postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan with strict accumulator postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan -- --
-- 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 :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan with strict accumulator scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a non-empty vector -- --
-- scanl f <x1,...,xn> = <y1,...,yn> -- where y1 = x1 -- yi = f y(i-1) xi --scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a non-empty vector with a strict accumulator scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a vector with its index iscanl :: (Storable a, Storable b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a vector (strictly) with its index iscanl' :: (Storable a, Storable b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Right-to-left prescan -- --
-- prescanr f z = reverse . prescanl (flip f) z . reverse --prescanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left prescan with strict accumulator prescanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan postscanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan with strict accumulator postscanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan with strict accumulator scanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a non-empty vector scanr1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a non-empty vector with a strict -- accumulator scanr1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a vector with its index iscanr :: (Storable a, Storable b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a vector (strictly) with its index iscanr' :: (Storable a, Storable b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Check if two vectors are equal using supplied equality -- predicate. eqBy :: (Storable a, Storable b) => (a -> b -> Bool) -> Vector a -> Vector b -> Bool -- | O(n) Compare two vectors using supplied comparison function for -- vector elements. Comparison works same as for lists. -- --
-- cmpBy compare == compare --cmpBy :: (Storable a, Storable b) => (a -> b -> Ordering) -> Vector a -> Vector b -> Ordering -- | Checks whether two values are same vector: they have same length and -- share same buffer. -- --
-- >>> let xs = fromList [0/0::Double] in isSameVector xs xs -- True --isSameVector :: Storable a => Vector a -> Vector a -> Bool -- | O(n) Convert a vector to a list toList :: Storable a => Vector a -> [a] -- | O(n) Convert a list to a vector fromList :: Storable a => [a] -> Vector a -- | O(n) Convert the first n elements of a list to a -- vector -- --
-- fromListN n xs = fromList (take n xs) ---- --
-- >>> import qualified Data.Vector.Storable as VS -- -- >>> VS.fromListN 3 [1,2,3,4,5::Int] -- [1,2,3] -- -- >>> VS.fromListN 3 [1::Int] -- [1] --fromListN :: Storable a => Int -> [a] -> Vector a -- | O(n) Convert different vector types convert :: (Vector v a, Vector w a) => v a -> w a -- | O(1) Unsafely cast a vector from one element type to another. -- The operation just changes the type of the underlying pointer and does -- not modify the elements. -- -- The resulting vector contains as many elements as can fit into the -- underlying memory block. unsafeCast :: forall a b. (Storable a, Storable b) => Vector a -> Vector b -- | O(n) Yield an immutable copy of the mutable vector. freeze :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(n) Yield a mutable copy of the immutable vector. thaw :: (Storable a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. copy :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () -- | O(1) Unsafe convert a mutable vector to an immutable one -- without copying. The mutable vector may not be used after this -- operation. unsafeFreeze :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(1) Unsafely convert an immutable vector to a mutable one -- without copying. Note that this is very dangerous function and -- generally it's only safe to read from resulting vector. In which case -- immutable vector could be used safely as well. -- -- Problem with mutation happens because GHC has a lot of freedom to -- introduce sharing. As a result mutable vectors produced by -- unsafeThaw may or may not share same underlying buffer. For -- example: -- --
-- foo = do -- let vec = V.generate 10 id -- mvec <- V.unsafeThaw vec -- do_something mvec ---- -- Here GHC could lift vec outside of foo which means all calls -- to do_something will use same buffer with possibly disastrous -- results. Whether such aliasing happens or not depends on program in -- question, optimization levels, and GHC flags. -- -- All in all attempts to modify vector after unsafeThaw falls out of -- domain of software engineering and into realm of black magic, dark -- rituals, and unspeakable horrors. Only advice that could be given is: -- "don't attempt to mutate vector after unsafeThaw unless you know how -- to prevent GHC from aliasing buffers accidentally. We don't" unsafeThaw :: (Storable a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. This is not checked. unsafeCopy :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () -- | O(1) Create a vector from a ForeignPtr with an offset -- and a length. -- -- The data may not be modified through the ForeignPtr afterwards. -- -- If your offset is 0 it is more efficient to use -- unsafeFromForeignPtr0. unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a -- | O(1) Create a vector from a ForeignPtr and a length. -- -- It is assumed the pointer points directly to the data (no offset). Use -- unsafeFromForeignPtr if you need to specify an offset. -- -- The data may not be modified through the ForeignPtr afterwards. unsafeFromForeignPtr0 :: Storable a => ForeignPtr a -> Int -> Vector a -- | O(1) Yield the underlying ForeignPtr together with the -- offset to the data and its length. The data may not be modified -- through the ForeignPtr. unsafeToForeignPtr :: Storable a => Vector a -> (ForeignPtr a, Int, Int) -- | O(1) Yield the underlying ForeignPtr together with its -- length. -- -- You can assume the pointer points directly to the data (no offset). -- -- The data may not be modified through the ForeignPtr. unsafeToForeignPtr0 :: Storable a => Vector a -> (ForeignPtr a, Int) -- | Pass a pointer to the vector's data to the IO action. The data may not -- be modified through the 'Ptr. unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b instance Control.DeepSeq.NFData (Data.Vector.Storable.Vector a) instance Control.DeepSeq.NFData1 Data.Vector.Storable.Vector instance (GHC.Show.Show a, Foreign.Storable.Storable a) => GHC.Show.Show (Data.Vector.Storable.Vector a) instance (GHC.Read.Read a, Foreign.Storable.Storable a) => GHC.Read.Read (Data.Vector.Storable.Vector a) instance (Data.Data.Data a, Foreign.Storable.Storable a) => Data.Data.Data (Data.Vector.Storable.Vector a) instance Foreign.Storable.Storable a => Data.Vector.Generic.Base.Vector Data.Vector.Storable.Vector a instance (Foreign.Storable.Storable a, GHC.Classes.Eq a) => GHC.Classes.Eq (Data.Vector.Storable.Vector a) instance (Foreign.Storable.Storable a, GHC.Classes.Ord a) => GHC.Classes.Ord (Data.Vector.Storable.Vector a) instance Foreign.Storable.Storable a => GHC.Base.Semigroup (Data.Vector.Storable.Vector a) instance Foreign.Storable.Storable a => GHC.Base.Monoid (Data.Vector.Storable.Vector a) instance Foreign.Storable.Storable a => GHC.Exts.IsList (Data.Vector.Storable.Vector a) -- | Adaptive unboxed vectors. The implementation is based on type families -- and picks an efficient, specialised representation for every element -- type. In particular, unboxed vectors of pairs are represented as pairs -- of unboxed vectors. -- -- Implementing unboxed vectors for new data types can be very easy. Here -- is how the library does this for Complex by simply wrapping -- vectors of pairs. -- --
-- newtype instance MVector s (Complex a) = MV_Complex (MVector s (a,a))
-- newtype instance Vector (Complex a) = V_Complex (Vector (a,a))
--
-- instance (RealFloat a, Unbox a) => MVector MVector (Complex a) where
-- {-# INLINE basicLength #-}
-- basicLength (MV_Complex v) = basicLength v
-- ...
--
-- instance (RealFloat a, Unbox a) => Data.Vector.Generic.Vector Vector (Complex a) where
-- {-# INLINE basicLength #-}
-- basicLength (V_Complex v) = Data.Vector.Generic.basicLength v
-- ...
--
-- instance (RealFloat a, Unbox a) => Unbox (Complex a)
--
module Data.Vector.Unboxed
data family Vector a
data family MVector s a
class (Vector Vector a, MVector MVector a) => Unbox a
-- | O(1) Yield the length of the vector
length :: Unbox a => Vector a -> Int
-- | O(1) Test whether a vector is empty
null :: Unbox a => Vector a -> Bool
-- | O(1) Indexing
(!) :: Unbox a => Vector a -> Int -> a
-- | O(1) Safe indexing
(!?) :: Unbox a => Vector a -> Int -> Maybe a
-- | O(1) First element
head :: Unbox a => Vector a -> a
-- | O(1) Last element
last :: Unbox a => Vector a -> a
-- | O(1) Unsafe indexing without bounds checking
unsafeIndex :: Unbox a => Vector a -> Int -> a
-- | O(1) First element without checking if the vector is empty
unsafeHead :: Unbox a => Vector a -> a
-- | O(1) Last element without checking if the vector is empty
unsafeLast :: Unbox a => Vector a -> a
-- | O(1) Indexing in a monad.
--
-- The monad allows operations to be strict in the vector when necessary.
-- Suppose vector copying is implemented like this:
--
-- -- copy mv v = ... write mv i (v ! i) ... ---- -- For lazy vectors, v ! i would not be evaluated which means -- that mv would unnecessarily retain a reference to v -- in each element written. -- -- With indexM, copying can be implemented like this instead: -- --
-- copy mv v = ... do -- x <- indexM v i -- write mv i x ---- -- Here, no references to v are retained because indexing (but -- not the elements) is evaluated eagerly. indexM :: (Unbox a, Monad m) => Vector a -> Int -> m a -- | O(1) First element of a vector in a monad. See indexM -- for an explanation of why this is useful. headM :: (Unbox a, Monad m) => Vector a -> m a -- | O(1) Last element of a vector in a monad. See indexM for -- an explanation of why this is useful. lastM :: (Unbox a, Monad m) => Vector a -> m a -- | O(1) Indexing in a monad without bounds checks. See -- indexM for an explanation of why this is useful. unsafeIndexM :: (Unbox a, Monad m) => Vector a -> Int -> m a -- | O(1) First element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeHeadM :: (Unbox a, Monad m) => Vector a -> m a -- | O(1) Last element in a monad without checking for empty -- vectors. See indexM for an explanation of why this is useful. unsafeLastM :: (Unbox a, Monad m) => Vector a -> m a -- | O(1) Yield a slice of the vector without copying it. The vector -- must contain at least i+n elements. slice :: Unbox a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty. init :: Unbox a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty. tail :: Unbox a => Vector a -> Vector a -- | O(1) Yield at the first n elements without copying. -- The vector may contain less than n elements in which case it -- is returned unchanged. take :: Unbox a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector may contain less than n elements in which -- case an empty vector is returned. drop :: Unbox a => Int -> Vector a -> Vector a -- | O(1) Yield the first n elements paired with the -- remainder without copying. -- -- Note that splitAt n v is equivalent to -- (take n v, drop n v) but slightly more -- efficient. splitAt :: Unbox a => Int -> Vector a -> (Vector a, Vector a) -- | O(1) Yield the head and tail of the vector, or -- Nothing if empty. uncons :: Unbox a => Vector a -> Maybe (a, Vector a) -- | O(1) Yield the last and init of the vector, or -- Nothing if empty. unsnoc :: Unbox a => Vector a -> Maybe (Vector a, a) -- | O(1) Yield a slice of the vector without copying. The vector -- must contain at least i+n elements but this is not checked. unsafeSlice :: Unbox a => Int -> Int -> Vector a -> Vector a -- | O(1) Yield all but the last element without copying. The vector -- may not be empty but this is not checked. unsafeInit :: Unbox a => Vector a -> Vector a -- | O(1) Yield all but the first element without copying. The -- vector may not be empty but this is not checked. unsafeTail :: Unbox a => Vector a -> Vector a -- | O(1) Yield the first n elements without copying. The -- vector must contain at least n elements but this is not -- checked. unsafeTake :: Unbox a => Int -> Vector a -> Vector a -- | O(1) Yield all but the first n elements without -- copying. The vector must contain at least n elements but this -- is not checked. unsafeDrop :: Unbox a => Int -> Vector a -> Vector a -- | O(1) Empty vector empty :: Unbox a => Vector a -- | O(1) Vector with exactly one element singleton :: Unbox a => a -> Vector a -- | O(n) Vector of the given length with the same value in each -- position replicate :: Unbox a => Int -> a -> Vector a -- | O(n) Construct a vector of the given length by applying the -- function to each index generate :: Unbox a => Int -> (Int -> a) -> Vector a -- | O(n) Apply function <math> times to an initial value, -- producing a vector of length <math>. Zeroth element will contain -- the initial value, that's why there is one less function application -- than the number of elements in the produced vector. -- -- <math> -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.iterateN 0 undefined undefined :: VU.Vector Int -- [] -- -- >>> VU.iterateN 3 (\(i, c) -> (pred i, succ c)) (0 :: Int, 'a') -- [(0,'a'),(-1,'b'),(-2,'c')] --iterateN :: Unbox a => Int -> (a -> a) -> a -> Vector a -- | O(n) Execute the monadic action the given number of times and -- store the results in a vector. replicateM :: (Monad m, Unbox a) => Int -> m a -> m (Vector a) -- | O(n) Construct a vector of the given length by applying the -- monadic action to each index generateM :: (Monad m, Unbox a) => Int -> (Int -> m a) -> m (Vector a) -- | O(n) Apply monadic function <math> times to an initial -- value, producing a vector of length <math>. Zeroth element will -- contain the initial value, that's why there is one less function -- application than the number of elements in the produced vector. -- -- For non-monadic version see iterateN iterateNM :: (Monad m, Unbox a) => Int -> (a -> m a) -> a -> m (Vector a) -- | Execute the monadic action and freeze the resulting vector. -- --
-- create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>
--
create :: Unbox a => (forall s. ST s (MVector s a)) -> Vector a
-- | Execute the monadic action and freeze the resulting vectors.
createT :: (Traversable f, Unbox a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
-- | O(n) Construct a vector by repeatedly applying the generator
-- function to a seed. The generator function yields Just the next
-- element and the new seed or Nothing if there are no more
-- elements.
--
-- -- unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10 -- = <10,9,8,7,6,5,4,3,2,1> --unfoldr :: Unbox a => (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with at most n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields Just the next element and the new seed or -- Nothing if there are no more elements. -- --
-- unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8> --unfoldrN :: Unbox a => Int -> (b -> Maybe (a, b)) -> b -> Vector a -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the generator function to a seed. The generator -- function yields the next element and the new seed. -- --
-- unfoldrExactN 3 (\n -> (n,n-1)) 10 = <10,9,8> --unfoldrExactN :: Unbox a => Int -> (b -> (a, b)) -> b -> Vector a -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrM :: (Monad m, Unbox a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector by repeatedly applying the monadic -- generator function to a seed. The generator function yields -- Just the next element and the new seed or Nothing if -- there are no more elements. unfoldrNM :: (Monad m, Unbox a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a) -- | O(n) Construct a vector with exactly n elements by -- repeatedly applying the monadic generator function to a seed. The -- generator function yields the next element and the new seed. unfoldrExactNM :: (Monad m, Unbox a) => Int -> (b -> m (a, b)) -> b -> m (Vector a) -- | O(n) Construct a vector with n elements by repeatedly -- applying the generator function to the already constructed part of the -- vector. -- --
-- constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in <a,b,c> --constructN :: Unbox a => Int -> (Vector a -> a) -> Vector a -- | O(n) Construct a vector with n elements from right to -- left by repeatedly applying the generator function to the already -- constructed part of the vector. -- --
-- constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in <c,b,a> --constructrN :: Unbox a => Int -> (Vector a -> a) -> Vector a -- | O(n) Yield a vector of the given length containing the values -- x, x+1 etc. This operation is usually more efficient -- than enumFromTo. -- --
-- enumFromN 5 3 = <5,6,7> --enumFromN :: (Unbox a, Num a) => a -> Int -> Vector a -- | O(n) 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. -- --
-- enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4> --enumFromStepN :: (Unbox a, Num a) => a -> a -> Int -> Vector a -- | O(n) Enumerate values from x to y. -- -- WARNING: This operation can be very inefficient. If at all -- possible, use enumFromN instead. enumFromTo :: (Unbox a, Enum a) => a -> a -> Vector a -- | O(n) 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. enumFromThenTo :: (Unbox a, Enum a) => a -> a -> a -> Vector a -- | O(n) Prepend an element cons :: Unbox a => a -> Vector a -> Vector a -- | O(n) Append an element snoc :: Unbox a => Vector a -> a -> Vector a -- | O(m+n) Concatenate two vectors (++) :: Unbox a => Vector a -> Vector a -> Vector a infixr 5 ++ -- | O(n) Concatenate all vectors in the list concat :: Unbox a => [Vector a] -> Vector a -- | O(n) Yield the argument but force it not to retain any extra -- memory, possibly by copying it. -- -- This is especially useful when dealing with slices. For example: -- --
-- force (slice 0 2 <huge vector>) ---- -- Here, the slice retains a reference to the huge vector. Forcing it -- creates a copy of just the elements that belong to the slice and -- allows the huge vector to be garbage collected. force :: Unbox a => Vector a -> Vector a -- | O(m+n) For each pair (i,a) from the list, replace the -- vector element at position i by a. -- --
-- <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7> --(//) :: Unbox a => Vector a -> [(Int, a)] -> Vector a -- | O(m+n) For each pair (i,a) from the vector of -- index/value pairs, replace the vector element at position i -- by a. -- --
-- update <5,9,2,7> <(2,1),(0,3),(2,8)> = <3,9,8,7> --update :: Unbox a => Vector a -> Vector (Int, a) -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value a from the value vector, replace -- the element of the initial vector at position i by -- a. -- --
-- update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7> ---- -- The function update provides the same functionality and is -- usually more convenient. -- --
-- update_ xs is ys = update xs (zip is ys) --update_ :: Unbox a => Vector a -> Vector Int -> Vector a -> Vector a -- | Same as (//) but without bounds checking. unsafeUpd :: Unbox a => Vector a -> [(Int, a)] -> Vector a -- | Same as update but without bounds checking. unsafeUpdate :: Unbox a => Vector a -> Vector (Int, a) -> Vector a -- | Same as update_ but without bounds checking. unsafeUpdate_ :: Unbox a => Vector a -> Vector Int -> Vector a -> Vector a -- | O(m+n) For each pair (i,b) from the list, replace the -- vector element a at position i by f a b. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.accum (+) (VU.fromList [1000.0,2000.0,3000.0]) [(2,4),(1,6),(0,3),(1,10)] -- [1003.0,2016.0,3004.0] --accum :: Unbox a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | O(m+n) For each pair (i,b) from the vector of pairs, -- replace the vector element a at position i by f -- a b. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.accumulate (+) (VU.fromList [1000.0,2000.0,3000.0]) (VU.fromList [(2,4),(1,6),(0,3),(1,10)]) -- [1003.0,2016.0,3004.0] --accumulate :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a -- | O(m+min(n1,n2)) For each index i from the index vector -- and the corresponding value b from the the value vector, -- replace the element of the initial vector at position i by -- f a b. -- --
-- accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4> ---- -- The function accumulate provides the same functionality and is -- usually more convenient. -- --
-- accumulate_ f as is bs = accumulate f as (zip is bs) --accumulate_ :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | Same as accum but without bounds checking. unsafeAccum :: Unbox a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a -- | Same as accumulate but without bounds checking. unsafeAccumulate :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a -- | Same as accumulate_ but without bounds checking. unsafeAccumulate_ :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a -- | O(n) Reverse a vector reverse :: Unbox a => Vector a -> Vector a -- | O(n) Yield the vector obtained by replacing each element -- i of the index vector by xs!i. This is -- equivalent to map (xs!) is but is often much -- more efficient. -- --
-- backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a> --backpermute :: Unbox a => Vector a -> Vector Int -> Vector a -- | Same as backpermute but without bounds checking. unsafeBackpermute :: Unbox a => Vector a -> Vector Int -> Vector a -- | Apply a destructive operation to a vector. The operation will be -- performed in place if it is safe to do so and will modify a copy of -- the vector otherwise. -- --
-- modify (\v -> write v 0 'x') (replicate 3 'a') = <'x','a','a'> --modify :: Unbox a => (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a -- | O(n) Pair each element in a vector with its index indexed :: Unbox a => Vector a -> Vector (Int, a) -- | O(n) Map a function over a vector map :: (Unbox a, Unbox b) => (a -> b) -> Vector a -> Vector b -- | O(n) Apply a function to every element of a vector and its -- index imap :: (Unbox a, Unbox b) => (Int -> a -> b) -> Vector a -> Vector b -- | Map a function over a vector and concatenate the results. concatMap :: (Unbox a, Unbox b) => (a -> Vector b) -> Vector a -> Vector b -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results mapM :: (Monad m, Unbox a, Unbox b) => (a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to every element of a vector and -- its index, yielding a vector of results imapM :: (Monad m, Unbox a, Unbox b) => (Int -> a -> m b) -> Vector a -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results mapM_ :: (Monad m, Unbox a) => (a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to every element of a vector and -- its index, ignoring the results imapM_ :: (Monad m, Unbox a) => (Int -> a -> m b) -> Vector a -> m () -- | O(n) Apply the monadic action to all elements of the vector, -- yielding a vector of results. Equivalent to flip mapM. forM :: (Monad m, Unbox a, Unbox b) => Vector a -> (a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of a vector and -- ignore the results. Equivalent to flip mapM_. forM_ :: (Monad m, Unbox a) => Vector a -> (a -> m b) -> m () -- | O(n) Apply the monadic action to all elements of the vector and -- their indices, yielding a vector of results. Equivalent to flip -- imapM. iforM :: (Monad m, Unbox a, Unbox b) => Vector a -> (Int -> a -> m b) -> m (Vector b) -- | O(n) Apply the monadic action to all elements of the vector and -- their indices and ignore the results. Equivalent to flip -- imapM_. iforM_ :: (Monad m, Unbox a) => Vector a -> (Int -> a -> m b) -> m () -- | O(min(m,n)) Zip two vectors with the given function. zipWith :: (Unbox a, Unbox b, Unbox c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors with the given function. zipWith3 :: (Unbox a, Unbox b, Unbox c, Unbox d) => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d zipWith4 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => (a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e zipWith5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => (a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f zipWith6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f, Unbox g) => (a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | O(min(m,n)) Zip two vectors with a function that also takes the -- elements' indices. izipWith :: (Unbox a, Unbox b, Unbox c) => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c -- | Zip three vectors and their indices with the given function. izipWith3 :: (Unbox a, Unbox b, Unbox c, Unbox d) => (Int -> a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d izipWith4 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => (Int -> a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e izipWith5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f izipWith6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f, Unbox g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector g -- | O(1) Zip 2 vectors zip :: (Unbox a, Unbox b) => Vector a -> Vector b -> Vector (a, b) -- | O(1) Zip 3 vectors zip3 :: (Unbox a, Unbox b, Unbox c) => Vector a -> Vector b -> Vector c -> Vector (a, b, c) -- | O(1) Zip 4 vectors zip4 :: (Unbox a, Unbox b, Unbox c, Unbox d) => Vector a -> Vector b -> Vector c -> Vector d -> Vector (a, b, c, d) -- | O(1) Zip 5 vectors zip5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector (a, b, c, d, e) -- | O(1) Zip 6 vectors zip6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => Vector a -> Vector b -> Vector c -> Vector d -> Vector e -> Vector f -> Vector (a, b, c, d, e, f) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- yield a vector of results zipWithM :: (Monad m, Unbox a, Unbox b, Unbox c) => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and yield a vector of results izipWithM :: (Monad m, Unbox a, Unbox b, Unbox c) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m (Vector c) -- | O(min(m,n)) Zip the two vectors with the monadic action and -- ignore the results zipWithM_ :: (Monad m, Unbox a, Unbox b) => (a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(min(m,n)) Zip the two vectors with a monadic action that also -- takes the element index and ignore the results izipWithM_ :: (Monad m, Unbox a, Unbox b) => (Int -> a -> b -> m c) -> Vector a -> Vector b -> m () -- | O(1) Unzip 2 vectors unzip :: (Unbox a, Unbox b) => Vector (a, b) -> (Vector a, Vector b) -- | O(1) Unzip 3 vectors unzip3 :: (Unbox a, Unbox b, Unbox c) => Vector (a, b, c) -> (Vector a, Vector b, Vector c) -- | O(1) Unzip 4 vectors unzip4 :: (Unbox a, Unbox b, Unbox c, Unbox d) => Vector (a, b, c, d) -> (Vector a, Vector b, Vector c, Vector d) -- | O(1) Unzip 5 vectors unzip5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => Vector (a, b, c, d, e) -> (Vector a, Vector b, Vector c, Vector d, Vector e) -- | O(1) Unzip 6 vectors unzip6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => Vector (a, b, c, d, e, f) -> (Vector a, Vector b, Vector c, Vector d, Vector e, Vector f) -- | O(n) Drop elements that do not satisfy the predicate filter :: Unbox a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the predicate which is -- applied to values and their indices ifilter :: Unbox a => (Int -> a -> Bool) -> Vector a -> Vector a -- | O(n) Drop elements that do not satisfy the monadic predicate filterM :: (Monad m, Unbox a) => (a -> m Bool) -> Vector a -> m (Vector a) -- | O(n) Drop repeated adjacent elements. uniq :: (Unbox a, Eq a) => Vector a -> Vector a -- | O(n) Drop elements when predicate returns Nothing mapMaybe :: (Unbox a, Unbox b) => (a -> Maybe b) -> Vector a -> Vector b -- | O(n) Drop elements when predicate, applied to index and value, -- returns Nothing imapMaybe :: (Unbox a, Unbox b) => (Int -> a -> Maybe b) -> Vector a -> Vector b -- | O(n) Apply monadic function to each element of vector and -- discard elements returning Nothing. mapMaybeM :: (Monad m, Unbox a, Unbox b) => (a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Apply monadic function to each element of vector and its -- index. Discards elements returning Nothing. imapMaybeM :: (Monad m, Unbox a, Unbox b) => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b) -- | O(n) Yield the longest prefix of elements satisfying the -- predicate. Current implementation is not copy-free, unless the result -- vector is fused away. takeWhile :: Unbox a => (a -> Bool) -> Vector a -> Vector a -- | O(n) Drop the longest prefix of elements that satisfy the -- predicate without copying. dropWhile :: Unbox a => (a -> Bool) -> Vector a -> Vector a -- | O(n) 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. partition :: Unbox a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) 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 but the -- operation is often faster than partition. unstablePartition :: Unbox a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into two parts, the first one containing -- the Left elements and the second containing the -- Right elements. The relative order of the elements is -- preserved. partitionWith :: (Unbox a, Unbox b, Unbox c) => (a -> Either b c) -> Vector a -> (Vector b, Vector c) -- | O(n) Split the vector into the longest prefix of elements that -- satisfy the predicate and the rest without copying. span :: Unbox a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Split the vector into the longest prefix of elements that -- do not satisfy the predicate and the rest without copying. break :: Unbox a => (a -> Bool) -> Vector a -> (Vector a, Vector a) -- | O(n) Check if the vector contains an element elem :: (Unbox a, Eq a) => a -> Vector a -> Bool infix 4 `elem` -- | O(n) Check if the vector does not contain an element (inverse -- of elem) notElem :: (Unbox a, Eq a) => a -> Vector a -> Bool infix 4 `notElem` -- | O(n) Yield Just the first element matching the predicate -- or Nothing if no such element exists. find :: Unbox a => (a -> Bool) -> Vector a -> Maybe a -- | O(n) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: Unbox a => (a -> Bool) -> Vector a -> Maybe Int -- | O(n) Yield the indices of elements satisfying the predicate in -- ascending order. findIndices :: Unbox a => (a -> Bool) -> Vector a -> Vector Int -- | O(n) Yield Just the index of the first occurence of the -- given element or Nothing if the vector does not contain the -- element. This is a specialised version of findIndex. elemIndex :: (Unbox a, Eq a) => a -> Vector a -> Maybe Int -- | O(n) Yield the indices of all occurences of the given element -- in ascending order. This is a specialised version of -- findIndices. elemIndices :: (Unbox a, Eq a) => a -> Vector a -> Vector Int -- | O(n) Left fold foldl :: Unbox b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors foldl1 :: Unbox a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold with strict accumulator foldl' :: Unbox b => (a -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold on non-empty vectors with strict accumulator foldl1' :: Unbox a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold foldr :: Unbox a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors foldr1 :: Unbox a => (a -> a -> a) -> Vector a -> a -- | O(n) Right fold with a strict accumulator foldr' :: Unbox a => (a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold on non-empty vectors with strict accumulator foldr1' :: Unbox a => (a -> a -> a) -> Vector a -> a -- | O(n) Left fold (function applied to each element and its index) ifoldl :: Unbox b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Left fold with strict accumulator (function applied to -- each element and its index) ifoldl' :: Unbox b => (a -> Int -> b -> a) -> a -> Vector b -> a -- | O(n) Right fold (function applied to each element and its -- index) ifoldr :: Unbox a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Right fold with strict accumulator (function applied to -- each element and its index) ifoldr' :: Unbox a => (Int -> a -> b -> b) -> b -> Vector a -> b -- | O(n) Map each element of the structure to a monoid, and combine -- the results. It uses same implementation as corresponding method of -- Foldable type cless. Note it's implemented in terms of -- foldr and won't fuse with functions that traverse vector from -- left to right (map, generate, etc.). foldMap :: (Monoid m, Unbox a) => (a -> m) -> Vector a -> m -- | O(n) foldMap which is strict in accumulator. It uses -- same implementation as corresponding method of Foldable type -- class. Note it's implemented in terms of foldl' so it fuses in -- most contexts. foldMap' :: (Monoid m, Unbox a) => (a -> m) -> Vector a -> m -- | O(n) Check if all elements satisfy the predicate. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.all even $ VU.fromList [2, 4, 12 :: Int] -- True -- -- >>> VU.all even $ VU.fromList [2, 4, 13 :: Int] -- False -- -- >>> VU.all even (VU.empty :: VU.Vector Int) -- True --all :: Unbox a => (a -> Bool) -> Vector a -> Bool -- | O(n) Check if any element satisfies the predicate. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.any even $ VU.fromList [1, 3, 7 :: Int] -- False -- -- >>> VU.any even $ VU.fromList [3, 2, 13 :: Int] -- True -- -- >>> VU.any even (VU.empty :: VU.Vector Int) -- False --any :: Unbox a => (a -> Bool) -> Vector a -> Bool -- | O(n) Check if all elements are True -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.and $ VU.fromList [True, False] -- False -- -- >>> VU.and VU.empty -- True --and :: Vector Bool -> Bool -- | O(n) Check if any element is True -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.or $ VU.fromList [True, False] -- True -- -- >>> VU.or VU.empty -- False --or :: Vector Bool -> Bool -- | O(n) Compute the sum of the elements -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.sum $ VU.fromList [300,20,1 :: Int] -- 321 -- -- >>> VU.sum (VU.empty :: VU.Vector Int) -- 0 --sum :: (Unbox a, Num a) => Vector a -> a -- | O(n) Compute the produce of the elements -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.product $ VU.fromList [1,2,3,4 :: Int] -- 24 -- -- >>> VU.product (VU.empty :: VU.Vector Int) -- 1 --product :: (Unbox a, Num a) => Vector a -> a -- | O(n) Yield the maximum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.maximum $ VU.fromList [2.0, 1.0] -- 2.0 --maximum :: (Unbox a, Ord a) => Vector a -> a -- | O(n) Yield the maximum element of the vector according to the -- given comparison function. The vector may not be empty. maximumBy :: Unbox a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the minimum element of the vector. The vector may -- not be empty. -- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.minimum $ VU.fromList [2.0, 1.0] -- 1.0 --minimum :: (Unbox a, Ord a) => Vector a -> a -- | O(n) Yield the minimum element of the vector according to the -- given comparison function. The vector may not be empty. minimumBy :: Unbox a => (a -> a -> Ordering) -> Vector a -> a -- | O(n) Yield the index of the minimum element of the vector. The -- vector may not be empty. minIndex :: (Unbox a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the minimum element of the vector -- according to the given comparison function. The vector may not be -- empty. minIndexBy :: Unbox a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector. The -- vector may not be empty. maxIndex :: (Unbox a, Ord a) => Vector a -> Int -- | O(n) Yield the index of the maximum element of the vector -- according to the given comparison function. The vector may not be -- empty. maxIndexBy :: Unbox a => (a -> a -> Ordering) -> Vector a -> Int -- | O(n) Monadic fold foldM :: (Monad m, Unbox b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold (action applied to each element and its -- index) ifoldM :: (Monad m, Unbox b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator foldM' :: (Monad m, Unbox b) => (a -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold with strict accumulator (action applied to -- each element and its index) ifoldM' :: (Monad m, Unbox b) => (a -> Int -> b -> m a) -> a -> Vector b -> m a -- | O(n) Monadic fold over non-empty vectors fold1M :: (Monad m, Unbox a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator fold1M' :: (Monad m, Unbox a) => (a -> a -> m a) -> Vector a -> m a -- | O(n) Monadic fold that discards the result foldM_ :: (Monad m, Unbox b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold that discards the result (action applied to -- each element and its index) ifoldM_ :: (Monad m, Unbox b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result foldM'_ :: (Monad m, Unbox b) => (a -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold with strict accumulator that discards the -- result (action applied to each element and its index) ifoldM'_ :: (Monad m, Unbox b) => (a -> Int -> b -> m a) -> a -> Vector b -> m () -- | O(n) Monadic fold over non-empty vectors that discards the -- result fold1M_ :: (Monad m, Unbox a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Monadic fold over non-empty vectors with strict -- accumulator that discards the result fold1M'_ :: (Monad m, Unbox a) => (a -> a -> m a) -> Vector a -> m () -- | O(n) Prescan -- --
-- prescanl f z = init . scanl f z ---- -- Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6> prescanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Prescan with strict accumulator prescanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan -- --
-- postscanl f z = tail . scanl f z ---- -- Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10> postscanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan with strict accumulator postscanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan -- --
-- 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 :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Haskell-style scan with strict accumulator scanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a non-empty vector -- --
-- scanl f <x1,...,xn> = <y1,...,yn> -- where y1 = x1 -- yi = f y(i-1) xi --scanl1 :: Unbox a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a non-empty vector with a strict accumulator scanl1' :: Unbox a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Scan over a vector with its index iscanl :: (Unbox a, Unbox b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Scan over a vector (strictly) with its index iscanl' :: (Unbox a, Unbox b) => (Int -> a -> b -> a) -> a -> Vector b -> Vector a -- | O(n) Right-to-left prescan -- --
-- prescanr f z = reverse . prescanl (flip f) z . reverse --prescanr :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left prescan with strict accumulator prescanr' :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan postscanr :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan with strict accumulator postscanr' :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan scanr :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left Haskell-style scan with strict accumulator scanr' :: (Unbox a, Unbox b) => (a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a non-empty vector scanr1 :: Unbox a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a non-empty vector with a strict -- accumulator scanr1' :: Unbox a => (a -> a -> a) -> Vector a -> Vector a -- | O(n) Right-to-left scan over a vector with its index iscanr :: (Unbox a, Unbox b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Right-to-left scan over a vector (strictly) with its index -- -- @sinqce 0.12.2.0 iscanr' :: (Unbox a, Unbox b) => (Int -> a -> b -> b) -> b -> Vector a -> Vector b -- | O(n) Check if two vectors are equal using supplied equality -- predicate. eqBy :: (Unbox a, Unbox b) => (a -> b -> Bool) -> Vector a -> Vector b -> Bool -- | O(n) Compare two vectors using supplied comparison function for -- vector elements. Comparison works same as for lists. -- --
-- cmpBy compare == compare --cmpBy :: (Unbox a, Unbox b) => (a -> b -> Ordering) -> Vector a -> Vector b -> Ordering -- | O(n) Convert a vector to a list toList :: Unbox a => Vector a -> [a] -- | O(n) Convert a list to a vector fromList :: Unbox a => [a] -> Vector a -- | O(n) Convert the first n elements of a list to a -- vector -- --
-- fromListN n xs = fromList (take n xs) ---- --
-- >>> import qualified Data.Vector.Unboxed as VU -- -- >>> VU.fromListN 3 [1,2,3,4,5::Int] -- [1,2,3] -- -- >>> VU.fromListN 3 [1::Int] -- [1] --fromListN :: Unbox a => Int -> [a] -> Vector a -- | O(n) Convert different vector types convert :: (Vector v a, Vector w a) => v a -> w a -- | O(n) Yield an immutable copy of the mutable vector. freeze :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(n) Yield a mutable copy of the immutable vector. thaw :: (Unbox a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. copy :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () -- | O(1) Unsafe convert a mutable vector to an immutable one -- without copying. The mutable vector may not be used after this -- operation. unsafeFreeze :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) -- | O(1) Unsafely convert an immutable vector to a mutable one -- without copying. Note that this is very dangerous function and -- generally it's only safe to read from resulting vector. In which case -- immutable vector could be used safely as well. -- -- Problem with mutation happens because GHC has a lot of freedom to -- introduce sharing. As a result mutable vectors produced by -- unsafeThaw may or may not share same underlying buffer. For -- example: -- --
-- foo = do -- let vec = V.generate 10 id -- mvec <- V.unsafeThaw vec -- do_something mvec ---- -- Here GHC could lift vec outside of foo which means all calls -- to do_something will use same buffer with possibly disastrous -- results. Whether such aliasing happens or not depends on program in -- question, optimization levels, and GHC flags. -- -- All in all attempts to modify vector after unsafeThaw falls out of -- domain of software engineering and into realm of black magic, dark -- rituals, and unspeakable horrors. Only advice that could be given is: -- "don't attempt to mutate vector after unsafeThaw unless you know how -- to prevent GHC from aliasing buffers accidentally. We don't" unsafeThaw :: (Unbox a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) -- | O(n) Copy an immutable vector into a mutable one. The two -- vectors must have the same length. This is not checked. unsafeCopy :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () instance (Data.Vector.Unboxed.Base.Unbox a, GHC.Classes.Eq a) => GHC.Classes.Eq (Data.Vector.Unboxed.Base.Vector a) instance (Data.Vector.Unboxed.Base.Unbox a, GHC.Classes.Ord a) => GHC.Classes.Ord (Data.Vector.Unboxed.Base.Vector a) instance Data.Vector.Unboxed.Base.Unbox a => GHC.Base.Semigroup (Data.Vector.Unboxed.Base.Vector a) instance Data.Vector.Unboxed.Base.Unbox a => GHC.Base.Monoid (Data.Vector.Unboxed.Base.Vector a) instance (GHC.Show.Show a, Data.Vector.Unboxed.Base.Unbox a) => GHC.Show.Show (Data.Vector.Unboxed.Base.Vector a) instance (GHC.Read.Read a, Data.Vector.Unboxed.Base.Unbox a) => GHC.Read.Read (Data.Vector.Unboxed.Base.Vector a) instance Data.Vector.Unboxed.Base.Unbox e => GHC.Exts.IsList (Data.Vector.Unboxed.Base.Vector e) -- | Mutable adaptive unboxed vectors module Data.Vector.Unboxed.Mutable data family MVector s a type IOVector = MVector RealWorld type STVector s = MVector s class (Vector Vector a, MVector MVector a) => Unbox a -- | Length of the mutable vector. length :: Unbox a => MVector s a -> Int -- | Check whether the vector is empty null :: Unbox a => MVector s a -> Bool -- | Yield a part of the mutable vector without copying it. The vector must -- contain at least i+n elements. slice :: Unbox a => Int -> Int -> MVector s a -> MVector s a -- | Drop last element of the mutable vector without making a copy. If -- vector is empty exception is thrown. init :: Unbox a => MVector s a -> MVector s a -- | Drop first element of the mutable vector without making a copy. If -- vector is empty exception is thrown. tail :: Unbox a => MVector s a -> MVector s a -- | Take n first elements of the mutable vector without making a -- copy. For negative n empty vector is returned. If n -- is larger than vector's length empty vector is returned, take :: Unbox a => Int -> MVector s a -> MVector s a -- | Drop n first element of the mutable vector without making a -- copy. For negative n vector is returned unchanged and if -- n is larger than vector's length empty vector is returned. drop :: Unbox a => Int -> MVector s a -> MVector s a splitAt :: Unbox a => Int -> MVector s a -> (MVector s a, MVector s a) -- | Yield a part of the mutable vector without copying it. No bounds -- checks are performed. unsafeSlice :: Unbox a => Int -> Int -> MVector s a -> MVector s a -- | Same as init but doesn't do range checks. unsafeInit :: Unbox a => MVector s a -> MVector s a -- | Same as tail but doesn't do range checks. unsafeTail :: Unbox a => MVector s a -> MVector s a -- | Unsafe variant of take. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeTake :: Unbox a => Int -> MVector s a -> MVector s a -- | Unsafe variant of drop. If called with out of range n -- it will simply create invalid slice that likely violate memory safety unsafeDrop :: Unbox a => Int -> MVector s a -> MVector s a -- | Check whether two vectors overlap. overlaps :: Unbox a => MVector s a -> MVector s a -> Bool -- | Create a mutable vector of the given length. new :: (PrimMonad m, Unbox a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length. The vector content is -- uninitialized, which means it is filled with whatever underlying -- memory buffer happens to contain. unsafeNew :: (PrimMonad m, Unbox a) => Int -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with an initial value. replicate :: (PrimMonad m, Unbox a) => Int -> a -> m (MVector (PrimState m) a) -- | Create a mutable vector of the given length (0 if the length is -- negative) and fill it with values produced by repeatedly executing the -- monadic action. replicateM :: (PrimMonad m, Unbox a) => Int -> m a -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- function to each index. generate :: (PrimMonad m, Unbox a) => Int -> (Int -> a) -> m (MVector (PrimState m) a) -- | O(n) Create a mutable vector of the given length (0 if the -- length is negative) and fill it with the results of applying the -- monadic function to each index. Iteration starts at index 0. generateM :: (PrimMonad m, Unbox a) => Int -> (Int -> m a) -> m (MVector (PrimState m) a) -- | Create a copy of a mutable vector. clone :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> m (MVector (PrimState m) a) -- | Grow an unboxed vector by the given number of elements. The number -- must be non-negative. Same semantics as in grow for generic -- vector. -- --
-- >>> import qualified Data.Vector.Unboxed as VU
--
-- >>> import qualified Data.Vector.Unboxed.Mutable as MVU
--
-- >>> mv <- VU.thaw $ VU.fromList ([('a', 10), ('b', 20), ('c', 30)] :: [(Char, Int)])
--
-- >>> mv' <- MVU.grow mv 2
--
--
-- Extra memory at the end of the newly allocated vector is initialized
-- to 0 bytes, which for Unbox instance will usually correspond to
-- some default value for a particular type, eg. 0 for
-- Int, False for Bool, etc. However, if
-- unsafeGrow was used instead this would not have been guaranteed
-- and some garbage would be there instead:
--
--
-- >>> VU.freeze mv'
-- [('a',10),('b',20),('c',30),('\NUL',0),('\NUL',0)]
--
--
-- Having the extra space we can write new values in there:
--
--
-- >>> MVU.write mv' 3 ('d', 999)
--
-- >>> VU.freeze mv'
-- [('a',10),('b',20),('c',30),('d',999),('\NUL',0)]
--
--
-- It is important to note that the source mutable vector is not affected
-- when the newly allocated one is mutated.
--
--
-- >>> MVU.write mv' 2 ('X', 888)
--
-- >>> VU.freeze mv'
-- [('a',10),('b',20),('X',888),('d',999),('\NUL',0)]
--
-- >>> VU.freeze mv
-- [('a',10),('b',20),('c',30)]
--
grow :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
-- | Grow a vector by the given number of elements. The number must be
-- non-negative but this is not checked. Same semantics as in
-- unsafeGrow for generic vector.
unsafeGrow :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
-- | Reset all elements of the vector to some undefined value, clearing all
-- references to external objects. This is usually a noop for unboxed
-- vectors.
clear :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> m ()
-- | O(1) Zip 2 vectors
zip :: (Unbox a, Unbox b) => MVector s a -> MVector s b -> MVector s (a, b)
-- | O(1) Zip 3 vectors
zip3 :: (Unbox a, Unbox b, Unbox c) => MVector s a -> MVector s b -> MVector s c -> MVector s (a, b, c)
-- | O(1) Zip 4 vectors
zip4 :: (Unbox a, Unbox b, Unbox c, Unbox d) => MVector s a -> MVector s b -> MVector s c -> MVector s d -> MVector s (a, b, c, d)
-- | O(1) Zip 5 vectors
zip5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => MVector s a -> MVector s b -> MVector s c -> MVector s d -> MVector s e -> MVector s (a, b, c, d, e)
-- | O(1) Zip 6 vectors
zip6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => MVector s a -> MVector s b -> MVector s c -> MVector s d -> MVector s e -> MVector s f -> MVector s (a, b, c, d, e, f)
-- | O(1) Unzip 2 vectors
unzip :: (Unbox a, Unbox b) => MVector s (a, b) -> (MVector s a, MVector s b)
-- | O(1) Unzip 3 vectors
unzip3 :: (Unbox a, Unbox b, Unbox c) => MVector s (a, b, c) -> (MVector s a, MVector s b, MVector s c)
-- | O(1) Unzip 4 vectors
unzip4 :: (Unbox a, Unbox b, Unbox c, Unbox d) => MVector s (a, b, c, d) -> (MVector s a, MVector s b, MVector s c, MVector s d)
-- | O(1) Unzip 5 vectors
unzip5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => MVector s (a, b, c, d, e) -> (MVector s a, MVector s b, MVector s c, MVector s d, MVector s e)
-- | O(1) Unzip 6 vectors
unzip6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => MVector s (a, b, c, d, e, f) -> (MVector s a, MVector s b, MVector s c, MVector s d, MVector s e, MVector s f)
-- | Yield the element at the given position.
read :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m a
-- | Replace the element at the given position.
write :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m ()
-- | Modify the element at the given position.
modify :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
-- | Modify the element at the given position using a monadic function.
modifyM :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m ()
-- | Swap the elements at the given positions.
swap :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> Int -> m ()
-- | Replace the element at the given position and return the old element.
exchange :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m a
-- | Yield the element at the given position. No bounds checks are
-- performed.
unsafeRead :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m a
-- | Replace the element at the given position. No bounds checks are
-- performed.
unsafeWrite :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m ()
-- | Modify the element at the given position. No bounds checks are
-- performed.
unsafeModify :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
-- | Modify the element at the given position using a monadic function. No
-- bounds checks are performed.
unsafeModifyM :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> m a) -> Int -> m ()
-- | Swap the elements at the given positions. No bounds checks are
-- performed.
unsafeSwap :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> Int -> m ()
-- | Replace the element at the given position and return the old element.
-- No bounds checks are performed.
unsafeExchange :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m a
-- | O(n) Apply the monadic action to every element of the vector,
-- discarding the results.
mapM_ :: (PrimMonad m, Unbox a) => (a -> m b) -> MVector (PrimState m) a -> m ()
-- | O(n) Apply the monadic action to every element of the vector
-- and its index, discarding the results.
imapM_ :: (PrimMonad m, Unbox a) => (Int -> a -> m b) -> MVector (PrimState m) a -> m ()
-- | O(n) Apply the monadic action to every element of the vector,
-- discarding the results. It's same as the flip mapM_.
forM_ :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> m b) -> m ()
-- | O(n) Apply the monadic action to every element of the vector
-- and its index, discarding the results. It's same as the flip
-- imapM_.
iforM_ :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (Int -> a -> m b) -> m ()
-- | O(n) Pure left fold.
foldl :: (PrimMonad m, Unbox a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure left fold with strict accumulator.
foldl' :: (PrimMonad m, Unbox a) => (b -> a -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic fold.
foldM :: (PrimMonad m, Unbox a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic fold with strict accumulator.
foldM' :: (PrimMonad m, Unbox a) => (b -> a -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure right fold.
foldr :: (PrimMonad m, Unbox a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure right fold with strict accumulator.
foldr' :: (PrimMonad m, Unbox a) => (a -> b -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic right fold.
foldrM :: (PrimMonad m, Unbox a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic right fold with strict accumulator.
foldrM' :: (PrimMonad m, Unbox a) => (a -> b -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure left fold (function applied to each element and its
-- index).
ifoldl :: (PrimMonad m, Unbox a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure left fold with strict accumulator (function applied
-- to each element and its index).
ifoldl' :: (PrimMonad m, Unbox a) => (b -> Int -> a -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic fold (action applied to each element and its
-- index).
ifoldM :: (PrimMonad m, Unbox a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic fold with strict accumulator (action applied to
-- each element and its index).
ifoldM' :: (PrimMonad m, Unbox a) => (b -> Int -> a -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure right fold (function applied to each element and its
-- index).
ifoldr :: (PrimMonad m, Unbox a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Pure right fold with strict accumulator (function applied
-- to each element and its index).
ifoldr' :: (PrimMonad m, Unbox a) => (Int -> a -> b -> b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic right fold (action applied to each element and its
-- index).
ifoldrM :: (PrimMonad m, Unbox a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b
-- | O(n) Monadic right fold with strict accumulator (action applied
-- to each element and its index).
ifoldrM' :: (PrimMonad m, Unbox a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b
-- | Compute the next (lexicographically) permutation of given vector
-- in-place. Returns False when input is the last permutation
nextPermutation :: (PrimMonad m, Ord e, Unbox e) => MVector (PrimState m) e -> m Bool
-- | Set all elements of the vector to the given value.
set :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> a -> m ()
-- | Copy a vector. The two vectors must have the same length and may not
-- overlap.
copy :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
-- | Move the contents of a vector. The two vectors must have the same
-- length.
--
-- If the vectors do not overlap, then this is equivalent to copy.
-- Otherwise, the copying is performed as if the source vector were
-- copied to a temporary vector and then the temporary vector was copied
-- to the target vector.
move :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
-- | Copy a vector. The two vectors must have the same length and may not
-- overlap. This is not checked.
unsafeCopy :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
-- | Move the contents of a vector. The two vectors must have the same
-- length, but this is not checked.
--
-- If the vectors do not overlap, then this is equivalent to
-- unsafeCopy. Otherwise, the copying is performed as if the
-- source vector were copied to a temporary vector and then the temporary
-- vector was copied to the target vector.
unsafeMove :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> MVector (PrimState m) a -> m ()