D.V.Unboxed => D.V.Primitive
[darcs-mirrors/vector.git] / Data / Vector / Primitive / Mutable / IO.hs
1 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
2
3 -- |
4 -- Module : Data.Vector.Mutable.Primitive.IO
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 primitive vectors in the IO monad.
13 --
14
15 module Data.Vector.Primitive.Mutable.IO ( Vector(..) )
16 where
17
18 import Data.Vector.MVector ( MVector(..), MVectorPure(..) )
19 import qualified Data.Vector.Primitive.Mutable.ST as STV
20 import Data.Vector.Primitive.Prim ( Prim )
21
22 import GHC.Base ( RealWorld )
23 import GHC.ST ( ST(..) )
24 import GHC.IOBase ( IO(..) )
25
26 import Prelude hiding ( length )
27
28 -- | IO-based mutable vectors
29 newtype Vector a = Vector (STV.Vector RealWorld a)
30
31 instance Prim a => MVectorPure Vector a where
32 {-# INLINE length #-}
33 length (Vector v) = length v
34
35 {-# INLINE unsafeSlice #-}
36 unsafeSlice (Vector v) j m = Vector (unsafeSlice v j m)
37
38 {-# INLINE overlaps #-}
39 overlaps (Vector v1) (Vector v2) = overlaps v1 v2
40
41 instance Prim a => MVector Vector IO a where
42 {-# INLINE unsafeNew #-}
43 unsafeNew n = Vector `fmap` stToIO (unsafeNew n)
44
45 {-# INLINE unsafeNewWith #-}
46 unsafeNewWith n x = Vector `fmap` stToIO (unsafeNewWith n x)
47
48 {-# INLINE unsafeRead #-}
49 unsafeRead (Vector v) i = stToIO (unsafeRead v i)
50
51 {-# INLINE unsafeWrite #-}
52 unsafeWrite (Vector v) i x = stToIO (unsafeWrite v i x)
53
54 {-# INLINE clear #-}
55 clear (Vector v) = stToIO (clear v)
56
57 stToIO :: ST RealWorld a -> IO a
58 stToIO (ST m) = IO m
59