Rename classes and modules
[darcs-mirrors/vector.git] / Data / Vector / Unboxed / Mutable.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, MultiParamTypeClasses, FlexibleInstances, GADTs, ScopedTypeVariables #-}
2
3 module Data.Vector.Unboxed.Mutable ( Vector(..) )
4 where
5
6 import qualified Data.Vector.MVector as MVector
7 import Data.Vector.MVector ( MVector )
8 import Data.Vector.Unboxed.Unbox
9
10 import GHC.Prim ( MutableByteArray#,
11 newByteArray#, sameMutableByteArray#, (+#) )
12
13 import GHC.ST ( ST(..) )
14
15 import GHC.Base ( Int(..) )
16
17 #ifndef __HADDOCK__
18 data Vector m a where
19 Vector :: {-# UNPACK #-} !Int
20 -> {-# UNPACK #-} !Int
21 -> MutableByteArray# s
22 -> Vector (ST s) a
23 #else
24 data Vector m a = forall s. Vector !Int !Int (MutableByteArray# s)
25 #endif
26
27 instance Unbox a => MVector Vector (ST s) a where
28 length (Vector _ n _) = n
29 unsafeSlice (Vector i _ arr#) j m = Vector (i+j) m arr#
30
31 {-# INLINE unsafeNew #-}
32 unsafeNew (I# n#) = ST (\s# ->
33 case newByteArray# (size# (undefined :: a) n#) s# of
34 (# s2#, arr# #) -> (# s2#, Vector 0 (I# n#) arr# #)
35 )
36
37 {-# INLINE unsafeRead #-}
38 unsafeRead (Vector (I# i#) _ arr#) (I# j#) = ST (read# arr# (i# +# j#))
39
40 {-# INLINE unsafeWrite #-}
41 unsafeWrite (Vector (I# i#) _ arr#) (I# j#) x = ST (\s# ->
42 case write# arr# (i# +# j#) x s# of s2# -> (# s2#, () #)
43 )
44
45 {-# INLINE overlaps #-}
46 overlaps (Vector i m arr1#) (Vector j n arr2#)
47 = sameMutableByteArray# arr1# arr2#
48 && (between i j (j+n) || between j i (i+m))
49 where
50 between x y z = x >= y && x < z
51