    {-# 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 -> I
    fi = Int -> I
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral

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

    -- | Number of elements
    dim :: (Storable t) => Vector t -> Int
    dim :: forall t. Storable t => Vector t -> Int
    dim = Vector t -> Int
    forall t. Storable t => Vector t -> 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 :: forall a f r.
    Storable a =>
    Vector a -> (f -> IO r) -> (I -> Ptr a -> f) -> IO r
    avec Vector a
    v f -> IO r
    f I -> 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 (I -> Ptr a -> f
    g (Int -> I
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Vector a -> Int
    forall t. Storable t => Vector t -> Int
    Vector.length Vector a
    v)) Ptr a
    ptr)

    -- allocates memory for a new vector
    createVector :: Storable a => Int -> IO (Vector a)
    createVector :: forall a. Storable a => 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)
        fp <- a -> IO (ForeignPtr a)
    forall b. Storable b => b -> IO (ForeignPtr b)
    doMalloc a
    forall a. HasCallStack => a
    undefined
        return $ unsafeFromForeignPtr fp 0 n
      where
        --
        -- Use the much cheaper Haskell heap allocated storage
        -- for foreign pointer space we control
        --
        doMalloc :: Storable b => b -> IO (ForeignPtr b)
        doMalloc :: forall b. Storable b => 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 :: forall a c. Storable a => 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 :: forall a. 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 :: forall a. Storable a => 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 t. Storable t => Vector t -> 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 |> :: forall a. Storable a => Int -> [a] -> Vector a
    |> [a]
    l
        | [a] -> Int
    forall a. [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 I
    idxs [Int]
    js = [I] -> Vector I
    forall a. Storable a => [a] -> Vector a
    fromList ((Int -> I) -> [Int] -> [I]
    forall a b. (a -> b) -> [a] -> [b]
    map Int -> I
    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 :: forall t. Storable t => Int -> Int -> Vector t -> Vector t
    subVector = Int -> Int -> Vector t -> Vector t
    forall t. Storable t => Int -> Int -> Vector t -> Vector t
    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 @> :: forall t. Storable t => 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 t. Storable t => Vector t -> Int
    dim Vector t
    v = Vector t -> Int -> t
    forall t. Storable t => Vector t -> Int -> t
    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' :: forall t. Storable t => Vector t -> Int -> t
    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 :: forall t. Storable t => [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 a. Num a => [a] -> a
    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 t. Storable t => Vector t -> Int
    dim [Vector t]
    as)
        r <- Int -> IO (Vector t)
    forall a. Storable a => Int -> IO (Vector a)
    createVector Int
    tot
        unsafeWith r $ \Ptr t
    ptr ->
            [Vector t] -> Int -> Ptr t -> IO ()
    forall {t} {a}.
    (Num t, Storable a) =>
    [Vector a] -> t -> Ptr a -> IO ()
    joiner [Vector t]
    as Int
    tot Ptr t
    ptr
        return r
      where joiner :: [Vector a] -> t -> Ptr a -> IO ()
    joiner [] t
    _ Ptr a
    _ = () -> IO ()
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
            joiner (Vector a
    v:[Vector a]
    cs) t
    _ Ptr a
    p = do
                let n :: Int
    n = Vector a -> Int
    forall t. Storable t => Vector t -> 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
    pb -> Ptr a -> Ptr a -> Int -> IO ()
    forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
    copyArray Ptr a
    p Ptr a
    pb Int
    n
                [Vector a] -> t -> Ptr a -> IO ()
    joiner [Vector a]
    cs t
    0 (Ptr a -> Int -> Ptr a
    forall a. Storable a => Ptr a -> Int -> Ptr a
    advancePtr Ptr a
    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 :: forall t. Storable t => [Int] -> Vector t -> [Vector t]
    takesV [Int]
    ms Vector t
    w | [Int] -> Int
    forall a. Num a => [a] -> a
    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 t. Storable t => Vector t -> 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 t. Storable t => Vector t -> 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 t. Storable t => Int -> Int -> Vector t -> Vector t
    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 t. Storable t => Int -> Int -> Vector t -> Vector t
    subVector Int
    n (Vector t -> Int
    forall t. Storable t => Vector t -> 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 :: forall a.
    (RealFloat a, Storable a) =>
    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. 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 :: forall a.
    (RealFloat a, Storable a) =>
    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. 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 :: forall a b.
    (Storable a, Storable b) =>
    (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
        w <- Int -> IO (Vector b)
    forall a. Storable a => Int -> IO (Vector a)
    createVector (Vector a -> Int
    forall t. Storable t => Vector t -> Int
    dim Vector a
    v)
        unsafeWith v $ \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 a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
                    go !Int
    k = do x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    p Int
    k
                               pokeElemOff      q k (f x)
                               go (k-1)
                Int -> IO ()
    go (Vector a -> Int
    forall t. Storable t => Vector t -> Int
    dim Vector a
    v Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    1)
        return w
    {-# INLINE mapVector #-}

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

    foldVector :: Storable a => (a -> b -> b) -> b -> Vector a -> b
    foldVector :: forall a b. Storable a => (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 a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return b
    s
                go !Int
    k !b
    s = do y <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    p Int
    k
                              go (k-1::Int) (f y s)
            Int -> b -> IO b
    go (Vector a -> Int
    forall t. Storable t => Vector t -> 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 :: forall a b.
    Storable a =>
    (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 a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return b
    s
                go !Int
    k !b
    s = do y <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    p Int
    k
                              go (k-1::Int) (f k y s)
            Int -> b -> IO b
    go (Vector a -> Int
    forall t. Storable t => Vector t -> 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 :: forall t. (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 :: forall t1 t.
    Storable t1 =>
    (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 t. Storable t => Vector t -> 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 :: forall a b (m :: * -> *).
    (Storable a, Storable b, Monad m) =>
    (a -> m b) -> Vector a -> m (Vector b)
    mapVectorM a -> m b
    f Vector a
    v = do
        w <- Vector b -> m (Vector b)
    forall a. a -> m a
    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 t. Storable t => Vector t -> Int
    dim Vector a
    v)
        mapVectorM' w 0 (dim v -1)
        return 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
                                           x <- a -> m a
    forall a. 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
                                           y <- f x
                                           return $! inlinePerformIO $! unsafeWith w' $! \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
                                           x <- a -> m a
    forall a. 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
                                           y <- f x
                                           _ <- return $! inlinePerformIO $! unsafeWith w' $! \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
                                           mapVectorM' w' (k+1) t
    {-# INLINE mapVectorM #-}

    -- | monadic map over Vectors
    mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m ()
    mapVectorM_ :: forall a (m :: * -> *).
    (Storable a, Monad m) =>
    (a -> m ()) -> Vector a -> m ()
    mapVectorM_ a -> m ()
    f Vector a
    v = do
        Int -> Int -> m ()
    mapVectorM' Int
    0 (Vector a -> Int
    forall t. Storable t => Vector t -> 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
                                        x <- a -> m a
    forall a. 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
                                        f x
                  | Bool
    otherwise         = do
                                        x <- a -> m a
    forall a. 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
                                        _ <- f x
                                        mapVectorM' (k+1) 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 :: forall a b (m :: * -> *).
    (Storable a, Storable b, Monad m) =>
    (Int -> a -> m b) -> Vector a -> m (Vector b)
    mapVectorWithIndexM Int -> a -> m b
    f Vector a
    v = do
        w <- Vector b -> m (Vector b)
    forall a. a -> m a
    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 t. Storable t => Vector t -> Int
    dim Vector a
    v)
        mapVectorM' w 0 (dim v -1)
        return 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
                                           x <- a -> m a
    forall a. 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
                                           y <- f k x
                                           return $! inlinePerformIO $! unsafeWith w' $! \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
                                           x <- a -> m a
    forall a. 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
                                           y <- f k x
                                           _ <- return $! inlinePerformIO $! unsafeWith w' $! \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
                                           mapVectorM' w' (k+1) 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_ :: forall a (m :: * -> *).
    (Storable a, Monad m) =>
    (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 t. Storable t => Vector t -> 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
                                        x <- a -> m a
    forall a. 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
                                        f k x
                  | Bool
    otherwise         = do
                                        x <- a -> m a
    forall a. 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
                                        _ <- f k x
                                        mapVectorM' (k+1) 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 :: forall a b.
    (Storable a, Storable b) =>
    (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
        w <- Int -> IO (Vector b)
    forall a. Storable a => Int -> IO (Vector a)
    createVector (Vector a -> Int
    forall t. Storable t => Vector t -> Int
    dim Vector a
    v)
        unsafeWith v $ \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 a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
                    go !Int
    k = do x <- Ptr a -> Int -> IO a
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff Ptr a
    p Int
    k
                               pokeElemOff      q k (f k x)
                               go (k-1)
                Int -> IO ()
    go (Vector a -> Int
    forall t. Storable t => Vector t -> Int
    dim Vector a
    v Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    1)
        return 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 :: forall t. (Storable t, Binary t) => 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 :: forall a. (Storable a, Binary a) => Int -> Get (Vector a)
    getVector Int
    d = do
                  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
                  return $! fromList xs

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

    toByteString :: Storable t => Vector t -> BS.ByteString
    toByteString :: forall t. Storable t => 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 t. Storable t => Vector t -> Int
    dim Vector t
    v)
      where
        (ForeignPtr t
    fp,Int
    o,Int
    _n) = Vector t -> (ForeignPtr t, Int, Int)
    forall 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 t. Storable t => Vector t -> Int -> t
    @>Int
    0)

    fromByteString :: Storable t => BS.ByteString -> Vector t
    fromByteString :: forall t. Storable t => 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 t. Storable t => Vector t -> Int -> t
    @>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 t. Storable t => Vector t -> 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
              d <- Get Int
    forall t. Binary t => Get t
    get
              vs <- mapM getVector $ chunks d
              return $! vjoin 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 :: forall a. Storable a => 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 :: forall a b.
    (Storable a, Storable b, Storable (a, b)) =>
    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 :: forall a b.
    (Storable a, Storable b, Storable (a, b)) =>
    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

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