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