{-# LANGUAGE MagicHash, UnboxedTuples, BangPatterns, FlexibleContexts #-}
{-# LANGUAGE TypeSynonymInstances #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

-- |
-- Module      :  Internal.Vector
-- Copyright   :  (c) Alberto Ruiz 2007-15
-- License     :  BSD3
-- Maintainer  :  Alberto Ruiz
-- Stability   :  provisional
--

module Internal.Vector(
    I,Z,R,C,
    fi,ti,
    Vector, fromList, unsafeToForeignPtr, unsafeFromForeignPtr, unsafeWith,
    createVector, avec, inlinePerformIO,
    toList, dim, (@>), at', (|>),
    vjoin, subVector, takesV, idxs,
    buildVector,
    asReal, asComplex,
    toByteString,fromByteString,
    zipVector, unzipVector, zipVectorWith, unzipVectorWith,
    foldVector, foldVectorG, foldVectorWithIndex, foldLoop,
    mapVector, mapVectorM, mapVectorM_,
    mapVectorWithIndex, mapVectorWithIndexM, mapVectorWithIndexM_
) where

import Foreign.Marshal.Array
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable
import Foreign.C.Types(CInt)
import Data.Int(Int64)
import Data.Complex
import System.IO.Unsafe(unsafePerformIO)
import GHC.ForeignPtr(mallocPlainForeignPtrBytes)
import GHC.Base(realWorld#, IO(IO), when)
import qualified Data.Vector.Storable as Vector
import Data.Vector.Storable(Vector, fromList, unsafeToForeignPtr, unsafeFromForeignPtr, unsafeWith)

import Data.Binary
import Data.Binary.Put
import Control.Monad(replicateM)
import qualified Data.ByteString.Internal as BS
import Data.Vector.Storable.Internal(updPtr)

type I = CInt
type Z = Int64
type R = Double
type C = Complex Double


-- | specialized fromIntegral
fi :: Int -> CInt
fi :: Int -> CInt
fi = Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-- | specialized fromIntegral
ti :: CInt -> Int
ti :: CInt -> Int
ti = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral


-- | Number of elements
dim :: (Storable t) => Vector t -> Int
dim :: Vector t -> Int
dim = Vector t -> Int
forall a. Storable a => Vector a -> Int
Vector.length
{-# INLINE dim #-}


-- C-Haskell vector adapter
{-# INLINE avec #-}
avec :: Storable a => Vector a -> (f -> IO r) -> ((CInt -> Ptr a -> f) -> IO r)
avec :: Vector a -> (f -> IO r) -> (CInt -> Ptr a -> f) -> IO r
avec Vector a
v f -> IO r
f CInt -> Ptr a -> f
g = Vector a -> (Ptr a -> IO r) -> IO r
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO r) -> IO r) -> (Ptr a -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> f -> IO r
f (CInt -> Ptr a -> f
g (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector a -> Int
forall a. Storable a => Vector a -> Int
Vector.length Vector a
v)) Ptr a
ptr)

-- allocates memory for a new vector
createVector :: Storable a => Int -> IO (Vector a)
createVector :: Int -> IO (Vector a)
createVector Int
n = do
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char]
"trying to createVector of negative dim: "[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n)
    ForeignPtr a
fp <- a -> IO (ForeignPtr a)
forall b. Storable b => b -> IO (ForeignPtr b)
doMalloc a
forall a. HasCallStack => a
undefined
    Vector a -> IO (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> IO (Vector a)) -> Vector a -> IO (Vector a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> Int -> Int -> Vector a
forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr ForeignPtr a
fp Int
0 Int
n
  where
    --
    -- Use the much cheaper Haskell heap allocated storage
    -- for foreign pointer space we control
    --
    doMalloc :: Storable b => b -> IO (ForeignPtr b)
    doMalloc :: b -> IO (ForeignPtr b)
doMalloc b
dummy = do
        Int -> IO (ForeignPtr b)
forall a. Int -> IO (ForeignPtr a)
mallocPlainForeignPtrBytes (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* b -> Int
forall a. Storable a => a -> Int
sizeOf b
dummy)

{- | creates a Vector from a list:

@> fromList [2,3,5,7]
4 |> [2.0,3.0,5.0,7.0]@

-}

safeRead :: Storable a => Vector a -> (Ptr a -> IO c) -> c
safeRead :: Vector a -> (Ptr a -> IO c) -> c
safeRead Vector a
v = IO c -> c
forall a. IO a -> a
inlinePerformIO (IO c -> c) -> ((Ptr a -> IO c) -> IO c) -> (Ptr a -> IO c) -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> (Ptr a -> IO c) -> IO c
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v
{-# INLINE safeRead #-}

inlinePerformIO :: IO a -> a
inlinePerformIO :: IO a -> a
inlinePerformIO (IO State# RealWorld -> (# State# RealWorld, a #)
m) = case State# RealWorld -> (# State# RealWorld, a #)
m State# RealWorld
realWorld# of (# State# RealWorld
_, a
r #) -> a
r
{-# INLINE inlinePerformIO #-}

{- extracts the Vector elements to a list

>>> toList (linspace 5 (1,10))
[1.0,3.25,5.5,7.75,10.0]

-}
toList :: Storable a => Vector a -> [a]
toList :: Vector a -> [a]
toList Vector a
v = Vector a -> (Ptr a -> IO [a]) -> [a]
forall a c. Storable a => Vector a -> (Ptr a -> IO c) -> c
safeRead Vector a
v ((Ptr a -> IO [a]) -> [a]) -> (Ptr a -> IO [a]) -> [a]
forall a b. (a -> b) -> a -> b
$ Int -> Ptr a -> IO [a]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v)

{- | Create a vector from a list of elements and explicit dimension. The input
     list is truncated if it is too long, so it may safely
     be used, for instance, with infinite lists.

>>> 5 |> [1..]
[1.0,2.0,3.0,4.0,5.0]
it :: (Enum a, Num a, Foreign.Storable.Storable a) => Vector a

-}
(|>) :: (Storable a) => Int -> [a] -> Vector a
infixl 9 |>
Int
n |> :: Int -> [a] -> Vector a
|> [a]
l
    | [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
l' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = [a] -> Vector a
forall a. Storable a => [a] -> Vector a
fromList [a]
l'
    | Bool
otherwise      = [Char] -> Vector a
forall a. HasCallStack => [Char] -> a
error [Char]
"list too short for |>"
  where
    l' :: [a]
l' = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
l


-- | Create a vector of indexes, useful for matrix extraction using '(??)'
idxs :: [Int] -> Vector I
idxs :: [Int] -> Vector CInt
idxs [Int]
js = [CInt] -> Vector CInt
forall a. Storable a => [a] -> Vector a
fromList ((Int -> CInt) -> [Int] -> [CInt]
forall a b. (a -> b) -> [a] -> [b]
map Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral [Int]
js) :: Vector I

{- | takes a number of consecutive elements from a Vector

>>> subVector 2 3 (fromList [1..10])
[3.0,4.0,5.0]
it :: (Enum t, Num t, Foreign.Storable.Storable t) => Vector t

-}
subVector :: Storable t => Int       -- ^ index of the starting element
                        -> Int       -- ^ number of elements to extract
                        -> Vector t  -- ^ source
                        -> Vector t  -- ^ result
subVector :: Int -> Int -> Vector t -> Vector t
subVector = Int -> Int -> Vector t -> Vector t
forall a. Storable a => Int -> Int -> Vector a -> Vector a
Vector.slice
{-# INLINE subVector #-}




{- | Reads a vector position:

>>> fromList [0..9] @> 7
7.0

-}
(@>) :: Storable t => Vector t -> Int -> t
infixl 9 @>
Vector t
v @> :: Vector t -> Int -> t
@> Int
n
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
v = Vector t -> Int -> t
forall a. Storable a => Vector a -> Int -> a
at' Vector t
v Int
n
    | Bool
otherwise = [Char] -> t
forall a. HasCallStack => [Char] -> a
error [Char]
"vector index out of range"
{-# INLINE (@>) #-}

-- | access to Vector elements without range checking
at' :: Storable a => Vector a -> Int -> a
at' :: Vector a -> Int -> a
at' Vector a
v Int
n = Vector a -> (Ptr a -> IO a) -> a
forall a c. Storable a => Vector a -> (Ptr a -> IO c) -> c
safeRead Vector a
v ((Ptr a -> IO a) -> a) -> (Ptr a -> IO a) -> a
forall a b. (a -> b) -> a -> b
$ (Ptr a -> Int -> IO a) -> Int -> Ptr a -> IO a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Int
n
{-# INLINE at' #-}

{- | concatenate a list of vectors

>>> vjoin [fromList [1..5::Double], konst 1 3]
[1.0,2.0,3.0,4.0,5.0,1.0,1.0,1.0]
it :: Vector Double

-}
vjoin :: Storable t => [Vector t] -> Vector t
vjoin :: [Vector t] -> Vector t
vjoin [] = [t] -> Vector t
forall a. Storable a => [a] -> Vector a
fromList []
vjoin [Vector t
v] = Vector t
v
vjoin [Vector t]
as = IO (Vector t) -> Vector t
forall a. IO a -> a
unsafePerformIO (IO (Vector t) -> Vector t) -> IO (Vector t) -> Vector t
forall a b. (a -> b) -> a -> b
$ do
    let tot :: Int
tot = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Vector t -> Int) -> [Vector t] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Vector t -> Int
forall a. Storable a => Vector a -> Int
dim [Vector t]
as)
    Vector t
r <- Int -> IO (Vector t)
forall a. Storable a => Int -> IO (Vector a)
createVector Int
tot
    Vector t -> (Ptr t -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector t
r ((Ptr t -> IO ()) -> IO ()) -> (Ptr t -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr t
ptr ->
        [Vector t] -> Int -> Ptr t -> IO ()
forall t t.
(Num t, Storable t) =>
[Vector t] -> t -> Ptr t -> IO ()
joiner [Vector t]
as Int
tot Ptr t
ptr
    Vector t -> IO (Vector t)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector t
r
  where joiner :: [Vector t] -> t -> Ptr t -> IO ()
joiner [] t
_ Ptr t
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        joiner (Vector t
v:[Vector t]
cs) t
_ Ptr t
p = do
            let n :: Int
n = Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
v
            Vector t -> (Ptr t -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector t
v ((Ptr t -> IO ()) -> IO ()) -> (Ptr t -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr t
pb -> Ptr t -> Ptr t -> Int -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr t
p Ptr t
pb Int
n
            [Vector t] -> t -> Ptr t -> IO ()
joiner [Vector t]
cs t
0 (Ptr t -> Int -> Ptr t
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr t
p Int
n)


{- | Extract consecutive subvectors of the given sizes.

>>> takesV [3,4] (linspace 10 (1,10::Double))
[[1.0,2.0,3.0],[4.0,5.0,6.0,7.0]]
it :: [Vector Double]

-}
takesV :: Storable t => [Int] -> Vector t -> [Vector t]
takesV :: [Int] -> Vector t -> [Vector t]
takesV [Int]
ms Vector t
w | [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
ms Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
w = [Char] -> [Vector t]
forall a. HasCallStack => [Char] -> a
error ([Char] -> [Vector t]) -> [Char] -> [Vector t]
forall a b. (a -> b) -> a -> b
$ [Char]
"takesV " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Int] -> [Char]
forall a. Show a => a -> [Char]
show [Int]
ms [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" on dim = " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (Int -> [Char]
forall a. Show a => a -> [Char]
show (Int -> [Char]) -> Int -> [Char]
forall a b. (a -> b) -> a -> b
$ Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
w)
            | Bool
otherwise = [Int] -> Vector t -> [Vector t]
forall t. Storable t => [Int] -> Vector t -> [Vector t]
go [Int]
ms Vector t
w
    where go :: [Int] -> Vector t -> [Vector t]
go [] Vector t
_ = []
          go (Int
n:[Int]
ns) Vector t
v = Int -> Int -> Vector t -> Vector t
forall a. Storable a => Int -> Int -> Vector a -> Vector a
subVector Int
0 Int
n Vector t
v
                      Vector t -> [Vector t] -> [Vector t]
forall a. a -> [a] -> [a]
: [Int] -> Vector t -> [Vector t]
go [Int]
ns (Int -> Int -> Vector t -> Vector t
forall a. Storable a => Int -> Int -> Vector a -> Vector a
subVector Int
n (Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) Vector t
v)

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

-- | transforms a complex vector into a real vector with alternating real and imaginary parts
asReal :: (RealFloat a, Storable a) => Vector (Complex a) -> Vector a
asReal :: Vector (Complex a) -> Vector a
asReal Vector (Complex a)
v = ForeignPtr a -> Int -> Int -> Vector a
forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr (ForeignPtr (Complex a) -> ForeignPtr a
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr (Complex a)
fp) (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i) (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n)
    where (ForeignPtr (Complex a)
fp,Int
i,Int
n) = Vector (Complex a) -> (ForeignPtr (Complex a), Int, Int)
forall a. Storable a => Vector a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr Vector (Complex a)
v

-- | transforms a real vector into a complex vector with alternating real and imaginary parts
asComplex :: (RealFloat a, Storable a) => Vector a -> Vector (Complex a)
asComplex :: Vector a -> Vector (Complex a)
asComplex Vector a
v = ForeignPtr (Complex a) -> Int -> Int -> Vector (Complex a)
forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr (ForeignPtr a -> ForeignPtr (Complex a)
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr a
fp) (Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2)
    where (ForeignPtr a
fp,Int
i,Int
n) = Vector a -> (ForeignPtr a, Int, Int)
forall a. Storable a => Vector a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr Vector a
v

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


-- | map on Vectors
mapVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
mapVector :: (a -> b) -> Vector a -> Vector b
mapVector a -> b
f Vector a
v = IO (Vector b) -> Vector b
forall a. IO a -> a
unsafePerformIO (IO (Vector b) -> Vector b) -> IO (Vector b) -> Vector b
forall a b. (a -> b) -> a -> b
$ do
    Vector b
w <- Int -> IO (Vector b)
forall a. Storable a => Int -> IO (Vector a)
createVector (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v)
    Vector a -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
        Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr b
q -> do
            let go :: Int -> IO ()
go (-1) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                go !Int
k = do a
x <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                           Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff      Ptr b
q Int
k (a -> b
f a
x)
                           Int -> IO ()
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
            Int -> IO ()
go (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    Vector b -> IO (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector b
w
{-# INLINE mapVector #-}

-- | zipWith for Vectors
zipVectorWith :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
zipVectorWith :: (a -> b -> c) -> Vector a -> Vector b -> Vector c
zipVectorWith a -> b -> c
f Vector a
u Vector b
v = IO (Vector c) -> Vector c
forall a. IO a -> a
unsafePerformIO (IO (Vector c) -> Vector c) -> IO (Vector c) -> Vector c
forall a b. (a -> b) -> a -> b
$ do
    let n :: Int
n = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
u) (Vector b -> Int
forall a. Storable a => Vector a -> Int
dim Vector b
v)
    Vector c
w <- Int -> IO (Vector c)
forall a. Storable a => Int -> IO (Vector a)
createVector Int
n
    Vector a -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
u ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
pu ->
        Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
v ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr b
pv ->
            Vector c -> (Ptr c -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector c
w ((Ptr c -> IO ()) -> IO ()) -> (Ptr c -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr c
pw -> do
                let go :: Int -> IO ()
go (-1) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                    go !Int
k = do a
x <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
pu Int
k
                               b
y <- Ptr b -> Int -> IO b
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr b
pv Int
k
                               Ptr c -> Int -> c -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff      Ptr c
pw Int
k (a -> b -> c
f a
x b
y)
                               Int -> IO ()
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
                Int -> IO ()
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    Vector c -> IO (Vector c)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector c
w
{-# INLINE zipVectorWith #-}

-- | unzipWith for Vectors
unzipVectorWith :: (Storable (a,b), Storable c, Storable d)
                   => ((a,b) -> (c,d)) -> Vector (a,b) -> (Vector c,Vector d)
unzipVectorWith :: ((a, b) -> (c, d)) -> Vector (a, b) -> (Vector c, Vector d)
unzipVectorWith (a, b) -> (c, d)
f Vector (a, b)
u = IO (Vector c, Vector d) -> (Vector c, Vector d)
forall a. IO a -> a
unsafePerformIO (IO (Vector c, Vector d) -> (Vector c, Vector d))
-> IO (Vector c, Vector d) -> (Vector c, Vector d)
forall a b. (a -> b) -> a -> b
$ do
      let n :: Int
n = Vector (a, b) -> Int
forall a. Storable a => Vector a -> Int
dim Vector (a, b)
u
      Vector c
v <- Int -> IO (Vector c)
forall a. Storable a => Int -> IO (Vector a)
createVector Int
n
      Vector d
w <- Int -> IO (Vector d)
forall a. Storable a => Int -> IO (Vector a)
createVector Int
n
      Vector (a, b) -> (Ptr (a, b) -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector (a, b)
u ((Ptr (a, b) -> IO ()) -> IO ()) -> (Ptr (a, b) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (a, b)
pu ->
          Vector c -> (Ptr c -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector c
v ((Ptr c -> IO ()) -> IO ()) -> (Ptr c -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr c
pv ->
              Vector d -> (Ptr d -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector d
w ((Ptr d -> IO ()) -> IO ()) -> (Ptr d -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr d
pw -> do
                  let go :: Int -> IO ()
go (-1) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                      go !Int
k   = do (a, b)
z <- Ptr (a, b) -> Int -> IO (a, b)
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr (a, b)
pu Int
k
                                   let (c
x,d
y) = (a, b) -> (c, d)
f (a, b)
z
                                   Ptr c -> Int -> c -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff      Ptr c
pv Int
k c
x
                                   Ptr d -> Int -> d -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff      Ptr d
pw Int
k d
y
                                   Int -> IO ()
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
                  Int -> IO ()
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
      (Vector c, Vector d) -> IO (Vector c, Vector d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector c
v,Vector d
w)
{-# INLINE unzipVectorWith #-}

foldVector :: Storable a => (a -> b -> b) -> b -> Vector a -> b
foldVector :: (a -> b -> b) -> b -> Vector a -> b
foldVector a -> b -> b
f b
x Vector a
v = IO b -> b
forall a. IO a -> a
unsafePerformIO (IO b -> b) -> IO b -> b
forall a b. (a -> b) -> a -> b
$
    Vector a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> do
        let go :: Int -> b -> IO b
go (-1) b
s = b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s
            go !Int
k !b
s = do a
y <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                          Int -> b -> IO b
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1::Int) (a -> b -> b
f a
y b
s)
        Int -> b -> IO b
go (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) b
x
{-# INLINE foldVector #-}

-- the zero-indexed index is passed to the folding function
foldVectorWithIndex :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
foldVectorWithIndex :: (Int -> a -> b -> b) -> b -> Vector a -> b
foldVectorWithIndex Int -> a -> b -> b
f b
x Vector a
v = IO b -> b
forall a. IO a -> a
unsafePerformIO (IO b -> b) -> IO b -> b
forall a b. (a -> b) -> a -> b
$
    Vector a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> do
        let go :: Int -> b -> IO b
go (-1) b
s = b -> IO b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s
            go !Int
k !b
s = do a
y <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                          Int -> b -> IO b
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1::Int) (Int -> a -> b -> b
f Int
k a
y b
s)
        Int -> b -> IO b
go (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) b
x
{-# INLINE foldVectorWithIndex #-}

foldLoop :: (Int -> t -> t) -> t -> Int -> t
foldLoop :: (Int -> t -> t) -> t -> Int -> t
foldLoop Int -> t -> t
f t
s0 Int
d = Int -> t -> t
go (Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) t
s0
     where
       go :: Int -> t -> t
go Int
0 t
s = Int -> t -> t
f (Int
0::Int) t
s
       go !Int
j !t
s = Int -> t -> t
go (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> t -> t
f Int
j t
s)

foldVectorG :: Storable t1 => (Int -> (Int -> t1) -> t -> t) -> t -> Vector t1 -> t
foldVectorG :: (Int -> (Int -> t1) -> t -> t) -> t -> Vector t1 -> t
foldVectorG Int -> (Int -> t1) -> t -> t
f t
s0 Vector t1
v = (Int -> t -> t) -> t -> Int -> t
forall t. (Int -> t -> t) -> t -> Int -> t
foldLoop Int -> t -> t
g t
s0 (Vector t1 -> Int
forall a. Storable a => Vector a -> Int
dim Vector t1
v)
    where g :: Int -> t -> t
g !Int
k !t
s = Int -> (Int -> t1) -> t -> t
f Int
k (Vector t1 -> (Ptr t1 -> IO t1) -> t1
forall a c. Storable a => Vector a -> (Ptr a -> IO c) -> c
safeRead Vector t1
v ((Ptr t1 -> IO t1) -> t1) -> (Int -> Ptr t1 -> IO t1) -> Int -> t1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr t1 -> Int -> IO t1) -> Int -> Ptr t1 -> IO t1
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr t1 -> Int -> IO t1
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff) t
s
          {-# INLINE g #-} -- Thanks to Ryan Ingram (http://permalink.gmane.org/gmane.comp.lang.haskell.cafe/46479)
{-# INLINE foldVectorG #-}

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

-- | monadic map over Vectors
--    the monad @m@ must be strict
mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b)
mapVectorM :: (a -> m b) -> Vector a -> m (Vector b)
mapVectorM a -> m b
f Vector a
v = do
    Vector b
w <- Vector b -> m (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector b -> m (Vector b)) -> Vector b -> m (Vector b)
forall a b. (a -> b) -> a -> b
$! IO (Vector b) -> Vector b
forall a. IO a -> a
unsafePerformIO (IO (Vector b) -> Vector b) -> IO (Vector b) -> Vector b
forall a b. (a -> b) -> a -> b
$! Int -> IO (Vector b)
forall a. Storable a => Int -> IO (Vector a)
createVector (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v)
    Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w Int
0 (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    Vector b -> m (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector b
w
    where mapVectorM' :: Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w' !Int
k !Int
t
              | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
t               = do
                                       a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                       b
y <- a -> m b
f a
x
                                       () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> m ()) -> () -> m ()
forall a b. (a -> b) -> a -> b
$! IO () -> ()
forall a. IO a -> a
inlinePerformIO (IO () -> ()) -> IO () -> ()
forall a b. (a -> b) -> a -> b
$! Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w' ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$! \Ptr b
q -> Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr b
q Int
k b
y
              | Bool
otherwise            = do
                                       a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                       b
y <- a -> m b
f a
x
                                       ()
_ <- () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> m ()) -> () -> m ()
forall a b. (a -> b) -> a -> b
$! IO () -> ()
forall a. IO a -> a
inlinePerformIO (IO () -> ()) -> IO () -> ()
forall a b. (a -> b) -> a -> b
$! Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w' ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$! \Ptr b
q -> Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr b
q Int
k b
y
                                       Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w' (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
t
{-# INLINE mapVectorM #-}

-- | monadic map over Vectors
mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m ()
mapVectorM_ :: (a -> m ()) -> Vector a -> m ()
mapVectorM_ a -> m ()
f Vector a
v = do
    Int -> Int -> m ()
mapVectorM' Int
0 (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    where mapVectorM' :: Int -> Int -> m ()
mapVectorM' !Int
k !Int
t
              | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
t            = do
                                    a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                    a -> m ()
f a
x
              | Bool
otherwise         = do
                                    a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                    ()
_ <- a -> m ()
f a
x
                                    Int -> Int -> m ()
mapVectorM' (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
t
{-# INLINE mapVectorM_ #-}

-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
--    the monad @m@ must be strict
mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b)
mapVectorWithIndexM :: (Int -> a -> m b) -> Vector a -> m (Vector b)
mapVectorWithIndexM Int -> a -> m b
f Vector a
v = do
    Vector b
w <- Vector b -> m (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector b -> m (Vector b)) -> Vector b -> m (Vector b)
forall a b. (a -> b) -> a -> b
$! IO (Vector b) -> Vector b
forall a. IO a -> a
unsafePerformIO (IO (Vector b) -> Vector b) -> IO (Vector b) -> Vector b
forall a b. (a -> b) -> a -> b
$! Int -> IO (Vector b)
forall a. Storable a => Int -> IO (Vector a)
createVector (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v)
    Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w Int
0 (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    Vector b -> m (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector b
w
    where mapVectorM' :: Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w' !Int
k !Int
t
              | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
t               = do
                                       a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                       b
y <- Int -> a -> m b
f Int
k a
x
                                       () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> m ()) -> () -> m ()
forall a b. (a -> b) -> a -> b
$! IO () -> ()
forall a. IO a -> a
inlinePerformIO (IO () -> ()) -> IO () -> ()
forall a b. (a -> b) -> a -> b
$! Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w' ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$! \Ptr b
q -> Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr b
q Int
k b
y
              | Bool
otherwise            = do
                                       a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                       b
y <- Int -> a -> m b
f Int
k a
x
                                       ()
_ <- () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> m ()) -> () -> m ()
forall a b. (a -> b) -> a -> b
$! IO () -> ()
forall a. IO a -> a
inlinePerformIO (IO () -> ()) -> IO () -> ()
forall a b. (a -> b) -> a -> b
$! Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w' ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$! \Ptr b
q -> Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr b
q Int
k b
y
                                       Vector b -> Int -> Int -> m ()
mapVectorM' Vector b
w' (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
t
{-# INLINE mapVectorWithIndexM #-}

-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m ()
mapVectorWithIndexM_ :: (Int -> a -> m ()) -> Vector a -> m ()
mapVectorWithIndexM_ Int -> a -> m ()
f Vector a
v = do
    Int -> Int -> m ()
mapVectorM' Int
0 (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    where mapVectorM' :: Int -> Int -> m ()
mapVectorM' !Int
k !Int
t
              | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
t            = do
                                    a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                    Int -> a -> m ()
f Int
k a
x
              | Bool
otherwise         = do
                                    a
x <- a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! IO a -> a
forall a. IO a -> a
inlinePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$! Vector a -> (Ptr a -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$! \Ptr a
p -> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                                    ()
_ <- Int -> a -> m ()
f Int
k a
x
                                    Int -> Int -> m ()
mapVectorM' (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
t
{-# INLINE mapVectorWithIndexM_ #-}


mapVectorWithIndex :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
--mapVectorWithIndex g = head . mapVectorWithIndexM (\a b -> [g a b])
mapVectorWithIndex :: (Int -> a -> b) -> Vector a -> Vector b
mapVectorWithIndex Int -> a -> b
f Vector a
v = IO (Vector b) -> Vector b
forall a. IO a -> a
unsafePerformIO (IO (Vector b) -> Vector b) -> IO (Vector b) -> Vector b
forall a b. (a -> b) -> a -> b
$ do
    Vector b
w <- Int -> IO (Vector b)
forall a. Storable a => Int -> IO (Vector a)
createVector (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v)
    Vector a -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector a
v ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
        Vector b -> (Ptr b -> IO ()) -> IO ()
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith Vector b
w ((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr b
q -> do
            let go :: Int -> IO ()
go (-1) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                go !Int
k = do a
x <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
k
                           Ptr b -> Int -> b -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff      Ptr b
q Int
k (Int -> a -> b
f Int
k a
x)
                           Int -> IO ()
go (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
            Int -> IO ()
go (Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
    Vector b -> IO (Vector b)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector b
w
{-# INLINE mapVectorWithIndex #-}

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



-- a 64K cache, with a Double taking 13 bytes in Bytestring,
-- implies a chunk size of 5041
chunk :: Int
chunk :: Int
chunk = Int
5000

chunks :: Int -> [Int]
chunks :: Int -> [Int]
chunks Int
d = let c :: Int
c = Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
chunk
               m :: Int
m = Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
chunk
           in if Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 then [Int] -> [Int]
forall a. [a] -> [a]
reverse (Int
mInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:(Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
c Int
chunk)) else (Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
c Int
chunk)

putVector :: (Storable t, Binary t) => Vector t -> Data.Binary.Put.PutM ()
putVector :: Vector t -> PutM ()
putVector Vector t
v = (t -> PutM ()) -> [t] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ t -> PutM ()
forall t. Binary t => t -> PutM ()
put ([t] -> PutM ()) -> [t] -> PutM ()
forall a b. (a -> b) -> a -> b
$! Vector t -> [t]
forall a. Storable a => Vector a -> [a]
toList Vector t
v

getVector :: (Storable a, Binary a) => Int -> Get (Vector a)
getVector :: Int -> Get (Vector a)
getVector Int
d = do
              [a]
xs <- Int -> Get a -> Get [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
d Get a
forall t. Binary t => Get t
get
              Vector a -> Get (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> Get (Vector a)) -> Vector a -> Get (Vector a)
forall a b. (a -> b) -> a -> b
$! [a] -> Vector a
forall a. Storable a => [a] -> Vector a
fromList [a]
xs

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

toByteString :: Storable t => Vector t -> BS.ByteString
toByteString :: Vector t -> ByteString
toByteString Vector t
v = ForeignPtr Word8 -> Int -> Int -> ByteString
BS.PS (ForeignPtr t -> ForeignPtr Word8
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr t
fp) (Int
szInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
o) (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
* Vector t -> Int
forall a. Storable a => Vector a -> Int
dim Vector t
v)
  where
    (ForeignPtr t
fp,Int
o,Int
_n) = Vector t -> (ForeignPtr t, Int, Int)
forall a. Storable a => Vector a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr Vector t
v
    sz :: Int
sz = t -> Int
forall a. Storable a => a -> Int
sizeOf (Vector t
vVector t -> Int -> t
forall a. Storable a => Vector a -> Int -> a
@>Int
0)


fromByteString :: Storable t => BS.ByteString -> Vector t
fromByteString :: ByteString -> Vector t
fromByteString (BS.PS ForeignPtr Word8
fp Int
o Int
n) = Vector t
r
  where
    r :: Vector t
r = ForeignPtr t -> Int -> Int -> Vector t
forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr (ForeignPtr Word8 -> ForeignPtr t
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ((Ptr Word8 -> Ptr Word8) -> ForeignPtr Word8 -> ForeignPtr Word8
forall a. (Ptr a -> Ptr a) -> ForeignPtr a -> ForeignPtr a
updPtr (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o) ForeignPtr Word8
fp)) Int
0 Int
n'
    n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
sz
    sz :: Int
sz = t -> Int
forall a. Storable a => a -> Int
sizeOf (Vector t
rVector t -> Int -> t
forall a. Storable a => Vector a -> Int -> a
@>Int
0)

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

instance (Binary a, Storable a) => Binary (Vector a) where

    put :: Vector a -> PutM ()
put Vector a
v = do
            let d :: Int
d = Vector a -> Int
forall a. Storable a => Vector a -> Int
dim Vector a
v
            Int -> PutM ()
forall t. Binary t => t -> PutM ()
put Int
d
            (Vector a -> PutM ()) -> [Vector a] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Vector a -> PutM ()
forall t. (Storable t, Binary t) => Vector t -> PutM ()
putVector ([Vector a] -> PutM ()) -> [Vector a] -> PutM ()
forall a b. (a -> b) -> a -> b
$! [Int] -> Vector a -> [Vector a]
forall t. Storable t => [Int] -> Vector t -> [Vector t]
takesV (Int -> [Int]
chunks Int
d) Vector a
v

    -- put = put . v2bs

    get :: Get (Vector a)
get = do
          Int
d <- Get Int
forall t. Binary t => Get t
get
          [Vector a]
vs <- (Int -> Get (Vector a)) -> [Int] -> Get [Vector a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Int -> Get (Vector a)
forall a. (Storable a, Binary a) => Int -> Get (Vector a)
getVector ([Int] -> Get [Vector a]) -> [Int] -> Get [Vector a]
forall a b. (a -> b) -> a -> b
$ Int -> [Int]
chunks Int
d
          Vector a -> Get (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> Get (Vector a)) -> Vector a -> Get (Vector a)
forall a b. (a -> b) -> a -> b
$! [Vector a] -> Vector a
forall t. Storable t => [Vector t] -> Vector t
vjoin [Vector a]
vs

    -- get = fmap bs2v get



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

{- | creates a Vector of the specified length using the supplied function to
     to map the index to the value at that index.

@> buildVector 4 fromIntegral
4 |> [0.0,1.0,2.0,3.0]@

-}
buildVector :: Storable a => Int -> (Int -> a) -> Vector a
buildVector :: Int -> (Int -> a) -> Vector a
buildVector Int
len Int -> a
f =
    [a] -> Vector a
forall a. Storable a => [a] -> Vector a
fromList ([a] -> Vector a) -> [a] -> Vector a
forall a b. (a -> b) -> a -> b
$ (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
f [Int
0 .. (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)]


-- | zip for Vectors
zipVector :: (Storable a, Storable b, Storable (a,b)) => Vector a -> Vector b -> Vector (a,b)
zipVector :: Vector a -> Vector b -> Vector (a, b)
zipVector = (a -> b -> (a, b)) -> Vector a -> Vector b -> Vector (a, b)
forall a b c.
(Storable a, Storable b, Storable c) =>
(a -> b -> c) -> Vector a -> Vector b -> Vector c
zipVectorWith (,)

-- | unzip for Vectors
unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b)
unzipVector :: Vector (a, b) -> (Vector a, Vector b)
unzipVector = ((a, b) -> (a, b)) -> Vector (a, b) -> (Vector a, Vector b)
forall a b c d.
(Storable (a, b), Storable c, Storable d) =>
((a, b) -> (c, d)) -> Vector (a, b) -> (Vector c, Vector d)
unzipVectorWith (a, b) -> (a, b)
forall a. a -> a
id

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