Mutable.Vector -> MVector everywhere
[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( MVector(..) )
16 where
17
18 import qualified Data.Vector.Generic.Mutable as G
19
20 import Foreign.Storable
21 import Foreign.ForeignPtr
22
23 -- | Mutable 'Storable'-based vectors in the 'IO' monad.
24 data MVector a = MVector {-# UNPACK #-} !Int
25 {-# UNPACK #-} !Int
26 {-# UNPACK #-} !(ForeignPtr a)
27
28 instance G.MVectorPure MVector a where
29 {-# INLINE length #-}
30 length (MVector _ n _) = n
31
32 {-# INLINE unsafeSlice #-}
33 unsafeSlice (MVector i _ p) j m = MVector (i+j) m p
34
35 -- FIXME: implement this properly
36 {-# INLINE overlaps #-}
37 overlaps (MVector i m p) (MVector j n q)
38 = True
39
40 instance Storable a => G.MVector MVector IO a where
41 {-# INLINE unsafeNew #-}
42 unsafeNew n = MVector 0 n `fmap` mallocForeignPtrArray n
43
44 {-# INLINE unsafeRead #-}
45 unsafeRead (MVector i n p) j = withForeignPtr p $ \ptr ->
46 peekElemOff ptr (i+j)
47
48 {-# INLINE unsafeWrite #-}
49 unsafeWrite (MVector i n p) j x = withForeignPtr p $ \ptr ->
50 pokeElemOff ptr (i+j) x
51
52 {-# INLINE clear #-}
53 clear _ = return ()
54