{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE Rank2Types            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
-- |
-- Implementation of fixed-vectors
module Data.Vector.Fixed.Internal where

import Control.DeepSeq       (NFData(..))
import Data.Typeable         (Proxy(..))
import Data.Functor.Identity (Identity(..))
import qualified Data.Foldable    as T
import qualified Data.Traversable as T
import Foreign.Storable (Storable(..))
import Foreign.Ptr      (Ptr,castPtr)
import GHC.TypeLits

import           Data.Vector.Fixed.Cont     (Vector(..),Dim,Arity,vector,Add)
import qualified Data.Vector.Fixed.Cont as C

import Prelude hiding ( replicate,map,zipWith,maximum,minimum,and,or,all,any
                      , foldl,foldr,foldl1,length,sum,reverse,scanl,scanl1
                      , head,tail,mapM,mapM_,sequence,sequence_,concat
                      )


----------------------------------------------------------------
-- Constructors
----------------------------------------------------------------

mk0 :: (Vector v a, Dim v ~ 0) => v a
mk0 :: v a
mk0 = ContVec 0 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector ContVec 0 a
forall a. ContVec 0 a
C.empty
{-# INLINE mk0 #-}

mk1 :: (Vector v a, Dim v ~ 1) => a -> v a
mk1 :: a -> v a
mk1 a
a1 = ContVec 1 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 1 a -> v a) -> ContVec 1 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> ContVec 1 a
forall a. a -> ContVec 1 a
C.mk1 a
a1
{-# INLINE mk1 #-}

mk2 :: (Vector v a, Dim v ~ 2) => a -> a -> v a
mk2 :: a -> a -> v a
mk2 a
a1 a
a2 = ContVec 2 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 2 a -> v a) -> ContVec 2 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> ContVec 2 a
forall a. a -> a -> ContVec 2 a
C.mk2 a
a1 a
a2
{-# INLINE mk2 #-}

mk3 :: (Vector v a, Dim v ~ 3) => a -> a -> a -> v a
mk3 :: a -> a -> a -> v a
mk3 a
a1 a
a2 a
a3 = ContVec 3 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 3 a -> v a) -> ContVec 3 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> ContVec 3 a
forall a. a -> a -> a -> ContVec 3 a
C.mk3 a
a1 a
a2 a
a3
{-# INLINE mk3 #-}

mk4 :: (Vector v a, Dim v ~ 4) => a -> a -> a -> a -> v a
mk4 :: a -> a -> a -> a -> v a
mk4 a
a1 a
a2 a
a3 a
a4 = ContVec 4 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 4 a -> v a) -> ContVec 4 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> ContVec 4 a
forall a. a -> a -> a -> a -> ContVec 4 a
C.mk4 a
a1 a
a2 a
a3 a
a4
{-# INLINE mk4 #-}

mk5 :: (Vector v a, Dim v ~ 5) => a -> a -> a -> a -> a -> v a
mk5 :: a -> a -> a -> a -> a -> v a
mk5 a
a1 a
a2 a
a3 a
a4 a
a5 = ContVec 5 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 5 a -> v a) -> ContVec 5 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> a -> ContVec 5 a
forall a. a -> a -> a -> a -> a -> ContVec 5 a
C.mk5 a
a1 a
a2 a
a3 a
a4 a
a5
{-# INLINE mk5 #-}

mk6 :: (Vector v a, Dim v ~ 6) => a -> a -> a -> a -> a -> a -> v a
mk6 :: a -> a -> a -> a -> a -> a -> v a
mk6 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6 = ContVec 6 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 6 a -> v a) -> ContVec 6 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> a -> a -> ContVec 6 a
forall a. a -> a -> a -> a -> a -> a -> ContVec 6 a
C.mk6 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6
{-# INLINE mk6 #-}

mk7 :: (Vector v a, Dim v ~ 7) => a -> a -> a -> a -> a -> a -> a -> v a
mk7 :: a -> a -> a -> a -> a -> a -> a -> v a
mk7 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6 a
a7 = ContVec 7 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 7 a -> v a) -> ContVec 7 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> a -> a -> a -> ContVec 7 a
forall a. a -> a -> a -> a -> a -> a -> a -> ContVec 7 a
C.mk7 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6 a
a7
{-# INLINE mk7 #-}

mk8 :: (Vector v a, Dim v ~ 8) => a -> a -> a -> a -> a -> a -> a -> a -> v a
mk8 :: a -> a -> a -> a -> a -> a -> a -> a -> v a
mk8 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6 a
a7 a
a8 = ContVec 8 a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec 8 a -> v a) -> ContVec 8 a -> v a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> a -> a -> a -> a -> ContVec 8 a
forall a. a -> a -> a -> a -> a -> a -> a -> a -> ContVec 8 a
C.mk8 a
a1 a
a2 a
a3 a
a4 a
a5 a
a6 a
a7 a
a8
{-# INLINE mk8 #-}

-- | N-ary constructor. Despite scary signature it's just N-ary
--   function with additional type parameter which is used to fix type
--   of vector being constructed. It could be used as:
--
--   > v = mkN (Proxy :: Proxy (Int,Int,Int)) 1 2 3
--
--   or using @TypeApplications@ syntax:
--
--   > v = mkN (Proxy @ (Int,Int,Int)) 1 2 3
--
--   or if type of @v@ is fixed elsewhere
--
--   > v = mkN [v] 1 2 3
mkN :: forall proxy v a. (Vector v a)
    => proxy (v a) -> C.Fn (C.Peano (Dim v)) a (v a)
mkN :: proxy (v a) -> Fn (Peano (Dim v)) a (v a)
mkN proxy (v a)
_ = Fun (Peano (Dim v)) a (v a) -> Fn (Peano (Dim v)) a (v a)
forall (n :: PeanoNum) a b. Fun n a b -> Fn n a b
C.unFun (Fun (Peano (Dim v)) a (v a)
forall (v :: * -> *) a. Vector v a => Fun (Peano (Dim v)) a (v a)
construct :: C.Fun (C.Peano (Dim v)) a (v a))

----------------------------------------------------------------
-- Generic functions
----------------------------------------------------------------

-- | Replicate value /n/ times.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec2)
--   >>> replicate 1 :: Vec2 Int
--   fromList [1,1]
--
--   >>> replicate 2 :: (Double,Double,Double)
--   (2.0,2.0,2.0)
--
--   >>> import Data.Vector.Fixed.Boxed (Vec4)
--   >>> replicate "foo" :: Vec4 String
--   fromList ["foo","foo","foo","foo"]
replicate :: Vector v a => a -> v a
{-# INLINE replicate #-}
replicate :: a -> v a
replicate
  = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a) -> (a -> ContVec (Dim v) a) -> a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ContVec (Dim v) a
forall (n :: Nat) a. Arity n => a -> ContVec n a
C.replicate


-- | Execute monadic action for every element of vector.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec2,Vec3)
--   >>> replicateM (Just 3) :: Maybe (Vec3 Int)
--   Just (fromList [3,3,3])
--   >>> replicateM (putStrLn "Hi!") :: IO (Vec2 ())
--   Hi!
--   Hi!
--   fromList [(),()]
replicateM :: (Vector v a, Applicative f) => f a -> f (v a)
{-# INLINE replicateM #-}
replicateM :: f a -> f (v a)
replicateM
  = (ContVec (Dim v) a -> v a) -> f (ContVec (Dim v) a) -> f (v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (f (ContVec (Dim v) a) -> f (v a))
-> (f a -> f (ContVec (Dim v) a)) -> f a -> f (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> f (ContVec (Dim v) a)
forall (n :: Nat) (f :: * -> *) a.
(Arity n, Applicative f) =>
f a -> f (ContVec n a)
C.replicateM


-- | Unit vector along Nth axis. If index is larger than vector
--   dimensions returns zero vector.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec3)
--   >>> basis 0 :: Vec3 Int
--   fromList [1,0,0]
--   >>> basis 1 :: Vec3 Int
--   fromList [0,1,0]
--   >>> basis 3 :: Vec3 Int
--   fromList [0,0,0]
basis :: (Vector v a, Num a) => Int -> v a
{-# INLINE basis #-}
basis :: Int -> v a
basis = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> (Int -> ContVec (Dim v) a) -> Int -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ContVec (Dim v) a
forall a (n :: Nat). (Num a, Arity n) => Int -> ContVec n a
C.basis


-- | Unfold vector.
unfoldr :: (Vector v a) => (b -> (a,b)) -> b -> v a
{-# INLINE unfoldr #-}
unfoldr :: (b -> (a, b)) -> b -> v a
unfoldr b -> (a, b)
f = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a) -> (b -> ContVec (Dim v) a) -> b -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> (a, b)) -> b -> ContVec (Dim v) a
forall (n :: Nat) b a. Arity n => (b -> (a, b)) -> b -> ContVec n a
C.unfoldr b -> (a, b)
f


-- | Generate vector from function which maps element's index to its
--   value.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Unboxed (Vec4)
--   >>> generate (^2) :: Vec4 Int
--   fromList [0,1,4,9]
generate :: (Vector v a) => (Int -> a) -> v a
{-# INLINE generate #-}
generate :: (Int -> a) -> v a
generate = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> ((Int -> a) -> ContVec (Dim v) a) -> (Int -> a) -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a) -> ContVec (Dim v) a
forall (n :: Nat) a. Arity n => (Int -> a) -> ContVec n a
C.generate


-- | Generate vector from monadic function which maps element's index
--   to its value.
generateM :: (Applicative f, Vector v a) => (Int -> f a) -> f (v a)
{-# INLINE generateM #-}
generateM :: (Int -> f a) -> f (v a)
generateM = (ContVec (Dim v) a -> v a) -> f (ContVec (Dim v) a) -> f (v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (f (ContVec (Dim v) a) -> f (v a))
-> ((Int -> f a) -> f (ContVec (Dim v) a))
-> (Int -> f a)
-> f (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> f a) -> f (ContVec (Dim v) a)
forall (f :: * -> *) (n :: Nat) a.
(Applicative f, Arity n) =>
(Int -> f a) -> f (ContVec n a)
C.generateM



----------------------------------------------------------------

-- | First element of vector.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec3)
--   >>> let x = mk3 1 2 3 :: Vec3 Int
--   >>> head x
--   1
head :: (Vector v a, 1 <= Dim v) => v a -> a
{-# INLINE head #-}
head :: v a -> a
head = ContVec (Dim v) a -> a
forall (n :: Nat) a. (Arity n, 1 <= n) => ContVec n a -> a
C.head (ContVec (Dim v) a -> a) -> (v a -> ContVec (Dim v) a) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec


-- | Tail of vector.
--
--   Examples:
--
--   >>> import Data.Complex
--   >>> tail (1,2,3) :: Complex Double
--   2.0 :+ 3.0
tail :: (Vector v a, Vector w a, Dim v ~ (Dim w + 1))
     => v a -> w a
{-# INLINE tail #-}
tail :: v a -> w a
tail = ContVec (Dim w) a -> w a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim w) a -> w a)
-> (v a -> ContVec (Dim w) a) -> v a -> w a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContVec (Dim w + 1) a -> ContVec (Dim w) a
forall (n :: Nat) a. Arity n => ContVec (n + 1) a -> ContVec n a
C.tail (ContVec (Dim w + 1) a -> ContVec (Dim w) a)
-> (v a -> ContVec (Dim w + 1) a) -> v a -> ContVec (Dim w) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim w + 1) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Cons element to the vector
cons :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1))
     => a -> v a -> w a
{-# INLINE cons #-}
cons :: a -> v a -> w a
cons a
a = ContVec (Dim v + 1) a -> w a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v + 1) a -> w a)
-> (v a -> ContVec (Dim v + 1) a) -> v a -> w a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ContVec (Dim v) a -> ContVec (Dim v + 1) a
forall (n :: Nat) a.
Arity n =>
a -> ContVec n a -> ContVec (n + 1) a
C.cons a
a (ContVec (Dim v) a -> ContVec (Dim v + 1) a)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v + 1) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Append element to the vector
snoc :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1))
     => a -> v a -> w a
{-# INLINE snoc #-}
snoc :: a -> v a -> w a
snoc a
a = ContVec (Dim v + 1) a -> w a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v + 1) a -> w a)
-> (v a -> ContVec (Dim v + 1) a) -> v a -> w a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ContVec (Dim v) a -> ContVec (Dim v + 1) a
forall (n :: Nat) a.
Arity n =>
a -> ContVec n a -> ContVec (n + 1) a
C.snoc a
a (ContVec (Dim v) a -> ContVec (Dim v + 1) a)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v + 1) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

concat :: ( Vector v a, Vector u a, Vector w a
          , (Dim v + Dim u) ~ Dim w
            -- Tautology
          , C.Peano (Dim v + Dim u) ~ Add (C.Peano (Dim v)) (C.Peano (Dim u))
          )
       => v a -> u a -> w a
{-# INLINE concat #-}
concat :: v a -> u a -> w a
concat v a
v u a
u = ContVec (Dim w) a -> w a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim w) a -> w a) -> ContVec (Dim w) a -> w a
forall a b. (a -> b) -> a -> b
$ ContVec (Dim v) a -> ContVec (Dim u) a -> ContVec (Dim v + Dim u) a
forall (n :: Nat) (k :: Nat) a.
(Arity n, Arity k, Arity (n + k),
 Peano (n + k) ~ Add (Peano n) (Peano k)) =>
ContVec n a -> ContVec k a -> ContVec (n + k) a
C.concat (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (u a -> ContVec (Dim u) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec u a
u)

-- | Reverse order of elements in the vector
reverse :: Vector v a => v a -> v a
reverse :: v a -> v a
reverse = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> (v a -> ContVec (Dim v) a) -> v a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContVec (Dim v) a -> ContVec (Dim v) a
forall (n :: Nat) a. Arity n => ContVec n a -> ContVec n a
C.reverse (ContVec (Dim v) a -> ContVec (Dim v) a)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE reverse #-}

-- | Retrieve vector's element at index. Generic implementation is
--   /O(n)/ but more efficient one is used when possible.
(!) :: (Vector v a) => v a -> Int -> a
{-# INLINE (!) #-}
v a
v ! :: v a -> Int -> a
! Int
n = Int -> ContVec (Dim v) a -> a
forall (n :: Nat) r. Arity n => Int -> ContVec n r -> r
runIndex Int
n (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v)

-- Used in rewriting of index function.
runIndex :: Arity n => Int -> C.ContVec n r -> r
runIndex :: Int -> ContVec n r -> r
runIndex = Int -> ContVec n r -> r
forall (n :: Nat) r. Arity n => Int -> ContVec n r -> r
C.index
{-# INLINE[0] runIndex #-}

-- We are trying to be clever with indexing here. It's not possible to
-- write generic indexing function. For example it's necessary O(n)
-- for VecList. It's however possible to write O(1) indexing for some
-- vectors and we trying to use such functions where possible.
--
-- We try to use presumable more efficient basicIndex
--
--  1. It should not interfere with deforestation. So we should
--     rewrite only when deforestation rule already fired.
--     (starting from phase 1).
--
--  2. Creation of vector is costlier than generic indexing so we should
--     apply rule only when vector is created anyway
--
-- In order to avoid firing this rule on implementation of (!) it has
-- been necessary to move definition of all functions to internal module.

{-# RULES
"fixed-vector:index/basicIndex"[1] forall vv i.
  runIndex i (C.cvec vv) = C.basicIndex vv i
 #-}


-- | Get element from vector at statically known index
index :: (Vector v a, KnownNat k, k + 1 <= Dim v)
      => v a -> proxy k -> a
{-# INLINE index #-}
index :: v a -> proxy k -> a
index v a
v proxy k
k = v a
v v a -> Int -> a
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
! Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (proxy k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy k
k)

-- | Set n'th element in the vector
set :: (Vector v a, KnownNat k, k + 1 <= Dim v) => proxy k -> a -> v a -> v a
{-# INLINE set #-}
set :: proxy k -> a -> v a -> v a
set proxy k
k a
a = Identity (v a) -> v a
forall a. Identity a -> a
runIdentity (Identity (v a) -> v a) -> (v a -> Identity (v a)) -> v a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (a -> Identity a) -> v a -> Identity (v a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Functor f) =>
Int -> (a -> f a) -> v a -> f (v a)
element (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (proxy k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy k
k))
                                (Identity a -> a -> Identity a
forall a b. a -> b -> a
const (a -> Identity a
forall a. a -> Identity a
Identity a
a))

-- | Twan van Laarhoven's lens for element of vector
element :: (Vector v a, Functor f) => Int -> (a -> f a) -> (v a -> f (v a))
{-# INLINE element #-}
element :: Int -> (a -> f a) -> v a -> f (v a)
element Int
i a -> f a
f v a
v = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a) -> f (ContVec (Dim v) a) -> f (v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> (a -> f a) -> ContVec (Dim v) a -> f (ContVec (Dim v) a)
forall (n :: Nat) (f :: * -> *) a.
(Arity n, Functor f) =>
Int -> (a -> f a) -> ContVec n a -> f (ContVec n a)
C.element Int
i a -> f a
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v)

-- | Twan van Laarhoven's lens for element of vector with statically
--   known index.
elementTy :: (Vector v a, KnownNat k, k + 1 <= Dim v, Functor f)
          => proxy k -> (a -> f a) -> (v a -> f (v a))
{-# INLINE elementTy #-}
elementTy :: proxy k -> (a -> f a) -> v a -> f (v a)
elementTy proxy k
k = Int -> (a -> f a) -> v a -> f (v a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Functor f) =>
Int -> (a -> f a) -> v a -> f (v a)
element (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (proxy k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy k
k))

-- | Left fold over vector
foldl :: Vector v a => (b -> a -> b) -> b -> v a -> b
{-# INLINE foldl #-}
foldl :: (b -> a -> b) -> b -> v a -> b
foldl b -> a -> b
f b
x = (b -> a -> b) -> b -> ContVec (Dim v) a -> b
forall (n :: Nat) b a.
Arity n =>
(b -> a -> b) -> b -> ContVec n a -> b
C.foldl b -> a -> b
f b
x
          (ContVec (Dim v) a -> b) -> (v a -> ContVec (Dim v) a) -> v a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Right fold over vector
foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b
{-# INLINE foldr #-}
foldr :: (a -> b -> b) -> b -> v a -> b
foldr a -> b -> b
f b
x = (a -> b -> b) -> b -> ContVec (Dim v) a -> b
forall (n :: Nat) a b.
Arity n =>
(a -> b -> b) -> b -> ContVec n a -> b
C.foldr a -> b -> b
f b
x
          (ContVec (Dim v) a -> b) -> (v a -> ContVec (Dim v) a) -> v a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec


-- | Left fold over vector
foldl1 :: (Vector v a, 1 <= Dim v) => (a -> a -> a) -> v a -> a
{-# INLINE foldl1 #-}
foldl1 :: (a -> a -> a) -> v a -> a
foldl1 a -> a -> a
f = (a -> a -> a) -> ContVec (Dim v) a -> a
forall (n :: Nat) a.
(Arity n, 1 <= n) =>
(a -> a -> a) -> ContVec n a -> a
C.foldl1 a -> a -> a
f
         (ContVec (Dim v) a -> a) -> (v a -> ContVec (Dim v) a) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Combine the elements of a structure using a monoid. Similar to
--   'T.fold'
fold :: (Vector v m, Monoid m) => v m -> m
{-# INLINE fold #-}
fold :: v m -> m
fold = ContVec (Dim v) m -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
T.fold
     (ContVec (Dim v) m -> m) -> (v m -> ContVec (Dim v) m) -> v m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v m -> ContVec (Dim v) m
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Map each element of the structure to a monoid,
--   and combine the results. Similar to 'T.foldMap'
foldMap :: (Vector v a, Monoid m) => (a -> m) -> v a -> m
{-# INLINE foldMap #-}
foldMap :: (a -> m) -> v a -> m
foldMap a -> m
f = (a -> m) -> ContVec (Dim v) a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
T.foldMap a -> m
f
          (ContVec (Dim v) a -> m) -> (v a -> ContVec (Dim v) a) -> v a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Right fold over vector
ifoldr :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b
{-# INLINE ifoldr #-}
ifoldr :: (Int -> a -> b -> b) -> b -> v a -> b
ifoldr Int -> a -> b -> b
f b
x = (Int -> a -> b -> b) -> b -> ContVec (Dim v) a -> b
forall (n :: Nat) a b.
Arity n =>
(Int -> a -> b -> b) -> b -> ContVec n a -> b
C.ifoldr Int -> a -> b -> b
f b
x
           (ContVec (Dim v) a -> b) -> (v a -> ContVec (Dim v) a) -> v a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Left fold over vector. Function is applied to each element and
--   its index.
ifoldl :: Vector v a => (b -> Int -> a -> b) -> b -> v a -> b
{-# INLINE ifoldl #-}
ifoldl :: (b -> Int -> a -> b) -> b -> v a -> b
ifoldl b -> Int -> a -> b
f b
z = (b -> Int -> a -> b) -> b -> ContVec (Dim v) a -> b
forall (n :: Nat) b a.
Arity n =>
(b -> Int -> a -> b) -> b -> ContVec n a -> b
C.ifoldl b -> Int -> a -> b
f b
z
           (ContVec (Dim v) a -> b) -> (v a -> ContVec (Dim v) a) -> v a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Monadic fold over vector.
foldM :: (Vector v a, Monad m) => (b -> a -> m b) -> b -> v a -> m b
{-# INLINE foldM #-}
foldM :: (b -> a -> m b) -> b -> v a -> m b
foldM b -> a -> m b
f b
x = (b -> a -> m b) -> b -> ContVec (Dim v) a -> m b
forall (n :: Nat) (m :: * -> *) b a.
(Arity n, Monad m) =>
(b -> a -> m b) -> b -> ContVec n a -> m b
C.foldM b -> a -> m b
f b
x (ContVec (Dim v) a -> m b)
-> (v a -> ContVec (Dim v) a) -> v a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Left monadic fold over vector. Function is applied to each element and
--   its index.
ifoldM :: (Vector v a, Monad m) => (b -> Int -> a -> m b) -> b -> v a -> m b
{-# INLINE ifoldM #-}
ifoldM :: (b -> Int -> a -> m b) -> b -> v a -> m b
ifoldM b -> Int -> a -> m b
f b
x = (b -> Int -> a -> m b) -> b -> ContVec (Dim v) a -> m b
forall (n :: Nat) (m :: * -> *) b a.
(Arity n, Monad m) =>
(b -> Int -> a -> m b) -> b -> ContVec n a -> m b
C.ifoldM b -> Int -> a -> m b
f b
x (ContVec (Dim v) a -> m b)
-> (v a -> ContVec (Dim v) a) -> v a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec



----------------------------------------------------------------

-- | Sum all elements in the vector.
sum :: (Vector v a, Num a) => v a -> a
sum :: v a -> a
sum = ContVec (Dim v) a -> a
forall a (n :: Nat). (Num a, Arity n) => ContVec n a -> a
C.sum (ContVec (Dim v) a -> a) -> (v a -> ContVec (Dim v) a) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE sum #-}

-- | Maximal element of vector.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec3)
--   >>> let x = mk3 1 2 3 :: Vec3 Int
--   >>> maximum x
--   3
maximum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
maximum :: v a -> a
maximum = ContVec (Dim v) a -> a
forall a (n :: Nat). (Ord a, Arity n, 1 <= n) => ContVec n a -> a
C.maximum (ContVec (Dim v) a -> a) -> (v a -> ContVec (Dim v) a) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE maximum #-}

-- | Minimal element of vector.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec3)
--   >>> let x = mk3 1 2 3 :: Vec3 Int
--   >>> minimum x
--   1
minimum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
minimum :: v a -> a
minimum = ContVec (Dim v) a -> a
forall a (n :: Nat). (Ord a, Arity n, 1 <= n) => ContVec n a -> a
C.minimum (ContVec (Dim v) a -> a) -> (v a -> ContVec (Dim v) a) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE minimum #-}

-- | Conjunction of all elements of a vector.
and :: (Vector v Bool) => v Bool -> Bool
and :: v Bool -> Bool
and = ContVec (Dim v) Bool -> Bool
forall (n :: Nat). Arity n => ContVec n Bool -> Bool
C.and (ContVec (Dim v) Bool -> Bool)
-> (v Bool -> ContVec (Dim v) Bool) -> v Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Bool -> ContVec (Dim v) Bool
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE and #-}

-- | Disjunction of all elements of a vector.
or :: (Vector v Bool) => v Bool -> Bool
or :: v Bool -> Bool
or = ContVec (Dim v) Bool -> Bool
forall (n :: Nat). Arity n => ContVec n Bool -> Bool
C.or (ContVec (Dim v) Bool -> Bool)
-> (v Bool -> ContVec (Dim v) Bool) -> v Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Bool -> ContVec (Dim v) Bool
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE or #-}

-- | Determines whether all elements of vector satisfy predicate.
all :: (Vector v a) => (a -> Bool) -> v a -> Bool
all :: (a -> Bool) -> v a -> Bool
all a -> Bool
f = ((a -> Bool) -> ContVec (Dim v) a -> Bool
forall (n :: Nat) a. Arity n => (a -> Bool) -> ContVec n a -> Bool
C.all a -> Bool
f) (ContVec (Dim v) a -> Bool)
-> (v a -> ContVec (Dim v) a) -> v a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE all #-}

-- | Determines whether any of element of vector satisfy predicate.
any :: (Vector v a) => (a -> Bool) -> v a -> Bool
any :: (a -> Bool) -> v a -> Bool
any a -> Bool
f = ((a -> Bool) -> ContVec (Dim v) a -> Bool
forall (n :: Nat) a. Arity n => (a -> Bool) -> ContVec n a -> Bool
C.any a -> Bool
f) (ContVec (Dim v) a -> Bool)
-> (v a -> ContVec (Dim v) a) -> v a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE any #-}

-- | The 'find' function takes a predicate and a vector and returns
--   the leftmost element of the vector matching the predicate,
--   or 'Nothing' if there is no such element.
find :: (Vector v a) => (a -> Bool) -> v a -> Maybe a
find :: (a -> Bool) -> v a -> Maybe a
find a -> Bool
f = ((a -> Bool) -> ContVec (Dim v) a -> Maybe a
forall (n :: Nat) a.
Arity n =>
(a -> Bool) -> ContVec n a -> Maybe a
C.find a -> Bool
f) (ContVec (Dim v) a -> Maybe a)
-> (v a -> ContVec (Dim v) a) -> v a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec
{-# INLINE find #-}

----------------------------------------------------------------

-- | Test two vectors for equality.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec2)
--   >>> let v0 = basis 0 :: Vec2 Int
--   >>> let v1 = basis 1 :: Vec2 Int
--   >>> v0 `eq` v0
--   True
--   >>> v0 `eq` v1
--   False
eq :: (Vector v a, Eq a) => v a -> v a -> Bool
{-# INLINE eq #-}
eq :: v a -> v a -> Bool
eq v a
v v a
w = ContVec (Dim v) Bool -> Bool
forall (n :: Nat). Arity n => ContVec n Bool -> Bool
C.and
       (ContVec (Dim v) Bool -> Bool) -> ContVec (Dim v) Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool)
-> ContVec (Dim v) a -> ContVec (Dim v) a -> ContVec (Dim v) Bool
forall (n :: Nat) a b c.
Arity n =>
(a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
C.zipWith a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
w)


-- | Lexicographic ordering of two vectors.
ord :: (Vector v a, Ord a) => v a -> v a -> Ordering
{-# INLINE ord #-}
ord :: v a -> v a -> Ordering
ord v a
v v a
w = (Ordering -> Ordering -> Ordering)
-> Ordering -> ContVec (Dim v) Ordering -> Ordering
forall (n :: Nat) b a.
Arity n =>
(b -> a -> b) -> b -> ContVec n a -> b
C.foldl Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
mappend Ordering
forall a. Monoid a => a
mempty
        (ContVec (Dim v) Ordering -> Ordering)
-> ContVec (Dim v) Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$ (a -> a -> Ordering)
-> ContVec (Dim v) a
-> ContVec (Dim v) a
-> ContVec (Dim v) Ordering
forall (n :: Nat) a b c.
Arity n =>
(a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
C.zipWith a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
w)



----------------------------------------------------------------

-- | Map over vector
map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b
{-# INLINE map #-}
map :: (a -> b) -> v a -> v b
map a -> b
f = ContVec (Dim v) b -> v b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
      (ContVec (Dim v) b -> v b)
-> (v a -> ContVec (Dim v) b) -> v a -> v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> ContVec (Dim v) a -> ContVec (Dim v) b
forall (n :: Nat) a b.
Arity n =>
(a -> b) -> ContVec n a -> ContVec n b
C.map a -> b
f
      (ContVec (Dim v) a -> ContVec (Dim v) b)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Evaluate every action in the vector from left to right.
sequence :: (Vector v a, Vector v (f a), Applicative f) => v (f a) -> f (v a)
{-# INLINE sequence #-}
sequence :: v (f a) -> f (v a)
sequence = (f a -> f a) -> v (f a) -> f (v a)
forall (v :: * -> *) a b (f :: * -> *).
(Vector v a, Vector v b, Applicative f) =>
(a -> f b) -> v a -> f (v b)
mapM f a -> f a
forall a. a -> a
id

-- | Evaluate every action in the vector from left to right and ignore result
sequence_ :: (Vector v (f a), Applicative f) => v (f a) -> f ()
{-# INLINE sequence_ #-}
sequence_ :: v (f a) -> f ()
sequence_ = (f a -> f a) -> v (f a) -> f ()
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
mapM_ f a -> f a
forall a. a -> a
id


-- | Effectful map over vector.
mapM :: (Vector v a, Vector v b, Applicative f) => (a -> f b) -> v a -> f (v b)
{-# INLINE mapM #-}
mapM :: (a -> f b) -> v a -> f (v b)
mapM a -> f b
f = (ContVec (Dim v) b -> v b) -> f (ContVec (Dim v) b) -> f (v b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) b -> v b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
       (f (ContVec (Dim v) b) -> f (v b))
-> (v a -> f (ContVec (Dim v) b)) -> v a -> f (v b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f b) -> ContVec (Dim v) a -> f (ContVec (Dim v) b)
forall (n :: Nat) (f :: * -> *) a b.
(Arity n, Applicative f) =>
(a -> f b) -> ContVec n a -> f (ContVec n b)
C.mapM a -> f b
f
       (ContVec (Dim v) a -> f (ContVec (Dim v) b))
-> (v a -> ContVec (Dim v) a) -> v a -> f (ContVec (Dim v) b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Apply monadic action to each element of vector and ignore result.
mapM_ :: (Vector v a, Applicative f) => (a -> f b) -> v a -> f ()
{-# INLINE mapM_ #-}
mapM_ :: (a -> f b) -> v a -> f ()
mapM_ a -> f b
f = (a -> f b) -> ContVec (Dim v) a -> f ()
forall (n :: Nat) (f :: * -> *) a b.
(Arity n, Applicative f) =>
(a -> f b) -> ContVec n a -> f ()
C.mapM_ a -> f b
f
        (ContVec (Dim v) a -> f ())
-> (v a -> ContVec (Dim v) a) -> v a -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec


-- | Apply function to every element of the vector and its index.
imap :: (Vector v a, Vector v b) =>
    (Int -> a -> b) -> v a -> v b
{-# INLINE imap #-}
imap :: (Int -> a -> b) -> v a -> v b
imap Int -> a -> b
f = ContVec (Dim v) b -> v b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
       (ContVec (Dim v) b -> v b)
-> (v a -> ContVec (Dim v) b) -> v a -> v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> b) -> ContVec (Dim v) a -> ContVec (Dim v) b
forall (n :: Nat) a b.
Arity n =>
(Int -> a -> b) -> ContVec n a -> ContVec n b
C.imap Int -> a -> b
f
       (ContVec (Dim v) a -> ContVec (Dim v) b)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Apply monadic function to every element of the vector and its index.
imapM :: (Vector v a, Vector v b, Applicative f)
      => (Int -> a -> f b) -> v a -> f (v b)
{-# INLINE imapM #-}
imapM :: (Int -> a -> f b) -> v a -> f (v b)
imapM Int -> a -> f b
f = (ContVec (Dim v) b -> v b) -> f (ContVec (Dim v) b) -> f (v b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) b -> v b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
        (f (ContVec (Dim v) b) -> f (v b))
-> (v a -> f (ContVec (Dim v) b)) -> v a -> f (v b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> f b) -> ContVec (Dim v) a -> f (ContVec (Dim v) b)
forall (n :: Nat) (f :: * -> *) a b.
(Arity n, Applicative f) =>
(Int -> a -> f b) -> ContVec n a -> f (ContVec n b)
C.imapM Int -> a -> f b
f
        (ContVec (Dim v) a -> f (ContVec (Dim v) b))
-> (v a -> ContVec (Dim v) a) -> v a -> f (ContVec (Dim v) b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Apply monadic function to every element of the vector and its
--   index and discard result.
imapM_ :: (Vector v a, Applicative f) => (Int -> a -> f b) -> v a -> f ()
{-# INLINE imapM_ #-}
imapM_ :: (Int -> a -> f b) -> v a -> f ()
imapM_ Int -> a -> f b
f = (Int -> a -> f b) -> ContVec (Dim v) a -> f ()
forall (n :: Nat) (f :: * -> *) a b.
(Arity n, Applicative f) =>
(Int -> a -> f b) -> ContVec n a -> f ()
C.imapM_ Int -> a -> f b
f
         (ContVec (Dim v) a -> f ())
-> (v a -> ContVec (Dim v) a) -> v a -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Left scan over vector
scanl :: (Vector v a, Vector w b, Dim w ~ (Dim v + 1))
      => (b -> a -> b) -> b -> v a -> w b
{-# INLINE scanl #-}
scanl :: (b -> a -> b) -> b -> v a -> w b
scanl b -> a -> b
f b
x0 = ContVec (Dim v + 1) b -> w b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v + 1) b -> w b)
-> (v a -> ContVec (Dim v + 1) b) -> v a -> w b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> ContVec (Dim v) a -> ContVec (Dim v + 1) b
forall (n :: Nat) b a.
Arity n =>
(b -> a -> b) -> b -> ContVec n a -> ContVec (n + 1) b
C.scanl b -> a -> b
f b
x0 (ContVec (Dim v) a -> ContVec (Dim v + 1) b)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v + 1) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Left scan over vector
scanl1 :: (Vector v a)
      => (a -> a -> a) -> v a -> v a
{-# INLINE scanl1 #-}
scanl1 :: (a -> a -> a) -> v a -> v a
scanl1 a -> a -> a
f = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> (v a -> ContVec (Dim v) a) -> v a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> ContVec (Dim v) a -> ContVec (Dim v) a
forall (n :: Nat) a.
Arity n =>
(a -> a -> a) -> ContVec n a -> ContVec n a
C.scanl1 a -> a -> a
f (ContVec (Dim v) a -> ContVec (Dim v) a)
-> (v a -> ContVec (Dim v) a) -> v a -> ContVec (Dim v) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Analog of 'T.sequenceA' from 'T.Traversable'.
sequenceA :: (Vector v a, Vector v (f a), Applicative f)
          => v (f a) -> f (v a)
{-# INLINE sequenceA #-}
sequenceA :: v (f a) -> f (v a)
sequenceA = (ContVec (Dim v) a -> v a) -> f (ContVec (Dim v) a) -> f (v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (f (ContVec (Dim v) a) -> f (v a))
-> (v (f a) -> f (ContVec (Dim v) a)) -> v (f a) -> f (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContVec (Dim v) (f a) -> f (ContVec (Dim v) a)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
T.sequenceA (ContVec (Dim v) (f a) -> f (ContVec (Dim v) a))
-> (v (f a) -> ContVec (Dim v) (f a))
-> v (f a)
-> f (ContVec (Dim v) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v (f a) -> ContVec (Dim v) (f a)
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Analog of 'T.traverse' from 'T.Traversable'.
traverse :: (Vector v a, Vector v b, Applicative f)
          => (a -> f b) -> v a -> f (v b)
{-# INLINE traverse #-}
traverse :: (a -> f b) -> v a -> f (v b)
traverse a -> f b
f = (ContVec (Dim v) b -> v b) -> f (ContVec (Dim v) b) -> f (v b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) b -> v b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (f (ContVec (Dim v) b) -> f (v b))
-> (v a -> f (ContVec (Dim v) b)) -> v a -> f (v b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f b) -> ContVec (Dim v) a -> f (ContVec (Dim v) b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse a -> f b
f (ContVec (Dim v) a -> f (ContVec (Dim v) b))
-> (v a -> ContVec (Dim v) a) -> v a -> f (ContVec (Dim v) b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

distribute :: (Vector v a, Vector v (f a), Functor f)
           => f (v a) -> v (f a)
{-# INLINE distribute #-}
distribute :: f (v a) -> v (f a)
distribute = ContVec (Dim v) (f a) -> v (f a)
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) (f a) -> v (f a))
-> (f (v a) -> ContVec (Dim v) (f a)) -> f (v a) -> v (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (ContVec (Dim v) a) -> ContVec (Dim v) (f a)
forall (f :: * -> *) (n :: Nat) a.
(Functor f, Arity n) =>
f (ContVec n a) -> ContVec n (f a)
C.distribute (f (ContVec (Dim v) a) -> ContVec (Dim v) (f a))
-> (f (v a) -> f (ContVec (Dim v) a))
-> f (v a)
-> ContVec (Dim v) (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v a -> ContVec (Dim v) a) -> f (v a) -> f (ContVec (Dim v) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

collect :: (Vector v a, Vector v b, Vector v (f b), Functor f)
        => (a -> v b) -> f a -> v (f b)
{-# INLINE collect #-}
collect :: (a -> v b) -> f a -> v (f b)
collect a -> v b
f = ContVec (Dim v) (f b) -> v (f b)
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) (f b) -> v (f b))
-> (f a -> ContVec (Dim v) (f b)) -> f a -> v (f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ContVec (Dim v) b) -> f a -> ContVec (Dim v) (f b)
forall (f :: * -> *) (n :: Nat) a b.
(Functor f, Arity n) =>
(a -> ContVec n b) -> f a -> ContVec n (f b)
C.collect (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec (v b -> ContVec (Dim v) b) -> (a -> v b) -> a -> ContVec (Dim v) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> v b
f)



----------------------------------------------------------------

-- | Zip two vector together using function.
--
--   Examples:
--
--   >>> import Data.Vector.Fixed.Boxed (Vec3)
--   >>> let b0 = basis 0 :: Vec3 Int
--   >>> let b1 = basis 1 :: Vec3 Int
--   >>> let b2 = basis 2 :: Vec3 Int
--   >>> let vplus x y = zipWith (+) x y
--   >>> vplus b0 b1
--   fromList [1,1,0]
--   >>> vplus b0 b2
--   fromList [1,0,1]
--   >>> vplus b1 b2
--   fromList [0,1,1]
zipWith :: (Vector v a, Vector v b, Vector v c)
        => (a -> b -> c) -> v a -> v b -> v c
{-# INLINE zipWith #-}
zipWith :: (a -> b -> c) -> v a -> v b -> v c
zipWith a -> b -> c
f v a
v v b
u = ContVec (Dim v) c -> v c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
              (ContVec (Dim v) c -> v c) -> ContVec (Dim v) c -> v c
forall a b. (a -> b) -> a -> b
$ (a -> b -> c)
-> ContVec (Dim v) a -> ContVec (Dim v) b -> ContVec (Dim v) c
forall (n :: Nat) a b c.
Arity n =>
(a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
C.zipWith a -> b -> c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
u)

-- | Zip three vector together
zipWith3
  :: (Vector v a, Vector v b, Vector v c, Vector v d)
  => (a -> b -> c -> d)
  -> v a -> v b -> v c
  -> v d
{-# INLINE zipWith3 #-}
zipWith3 :: (a -> b -> c -> d) -> v a -> v b -> v c -> v d
zipWith3 a -> b -> c -> d
f v a
v1 v b
v2 v c
v3
  = ContVec (Dim v) d -> v d
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
  (ContVec (Dim v) d -> v d) -> ContVec (Dim v) d -> v d
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d)
-> ContVec (Dim v) a
-> ContVec (Dim v) b
-> ContVec (Dim v) c
-> ContVec (Dim v) d
forall (n :: Nat) a b c d.
Arity n =>
(a -> b -> c -> d)
-> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d
C.zipWith3 a -> b -> c -> d
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v1) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
v2) (v c -> ContVec (Dim v) c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v c
v3)

-- | Zip two vector together using monadic function.
zipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f)
         => (a -> b -> f c) -> v a -> v b -> f (v c)
{-# INLINE zipWithM #-}
zipWithM :: (a -> b -> f c) -> v a -> v b -> f (v c)
zipWithM a -> b -> f c
f v a
v v b
u = (ContVec (Dim v) c -> v c) -> f (ContVec (Dim v) c) -> f (v c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) c -> v c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
               (f (ContVec (Dim v) c) -> f (v c))
-> f (ContVec (Dim v) c) -> f (v c)
forall a b. (a -> b) -> a -> b
$ (a -> b -> f c)
-> ContVec (Dim v) a -> ContVec (Dim v) b -> f (ContVec (Dim v) c)
forall (n :: Nat) (f :: * -> *) a b c.
(Arity n, Applicative f) =>
(a -> b -> f c) -> ContVec n a -> ContVec n b -> f (ContVec n c)
C.zipWithM a -> b -> f c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
u)

-- | Zip two vector elementwise using monadic function and discard
--   result
zipWithM_
  :: (Vector v a, Vector v b, Applicative f)
  => (a -> b -> f c) -> v a -> v b -> f ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: (a -> b -> f c) -> v a -> v b -> f ()
zipWithM_ a -> b -> f c
f v a
xs v b
ys = (a -> b -> f c) -> ContVec (Dim v) a -> ContVec (Dim v) b -> f ()
forall (n :: Nat) (f :: * -> *) a b c.
(Arity n, Applicative f) =>
(a -> b -> f c) -> ContVec n a -> ContVec n b -> f ()
C.zipWithM_ a -> b -> f c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
xs) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
ys)

-- | Zip two vector together using function which takes element index
--   as well.
izipWith :: (Vector v a, Vector v b, Vector v c)
         => (Int -> a -> b -> c) -> v a -> v b -> v c
{-# INLINE izipWith #-}
izipWith :: (Int -> a -> b -> c) -> v a -> v b -> v c
izipWith Int -> a -> b -> c
f v a
v v b
u = ContVec (Dim v) c -> v c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
               (ContVec (Dim v) c -> v c) -> ContVec (Dim v) c -> v c
forall a b. (a -> b) -> a -> b
$ (Int -> a -> b -> c)
-> ContVec (Dim v) a -> ContVec (Dim v) b -> ContVec (Dim v) c
forall (n :: Nat) a b c.
Arity n =>
(Int -> a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
C.izipWith Int -> a -> b -> c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
u)

-- | Zip three vector together
izipWith3
  :: (Vector v a, Vector v b, Vector v c, Vector v d)
  => (Int -> a -> b -> c -> d)
  -> v a -> v b -> v c
  -> v d
{-# INLINE izipWith3 #-}
izipWith3 :: (Int -> a -> b -> c -> d) -> v a -> v b -> v c -> v d
izipWith3 Int -> a -> b -> c -> d
f v a
v1 v b
v2 v c
v3
  = ContVec (Dim v) d -> v d
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
  (ContVec (Dim v) d -> v d) -> ContVec (Dim v) d -> v d
forall a b. (a -> b) -> a -> b
$ (Int -> a -> b -> c -> d)
-> ContVec (Dim v) a
-> ContVec (Dim v) b
-> ContVec (Dim v) c
-> ContVec (Dim v) d
forall (n :: Nat) a b c d.
Arity n =>
(Int -> a -> b -> c -> d)
-> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d
C.izipWith3 Int -> a -> b -> c -> d
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v1) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
v2) (v c -> ContVec (Dim v) c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v c
v3)

-- | Zip two vector together using monadic function which takes element
--   index as well..
izipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f)
          => (Int -> a -> b -> f c) -> v a -> v b -> f (v c)
{-# INLINE izipWithM #-}
izipWithM :: (Int -> a -> b -> f c) -> v a -> v b -> f (v c)
izipWithM Int -> a -> b -> f c
f v a
v v b
u = (ContVec (Dim v) c -> v c) -> f (ContVec (Dim v) c) -> f (v c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) c -> v c
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector
                (f (ContVec (Dim v) c) -> f (v c))
-> f (ContVec (Dim v) c) -> f (v c)
forall a b. (a -> b) -> a -> b
$ (Int -> a -> b -> f c)
-> ContVec (Dim v) a -> ContVec (Dim v) b -> f (ContVec (Dim v) c)
forall (n :: Nat) (f :: * -> *) a b c.
(Arity n, Applicative f) =>
(Int -> a -> b -> f c)
-> ContVec n a -> ContVec n b -> f (ContVec n c)
C.izipWithM Int -> a -> b -> f c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
v) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
u)

-- | Zip two vector elementwise using monadic function and discard
--   result
izipWithM_
  :: (Vector v a, Vector v b, Vector v c, Applicative f, Vector v (f c))
  => (Int -> a -> b -> f c) -> v a -> v b -> f ()
{-# INLINE izipWithM_ #-}
izipWithM_ :: (Int -> a -> b -> f c) -> v a -> v b -> f ()
izipWithM_ Int -> a -> b -> f c
f v a
xs v b
ys = (Int -> a -> b -> f c)
-> ContVec (Dim v) a -> ContVec (Dim v) b -> f ()
forall (n :: Nat) (f :: * -> *) a b c.
(Arity n, Applicative f) =>
(Int -> a -> b -> f c) -> ContVec n a -> ContVec n b -> f ()
C.izipWithM_ Int -> a -> b -> f c
f (v a -> ContVec (Dim v) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v a
xs) (v b -> ContVec (Dim v) b
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec v b
ys)


----------------------------------------------------------------

-- | Default implementation of 'alignment' for 'Storable' type class
--   for fixed vectors.
defaultAlignemnt :: forall a v. Storable a => v a -> Int
defaultAlignemnt :: v a -> Int
defaultAlignemnt v a
_ = a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined :: a)
{-# INLINE defaultAlignemnt #-}

-- | Default implementation of 'sizeOf` for 'Storable' type class for
--   fixed vectors
defaultSizeOf
  :: forall a v. (Storable a, Vector v a)
  => v a -> Int
defaultSizeOf :: v a -> Int
defaultSizeOf v 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
* Proxy (Dim v) -> Int
forall (n :: Nat) (proxy :: Nat -> *). KnownNat n => proxy n -> Int
C.arity (Proxy (Dim v)
forall k (t :: k). Proxy t
Proxy :: Proxy (Dim v))
{-# INLINE defaultSizeOf #-}

-- | Default implementation of 'peek' for 'Storable' type class for
--   fixed vector
defaultPeek :: (Storable a, Vector v a) => Ptr (v a) -> IO (v a)
{-# INLINE defaultPeek #-}
defaultPeek :: Ptr (v a) -> IO (v a)
defaultPeek Ptr (v a)
ptr
  = (Int -> IO a) -> IO (v a)
forall (f :: * -> *) (v :: * -> *) a.
(Applicative f, Vector v a) =>
(Int -> f a) -> f (v a)
generateM (Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (Ptr (v a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (v a)
ptr))

-- | Default implementation of 'poke' for 'Storable' type class for
--   fixed vector
defaultPoke :: (Storable a, Vector v a) => Ptr (v a) -> v a -> IO ()
{-# INLINE defaultPoke #-}
defaultPoke :: Ptr (v a) -> v a -> IO ()
defaultPoke Ptr (v a)
ptr
  = (Int -> a -> IO ()) -> v a -> IO ()
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(Int -> a -> f b) -> v a -> f ()
imapM_ (Ptr a -> Int -> a -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (Ptr (v a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (v a)
ptr))

-- | Default implementation of 'rnf' from `NFData' type class
defaultRnf :: (NFData a, Vector v a) => v a -> ()
defaultRnf :: v a -> ()
defaultRnf = (() -> a -> ()) -> () -> v a -> ()
forall (v :: * -> *) a b.
Vector v a =>
(b -> a -> b) -> b -> v a -> b
foldl (\() a
a -> a -> ()
forall a. NFData a => a -> ()
rnf a
a) ()

----------------------------------------------------------------

-- | Convert between different vector types
convert :: (Vector v a, Vector w a, Dim v ~ Dim w) => v a -> w a
{-# INLINE convert #-}
convert :: v a -> w a
convert = ContVec (Dim w) a -> w a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim w) a -> w a)
-> (v a -> ContVec (Dim w) a) -> v a -> w a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> ContVec (Dim w) a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
v a -> ContVec n a
C.cvec

-- | Convert vector to the list
toList :: (Vector v a) => v a -> [a]
toList :: v a -> [a]
toList = (a -> [a] -> [a]) -> [a] -> v a -> [a]
forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
foldr (:) []
{-# INLINE toList #-}

-- | Create vector form list. Will throw error if list is shorter than
--   resulting vector.
fromList :: (Vector v a) => [a] -> v a
{-# INLINE fromList #-}
fromList :: [a] -> v a
fromList = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> ([a] -> ContVec (Dim v) a) -> [a] -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ContVec (Dim v) a
forall (n :: Nat) a. Arity n => [a] -> ContVec n a
C.fromList

-- | Create vector form list. Will throw error if list has different
--   length from resulting vector.
fromList' :: (Vector v a) => [a] -> v a
{-# INLINE fromList' #-}
fromList' :: [a] -> v a
fromList' = ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (ContVec (Dim v) a -> v a)
-> ([a] -> ContVec (Dim v) a) -> [a] -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ContVec (Dim v) a
forall (n :: Nat) a. Arity n => [a] -> ContVec n a
C.fromList'

-- | Create vector form list. Will return @Nothing@ if list has different
--   length from resulting vector.
fromListM :: (Vector v a) => [a] -> Maybe (v a)
{-# INLINE fromListM #-}
fromListM :: [a] -> Maybe (v a)
fromListM = (ContVec (Dim v) a -> v a)
-> Maybe (ContVec (Dim v) a) -> Maybe (v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ContVec (Dim v) a -> v a
forall (v :: * -> *) a (n :: Nat).
(Vector v a, Dim v ~ n) =>
ContVec n a -> v a
vector (Maybe (ContVec (Dim v) a) -> Maybe (v a))
-> ([a] -> Maybe (ContVec (Dim v) a)) -> [a] -> Maybe (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (ContVec (Dim v) a)
forall (n :: Nat) a. Arity n => [a] -> Maybe (ContVec n a)
C.fromListM

-- | Create vector from 'Foldable' data type. Will return @Nothing@ if
--   data type different number of elements that resulting vector.
fromFoldable :: (Vector v a, T.Foldable f) => f a -> Maybe (v a)
{-# INLINE fromFoldable #-}
fromFoldable :: f a -> Maybe (v a)
fromFoldable = [a] -> Maybe (v a)
forall (v :: * -> *) a. Vector v a => [a] -> Maybe (v a)
fromListM ([a] -> Maybe (v a)) -> (f a -> [a]) -> f a -> Maybe (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
T.toList

-- | Generic definition of 'Prelude.showsPrec'
showsPrec :: (Vector v a, Show a) => Int -> v a -> ShowS
showsPrec :: Int -> v a -> ShowS
showsPrec Int
d v a
v = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [a] -> ShowS
forall a. Show a => Int -> a -> ShowS
Prelude.showsPrec Int
11 (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
toList v a
v)
{-# INLINE showsPrec #-}