Array implementation, @PrelArr@ exports the basic array
types and operations.
+For byte-arrays see @PrelByteArr@.
+
\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
import PrelList (foldl)
import PrelST
import PrelBase
-import PrelCCall
import PrelAddr
import PrelGHC
+import PrelShow
infixl 9 !, //
+
+default ()
\end{code}
\begin{code}
-{-# GENERATE_SPECS array a{~,Int,IPr} b{} #-}
+{-# SPECIALISE array :: (Int,Int) -> [(Int,b)] -> Array Int b #-}
array :: (Ix a) => (a,a) -> [(a,b)] -> Array a b
-{-# GENERATE_SPECS (!) a{~,Int,IPr} b{} #-}
+{-# SPECIALISE (!) :: Array Int b -> Int -> b #-}
(!) :: (Ix a) => Array a b -> a -> b
-bounds :: (Ix a) => Array a b -> (a,a)
-
-{-# GENERATE_SPECS (//) a{~,Int,IPr} b{} #-}
+{-# SPECIALISE (//) :: Array Int b -> [(Int,b)] -> Array Int b #-}
(//) :: (Ix a) => Array a b -> [(a,b)] -> Array a b
-{-# GENERATE_SPECS accum a{~,Int,IPr} b{} c{} #-}
+{-# SPECIALISE accum :: (b -> c -> b) -> Array Int b -> [(Int,c)] -> Array Int b #-}
accum :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b
-{-# GENERATE_SPECS accumArray a{~,Int,IPr} b{} c{} #-}
+{-# SPECIALISE accumArray :: (b -> c -> b) -> b -> (Int,Int) -> [(Int,c)] -> Array Int b #-}
accumArray :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
+
+bounds :: (Ix a) => Array a b -> (a,a)
+assocs :: (Ix a) => Array a b -> [(a,b)]
+indices :: (Ix a) => Array a b -> [a]
\end{code}
\begin{code}
type IPr = (Int, Int)
-data Ix ix => Array ix elt = Array (ix,ix) (Array# elt)
-data Ix ix => ByteArray ix = ByteArray (ix,ix) ByteArray#
-data Ix ix => MutableArray s ix elt = MutableArray (ix,ix) (MutableArray# s elt)
-data Ix ix => MutableByteArray s ix = MutableByteArray (ix,ix) (MutableByteArray# s)
+data Ix ix => Array ix elt = Array ix ix (Array# elt)
+data Ix ix => MutableArray s ix elt = MutableArray ix ix (MutableArray# s elt)
-instance CCallable (MutableByteArray s ix)
-instance CCallable (MutableByteArray# s)
-instance CCallable (ByteArray ix)
-instance CCallable ByteArray#
+data MutableVar s a = MutableVar (MutVar# s a)
--- A one-element mutable array:
-type MutableVar s a = MutableArray s Int a
+instance Eq (MutableVar s a) where
+ MutableVar v1# == MutableVar v2#
+ = sameMutVar# v1# v2#
-- just pointer equality on arrays:
instance Eq (MutableArray s ix elt) where
- MutableArray _ arr1# == MutableArray _ arr2#
+ MutableArray _ _ arr1# == MutableArray _ _ arr2#
= sameMutableArray# arr1# arr2#
-
-instance Eq (MutableByteArray s ix) where
- MutableByteArray _ arr1# == MutableByteArray _ arr2#
- = sameMutableByteArray# arr1# arr2#
\end{code}
%*********************************************************
writeVar :: MutableVar s a -> a -> ST s ()
newVar init = ST $ \ s# ->
- case (newArray# 1# init s#) of { StateAndMutableArray# s2# arr# ->
- STret s2# (MutableArray vAR_IXS arr#) }
- where
- vAR_IXS = error "newVar: Shouldn't access `bounds' of a MutableVar\n"
+ case (newMutVar# init s#) of { (# s2#, var# #) ->
+ (# s2#, MutableVar var# #) }
-readVar (MutableArray _ var#) = ST $ \ s# ->
- case readArray# var# 0# s# of { StateAndPtr# s2# r ->
- STret s2# r }
+readVar (MutableVar var#) = ST $ \ s# -> readMutVar# var# s#
-writeVar (MutableArray _ var#) val = ST $ \ s# ->
- case writeArray# var# 0# val s# of { s2# ->
- STret s2# () }
+writeVar (MutableVar var#) val = ST $ \ s# ->
+ case writeMutVar# var# val s# of { s2# ->
+ (# s2#, () #) }
\end{code}
%*********************************************************
"array", "!" and "bounds" are basic; the rest can be defined in terms of them
\begin{code}
-bounds (Array b _) = b
+{-# INLINE bounds #-}
+bounds (Array l u _) = (l,u)
-(Array bounds arr#) ! i
- = let n# = case (index bounds i) of { I# x -> x } -- index fails if out of range
+{-# INLINE assocs #-} -- Want to fuse the list comprehension
+assocs a = [(i, a!i) | i <- indices a]
+
+{-# INLINE indices #-}
+indices = range . bounds
+
+{-# SPECIALISE amap :: (b -> c) -> Array Int b -> Array Int c #-}
+amap :: (Ix a) => (b -> c) -> Array a b -> Array a c
+amap f a = array b [(i, f (a!i)) | i <- range b]
+ where b = bounds a
+
+(Array l u arr#) ! i
+ = let n# = case (index (l,u) i) of { I# x -> x } -- index fails if out of range
in
case (indexArray# arr# n#) of
- Lift v -> v
+ (# v #) -> v
-#ifdef USE_FOLDR_BUILD
{-# INLINE array #-}
-#endif
-array ixs@(ix_start, ix_end) ivs =
- runST ( ST $ \ s ->
- case (newArray ixs arrEleBottom) of { ST new_array_thing ->
- case (new_array_thing s) of { STret s# arr@(MutableArray _ arr#) ->
- let
- fill_in s# [] = s#
- fill_in s# ((i,v):ivs) =
- case (index ixs i) of { I# n# ->
- case writeArray# arr# n# v s# of { s2# ->
- fill_in s2# ivs }}
- in
-
- case (fill_in s# ivs) of { s# ->
- case (freezeArray arr) of { ST freeze_array_thing ->
- freeze_array_thing s# }}}})
-
+array ixs ivs
+ = case rangeSize ixs of { I# n ->
+ runST ( ST $ \ s1 ->
+ case newArray# n arrEleBottom s1 of { (# s2, marr #) ->
+ foldr (fill ixs marr) (done ixs marr) ivs s2
+ })}
+
+fill :: Ix ix => (ix,ix) -> MutableArray# s elt
+ -> (ix,elt) -> STRep s a -> STRep s a
+{-# INLINE fill #-}
+fill ixs marr (i,v) next = \s1 -> case index ixs i of { I# n ->
+ case writeArray# marr n v s1 of { s2 ->
+ next s2 }}
+
+done :: Ix ix => (ix,ix) -> MutableArray# s elt
+ -> STRep s (Array ix elt)
+{-# INLINE done #-}
+done (l,u) marr = \s1 ->
+ case unsafeFreezeArray# marr s1 of { (# s2, arr #) ->
+ (# s2, Array l u arr #) }
+
+arrEleBottom :: a
arrEleBottom = error "(Array.!): undefined array element"
-fill_it_in :: Ix ix => MutableArray s ix elt -> [(ix, elt)] -> ST s ()
-fill_it_in arr lst
- = foldr fill_one_in (return ()) lst
- where -- **** STRICT **** (but that's OK...)
- fill_one_in (i, v) rst
- = writeArray arr i v >> rst
-----------------------------------------------------------------------
--- these also go better with magic: (//), accum, accumArray
+-- These also go better with magic: (//), accum, accumArray
+-- *** NB *** We INLINE them all so that their foldr's get to the call site
+{-# INLINE (//) #-}
old_array // ivs
= runST (do
-- copy the old array:
fill_it_in arr ivs
freezeArray arr
)
- where
- bottom = error "(Array.//): error in copying old array\n"
+
+fill_it_in :: Ix ix => MutableArray s ix elt -> [(ix, elt)] -> ST s ()
+{-# INLINE fill_it_in #-}
+fill_it_in arr lst = foldr (fill_one_in arr) (return ()) lst
+ -- **** STRICT **** (but that's OK...)
+
+fill_one_in arr (i, v) rst = writeArray arr i v >> rst
zap_with_f :: Ix ix => (elt -> elt2 -> elt) -> MutableArray s ix elt -> [(ix,elt2)] -> ST s ()
-- zap_with_f: reads an elem out first, then uses "f" on that and the new value
+{-# INLINE zap_with_f #-}
zap_with_f f arr lst
- = foldr zap_one (return ()) lst
- where
- zap_one (i, new_v) rst = do
- old_v <- readArray arr i
+ = foldr (zap_one f arr) (return ()) lst
+
+zap_one f arr (i, new_v) rst = do
+ old_v <- readArray arr i
writeArray arr i (f old_v new_v)
rst
+{-# INLINE accum #-}
accum f old_array ivs
= runST (do
-- copy the old array:
zap_with_f f arr ivs
freezeArray arr
)
- where
- bottom = error "Array.accum: error in copying old array\n"
+{-# INLINE accumArray #-}
accumArray f zero ixs ivs
= runST (do
- arr# <- newArray ixs zero
- zap_with_f f arr# ivs
- freezeArray arr#
+ arr <- newArray ixs zero
+ zap_with_f f arr ivs
+ freezeArray arr
)
\end{code}
%*********************************************************
%* *
+\subsection{Array instances}
+%* *
+%*********************************************************
+
+
+\begin{code}
+instance Ix a => Functor (Array a) where
+ fmap = amap
+
+instance (Ix a, Eq b) => Eq (Array a b) where
+ a == a' = assocs a == assocs a'
+ a /= a' = assocs a /= assocs a'
+
+instance (Ix a, Ord b) => Ord (Array a b) where
+ compare a b = compare (assocs a) (assocs b)
+
+instance (Ix a, Show a, Show b) => Show (Array a b) where
+ showsPrec p a = showParen (p > 9) (
+ showString "array " .
+ shows (bounds a) . showChar ' ' .
+ shows (assocs a) )
+ showList = showList__ (showsPrec 0)
+
+{-
+instance (Ix a, Read a, Read b) => Read (Array a b) where
+ readsPrec p = readParen (p > 9)
+ (\r -> [(array b as, u) | ("array",s) <- lex r,
+ (b,t) <- reads s,
+ (as,u) <- reads t ])
+ readList = readList__ (readsPrec 0)
+-}
+\end{code}
+
+
+%*********************************************************
+%* *
\subsection{Operations on mutable arrays}
%* *
%*********************************************************
\begin{code}
newArray :: Ix ix => (ix,ix) -> elt -> ST s (MutableArray s ix elt)
-newCharArray, newIntArray, newAddrArray, newFloatArray, newDoubleArray
- :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
{-# SPECIALIZE newArray :: IPr -> elt -> ST s (MutableArray s Int elt),
(IPr,IPr) -> elt -> ST s (MutableArray s IPr elt)
#-}
-{-# SPECIALIZE newCharArray :: IPr -> ST s (MutableByteArray s Int) #-}
-{-# SPECIALIZE newIntArray :: IPr -> ST s (MutableByteArray s Int) #-}
-{-# SPECIALIZE newAddrArray :: IPr -> ST s (MutableByteArray s Int) #-}
-{-# SPECIALIZE newFloatArray :: IPr -> ST s (MutableByteArray s Int) #-}
-{-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
-
-newArray ixs init = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newArray# n# init s#) of { StateAndMutableArray# s2# arr# ->
- STret s2# (MutableArray ixs arr#) }}
-
-newCharArray ixs = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newCharArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
- STret s2# (MutableByteArray ixs barr#) }}
-
-newIntArray ixs = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newIntArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
- STret s2# (MutableByteArray ixs barr#) }}
-
-newAddrArray ixs = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newAddrArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
- STret s2# (MutableByteArray ixs barr#) }}
-
-newFloatArray ixs = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newFloatArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
- STret s2# (MutableByteArray ixs barr#) }}
-
-newDoubleArray ixs = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case (newDoubleArray# n# s#) of { StateAndMutableByteArray# s2# barr# ->
- STret s2# (MutableByteArray ixs barr#) }}
+newArray (l,u) init = ST $ \ s# ->
+ case rangeSize (l,u) of { I# n# ->
+ case (newArray# n# init s#) of { (# s2#, arr# #) ->
+ (# s2#, MutableArray l u arr# #) }}
-boundsOfArray :: Ix ix => MutableArray s ix elt -> (ix, ix)
-boundsOfByteArray :: Ix ix => MutableByteArray s ix -> (ix, ix)
-{-# SPECIALIZE boundsOfArray :: MutableArray s Int elt -> IPr #-}
-{-# SPECIALIZE boundsOfByteArray :: MutableByteArray s Int -> IPr #-}
-boundsOfArray (MutableArray ixs _) = ixs
-boundsOfByteArray (MutableByteArray ixs _) = ixs
+boundsOfArray :: Ix ix => MutableArray s ix elt -> (ix, ix)
+{-# SPECIALIZE boundsOfArray :: MutableArray s Int elt -> IPr #-}
+boundsOfArray (MutableArray l u _) = (l,u)
readArray :: Ix ix => MutableArray s ix elt -> ix -> ST s elt
-
-readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
-readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
-readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
-readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
-readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
-
{-# SPECIALIZE readArray :: MutableArray s Int elt -> Int -> ST s elt,
MutableArray s IPr elt -> IPr -> ST s elt
#-}
-{-# SPECIALIZE readCharArray :: MutableByteArray s Int -> Int -> ST s Char #-}
-{-# SPECIALIZE readIntArray :: MutableByteArray s Int -> Int -> ST s Int #-}
-{-# SPECIALIZE readAddrArray :: MutableByteArray s Int -> Int -> ST s Addr #-}
---NO:{-# SPECIALIZE readFloatArray :: MutableByteArray s Int -> Int -> ST s Float #-}
-{-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
-
-readArray (MutableArray ixs arr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readArray# arr# n# s# of { StateAndPtr# s2# r ->
- STret s2# r }}
-
-readCharArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readCharArray# barr# n# s# of { StateAndChar# s2# r# ->
- STret s2# (C# r#) }}
-
-readIntArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readIntArray# barr# n# s# of { StateAndInt# s2# r# ->
- STret s2# (I# r#) }}
-
-readAddrArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readAddrArray# barr# n# s# of { StateAndAddr# s2# r# ->
- STret s2# (A# r#) }}
-
-readFloatArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readFloatArray# barr# n# s# of { StateAndFloat# s2# r# ->
- STret s2# (F# r#) }}
-
-readDoubleArray (MutableByteArray ixs barr#) n = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case readDoubleArray# barr# n# s# of { StateAndDouble# s2# r# ->
- STret s2# (D# r#) }}
-
---Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
-indexCharArray :: Ix ix => ByteArray ix -> ix -> Char
-indexIntArray :: Ix ix => ByteArray ix -> ix -> Int
-indexAddrArray :: Ix ix => ByteArray ix -> ix -> Addr
-indexFloatArray :: Ix ix => ByteArray ix -> ix -> Float
-indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
-
-{-# SPECIALIZE indexCharArray :: ByteArray Int -> Int -> Char #-}
-{-# SPECIALIZE indexIntArray :: ByteArray Int -> Int -> Int #-}
-{-# SPECIALIZE indexAddrArray :: ByteArray Int -> Int -> Addr #-}
---NO:{-# SPECIALIZE indexFloatArray :: ByteArray Int -> Int -> Float #-}
-{-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
-
-indexCharArray (ByteArray ixs barr#) n
- = case (index ixs n) of { I# n# ->
- case indexCharArray# barr# n# of { r# ->
- (C# r#)}}
-
-indexIntArray (ByteArray ixs barr#) n
- = case (index ixs n) of { I# n# ->
- case indexIntArray# barr# n# of { r# ->
- (I# r#)}}
-
-indexAddrArray (ByteArray ixs barr#) n
- = case (index ixs n) of { I# n# ->
- case indexAddrArray# barr# n# of { r# ->
- (A# r#)}}
-
-indexFloatArray (ByteArray ixs barr#) n
- = case (index ixs n) of { I# n# ->
- case indexFloatArray# barr# n# of { r# ->
- (F# r#)}}
-
-indexDoubleArray (ByteArray ixs barr#) n
- = case (index ixs n) of { I# n# ->
- case indexDoubleArray# barr# n# of { r# ->
- (D# r#)}}
-
---Indexing off @Addrs@ is similar, and therefore given here.
-indexCharOffAddr :: Addr -> Int -> Char
-indexIntOffAddr :: Addr -> Int -> Int
-indexAddrOffAddr :: Addr -> Int -> Addr
-indexFloatOffAddr :: Addr -> Int -> Float
-indexDoubleOffAddr :: Addr -> Int -> Double
-
-indexCharOffAddr (A# addr#) n
- = case n of { I# n# ->
- case indexCharOffAddr# addr# n# of { r# ->
- (C# r#)}}
-
-indexIntOffAddr (A# addr#) n
- = case n of { I# n# ->
- case indexIntOffAddr# addr# n# of { r# ->
- (I# r#)}}
-
-indexAddrOffAddr (A# addr#) n
- = case n of { I# n# ->
- case indexAddrOffAddr# addr# n# of { r# ->
- (A# r#)}}
-
-indexFloatOffAddr (A# addr#) n
- = case n of { I# n# ->
- case indexFloatOffAddr# addr# n# of { r# ->
- (F# r#)}}
-
-indexDoubleOffAddr (A# addr#) n
- = case n of { I# n# ->
- case indexDoubleOffAddr# addr# n# of { r# ->
- (D# r#)}}
-writeArray :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
-writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
-writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
-writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
-writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
-writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
+readArray (MutableArray l u arr#) n = ST $ \ s# ->
+ case (index (l,u) n) of { I# n# ->
+ case readArray# arr# n# s# of { (# s2#, r #) ->
+ (# s2#, r #) }}
+writeArray :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
{-# SPECIALIZE writeArray :: MutableArray s Int elt -> Int -> elt -> ST s (),
MutableArray s IPr elt -> IPr -> elt -> ST s ()
#-}
-{-# SPECIALIZE writeCharArray :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
-{-# SPECIALIZE writeIntArray :: MutableByteArray s Int -> Int -> Int -> ST s () #-}
-{-# SPECIALIZE writeAddrArray :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
---NO:{-# SPECIALIZE writeFloatArray :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
-{-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
-
-writeArray (MutableArray ixs arr#) n ele = ST $ \ s# ->
- case index ixs n of { I# n# ->
- case writeArray# arr# n# ele s# of { s2# ->
- STret s2# () }}
-
-writeCharArray (MutableByteArray ixs barr#) n (C# ele) = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case writeCharArray# barr# n# ele s# of { s2# ->
- STret s2# () }}
-
-writeIntArray (MutableByteArray ixs barr#) n (I# ele) = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case writeIntArray# barr# n# ele s# of { s2# ->
- STret s2# () }}
-
-writeAddrArray (MutableByteArray ixs barr#) n (A# ele) = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case writeAddrArray# barr# n# ele s# of { s2# ->
- STret s2# () }}
-
-writeFloatArray (MutableByteArray ixs barr#) n (F# ele) = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case writeFloatArray# barr# n# ele s# of { s2# ->
- STret s2# () }}
-
-writeDoubleArray (MutableByteArray ixs barr#) n (D# ele) = ST $ \ s# ->
- case (index ixs n) of { I# n# ->
- case writeDoubleArray# barr# n# ele s# of { s2# ->
- STret s2# () }}
+
+writeArray (MutableArray l u arr#) n ele = ST $ \ s# ->
+ case index (l,u) n of { I# n# ->
+ case writeArray# arr# n# ele s# of { s2# ->
+ (# s2#, () #) }}
\end{code}
\begin{code}
freezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
-freezeCharArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-freezeIntArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-freezeAddrArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-freezeFloatArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-freezeDoubleArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-
{-# SPECIALISE freezeArray :: MutableArray s Int elt -> ST s (Array Int elt),
MutableArray s IPr elt -> ST s (Array IPr elt)
#-}
-{-# SPECIALISE freezeCharArray :: MutableByteArray s Int -> ST s (ByteArray Int) #-}
-freezeArray (MutableArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndArray# s2# frozen# ->
- STret s2# (Array ixs frozen#) }}
+freezeArray (MutableArray l u arr#) = ST $ \ s# ->
+ case rangeSize (l,u) of { I# n# ->
+ case freeze arr# n# s# of { (# s2#, frozen# #) ->
+ (# s2#, Array l u frozen# #) }}
where
freeze :: MutableArray# s ele -- the thing
-> Int# -- size of thing to be frozen
-> State# s -- the Universe and everything
- -> StateAndArray# s ele
-
- freeze arr# n# s#
- = case newArray# n# init s# of { StateAndMutableArray# s2# newarr1# ->
- case copy 0# n# arr# newarr1# s2# of { StateAndMutableArray# s3# newarr2# ->
+ -> (# State# s, Array# ele #)
+ freeze m_arr# n# s#
+ = case newArray# n# init s# of { (# s2#, newarr1# #) ->
+ case copy 0# n# m_arr# newarr1# s2# of { (# s3#, newarr2# #) ->
unsafeFreezeArray# newarr2# s3#
}}
where
init = error "freezeArray: element not copied"
copy :: Int# -> Int#
- -> MutableArray# s ele -> MutableArray# s ele
+ -> MutableArray# s ele
+ -> MutableArray# s ele
-> State# s
- -> StateAndMutableArray# s ele
+ -> (# State# s, MutableArray# s ele #)
- copy cur# end# from# to# s#
+ copy cur# end# from# to# st#
| cur# ==# end#
- = StateAndMutableArray# s# to#
+ = (# st#, to# #)
| otherwise
- = case readArray# from# cur# s# of { StateAndPtr# s1# ele ->
+ = case readArray# from# cur# st# of { (# s1#, ele #) ->
case writeArray# to# cur# ele s1# of { s2# ->
copy (cur# +# 1#) end# from# to# s2#
}}
-freezeCharArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }}
- where
- freeze :: MutableByteArray# s -- the thing
- -> Int# -- size of thing to be frozen
- -> State# s -- the Universe and everything
- -> StateAndByteArray# s
-
- freeze arr# n# s#
- = case (newCharArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
- case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
- unsafeFreezeByteArray# newarr2# s3#
- }}
- where
- copy :: Int# -> Int#
- -> MutableByteArray# s -> MutableByteArray# s
- -> State# s
- -> StateAndMutableByteArray# s
-
- copy cur# end# from# to# s#
- | cur# ==# end#
- = StateAndMutableByteArray# s# to#
- | otherwise
- = case (readCharArray# from# cur# s#) of { StateAndChar# s1# ele ->
- case (writeCharArray# to# cur# ele s1#) of { s2# ->
- copy (cur# +# 1#) end# from# to# s2#
- }}
-
-freezeIntArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }}
- where
- freeze :: MutableByteArray# s -- the thing
- -> Int# -- size of thing to be frozen
- -> State# s -- the Universe and everything
- -> StateAndByteArray# s
-
- freeze arr# n# s#
- = case (newIntArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
- case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
- unsafeFreezeByteArray# newarr2# s3#
- }}
- where
- copy :: Int# -> Int#
- -> MutableByteArray# s -> MutableByteArray# s
- -> State# s
- -> StateAndMutableByteArray# s
-
- copy cur# end# from# to# s#
- | cur# ==# end#
- = StateAndMutableByteArray# s# to#
- | otherwise
- = case (readIntArray# from# cur# s#) of { StateAndInt# s1# ele ->
- case (writeIntArray# to# cur# ele s1#) of { s2# ->
- copy (cur# +# 1#) end# from# to# s2#
- }}
-
-freezeAddrArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }}
- where
- freeze :: MutableByteArray# s -- the thing
- -> Int# -- size of thing to be frozen
- -> State# s -- the Universe and everything
- -> StateAndByteArray# s
-
- freeze arr# n# s#
- = case (newAddrArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
- case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
- unsafeFreezeByteArray# newarr2# s3#
- }}
- where
- copy :: Int# -> Int#
- -> MutableByteArray# s -> MutableByteArray# s
- -> State# s
- -> StateAndMutableByteArray# s
-
- copy cur# end# from# to# s#
- | cur# ==# end#
- = StateAndMutableByteArray# s# to#
- | otherwise
- = case (readAddrArray# from# cur# s#) of { StateAndAddr# s1# ele ->
- case (writeAddrArray# to# cur# ele s1#) of { s2# ->
- copy (cur# +# 1#) end# from# to# s2#
- }}
-
-freezeFloatArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }}
- where
- freeze :: MutableByteArray# s -- the thing
- -> Int# -- size of thing to be frozen
- -> State# s -- the Universe and everything
- -> StateAndByteArray# s
-
- freeze arr# end# s#
- = case (newFloatArray# end# s#) of { StateAndMutableByteArray# s2# newarr1# ->
- case copy 0# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
- unsafeFreezeByteArray# newarr2# s3#
- }}
- where
- copy :: Int#
- -> MutableByteArray# s -> MutableByteArray# s
- -> State# s
- -> StateAndMutableByteArray# s
-
- copy cur# from# to# s#
- | cur# ==# end#
- = StateAndMutableByteArray# s# to#
- | otherwise
- = case (readFloatArray# from# cur# s#) of { StateAndFloat# s1# ele ->
- case (writeFloatArray# to# cur# ele s1#) of { s2# ->
- copy (cur# +# 1#) from# to# s2#
- }}
-
-freezeDoubleArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case freeze arr# n# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }}
- where
- freeze :: MutableByteArray# s -- the thing
- -> Int# -- size of thing to be frozen
- -> State# s -- the Universe and everything
- -> StateAndByteArray# s
-
- freeze arr# n# s#
- = case (newDoubleArray# n# s#) of { StateAndMutableByteArray# s2# newarr1# ->
- case copy 0# n# arr# newarr1# s2# of { StateAndMutableByteArray# s3# newarr2# ->
- unsafeFreezeByteArray# newarr2# s3#
- }}
- where
- copy :: Int# -> Int#
- -> MutableByteArray# s -> MutableByteArray# s
- -> State# s
- -> StateAndMutableByteArray# s
-
- copy cur# end# from# to# s#
- | cur# ==# end#
- = StateAndMutableByteArray# s# to#
- | otherwise
- = case (readDoubleArray# from# cur# s#) of { StateAndDouble# s1# ele ->
- case (writeDoubleArray# to# cur# ele s1#) of { s2# ->
- copy (cur# +# 1#) end# from# to# s2#
- }}
-
unsafeFreezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
-unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
-
-{-# SPECIALIZE unsafeFreezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int)
- #-}
-
-unsafeFreezeArray (MutableArray ixs arr#) = ST $ \ s# ->
- case unsafeFreezeArray# arr# s# of { StateAndArray# s2# frozen# ->
- STret s2# (Array ixs frozen#) }
-
-unsafeFreezeByteArray (MutableByteArray ixs arr#) = ST $ \ s# ->
- case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
- STret s2# (ByteArray ixs frozen#) }
-
+unsafeFreezeArray (MutableArray l u arr#) = ST $ \ s# ->
+ case unsafeFreezeArray# arr# s# of { (# s2#, frozen# #) ->
+ (# s2#, Array l u frozen# #) }
--This takes a immutable array, and copies it into a mutable array, in a
--hurry.
+thawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
{-# SPECIALISE thawArray :: Array Int elt -> ST s (MutableArray s Int elt),
Array IPr elt -> ST s (MutableArray s IPr elt)
#-}
-thawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
-thawArray (Array ixs arr#) = ST $ \ s# ->
- case rangeSize ixs of { I# n# ->
- case thaw arr# n# s# of { StateAndMutableArray# s2# thawed# ->
- STret s2# (MutableArray ixs thawed#)}}
+thawArray (Array l u arr#) = ST $ \ s# ->
+ case rangeSize (l,u) of { I# n# ->
+ case thaw arr# n# s# of { (# s2#, thawed# #) ->
+ (# s2#, MutableArray l u thawed# #)}}
where
thaw :: Array# ele -- the thing
-> Int# -- size of thing to be thawed
-> State# s -- the Universe and everything
- -> StateAndMutableArray# s ele
+ -> (# State# s, MutableArray# s ele #)
- thaw arr# n# s#
- = case newArray# n# init s# of { StateAndMutableArray# s2# newarr1# ->
- copy 0# n# arr# newarr1# s2# }
+ thaw arr1# n# s#
+ = case newArray# n# init s# of { (# s2#, newarr1# #) ->
+ copy 0# n# arr1# newarr1# s2# }
where
init = error "thawArray: element not copied"
-> Array# ele
-> MutableArray# s ele
-> State# s
- -> StateAndMutableArray# s ele
+ -> (# State# s, MutableArray# s ele #)
- copy cur# end# from# to# s#
+ copy cur# end# from# to# st#
| cur# ==# end#
- = StateAndMutableArray# s# to#
+ = (# st#, to# #)
| otherwise
- = case indexArray# from# cur# of { Lift ele ->
- case writeArray# to# cur# ele s# of { s1# ->
+ = case indexArray# from# cur# of { (# ele #) ->
+ case writeArray# to# cur# ele st# of { s1# ->
copy (cur# +# 1#) end# from# to# s1#
}}
-\end{code}
-%*********************************************************
-%* *
-\subsection{Ghastly return types}
-%* *
-%*********************************************************
-
-\begin{code}
-data StateAndArray# s elt = StateAndArray# (State# s) (Array# elt)
-data StateAndMutableArray# s elt = StateAndMutableArray# (State# s) (MutableArray# s elt)
-data StateAndByteArray# s = StateAndByteArray# (State# s) ByteArray#
-data StateAndMutableByteArray# s = StateAndMutableByteArray# (State# s) (MutableByteArray# s)
+-- this is a quicker version of the above, just flipping the type
+-- (& representation) of an immutable array. And placing a
+-- proof obligation on the programmer.
+unsafeThawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
+unsafeThawArray (Array l u arr#) = ST $ \ s# ->
+ case unsafeThawArray# arr# s# of
+ (# s2#, marr# #) -> (# s2#, MutableArray l u marr# #)
\end{code}