7d134e9fb9f8c1fa3dc80722caa5f3e99b5dc918
[darcs-mirrors/vector.git] / Data / Vector / Storable / Mutable.hs
1 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
2
3 -- |
4 -- Module : Data.Vector.Storable.Mutable
5 -- Copyright : (c) Roman Leshchinskiy 2009
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Mutable vectors based on Storable.
13 --
14
15 module Data.Vector.Storable.Mutable( Vector(..) )
16 where
17
18 import Data.Vector.Generic.Mutable ( MVector(..), MVectorPure(..) )
19
20 import Foreign.Storable
21 import Foreign.ForeignPtr
22
23 -- | Mutable 'Storable'-based vectors in the 'IO' monad.
24 data Vector a = Vector {-# UNPACK #-} !Int
25 {-# UNPACK #-} !Int
26 {-# UNPACK #-} !(ForeignPtr a)
27
28 instance MVectorPure Vector a where
29 {-# INLINE length #-}
30 length (Vector _ n _) = n
31
32 {-# INLINE unsafeSlice #-}
33 unsafeSlice (Vector i _ p) j m = Vector (i+j) m p
34
35 -- FIXME: implement this properly
36 {-# INLINE overlaps #-}
37 overlaps (Vector i m p) (Vector j n q)
38 = True
39
40 instance Storable a => MVector Vector IO a where
41 {-# INLINE unsafeNew #-}
42 unsafeNew n = Vector 0 n `fmap` mallocForeignPtrArray n
43
44 {-# INLINE unsafeRead #-}
45 unsafeRead (Vector i n p) j = withForeignPtr p $ \ptr ->
46 peekElemOff ptr (i+j)
47
48 {-# INLINE unsafeWrite #-}
49 unsafeWrite (Vector i n p) j x = withForeignPtr p $ \ptr ->
50 pokeElemOff ptr (i+j) x
51
52 {-# INLINE clear #-}
53 clear _ = return ()
54