Bryan O'Sullivan avatar Bryan O'Sullivan committed 0b2cf98

Add Ord instances. Oops!

Comments (0)

Files changed (3)

     ) where
 
 import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
-                Eq(..), (++),
+                Eq(..), Ord(..), (++),
                 Read(..), Show(..),
                 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
                 not, return, otherwise)
     t1 == t2 = stream t1 == stream t2
     {-# INLINE (==) #-}
 
+instance Ord Text where
+    compare t1 t2 = compare (stream t1) (stream t2)
+    {-# INLINE compare #-}
+
 instance Show Text where
     showsPrec p ps r = showsPrec p (unpack ps) r
 

Data/Text/Fusion/Internal.hs

           show (Skip _)    = "Skip"
           show (Yield x _) = "Yield " ++ show x
 
-instance Eq a => Eq (Stream a) where
+instance (Eq a) => Eq (Stream a) where
     (==) = eq
 
+instance (Ord a) => Ord (Stream a) where
+    compare = cmp
+
 -- The length hint in a Stream has two roles.  If its value is zero,
 -- we trust it, and treat the stream as empty.  Otherwise, we treat it
 -- as a hint: it should usually be accurate, so we use it when
 
 -- | /O(n)/ Determines if two streams are equal.
 eq :: (Eq a) => Stream a -> Stream a -> Bool
-eq (Stream next1 s1 _) (Stream next2 s2 _) = cmp (next1 s1) (next2 s2)
+eq (Stream next1 s1 _) (Stream next2 s2 _) = loop (next1 s1) (next2 s2)
     where
-      cmp Done Done                     = True
-      cmp (Skip s1')     (Skip s2')     = cmp (next1 s1') (next2 s2')
-      cmp (Skip s1')     x2             = cmp (next1 s1') x2
-      cmp x1             (Skip s2')     = cmp x1          (next2 s2')
-      cmp Done _                        = False
-      cmp _    Done                     = False
-      cmp (Yield x1 s1') (Yield x2 s2') = x1 == x2 &&
-                                          cmp (next1 s1') (next2 s2')
+      loop Done Done                     = True
+      loop (Skip s1')     (Skip s2')     = loop (next1 s1') (next2 s2')
+      loop (Skip s1')     x2             = loop (next1 s1') x2
+      loop x1             (Skip s2')     = loop x1          (next2 s2')
+      loop Done _                        = False
+      loop _    Done                     = False
+      loop (Yield x1 s1') (Yield x2 s2') = x1 == x2 &&
+                                           loop (next1 s1') (next2 s2')
 {-# INLINE [0] eq #-}
 {-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
 
+cmp :: (Ord a) => Stream a -> Stream a -> Ordering
+cmp (Stream next1 s1 _) (Stream next2 s2 _) = loop (next1 s1) (next2 s2)
+    where
+      loop Done Done                     = EQ
+      loop (Skip s1')     (Skip s2')     = loop (next1 s1') (next2 s2')
+      loop (Skip s1')     x2             = loop (next1 s1') x2
+      loop x1             (Skip s2')     = loop x1          (next2 s2')
+      loop Done _                        = LT
+      loop _    Done                     = GT
+      loop (Yield x1 s1') (Yield x2 s2') =
+          case compare x1 x2 of
+            EQ    -> loop (next1 s1') (next2 s2')
+            other -> other
+{-# INLINE [0] cmp #-}
+{-# SPECIALISE cmp :: Stream Char -> Stream Char -> Ordering #-}
+
 streamList :: [a] -> Stream a
 {-# INLINE streamList #-}
 streamList [] = empty

Data/Text/Lazy.hs

     t1 == t2 = stream t1 == stream t2
     {-# INLINE (==) #-}
 
+instance Ord Text where
+    compare t1 t2 = compare (stream t1) (stream t2)
+    {-# INLINE compare #-}
+
 instance Show Text where
     showsPrec p ps r = showsPrec p (unpack ps) r
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.