diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index f590919..dd9b9b6 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# LANGUAGE MagicHash, CPP, UnboxedTuples #-} | 1 | {-# LANGUAGE MagicHash, CPP, UnboxedTuples, BangPatterns #-} |
2 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
3 | -- | | 3 | -- | |
4 | -- Module : Data.Packed.Internal.Vector | 4 | -- Module : Data.Packed.Internal.Vector |
@@ -182,7 +182,7 @@ asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v) } | |||
182 | 182 | ||
183 | -- | map on Vectors | 183 | -- | map on Vectors |
184 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b | 184 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b |
185 | liftVector f = fromList . map f . toList | 185 | liftVector = mapVector |
186 | 186 | ||
187 | -- | zipWith for Vectors | 187 | -- | zipWith for Vectors |
188 | liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c | 188 | liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c |
@@ -196,3 +196,35 @@ cloneVector (v@V {dim=n}) = do | |||
196 | let f _ s _ d = copyArray d s n >> return 0 | 196 | let f _ s _ d = copyArray d s n >> return 0 |
197 | app2 f vec v vec r "cloneVector" | 197 | app2 f vec v vec r "cloneVector" |
198 | return r | 198 | return r |
199 | |||
200 | ------------------------------------------------------------------ | ||
201 | |||
202 | mapVector f v = unsafePerformIO $ do | ||
203 | w <- createVector (dim v) | ||
204 | withForeignPtr (fptr v) $ \p -> | ||
205 | withForeignPtr (fptr w) $ \q -> do | ||
206 | let go (-1) = return () | ||
207 | go !k = do x <- peekElemOff p k | ||
208 | pokeElemOff q k (f x) | ||
209 | go (k-1) | ||
210 | go (dim v -1) | ||
211 | return w | ||
212 | {-# INLINE mapVector #-} | ||
213 | |||
214 | foldVector f x v = unsafePerformIO $ | ||
215 | withForeignPtr (fptr (v::Vector Double)) $ \p -> do | ||
216 | let go (-1) s = return s | ||
217 | go !k !s = do y <- peekElemOff p k | ||
218 | go (k-1::Int) (f y s) | ||
219 | go (dim v -1) x | ||
220 | {-# INLINE foldVector #-} | ||
221 | |||
222 | foldLoop f s0 d = go (d - 1) s0 | ||
223 | where | ||
224 | go 0 s = f (0::Int) s | ||
225 | go !j !s = go (j - 1) (f j s) | ||
226 | |||
227 | foldVectorG f s0 v = foldLoop g s0 (dim v) | ||
228 | where g !k !s = f k (at' v) s | ||
229 | {-# INLINE g #-} -- Thanks to Ryan Ingram (http://permalink.gmane.org/gmane.comp.lang.haskell.cafe/46479) | ||
230 | {-# INLINE foldVectorG #-} | ||