Ticket #3181: Vector.hs

File Vector.hs, 1.1 KB (added by dolio, 4 years ago)

A minimal implementation of Data.Array.Vector for this test.

Line 
1{-# LANGUAGE TypeFamilies, TypeOperators, MagicHash, UnboxedTuples #-}
2
3module Data.Array.Vector (UA(..), (:*:)(..)) where
4
5import Control.Monad.ST
6
7import GHC.ST
8import GHC.Prim
9import GHC.Exts
10
11data a :*: b = !a :*: !b
12
13class UA e where
14  data MUArr e :: * -> *
15  newMU    :: Int -> ST s (MUArr e s)
16  lengthMU :: MUArr e s -> Int
17  readMU   :: MUArr e s -> Int -> ST s e
18  writeMU  :: MUArr e s -> Int -> e -> ST s ()
19
20instance UA Int where
21  data MUArr Int s = IArr {-# UNPACK #-} !Int {-# UNPACK #-} !(MutableByteArray# s)
22 
23  {-# INLINE newMU #-}
24  newMU i@(I# i#) = ST (\s -> case newByteArray# (i# *# 4#) s of
25                              (# s', arr #) -> (# s', IArr i arr #))
26 
27  {-# INLINE lengthMU #-}
28  lengthMU (IArr i _) = i
29 
30  {-# INLINE readMU #-}
31  readMU (IArr _ arr) (I# i#) = ST (\s -> case readIntArray# arr i# s of
32                                          (# s', j# #) -> (# s', I# j# #))
33 
34  {-# INLINE writeMU #-}
35  writeMU (IArr _ arr) (I# i#) (I# j#) = ST (\s -> case writeIntArray# arr i# j# s of
36                                                   s' -> (# s', () #))