on master help sign in
Turn on use of array lengths only if ASSERTS
8111ffbBryan O'Sullivan
Data/Text/Array.hs+16−2
@@ -69,15 +69,20 @@ import Prelude hiding (length, read)
69-- | Immutable array type.
69-- | Immutable array type.
70data Array = Array {
70data Array = Array {
71 aBA :: ByteArray#
71 aBA :: ByteArray#
72+#if defined(ASSERTS)
72 , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
73 , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
74+#endif
73 }
75 }
74
76
75-- | Mutable array type, for use in the ST monad.
77-- | Mutable array type, for use in the ST monad.
76data MArray s = MArray {
78data MArray s = MArray {
77 maBA :: MutableByteArray# s
79 maBA :: MutableByteArray# s
80+#if defined(ASSERTS)
78 , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
81 , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
82+#endif
79 }
83 }
80
84
85+#if defined(ASSERTS)
81-- | Operations supported by all arrays.
86-- | Operations supported by all arrays.
82class IArray a where
87class IArray a where
83 -- | Return the length of an array.
88 -- | Return the length of an array.
@@ -90,6 +95,7 @@ instance IArray Array where
90instance IArray (MArray s) where
95instance IArray (MArray s) where
91 length = maLen
96 length = maLen
92 {-# INLINE length #-}
97 {-# INLINE length #-}
98+#endif
93
99
94-- | Create an uninitialized mutable array.
100-- | Create an uninitialized mutable array.
95unsafeNew :: forall s. Int -> ST s (MArray s)
101unsafeNew :: forall s. Int -> ST s (MArray s)
@@ -104,13 +110,21 @@ unsafeNew n =
104 if len < 0 then error (show ("unsafeNew",len)) else
110 if len < 0 then error (show ("unsafeNew",len)) else
105#endif
111#endif
106 case newByteArray# len# s1# of
112 case newByteArray# len# s1# of
107− (# s2#, marr# #) -> (# s2#, MArray marr# n #)
113+ (# s2#, marr# #) -> (# s2#, MArray marr#
114+#if defined(ASSERTS)
115+ n
116+#endif
117+ #)
108{-# INLINE unsafeNew #-}
118{-# INLINE unsafeNew #-}
109
119
110-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
120-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
111unsafeFreeze :: MArray s -> ST s Array
121unsafeFreeze :: MArray s -> ST s Array
112unsafeFreeze MArray{..} = ST $ \s# ->
122unsafeFreeze MArray{..} = ST $ \s# ->
113− (# s#, Array (unsafeCoerce# maBA) maLen #)
123+ (# s#, Array (unsafeCoerce# maBA)
124+#if defined(ASSERTS)
125+ maLen
126+#endif
127+ #)
114{-# INLINE unsafeFreeze #-}
128{-# INLINE unsafeFreeze #-}
115
129
116-- | Indicate how many bytes would be used for an array of the given
130-- | Indicate how many bytes would be used for an array of the given