87bf1f681d3c78d32fe07be50f526d578ccba7b8
[darcs-mirrors/vector.git] / Data / Vector / Unboxed.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, FlexibleInstances, MultiParamTypeClasses #-}
2
3 module Data.Vector.Unboxed (
4 Vector(..), module Data.Vector.IVector
5 ) where
6
7 import Data.Vector.IVector
8 import qualified Data.Vector.Unboxed.Mutable as Mut
9 import Data.Vector.Unboxed.Unbox
10
11 import Control.Monad.ST ( runST )
12
13 import GHC.ST ( ST(..) )
14 import GHC.Prim ( ByteArray#, unsafeFreezeByteArray#, (+#) )
15 import GHC.Base ( Int(..) )
16
17 data Vector a = Vector {-# UNPACK #-} !Int
18 {-# UNPACK #-} !Int
19 ByteArray#
20
21 instance Unbox a => IVector Vector a where
22 {-# INLINE create #-}
23 create init = runST (do_create init)
24 where
25 do_create :: ST s (Mut.Vector (ST s) a) -> ST s (Vector a)
26 do_create init = do
27 Mut.Vector i n marr# <- init
28 ST (\s# -> case unsafeFreezeByteArray# marr# s# of
29 (# s2#, arr# #) -> (# s2#, Vector i n arr# #)
30 )
31
32 {-# INLINE length #-}
33 length (Vector _ n _) = n
34
35 {-# INLINE unsafeSlice #-}
36 unsafeSlice (Vector i _ arr#) j n = Vector (i+j) n arr#
37
38 {-# INLINE unsafeIndex #-}
39 unsafeIndex (Vector (I# i#) _ arr#) (I# j#) f = f (at# arr# (i# +# j#))
40