Generic Vector framework
[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.Base
5 ) where
6
7 import Data.Vector.Base
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 => Base Vector a where
22 {-# INLINE create #-}
23 create init = runST (do
24 Mut.Vector i n marr# <- init
25 ST (\s# -> case unsafeFreezeByteArray# marr# s# of
26 (# s2#, arr# #) -> (# s2#, Vector i n arr# #)
27 )
28 )
29
30 {-# INLINE length #-}
31 length (Vector _ n _) = n
32
33 {-# INLINE unsafeSlice #-}
34 unsafeSlice (Vector i _ arr#) j n = Vector (i+j) n arr#
35
36 {-# INLINE unsafeIndex #-}
37 unsafeIndex (Vector (I# i#) _ arr#) (I# j#) f = f (at# arr# (i# +# j#))
38