module Data.Packed.Vector (
Vector,
fromList, (|>), toList, buildVector,
dim, (@>),
subVector, takesV, join,
mapVector, zipVector, zipVectorWith, unzipVector, unzipVectorWith,
mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_,
foldLoop, foldVector, foldVectorG, foldVectorWithIndex
) where
import Data.Packed.Internal.Vector
import Foreign.Storable
#ifdef BINARY
import Data.Binary
import Control.Monad(replicateM)
chunk :: Int
chunk = 5000
chunks :: Int -> [Int]
chunks d = let c = d `div` chunk
m = d `mod` chunk
in if m /= 0 then reverse (m:(replicate c chunk)) else (replicate c chunk)
putVector v = do
let d = dim v
mapM_ (\i -> put $ v @> i) [0..(d1)]
getVector d = do
xs <- replicateM d get
return $! fromList xs
instance (Binary a, Storable a) => Binary (Vector a) where
put v = do
let d = dim v
put d
mapM_ putVector $! takesV (chunks d) v
get = do
d <- get
vs <- mapM getVector $ chunks d
return $! join vs
#endif
buildVector :: Storable a => Int -> (Int -> a) -> Vector a
buildVector len f =
fromList $ map f [0 .. (len 1)]
zipVector :: (Storable a, Storable b, Storable (a,b)) => Vector a -> Vector b -> Vector (a,b)
zipVector = zipVectorWith (,)
unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b)
unzipVector = unzipVectorWith id