{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

#if MIN_VERSION_base(4,12,0)
{-# LANGUAGE NoStarIsType #-}
#endif

{-# OPTIONS_GHC -Wno-orphans #-}
{-|
This module reexports the functionality in 'Data.Vector.Generic' which maps well
to explicity sized vectors.

Functions returning a vector determine the size from the type context unless
they have a @'@ suffix in which case they take an explicit 'Proxy' argument.

Functions where the resultant vector size is not know until compile time are
not exported.
-}

module Data.Vector.Generic.Sized
  ( Vector(SomeSized)
  , MVector
   -- * Accessors
   -- ** Length information
  , length
  , length'
  , knownLength
  , knownLength'
    -- ** Indexing
  , index
  , index'
  , unsafeIndex
  , head
  , last
    -- ** Monadic indexing
  , indexM
  , indexM'
  , unsafeIndexM
  , headM
  , lastM
    -- ** Extracting subvectors (slicing)
  , slice
  , slice'
  , init
  , tail
  , take
  , take'
  , drop
  , drop'
  , splitAt
  , splitAt'
    -- * Construction
    -- ** Initialization
  , empty
  , singleton
  , fromTuple
  , replicate
  , replicate'
  , generate
  , generate'
  , iterateN
  , iterateN'
    -- ** Monadic initialization
  , replicateM
  , replicateM'
  , generateM
  , generateM'
    -- ** Unfolding
  , unfoldrN
  , unfoldrN'
    -- ** Enumeration
  , enumFromN
  , enumFromN'
  , enumFromStepN
  , enumFromStepN'
    -- ** Concatenation
  , cons
  , snoc
  , (++)
    -- ** Restricting memory usage
  , force
    -- * Modifying vectors
    -- ** Bulk updates
  , (//)
  , update
  , update_
  , unsafeUpd
  , unsafeUpdate
  , unsafeUpdate_
    -- ** Accumulations
  , accum
  , accumulate
  , accumulate_
  , unsafeAccum
  , unsafeAccumulate
  , unsafeAccumulate_
    -- ** Permutations
  , reverse
  , backpermute
  , unsafeBackpermute
    -- * Lenses
  , ix
  , ix'
  , _head
  , _last
    -- * Elementwise operations
    -- ** Indexing
  , indexed
    -- ** Mapping
  , map
  , imap
  , concatMap
    -- ** Monadic mapping
  , mapM
  , imapM
  , mapM_
  , imapM_
  , forM
  , forM_
    -- ** Zipping
  , zipWith
  , zipWith3
  , zipWith4
  , zipWith5
  , zipWith6
  , izipWith
  , izipWith3
  , izipWith4
  , izipWith5
  , izipWith6
  , zip
  , zip3
  , zip4
  , zip5
  , zip6
    -- ** Monadic zipping
  , zipWithM
  , izipWithM
  , zipWithM_
  , izipWithM_
    -- ** Unzipping
  , unzip
  , unzip3
  , unzip4
  , unzip5
  , unzip6
    -- * Working with predicates
    -- ** Searching
  , elem
  , notElem
  , find
  , findIndex
  , elemIndex
    -- * Folding
  , foldl
  , foldl1
  , foldl'
  , foldl1'
  , foldr
  , foldr1
  , foldr'
  , foldr1'
  , ifoldl
  , ifoldl'
  , ifoldr
  , ifoldr'
    -- ** Specialised folds
  , all
  , any
  , and
  , or
  , sum
  , product
  , maximum
  , maximumBy
  , minimum
  , minimumBy
  , maxIndex
  , maxIndexBy
  , minIndex
  , minIndexBy
    -- ** Monadic folds
  , foldM
  , ifoldM
  , fold1M
  , foldM'
  , ifoldM'
  , fold1M'
  , foldM_
  , ifoldM_
  , fold1M_
  , foldM'_
  , ifoldM'_
  , fold1M'_
    -- ** Monadic sequencing
  , sequence
  , sequence_
    -- * Prefix sums (scans)
  , prescanl
  , prescanl'
  , postscanl
  , postscanl'
  , scanl
  , scanl'
  , scanl1
  , scanl1'
  , prescanr
  , prescanr'
  , postscanr
  , postscanr'
  , scanr
  , scanr'
  , scanr1
  , scanr1'
    -- * Conversions
    -- ** Lists
  , toList
  , fromList
  , fromListN
  , fromListN'
  , withSizedList
    -- ** Other Vector types
  , convert
    -- ** Mutable vectors
  , freeze
  , thaw
  , copy
  , unsafeFreeze
  , unsafeThaw
    -- ** Unsized Vectors
  , toSized
  , withSized
  , fromSized
  , withVectorUnsafe
  , zipVectorsUnsafe
  ) where

import Data.Vector.Generic.Sized.Internal
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector as Boxed
import qualified Data.Vector.Storable as Storable
import qualified Data.Vector.Unboxed as Unboxed
import qualified Data.Vector.Generic.Mutable.Sized as SVGM
import Data.Vector.Generic.Mutable.Sized.Internal
import Data.Binary
import Data.Bits
import Data.Bifunctor
import Data.Foldable (for_)
import Data.Coerce
import Data.Finite hiding (shift)
import Data.Finite.Internal
import Data.Proxy
import qualified Control.Exception as Exception
import Control.Monad (mzero)
import Control.Monad.Primitive
import Control.Monad.ST
import Foreign.Storable
import Data.Data
import Control.Comonad
import Foreign.Ptr (castPtr)
import Data.Semigroup
import Text.Read.Lex
import Text.ParserCombinators.ReadPrec
import GHC.Read
import GHC.TypeLits
import Unsafe.Coerce
import qualified Data.Functor.Rep as Rep
import Data.Distributive
import Prelude
       hiding (length, replicate, (++), head, last, init, tail, take,
               drop, splitAt, reverse, map, concatMap, zipWith, zipWith3, zip,
               zip3, unzip, unzip3, elem, notElem, foldl, foldl1, foldr, foldr1,
               all, any, and, or, sum, product, maximum, minimum, scanl, scanl1,
               scanr, scanr1, mapM, mapM_, sequence, sequence_)


import Data.IndexedListLiterals hiding (toList, fromList)
import Data.Hashable (Hashable(..))
import qualified Data.IndexedListLiterals as ILL
import Data.Vector.Unboxed (Unbox)

instance (KnownNat n, VG.Vector v a, Read (v a)) => Read (Vector v n a) where
  readPrec :: ReadPrec (Vector v n a)
readPrec = ReadPrec (Vector v n a) -> ReadPrec (Vector v n a)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (Vector v n a) -> ReadPrec (Vector v n a))
-> ReadPrec (Vector v n a) -> ReadPrec (Vector v n a)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (Vector v n a) -> ReadPrec (Vector v n a)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (Vector v n a) -> ReadPrec (Vector v n a))
-> ReadPrec (Vector v n a) -> ReadPrec (Vector v n a)
forall a b. (a -> b) -> a -> b
$ do
      Lexeme -> ReadPrec ()
expectP (String -> Lexeme
Ident String
"Vector")
      v a
vec <- ReadPrec (v a)
forall a. Read a => ReadPrec a
readPrec
      if v a -> Int
forall (v :: Type -> Type) a. Vector v a => v a -> Int
VG.length v a
vec Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy @n)) then Vector v n a -> ReadPrec (Vector v n a)
forall (m :: Type -> Type) a. Monad m => a -> m a
return (Vector v n a -> ReadPrec (Vector v n a))
-> Vector v n a -> ReadPrec (Vector v n a)
forall a b. (a -> b) -> a -> b
$ v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
vec else ReadPrec (Vector v n a)
forall a. ReadPrec a
pfail

type instance VG.Mutable (Vector v n) = MVector (VG.Mutable v) n

-- | Any sized vector containing 'Storable' elements is itself 'Storable'.
instance (KnownNat n, Storable a, VG.Vector v a)
      => Storable (Vector v n a) where
  sizeOf :: Vector v n a -> Int
sizeOf Vector v n a
_ = a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
  alignment :: Vector v n a -> Int
alignment Vector v n a
_ = a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined :: a)
  peek :: Ptr (Vector v n a) -> IO (Vector v n a)
peek Ptr (Vector v n a)
ptr = (Finite n -> IO a) -> IO (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) (m :: Type -> Type) a.
(KnownNat n, Vector v a, Monad m) =>
(Finite n -> m a) -> m (Vector v n a)
generateM (Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (Ptr (Vector v n a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Vector v n a)
ptr) (Int -> IO a) -> (Finite n -> Int) -> Finite n -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite n -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
  poke :: Ptr (Vector v n a) -> Vector v n a -> IO ()
poke Ptr (Vector v n a)
ptr = (Finite n -> a -> IO ()) -> Vector v n a -> IO ()
forall (m :: Type -> Type) (v :: Type -> Type) a (n :: Nat) b.
(Monad m, Vector v a) =>
(Finite n -> a -> m b) -> Vector v n a -> m ()
imapM_ (Ptr a -> Int -> a -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (Ptr (Vector v n a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Vector v n a)
ptr) (Int -> a -> IO ()) -> (Finite n -> Int) -> Finite n -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite n -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral)

-- | The 'Applicative' instance for sized vectors does not have the same
-- behaviour as the 'Applicative' instance for the unsized vectors found in the
-- 'vectors' package. The instance defined here has the same behaviour as the
-- 'Control.Applicative.ZipList' instance.
instance KnownNat n => Applicative (Vector Boxed.Vector n) where
  pure :: a -> Vector Vector n a
pure = a -> Vector Vector n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate
  <*> :: Vector Vector n (a -> b) -> Vector Vector n a -> Vector Vector n b
(<*>) = ((a -> b) -> a -> b)
-> Vector Vector n (a -> b)
-> Vector Vector n a
-> Vector Vector n b
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
($)
  *> :: Vector Vector n a -> Vector Vector n b -> Vector Vector n b
(*>) = Vector Vector n a -> Vector Vector n b -> Vector Vector n b
seq
  <* :: Vector Vector n a -> Vector Vector n b -> Vector Vector n a
(<*) = (Vector Vector n b -> Vector Vector n a -> Vector Vector n a)
-> Vector Vector n a -> Vector Vector n b -> Vector Vector n a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Vector Vector n b -> Vector Vector n a -> Vector Vector n a
seq

-- | Treats a @'Data.Vector.Sized.Vector' n a@ as, essentially, a @'Finite'
-- n -> a@, and emulates the 'Monad' instance for that function.
--
-- @'join' :: Vector n (Vector n a) -> Vector n a@ gets the /diagonal/ from
-- a square "matrix".
instance KnownNat n => Monad (Vector Boxed.Vector n) where
  return :: a -> Vector Vector n a
return   = a -> Vector Vector n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate
  Vector Vector n a
xs >>= :: Vector Vector n a -> (a -> Vector Vector n b) -> Vector Vector n b
>>= a -> Vector Vector n b
f = (Finite n -> a -> b) -> Vector Vector n a -> Vector Vector n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(Finite n -> a -> b) -> Vector v n a -> Vector v n b
imap (\Finite n
i a
x -> a -> Vector Vector n b
f a
x Vector Vector n b -> Finite n -> b
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v n a -> Finite n -> a
`index` Finite n
i) Vector Vector n a
xs
  >> :: Vector Vector n a -> Vector Vector n b -> Vector Vector n b
(>>)     = Vector Vector n a -> Vector Vector n b -> Vector Vector n b
seq

-- | Non-empty sized vectors are lawful comonads.
--
-- 'extract' is 'head'
--
-- 'duplicate' generates all unique sequences of a vector with the same length as it,
-- using wrap-around.
--
-- e.g.
-- @
-- duplicate [1,2,3,4,5] = [[1,2,3,4,5], [2,3,4,5,1], [3,4,5,1,2], [4,5,1,2,3], [5,1,2,3,4]]
-- @
instance (KnownNat n, n ~ (1 + m)) => Comonad (Vector Boxed.Vector n) where
  extract :: Vector Vector n a -> a
extract = Vector Vector n a -> a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v (1 + n) a -> a
head
  extend :: (Vector Vector n a -> b) -> Vector Vector n a -> Vector Vector n b
extend Vector Vector n a -> b
f r :: Vector Vector n a
r@(Vector Vector a
v) = Vector b -> Vector Vector n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Vector b -> Vector Vector n b) -> Vector b -> Vector Vector n b
forall a b. (a -> b) -> a -> b
$ Int -> (Int -> b) -> Vector b
forall (v :: Type -> Type) a.
Vector v a =>
Int -> (Int -> a) -> v a
VG.generate Int
l (\Int
i -> Vector Vector n a -> b
f (Vector a -> Vector Vector n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> Int -> Vector a -> Vector a
forall (v :: Type -> Type) a.
Vector v a =>
Int -> Int -> v a -> v a
VG.slice Int
i Int
l Vector a
v')))
    where
      v' :: Vector a
v' = Vector a
v Vector a -> Vector a -> Vector a
forall (v :: Type -> Type) a. Vector v a => v a -> v a -> v a
VG.++ Vector a -> Vector a
forall (v :: Type -> Type) a. Vector v a => v a -> v a
VG.init Vector a
v
      l :: Int
l = Vector Vector n a -> Int
forall (v :: Type -> Type) (n :: Nat) a.
KnownNat n =>
Vector v n a -> Int
length Vector Vector n a
r

instance (KnownNat n, n ~ (1 + m)) => ComonadApply (Vector Boxed.Vector n) where
  <@> :: Vector Vector n (a -> b) -> Vector Vector n a -> Vector Vector n b
(<@>) = Vector Vector n (a -> b) -> Vector Vector n a -> Vector Vector n b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
(<*>)
  <@ :: Vector Vector n a -> Vector Vector n b -> Vector Vector n a
(<@)  = Vector Vector n a -> Vector Vector n b -> Vector Vector n a
forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f a
(<*)
  @> :: Vector Vector n a -> Vector Vector n b -> Vector Vector n b
(@>)  = Vector Vector n a -> Vector Vector n b -> Vector Vector n b
forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f b
(*>)

-- | The 'Semigroup' instance for sized vectors does not have the same
-- behaviour as the 'Semigroup' instance for the unsized vectors found in the
-- 'vectors' package. This instance has @(<>) = zipWith (<>)@, but 'vectors'
-- uses concatentation.
instance (Semigroup g, VG.Vector v g) => Semigroup (Vector v n g) where
  <> :: Vector v n g -> Vector v n g -> Vector v n g
(<>) = (g -> g -> g) -> Vector v n g -> Vector v n g -> Vector v n g
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith g -> g -> g
forall a. Semigroup a => a -> a -> a
(<>)
  stimes :: b -> Vector v n g -> Vector v n g
stimes = (g -> g) -> Vector v n g -> Vector v n g
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map ((g -> g) -> Vector v n g -> Vector v n g)
-> (b -> g -> g) -> b -> Vector v n g -> Vector v n g
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> g -> g
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes

-- | The 'Monoid' instance for sized vectors does not have the same
-- behaviour as the 'Monoid' instance for the unsized vectors found in the
-- 'vectors' package. This instance has @mempty = replicate mempty@ and
-- @mappend = zipWith mappend@, where the 'vectors' instance uses the empty
-- vector and concatenation.
--
-- If 'mempty' is not necessary, using the 'Semigroup' instance over this
-- 'Monoid' will dodge the 'KnownNat' constraint.
instance (Monoid m, VG.Vector v m, KnownNat n) => Monoid (Vector v n m) where
  mempty :: Vector v n m
mempty = m -> Vector v n m
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate m
forall a. Monoid a => a
mempty
#if MIN_VERSION_base(4,11,0)
  -- begone, non-canonical mappend!
#else
  mappend = zipWith mappend
#endif
  mconcat :: [Vector v n m] -> Vector v n m
mconcat [Vector v n m]
vs = (Finite n -> m) -> Vector v n m
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
(Finite n -> a) -> Vector v n a
generate ((Finite n -> m) -> Vector v n m)
-> (Finite n -> m) -> Vector v n m
forall a b. (a -> b) -> a -> b
$ [m] -> m
forall a. Monoid a => [a] -> a
mconcat ([m] -> m) -> (Finite n -> [m]) -> Finite n -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Vector v n m -> m) -> [Vector v n m] -> [m])
-> [Vector v n m] -> (Vector v n m -> m) -> [m]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Vector v n m -> m) -> [Vector v n m] -> [m]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [Vector v n m]
vs ((Vector v n m -> m) -> [m])
-> (Finite n -> Vector v n m -> m) -> Finite n -> [m]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Vector v n m -> Finite n -> m) -> Finite n -> Vector v n m -> m
forall a b c. (a -> b -> c) -> b -> a -> c
flip Vector v n m -> Finite n -> m
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v n a -> Finite n -> a
index

instance KnownNat n => Distributive (Vector Boxed.Vector n) where
  distribute :: f (Vector Vector n a) -> Vector Vector n (f a)
distribute = f (Vector Vector n a) -> Vector Vector n (f a)
forall (f :: Type -> Type) (w :: Type -> Type) a.
(Representable f, Functor w) =>
w (f a) -> f (w a)
Rep.distributeRep
  {-# inline distribute #-}

instance KnownNat n => Rep.Representable (Vector Boxed.Vector n) where
  type Rep (Vector Boxed.Vector n) = Finite n
  tabulate :: (Rep (Vector Vector n) -> a) -> Vector Vector n a
tabulate = (Rep (Vector Vector n) -> a) -> Vector Vector n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
(Finite n -> a) -> Vector v n a
generate
  {-# inline tabulate #-}
  index :: Vector Vector n a -> Rep (Vector Vector n) -> a
index = Vector Vector n a -> Rep (Vector Vector n) -> a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v n a -> Finite n -> a
Data.Vector.Generic.Sized.index
  {-# inline index #-}

instance (Eq a, Hashable a) => Hashable (Vector Boxed.Vector n a) where
  {-# INLINE hashWithSalt #-}
  hashWithSalt :: Int -> Vector Vector n a -> Int
hashWithSalt Int
salt = Int -> [a] -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ([a] -> Int)
-> (Vector Vector n a -> [a]) -> Vector Vector n a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Vector n a -> [a]
forall (v :: Type -> Type) a (n :: Nat).
Vector v a =>
Vector v n a -> [a]
toList

instance (Eq a, Hashable a, Storable a) => Hashable (Vector Storable.Vector n a) where
  {-# INLINE hashWithSalt #-}
  hashWithSalt :: Int -> Vector Vector n a -> Int
hashWithSalt Int
salt = Int -> [a] -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ([a] -> Int)
-> (Vector Vector n a -> [a]) -> Vector Vector n a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Vector n a -> [a]
forall (v :: Type -> Type) a (n :: Nat).
Vector v a =>
Vector v n a -> [a]
toList

instance (Eq a, Hashable a, Unbox a) => Hashable (Vector Unboxed.Vector n a) where
  {-# INLINE hashWithSalt #-}
  hashWithSalt :: Int -> Vector Vector n a -> Int
hashWithSalt Int
salt = Int -> [a] -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ([a] -> Int)
-> (Vector Vector n a -> [a]) -> Vector Vector n a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Vector n a -> [a]
forall (v :: Type -> Type) a (n :: Nat).
Vector v a =>
Vector v n a -> [a]
toList

-- | /O(1)/ Yield the length of the vector as an 'Int'. This is more like
-- 'natVal' than 'Data.Vector.length', extracting the value from the 'KnownNat'
-- instance and not looking at the vector itself.
length :: forall v n a. KnownNat n
       => Vector v n a -> Int
length :: Vector v n a -> Int
length Vector v n a
_ = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline length #-}

-- | /O(1)/ Yield the length of the vector as a 'Proxy'. This function
-- doesn't /do/ anything; it merely allows the size parameter of the vector
-- to be passed around as a 'Proxy'.
length' :: forall v n a.
           Vector v n a -> Proxy n
length' :: Vector v n a -> Proxy n
length' Vector v n a
_ = Proxy n
forall k (t :: k). Proxy t
Proxy
{-# inline length' #-}

-- | /O(1)/ Reveal a 'KnownNat' instance for a vector's length, determined
-- at runtime.
knownLength :: forall v n a r. VG.Vector v a
            => Vector v n a -- ^ a vector of some (potentially unknown) length
            -> (KnownNat n => r) -- ^ a value that depends on knowing the vector's length
            -> r -- ^ the value computed with the length
knownLength :: Vector v n a -> (KnownNat n => r) -> r
knownLength Vector v n a
v KnownNat n => r
x = Vector v n a -> (KnownNat n => Proxy n -> r) -> r
forall (v :: Type -> Type) (n :: Nat) a r.
Vector v a =>
Vector v n a -> (KnownNat n => Proxy n -> r) -> r
knownLength' Vector v n a
v ((KnownNat n => Proxy n -> r) -> r)
-> (KnownNat n => Proxy n -> r) -> r
forall a b. (a -> b) -> a -> b
$ r -> Proxy n -> r
forall a b. a -> b -> a
const r
KnownNat n => r
x

-- | /O(1)/ Reveal a 'KnownNat' instance and 'Proxy' for a vector's length,
-- determined at runtime.
knownLength' :: forall v n a r. VG.Vector v a
             => Vector v n a -- ^ a vector of some (potentially unknown) length
             -> (KnownNat n => Proxy n -> r) -- ^ a value that depends on knowing the vector's length, which is given as a 'Proxy'
             -> r -- ^ the value computed with the length
knownLength' :: Vector v n a -> (KnownNat n => Proxy n -> r) -> r
knownLength' (Vector v a
v) KnownNat n => Proxy n -> r
x = case Integer -> Maybe SomeNat
someNatVal (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v a -> Int
forall (v :: Type -> Type) a. Vector v a => v a -> Int
VG.length v a
v)) of
  Just (SomeNat (Proxy n
Proxy :: Proxy n')) -> case (Any :~: Any) -> n :~: n
forall a b. a -> b
unsafeCoerce Any :~: Any
forall k (a :: k). a :~: a
Refl :: n' :~: n of n :~: n
Refl -> KnownNat n => Proxy n -> r
Proxy n -> r
x Proxy n
forall k (t :: k). Proxy t
Proxy
  Maybe SomeNat
Nothing -> String -> r
forall a. HasCallStack => String -> a
error String
"impossible: Vector has negative length"

-- | /O(1)/ Safe indexing using a 'Finite'.
index :: forall v n a. VG.Vector v a
      => Vector v n a -> Finite n -> a
index :: Vector v n a -> Finite n -> a
index (Vector v a
v) (Finite Integer
i) = v a
v v a -> Int -> a
forall (v :: Type -> Type) a. Vector v a => v a -> Int -> a
`VG.unsafeIndex` Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i
{-# inline index #-}

-- | /O(1)/ Safe indexing using a 'Proxy'.
index' :: forall v n m a p. (KnownNat n, VG.Vector v a)
       => Vector v (n+m+1) a -> p n -> a
index' :: Vector v ((n + m) + 1) a -> p n -> a
index' (Vector v a
v) p n
p = v a
v v a -> Int -> a
forall (v :: Type -> Type) a. Vector v a => v a -> Int -> a
`VG.unsafeIndex` Int
i
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (p n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal p n
p)
{-# inline index' #-}

-- | /O(1)/ Indexing using an 'Int' without bounds checking.
unsafeIndex :: forall v n a. VG.Vector v a
      => Vector v n a -> Int -> a
unsafeIndex :: Vector v n a -> Int -> a
unsafeIndex (Vector v a
v) Int
i = v a
v v a -> Int -> a
forall (v :: Type -> Type) a. Vector v a => v a -> Int -> a
`VG.unsafeIndex` Int
i
{-# inline unsafeIndex #-}

-- | /O(1)/ Yield the first element of a non-empty vector.
head :: forall v n a. VG.Vector v a
     => Vector v (1+n) a -> a
head :: Vector v (1 + n) a -> a
head (Vector v a
v) = v a -> a
forall (v :: Type -> Type) a. Vector v a => v a -> a
VG.unsafeHead v a
v
{-# inline head #-}

-- | /O(1)/ Yield the last element of a non-empty vector.
last :: forall v n a. (VG.Vector v a)
     => Vector v (n+1) a -> a
last :: Vector v (n + 1) a -> a
last (Vector v a
v) = v a -> a
forall (v :: Type -> Type) a. Vector v a => v a -> a
VG.unsafeLast v a
v
{-# inline last #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index.
ix :: forall v n a f. (VG.Vector v a, Functor f)
   => Finite n -> (a -> f a) -> Vector v n a -> f (Vector v n a)
ix :: Finite n -> (a -> f a) -> Vector v n a -> f (Vector v n a)
ix Finite n
n a -> f a
f Vector v n a
vector = (\a
x -> Vector v n a
vector Vector v n a -> [(Finite n, a)] -> Vector v n a
forall (v :: Type -> Type) a (m :: Nat).
Vector v a =>
Vector v m a -> [(Finite m, a)] -> Vector v m a
// [(Finite n
n, a
x)]) (a -> Vector v n a) -> f a -> f (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f (Vector v n a -> Finite n -> a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v n a -> Finite n -> a
index Vector v n a
vector Finite n
n)
{-# inline ix #-}

-- | Type-safe lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index
-- which should be supplied via TypeApplications.
ix' :: forall i v n a f. (VG.Vector v a, Functor f,
  KnownNat i, KnownNat n, i+1 <= n)
   => (a -> f a) -> Vector v n a -> f (Vector v n a)
ix' :: (a -> f a) -> Vector v n a -> f (Vector v n a)
ix' = Finite n -> (a -> f a) -> Vector v n a -> f (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) a (f :: Type -> Type).
(Vector v a, Functor f) =>
Finite n -> (a -> f a) -> Vector v n a -> f (Vector v n a)
ix (Proxy i -> Finite n
forall (n :: Nat) (m :: Nat) (proxy :: Nat -> Type).
(KnownNat n, KnownNat m, (n + 1) <= m) =>
proxy n -> Finite m
natToFinite (Proxy i
forall k (t :: k). Proxy t
Proxy::Proxy i))
{-# inline ix' #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the first element of a non-empty vector.
_head :: forall v n a f. (VG.Vector v a, Functor f)
      => (a -> f a) -> Vector v (1+n) a -> f (Vector v (1+n) a)
_head :: (a -> f a) -> Vector v (1 + n) a -> f (Vector v (1 + n) a)
_head a -> f a
f Vector v (1 + n) a
vector = (\a
x -> a -> Vector v n a -> Vector v (1 + n) a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
a -> Vector v n a -> Vector v (1 + n) a
cons a
x (Vector v n a -> Vector v (1 + n) a)
-> Vector v n a -> Vector v (1 + n) a
forall a b. (a -> b) -> a -> b
$ Vector v (1 + n) a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v (1 + n) a -> Vector v n a
tail Vector v (1 + n) a
vector) (a -> Vector v (1 + n) a) -> f a -> f (Vector v (1 + n) a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f (Vector v (1 + n) a -> a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v (1 + n) a -> a
head Vector v (1 + n) a
vector)
{-# inline _head #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the last element of a non-empty vector.
_last :: forall v n a f. (VG.Vector v a, Functor f)
       => (a -> f a) -> Vector v (n+1) a -> f (Vector v (n+1) a)
_last :: (a -> f a) -> Vector v (n + 1) a -> f (Vector v (n + 1) a)
_last a -> f a
f Vector v (n + 1) a
vector = Vector v n a -> a -> Vector v (n + 1) a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v n a -> a -> Vector v (n + 1) a
snoc (Vector v (n + 1) a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v (n + 1) a -> Vector v n a
init Vector v (n + 1) a
vector) (a -> Vector v (n + 1) a) -> f a -> f (Vector v (n + 1) a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f (Vector v (n + 1) a -> a
forall (v :: Type -> Type) (n :: Nat) a.
Vector v a =>
Vector v (n + 1) a -> a
last Vector v (n + 1) a
vector)
{-# inline _last #-}

-- | /O(1)/ Safe 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 when 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) are evaluated eagerly.
indexM :: forall v n a m. (VG.Vector v a, Monad m)
      => Vector v n a -> Finite n -> m a
indexM :: Vector v n a -> Finite n -> m a
indexM (Vector v a
v) (Finite Integer
i) = v a
v v a -> Int -> m a
forall (v :: Type -> Type) a (m :: Type -> Type).
(Vector v a, Monad m) =>
v a -> Int -> m a
`VG.indexM` Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i
{-# inline indexM #-}

-- | /O(1)/ Safe indexing in a monad using a 'Proxy'. See the documentation for
-- 'indexM' for an explanation of why this is useful.
indexM' :: forall v n k a m p. (KnownNat n, VG.Vector v a, Monad m)
      => Vector v (n+k) a -> p n -> m a
indexM' :: Vector v (n + k) a -> p n -> m a
indexM' (Vector v a
v) p n
p = v a
v v a -> Int -> m a
forall (v :: Type -> Type) a (m :: Type -> Type).
(Vector v a, Monad m) =>
v a -> Int -> m a
`VG.indexM` Int
i
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (p n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal p n
p)
{-# inline indexM' #-}

-- | /O(1)/ Indexing using an Int without bounds checking. See the
-- documentation for 'indexM' for an explanation of why this is useful.
unsafeIndexM :: forall v n a m. (VG.Vector v a, Monad m)
      => Vector v n a -> Int -> m a
unsafeIndexM :: Vector v n a -> Int -> m a
unsafeIndexM (Vector v a
v) Int
i = v a
v v a -> Int -> m a
forall (v :: Type -> Type) a (m :: Type -> Type).
(Vector v a, Monad m) =>
v a -> Int -> m a
`VG.unsafeIndexM` Int
i
{-# inline unsafeIndexM #-}

-- | /O(1)/ Yield the first element of a non-empty vector in a monad. See the
-- documentation for 'indexM' for an explanation of why this is useful.
headM :: forall v n a m. (VG.Vector v a, Monad m)
      => Vector v (1+n) a -> m a
headM :: Vector v (1 + n) a -> m a
headM (Vector v a
v) = v a -> m a
forall (v :: Type -> Type) a (m :: Type -> Type).
(Vector v a, Monad m) =>
v a -> m a
VG.unsafeHeadM v a
v
{-# inline headM #-}

-- | /O(1)/ Yield the last element of a non-empty vector in a monad. See the
-- documentation for 'indexM' for an explanation of why this is useful.
lastM :: forall v n a m. (VG.Vector v a, Monad m)
      => Vector v (n+1) a -> m a
lastM :: Vector v (n + 1) a -> m a
lastM (Vector v a
v) = v a -> m a
forall (v :: Type -> Type) a (m :: Type -> Type).
(Vector v a, Monad m) =>
v a -> m a
VG.unsafeLastM v a
v
{-# inline lastM #-}

-- | /O(1)/ Yield a slice of the vector without copying it with an inferred
-- length argument.
slice :: forall v i n m a p. (KnownNat i, KnownNat n, VG.Vector v a)
      => p i -- ^ starting index
      -> Vector v (i+n+m) a
      -> Vector v n a
slice :: p i -> Vector v ((i + n) + m) a -> Vector v n a
slice p i
start (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> Int -> v a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
Int -> Int -> v a -> v a
VG.unsafeSlice Int
i Int
n v a
v)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (p i -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal p i
start)
        n :: Int
n = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline slice #-}

-- | /O(1)/ Yield a slice of the vector without copying it with an explicit
-- length argument.
slice' :: forall v i n m a p
        . (KnownNat i, KnownNat n, VG.Vector v a)
       => p i -- ^ starting index
       -> p n -- ^ length
       -> Vector v (i+n+m) a
       -> Vector v n a
slice' :: p i -> p n -> Vector v ((i + n) + m) a -> Vector v n a
slice' p i
start p n
_ = p i -> Vector v ((i + n) + m) a -> Vector v n a
forall (v :: Type -> Type) (i :: Nat) (n :: Nat) (m :: Nat) a
       (p :: Nat -> Type).
(KnownNat i, KnownNat n, Vector v a) =>
p i -> Vector v ((i + n) + m) a -> Vector v n a
slice p i
start
{-# inline slice' #-}

-- | /O(1)/ Yield all but the last element of a non-empty vector without
-- copying.
init :: forall v n a. (VG.Vector v a)
     => Vector v (n+1) a -> Vector v n a
init :: Vector v (n + 1) a -> Vector v n a
init (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> v a
VG.unsafeInit v a
v)
{-# inline init #-}

-- | /O(1)/ Yield all but the first element of a non-empty vector without
-- copying.
tail :: forall v n a. (VG.Vector v a)
     => Vector v (1+n) a -> Vector v n a
tail :: Vector v (1 + n) a -> Vector v n a
tail (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> v a
VG.unsafeTail v a
v)
{-# inline tail #-}

-- | /O(1)/ Yield the first @n@ elements. The resulting vector always contains
-- this many elements. The length of the resulting vector is inferred from the
-- type.
take :: forall v n m a. (KnownNat n, VG.Vector v a)
     => Vector v (n+m) a -> Vector v n a
take :: Vector v (n + m) a -> Vector v n a
take (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> v a -> v a
VG.unsafeTake Int
i v a
v)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline take #-}

-- | /O(1)/ Yield the first @n@ elements. The resulting vector always contains
-- this many elements. The length of the resulting vector is given explicitly
-- as a 'Proxy' argument.
take' :: forall v n m a p. (KnownNat n, VG.Vector v a)
      => p n -> Vector v (n+m) a -> Vector v n a
take' :: p n -> Vector v (n + m) a -> Vector v n a
take' p n
_ = Vector v (n + m) a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) (m :: Nat) a.
(KnownNat n, Vector v a) =>
Vector v (n + m) a -> Vector v n a
take
{-# inline take' #-}

-- | /O(1)/ Yield all but the the first @n@ elements. The given vector must
-- contain at least this many elements. The length of the resulting vector is
-- inferred from the type.
drop :: forall v n m a. (KnownNat n, VG.Vector v a)
     => Vector v (n+m) a -> Vector v m a
drop :: Vector v (n + m) a -> Vector v m a
drop (Vector v a
v) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> v a -> v a
VG.unsafeDrop Int
i v a
v)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline drop #-}

-- | /O(1)/ Yield all but the the first @n@ elements. The given vector must
-- contain at least this many elements. The length of the resulting vector is
-- givel explicitly as a 'Proxy' argument.
drop' :: forall v n m a p. (KnownNat n, VG.Vector v a)
      => p n -> Vector v (n+m) a -> Vector v m a
drop' :: p n -> Vector v (n + m) a -> Vector v m a
drop' p n
_ = Vector v (n + m) a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) (m :: Nat) a.
(KnownNat n, Vector v a) =>
Vector v (n + m) a -> Vector v m a
drop
{-# inline drop' #-}

-- | /O(1)/ Yield the first @n@ elements, paired with the rest, without copying.
-- The lengths of the resulting vectors are inferred from the type.
splitAt :: forall v n m a. (KnownNat n, VG.Vector v a)
        => Vector v (n+m) a -> (Vector v n a, Vector v m a)
splitAt :: Vector v (n + m) a -> (Vector v n a, Vector v m a)
splitAt (Vector v a
v) = (v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
a, v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
b)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
        (v a
a, v a
b) = Int -> v a -> (v a, v a)
forall (v :: Type -> Type) a.
Vector v a =>
Int -> v a -> (v a, v a)
VG.splitAt Int
i v a
v
{-# inline splitAt #-}

-- | /O(1)/ Yield the first @n@ elements, paired with the rest, without
-- copying.  The length of the first resulting vector is passed explicitly as a
-- 'Proxy' argument.
splitAt' :: forall v n m a p. (KnownNat n, VG.Vector v a)
         => p n -> Vector v (n+m) a -> (Vector v n a, Vector v m a)
splitAt' :: p n -> Vector v (n + m) a -> (Vector v n a, Vector v m a)
splitAt' p n
_ = Vector v (n + m) a -> (Vector v n a, Vector v m a)
forall (v :: Type -> Type) (n :: Nat) (m :: Nat) a.
(KnownNat n, Vector v a) =>
Vector v (n + m) a -> (Vector v n a, Vector v m a)
splitAt
{-# inline splitAt' #-}

--------------------------------------------------------------------------------
-- * Construction
--------------------------------------------------------------------------------

--
-- ** Initialization
--

-- | /O(1)/ Empty vector.
empty :: forall v a. (VG.Vector v a)
      => Vector v 0 a
empty :: Vector v 0 a
empty = v a -> Vector v 0 a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
forall (v :: Type -> Type) a. Vector v a => v a
VG.empty
{-# inline empty #-}

-- | /O(1)/ Vector with exactly one element.
singleton :: forall v a. (VG.Vector v a)
           => a -> Vector v 1 a
singleton :: a -> Vector v 1 a
singleton a
a = v a -> Vector v 1 a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (a -> v a
forall (v :: Type -> Type) a. Vector v a => a -> v a
VG.singleton a
a)
{-# inline singleton #-}

-- | /O(n)/ Construct a vector in a type-safe manner.
-- @
--   fromTuple (1,2) :: Vector v 2 Int
--   fromTuple ("hey", "what's", "going", "on") :: Vector v 4 String
-- @
fromTuple :: forall v a input length.
             (VG.Vector v a, IndexedListLiterals input length a, KnownNat length)
          => input -> Vector v length a
fromTuple :: input -> Vector v length a
fromTuple = v a -> Vector v length a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v length a)
-> (input -> v a) -> input -> Vector v length a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [a] -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> [a] -> v a
VG.fromListN (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy length -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy length -> Integer) -> Proxy length -> Integer
forall a b. (a -> b) -> a -> b
$ Proxy length
forall k (t :: k). Proxy t
Proxy @length) ([a] -> v a) -> (input -> [a]) -> input -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. input -> [a]
forall input (length :: Nat) output.
IndexedListLiterals input length output =>
input -> [output]
ILL.toList

-- | /O(n)/ Construct a vector with the same element in each position where the
-- length is inferred from the type.
replicate :: forall v n a. (KnownNat n, VG.Vector v a)
          => a -> Vector v n a
replicate :: a -> Vector v n a
replicate a
a = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> a -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> a -> v a
VG.replicate Int
i a
a)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline replicate #-}

-- | /O(n)/ Construct a vector with the same element in each position where the
-- length is given explicitly as a 'Proxy' argument.
replicate' :: forall v n a p. (KnownNat n, VG.Vector v a)
           => p n -> a -> Vector v n a
replicate' :: p n -> a -> Vector v n a
replicate' p n
_ = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate
{-# inline replicate' #-}

-- | /O(n)/ Construct a vector of the given length by applying the function to
-- each index where the length is inferred from the type.
generate :: forall v n a. (KnownNat n, VG.Vector v a)
         => (Finite n -> a) -> Vector v n a
generate :: (Finite n -> a) -> Vector v n a
generate Finite n -> a
f = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> (Int -> a) -> v a
forall (v :: Type -> Type) a.
Vector v a =>
Int -> (Int -> a) -> v a
VG.generate Int
i (Finite n -> a
f (Finite n -> a) -> (Int -> Finite n) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral))
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline generate #-}

-- | /O(n)/ Construct a vector of the given length by applying the function to
-- each index where the length is given explicitly as a 'Proxy' argument.
generate' :: forall v n a p. (KnownNat n, VG.Vector v a)
          => p n -> (Finite n -> a) -> Vector v n a
generate' :: p n -> (Finite n -> a) -> Vector v n a
generate' p n
_ = (Finite n -> a) -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
(Finite n -> a) -> Vector v n a
generate
{-# inline generate' #-}

-- | /O(n)/ Apply the function @n@ times to a value. Zeroth element is the original value.
-- The length is inferred from the type.
iterateN :: forall v n a. (KnownNat n, VG.Vector v a)
         => (a -> a) -> a -> Vector v n a
iterateN :: (a -> a) -> a -> Vector v n a
iterateN a -> a
f a
z = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> (a -> a) -> a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
Int -> (a -> a) -> a -> v a
VG.iterateN Int
i a -> a
f a
z)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline iterateN #-}

-- | /O(n)/ Apply the function @n@ times to a value. Zeroth element is the original value.
-- The length is given explicitly as a 'Proxy' argument.
iterateN' :: forall v n a p. (KnownNat n, VG.Vector v a)
          => p n -> (a -> a) -> a -> Vector v n a
iterateN' :: p n -> (a -> a) -> a -> Vector v n a
iterateN' p n
_ = (a -> a) -> a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
(a -> a) -> a -> Vector v n a
iterateN
{-# inline iterateN' #-}

--
-- ** Monadic initialisation
--

-- | /O(n)/ Execute the monadic action @n@ times and store the results in a
-- vector where @n@ is inferred from the type.
replicateM :: forall v n m a. (KnownNat n, VG.Vector v a, Monad m)
           => m a -> m (Vector v n a)
replicateM :: m a -> m (Vector v n a)
replicateM m a
a = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> m (v a) -> m (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m a -> m (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
Int -> m a -> m (v a)
VG.replicateM Int
i m a
a
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline replicateM #-}

-- | /O(n)/ Execute the monadic action @n@ times and store the results in a
-- vector where @n@ is given explicitly as a 'Proxy' argument.
replicateM' :: forall v n m a p. (KnownNat n, VG.Vector v a, Monad m)
            => p n -> m a -> m (Vector v n a)
replicateM' :: p n -> m a -> m (Vector v n a)
replicateM' p n
_ = m a -> m (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) (m :: Type -> Type) a.
(KnownNat n, Vector v a, Monad m) =>
m a -> m (Vector v n a)
replicateM
{-# inline replicateM' #-}

-- | /O(n)/ Construct a vector of length @n@ by applying the monadic action to
-- each index where @n@ is inferred from the type.
generateM :: forall v n m a. (KnownNat n, VG.Vector v a, Monad m)
          => (Finite n -> m a) -> m (Vector v n a)
generateM :: (Finite n -> m a) -> m (Vector v n a)
generateM Finite n -> m a
f = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> m (v a) -> m (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (Int -> m a) -> m (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
Int -> (Int -> m a) -> m (v a)
VG.generateM Int
i (Finite n -> m a
f (Finite n -> m a) -> (Int -> Finite n) -> Int -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline generateM #-}

-- | /O(n)/ Construct a vector of length @n@ by applying the monadic action to
-- each index where @n@ is given explicitly as a 'Proxy' argument.
generateM' :: forall v n m a p. (KnownNat n, VG.Vector v a, Monad m)
           => p n -> (Finite n -> m a) -> m (Vector v n a)
generateM' :: p n -> (Finite n -> m a) -> m (Vector v n a)
generateM' p n
_ = (Finite n -> m a) -> m (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) (m :: Type -> Type) a.
(KnownNat n, Vector v a, Monad m) =>
(Finite n -> m a) -> m (Vector v n a)
generateM
{-# inline generateM' #-}

--
-- ** Unfolding
--

-- | /O(n)/ Construct a vector with exactly @n@ elements by repeatedly applying
-- the generator function to the a seed. The length is inferred from the
-- type.
unfoldrN :: forall v n a b. (KnownNat n, VG.Vector v a)
         => (b -> (a, b)) -> b -> Vector v n a
unfoldrN :: (b -> (a, b)) -> b -> Vector v n a
unfoldrN b -> (a, b)
f b
z = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (Int -> (b -> Maybe (a, b)) -> b -> v a
forall (v :: Type -> Type) a b.
Vector v a =>
Int -> (b -> Maybe (a, b)) -> b -> v a
VG.unfoldrN Int
i ((a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just ((a, b) -> Maybe (a, b)) -> (b -> (a, b)) -> b -> Maybe (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> (a, b)
f) b
z)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline unfoldrN #-}

-- | /O(n)/ Construct a vector with exactly @n@ elements by repeatedly applying
-- the generator function to the a seed. The length is given explicitly
-- as a 'Proxy' argument.
unfoldrN' :: forall v n a b p. (KnownNat n, VG.Vector v a)
          => p n -> (b -> (a, b)) -> b -> Vector v n a
unfoldrN' :: p n -> (b -> (a, b)) -> b -> Vector v n a
unfoldrN' p n
_ = (b -> (a, b)) -> b -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a b.
(KnownNat n, Vector v a) =>
(b -> (a, b)) -> b -> Vector v n a
unfoldrN
{-# inline unfoldrN' #-}

--
-- ** Enumeration
--

-- | /O(n)/ Yield a vector of length @n@ containing the values @x@, @x+1@,
-- ... @x + (n - 1)@. The length is inferred from the type.
enumFromN :: forall v n a. (KnownNat n, VG.Vector v a, Num a)
          => a -> Vector v n a
enumFromN :: a -> Vector v n a
enumFromN a
a = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (a -> Int -> v a
forall (v :: Type -> Type) a.
(Vector v a, Num a) =>
a -> Int -> v a
VG.enumFromN a
a Int
i)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline enumFromN #-}

-- | /O(n)/ Yield a vector of length @n@ containing the values @x@, @x+1@,
-- ..., @x + (n - 1)@ The length is given explicitly as a 'Proxy' argument.
enumFromN' :: forall v n a p. (KnownNat n, VG.Vector v a, Num a)
           => a -> p n -> Vector v n a
enumFromN' :: a -> p n -> Vector v n a
enumFromN' a
a p n
_ = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a, Num a) =>
a -> Vector v n a
enumFromN a
a
{-# inline enumFromN' #-}

-- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+y@,
-- @x+2y@, ... @x + (n - 1)y@. The length is inferred from the type.
enumFromStepN :: forall v n a. (KnownNat n, VG.Vector v a, Num a)
          => a -> a -> Vector v n a
enumFromStepN :: a -> a -> Vector v n a
enumFromStepN a
a a
a' = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (a -> a -> Int -> v a
forall (v :: Type -> Type) a.
(Vector v a, Num a) =>
a -> a -> Int -> v a
VG.enumFromStepN a
a a
a' Int
i)
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline enumFromStepN #-}

-- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+y@,
-- @x+2y@, ..., @x + (n - 1)y@. The length is given explicitly as a 'Proxy' argument.
enumFromStepN' :: forall v n a p. (KnownNat n, VG.Vector v a, Num a)
               => a -> a -> p n -> Vector v n a
enumFromStepN' :: a -> a -> p n -> Vector v n a
enumFromStepN' a
a a
a' p n
_ = a -> a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a, Num a) =>
a -> a -> Vector v n a
enumFromStepN a
a a
a'
{-# inline enumFromStepN' #-}

--
-- ** Concatenation
--

-- | /O(n)/ Prepend an element.
cons :: forall v n a. VG.Vector v a
     => a -> Vector v n a -> Vector v (1+n) a
cons :: a -> Vector v n a -> Vector v (1 + n) a
cons a
x (Vector v a
xs) = v a -> Vector v (1 + n) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (a -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => a -> v a -> v a
VG.cons a
x v a
xs)
{-# inline cons #-}

-- | /O(n)/ Append an element.
snoc :: forall v n a. VG.Vector v a
     => Vector v n a -> a -> Vector v (n+1) a
snoc :: Vector v n a -> a -> Vector v (n + 1) a
snoc (Vector v a
xs) a
x = v a -> Vector v (n + 1) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> a -> v a
VG.snoc v a
xs a
x)
{-# inline snoc #-}

-- | /O(m+n)/ Concatenate two vectors.
(++) :: forall v n m a. VG.Vector v a
     => Vector v n a -> Vector v m a -> Vector v (n+m) a
Vector v a
vn ++ :: Vector v n a -> Vector v m a -> Vector v (n + m) a
++ Vector v a
vm = v a -> Vector v (n + m) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a
vn v a -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> v a -> v a
VG.++ v a
vm)
{-# inline (++) #-}

--
-- ** Restricting memory usage
--

-- | /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 :: VG.Vector v a => Vector v n a -> Vector v n a
force :: Vector v n a -> Vector v n a
force (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> v a
VG.force v a
v)
{-# inline force #-}


--------------------------------------------------------------------------------
-- * Modifying vectors
--------------------------------------------------------------------------------

--
-- ** Bulk updates
--

-- | /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>
--
(//) :: VG.Vector v a
     => Vector v m a    -- ^ initial vector (of length @m@)
     -> [(Finite m, a)] -- ^ list of index/value pairs (of length @n@)
     -> Vector v m a
Vector v a
v // :: Vector v m a -> [(Finite m, a)] -> Vector v m a
// [(Finite m, a)]
us = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a
v v a -> [(Int, a)] -> v a
forall (v :: Type -> Type) a.
Vector v a =>
v a -> [(Int, a)] -> v a
VG.// (((Finite m, a) -> (Int, a)) -> [(Finite m, a)] -> [(Int, a)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Finite m, a) -> (Int, a)) -> [(Finite m, a)] -> [(Int, a)])
-> ((Finite m -> Int) -> (Finite m, a) -> (Int, a))
-> (Finite m -> Int)
-> [(Finite m, a)]
-> [(Int, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Finite m -> Int) -> (Finite m, a) -> (Int, a)
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first) (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int) -> (Finite m -> Integer) -> Finite m -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite m -> Integer
forall (n :: Nat). Finite n -> Integer
getFinite) [(Finite m, a)]
us)
{-# inline (//) #-}

-- | /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 :: (VG.Vector v a, VG.Vector v (Int, a))
        => Vector v m a        -- ^ initial vector (of length @m@)
        -> Vector v n (Int, a) -- ^ vector of index/value pairs (of length @n@)
        -> Vector v m a
update :: Vector v m a -> Vector v n (Int, a) -> Vector v m a
update (Vector v a
v) (Vector v (Int, a)
w) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v (Int, a) -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v (Int, a)) =>
v a -> v (Int, a) -> v a
VG.update v a
v v (Int, a)
w)
{-# inline update #-}

-- | /O(m+n)/ 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_ :: (VG.Vector v a, VG.Vector v Int)
        => Vector v m a   -- ^ initial vector (of length @m@)
        -> Vector v n Int -- ^ index vector (of length @n@)
        -> Vector v n a   -- ^ value vector (of length @n@)
        -> Vector v m a
update_ :: Vector v m a -> Vector v n Int -> Vector v n a -> Vector v m a
update_ (Vector v a
v) (Vector v Int
is) (Vector v a
w) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v Int -> v a -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v Int) =>
v a -> v Int -> v a -> v a
VG.update_ v a
v v Int
is v a
w)
{-# inline update_ #-}

-- | Same as ('//') but without bounds checking.
unsafeUpd :: (VG.Vector v a)
          => Vector v m a -- ^ initial vector (of length @m@)
          -> [(Int, a)]   -- ^ list of index/value pairs (of length @n@)
          -> Vector v m a
unsafeUpd :: Vector v m a -> [(Int, a)] -> Vector v m a
unsafeUpd (Vector v a
v) [(Int, a)]
us = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> [(Int, a)] -> v a
forall (v :: Type -> Type) a.
Vector v a =>
v a -> [(Int, a)] -> v a
VG.unsafeUpd v a
v [(Int, a)]
us)
{-# inline unsafeUpd #-}

-- | Same as 'update' but without bounds checking.
unsafeUpdate :: (VG.Vector v a, VG.Vector v (Int, a))
             => Vector v m a        -- ^ initial vector (of length @m@)
             -> Vector v n (Int, a) -- ^ vector of index/value pairs (of length @n@)
             -> Vector v m a
unsafeUpdate :: Vector v m a -> Vector v n (Int, a) -> Vector v m a
unsafeUpdate (Vector v a
v) (Vector v (Int, a)
w) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v (Int, a) -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v (Int, a)) =>
v a -> v (Int, a) -> v a
VG.unsafeUpdate v a
v v (Int, a)
w)
{-# inline unsafeUpdate #-}

-- | Same as 'update_' but without bounds checking.
unsafeUpdate_ :: (VG.Vector v a, VG.Vector v Int)
              => Vector v m a   -- ^ initial vector (of length @m@)
              -> Vector v n Int -- ^ index vector (of length @n@)
              -> Vector v n a   -- ^ value vector (of length @n@)
              -> Vector v m a
unsafeUpdate_ :: Vector v m a -> Vector v n Int -> Vector v n a -> Vector v m a
unsafeUpdate_ (Vector v a
v) (Vector v Int
is) (Vector v a
w) =
  v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v Int -> v a -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v Int) =>
v a -> v Int -> v a -> v a
VG.unsafeUpdate_ v a
v v Int
is v a
w)
{-# inline unsafeUpdate_ #-}

--
-- ** Accumulations
--

-- | /O(m+n)/ For each pair @(i,b)@ from the list, replace the vector element
-- @a@ at position @i@ by @f a b@.
--
-- > accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
accum :: VG.Vector v a
      => (a -> b -> a) -- ^ accumulating function @f@
      -> Vector v m a  -- ^ initial vector (of length @m@)
      -> [(Finite m,b)]     -- ^ list of index/value pairs (of length @n@)
      -> Vector v m a
accum :: (a -> b -> a) -> Vector v m a -> [(Finite m, b)] -> Vector v m a
accum a -> b -> a
f (Vector v a
v) [(Finite m, b)]
us =
  v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> [(Int, b)] -> v a
forall (v :: Type -> Type) a b.
Vector v a =>
(a -> b -> a) -> v a -> [(Int, b)] -> v a
VG.accum a -> b -> a
f v a
v ([(Int, b)] -> v a) -> [(Int, b)] -> v a
forall a b. (a -> b) -> a -> b
$ (((Finite m, b) -> (Int, b)) -> [(Finite m, b)] -> [(Int, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Finite m, b) -> (Int, b)) -> [(Finite m, b)] -> [(Int, b)])
-> ((Finite m -> Int) -> (Finite m, b) -> (Int, b))
-> (Finite m -> Int)
-> [(Finite m, b)]
-> [(Int, b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Finite m -> Int) -> (Finite m, b) -> (Int, b)
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first) (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int) -> (Finite m -> Integer) -> Finite m -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite m -> Integer
forall (n :: Nat). Finite n -> Integer
getFinite) [(Finite m, b)]
us)
{-# inline accum #-}

-- | /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@.
--
-- > accumulate (+) <5,9,2> <(2,4),(1,6),(0,3),(1,7)> = <5+3, 9+6+7, 2+4>
accumulate :: (VG.Vector v a, VG.Vector v (Int, b))
           => (a -> b -> a)      -- ^ accumulating function @f@
           -> Vector v m a       -- ^ initial vector (of length @m@)
           -> Vector v n (Int,b) -- ^ vector of index/value pairs (of length @n@)
           -> Vector v m a
accumulate :: (a -> b -> a)
-> Vector v m a -> Vector v n (Int, b) -> Vector v m a
accumulate a -> b -> a
f (Vector v a
v) (Vector v (Int, b)
us) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> v (Int, b) -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v (Int, b)) =>
(a -> b -> a) -> v a -> v (Int, b) -> v a
VG.accumulate a -> b -> a
f v a
v v (Int, b)
us)
{-# inline accumulate #-}

-- | /O(m+n)/ 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_ :: (VG.Vector v a, VG.Vector v Int, VG.Vector v b)
            => (a -> b -> a)  -- ^ accumulating function @f@
            -> Vector v m a   -- ^ initial vector (of length @m@)
            -> Vector v n Int -- ^ index vector (of length @n@)
            -> Vector v n b   -- ^ value vector (of length @n@)
            -> Vector v m a
accumulate_ :: (a -> b -> a)
-> Vector v m a -> Vector v n Int -> Vector v n b -> Vector v m a
accumulate_ a -> b -> a
f (Vector v a
v) (Vector v Int
is) (Vector v b
xs) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> v Int -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v Int, Vector v b) =>
(a -> b -> a) -> v a -> v Int -> v b -> v a
VG.accumulate_ a -> b -> a
f v a
v v Int
is v b
xs)
{-# inline accumulate_ #-}

-- | Same as 'accum' but without bounds checking.
unsafeAccum :: VG.Vector v a
            => (a -> b -> a) -- ^ accumulating function @f@
            -> Vector v m a  -- ^ initial vector (of length @m@)
            -> [(Int,b)]     -- ^ list of index/value pairs (of length @n@)
            -> Vector v m a
unsafeAccum :: (a -> b -> a) -> Vector v m a -> [(Int, b)] -> Vector v m a
unsafeAccum a -> b -> a
f (Vector v a
v) [(Int, b)]
us = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> [(Int, b)] -> v a
forall (v :: Type -> Type) a b.
Vector v a =>
(a -> b -> a) -> v a -> [(Int, b)] -> v a
VG.unsafeAccum a -> b -> a
f v a
v [(Int, b)]
us)
{-# inline unsafeAccum #-}

-- | Same as 'accumulate' but without bounds checking.
unsafeAccumulate :: (VG.Vector v a, VG.Vector v (Int, b))
                 => (a -> b -> a)      -- ^ accumulating function @f@
                 -> Vector v m a       -- ^ initial vector (of length @m@)
                 -> Vector v n (Int,b) -- ^ vector of index/value pairs (of length @n@)
                 -> Vector v m a
unsafeAccumulate :: (a -> b -> a)
-> Vector v m a -> Vector v n (Int, b) -> Vector v m a
unsafeAccumulate a -> b -> a
f (Vector v a
v) (Vector v (Int, b)
us) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> v (Int, b) -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v (Int, b)) =>
(a -> b -> a) -> v a -> v (Int, b) -> v a
VG.unsafeAccumulate a -> b -> a
f v a
v v (Int, b)
us)
{-# inline unsafeAccumulate #-}

-- | Same as 'accumulate_' but without bounds checking.
unsafeAccumulate_ :: (VG.Vector v a, VG.Vector v Int, VG.Vector v b)
            => (a -> b -> a)  -- ^ accumulating function @f@
            -> Vector v m a   -- ^ initial vector (of length @m@)
            -> Vector v n Int -- ^ index vector (of length @n@)
            -> Vector v n b   -- ^ value vector (of length @n@)
            -> Vector v m a
unsafeAccumulate_ :: (a -> b -> a)
-> Vector v m a -> Vector v n Int -> Vector v n b -> Vector v m a
unsafeAccumulate_ a -> b -> a
f (Vector v a
v) (Vector v Int
is) (Vector v b
xs) = v a -> Vector v m a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> v a -> v Int -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v Int, Vector v b) =>
(a -> b -> a) -> v a -> v Int -> v b -> v a
VG.unsafeAccumulate_ a -> b -> a
f v a
v v Int
is v b
xs)
{-# inline unsafeAccumulate_ #-}

--
-- ** Permutations
--

-- | /O(n)/ Reverse a vector
reverse :: (VG.Vector v a) => Vector v n a -> Vector v n a
reverse :: Vector v n a -> Vector v n a
reverse (Vector v a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v a
forall (v :: Type -> Type) a. Vector v a => v a -> v a
VG.reverse v a
v)
{-# inline reverse #-}

-- | /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 :: (VG.Vector v a, VG.Vector v Int)
            => Vector v m a   -- ^ @xs@ value vector
            -> Vector v n Int -- ^ @is@ index vector (of length @n@)
            -> Vector v n a
backpermute :: Vector v m a -> Vector v n Int -> Vector v n a
backpermute (Vector v a
v) (Vector v Int
is) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v Int -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v Int) =>
v a -> v Int -> v a
VG.backpermute v a
v v Int
is)
{-# inline backpermute #-}

-- | Same as 'backpermute' but without bounds checking.
unsafeBackpermute :: (VG.Vector v a, VG.Vector v Int)
                  => Vector v m a   -- ^ @xs@ value vector
                  -> Vector v n Int -- ^ @is@ index vector (of length @n@)
                  -> Vector v n a
unsafeBackpermute :: Vector v m a -> Vector v n Int -> Vector v n a
unsafeBackpermute (Vector v a
v) (Vector v Int
is) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> v Int -> v a
forall (v :: Type -> Type) a.
(Vector v a, Vector v Int) =>
v a -> v Int -> v a
VG.unsafeBackpermute v a
v v Int
is)
{-# inline unsafeBackpermute #-}

--------------------------------------------------------------------------------
-- * Elementwise Operations
--------------------------------------------------------------------------------

--
-- ** Indexing
--

-- | /O(n)/ Pair each element in a vector with its index.
indexed :: (VG.Vector v a, VG.Vector v (Int, a), VG.Vector v (Finite n,a))
        => Vector v n a -> Vector v n (Finite n,a)
indexed :: Vector v n a -> Vector v n (Finite n, a)
indexed (Vector v a
v) = v (Finite n, a) -> Vector v n (Finite n, a)
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((((Int, a) -> (Finite n, a)) -> v (Int, a) -> v (Finite n, a)
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map (((Int, a) -> (Finite n, a)) -> v (Int, a) -> v (Finite n, a))
-> ((Int -> Finite n) -> (Int, a) -> (Finite n, a))
-> (Int -> Finite n)
-> v (Int, a)
-> v (Finite n, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Finite n) -> (Int, a) -> (Finite n, a)
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first) (Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (v (Int, a) -> v (Finite n, a)) -> v (Int, a) -> v (Finite n, a)
forall a b. (a -> b) -> a -> b
$ v a -> v (Int, a)
forall (v :: Type -> Type) a.
(Vector v a, Vector v (Int, a)) =>
v a -> v (Int, a)
VG.indexed v a
v)
{-# inline indexed #-}

--
-- ** Mapping
--

-- | /O(n)/ Map a function over a vector.
map :: (VG.Vector v a, VG.Vector v b)
    => (a -> b) -> Vector v n a -> Vector v n b
map :: (a -> b) -> Vector v n a -> Vector v n b
map a -> b
f (Vector v a
v) = v b -> Vector v n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b) -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map a -> b
f v a
v)
{-# inline map #-}

-- | /O(n)/ Apply a function to every element of a vector and its index.
imap :: (VG.Vector v a, VG.Vector v b)
     => (Finite n -> a -> b) -> Vector v n a -> Vector v n b
imap :: (Finite n -> a -> b) -> Vector v n a -> Vector v n b
imap Finite n -> a -> b
f (Vector v a
v) = v b -> Vector v n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b) -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(Int -> a -> b) -> v a -> v b
VG.imap (Finite n -> a -> b
f (Finite n -> a -> b) -> (Int -> Finite n) -> Int -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
v)
{-# inline imap #-}

-- | /O(n*m)/ Map a function over a vector and concatenate the results. The
-- function is required to always return a vector of the same length.
concatMap :: (VG.Vector v a, VG.Vector v' b)
          => (a -> Vector v' m b) -> Vector v n a -> Vector v' (n*m) b
concatMap :: (a -> Vector v' m b) -> Vector v n a -> Vector v' (n * m) b
concatMap a -> Vector v' m b
f (Vector v a
v) = v' b -> Vector v' (n * m) b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v' b -> Vector v' (n * m) b)
-> (v a -> v' b) -> v a -> Vector v' (n * m) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [v' b] -> v' b
forall (v :: Type -> Type) a. Vector v a => [v a] -> v a
VG.concat ([v' b] -> v' b) -> (v a -> [v' b]) -> v a -> v' b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> v' b) -> [a] -> [v' b]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Vector v' m b -> v' b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized (Vector v' m b -> v' b) -> (a -> Vector v' m b) -> a -> v' b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Vector v' m b
f) ([a] -> [v' b]) -> (v a -> [a]) -> v a -> [v' b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> [a]
forall (v :: Type -> Type) a. Vector v a => v a -> [a]
VG.toList (v a -> Vector v' (n * m) b) -> v a -> Vector v' (n * m) b
forall a b. (a -> b) -> a -> b
$ v a
v
{-# inline concatMap #-}

--
-- ** Monadic mapping
--

-- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
-- vector of results.
mapM :: (Monad m, VG.Vector v a, VG.Vector v b)
      => (a -> m b) -> Vector v n a -> m (Vector v n b)
mapM :: (a -> m b) -> Vector v n a -> m (Vector v n b)
mapM a -> m b
f (Vector v a
v) = v b -> Vector v n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v b -> Vector v n b) -> m (v b) -> m (Vector v n b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> m b) -> v a -> m (v b)
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a, Vector v b) =>
(a -> m b) -> v a -> m (v b)
VG.mapM a -> m b
f v a
v
{-# inline mapM #-}

-- | /O(n)/ Apply the monadic action to every element of a vector and its
-- index, yielding a vector of results.
imapM :: (Monad m, VG.Vector v a, VG.Vector v b)
      => (Finite n -> a -> m b) -> Vector v n a -> m (Vector v n b)
imapM :: (Finite n -> a -> m b) -> Vector v n a -> m (Vector v n b)
imapM Finite n -> a -> m b
f (Vector v a
v) = v b -> Vector v n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v b -> Vector v n b) -> m (v b) -> m (Vector v n b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> a -> m b) -> v a -> m (v b)
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a, Vector v b) =>
(Int -> a -> m b) -> v a -> m (v b)
VG.imapM (Finite n -> a -> m b
f (Finite n -> a -> m b) -> (Int -> Finite n) -> Int -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
v
{-# inline imapM #-}

-- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
-- results.
mapM_ :: (Monad m, VG.Vector v a) => (a -> m b) -> Vector v n a -> m ()
mapM_ :: (a -> m b) -> Vector v n a -> m ()
mapM_ a -> m b
f (Vector v a
v) = (a -> m b) -> v a -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a) =>
(a -> m b) -> v a -> m ()
VG.mapM_ a -> m b
f v a
v
{-# inline mapM_ #-}

-- | /O(n)/ Apply the monadic action to every element of a vector and its
-- index, ignoring the results.
imapM_ :: (Monad m, VG.Vector v a) => (Finite n -> a -> m b) -> Vector v n a -> m ()
imapM_ :: (Finite n -> a -> m b) -> Vector v n a -> m ()
imapM_ Finite n -> a -> m b
f (Vector v a
v) = (Int -> a -> m b) -> v a -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a) =>
(Int -> a -> m b) -> v a -> m ()
VG.imapM_ (Finite n -> a -> m b
f (Finite n -> a -> m b) -> (Int -> Finite n) -> Int -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
v
{-# inline imapM_ #-}

-- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
-- vector of results. Equvalent to @flip 'mapM'@.
forM :: (Monad m, VG.Vector v a, VG.Vector v b)
     => Vector v n a -> (a -> m b) -> m (Vector v n b)
forM :: Vector v n a -> (a -> m b) -> m (Vector v n b)
forM (Vector v a
v) a -> m b
f = v b -> Vector v n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v b -> Vector v n b) -> m (v b) -> m (Vector v n b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> v a -> (a -> m b) -> m (v b)
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a, Vector v b) =>
v a -> (a -> m b) -> m (v b)
VG.forM v a
v a -> m b
f
{-# inline forM #-}

-- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
-- results. Equivalent to @flip 'mapM_'@.
forM_ :: (Monad m, VG.Vector v a) => Vector v n a -> (a -> m b) -> m ()
forM_ :: Vector v n a -> (a -> m b) -> m ()
forM_ (Vector v a
v) = v a -> (a -> m b) -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a b.
(Monad m, Vector v a) =>
v a -> (a -> m b) -> m ()
VG.forM_ v a
v
{-# inline forM_ #-}

--
-- ** Zipping
--

-- | /O(n)/ Zip two vectors of the same length with the given function.
zipWith :: (VG.Vector v a, VG.Vector v b, VG.Vector v c)
        => (a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith :: (a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> b -> c
f (Vector v a
as) (Vector v b
bs) = v c -> Vector v n c
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> c) -> v a -> v b -> v c
forall (v :: Type -> Type) a b c.
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> v a -> v b -> v c
VG.zipWith a -> b -> c
f v a
as v b
bs)
{-# inline zipWith #-}

-- | Zip three vectors with the given function.
zipWith3 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v d)
         => (a -> b -> c -> d) -> Vector v n a -> Vector v n b -> Vector v n c -> Vector v n d
zipWith3 :: (a -> b -> c -> d)
-> Vector v n a -> Vector v n b -> Vector v n c -> Vector v n d
zipWith3 a -> b -> c -> d
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) = v d -> Vector v n d
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> c -> d) -> v a -> v b -> v c -> v d
forall (v :: Type -> Type) a b c d.
(Vector v a, Vector v b, Vector v c, Vector v d) =>
(a -> b -> c -> d) -> v a -> v b -> v c -> v d
VG.zipWith3 a -> b -> c -> d
f v a
as v b
bs v c
cs)
{-# inline zipWith3 #-}

zipWith4 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e)
         => (a -> b -> c -> d -> e)
         -> Vector v n a
         -> Vector v n b
         -> Vector v n c
         -> Vector v n d
         -> Vector v n e
zipWith4 :: (a -> b -> c -> d -> e)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
zipWith4 a -> b -> c -> d -> e
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds)
  = v e -> Vector v n e
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> c -> d -> e) -> v a -> v b -> v c -> v d -> v e
forall (v :: Type -> Type) a b c d e.
(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
VG.zipWith4 a -> b -> c -> d -> e
f v a
as v b
bs v c
cs v d
ds)
{-# inline zipWith4 #-}

zipWith5 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v f)
         => (a -> b -> c -> d -> e -> f)
         -> Vector v n a
         -> Vector v n b
         -> Vector v n c
         -> Vector v n d
         -> Vector v n e
         -> Vector v n f
zipWith5 :: (a -> b -> c -> d -> e -> f)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
zipWith5 a -> b -> c -> d -> e -> f
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds) (Vector v e
es)
  = v f -> Vector v n f
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> c -> d -> e -> f)
-> v a -> v b -> v c -> v d -> v e -> v f
forall (v :: Type -> Type) a b c d e f.
(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
VG.zipWith5 a -> b -> c -> d -> e -> f
f v a
as v b
bs v c
cs v d
ds v e
es)
{-# inline zipWith5 #-}

zipWith6 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v f,VG.Vector v g)
         => (a -> b -> c -> d -> e -> f -> g)
         -> Vector v n a
         -> Vector v n b
         -> Vector v n c
         -> Vector v n d
         -> Vector v n e
         -> Vector v n f
         -> Vector v n g
zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
-> Vector v n g
zipWith6 a -> b -> c -> d -> e -> f -> g
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds) (Vector v e
es) (Vector v f
fs)
  = v g -> Vector v n g
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> c -> d -> e -> f -> g)
-> v a -> v b -> v c -> v d -> v e -> v f -> v g
forall (v :: Type -> Type) a b c d e f g.
(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
VG.zipWith6 a -> b -> c -> d -> e -> f -> g
f v a
as v b
bs v c
cs v d
ds v e
es v f
fs)
{-# inline zipWith6 #-}

-- | /O(n)/ Zip two vectors of the same length with a function that also takes
-- the elements' indices).
izipWith :: (VG.Vector v a,VG.Vector v b,VG.Vector v c)
         => (Finite n -> a -> b -> c)
         -> Vector v n a
         -> Vector v n b
         -> Vector v n c
izipWith :: (Finite n -> a -> b -> c)
-> Vector v n a -> Vector v n b -> Vector v n c
izipWith Finite n -> a -> b -> c
f (Vector v a
xs) (Vector v b
ys)
  = v c -> Vector v n c
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b -> c) -> v a -> v b -> v c
forall (v :: Type -> Type) a b c.
(Vector v a, Vector v b, Vector v c) =>
(Int -> a -> b -> c) -> v a -> v b -> v c
VG.izipWith (Finite n -> a -> b -> c
f (Finite n -> a -> b -> c)
-> (Int -> Finite n) -> Int -> a -> b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
xs v b
ys)
{-# inline izipWith #-}

izipWith3 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d)
          => (Finite n -> a -> b -> c -> d)
          -> Vector v n a
          -> Vector v n b
          -> Vector v n c
          -> Vector v n d
izipWith3 :: (Finite n -> a -> b -> c -> d)
-> Vector v n a -> Vector v n b -> Vector v n c -> Vector v n d
izipWith3 Finite n -> a -> b -> c -> d
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs)
  = v d -> Vector v n d
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b -> c -> d) -> v a -> v b -> v c -> v d
forall (v :: Type -> Type) a b c d.
(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
VG.izipWith3 (Finite n -> a -> b -> c -> d
f (Finite n -> a -> b -> c -> d)
-> (Int -> Finite n) -> Int -> a -> b -> c -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs v c
cs)
{-# inline izipWith3 #-}

izipWith4 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e)
          => (Finite n -> a -> b -> c -> d -> e)
          -> Vector v n a
          -> Vector v n b
          -> Vector v n c
          -> Vector v n d
          -> Vector v n e
izipWith4 :: (Finite n -> a -> b -> c -> d -> e)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
izipWith4 Finite n -> a -> b -> c -> d -> e
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds)
  = v e -> Vector v n e
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b -> c -> d -> e) -> v a -> v b -> v c -> v d -> v e
forall (v :: Type -> Type) a b c d e.
(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
VG.izipWith4 (Finite n -> a -> b -> c -> d -> e
f (Finite n -> a -> b -> c -> d -> e)
-> (Int -> Finite n) -> Int -> a -> b -> c -> d -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs v c
cs v d
ds)
{-# inline izipWith4 #-}

izipWith5 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v f)
          => (Finite n -> a -> b -> c -> d -> e -> f)
          -> Vector v n a
          -> Vector v n b
          -> Vector v n c
          -> Vector v n d
          -> Vector v n e
          -> Vector v n f
izipWith5 :: (Finite n -> a -> b -> c -> d -> e -> f)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
izipWith5 Finite n -> a -> b -> c -> d -> e -> f
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds) (Vector v e
es)
  = v f -> Vector v n f
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b -> c -> d -> e -> f)
-> v a -> v b -> v c -> v d -> v e -> v f
forall (v :: Type -> Type) a b c d e f.
(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
VG.izipWith5 (Finite n -> a -> b -> c -> d -> e -> f
f (Finite n -> a -> b -> c -> d -> e -> f)
-> (Int -> Finite n) -> Int -> a -> b -> c -> d -> e -> f
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs v c
cs v d
ds v e
es)
{-# inline izipWith5 #-}

izipWith6 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v f,VG.Vector v g)
          => (Finite n -> a -> b -> c -> d -> e -> f -> g)
          -> Vector v n a
          -> Vector v n b
          -> Vector v n c
          -> Vector v n d
          -> Vector v n e
          -> Vector v n f
          -> Vector v n g
izipWith6 :: (Finite n -> a -> b -> c -> d -> e -> f -> g)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
-> Vector v n g
izipWith6 Finite n -> a -> b -> c -> d -> e -> f -> g
f (Vector v a
as) (Vector v b
bs) (Vector v c
cs) (Vector v d
ds) (Vector v e
es) (Vector v f
fs)
  = v g -> Vector v n g
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((Int -> a -> b -> c -> d -> e -> f -> g)
-> v a -> v b -> v c -> v d -> v e -> v f -> v g
forall (v :: Type -> Type) a b c d e f g.
(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
VG.izipWith6 (Finite n -> a -> b -> c -> d -> e -> f -> g
f (Finite n -> a -> b -> c -> d -> e -> f -> g)
-> (Int -> Finite n) -> Int -> a -> b -> c -> d -> e -> f -> g
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs v c
cs v d
ds v e
es v f
fs)
{-# inline izipWith6 #-}

-- | /O(n)/ Zip two vectors of the same length
zip :: (VG.Vector v a, VG.Vector v b, VG.Vector v (a,b))
    => Vector v n a -> Vector v n b -> Vector v n (a, b)
zip :: Vector v n a -> Vector v n b -> Vector v n (a, b)
zip = (a -> b -> (a, b))
-> Vector v n a -> Vector v n b -> Vector v n (a, b)
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith (,)
{-# inline zip #-}

zip3 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v (a, b, c))
     => Vector v n a -> Vector v n b -> Vector v n c -> Vector v n (a, b, c)
zip3 :: Vector v n a
-> Vector v n b -> Vector v n c -> Vector v n (a, b, c)
zip3 = (a -> b -> c -> (a, b, c))
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n (a, b, c)
forall (v :: Type -> Type) a b c d (n :: Nat).
(Vector v a, Vector v b, Vector v c, Vector v d) =>
(a -> b -> c -> d)
-> Vector v n a -> Vector v n b -> Vector v n c -> Vector v n d
zipWith3 (,,)
{-# inline zip3 #-}

zip4 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v (a,b,c,d))
     => Vector v n a
     -> Vector v n b
     -> Vector v n c
     -> Vector v n d
     -> Vector v n (a,b,c,d)
zip4 :: Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n (a, b, c, d)
zip4 = (a -> b -> c -> d -> (a, b, c, d))
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n (a, b, c, d)
forall (v :: Type -> Type) a b c d e (n :: Nat).
(Vector v a, Vector v b, Vector v c, Vector v d, Vector v e) =>
(a -> b -> c -> d -> e)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
zipWith4 (,,,)
{-# inline zip4 #-}

zip5 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v (a,b,c,d,e))
     => Vector v n a
     -> Vector v n b
     -> Vector v n c
     -> Vector v n d
     -> Vector v n e
     -> Vector v n (a,b,c,d,e)
zip5 :: Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n (a, b, c, d, e)
zip5 = (a -> b -> c -> d -> e -> (a, b, c, d, e))
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n (a, b, c, d, e)
forall (v :: Type -> Type) a b c d e f (n :: Nat).
(Vector v a, Vector v b, Vector v c, Vector v d, Vector v e,
 Vector v f) =>
(a -> b -> c -> d -> e -> f)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
zipWith5 (,,,,)
{-# inline zip5 #-}

zip6 :: (VG.Vector v a,VG.Vector v b,VG.Vector v c,VG.Vector v d,VG.Vector v e,VG.Vector v f,VG.Vector v (a,b,c,d,e,f))
     => Vector v n a
     -> Vector v n b
     -> Vector v n c
     -> Vector v n d
     -> Vector v n e
     -> Vector v n f
     -> Vector v n (a,b,c,d,e,f)
zip6 :: Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
-> Vector v n (a, b, c, d, e, f)
zip6 = (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
-> Vector v n (a, b, c, d, e, f)
forall (v :: Type -> Type) a b c d e f g (n :: Nat).
(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)
-> Vector v n a
-> Vector v n b
-> Vector v n c
-> Vector v n d
-> Vector v n e
-> Vector v n f
-> Vector v n g
zipWith6 (,,,,,)
{-# inline zip6 #-}

--
-- ** Monadic zipping
--

-- | /O(n)/ Zip the two vectors of the same length with the monadic action and
-- yield a vector of results.
zipWithM :: (Monad m, VG.Vector v a, VG.Vector v b, VG.Vector v c)
         => (a -> b -> m c) -> Vector v n a -> Vector v n b -> m (Vector v n c)
zipWithM :: (a -> b -> m c) -> Vector v n a -> Vector v n b -> m (Vector v n c)
zipWithM a -> b -> m c
f (Vector v a
as) (Vector v b
bs) = v c -> Vector v n c
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v c -> Vector v n c) -> m (v c) -> m (Vector v n c)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> b -> m c) -> v a -> v b -> m (v c)
forall (m :: Type -> Type) (v :: Type -> Type) a b c.
(Monad m, Vector v a, Vector v b, Vector v c) =>
(a -> b -> m c) -> v a -> v b -> m (v c)
VG.zipWithM a -> b -> m c
f v a
as v b
bs
{-# inline zipWithM #-}

-- | /O(n)/ Zip the two vectors with a monadic action that also takes the
-- element index and yield a vector of results.
izipWithM :: (Monad m, VG.Vector v a, VG.Vector v b, VG.Vector v c)
         => (Finite n -> a -> b -> m c) -> Vector v n a -> Vector v n b -> m (Vector v n c)
izipWithM :: (Finite n -> a -> b -> m c)
-> Vector v n a -> Vector v n b -> m (Vector v n c)
izipWithM Finite n -> a -> b -> m c
m (Vector v a
as) (Vector v b
bs) = v c -> Vector v n c
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v c -> Vector v n c) -> m (v c) -> m (Vector v n c)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> a -> b -> m c) -> v a -> v b -> m (v c)
forall (m :: Type -> Type) (v :: Type -> Type) a b c.
(Monad m, Vector v a, Vector v b, Vector v c) =>
(Int -> a -> b -> m c) -> v a -> v b -> m (v c)
VG.izipWithM (Finite n -> a -> b -> m c
m (Finite n -> a -> b -> m c)
-> (Int -> Finite n) -> Int -> a -> b -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs
{-# inline izipWithM #-}

-- | /O(n)/ Zip the two vectors with the monadic action and ignore the results.
zipWithM_ :: (Monad m, VG.Vector v a, VG.Vector v b)
          => (a -> b -> m c) -> Vector v n a -> Vector v n b -> m ()
zipWithM_ :: (a -> b -> m c) -> Vector v n a -> Vector v n b -> m ()
zipWithM_ a -> b -> m c
f (Vector v a
as) (Vector v b
bs) = (a -> b -> m c) -> v a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a b c.
(Monad m, Vector v a, Vector v b) =>
(a -> b -> m c) -> v a -> v b -> m ()
VG.zipWithM_ a -> b -> m c
f v a
as v b
bs
{-# inline zipWithM_ #-}

-- | /O(n)/ Zip the two vectors with a monadic action that also takes
-- the element index and ignore the results.
izipWithM_ :: (Monad m, VG.Vector v a, VG.Vector v b)
           => (Finite n -> a -> b -> m c) -> Vector v n a -> Vector v n b -> m ()
izipWithM_ :: (Finite n -> a -> b -> m c) -> Vector v n a -> Vector v n b -> m ()
izipWithM_ Finite n -> a -> b -> m c
m (Vector v a
as) (Vector v b
bs) = (Int -> a -> b -> m c) -> v a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a b c.
(Monad m, Vector v a, Vector v b) =>
(Int -> a -> b -> m c) -> v a -> v b -> m ()
VG.izipWithM_ (Finite n -> a -> b -> m c
m (Finite n -> a -> b -> m c)
-> (Int -> Finite n) -> Int -> a -> b -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) v a
as v b
bs
{-# inline izipWithM_ #-}

-- Unzipping
-- ---------

-- | /O(min(m,n))/ Unzip a vector of pairs.
unzip :: (VG.Vector v a, VG.Vector v b, VG.Vector v (a,b))
      => Vector v n (a, b) -> (Vector v n a, Vector v n b)
unzip :: Vector v n (a, b) -> (Vector v n a, Vector v n b)
unzip Vector v n (a, b)
xs = (((a, b) -> a) -> Vector v n (a, b) -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (a, b) -> a
forall a b. (a, b) -> a
fst Vector v n (a, b)
xs, ((a, b) -> b) -> Vector v n (a, b) -> Vector v n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (a, b) -> b
forall a b. (a, b) -> b
snd Vector v n (a, b)
xs)
{-# inline unzip #-}

unzip3 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v (a, b, c))
       => Vector v n (a, b, c) -> (Vector v n a, Vector v n b, Vector v n c)
unzip3 :: Vector v n (a, b, c) -> (Vector v n a, Vector v n b, Vector v n c)
unzip3 Vector v n (a, b, c)
xs = (((a, b, c) -> a) -> Vector v n (a, b, c) -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
a, b
_, c
_) -> a
a) Vector v n (a, b, c)
xs,
             ((a, b, c) -> b) -> Vector v n (a, b, c) -> Vector v n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
b, c
_) -> b
b) Vector v n (a, b, c)
xs,
             ((a, b, c) -> c) -> Vector v n (a, b, c) -> Vector v n c
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
c) -> c
c) Vector v n (a, b, c)
xs)
{-# inline unzip3 #-}

unzip4 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v d,
           VG.Vector v (a, b, c, d))
       => Vector v n (a, b, c, d) -> (Vector v n a, Vector v n b, Vector v n c, Vector v n d)
unzip4 :: Vector v n (a, b, c, d)
-> (Vector v n a, Vector v n b, Vector v n c, Vector v n d)
unzip4 Vector v n (a, b, c, d)
xs = (((a, b, c, d) -> a) -> Vector v n (a, b, c, d) -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
a, b
_, c
_, d
_) -> a
a) Vector v n (a, b, c, d)
xs,
             ((a, b, c, d) -> b) -> Vector v n (a, b, c, d) -> Vector v n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
b, c
_, d
_) -> b
b) Vector v n (a, b, c, d)
xs,
             ((a, b, c, d) -> c) -> Vector v n (a, b, c, d) -> Vector v n c
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
c, d
_) -> c
c) Vector v n (a, b, c, d)
xs,
             ((a, b, c, d) -> d) -> Vector v n (a, b, c, d) -> Vector v n d
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
d) -> d
d) Vector v n (a, b, c, d)
xs)
{-# inline unzip4 #-}

unzip5 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v d, VG.Vector v e,
           VG.Vector v (a, b, c, d, e))
       => Vector v n (a, b, c, d, e) -> (Vector v n a, Vector v n b, Vector v n c, Vector v n d, Vector v n e)
unzip5 :: Vector v n (a, b, c, d, e)
-> (Vector v n a, Vector v n b, Vector v n c, Vector v n d,
    Vector v n e)
unzip5 Vector v n (a, b, c, d, e)
xs = (((a, b, c, d, e) -> a)
-> Vector v n (a, b, c, d, e) -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
a, b
_, c
_, d
_, e
_) -> a
a) Vector v n (a, b, c, d, e)
xs,
             ((a, b, c, d, e) -> b)
-> Vector v n (a, b, c, d, e) -> Vector v n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
b, c
_, d
_, e
_) -> b
b) Vector v n (a, b, c, d, e)
xs,
             ((a, b, c, d, e) -> c)
-> Vector v n (a, b, c, d, e) -> Vector v n c
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
c, d
_, e
_) -> c
c) Vector v n (a, b, c, d, e)
xs,
             ((a, b, c, d, e) -> d)
-> Vector v n (a, b, c, d, e) -> Vector v n d
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
d, e
_) -> d
d) Vector v n (a, b, c, d, e)
xs,
             ((a, b, c, d, e) -> e)
-> Vector v n (a, b, c, d, e) -> Vector v n e
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
_, e
e) -> e
e) Vector v n (a, b, c, d, e)
xs)
{-# inline unzip5 #-}

unzip6 :: (VG.Vector v a, VG.Vector v b, VG.Vector v c, VG.Vector v d, VG.Vector v e,
           VG.Vector v f, VG.Vector v (a, b, c, d, e, f))
       => Vector v n (a, b, c, d, e, f) -> (Vector v n a, Vector v n b, Vector v n c, Vector v n d, Vector v n e, Vector v n f)
unzip6 :: Vector v n (a, b, c, d, e, f)
-> (Vector v n a, Vector v n b, Vector v n c, Vector v n d,
    Vector v n e, Vector v n f)
unzip6 Vector v n (a, b, c, d, e, f)
xs = (((a, b, c, d, e, f) -> a)
-> Vector v n (a, b, c, d, e, f) -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
a, b
_, c
_, d
_, e
_, f
_) -> a
a) Vector v n (a, b, c, d, e, f)
xs,
             ((a, b, c, d, e, f) -> b)
-> Vector v n (a, b, c, d, e, f) -> Vector v n b
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
b, c
_, d
_, e
_, f
_) -> b
b) Vector v n (a, b, c, d, e, f)
xs,
             ((a, b, c, d, e, f) -> c)
-> Vector v n (a, b, c, d, e, f) -> Vector v n c
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
c, d
_, e
_, f
_) -> c
c) Vector v n (a, b, c, d, e, f)
xs,
             ((a, b, c, d, e, f) -> d)
-> Vector v n (a, b, c, d, e, f) -> Vector v n d
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
d, e
_, f
_) -> d
d) Vector v n (a, b, c, d, e, f)
xs,
             ((a, b, c, d, e, f) -> e)
-> Vector v n (a, b, c, d, e, f) -> Vector v n e
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
_, e
e, f
_) -> e
e) Vector v n (a, b, c, d, e, f)
xs,
             ((a, b, c, d, e, f) -> f)
-> Vector v n (a, b, c, d, e, f) -> Vector v n f
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map (\(a
_, b
_, c
_, d
_, e
_, f
f) -> f
f) Vector v n (a, b, c, d, e, f)
xs)
{-# inline unzip6 #-}

--------------------------------------------------------------------------------
-- * Working with predicates
--------------------------------------------------------------------------------

--
-- ** Searching
--


infix 4 `elem`
-- | /O(n)/ Check if the vector contains an element.
elem :: (VG.Vector v a, Eq a) => a -> Vector v n a -> Bool
elem :: a -> Vector v n a -> Bool
elem a
x (Vector v a
v) = a -> v a -> Bool
forall (v :: Type -> Type) a.
(Vector v a, Eq a) =>
a -> v a -> Bool
VG.elem a
x v a
v
{-# inline elem #-}

infix 4 `notElem`
-- | /O(n)/ Check if the vector does not contain an element (inverse of 'elem').
notElem :: (VG.Vector v a, Eq a) => a -> Vector v n a -> Bool
notElem :: a -> Vector v n a -> Bool
notElem a
x (Vector v a
v) = a -> v a -> Bool
forall (v :: Type -> Type) a.
(Vector v a, Eq a) =>
a -> v a -> Bool
VG.notElem a
x v a
v
{-# inline notElem #-}

-- | /O(n)/ Yield 'Just' the first element matching the predicate or 'Nothing'
-- if no such element exists.
find :: VG.Vector v a => (a -> Bool) -> Vector v n a -> Maybe a
find :: (a -> Bool) -> Vector v n a -> Maybe a
find a -> Bool
f (Vector v a
v) = (a -> Bool) -> v a -> Maybe a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> Bool) -> v a -> Maybe a
VG.find a -> Bool
f v a
v
{-# inline find #-}

-- | /O(n)/ Yield 'Just' the index of the first element matching the predicate
-- or 'Nothing' if no such element exists.
findIndex :: VG.Vector v a => (a -> Bool) -> Vector v n a -> Maybe (Finite n)
findIndex :: (a -> Bool) -> Vector v n a -> Maybe (Finite n)
findIndex a -> Bool
f (Vector v a
v) = Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Finite n) -> Maybe Int -> Maybe (Finite n)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Bool) -> v a -> Maybe Int
forall (v :: Type -> Type) a.
Vector v a =>
(a -> Bool) -> v a -> Maybe Int
VG.findIndex a -> Bool
f v a
v
{-# inline findIndex #-}

-- | /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 :: (VG.Vector v a, Eq a) => a -> Vector v n a -> Maybe (Finite n)
elemIndex :: a -> Vector v n a -> Maybe (Finite n)
elemIndex a
x (Vector v a
v) = Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Finite n) -> Maybe Int -> Maybe (Finite n)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> v a -> Maybe Int
forall (v :: Type -> Type) a.
(Vector v a, Eq a) =>
a -> v a -> Maybe Int
VG.elemIndex a
x v a
v
{-# inline elemIndex #-}

--------------------------------------------------------------------------------
-- * Folding
--------------------------------------------------------------------------------

-- | /O(n)/ Left fold.
foldl :: VG.Vector v b => (a -> b -> a) -> a -> Vector v n b -> a
foldl :: (a -> b -> a) -> a -> Vector v n b -> a
foldl a -> b -> a
f a
z = (a -> b -> a) -> a -> v b -> a
forall (v :: Type -> Type) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
VG.foldl a -> b -> a
f a
z (v b -> a) -> (Vector v n b -> v b) -> Vector v n b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldl #-}

-- | /O(n)/ Left fold on non-empty vectors.
foldl1 :: VG.Vector v a => (a -> a -> a) -> Vector v (1+n) a -> a
foldl1 :: (a -> a -> a) -> Vector v (1 + n) a -> a
foldl1 a -> a -> a
f = (a -> a -> a) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> a
VG.foldl1 a -> a -> a
f (v a -> a)
-> (Vector v (1 + n) a -> v a) -> Vector v (1 + n) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (1 + n) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldl1 #-}

-- | /O(n)/ Left fold with strict accumulator.
foldl' :: VG.Vector v b => (a -> b -> a) -> a -> Vector v n b -> a
foldl' :: (a -> b -> a) -> a -> Vector v n b -> a
foldl' a -> b -> a
f a
z = (a -> b -> a) -> a -> v b -> a
forall (v :: Type -> Type) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
VG.foldl' a -> b -> a
f a
z (v b -> a) -> (Vector v n b -> v b) -> Vector v n b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldl' #-}

-- | /O(n)/ Left fold on non-empty vectors with strict accumulator.
foldl1' :: VG.Vector v a => (a -> a -> a) -> Vector v (1+n) a -> a
foldl1' :: (a -> a -> a) -> Vector v (1 + n) a -> a
foldl1' a -> a -> a
f = (a -> a -> a) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> a
VG.foldl1' a -> a -> a
f (v a -> a)
-> (Vector v (1 + n) a -> v a) -> Vector v (1 + n) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (1 + n) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldl1' #-}

-- | /O(n)/ Right fold.
foldr :: VG.Vector v a => (a -> b -> b) -> b -> Vector v n a -> b
foldr :: (a -> b -> b) -> b -> Vector v n a -> b
foldr a -> b -> b
f b
z = (a -> b -> b) -> b -> v a -> b
forall (v :: Type -> Type) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
VG.foldr a -> b -> b
f b
z (v a -> b) -> (Vector v n a -> v a) -> Vector v n a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldr #-}

-- | /O(n)/ Right fold on non-empty vectors.
foldr1 :: VG.Vector v a => (a -> a -> a) -> Vector v (n+1) a -> a
foldr1 :: (a -> a -> a) -> Vector v (n + 1) a -> a
foldr1 a -> a -> a
f = (a -> a -> a) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> a
VG.foldr1 a -> a -> a
f (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldr1 #-}

-- | /O(n)/ Right fold with a strict accumulator.
foldr' :: VG.Vector v a => (a -> b -> b) -> b -> Vector v n a -> b
foldr' :: (a -> b -> b) -> b -> Vector v n a -> b
foldr' a -> b -> b
f b
z = (a -> b -> b) -> b -> v a -> b
forall (v :: Type -> Type) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
VG.foldr' a -> b -> b
f b
z (v a -> b) -> (Vector v n a -> v a) -> Vector v n a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldr' #-}

-- | /O(n)/ Right fold on non-empty vectors with strict accumulator.
foldr1' :: VG.Vector v a => (a -> a -> a) -> Vector v (n+1) a -> a
foldr1' :: (a -> a -> a) -> Vector v (n + 1) a -> a
foldr1' a -> a -> a
f = (a -> a -> a) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> a
VG.foldr1' a -> a -> a
f (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldr1' #-}

-- | /O(n)/ Left fold (function applied to each element and its index).
ifoldl :: VG.Vector v b => (a -> Finite n -> b -> a) -> a -> Vector v n b -> a
ifoldl :: (a -> Finite n -> b -> a) -> a -> Vector v n b -> a
ifoldl a -> Finite n -> b -> a
f a
z = (a -> Int -> b -> a) -> a -> v b -> a
forall (v :: Type -> Type) b a.
Vector v b =>
(a -> Int -> b -> a) -> a -> v b -> a
VG.ifoldl (\a
x -> a -> Finite n -> b -> a
f a
x (Finite n -> b -> a) -> (Int -> Finite n) -> Int -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) a
z (v b -> a) -> (Vector v n b -> v b) -> Vector v n b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldl #-}

-- | /O(n)/ Left fold with strict accumulator (function applied to each element
-- and its index).
ifoldl' :: VG.Vector v b => (a -> Finite n -> b -> a) -> a -> Vector v n b -> a
ifoldl' :: (a -> Finite n -> b -> a) -> a -> Vector v n b -> a
ifoldl' a -> Finite n -> b -> a
f a
z = (a -> Int -> b -> a) -> a -> v b -> a
forall (v :: Type -> Type) b a.
Vector v b =>
(a -> Int -> b -> a) -> a -> v b -> a
VG.ifoldl' (\a
x -> a -> Finite n -> b -> a
f a
x (Finite n -> b -> a) -> (Int -> Finite n) -> Int -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) a
z (v b -> a) -> (Vector v n b -> v b) -> Vector v n b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldl' #-}

-- | /O(n)/ Right fold (function applied to each element and its index).
ifoldr :: VG.Vector v a => (Finite n -> a -> b -> b) -> b -> Vector v n a -> b
ifoldr :: (Finite n -> a -> b -> b) -> b -> Vector v n a -> b
ifoldr Finite n -> a -> b -> b
f b
z = (Int -> a -> b -> b) -> b -> v a -> b
forall (v :: Type -> Type) a b.
Vector v a =>
(Int -> a -> b -> b) -> b -> v a -> b
VG.ifoldr (Finite n -> a -> b -> b
f (Finite n -> a -> b -> b)
-> (Int -> Finite n) -> Int -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) b
z (v a -> b) -> (Vector v n a -> v a) -> Vector v n a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldr #-}

-- | /O(n)/ Right fold with strict accumulator (function applied to each
-- element and its index).
ifoldr' :: VG.Vector v a => (Finite n -> a -> b -> b) -> b -> Vector v n a -> b
ifoldr' :: (Finite n -> a -> b -> b) -> b -> Vector v n a -> b
ifoldr' Finite n -> a -> b -> b
f b
z = (Int -> a -> b -> b) -> b -> v a -> b
forall (v :: Type -> Type) a b.
Vector v a =>
(Int -> a -> b -> b) -> b -> v a -> b
VG.ifoldr' (Finite n -> a -> b -> b
f (Finite n -> a -> b -> b)
-> (Int -> Finite n) -> Int -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) b
z (v a -> b) -> (Vector v n a -> v a) -> Vector v n a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldr' #-}

-- ** Specialised folds

-- | /O(n)/ Check if all elements satisfy the predicate.
all :: VG.Vector v a => (a -> Bool) -> Vector v n a -> Bool
all :: (a -> Bool) -> Vector v n a -> Bool
all a -> Bool
f = (a -> Bool) -> v a -> Bool
forall (v :: Type -> Type) a.
Vector v a =>
(a -> Bool) -> v a -> Bool
VG.all a -> Bool
f (v a -> Bool) -> (Vector v n a -> v a) -> Vector v n a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline all #-}

-- | /O(n)/ Check if any element satisfies the predicate.
any :: VG.Vector v a => (a -> Bool) -> Vector v n a -> Bool
any :: (a -> Bool) -> Vector v n a -> Bool
any a -> Bool
f = (a -> Bool) -> v a -> Bool
forall (v :: Type -> Type) a.
Vector v a =>
(a -> Bool) -> v a -> Bool
VG.any a -> Bool
f (v a -> Bool) -> (Vector v n a -> v a) -> Vector v n a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline any #-}

-- | /O(n)/ Check if all elements are 'True'
and :: VG.Vector v Bool => Vector v n Bool -> Bool
and :: Vector v n Bool -> Bool
and = v Bool -> Bool
forall (v :: Type -> Type). Vector v Bool => v Bool -> Bool
VG.and (v Bool -> Bool)
-> (Vector v n Bool -> v Bool) -> Vector v n Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n Bool -> v Bool
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline and #-}

-- | /O(n)/ Check if any element is 'True'
or :: VG.Vector v Bool => Vector v n Bool -> Bool
or :: Vector v n Bool -> Bool
or = v Bool -> Bool
forall (v :: Type -> Type). Vector v Bool => v Bool -> Bool
VG.or (v Bool -> Bool)
-> (Vector v n Bool -> v Bool) -> Vector v n Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n Bool -> v Bool
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline or #-}

-- | /O(n)/ Compute the sum of the elements.
sum :: (VG.Vector v a, Num a) => Vector v n a -> a
sum :: Vector v n a -> a
sum = v a -> a
forall (v :: Type -> Type) a. (Vector v a, Num a) => v a -> a
VG.sum (v a -> a) -> (Vector v n a -> v a) -> Vector v n a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline sum #-}

-- | /O(n)/ Compute the product of the elements.
product :: (VG.Vector v a, Num a) => Vector v n a -> a
product :: Vector v n a -> a
product = v a -> a
forall (v :: Type -> Type) a. (Vector v a, Num a) => v a -> a
VG.product (v a -> a) -> (Vector v n a -> v a) -> Vector v n a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline product #-}

-- | /O(n)/ Yield the maximum element of the non-empty vector.
maximum :: (VG.Vector v a, Ord a) => Vector v (n+1) a -> a
maximum :: Vector v (n + 1) a -> a
maximum = v a -> a
forall (v :: Type -> Type) a. (Vector v a, Ord a) => v a -> a
VG.maximum (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline maximum #-}

-- | /O(n)/ Yield the maximum element of the non-empty vector according to the
-- given comparison function.
maximumBy :: VG.Vector v a
          => (a -> a -> Ordering) -> Vector v (n+1) a -> a
maximumBy :: (a -> a -> Ordering) -> Vector v (n + 1) a -> a
maximumBy a -> a -> Ordering
cmpr = (a -> a -> Ordering) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> a
VG.maximumBy a -> a -> Ordering
cmpr (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline maximumBy #-}

-- | /O(n)/ Yield the minimum element of the non-empty vector.
minimum :: (VG.Vector v a, Ord a) => Vector v (n+1) a -> a
minimum :: Vector v (n + 1) a -> a
minimum = v a -> a
forall (v :: Type -> Type) a. (Vector v a, Ord a) => v a -> a
VG.minimum (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline minimum #-}

-- | /O(n)/ Yield the minimum element of the non-empty vector according to the
-- given comparison function.
minimumBy :: VG.Vector v a
          => (a -> a -> Ordering) -> Vector v (n+1) a -> a
minimumBy :: (a -> a -> Ordering) -> Vector v (n + 1) a -> a
minimumBy a -> a -> Ordering
cmpr = (a -> a -> Ordering) -> v a -> a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> a
VG.minimumBy a -> a -> Ordering
cmpr (v a -> a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline minimumBy #-}

-- | /O(n)/ Yield the index of the maximum element of the non-empty vector.
maxIndex :: (VG.Vector v a, Ord a) => Vector v (n+1) a -> Finite (n+1)
maxIndex :: Vector v (n + 1) a -> Finite (n + 1)
maxIndex = Integer -> Finite (n + 1)
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite (n + 1))
-> (Vector v (n + 1) a -> Integer)
-> Vector v (n + 1) a
-> Finite (n + 1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer)
-> (Vector v (n + 1) a -> Int) -> Vector v (n + 1) a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> Int
forall (v :: Type -> Type) a. (Vector v a, Ord a) => v a -> Int
VG.maxIndex (v a -> Int)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline maxIndex #-}

-- | /O(n)/ Yield the index of the maximum element of the non-empty vector
-- according to the given comparison function.
maxIndexBy :: VG.Vector v a
           => (a -> a -> Ordering) -> Vector v (n+1) a -> Finite (n + 1)
maxIndexBy :: (a -> a -> Ordering) -> Vector v (n + 1) a -> Finite (n + 1)
maxIndexBy a -> a -> Ordering
cmpr = Integer -> Finite (n + 1)
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite (n + 1))
-> (Vector v (n + 1) a -> Integer)
-> Vector v (n + 1) a
-> Finite (n + 1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer)
-> (Vector v (n + 1) a -> Int) -> Vector v (n + 1) a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> v a -> Int
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> Int
VG.maxIndexBy a -> a -> Ordering
cmpr (v a -> Int)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline maxIndexBy #-}

-- | /O(n)/ Yield the index of the minimum element of the non-empty vector.
minIndex :: (VG.Vector v a, Ord a) => Vector v (n+1) a -> Finite (n+1)
minIndex :: Vector v (n + 1) a -> Finite (n + 1)
minIndex = Integer -> Finite (n + 1)
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite (n + 1))
-> (Vector v (n + 1) a -> Integer)
-> Vector v (n + 1) a
-> Finite (n + 1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer)
-> (Vector v (n + 1) a -> Int) -> Vector v (n + 1) a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> Int
forall (v :: Type -> Type) a. (Vector v a, Ord a) => v a -> Int
VG.minIndex (v a -> Int)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline minIndex #-}

-- | /O(n)/ Yield the index of the minimum element of the non-empty vector
-- according to the given comparison function.
minIndexBy :: VG.Vector v a
           => (a -> a -> Ordering) -> Vector v (n+1) a -> Finite (n + 1)
minIndexBy :: (a -> a -> Ordering) -> Vector v (n + 1) a -> Finite (n + 1)
minIndexBy a -> a -> Ordering
cmpr = Integer -> Finite (n + 1)
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite (n + 1))
-> (Vector v (n + 1) a -> Integer)
-> Vector v (n + 1) a
-> Finite (n + 1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer)
-> (Vector v (n + 1) a -> Int) -> Vector v (n + 1) a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> v a -> Int
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> Ordering) -> v a -> Int
VG.minIndexBy a -> a -> Ordering
cmpr (v a -> Int)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline minIndexBy #-}

-- ** Monadic folds

-- | /O(n)/ Monadic fold.
foldM :: (Monad m, VG.Vector v b) => (a -> b -> m a) -> a -> Vector v n b -> m a
foldM :: (a -> b -> m a) -> a -> Vector v n b -> m a
foldM a -> b -> m a
m a
z = (a -> b -> m a) -> a -> v b -> m a
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
VG.foldM a -> b -> m a
m a
z (v b -> m a) -> (Vector v n b -> v b) -> Vector v n b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldM #-}

-- | /O(n)/ Monadic fold (action applied to each element and its index).
ifoldM :: (Monad m, VG.Vector v b) => (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m a
ifoldM :: (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m a
ifoldM a -> Finite n -> b -> m a
m a
z = (a -> Int -> b -> m a) -> a -> v b -> m a
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> Int -> b -> m a) -> a -> v b -> m a
VG.ifoldM (\a
x -> a -> Finite n -> b -> m a
m a
x (Finite n -> b -> m a) -> (Int -> Finite n) -> Int -> b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) a
z (v b -> m a) -> (Vector v n b -> v b) -> Vector v n b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldM #-}

-- | /O(n)/ Monadic fold over non-empty vectors.
fold1M :: (Monad m, VG.Vector v a)
       => (a -> a -> m a) -> Vector v (1+n) a -> m a
fold1M :: (a -> a -> m a) -> Vector v (1 + n) a -> m a
fold1M a -> a -> m a
m = (a -> a -> m a) -> v a -> m a
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m a
VG.fold1M a -> a -> m a
m (v a -> m a)
-> (Vector v (1 + n) a -> v a) -> Vector v (1 + n) a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (1 + n) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline fold1M #-}

-- | /O(n)/ Monadic fold with strict accumulator.
foldM' :: (Monad m, VG.Vector v b) => (a -> b -> m a) -> a -> Vector v n b -> m a
foldM' :: (a -> b -> m a) -> a -> Vector v n b -> m a
foldM' a -> b -> m a
m a
z = (a -> b -> m a) -> a -> v b -> m a
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
VG.foldM' a -> b -> m a
m a
z (v b -> m a) -> (Vector v n b -> v b) -> Vector v n b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldM' #-}

-- | /O(n)/ Monadic fold with strict accumulator (action applied to each
-- element and its index).
ifoldM' :: (Monad m, VG.Vector v b)
        => (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m a
ifoldM' :: (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m a
ifoldM' a -> Finite n -> b -> m a
m a
z = (a -> Int -> b -> m a) -> a -> v b -> m a
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> Int -> b -> m a) -> a -> v b -> m a
VG.ifoldM' (\a
x -> a -> Finite n -> b -> m a
m a
x (Finite n -> b -> m a) -> (Int -> Finite n) -> Int -> b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) a
z (v b -> m a) -> (Vector v n b -> v b) -> Vector v n b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldM' #-}

-- | /O(n)/ Monadic fold over non-empty vectors with strict accumulator.
fold1M' :: (Monad m, VG.Vector v a)
        => (a -> a -> m a) -> Vector v (n+1) a -> m a
fold1M' :: (a -> a -> m a) -> Vector v (n + 1) a -> m a
fold1M' a -> a -> m a
m = (a -> a -> m a) -> v a -> m a
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m a
VG.fold1M' a -> a -> m a
m (v a -> m a)
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline fold1M' #-}

-- | /O(n)/ Monadic fold that discards the result.
foldM_ :: (Monad m, VG.Vector v b)
       => (a -> b -> m a) -> a -> Vector v n b -> m ()
foldM_ :: (a -> b -> m a) -> a -> Vector v n b -> m ()
foldM_ a -> b -> m a
m a
z = (a -> b -> m a) -> a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m ()
VG.foldM_ a -> b -> m a
m a
z (v b -> m ()) -> (Vector v n b -> v b) -> Vector v n b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldM_ #-}

-- | /O(n)/ Monadic fold that discards the result (action applied to
-- each element and its index).
ifoldM_ :: (Monad m, VG.Vector v b)
        => (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m ()
ifoldM_ :: (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m ()
ifoldM_ a -> Finite n -> b -> m a
m a
z = (a -> Int -> b -> m a) -> a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> Int -> b -> m a) -> a -> v b -> m ()
VG.ifoldM_ (\a
x -> a -> Finite n -> b -> m a
m a
x (Finite n -> b -> m a) -> (Int -> Finite n) -> Int -> b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral)  a
z (v b -> m ()) -> (Vector v n b -> v b) -> Vector v n b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldM_ #-}

-- | /O(n)/ Monadic fold over non-empty vectors that discards the result.
fold1M_ :: (Monad m, VG.Vector v a)
        => (a -> a -> m a) -> Vector v (n+1) a -> m ()
fold1M_ :: (a -> a -> m a) -> Vector v (n + 1) a -> m ()
fold1M_ a -> a -> m a
m = (a -> a -> m a) -> v a -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m ()
VG.fold1M_ a -> a -> m a
m (v a -> m ())
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline fold1M_ #-}

-- | /O(n)/ Monadic fold with strict accumulator that discards the result.
foldM'_ :: (Monad m, VG.Vector v b)
        => (a -> b -> m a) -> a -> Vector v n b -> m ()
foldM'_ :: (a -> b -> m a) -> a -> Vector v n b -> m ()
foldM'_ a -> b -> m a
m a
z = (a -> b -> m a) -> a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m ()
VG.foldM'_ a -> b -> m a
m a
z (v b -> m ()) -> (Vector v n b -> v b) -> Vector v n b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline foldM'_ #-}

-- | /O(n)/ Monadic fold with strict accumulator that discards the result
-- (action applied to each element and its index).
ifoldM'_ :: (Monad m, VG.Vector v b)
         => (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m ()
ifoldM'_ :: (a -> Finite n -> b -> m a) -> a -> Vector v n b -> m ()
ifoldM'_ a -> Finite n -> b -> m a
m a
z = (a -> Int -> b -> m a) -> a -> v b -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) b a.
(Monad m, Vector v b) =>
(a -> Int -> b -> m a) -> a -> v b -> m ()
VG.ifoldM'_ (\a
x -> a -> Finite n -> b -> m a
m a
x (Finite n -> b -> m a) -> (Int -> Finite n) -> Int -> b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Finite n
forall (n :: Nat). Integer -> Finite n
Finite (Integer -> Finite n) -> (Int -> Integer) -> Int -> Finite n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) a
z (v b -> m ()) -> (Vector v n b -> v b) -> Vector v n b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n b -> v b
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline ifoldM'_ #-}

-- | /O(n)/ Monad fold over non-empty vectors with strict accumulator
-- that discards the result.
fold1M'_ :: (Monad m, VG.Vector v a)
         => (a -> a -> m a) -> Vector v (n+1) a -> m ()
fold1M'_ :: (a -> a -> m a) -> Vector v (n + 1) a -> m ()
fold1M'_ a -> a -> m a
m = (a -> a -> m a) -> v a -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a) =>
(a -> a -> m a) -> v a -> m ()
VG.fold1M'_ a -> a -> m a
m (v a -> m ())
-> (Vector v (n + 1) a -> v a) -> Vector v (n + 1) a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v (n + 1) a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline fold1M'_ #-}

-- ** Monadic sequencing

-- | Evaluate each action and collect the results.
sequence :: (Monad m, VG.Vector v a, VG.Vector v (m a))
         => Vector v n (m a) -> m (Vector v n a)
sequence :: Vector v n (m a) -> m (Vector v n a)
sequence (Vector v (m a)
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> m (v a) -> m (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> v (m a) -> m (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v a, Vector v (m a)) =>
v (m a) -> m (v a)
VG.sequence v (m a)
v
{-# inline sequence #-}

-- | Evaluate each action and discard the results.
sequence_ :: (Monad m, VG.Vector v (m a)) => Vector v n (m a) -> m ()
sequence_ :: Vector v n (m a) -> m ()
sequence_ (Vector v (m a)
v) = v (m a) -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(Monad m, Vector v (m a)) =>
v (m a) -> m ()
VG.sequence_ v (m a)
v
{-# inline sequence_ #-}

--------------------------------------------------------------------------------
-- * Prefix sums (scans)
--------------------------------------------------------------------------------

-- | /O(n)/ Prescan
--
-- @
-- prescanl f z = 'init' . 'scanl' f z
-- @
--
-- Example: @prescanl (+) 0 \<1,2,3,4\> = \<0,1,3,6\>@
--
prescanl :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v n a
prescanl :: (a -> b -> a) -> a -> Vector v n b -> Vector v n a
prescanl a -> b -> a
f a
z = (v b -> v a) -> Vector v n b -> Vector v n a
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.prescanl a -> b -> a
f a
z )
{-# inline prescanl #-}

-- | /O(n)/ Prescan with strict accumulator.
prescanl' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v n a
prescanl' :: (a -> b -> a) -> a -> Vector v n b -> Vector v n a
prescanl' a -> b -> a
f a
z = (v b -> v a) -> Vector v n b -> Vector v n a
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.prescanl' a -> b -> a
f a
z )
{-# inline prescanl' #-}

-- | /O(n)/ Scan
postscanl :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v n a
postscanl :: (a -> b -> a) -> a -> Vector v n b -> Vector v n a
postscanl a -> b -> a
f a
z = (v b -> v a) -> Vector v n b -> Vector v n a
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.postscanl a -> b -> a
f a
z )
{-# inline postscanl #-}

-- | /O(n)/ Scan with strict accumulator.
postscanl' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v n a
postscanl' :: (a -> b -> a) -> a -> Vector v n b -> Vector v n a
postscanl' a -> b -> a
f a
z = (v b -> v a) -> Vector v n b -> Vector v n a
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.postscanl' a -> b -> a
f a
z )
{-# inline postscanl' #-}

-- | /O(n)/ Haskell-style scan.
scanl :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v (1+n) a
scanl :: (a -> b -> a) -> a -> Vector v n b -> Vector v (1 + n) a
scanl a -> b -> a
f a
z (Vector v b
v) = v a -> Vector v (1 + n) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.scanl a -> b -> a
f a
z v b
v)
{-# inline scanl #-}

-- | /O(n)/ Haskell-style scan with strict accumulator.
scanl' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> a) -> a -> Vector v n b -> Vector v (1+n) a
scanl' :: (a -> b -> a) -> a -> Vector v n b -> Vector v (1 + n) a
scanl' a -> b -> a
f a
z (Vector v b
v) = v a -> Vector v (1 + n) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> a) -> a -> v b -> v a
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
VG.scanl' a -> b -> a
f a
z v b
v)
{-# inline scanl' #-}

-- | /O(n)/ Scan over a non-empty vector.
scanl1 :: VG.Vector v a => (a -> a -> a) -> Vector v (1+n) a -> Vector v (2+n) a
scanl1 :: (a -> a -> a) -> Vector v (1 + n) a -> Vector v (2 + n) a
scanl1 a -> a -> a
f (Vector v a
v) = v a -> Vector v (2 + n) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> a -> a) -> v a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> v a
VG.scanl1 a -> a -> a
f v a
v)
{-# inline scanl1 #-}

-- | /O(n)/ Scan over a non-empty vector with a strict accumulator.
scanl1' :: VG.Vector v a => (a -> a -> a) -> Vector v (1+n) a -> Vector v (2+n) a
scanl1' :: (a -> a -> a) -> Vector v (1 + n) a -> Vector v (2 + n) a
scanl1' a -> a -> a
f (Vector v a
v) = v a -> Vector v (2 + n) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> a -> a) -> v a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> v a
VG.scanl1' a -> a -> a
f v a
v)
{-# inline scanl1' #-}

-- | /O(n)/ Right-to-left prescan.
prescanr :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v n b
prescanr :: (a -> b -> b) -> b -> Vector v n a -> Vector v n b
prescanr a -> b -> b
f b
z = (v a -> v b) -> Vector v n a -> Vector v n b
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.prescanr a -> b -> b
f b
z )
{-# inline prescanr #-}

-- | /O(n)/ Right-to-left prescan with strict accumulator.
prescanr' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v n b
prescanr' :: (a -> b -> b) -> b -> Vector v n a -> Vector v n b
prescanr' a -> b -> b
f b
z = (v a -> v b) -> Vector v n a -> Vector v n b
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.prescanr' a -> b -> b
f b
z )
{-# inline prescanr' #-}

-- | /O(n)/ Right-to-left scan.
postscanr :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v n b
postscanr :: (a -> b -> b) -> b -> Vector v n a -> Vector v n b
postscanr a -> b -> b
f b
z = (v a -> v b) -> Vector v n a -> Vector v n b
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.postscanr a -> b -> b
f b
z )
{-# inline postscanr #-}

-- | /O(n)/ Right-to-left scan with strict accumulator.
postscanr' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v n b
postscanr' :: (a -> b -> b) -> b -> Vector v n a -> Vector v n b
postscanr' a -> b -> b
f b
z = (v a -> v b) -> Vector v n a -> Vector v n b
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.postscanr' a -> b -> b
f b
z )
{-# inline postscanr' #-}

-- | /O(n)/ Right-to-left Haskell-style scan.
scanr :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v (n+1) b
scanr :: (a -> b -> b) -> b -> Vector v n a -> Vector v (n + 1) b
scanr a -> b -> b
f b
z (Vector v a
v) = v b -> Vector v (n + 1) b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.scanr a -> b -> b
f b
z v a
v)
{-# inline scanr #-}

-- | /O(n)/ Right-to-left Haskell-style scan with strict accumulator.
scanr' :: (VG.Vector v a, VG.Vector v b) => (a -> b -> b) -> b -> Vector v n a -> Vector v (n+1) b
scanr' :: (a -> b -> b) -> b -> Vector v n a -> Vector v (n + 1) b
scanr' a -> b -> b
f b
z (Vector v a
v) = v b -> Vector v (n + 1) b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> b -> b) -> b -> v a -> v b
forall (v :: Type -> Type) a b.
(Vector v a, Vector v b) =>
(a -> b -> b) -> b -> v a -> v b
VG.scanr' a -> b -> b
f b
z v a
v)
{-# inline scanr' #-}

-- | /O(n)/ Right-to-left scan over a non-empty vector.
scanr1 :: VG.Vector v a => (a -> a -> a) -> Vector v (n+1) a -> Vector v (n+2) a
scanr1 :: (a -> a -> a) -> Vector v (n + 1) a -> Vector v (n + 2) a
scanr1 a -> a -> a
f (Vector v a
v) = v a -> Vector v (n + 2) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> a -> a) -> v a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> v a
VG.scanr1 a -> a -> a
f v a
v)
{-# inline scanr1 #-}

-- | /O(n)/ Right-to-left scan over a non-empty vector with a strict
-- accumulator.
scanr1' :: VG.Vector v a => (a -> a -> a) -> Vector v (n+1) a -> Vector v (n+2) a
scanr1' :: (a -> a -> a) -> Vector v (n + 1) a -> Vector v (n + 2) a
scanr1' a -> a -> a
f (Vector v a
v) = v a -> Vector v (n + 2) a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector ((a -> a -> a) -> v a -> v a
forall (v :: Type -> Type) a.
Vector v a =>
(a -> a -> a) -> v a -> v a
VG.scanr1' a -> a -> a
f v a
v)
{-# inline scanr1' #-}


-- * Conversions

-- ** Lists

-- | /O(n)/ Convert a vector to a list.
toList :: VG.Vector v a => Vector v n a -> [a]
toList :: Vector v n a -> [a]
toList = v a -> [a]
forall (v :: Type -> Type) a. Vector v a => v a -> [a]
VG.toList (v a -> [a]) -> (Vector v n a -> v a) -> Vector v n a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized
{-# inline toList #-}

-- | /O(n)/ Convert a list to a vector.
fromList :: (VG.Vector v a, KnownNat n) => [a] -> Maybe (Vector v n a)
fromList :: [a] -> Maybe (Vector v n a)
fromList = v a -> Maybe (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) a.
(Vector v a, KnownNat n) =>
v a -> Maybe (Vector v n a)
toSized (v a -> Maybe (Vector v n a))
-> ([a] -> v a) -> [a] -> Maybe (Vector v n a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> v a
forall (v :: Type -> Type) a. Vector v a => [a] -> v a
VG.fromList
{-# inline fromList #-}

-- | /O(n)/ Convert the first @n@ elements of a list to a vector. The length of
-- the resultant vector is inferred from the type.
fromListN :: forall v n a. (VG.Vector v a, KnownNat n)
          => [a] -> Maybe (Vector v n a)
fromListN :: [a] -> Maybe (Vector v n a)
fromListN = v a -> Maybe (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) a.
(Vector v a, KnownNat n) =>
v a -> Maybe (Vector v n a)
toSized (v a -> Maybe (Vector v n a))
-> ([a] -> v a) -> [a] -> Maybe (Vector v n a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [a] -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> [a] -> v a
VG.fromListN Int
i
  where i :: Int
i = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline fromListN #-}

-- | /O(n)/ Convert the first @n@ elements of a list to a vector. The length of
-- the resultant vector is given explicitly as a 'Proxy' argument.
fromListN' :: forall v n a p. (VG.Vector v a, KnownNat n)
           => p n -> [a] -> Maybe (Vector v n a)
fromListN' :: p n -> [a] -> Maybe (Vector v n a)
fromListN' p n
_ = [a] -> Maybe (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) a.
(Vector v a, KnownNat n) =>
[a] -> Maybe (Vector v n a)
fromListN
{-# inline fromListN' #-}

-- | /O(n)/ Takes a list and returns a continuation providing a vector with
-- a size parameter corresponding to the length of the list.
--
-- Essentially converts a list into a vector with the proper size
-- parameter, determined at runtime.
--
-- See 'withSized'
withSizedList :: forall v a r. VG.Vector v a
              => [a] -> (forall n. KnownNat n => Vector v n a -> r) -> r
withSizedList :: [a] -> (forall (n :: Nat). KnownNat n => Vector v n a -> r) -> r
withSizedList [a]
xs = v a -> (forall (n :: Nat). KnownNat n => Vector v n a -> r) -> r
forall (v :: Type -> Type) a r.
Vector v a =>
v a -> (forall (n :: Nat). KnownNat n => Vector v n a -> r) -> r
withSized ([a] -> v a
forall (v :: Type -> Type) a. Vector v a => [a] -> v a
VG.fromList [a]
xs)

-- ** Different Vector types

-- | /O(n)/ Convert different vector types.
convert :: (VG.Vector v a, VG.Vector w a) => Vector v n a -> Vector w n a
convert :: Vector v n a -> Vector w n a
convert = (v a -> w a) -> Vector v n a -> Vector w n a
forall (v :: Type -> Type) a (w :: Type -> Type) b (n :: Nat).
(v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe v a -> w a
forall (v :: Type -> Type) a (w :: Type -> Type).
(Vector v a, Vector w a) =>
v a -> w a
VG.convert
{-# inline convert #-}

-- ** Mutable vectors

-- | /O(n)/ Yield an immutable copy of the mutable vector.
freeze :: (PrimMonad m, VG.Vector v a)
       => SVGM.MVector (VG.Mutable v) n (PrimState m) a
       -> m (Vector v n a)
freeze :: MVector (Mutable v) n (PrimState m) a -> m (Vector v n a)
freeze (MVector Mutable v (PrimState m) a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> m (v a) -> m (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable v (PrimState m) a -> m (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.freeze Mutable v (PrimState m) a
v

-- | /O(1)/ Unsafely convert a mutable vector to an immutable one withouy
-- copying. The mutable vector may not be used after this operation.
unsafeFreeze :: (PrimMonad m, VG.Vector v a)
             => SVGM.MVector (VG.Mutable v) n (PrimState m) a
             -> m (Vector v n a)
unsafeFreeze :: MVector (Mutable v) n (PrimState m) a -> m (Vector v n a)
unsafeFreeze (MVector Mutable v (PrimState m) a
v) = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> m (v a) -> m (Vector v n a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable v (PrimState m) a -> m (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze Mutable v (PrimState m) a
v

-- | /O(n)/ Yield a mutable copy of the immutable vector.
thaw :: (PrimMonad m, VG.Vector v a)
     => Vector v n a
     -> m (SVGM.MVector (VG.Mutable v) n (PrimState m) a)
thaw :: Vector v n a -> m (MVector (Mutable v) n (PrimState m) a)
thaw (Vector v a
v) = Mutable v (PrimState m) a -> MVector (Mutable v) n (PrimState m) a
forall (v :: Type -> Type -> Type) (n :: Nat) s a.
v s a -> MVector v n s a
MVector (Mutable v (PrimState m) a
 -> MVector (Mutable v) n (PrimState m) a)
-> m (Mutable v (PrimState m) a)
-> m (MVector (Mutable v) n (PrimState m) a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> v a -> m (Mutable v (PrimState m) a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
VG.thaw v a
v

-- | /O(n)/ Unsafely convert an immutable vector to a mutable one without
-- copying. The immutable vector may not be used after this operation.
unsafeThaw :: (PrimMonad m, VG.Vector v a)
           => Vector v n a
           -> m (SVGM.MVector (VG.Mutable v) n (PrimState m) a)
unsafeThaw :: Vector v n a -> m (MVector (Mutable v) n (PrimState m) a)
unsafeThaw (Vector v a
v) = Mutable v (PrimState m) a -> MVector (Mutable v) n (PrimState m) a
forall (v :: Type -> Type -> Type) (n :: Nat) s a.
v s a -> MVector v n s a
MVector (Mutable v (PrimState m) a
 -> MVector (Mutable v) n (PrimState m) a)
-> m (Mutable v (PrimState m) a)
-> m (MVector (Mutable v) n (PrimState m) a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> v a -> m (Mutable v (PrimState m) a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
VG.unsafeThaw v a
v

-- | /O(n)/ Copy an immutable vector into a mutable one.
copy :: (PrimMonad m, VG.Vector v a)
     => SVGM.MVector (VG.Mutable v) n (PrimState m) a
     -> Vector v n a
     -> m ()
copy :: MVector (Mutable v) n (PrimState m) a -> Vector v n a -> m ()
copy (MVector Mutable v (PrimState m) a
v) (Vector v a
u) = Mutable v (PrimState m) a -> v a -> m ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
VG.unsafeCopy Mutable v (PrimState m) a
v v a
u

-- ** Unsized vectors

-- | Convert a 'Data.Vector.Generic.Vector' into a
-- 'Data.Vector.Generic.Sized.Vector' if it has the correct size, otherwise
-- return Nothing.
toSized :: forall v n a. (VG.Vector v a, KnownNat n)
        => v a -> Maybe (Vector v n a)
toSized :: v a -> Maybe (Vector v n a)
toSized v a
v
  | Integer
n' Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v a -> Int
forall (v :: Type -> Type) a. Vector v a => v a -> Int
VG.length v a
v) = Vector v n a -> Maybe (Vector v n a)
forall a. a -> Maybe a
Just (v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
v)
  | Bool
otherwise                        = Maybe (Vector v n a)
forall a. Maybe a
Nothing
  where n' :: Integer
n' = Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)
{-# inline toSized #-}

-- | Takes a 'Data.Vector.Generic.Vector' and returns a continuation
-- providing a 'Data.Vector.Generic.Sized' with a size parameter @n@ that
-- is determined at runtime based on the length of the input vector.
--
-- Essentially converts a 'Data.Vector.Generic.Vector' into
-- a 'Data.Vector.Generic.Sized.Vector' with the correct size parameter
-- @n@.
withSized :: forall v a r. VG.Vector v a
          => v a
          -> (forall n. KnownNat n => Vector v n a -> r) -> r
withSized :: v a -> (forall (n :: Nat). KnownNat n => Vector v n a -> r) -> r
withSized v a
v forall (n :: Nat). KnownNat n => Vector v n a -> r
f = case Integer -> Maybe SomeNat
someNatVal (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v a -> Int
forall (v :: Type -> Type) a. Vector v a => v a -> Int
VG.length v a
v)) of
  Just (SomeNat (Proxy n
Proxy :: Proxy n)) -> Vector v n a -> r
forall (n :: Nat). KnownNat n => Vector v n a -> r
f (v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
v :: Vector v n a)
  Maybe SomeNat
Nothing -> String -> r
forall a. HasCallStack => String -> a
error String
"impossible: Vector has negative length"

fromSized :: Vector v n a -> v a
fromSized :: Vector v n a -> v a
fromSized (Vector v a
v) = v a
v
{-# inline fromSized #-}

-- | Apply a function on unsized vectors to a sized vector. The function must
-- preserve the size of the vector, this is not checked.
withVectorUnsafe :: (v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe :: (v a -> w b) -> Vector v n a -> Vector w n b
withVectorUnsafe v a -> w b
f (Vector v a
v) = w b -> Vector w n b
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> w b
f v a
v)
{-# inline withVectorUnsafe #-}

-- | Apply a function on two unsized vectors to sized vectors. The function must
-- preserve the size of the vectors, this is not checked.
zipVectorsUnsafe :: (u a -> v b -> w c) -> Vector u n a -> Vector v n b -> Vector w n c
zipVectorsUnsafe :: (u a -> v b -> w c) -> Vector u n a -> Vector v n b -> Vector w n c
zipVectorsUnsafe u a -> v b -> w c
f (Vector u a
u) (Vector v b
v) = w c -> Vector w n c
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (u a -> v b -> w c
f u a
u v b
v)
{-# inline zipVectorsUnsafe #-}

-- | Internal existential wrapper used for implementing 'SomeSized'
-- pattern synonym
data SV_ v a = forall n. KnownNat n => SV_ (Vector v n a)

-- | Pattern synonym that lets you treat an unsized vector as if it
-- "contained" a sized vector.  If you pattern match on an unsized vector,
-- its contents will be the /sized/ vector counterpart.
--
-- @
-- testFunc :: Unsized.Vector Int -> Int
-- testFunc ('SomeSized' v) =
--     'sum' ('zipWith' (+) v ('replicate' 1))
--         -- ^ here, v is `Sized.Vector n Int`, and we have
--                     `'KnownNat' n`
-- @
--
-- The @n@ type variable will be properly instantiated to whatever the
-- length of the vector is, and you will also have a @'KnownNat' n@
-- instance available.  You can get @n@ in scope by turning on
-- ScopedTypeVariables and matching on @'SomeSized' (v :: Sized.Vector
-- n Int)@.
--
-- Without this, you would otherwise have to use 'withSized' to do the same
-- thing:
--
-- @
-- testFunc :: Unsized.Vector Int -> Int
-- testFunc u = 'withSized' u $ \\v ->
--     'sum' ('zipWith' (+) v ('replicate' 1))
-- @
--
-- Remember that the type of final result of your function (the @Int@,
-- here) must /not/ depend on @n@.  However, the types of the intermediate
-- values are allowed to depend on @n@.
--
-- This is /especially/ useful in do blocks, where you can pattern match on
-- the unsized results of actions, to use the sized vector in the rest of
-- the do block.  You also get a @'KnownNat' n@ constraint for the
-- remainder of the do block.
--
-- @
-- -- If you had:
-- getAVector :: IO (Unsized.Vector Int)
--
-- main :: IO ()
-- main = do
--     SomeSized v <- getAVector -- v is `Sized.Vector n Int`
--     print v
--
--     -- alternatively, get n in scope
--     SomeSized (v2 :: Sized.Vector n Int) <- getAVector
--     print v2
-- @
--
-- Remember that the final type of the result of the do block ('()', here)
-- must not depend on @n@.  However, the
--
-- Also useful in ghci, where you can pattern match to get sized vectors
-- from unsized vectors.
--
-- @
-- ghci> SomeSized v <- pure (myUnsizedVector :: Unsized.Vector Int)
--              -- ^ v is `Sized.Vector n Int`
-- @
--
-- This enables interactive exploration with sized vectors in ghci, and is
-- useful for using with other libraries and functions that expect sized
-- vectors in an interactive setting.
--
-- (Note that as of GHC 8.6, you cannot get the @n@ in scope in your ghci
-- session using ScopedTypeVariables, like you can with do blocks)
--
-- You can also use this as a constructor, to take a sized vector and
-- "hide" the size, to produce an unsized vector:
--
-- @
-- SomeSized :: Sized.Vector n a -> Unsized.Vector a
-- @
--
-- Note that due to quirks in GHC pattern synonym completeness checking,
-- you will get incomplete pattern matches if you use this polymorphically
-- over different vector types, or you use any vector type other than the
-- three supported by this library (normal, storable, unboxed).
pattern SomeSized
    :: VG.Vector v a
    => forall n. KnownNat n
    => Vector v n a
    -> v a
pattern $bSomeSized :: Vector v n a -> v a
$mSomeSized :: forall r (v :: Type -> Type) a.
Vector v a =>
v a
-> (forall (n :: Nat). KnownNat n => Vector v n a -> r)
-> (Void# -> r)
-> r
SomeSized v <- ((`withSized` SV_) -> SV_ v)
  where
    SomeSized Vector v n a
v = Vector v n a -> v a
forall (v :: Type -> Type) (n :: Nat) a. Vector v n a -> v a
fromSized Vector v n a
v
{-# complete SomeSized :: Boxed.Vector    #-}
{-# complete SomeSized :: Unboxed.Vector  #-}
{-# complete SomeSized :: Storable.Vector #-}

instance (VG.Vector v a, Num a, KnownNat n) => Num (Vector v n a) where
    + :: Vector v n a -> Vector v n a -> Vector v n a
(+)         = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> a -> a
forall a. Num a => a -> a -> a
(+)
    (-)         = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith (-)
    * :: Vector v n a -> Vector v n a -> Vector v n a
(*)         = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> a -> a
forall a. Num a => a -> a -> a
(*)
    negate :: Vector v n a -> Vector v n a
negate      = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Num a => a -> a
negate
    abs :: Vector v n a -> Vector v n a
abs         = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Num a => a -> a
abs
    signum :: Vector v n a -> Vector v n a
signum      = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Num a => a -> a
signum
    fromInteger :: Integer -> Vector v n a
fromInteger = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate (a -> Vector v n a) -> (Integer -> a) -> Integer -> Vector v n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger

instance (VG.Vector v a, Fractional a, KnownNat n) => Fractional (Vector v n a) where
    / :: Vector v n a -> Vector v n a -> Vector v n a
(/)          = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
    recip :: Vector v n a -> Vector v n a
recip        = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Fractional a => a -> a
recip
    fromRational :: Rational -> Vector v n a
fromRational = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate (a -> Vector v n a) -> (Rational -> a) -> Rational -> Vector v n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational

instance (VG.Vector v a, Floating a, KnownNat n) => Floating (Vector v n a) where
    pi :: Vector v n a
pi      = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate a
forall a. Floating a => a
pi
    exp :: Vector v n a -> Vector v n a
exp     = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
exp
    log :: Vector v n a -> Vector v n a
log     = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
log
    sqrt :: Vector v n a -> Vector v n a
sqrt    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
sqrt
    ** :: Vector v n a -> Vector v n a -> Vector v n a
(**)    = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> a -> a
forall a. Floating a => a -> a -> a
(**)
    logBase :: Vector v n a -> Vector v n a -> Vector v n a
logBase = (a -> a -> a) -> Vector v n a -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b c (n :: Nat).
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> Vector v n a -> Vector v n b -> Vector v n c
zipWith a -> a -> a
forall a. Floating a => a -> a -> a
logBase
    sin :: Vector v n a -> Vector v n a
sin     = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
sin
    cos :: Vector v n a -> Vector v n a
cos     = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
cos
    tan :: Vector v n a -> Vector v n a
tan     = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
tan
    asin :: Vector v n a -> Vector v n a
asin    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
asin
    acos :: Vector v n a -> Vector v n a
acos    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
acos
    atan :: Vector v n a -> Vector v n a
atan    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
atan
    sinh :: Vector v n a -> Vector v n a
sinh    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
sinh
    cosh :: Vector v n a -> Vector v n a
cosh    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
cosh
    tanh :: Vector v n a -> Vector v n a
tanh    = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
tanh
    asinh :: Vector v n a -> Vector v n a
asinh   = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
asinh
    acosh :: Vector v n a -> Vector v n a
acosh   = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
acosh
    atanh :: Vector v n a -> Vector v n a
atanh   = (a -> a) -> Vector v n a -> Vector v n a
forall (v :: Type -> Type) a b (n :: Nat).
(Vector v a, Vector v b) =>
(a -> b) -> Vector v n a -> Vector v n b
map a -> a
forall a. Floating a => a -> a
atanh

instance (VG.Vector v a, Binary a, KnownNat n) => Binary (Vector v n a) where
  get :: Get (Vector v n a)
get = Get a -> Get (Vector v n a)
forall (v :: Type -> Type) (n :: Nat) (m :: Type -> Type) a.
(KnownNat n, Vector v a, Monad m) =>
m a -> m (Vector v n a)
replicateM Get a
forall t. Binary t => Get t
Data.Binary.get
  put :: Vector v n a -> Put
put = (a -> Put) -> Vector v n a -> Put
forall (m :: Type -> Type) (v :: Type -> Type) a b (n :: Nat).
(Monad m, Vector v a) =>
(a -> m b) -> Vector v n a -> m ()
mapM_ a -> Put
forall t. Binary t => t -> Put
put

-- | Only usable if @v a@ is itself an instance of 'Bits', like in the case
-- with the bitvec library @Bit@ type for unboxed vectors.
instance (VG.Vector v a, Bits (v a), Bits a, KnownNat n) => Bits (Vector v n a) where
    .&. :: Vector v n a -> Vector v n a -> Vector v n a
(.&.) = (v a -> v a -> v a) -> Vector v n a -> Vector v n a -> Vector v n a
coerce (Bits (v a) => v a -> v a -> v a
forall a. Bits a => a -> a -> a
(.&.) @(v a))
    .|. :: Vector v n a -> Vector v n a -> Vector v n a
(.|.) = (v a -> v a -> v a) -> Vector v n a -> Vector v n a -> Vector v n a
coerce (Bits (v a) => v a -> v a -> v a
forall a. Bits a => a -> a -> a
(.|.) @(v a))
    xor :: Vector v n a -> Vector v n a -> Vector v n a
xor   = (v a -> v a -> v a) -> Vector v n a -> Vector v n a -> Vector v n a
coerce (Bits (v a) => v a -> v a -> v a
forall a. Bits a => a -> a -> a
xor   @(v a))
    complement :: Vector v n a -> Vector v n a
complement = (v a -> v a) -> Vector v n a -> Vector v n a
coerce (Bits (v a) => v a -> v a
forall a. Bits a => a -> a
complement @(v a))
    rotate :: Vector v n a -> Int -> Vector v n a
rotate = (v a -> Int -> v a) -> Vector v n a -> Int -> Vector v n a
coerce (Bits (v a) => v a -> Int -> v a
forall a. Bits a => a -> Int -> a
rotate @(v a))
    rotateL :: Vector v n a -> Int -> Vector v n a
rotateL = (v a -> Int -> v a) -> Vector v n a -> Int -> Vector v n a
coerce (Bits (v a) => v a -> Int -> v a
forall a. Bits a => a -> Int -> a
rotateL @(v a))
    rotateR :: Vector v n a -> Int -> Vector v n a
rotateR = (v a -> Int -> v a) -> Vector v n a -> Int -> Vector v n a
coerce (Bits (v a) => v a -> Int -> v a
forall a. Bits a => a -> Int -> a
rotateR @(v a))
    bitSize :: Vector v n a -> Int
bitSize Vector v n a
x = case Vector v n a -> Maybe Int
forall a. Bits a => a -> Maybe Int
bitSizeMaybe Vector v n a
x of
      Maybe Int
Nothing -> String -> Int
forall a. HasCallStack => String -> a
error String
"Vector v n a: bitSize"
      Just Int
c  -> Int
c
    bitSizeMaybe :: Vector v n a -> Maybe Int
bitSizeMaybe Vector v n a
_ = (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy @n))) (Int -> Int) -> Maybe Int -> Maybe Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe Int
forall a. Bits a => a -> Maybe Int
bitSizeMaybe @a a
forall a. HasCallStack => a
undefined
    isSigned :: Vector v n a -> Bool
isSigned Vector v n a
_ = Bool
False
    testBit :: Vector v n a -> Int -> Bool
testBit = (v a -> Int -> Bool) -> Vector v n a -> Int -> Bool
coerce (Bits (v a) => v a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit @(v a))
    popCount :: Vector v n a -> Int
popCount = (v a -> Int) -> Vector v n a -> Int
coerce (Bits (v a) => v a -> Int
forall a. Bits a => a -> Int
popCount @(v a))
    setBit :: Vector v n a -> Int -> Vector v n a
setBit = (v a -> Int -> v a) -> Vector v n a -> Int -> Vector v n a
coerce (Bits (v a) => v a -> Int -> v a
forall a. Bits a => a -> Int -> a
setBit @(v a))
    complementBit :: Vector v n a -> Int -> Vector v n a
complementBit = (v a -> Int -> v a) -> Vector v n a -> Int -> Vector v n a
coerce (Bits (v a) => v a -> Int -> v a
forall a. Bits a => a -> Int -> a
complementBit @(v a))
    -- need to do special stuff because they return a vector from scratch
    bit :: Int -> Vector v n a
bit Int
n = (forall s. ST s (Vector v n a)) -> Vector v n a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector v n a)) -> Vector v n a)
-> (forall s. ST s (Vector v n a)) -> Vector v n a
forall a b. (a -> b) -> a -> b
$ do
      MVector (Mutable v) n s a
v <- a -> ST s (MVector (Mutable v) n (PrimState (ST s)) a)
forall (v :: Type -> Type -> Type) (n :: Nat) (m :: Type -> Type)
       a.
(KnownNat n, PrimMonad m, MVector v a) =>
a -> m (MVector v n (PrimState m) a)
SVGM.replicate a
forall a. Bits a => a
zeroBits
      Maybe (Finite n) -> (Finite n -> ST s ()) -> ST s ()
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (Integer -> Maybe (Finite n)
forall (n :: Nat). KnownNat n => Integer -> Maybe (Finite n)
packFinite (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)) ((Finite n -> ST s ()) -> ST s ())
-> (Finite n -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Finite n
i ->
        MVector (Mutable v) n (PrimState (ST s)) a
-> Finite n -> a -> ST s ()
forall (v :: Type -> Type -> Type) (n :: Nat) (m :: Type -> Type)
       a.
(PrimMonad m, MVector v a) =>
MVector v n (PrimState m) a -> Finite n -> a -> m ()
SVGM.write MVector (Mutable v) n s a
MVector (Mutable v) n (PrimState (ST s)) a
v Finite n
i (a -> a
forall a. Bits a => a -> a
complement a
forall a. Bits a => a
zeroBits)
      MVector (Mutable v) n (PrimState (ST s)) a -> ST s (Vector v n a)
forall (m :: Type -> Type) (v :: Type -> Type) a (n :: Nat).
(PrimMonad m, Vector v a) =>
MVector (Mutable v) n (PrimState m) a -> m (Vector v n a)
freeze MVector (Mutable v) n s a
MVector (Mutable v) n (PrimState (ST s)) a
v
    zeroBits :: Vector v n a
zeroBits = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate a
forall a. Bits a => a
zeroBits
    -- need to do special stuff because they have to preserve vector size
    shiftL :: Vector v n a -> Int -> Vector v n a
shiftL Vector v n a
xs Int
i
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0     = ArithException -> Vector v n a
forall a e. Exception e => e -> a
Exception.throw ArithException
Exception.Overflow
      | Bool
otherwise = Vector v n a -> Int -> Vector v n a
forall a. Bits a => a -> Int -> a
unsafeShiftL Vector v n a
xs Int
i
    unsafeShiftL :: Vector v n a -> Int -> Vector v n a
unsafeShiftL (Vector v a
xs) Int
i
        | Int
i  Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0   = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
xs
        | Int
i' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n   = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate a
forall a. Bits a => a
zeroBits
        | Bool
otherwise = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> v a -> Vector v n a
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (v a)) -> v a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (v a)) -> v a) -> (forall s. ST s (v a)) -> v a
forall a b. (a -> b) -> a -> b
$ do
            Mutable v s a
u <- Int -> a -> ST s (Mutable v (PrimState (ST s)) a)
forall (m :: Type -> Type) (v :: Type -> Type -> Type) a.
(PrimMonad m, MVector v a) =>
Int -> a -> m (v (PrimState m) a)
VGM.replicate Int
n a
forall a. Bits a => a
zeroBits
            Mutable v (PrimState (ST s)) a -> v a -> ST s ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
VG.unsafeCopy (Int -> Mutable v s a -> Mutable v s a
forall (v :: Type -> Type -> Type) a s.
MVector v a =>
Int -> v s a -> v s a
VGM.unsafeDrop Int
i' Mutable v s a
u) (Int -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> v a -> v a
VG.unsafeTake (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i') v a
xs)
            Mutable v (PrimState (ST s)) a -> ST s (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze Mutable v s a
Mutable v (PrimState (ST s)) a
u
      where
        n :: Int
n   = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy @n)
        i' :: Int
i' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n Int
i
    shiftR :: Vector v n a -> Int -> Vector v n a
shiftR Vector v n a
xs Int
i
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0     = ArithException -> Vector v n a
forall a e. Exception e => e -> a
Exception.throw ArithException
Exception.Overflow
      | Bool
otherwise = Vector v n a -> Int -> Vector v n a
forall a. Bits a => a -> Int -> a
unsafeShiftR Vector v n a
xs Int
i
    unsafeShiftR :: Vector v n a -> Int -> Vector v n a
unsafeShiftR (Vector v a
xs) Int
i
        | Int
i  Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0   = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector v a
xs
        | Int
i' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n   = a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a.
(KnownNat n, Vector v a) =>
a -> Vector v n a
replicate a
forall a. Bits a => a
zeroBits
        | Bool
otherwise = v a -> Vector v n a
forall (v :: Type -> Type) (n :: Nat) a. v a -> Vector v n a
Vector (v a -> Vector v n a) -> v a -> Vector v n a
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (v a)) -> v a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (v a)) -> v a) -> (forall s. ST s (v a)) -> v a
forall a b. (a -> b) -> a -> b
$ do
            Mutable v s a
u <- Int -> a -> ST s (Mutable v (PrimState (ST s)) a)
forall (m :: Type -> Type) (v :: Type -> Type -> Type) a.
(PrimMonad m, MVector v a) =>
Int -> a -> m (v (PrimState m) a)
VGM.replicate Int
n a
forall a. Bits a => a
zeroBits
            Mutable v (PrimState (ST s)) a -> v a -> ST s ()
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> v a -> m ()
VG.unsafeCopy (Int -> Mutable v s a -> Mutable v s a
forall (v :: Type -> Type -> Type) a s.
MVector v a =>
Int -> v s a -> v s a
VGM.unsafeTake (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i') Mutable v s a
u) (Int -> v a -> v a
forall (v :: Type -> Type) a. Vector v a => Int -> v a -> v a
VG.unsafeDrop Int
i' v a
xs)
            Mutable v (PrimState (ST s)) a -> ST s (v a)
forall (m :: Type -> Type) (v :: Type -> Type) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze Mutable v s a
Mutable v (PrimState (ST s)) a
u
      where
        n :: Int
n  = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy @n)
        i' :: Int
i' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n Int
i

-- | Treats a bit vector as n times the size of the stored bits, reflecting
-- the 'Bits' instance; does not necessarily reflect exact in-memory
-- representation.  See 'Storable' instance to get information on the
-- actual in-memry representation.
instance (VG.Vector v a, Bits (v a), FiniteBits a, KnownNat n) => FiniteBits (Vector v n a) where
    finiteBitSize :: Vector v n a -> Int
finiteBitSize Vector v n a
_ = a -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize @a a
forall a. HasCallStack => a
undefined Int -> Int -> Int
forall a. Num a => a -> a -> a
* Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy @n))