@@ -1,4 +1,5 @@
1−{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples #-}
1+{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, RecordWildCards,
2+ UnboxedTuples #-}
23{-# OPTIONS_GHC -fno-warn-unused-matches #-}
34-- |
45-- Module : Data.Text.Array
@@ -25,8 +26,7 @@
2526module Data.Text.Array
2627 (
2728 -- * Types
28− IArray(..)
29− , Array
29+ Array
3030 , MArray
3131
3232 -- * Functions
@@ -68,14 +68,16 @@ import GHC.Word (Word16(..), Word(..))
6868import Prelude hiding (length, read)
6969
7070-- | Immutable array type.
71−data Array = Array
72− {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
73− ByteArray#
71+data Array = Array {
72+ aBA :: ByteArray#
73+ , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
74+ }
7475
7576-- | Mutable array type, for use in the ST monad.
76−data MArray s = MArray
77− {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
78− (MutableByteArray# s)
77+data MArray s = MArray {
78+ maBA :: MutableByteArray# s
79+ , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
80+ }
7981
8082-- | Operations supported by all arrays.
8183class IArray a where
@@ -83,11 +85,11 @@ class IArray a where
8385 length :: a -> Int
8486
8587instance IArray Array where
86− length (Array len _ba) = len
88+ length = aLen
8789 {-# INLINE length #-}
8890
8991instance IArray (MArray s) where
90− length (MArray len _ba) = len
92+ length = maLen
9193 {-# INLINE length #-}
9294
9395-- | Create an uninitialized mutable array.
@@ -103,13 +105,13 @@ unsafeNew n =
103105 if len < 0 then error (show ("unsafeNew",len)) else
104106#endif
105107 case newByteArray# len# s1# of
106− (# s2#, marr# #) -> (# s2#, MArray n marr# #)
108+ (# s2#, marr# #) -> (# s2#, MArray marr# n #)
107109{-# INLINE unsafeNew #-}
108110
109111-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
110−unsafeFreeze :: MArray s -> ST s (Array)
111−unsafeFreeze (MArray len mba#) = ST $ \s# ->
112− (# s#, Array len (unsafeCoerce# mba#) #)
112+unsafeFreeze :: MArray s -> ST s Array
113+unsafeFreeze MArray{..} = ST $ \s# ->
114+ (# s#, Array (unsafeCoerce# maBA) maLen #)
113115{-# INLINE unsafeFreeze #-}
114116
115117-- | Indicate how many bytes would be used for an array of the given
@@ -121,52 +123,52 @@ bytesInArray n = n `shiftL` 1
121123-- | Unchecked read of an immutable array. May return garbage or
122124-- crash on an out-of-bounds access.
123125unsafeIndex :: Array -> Int -> Word16
124−unsafeIndex (Array len ba#) i@(I# i#) =
126+unsafeIndex Array{..} i@(I# i#) =
125127 CHECK_BOUNDS("unsafeIndex",len,i)
126− case indexWord16Array# ba# i# of r# -> (W16# r#)
128+ case indexWord16Array# aBA i# of r# -> (W16# r#)
127129{-# INLINE unsafeIndex #-}
128130
129131-- | Unchecked read of an immutable array. May return garbage or
130132-- crash on an out-of-bounds access.
131133unsafeIndexWord :: Array -> Int -> Word
132−unsafeIndexWord (Array len ba#) i@(I# i#) =
134+unsafeIndexWord Array{..} i@(I# i#) =
133135 CHECK_BOUNDS("unsafeIndexWord",len,i)
134− case indexWordArray# ba# i# of r# -> (W# r#)
136+ case indexWordArray# aBA i# of r# -> (W# r#)
135137{-# INLINE unsafeIndexWord #-}
136138
137139-- | Unchecked read of a mutable array. May return garbage or
138140-- crash on an out-of-bounds access.
139141unsafeRead :: MArray s -> Int -> ST s Word16
140−unsafeRead (MArray len mba#) i@(I# i#) = ST $ \s# ->
142+unsafeRead MArray{..} i@(I# i#) = ST $ \s# ->
141143 CHECK_BOUNDS("unsafeRead",len,i)
142− case readWord16Array# mba# i# s# of
144+ case readWord16Array# maBA i# s# of
143145 (# s2#, r# #) -> (# s2#, W16# r# #)
144146{-# INLINE unsafeRead #-}
145147
146148-- | Unchecked write of a mutable array. May return garbage or crash
147149-- on an out-of-bounds access.
148150unsafeWrite :: 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# ->
150152 CHECK_BOUNDS("unsafeWrite",len,i)
151− case writeWord16Array# marr# i# e# s1# of
153+ case writeWord16Array# maBA i# e# s1# of
152154 s2# -> (# s2#, () #)
153155{-# INLINE unsafeWrite #-}
154156
155157-- | Unchecked read of a mutable array. May return garbage or
156158-- crash on an out-of-bounds access.
157159unsafeReadWord :: MArray s -> Int -> ST s Word
158−unsafeReadWord (MArray len mba#) i@(I# i#) = ST $ \s# ->
160+unsafeReadWord MArray{..} i@(I# i#) = ST $ \s# ->
159161 CHECK_BOUNDS("unsafeRead64",len,i)
160− case readWordArray# mba# i# s# of
162+ case readWordArray# maBA i# s# of
161163 (# s2#, r# #) -> (# s2#, W# r# #)
162164{-# INLINE unsafeReadWord #-}
163165
164166-- | Unchecked write of a mutable array. May return garbage or crash
165167-- on an out-of-bounds access.
166168unsafeWriteWord :: 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# ->
168170 CHECK_BOUNDS("unsafeWriteWord",len,i)
169− case writeWordArray# marr# i# e# s1# of
171+ case writeWordArray# maBA i# e# s1# of
170172 s2# -> (# s2#, () #)
171173{-# INLINE unsafeWriteWord #-}
172174
@@ -208,7 +210,7 @@ wordAligned i = i .&. (wordFactor - 1) == 0
208210copy :: MArray s -- ^ source array
209211 -> MArray s -- ^ destination array
210212 -> ST s ()
211−copy dest@(MArray dlen _) src@(MArray slen _)
213+copy dest@(MArray _ dlen) src@(MArray _ slen)
212214 | dlen >= slen = fast_loop 0
213215 | otherwise = fail "Data.Text.Array.copy: array too small"
214216 where