summaryrefslogtreecommitdiff
path: root/lib/Data/Packed
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs43
1 files changed, 37 insertions, 6 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 9ca8e58..a6868d9 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -47,6 +47,22 @@ import GHC.Base
47import GHC.IOBase 47import GHC.IOBase
48#endif 48#endif
49 49
50#ifdef VECTOR
51import qualified Data.Vector.Storable as Vector
52import Data.Vector.Storable(Vector,
53 unsafeToForeignPtr,
54 unsafeFromForeignPtr,
55 unsafeWith)
56#endif
57
58#ifdef VECTOR
59
60-- | Number of elements
61dim :: (Storable t) => Vector t -> Int
62dim = Vector.length
63
64#else
65
50-- | One-dimensional array of objects stored in a contiguous memory block. 66-- | One-dimensional array of objects stored in a contiguous memory block.
51data Vector t = 67data Vector t =
52 V { ioff :: {-# UNPACK #-} !Int -- ^ offset of first element 68 V { ioff :: {-# UNPACK #-} !Int -- ^ offset of first element
@@ -65,11 +81,13 @@ unsafeFromForeignPtr fp i n | n > 0 = V {ioff = i, idim = n, fptr = fp}
65unsafeWith (V i _ fp) m = withForeignPtr fp $ \p -> m (p `advancePtr` i) 81unsafeWith (V i _ fp) m = withForeignPtr fp $ \p -> m (p `advancePtr` i)
66{-# INLINE unsafeWith #-} 82{-# INLINE unsafeWith #-}
67 83
68
69-- | Number of elements 84-- | Number of elements
70dim :: Vector t -> Int 85dim :: (Storable t) => Vector t -> Int
71dim = idim 86dim = idim
72 87
88#endif
89
90
73-- C-Haskell vector adapter 91-- C-Haskell vector adapter
74-- vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r 92-- vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r
75vec :: (Storable t) => Vector t -> (((CInt -> Ptr t -> t1) -> t1) -> IO b) -> IO b 93vec :: (Storable t) => Vector t -> (((CInt -> Ptr t -> t1) -> t1) -> IO b) -> IO b
@@ -85,7 +103,7 @@ createVector :: Storable a => Int -> IO (Vector a)
85createVector n = do 103createVector n = do
86 when (n <= 0) $ error ("trying to createVector of dim "++show n) 104 when (n <= 0) $ error ("trying to createVector of dim "++show n)
87 fp <- doMalloc undefined 105 fp <- doMalloc undefined
88 return $ V 0 n fp 106 return $ unsafeFromForeignPtr fp 0 n
89 where 107 where
90 -- 108 --
91 -- Use the much cheaper Haskell heap allocated storage 109 -- Use the much cheaper Haskell heap allocated storage
@@ -177,6 +195,12 @@ subVector :: Storable t => Int -- ^ index of the starting element
177 -> Vector t -- ^ source 195 -> Vector t -- ^ source
178 -> Vector t -- ^ result 196 -> Vector t -- ^ result
179 197
198#ifdef VECTOR
199
200subVector = Vector.slice
201
202#else
203
180subVector k l v@V{idim = n, ioff = i} 204subVector k l v@V{idim = n, ioff = i}
181 | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" 205 | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range"
182 | otherwise = v {idim = l, ioff = i+k} 206 | otherwise = v {idim = l, ioff = i+k}
@@ -189,6 +213,8 @@ subVectorCopy k l (v@V {idim=n})
189 app2 f vec v vec r "subVector" 213 app2 f vec v vec r "subVector"
190 return r 214 return r
191 215
216#endif
217
192{- | Reads a vector position: 218{- | Reads a vector position:
193 219
194@> fromList [0..9] \@\> 7 220@> fromList [0..9] \@\> 7
@@ -239,16 +265,21 @@ takesV ms w | sum ms > dim w = error $ "takesV " ++ show ms ++ " on dim = " ++ (
239 265
240-- | transforms a complex vector into a real vector with alternating real and imaginary parts 266-- | transforms a complex vector into a real vector with alternating real and imaginary parts
241asReal :: Vector (Complex Double) -> Vector Double 267asReal :: Vector (Complex Double) -> Vector Double
242asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) } 268--asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) }
269asReal v = unsafeFromForeignPtr (castForeignPtr fp) (2*i) (2*n)
270 where (fp,i,n) = unsafeToForeignPtr v
243 271
244-- | transforms a real vector into a complex vector with alternating real and imaginary parts 272-- | transforms a real vector into a complex vector with alternating real and imaginary parts
245asComplex :: Vector Double -> Vector (Complex Double) 273asComplex :: Vector Double -> Vector (Complex Double)
246asComplex v = V { ioff = ioff v `div` 2, idim = dim v `div` 2, fptr = castForeignPtr (fptr v) } 274--asComplex v = V { ioff = ioff v `div` 2, idim = dim v `div` 2, fptr = castForeignPtr (fptr v) }
275asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2)
276 where (fp,i,n) = unsafeToForeignPtr v
247 277
248---------------------------------------------------------------- 278----------------------------------------------------------------
249 279
250cloneVector :: Storable t => Vector t -> IO (Vector t) 280cloneVector :: Storable t => Vector t -> IO (Vector t)
251cloneVector (v@V {idim=n}) = do 281cloneVector v = do
282 let n = dim v
252 r <- createVector n 283 r <- createVector n
253 let f _ s _ d = copyArray d s n >> return 0 284 let f _ s _ d = copyArray d s n >> return 0
254 app2 f vec v vec r "cloneVector" 285 app2 f vec v vec r "cloneVector"