@@ -22,10 +22,12 @@ module Data.Text.Internal.Fusion.Size
23 , exactSize
23 , exactSize
25 , unknownSize
26 , unknownSize
28 , upperBound
29 , upperBound
@@ -33,14 +35,13 @@ module Data.Text.Internal.Fusion.Size
33import Control.Exception (assert)
35import Control.Exception (assert)
36−data Size = Exact {-# UNPACK #-} !Int -- ^ Exact size.
38+data Size = Between {-# UNPACK #-} !Int {-# UNPACK #-} !Int -- ^ Lower and upper bounds on size.
37− | Max {-# UNPACK #-} !Int -- ^ Upper bound on size.
38− | Unknown -- ^ Unknown size.
39+ | Unknown -- ^ Unknown size.
39 deriving (Eq, Show)
40 deriving (Eq, Show)
41exactly :: Size -> Maybe Int
42exactly :: Size -> Maybe Int
42−exactly (Exact n) = Just n
43+exactly (Between na nb) | na == nb = Just na
43−exactly _ = Nothing
44+exactly _ = Nothing
44{-# INLINE exactly #-}
45{-# INLINE exactly #-}
46exactSize :: Int -> Size
47exactSize :: Int -> Size
@@ -48,7 +49,7 @@ exactSize n =
48#if defined(ASSERTS)
49#if defined(ASSERTS)
49 assert (n >= 0)
50 assert (n >= 0)
51− Exact n
52+ Between n n
52{-# INLINE exactSize #-}
53{-# INLINE exactSize #-}
54maxSize :: Int -> Size
55maxSize :: Int -> Size
@@ -56,9 +57,18 @@ maxSize n =
56#if defined(ASSERTS)
57#if defined(ASSERTS)
57 assert (n >= 0)
58 assert (n >= 0)
60{-# INLINE maxSize #-}
61{-# INLINE maxSize #-}
63+betweenSize :: Int -> Int -> Size
70+{-# INLINE betweenSize #-}
62unknownSize :: Size
72unknownSize :: Size
63unknownSize = Unknown
73unknownSize = Unknown
64{-# INLINE unknownSize #-}
74{-# INLINE unknownSize #-}
@@ -68,7 +78,7 @@ instance Num Size where
68 (-) = subtractSize
78 (-) = subtractSize
69 (*) = mulSize
79 (*) = mulSize
71− fromInteger = f where f = Exact . fromInteger
81+ fromInteger = f where f = exactSize . fromInteger
72 {-# INLINE f #-}
82 {-# INLINE f #-}
74add :: Int -> Int -> Int
84add :: Int -> Int -> Int
@@ -78,20 +88,15 @@ add m n | mn >= 0 = mn
78{-# INLINE add #-}
88{-# INLINE add #-}
80addSize :: Size -> Size -> Size
90addSize :: Size -> Size -> Size
81−addSize (Exact m) (Exact n) = Exact (add m n)
91+addSize (Between ma mb) (Between na nb) = Between (add ma na) (add mb nb)
82−addSize (Exact m) (Max n) = Max (add m n)
83−addSize (Max m) (Exact n) = Max (add m n)
84−addSize (Max m) (Max n) = Max (add m n)
85−addSize _ _ = Unknown
92+addSize _ _ = Unknown
86{-# INLINE addSize #-}
93{-# INLINE addSize #-}
88subtractSize :: Size -> Size -> Size
95subtractSize :: Size -> Size -> Size
89−subtractSize (Exact m) (Exact n) = Exact (max (m-n) 0)
96+subtractSize (Between ma mb) (Between na nb) = Between (max (ma-nb) 0) (max (mb-na) 0)
90−subtractSize (Exact m) (Max _) = Max m
91−subtractSize (Max m) (Exact n) = Max (max (m-n) 0)
92−subtractSize a@(Max _) (Max _) = a
97+subtractSize a@(Between 0 _) Unknown = a
93−subtractSize a@(Max _) Unknown = a
98+subtractSize (Between _ mb) Unknown = Between 0 mb
94−subtractSize _ _ = Unknown
99+subtractSize _ _ = Unknown
95{-# INLINE subtractSize #-}
100{-# INLINE subtractSize #-}
97mul :: Int -> Int -> Int
102mul :: Int -> Int -> Int
@@ -101,48 +106,47 @@ mul m n
101{-# INLINE mul #-}
106{-# INLINE mul #-}
103mulSize :: Size -> Size -> Size
108mulSize :: Size -> Size -> Size
104−mulSize (Exact m) (Exact n) = Exact (mul m n)
109+mulSize (Between ma mb) (Between na nb) = Between (mul ma na) (mul mb nb)
105−mulSize (Exact m) (Max n) = Max (mul m n)
106−mulSize (Max m) (Exact n) = Max (mul m n)
107−mulSize (Max m) (Max n) = Max (mul m n)
108−mulSize _ _ = Unknown
110+mulSize _ _ = Unknown
109{-# INLINE mulSize #-}
111{-# INLINE mulSize #-}
111-- | Minimum of two size hints.
113-- | Minimum of two size hints.
112smaller :: Size -> Size -> Size
114smaller :: Size -> Size -> Size
113−smaller (Exact m) (Exact n) = Exact (m `min` n)
115+smaller a@(Between ma mb) b@(Between na nb)
114−smaller (Exact m) (Max n) = Max (m `min` n)
116+ | mb <= na = a
115−smaller (Exact m) Unknown = Max m
117+ | nb <= ma = b
116−smaller (Max m) (Exact n) = Max (m `min` n)
118+ | otherwise = Between (ma `min` na) (mb `min` nb)
117−smaller (Max m) (Max n) = Max (m `min` n)
118−smaller a@(Max _) Unknown = a
119+smaller a@(Between 0 _) Unknown = a
119−smaller Unknown (Exact n) = Max n
120+smaller (Between _ mb) Unknown = Between 0 mb
121+smaller Unknown b@(Between 0 _) = b
120−smaller Unknown (Max n) = Max n
122+smaller Unknown (Between _ nb) = Between 0 nb
121−smaller Unknown Unknown = Unknown
123+smaller Unknown Unknown = Unknown
122{-# INLINE smaller #-}
124{-# INLINE smaller #-}
124-- | Maximum of two size hints.
126-- | Maximum of two size hints.
125larger :: Size -> Size -> Size
127larger :: Size -> Size -> Size
126−larger (Exact m) (Exact n) = Exact (m `max` n)
128+larger a@(Between ma mb) b@(Between na nb)
127−larger a@(Exact m) b@(Max n) | m >= n = a
129+ | ma >= nb = a
128− | otherwise = b
130+ | na >= mb = b
129−larger a@(Max m) b@(Exact n) | n >= m = b
130− | otherwise = a
131+ | otherwise = Between (ma `max` na) (mb `max` nb)
131−larger (Max m) (Max n) = Max (m `max` n)
132−larger _ _ = Unknown
132+larger _ _ = Unknown
133{-# INLINE larger #-}
133{-# INLINE larger #-}
135-- | Compute the maximum size from a size hint, if possible.
135-- | Compute the maximum size from a size hint, if possible.
136upperBound :: Int -> Size -> Int
136upperBound :: Int -> Size -> Int
137−upperBound _ (Exact n) = n
137+upperBound _ (Between n _) = n
138−upperBound _ (Max n) = n
139−upperBound k _ = k
138+upperBound k _ = k
140{-# INLINE upperBound #-}
139{-# INLINE upperBound #-}
141+-- | Compute the maximum size from a size hint, if possible.
142+lowerBound :: Int -> Size -> Int
143+lowerBound _ (Between n _) = n
145+{-# INLINE lowerBound #-}
142isEmpty :: Size -> Bool
147isEmpty :: Size -> Bool
143−isEmpty (Exact n) = n <= 0
148+isEmpty (Between _ n) = n <= 0
144−isEmpty (Max n) = n <= 0
145−isEmpty _ = False
149+isEmpty _ = False
146{-# INLINE isEmpty #-}
150{-# INLINE isEmpty #-}
148overflowError :: Int
152overflowError :: Int