More Haddock comments
[darcs-mirrors/vector.git] / Data / Vector / Unboxed / Mutable.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, MultiParamTypeClasses, FlexibleInstances, GADTs, ScopedTypeVariables #-}
2
3 -- |
4 -- Module : Data.Vector.Unboxed.Mutable
5 -- Copyright : (c) Roman Leshchinskiy 2008
6 -- License : BSD-style
7 --
8 -- Maintainer : rl@cse.unsw.edu.au
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Mutable unboxed vectors based on 'Unbox'.
13 --
14
15 module Data.Vector.Unboxed.Mutable ( Vector(..) )
16 where
17
18 import qualified Data.Vector.MVector as MVector
19 import Data.Vector.MVector ( MVector )
20 import Data.Vector.Unboxed.Unbox
21
22 import GHC.Prim ( MutableByteArray#,
23 newByteArray#, sameMutableByteArray#, (+#) )
24
25 import GHC.ST ( ST(..) )
26
27 import GHC.Base ( Int(..) )
28
29 #ifndef __HADDOCK__
30 data Vector m a where
31 Vector :: {-# UNPACK #-} !Int
32 -> {-# UNPACK #-} !Int
33 -> MutableByteArray# s
34 -> Vector (ST s) a
35 #else
36 -- | Type of mutable unboxed vectors. This is actually a GADT:
37 --
38 -- > data Vector m a where
39 -- > Vector :: !Int -> !Int -> MutableByteArray# s -> Vector (ST s) a
40 --
41 data Vector m a = forall s. Vector !Int !Int (MutableByteArray# s)
42 #endif
43
44 instance Unbox a => MVector Vector (ST s) a where
45 length (Vector _ n _) = n
46 unsafeSlice (Vector i _ arr#) j m = Vector (i+j) m arr#
47
48 {-# INLINE unsafeNew #-}
49 unsafeNew (I# n#) = ST (\s# ->
50 case newByteArray# (size# (undefined :: a) n#) s# of
51 (# s2#, arr# #) -> (# s2#, Vector 0 (I# n#) arr# #)
52 )
53
54 {-# INLINE unsafeRead #-}
55 unsafeRead (Vector (I# i#) _ arr#) (I# j#) = ST (read# arr# (i# +# j#))
56
57 {-# INLINE unsafeWrite #-}
58 unsafeWrite (Vector (I# i#) _ arr#) (I# j#) x = ST (\s# ->
59 case write# arr# (i# +# j#) x s# of s2# -> (# s2#, () #)
60 )
61
62 {-# INLINE overlaps #-}
63 overlaps (Vector i m arr1#) (Vector j n arr2#)
64 = sameMutableByteArray# arr1# arr2#
65 && (between i j (j+n) || between j i (i+m))
66 where
67 between x y z = x >= y && x < z
68