@@ -22,10 +22,13 @@ module Data.Text.Internal.Fusion.Size
2222 , exactly
2323 , exactSize
2424 , maxSize
25+ , betweenSize
2526 , unknownSize
2627 , smaller
2728 , larger
2829 , upperBound
30+ , lowerBound
31+ , compareSize
2932 , isEmpty
3033 ) where
3134
@@ -33,14 +36,13 @@ module Data.Text.Internal.Fusion.Size
3336import Control.Exception (assert)
3437#endif
3538
36−data Size = Exact {-# UNPACK #-} !Int -- ^ Exact size.
37− | Max {-# UNPACK #-} !Int -- ^ Upper bound on size.
38− | Unknown -- ^ Unknown size.
39+data Size = Between {-# UNPACK #-} !Int {-# UNPACK #-} !Int -- ^ Lower and upper bounds on size.
40+ | Unknown -- ^ Unknown size.
3941 deriving (Eq, Show)
4042
4143exactly :: Size -> Maybe Int
42−exactly (Exact n) = Just n
43−exactly _ = Nothing
44+exactly (Between na nb) | na == nb = Just na
45+exactly _ = Nothing
4446{-# INLINE exactly #-}
4547
4648exactSize :: Int -> Size
@@ -48,7 +50,7 @@ exactSize n =
4850#if defined(ASSERTS)
4951 assert (n >= 0)
5052#endif
51− Exact n
53+ Between n n
5254{-# INLINE exactSize #-}
5355
5456maxSize :: Int -> Size
@@ -56,9 +58,18 @@ maxSize n =
5658#if defined(ASSERTS)
5759 assert (n >= 0)
5860#endif
59− Max n
61+ Between 0 n
6062{-# INLINE maxSize #-}
6163
64+betweenSize :: Int -> Int -> Size
65+betweenSize m n =
66+#if defined(ASSERTS)
67+ assert (m >= 0)
68+ assert (n >= m)
69+#endif
70+ Between m n
71+{-# INLINE betweenSize #-}
72+
6273unknownSize :: Size
6374unknownSize = Unknown
6475{-# INLINE unknownSize #-}
@@ -68,7 +79,7 @@ instance Num Size where
6879 (-) = subtractSize
6980 (*) = mulSize
7081
71− fromInteger = f where f = Exact . fromInteger
82+ fromInteger = f where f = exactSize . fromInteger
7283 {-# INLINE f #-}
7384
7485add :: Int -> Int -> Int
@@ -78,20 +89,15 @@ add m n | mn >= 0 = mn
7889{-# INLINE add #-}
7990
8091addSize :: Size -> Size -> Size
81−addSize (Exact m) (Exact n) = Exact (add m n)
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 (Between ma mb) (Between na nb) = Between (add ma na) (add mb nb)
93+addSize _ _ = Unknown
8694{-# INLINE addSize #-}
8795
8896subtractSize :: Size -> Size -> Size
89−subtractSize (Exact m) (Exact n) = Exact (max (m-n) 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
93−subtractSize a@(Max _) Unknown = a
94−subtractSize _ _ = Unknown
97+subtractSize (Between ma mb) (Between na nb) = Between (max (ma-nb) 0) (max (mb-na) 0)
98+subtractSize a@(Between 0 _) Unknown = a
99+subtractSize (Between _ mb) Unknown = Between 0 mb
100+subtractSize _ _ = Unknown
95101{-# INLINE subtractSize #-}
96102
97103mul :: Int -> Int -> Int
@@ -101,48 +107,55 @@ mul m n
101107{-# INLINE mul #-}
102108
103109mulSize :: Size -> Size -> Size
104−mulSize (Exact m) (Exact n) = Exact (mul m n)
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 (Between ma mb) (Between na nb) = Between (mul ma na) (mul mb nb)
111+mulSize _ _ = Unknown
109112{-# INLINE mulSize #-}
110113
111114-- | Minimum of two size hints.
112115smaller :: Size -> Size -> Size
113−smaller (Exact m) (Exact n) = Exact (m `min` n)
114−smaller (Exact m) (Max n) = Max (m `min` n)
115−smaller (Exact m) Unknown = Max m
116−smaller (Max m) (Exact n) = Max (m `min` n)
117−smaller (Max m) (Max n) = Max (m `min` n)
118−smaller a@(Max _) Unknown = a
119−smaller Unknown (Exact n) = Max n
120−smaller Unknown (Max n) = Max n
121−smaller Unknown Unknown = Unknown
116+smaller a@(Between ma mb) b@(Between na nb)
117+ | mb <= na = a
118+ | nb <= ma = b
119+ | otherwise = Between (ma `min` na) (mb `min` nb)
120+smaller a@(Between 0 _) Unknown = a
121+smaller (Between _ mb) Unknown = Between 0 mb
122+smaller Unknown b@(Between 0 _) = b
123+smaller Unknown (Between _ nb) = Between 0 nb
124+smaller Unknown Unknown = Unknown
122125{-# INLINE smaller #-}
123126
124127-- | Maximum of two size hints.
125128larger :: Size -> Size -> Size
126−larger (Exact m) (Exact n) = Exact (m `max` n)
127−larger a@(Exact m) b@(Max n) | m >= n = a
128− | otherwise = b
129−larger a@(Max m) b@(Exact n) | n >= m = b
130− | otherwise = a
131−larger (Max m) (Max n) = Max (m `max` n)
132−larger _ _ = Unknown
129+larger a@(Between ma mb) b@(Between na nb)
130+ | ma >= nb = a
131+ | na >= mb = b
132+ | otherwise = Between (ma `max` na) (mb `max` nb)
133+larger _ _ = Unknown
133134{-# INLINE larger #-}
134135
135136-- | Compute the maximum size from a size hint, if possible.
136137upperBound :: Int -> Size -> Int
137−upperBound _ (Exact n) = n
138−upperBound _ (Max n) = n
139−upperBound k _ = k
138+upperBound _ (Between n _) = n
139+upperBound k _ = k
140140{-# INLINE upperBound #-}
141141
142+-- | Compute the maximum size from a size hint, if possible.
143+lowerBound :: Int -> Size -> Int
144+lowerBound _ (Between n _) = n
145+lowerBound k _ = k
146+{-# INLINE lowerBound #-}
147+
148+compareSize :: Size -> Int -> Maybe Ordering
149+compareSize (Between ma mb) n
150+ | mb < n = Just LT
151+ | ma > n = Just GT
152+ | ma == n && mb == n = Just EQ
153+compareSize _ _ = Nothing
154+
155+
142156isEmpty :: Size -> Bool
143−isEmpty (Exact n) = n <= 0
144−isEmpty (Max n) = n <= 0
145−isEmpty _ = False
157+isEmpty (Between _ n) = n <= 0
158+isEmpty _ = False
146159{-# INLINE isEmpty #-}
147160
148161overflowError :: Int