Haddock comments
[darcs-mirrors/vector.git] / Data / Vector / Unboxed / Unbox.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples #-}
2
3 -- |
4 -- Module : Data.Vector.Unboxed.Unbox
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 -- Primitives for manipulating unboxed arrays
13 --
14
15 module Data.Vector.Unboxed.Unbox (
16 Unbox(..)
17 ) where
18
19 import GHC.Base (
20 Int(..)
21 )
22 import GHC.Float (
23 Float(..), Double(..)
24 )
25
26 import GHC.Prim (
27 ByteArray#, MutableByteArray#, State#,
28
29 Int#, indexIntArray#, readIntArray#, writeIntArray#,
30 indexFloatArray#, readFloatArray#, writeFloatArray#,
31 indexDoubleArray#, readDoubleArray#, writeDoubleArray#
32 )
33 import Data.Array.Base (
34 wORD_SCALE, fLOAT_SCALE, dOUBLE_SCALE
35 )
36
37 -- | Class of types which can be stored in unboxed arrays
38 class Unbox a where
39 -- | Yield the size in bytes of a 'ByteArray#' which can store @n@ elements
40 size# :: a -- ^ Dummy type parameter, never evaluated
41 -> Int# -- ^ Number of elements
42 -> Int#
43
44 -- | Indexing
45 at# :: ByteArray# -> Int# -> a
46
47 -- | Yield the element at the given position
48 read# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
49
50 -- | Store the given element at the given position
51 write# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s
52
53 instance Unbox Int where
54 size# _ = wORD_SCALE
55 at# arr# i# = I# (indexIntArray# arr# i#)
56 read# arr# i# s# = case readIntArray# arr# i# s# of
57 (# s1#, n# #) -> (# s1#, I# n# #)
58 write# arr# i# (I# n#) s# = writeIntArray# arr# i# n# s#
59
60 instance Unbox Float where
61 size# _ = fLOAT_SCALE
62 at# arr# i# = F# (indexFloatArray# arr# i#)
63 read# arr# i# s# = case readFloatArray# arr# i# s# of
64 (# s1#, x# #) -> (# s1#, F# x# #)
65 write# arr# i# (F# x#) s# = writeFloatArray# arr# i# x# s#
66
67 instance Unbox Double where
68 size# _ = dOUBLE_SCALE
69 at# arr# i# = D# (indexDoubleArray# arr# i#)
70 read# arr# i# s# = case readDoubleArray# arr# i# s# of
71 (# s1#, x# #) -> (# s1#, D# x# #)
72 write# arr# i# (D# x#) s# = writeDoubleArray# arr# i# x# s#
73