module Data.PrimitiveArray where
import Control.Monad.Primitive (PrimMonad)
import Control.Exception (assert)
import Text.Read.Lex as L
import Text.Read
import Text.ParserCombinators.ReadP
class PrimArrayOps a b where
data PrimArray a b :: *
unsafeIndex :: PrimArray a b -> a -> b
assocs :: PrimArray a b -> [(a,b)]
fromAssocs :: a -> a -> b -> [(a,b)] -> PrimArray a b
bounds :: PrimArray a b -> (a,a)
checkBounds :: PrimArray a b -> a -> Bool
fromList :: a -> a -> [b] -> PrimArray a b
toList :: PrimArray a b -> [b]
class (PrimMonad s) => PrimArrayOpsM a b s where
data PrimArrayM a b s :: *
readM :: PrimArrayM a b s -> a -> s b
writeM :: PrimArrayM a b s -> a -> b -> s ()
boundsM :: PrimArrayM a b s -> s (a,a)
fromAssocsM :: a -> a -> b -> [(a,b)] -> s (PrimArrayM a b s)
unsafeFreezeM :: PrimArrayM a b s -> s (PrimArray a b)
fromListM :: a -> a -> [b] -> s (PrimArrayM a b s)
toListM :: PrimArrayM a b s -> s [b]
(!) :: (PrimArrayOps a b) => PrimArray a b -> a -> b
(!) pa idx = assert (checkBounds pa idx) $ unsafeIndex pa idx
amap :: (PrimArrayOps a b, PrimArrayOps a c) => (b -> c) -> PrimArray a b -> PrimArray a c
amap f pa = fromList lb ub $ map f $ toList pa where
(lb,ub) = bounds pa
instance (Bounded a, Show a, Show b, PrimArrayOps a b) => Show (PrimArray a b) where
show pa = "fromList " ++ show l ++ " " ++ show u ++ " " ++ (show $ toList pa) ++ " :: Data.PrimitiveArray.PrimitiveArray" where
(l,u) = bounds pa
instance (Bounded a, Read a, Read b, PrimArrayOps a b) => Read (PrimArray a b) where
readPrec =
parens $ do
lift $ skipSpaces
L.Ident "fromList" <- lexP
lift $ skipSpaces
(l :: a) <- readPrec
lift $ skipSpaces
(u :: a) <- readPrec
(vals :: [b]) <- readPrec
lift $ skipSpaces
lift $ string "::"
lift $ skipSpaces
lift $ string "Data.PrimitiveArray.PrimitiveArray"
lift $ skipSpaces
return $ fromList l u vals
readListPrec = readListPrecDefault
readList = readListDefault