on master help sign in
Add Ord instances. Oops!
3a4bbe6Bryan O'Sullivan
Data/Text.hs+5−1
@@ -144,7 +144,7 @@ module Data.Text
144 ) where
144 ) where
145
145
146import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
146import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
147− Eq(..), (++),
147+ Eq(..), Ord(..), (++),
148 Read(..), Show(..),
148 Read(..), Show(..),
149 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
149 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
150 not, return, otherwise)
150 not, return, otherwise)
@@ -177,6 +177,10 @@ instance Eq Text where
177 t1 == t2 = stream t1 == stream t2
177 t1 == t2 = stream t1 == stream t2
178 {-# INLINE (==) #-}
178 {-# INLINE (==) #-}
179
179
180+instance Ord Text where
181+ compare t1 t2 = compare (stream t1) (stream t2)
182+ {-# INLINE compare #-}
183+
180instance Show Text where
184instance Show Text where
181 showsPrec p ps r = showsPrec p (unpack ps) r
185 showsPrec p ps r = showsPrec p (unpack ps) r
182
186
Data/Text/Fusion/Internal.hs+29−10
@@ -41,9 +41,12 @@ instance Show a => Show (Step s a)
41 show (Skip _) = "Skip"
41 show (Skip _) = "Skip"
42 show (Yield x _) = "Yield " ++ show x
42 show (Yield x _) = "Yield " ++ show x
43
43
44−instance Eq a => Eq (Stream a) where
44+instance (Eq a) => Eq (Stream a) where
45 (==) = eq
45 (==) = eq
46
46
47+instance (Ord a) => Ord (Stream a) where
48+ compare = cmp
49+
47-- The length hint in a Stream has two roles. If its value is zero,
50-- The length hint in a Stream has two roles. If its value is zero,
48-- we trust it, and treat the stream as empty. Otherwise, we treat it
51-- we trust it, and treat the stream as empty. Otherwise, we treat it
49-- as a hint: it should usually be accurate, so we use it when
52-- as a hint: it should usually be accurate, so we use it when
@@ -76,19 +79,35 @@ empty = Stream next () 0
76
79
77-- | /O(n)/ Determines if two streams are equal.
80-- | /O(n)/ Determines if two streams are equal.
78eq :: (Eq a) => Stream a -> Stream a -> Bool
81eq :: (Eq a) => Stream a -> Stream a -> Bool
79−eq (Stream next1 s1 _) (Stream next2 s2 _) = cmp (next1 s1) (next2 s2)
82+eq (Stream next1 s1 _) (Stream next2 s2 _) = loop (next1 s1) (next2 s2)
80 where
83 where
81− cmp Done Done = True
84+ loop Done Done = True
82− cmp (Skip s1') (Skip s2') = cmp (next1 s1') (next2 s2')
85+ loop (Skip s1') (Skip s2') = loop (next1 s1') (next2 s2')
83− cmp (Skip s1') x2 = cmp (next1 s1') x2
86+ loop (Skip s1') x2 = loop (next1 s1') x2
84− cmp x1 (Skip s2') = cmp x1 (next2 s2')
87+ loop x1 (Skip s2') = loop x1 (next2 s2')
85− cmp Done _ = False
88+ loop Done _ = False
86− cmp _ Done = False
89+ loop _ Done = False
87− cmp (Yield x1 s1') (Yield x2 s2') = x1 == x2 &&
90+ loop (Yield x1 s1') (Yield x2 s2') = x1 == x2 &&
88− cmp (next1 s1') (next2 s2')
91+ loop (next1 s1') (next2 s2')
89{-# INLINE [0] eq #-}
92{-# INLINE [0] eq #-}
90{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
93{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
91
94
95+cmp :: (Ord a) => Stream a -> Stream a -> Ordering
96+cmp (Stream next1 s1 _) (Stream next2 s2 _) = loop (next1 s1) (next2 s2)
97+ where
98+ loop Done Done = EQ
99+ loop (Skip s1') (Skip s2') = loop (next1 s1') (next2 s2')
100+ loop (Skip s1') x2 = loop (next1 s1') x2
101+ loop x1 (Skip s2') = loop x1 (next2 s2')
102+ loop Done _ = LT
103+ loop _ Done = GT
104+ loop (Yield x1 s1') (Yield x2 s2') =
105+ case compare x1 x2 of
106+ EQ -> loop (next1 s1') (next2 s2')
107+ other -> other
108+{-# INLINE [0] cmp #-}
109+{-# SPECIALISE cmp :: Stream Char -> Stream Char -> Ordering #-}
110+
92streamList :: [a] -> Stream a
111streamList :: [a] -> Stream a
93{-# INLINE streamList #-}
112{-# INLINE streamList #-}
94streamList [] = empty
113streamList [] = empty
Data/Text/Lazy.hs+4
@@ -157,6 +157,10 @@ instance Eq Text where
157 t1 == t2 = stream t1 == stream t2
157 t1 == t2 = stream t1 == stream t2
158 {-# INLINE (==) #-}
158 {-# INLINE (==) #-}
159
159
160+instance Ord Text where
161+ compare t1 t2 = compare (stream t1) (stream t2)
162+ {-# INLINE compare #-}
163+
160instance Show Text where
164instance Show Text where
161 showsPrec p ps r = showsPrec p (unpack ps) r
165 showsPrec p ps r = showsPrec p (unpack ps) r
162
166