summaryrefslogtreecommitdiff
path: root/lib/Data/Packed
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r--lib/Data/Packed/Convert.hs59
-rw-r--r--lib/Data/Packed/Internal/Vector.hs9
2 files changed, 68 insertions, 0 deletions
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 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Convert
4-- Copyright : (c) Alberto Ruiz 2008
5-- License : GPL-style
6--
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional
9-- Portability : portable
10--
11-- Conversion of Vectors and Matrices to and from the standard Haskell arrays.
12--
13-----------------------------------------------------------------------------
14
15module Data.Packed.Convert (
16 vectorToStorableArray,
17 storableArrayToVector,
18-- unsafeUpdateVector,
19) where
20
21import Data.Packed.Internal
22import Data.Array.Storable
23import Foreign
24
25-- | Creates a StorableArray indexed from 0 to dim -1.
26-- (Memory is efficiently copied, so you can then freely modify the obtained array)
27vectorToStorableArray :: Storable t => Vector t -> IO (StorableArray Int t)
28vectorToStorableArray v = do
29 r <- cloneVector v
30 unsafeForeignPtrToStorableArray (fptr r) (0,dim r -1)
31
32-- | Creates a Vector from a StorableArray.
33-- (Memory is efficiently copied, so posterior changes in the array will not affect the result)
34storableArrayToVector :: Storable t => StorableArray Int t -> IO (Vector t)
35storableArrayToVector s = do
36 (a,b) <- getBounds s
37 let n = (b-a+1)
38 r <- createVector n
39 withStorableArray s $ \p -> do
40 let f _ d = copyArray d p n >> return 0
41 app1 f vec r "storableArrayToVector"
42 return r
43
44
45unsafeVectorToStorableArray :: Storable t => Vector t -> IO (StorableArray Int t)
46unsafeVectorToStorableArray v = unsafeForeignPtrToStorableArray (fptr v) (0,dim v -1)
47
48--unsafeStorableArrayToVector :: Storable t => StorableArray Int t -> IO (Vector t)
49--unsafeStorableArrayToVector s = undefined -- the foreign ptr of Storable Array is not available?
50
51-----------------------------------------------------------------
52
53
54unsafeUpdateVector :: Storable t => Int -- ^ position
55 -> (t->t) -- ^ modification function
56 -> Vector t -- ^ source
57 -> IO () -- ^ result
58unsafeUpdateVector k h v = do
59 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})
166 >> return 0 166 >> return 0
167 app2 f vec v vec r "updateVector" 167 app2 f vec v vec r "updateVector"
168 return r 168 return r
169
170-----------------------------------------------------------------
171
172cloneVector :: Storable t => Vector t -> IO (Vector t)
173cloneVector (v@V {dim=n}) = do
174 r <- createVector n
175 let f _ s _ d = copyArray d s n >> return 0
176 app2 f vec v vec r "cloneVector"
177 return r