More flexible size hints
[darcs-mirrors/vector.git] / Data / Vector / Unboxed / Unbox.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples #-}
2
3 module Data.Vector.Unboxed.Unbox (
4 Unbox(..)
5 ) where
6
7 import GHC.Base (
8 Int(..)
9 )
10 import GHC.Float (
11 Float(..), Double(..)
12 )
13
14 import GHC.Prim (
15 ByteArray#, MutableByteArray#, State#,
16
17 Int#, indexIntArray#, readIntArray#, writeIntArray#,
18 indexFloatArray#, readFloatArray#, writeFloatArray#,
19 indexDoubleArray#, readDoubleArray#, writeDoubleArray#
20 )
21 import Data.Array.Base (
22 wORD_SCALE, fLOAT_SCALE, dOUBLE_SCALE
23 )
24
25 class Unbox a where
26 size# :: a -> Int# -> Int#
27 at# :: ByteArray# -> Int# -> a
28 read# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
29 write# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s
30
31 instance Unbox Int where
32 size# _ = wORD_SCALE
33 at# arr# i# = I# (indexIntArray# arr# i#)
34 read# arr# i# s# = case readIntArray# arr# i# s# of
35 (# s1#, n# #) -> (# s1#, I# n# #)
36 write# arr# i# (I# n#) s# = writeIntArray# arr# i# n# s#
37
38 instance Unbox Float where
39 size# _ = fLOAT_SCALE
40 at# arr# i# = F# (indexFloatArray# arr# i#)
41 read# arr# i# s# = case readFloatArray# arr# i# s# of
42 (# s1#, x# #) -> (# s1#, F# x# #)
43 write# arr# i# (F# x#) s# = writeFloatArray# arr# i# x# s#
44
45 instance Unbox Double where
46 size# _ = dOUBLE_SCALE
47 at# arr# i# = D# (indexDoubleArray# arr# i#)
48 read# arr# i# s# = case readDoubleArray# arr# i# s# of
49 (# s1#, x# #) -> (# s1#, D# x# #)
50 write# arr# i# (D# x#) s# = writeDoubleArray# arr# i# x# s#
51