on master help sign in
Make inits run in O(n) time instead of O(n^2)

--HG-- extra : convert_revision : 946b2dba8b7e29beec1f96b521de14caaecf9565

commit40997b2ab78b309159fdf101ab807ebb55e30fa7 authorBryan O'Sullivan <bos@serpentine.com> dateWed 28 Jan 2009, 06:24 (18 years ago) parent889b318
1 file changed+6−1
Data/Text.hs+6−1
Data/Text.hs+6−1
@@ -581,7 +581,12 @@ dropWhile p t = unstream (S.dropWhile p (stream t))
581581-- | /O(n)/ Return all initial segments of the given 'Text', shortest
582582-- first.
583583inits :: Text -> [Text]
584−inits (Text arr off len) = [Text arr off l | l <- [0..len]]
584+inits t@(Text arr off len) = loop off
585+ where loop i | i >= len = [t]
586+ | otherwise = Text arr off i : loop j
587+ where j | n < 0xDC00 || n > 0xDFFF = i + 1
588+ | otherwise = i + 2
589+ n = A.unsafeIndex arr i
585590
586591-- | /O(n)/ Return all final segments of the given 'Text', longest
587592-- first.