Fusible length
[darcs-mirrors/vector.git] / Data / Vector / Unboxed.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, FlexibleInstances, MultiParamTypeClasses #-}
2
3 -- |
4 -- Module : Data.Vector.Unboxed
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 -- Unboxed vectors based on 'Unbox'.
13 --
14
15 module Data.Vector.Unboxed (
16 Vector(..), module Data.Vector.IVector
17 ) where
18
19 import Data.Vector.IVector
20 import qualified Data.Vector.Unboxed.Mutable as Mut
21 import Data.Vector.Unboxed.Unbox
22
23 import Control.Monad.ST ( runST )
24
25 import GHC.ST ( ST(..) )
26 import GHC.Prim ( ByteArray#, unsafeFreezeByteArray#, (+#) )
27 import GHC.Base ( Int(..) )
28
29 data Vector a = Vector {-# UNPACK #-} !Int
30 {-# UNPACK #-} !Int
31 ByteArray#
32
33 instance Unbox a => IVector Vector a where
34 {-# INLINE create #-}
35 create init = runST (do_create init)
36 where
37 do_create :: ST s (Mut.Vector (ST s) a) -> ST s (Vector a)
38 do_create init = do
39 Mut.Vector i n marr# <- init
40 ST (\s# -> case unsafeFreezeByteArray# marr# s# of
41 (# s2#, arr# #) -> (# s2#, Vector i n arr# #)
42 )
43
44 {-# INLINE vlength #-}
45 vlength (Vector _ n _) = n
46
47 {-# INLINE unsafeSlice #-}
48 unsafeSlice (Vector i _ arr#) j n = Vector (i+j) n arr#
49
50 {-# INLINE unsafeIndex #-}
51 unsafeIndex (Vector (I# i#) _ arr#) (I# j#) f = f (at# arr# (i# +# j#))
52