summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Convert.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2008-06-03 09:00:37 +0000
committerAlberto Ruiz <aruiz@um.es>2008-06-03 09:00:37 +0000
commitd0fde9bdcb7cf490f7b067f0fa4d15b4f8fc3a61 (patch)
tree41ccf852f51c403d0dbe9d87aba4612604a31155 /lib/Data/Packed/Convert.hs
parentf2d385ab680809e08e7c73c70e996284e05eae8c (diff)
Data.Packed.Convert
Diffstat (limited to 'lib/Data/Packed/Convert.hs')
-rw-r--r--lib/Data/Packed/Convert.hs59
1 files changed, 59 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))