diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index bc623de..66acd87 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -15,7 +15,7 @@ | |||
15 | -- #hide | 15 | -- #hide |
16 | 16 | ||
17 | module Data.Packed.Internal.Vector ( | 17 | module Data.Packed.Internal.Vector ( |
18 | Vector(..), | 18 | Vector(..), dim, |
19 | fromList, toList, (|>), | 19 | fromList, toList, (|>), |
20 | join, (@>), safe, at, at', subVector, | 20 | join, (@>), safe, at, at', subVector, |
21 | mapVector, zipVector, | 21 | mapVector, zipVector, |
@@ -47,10 +47,14 @@ import GHC.IOBase | |||
47 | 47 | ||
48 | -- | A one-dimensional array of objects stored in a contiguous memory block. | 48 | -- | A one-dimensional array of objects stored in a contiguous memory block. |
49 | data Vector t = | 49 | data Vector t = |
50 | V { dim :: {-# UNPACK #-} !Int -- ^ number of elements | 50 | V { idim :: {-# UNPACK #-} !Int -- ^ number of elements |
51 | , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block | 51 | , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block |
52 | } | 52 | } |
53 | 53 | ||
54 | -- | Number of elements | ||
55 | dim :: Vector t -> Int | ||
56 | dim = idim | ||
57 | |||
54 | -- C-Haskell vector adapter | 58 | -- C-Haskell vector adapter |
55 | vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r | 59 | vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r |
56 | vec = withVector | 60 | vec = withVector |
@@ -157,7 +161,7 @@ subVector :: Storable t => Int -- ^ index of the starting element | |||
157 | -> Int -- ^ number of elements to extract | 161 | -> Int -- ^ number of elements to extract |
158 | -> Vector t -- ^ source | 162 | -> Vector t -- ^ source |
159 | -> Vector t -- ^ result | 163 | -> Vector t -- ^ result |
160 | subVector k l (v@V {dim=n}) | 164 | subVector k l (v@V {idim=n}) |
161 | | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" | 165 | | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" |
162 | | otherwise = unsafePerformIO $ do | 166 | | otherwise = unsafePerformIO $ do |
163 | r <- createVector l | 167 | r <- createVector l |
@@ -192,23 +196,23 @@ join as = unsafePerformIO $ do | |||
192 | joiner as tot ptr | 196 | joiner as tot ptr |
193 | return r | 197 | return r |
194 | where joiner [] _ _ = return () | 198 | where joiner [] _ _ = return () |
195 | joiner (V {dim = n, fptr = b} : cs) _ p = do | 199 | joiner (V {idim = n, fptr = b} : cs) _ p = do |
196 | withForeignPtr b $ \pb -> copyArray p pb n | 200 | withForeignPtr b $ \pb -> copyArray p pb n |
197 | joiner cs 0 (advancePtr p n) | 201 | joiner cs 0 (advancePtr p n) |
198 | 202 | ||
199 | 203 | ||
200 | -- | transforms a complex vector into a real vector with alternating real and imaginary parts | 204 | -- | transforms a complex vector into a real vector with alternating real and imaginary parts |
201 | asReal :: Vector (Complex Double) -> Vector Double | 205 | asReal :: Vector (Complex Double) -> Vector Double |
202 | asReal v = V { dim = 2*dim v, fptr = castForeignPtr (fptr v) } | 206 | asReal v = V { idim = 2*dim v, fptr = castForeignPtr (fptr v) } |
203 | 207 | ||
204 | -- | transforms a real vector into a complex vector with alternating real and imaginary parts | 208 | -- | transforms a real vector into a complex vector with alternating real and imaginary parts |
205 | asComplex :: Vector Double -> Vector (Complex Double) | 209 | asComplex :: Vector Double -> Vector (Complex Double) |
206 | asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v) } | 210 | asComplex v = V { idim = dim v `div` 2, fptr = castForeignPtr (fptr v) } |
207 | 211 | ||
208 | ---------------------------------------------------------------- | 212 | ---------------------------------------------------------------- |
209 | 213 | ||
210 | cloneVector :: Storable t => Vector t -> IO (Vector t) | 214 | cloneVector :: Storable t => Vector t -> IO (Vector t) |
211 | cloneVector (v@V {dim=n}) = do | 215 | cloneVector (v@V {idim=n}) = do |
212 | r <- createVector n | 216 | r <- createVector n |
213 | let f _ s _ d = copyArray d s n >> return 0 | 217 | let f _ s _ d = copyArray d s n >> return 0 |
214 | app2 f vec v vec r "cloneVector" | 218 | app2 f vec v vec r "cloneVector" |