| 1 | {-# LANGUAGE TypeFamilies, TypeOperators, MagicHash, UnboxedTuples #-} |
|---|
| 2 | |
|---|
| 3 | module Data.Array.Vector (UA(..), (:*:)(..)) where |
|---|
| 4 | |
|---|
| 5 | import Control.Monad.ST |
|---|
| 6 | |
|---|
| 7 | import GHC.ST |
|---|
| 8 | import GHC.Prim |
|---|
| 9 | import GHC.Exts |
|---|
| 10 | |
|---|
| 11 | data a :*: b = !a :*: !b |
|---|
| 12 | |
|---|
| 13 | class 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 | |
|---|
| 20 | instance 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', () #)) |
|---|