on master help sign in
Implement count.

--HG-- extra : convert_revision : 2e9731273c11312ad9c6b2c0a9ec80ce765fe380

commit6f9ab7bcff89e0c14640fc8ed03a61c75bd2dbc2 authorBryan O'Sullivan <bos@serpentine.com> dateThu 12 Feb 2009, 09:18 (18 years ago) parentdf49b20
3 files changed+33−11
Data/Text.hs+18−11 Data/Text/Fusion.hs+13 tests/Properties.hs+2
Data/Text.hs+18−11
@@ -133,7 +133,7 @@ module Data.Text
133133 , findIndices
134134 , elemIndex
135135 , elemIndices
136− -- , count
136+ , count
137137
138138 -- * Zipping and unzipping
139139 , zipWith
@@ -561,9 +561,9 @@ take n t@(Text arr off len)
561561 | n >= len = t
562562 | otherwise = Text arr off (loop 0 0)
563563 where
564− loop !i !count
565− | i >= len || count >= n = i
566− | otherwise = loop (i+d) (count+1)
564+ loop !i !cnt
565+ | i >= len || cnt >= n = i
566+ | otherwise = loop (i+d) (cnt+1)
567567 where d = iter_ t i
568568{-# INLINE [1] take #-}
569569
@@ -583,9 +583,9 @@ drop n t@(Text arr off len)
583583 | n >= len = empty
584584 | otherwise = loop 0 0
585585 where end = off + len
586− loop !i !count
587− | i >= end || count >= n = Text arr (off+i) (len-i)
588− | otherwise = loop (i+d) (count+1)
586+ loop !i !cnt
587+ | i >= end || cnt >= n = Text arr (off+i) (len-i)
588+ | otherwise = loop (i+d) (cnt+1)
589589 where d = iter_ t i
590590{-# INLINE [1] drop #-}
591591
@@ -640,10 +640,10 @@ splitAt n t@(Text arr off len)
640640 | n >= len = (t, empty)
641641 | otherwise = (Text arr off k, Text arr (off+k) (len-k))
642642 where k = loop 0 0
643− loop !i !count
644− | i >= len || count >= n = i
645− | otherwise = loop (i+d) (count+1)
646− where d = iter_ t i
643+ loop !i !cnt
644+ | i >= len || cnt >= n = i
645+ | otherwise = loop (i+d) (cnt+1)
646+ where d = iter_ t i
647647{-# INLINE splitAt #-}
648648
649649-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns a
@@ -852,6 +852,13 @@ elemIndices :: Char -> Text -> [Int]
852852elemIndices c t = S.elemIndices c (stream t)
853853{-# INLINE elemIndices #-}
854854
855+-- | /O(n)/ The 'count' function returns the number of times the query
856+-- element appears in the given 'Text'. This function is subject to
857+-- fusion.
858+count :: Char -> Text -> Int
859+count c t = S.count c (stream t)
860+{-# INLINE count #-}
861+
855862-------------------------------------------------------------------------------
856863-- * Zipping
857864
Data/Text/Fusion.hs+13
@@ -94,6 +94,7 @@ module Data.Text.Fusion
9494 , findIndexOrEnd
9595 , elemIndex
9696 , elemIndices
97+ , count
9798
9899 -- * Zipping and unzipping
99100 , zipWith
@@ -838,6 +839,18 @@ elemIndices a (Stream next s0 _len) = loop 0 s0
838839 | otherwise -> loop (i+1) s'
839840{-# INLINE [0] elemIndices #-}
840841
842+-- | /O(n)/ The 'count' function returns the number of times the query
843+-- element appears in the given stream.
844+count :: Char -> Stream Char -> Int
845+count a (Stream next s0 _len) = loop 0 s0
846+ where
847+ loop !i !s = case next s of
848+ Done -> i
849+ Skip s' -> loop i s'
850+ Yield x s' | a == x -> loop (i+1) s'
851+ | otherwise -> loop i s'
852+{-# INLINE [0] count #-}
853+
841854-------------------------------------------------------------------------------
842855-- * Zipping
843856
tests/Properties.hs+2
@@ -165,6 +165,7 @@ prop_findIndex p = L.findIndex p `eqP` T.findIndex p
165165prop_findIndices p = L.findIndices p`eqP` T.findIndices p
166166prop_elemIndex c = L.elemIndex c `eqP` T.elemIndex c
167167prop_elemIndices c = L.elemIndices c`eqP` T.elemIndices c
168+prop_count c = (L.length . L.elemIndices c) `eqP` T.count c
168169prop_zipWith c s = L.zipWith c s `eqP` (unpack . T.zipWith c (pack s))
169170
170171main = run tests =<< getArgs
@@ -269,5 +270,6 @@ tests = [
269270 ("prop_findIndices", mytest prop_findIndices),
270271 ("prop_elemIndex", mytest prop_elemIndex),
271272 ("prop_elemIndices", mytest prop_elemIndices),
273+ ("prop_count", mytest prop_count),
272274 ("prop_zipWith", mytest prop_zipWith)
273275 ]