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