@@ -1,4 +1,5 @@
1−{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples #-}
1+{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, RecordWildCards,
2{-# OPTIONS_GHC -fno-warn-unused-matches #-}
3{-# OPTIONS_GHC -fno-warn-unused-matches #-}
4-- Module : Data.Text.Array
5-- Module : Data.Text.Array
@@ -25,8 +26,7 @@
25module Data.Text.Array
26module Data.Text.Array
27 -- * Types
28 -- * Types
32 -- * Functions
32 -- * Functions
@@ -68,14 +68,16 @@ import GHC.Word (Word16(..), Word(..))
68import Prelude hiding (length, read)
68import Prelude hiding (length, read)
70-- | Immutable array type.
70-- | Immutable array type.
71−data Array = Array
71+data Array = Array {
72− {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
73+ , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
75-- | Mutable array type, for use in the ST monad.
76-- | Mutable array type, for use in the ST monad.
76−data MArray s = MArray
77+data MArray s = MArray {
78+ maBA :: MutableByteArray# s
77− {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
79+ , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
78− (MutableByteArray# s)
80+ }
80-- | Operations supported by all arrays.
82-- | Operations supported by all arrays.
81class IArray a where
83class IArray a where
@@ -83,11 +85,11 @@ class IArray a where
83 length :: a -> Int
85 length :: a -> Int
85instance IArray Array where
87instance IArray Array where
86− length (Array len _ba) = len
88+ length = aLen
87 {-# INLINE length #-}
89 {-# INLINE length #-}
89instance IArray (MArray s) where
91instance IArray (MArray s) where
90− length (MArray len _ba) = len
92+ length = maLen
91 {-# INLINE length #-}
93 {-# INLINE length #-}
93-- | Create an uninitialized mutable array.
95-- | Create an uninitialized mutable array.
@@ -103,13 +105,13 @@ unsafeNew n =
103 if len < 0 then error (show ("unsafeNew",len)) else
105 if len < 0 then error (show ("unsafeNew",len)) else
105 case newByteArray# len# s1# of
107 case newByteArray# len# s1# of
106− (# s2#, marr# #) -> (# s2#, MArray n marr# #)
108+ (# s2#, marr# #) -> (# s2#, MArray marr# n #)
107{-# INLINE unsafeNew #-}
109{-# INLINE unsafeNew #-}
109-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
111-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
110−unsafeFreeze :: MArray s -> ST s (Array)
112+unsafeFreeze :: MArray s -> ST s Array
111−unsafeFreeze (MArray len mba#) = ST $ \s# ->
113+unsafeFreeze MArray{..} = ST $ \s# ->
112− (# s#, Array len (unsafeCoerce# mba#) #)
114+ (# s#, Array (unsafeCoerce# maBA) maLen #)
113{-# INLINE unsafeFreeze #-}
115{-# INLINE unsafeFreeze #-}
115-- | Indicate how many bytes would be used for an array of the given
117-- | Indicate how many bytes would be used for an array of the given
@@ -121,52 +123,52 @@ bytesInArray n = n `shiftL` 1
121-- | Unchecked read of an immutable array. May return garbage or
123-- | Unchecked read of an immutable array. May return garbage or
122-- crash on an out-of-bounds access.
124-- crash on an out-of-bounds access.
123unsafeIndex :: Array -> Int -> Word16
125unsafeIndex :: Array -> Int -> Word16
124−unsafeIndex (Array len ba#) i@(I# i#) =
126+unsafeIndex Array{..} i@(I# i#) =
125 CHECK_BOUNDS("unsafeIndex",len,i)
127 CHECK_BOUNDS("unsafeIndex",len,i)
126− case indexWord16Array# ba# i# of r# -> (W16# r#)
128+ case indexWord16Array# aBA i# of r# -> (W16# r#)
127{-# INLINE unsafeIndex #-}
129{-# INLINE unsafeIndex #-}
129-- | Unchecked read of an immutable array. May return garbage or
131-- | Unchecked read of an immutable array. May return garbage or
130-- crash on an out-of-bounds access.
132-- crash on an out-of-bounds access.
131unsafeIndexWord :: Array -> Int -> Word
133unsafeIndexWord :: Array -> Int -> Word
132−unsafeIndexWord (Array len ba#) i@(I# i#) =
134+unsafeIndexWord Array{..} i@(I# i#) =
133 CHECK_BOUNDS("unsafeIndexWord",len,i)
135 CHECK_BOUNDS("unsafeIndexWord",len,i)
134− case indexWordArray# ba# i# of r# -> (W# r#)
136+ case indexWordArray# aBA i# of r# -> (W# r#)
135{-# INLINE unsafeIndexWord #-}
137{-# INLINE unsafeIndexWord #-}
137-- | Unchecked read of a mutable array. May return garbage or
139-- | Unchecked read of a mutable array. May return garbage or
138-- crash on an out-of-bounds access.
140-- crash on an out-of-bounds access.
139unsafeRead :: MArray s -> Int -> ST s Word16
141unsafeRead :: MArray s -> Int -> ST s Word16
140−unsafeRead (MArray len mba#) i@(I# i#) = ST $ \s# ->
142+unsafeRead MArray{..} i@(I# i#) = ST $ \s# ->
141 CHECK_BOUNDS("unsafeRead",len,i)
143 CHECK_BOUNDS("unsafeRead",len,i)
142− case readWord16Array# mba# i# s# of
144+ case readWord16Array# maBA i# s# of
143 (# s2#, r# #) -> (# s2#, W16# r# #)
145 (# s2#, r# #) -> (# s2#, W16# r# #)
144{-# INLINE unsafeRead #-}
146{-# INLINE unsafeRead #-}
146-- | Unchecked write of a mutable array. May return garbage or crash
148-- | Unchecked write of a mutable array. May return garbage or crash
147-- on an out-of-bounds access.
149-- on an out-of-bounds access.
148unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
150unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
149−unsafeWrite (MArray len marr#) i@(I# i#) (W16# e#) = ST $ \s1# ->
151+unsafeWrite MArray{..} i@(I# i#) (W16# e#) = ST $ \s1# ->
150 CHECK_BOUNDS("unsafeWrite",len,i)
152 CHECK_BOUNDS("unsafeWrite",len,i)
151− case writeWord16Array# marr# i# e# s1# of
153+ case writeWord16Array# maBA i# e# s1# of
152 s2# -> (# s2#, () #)
154 s2# -> (# s2#, () #)
153{-# INLINE unsafeWrite #-}
155{-# INLINE unsafeWrite #-}
155-- | Unchecked read of a mutable array. May return garbage or
157-- | Unchecked read of a mutable array. May return garbage or
156-- crash on an out-of-bounds access.
158-- crash on an out-of-bounds access.
157unsafeReadWord :: MArray s -> Int -> ST s Word
159unsafeReadWord :: MArray s -> Int -> ST s Word
158−unsafeReadWord (MArray len mba#) i@(I# i#) = ST $ \s# ->
160+unsafeReadWord MArray{..} i@(I# i#) = ST $ \s# ->
159 CHECK_BOUNDS("unsafeRead64",len,i)
161 CHECK_BOUNDS("unsafeRead64",len,i)
160− case readWordArray# mba# i# s# of
162+ case readWordArray# maBA i# s# of
161 (# s2#, r# #) -> (# s2#, W# r# #)
163 (# s2#, r# #) -> (# s2#, W# r# #)
162{-# INLINE unsafeReadWord #-}
164{-# INLINE unsafeReadWord #-}
164-- | Unchecked write of a mutable array. May return garbage or crash
166-- | Unchecked write of a mutable array. May return garbage or crash
165-- on an out-of-bounds access.
167-- on an out-of-bounds access.
166unsafeWriteWord :: MArray s -> Int -> Word -> ST s ()
168unsafeWriteWord :: MArray s -> Int -> Word -> ST s ()
167−unsafeWriteWord (MArray len marr#) i@(I# i#) (W# e#) = ST $ \s1# ->
169+unsafeWriteWord MArray{..} i@(I# i#) (W# e#) = ST $ \s1# ->
168 CHECK_BOUNDS("unsafeWriteWord",len,i)
170 CHECK_BOUNDS("unsafeWriteWord",len,i)
169− case writeWordArray# marr# i# e# s1# of
171+ case writeWordArray# maBA i# e# s1# of
170 s2# -> (# s2#, () #)
172 s2# -> (# s2#, () #)
171{-# INLINE unsafeWriteWord #-}
173{-# INLINE unsafeWriteWord #-}
@@ -208,7 +210,7 @@ wordAligned i = i .&. (wordFactor - 1) == 0
208copy :: MArray s -- ^ source array
210copy :: MArray s -- ^ source array
209 -> MArray s -- ^ destination array
211 -> MArray s -- ^ destination array
210 -> ST s ()
212 -> ST s ()
211−copy dest@(MArray dlen _) src@(MArray slen _)
213+copy dest@(MArray _ dlen) src@(MArray _ slen)
212 | dlen >= slen = fast_loop 0
214 | dlen >= slen = fast_loop 0
213 | otherwise = fail "Data.Text.Array.copy: array too small"
215 | otherwise = fail "Data.Text.Array.copy: array too small"