on master help sign in
Move array copy code into Array
d92611fBryan O'Sullivan
Data/Text.hs+4−13
@@ -185,7 +185,6 @@ import Data.Data (mkNoRepType)
185import Data.Data (mkNorepType)
185import Data.Data (mkNorepType)
186#endif
186#endif
187import Control.Monad (foldM)
187import Control.Monad (foldM)
188−import Control.Monad.ST (ST)
189import qualified Data.Text.Array as A
188import qualified Data.Text.Array as A
190import qualified Data.List as L
189import qualified Data.List as L
191import Data.Monoid (Monoid(..))
190import Data.Monoid (Monoid(..))
@@ -297,8 +296,8 @@ append (Text arr1 off1 len1) (Text arr2 off2 len2) = Text (A.run x) 0 len
297 len = len1+len2
296 len = len1+len2
298 x = do
297 x = do
299 arr <- A.unsafeNew len
298 arr <- A.unsafeNew len
300− copy arr 0 arr1 off1 len1
299+ A.partialCopyI arr 0 arr1 off1 len1
301− copy arr len1 arr2 off2 (len1+len2)
300+ A.partialCopyI arr len1 arr2 off2 (len1+len2)
302 return arr
301 return arr
303{-# INLINE append #-}
302{-# INLINE append #-}
304
303
@@ -309,14 +308,6 @@ append (Text arr1 off1 len1) (Text arr2 off2 len2) = Text (A.run x) 0 len
309 unstream (S.append (stream t1) (stream t2)) = append t1 t2
308 unstream (S.append (stream t1) (stream t2)) = append t1 t2
310 #-}
309 #-}
311
310
312−copy :: forall s. A.MArray s -> Int -> A.Array -> Int -> Int
313− -> ST s ()
314−copy dest i0 src j0 top = go i0 j0
315− where
316− go i j | i >= top = return ()
317− | otherwise = do A.unsafeWrite dest i (src `A.unsafeIndex` j)
318− go (i+1) (j+1)
319−
320-- | /O(1)/ Returns the first character of a 'Text', which must be
311-- | /O(1)/ Returns the first character of a 'Text', which must be
321-- non-empty. Subject to fusion.
312-- non-empty. Subject to fusion.
322head :: Text -> Char
313head :: Text -> Char
@@ -611,7 +602,7 @@ concat ts = Text (A.run go) 0 len
611 len = L.sum (L.map (\(Text _ _ l) -> l) ts)
602 len = L.sum (L.map (\(Text _ _ l) -> l) ts)
612 go = do
603 go = do
613 arr <- A.unsafeNew len
604 arr <- A.unsafeNew len
614− let step i (Text a o l) = let j = i + l in copy arr i a o j >> return j
605+ let step i (Text a o l) = let j = i + l in A.partialCopyI arr i a o j >> return j
615 foldM step 0 ts >> return arr
606 foldM step 0 ts >> return arr
616{-# INLINE concat #-}
607{-# INLINE concat #-}
617
608
@@ -721,7 +712,7 @@ replicate n t@(Text a o l)
721 arr <- A.unsafeNew len
712 arr <- A.unsafeNew len
722 let loop !d !i | i >= n = return arr
713 let loop !d !i | i >= n = return arr
723 | otherwise = let m = d + l
714 | otherwise = let m = d + l
724− in copy arr d a o m >> loop m (i+1)
715+ in A.partialCopyI arr d a o m >> loop m (i+1)
725 loop 0 0
716 loop 0 0
726{-# INLINE [1] replicate #-}
717{-# INLINE [1] replicate #-}
727
718
Data/Text/Array.hs+9−1
@@ -32,6 +32,7 @@ module Data.Text.Array
32 -- * Functions
32 -- * Functions
33 , copy
33 , copy
34 , partialCopyM
34 , partialCopyM
35+ , partialCopyI
35 , empty
36 , empty
36 , run
37 , run
37 , run2
38 , run2
@@ -217,4 +218,11 @@ partialCopyM dest didx src sidx count =
217 | c >= count = return ()
218 | c >= count = return ()
218 | otherwise = do unsafeRead src i >>= unsafeWrite dest j
219 | otherwise = do unsafeRead src i >>= unsafeWrite dest j
219 copy_loop (i+1) (j+1) (c+1)
220 copy_loop (i+1) (j+1) (c+1)
220−{-# INLINE partialCopyM #-}
221+
222+-- | Copy some elements of an immutable array.
223+partialCopyI :: MArray s -> Int -> Array -> Int -> Int -> ST s ()
224+partialCopyI dest i0 src j0 top = go i0 j0
225+ where
226+ go !i !j | i >= top = return ()
227+ | otherwise = do unsafeWrite dest i (src `unsafeIndex` j)
228+ go (i+1) (j+1)