From d0fde9bdcb7cf490f7b067f0fa4d15b4f8fc3a61 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 3 Jun 2008 09:00:37 +0000 Subject: Data.Packed.Convert --- lib/Data/Packed/Convert.hs | 59 ++++++++++++++++++++++++++++++++++++++ lib/Data/Packed/Internal/Vector.hs | 9 ++++++ 2 files changed, 68 insertions(+) create mode 100644 lib/Data/Packed/Convert.hs (limited to 'lib/Data') diff --git a/lib/Data/Packed/Convert.hs b/lib/Data/Packed/Convert.hs new file mode 100644 index 0000000..55fdf8f --- /dev/null +++ b/lib/Data/Packed/Convert.hs @@ -0,0 +1,59 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Data.Packed.Convert +-- Copyright : (c) Alberto Ruiz 2008 +-- License : GPL-style +-- +-- Maintainer : Alberto Ruiz +-- Stability : provisional +-- Portability : portable +-- +-- Conversion of Vectors and Matrices to and from the standard Haskell arrays. +-- +----------------------------------------------------------------------------- + +module Data.Packed.Convert ( + vectorToStorableArray, + storableArrayToVector, +-- unsafeUpdateVector, +) where + +import Data.Packed.Internal +import Data.Array.Storable +import Foreign + +-- | Creates a StorableArray indexed from 0 to dim -1. +-- (Memory is efficiently copied, so you can then freely modify the obtained array) +vectorToStorableArray :: Storable t => Vector t -> IO (StorableArray Int t) +vectorToStorableArray v = do + r <- cloneVector v + unsafeForeignPtrToStorableArray (fptr r) (0,dim r -1) + +-- | Creates a Vector from a StorableArray. +-- (Memory is efficiently copied, so posterior changes in the array will not affect the result) +storableArrayToVector :: Storable t => StorableArray Int t -> IO (Vector t) +storableArrayToVector s = do + (a,b) <- getBounds s + let n = (b-a+1) + r <- createVector n + withStorableArray s $ \p -> do + let f _ d = copyArray d p n >> return 0 + app1 f vec r "storableArrayToVector" + return r + + +unsafeVectorToStorableArray :: Storable t => Vector t -> IO (StorableArray Int t) +unsafeVectorToStorableArray v = unsafeForeignPtrToStorableArray (fptr v) (0,dim v -1) + +--unsafeStorableArrayToVector :: Storable t => StorableArray Int t -> IO (Vector t) +--unsafeStorableArrayToVector s = undefined -- the foreign ptr of Storable Array is not available? + +----------------------------------------------------------------- + + +unsafeUpdateVector :: Storable t => Int -- ^ position + -> (t->t) -- ^ modification function + -> Vector t -- ^ source + -> IO () -- ^ result +unsafeUpdateVector k h v = do + withForeignPtr (fptr v) $ \s -> pokeElemOff s k (h (v`at'`k)) diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 5a42f9d..772aafd 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -166,3 +166,12 @@ updateVector k h (v@V {dim=n}) >> return 0 app2 f vec v vec r "updateVector" return r + +----------------------------------------------------------------- + +cloneVector :: Storable t => Vector t -> IO (Vector t) +cloneVector (v@V {dim=n}) = do + r <- createVector n + let f _ s _ d = copyArray d s n >> return 0 + app2 f vec v vec r "cloneVector" + return r -- cgit v1.2.3