[project @ 2005-10-22 00:28:21 by ross]
[ghc-base.git] / Data / IntSet.hs
index f86f40b..6220827 100644 (file)
@@ -94,7 +94,6 @@ import Data.Bits
 import Data.Int
 
 import qualified Data.List as List
-import Data.Monoid
 import Data.Typeable
 
 {-
@@ -105,6 +104,7 @@ import qualified List
 -}
 
 #if __GLASGOW_HASKELL__
+import Text.Read
 import Data.Generics.Basics
 import Data.Generics.Instances
 #endif
@@ -465,17 +465,17 @@ split x t
 
 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
 -- element was found in the original set.
-splitMember :: Int -> IntSet -> (Bool,IntSet,IntSet)
+splitMember :: Int -> IntSet -> (IntSet,Bool,IntSet)
 splitMember x t
   = case t of
       Bin p m l r
-        | zero x m  -> let (found,lt,gt) = splitMember x l in (found,lt,union gt r)
-        | otherwise -> let (found,lt,gt) = splitMember x r in (found,union l lt,gt)
+        | zero x m  -> let (lt,found,gt) = splitMember x l in (lt,found,union gt r)
+        | otherwise -> let (lt,found,gt) = splitMember x r in (union l lt,found,gt)
       Tip y 
-        | x>y       -> (False,t,Nil)
-        | x<y       -> (False,Nil,t)
-        | otherwise -> (True,Nil,Nil)
-      Nil -> (False,Nil,Nil)
+        | x>y       -> (t,False,Nil)
+        | x<y       -> (Nil,False,t)
+        | otherwise -> (Nil,True,Nil)
+      Nil -> (Nil,False,Nil)
 
 {----------------------------------------------------------------------
   Map
@@ -580,19 +580,11 @@ instance Ord IntSet where
     -- tentative implementation. See if more efficient exists.
 
 {--------------------------------------------------------------------
-  Monoid 
---------------------------------------------------------------------}
-
-instance Monoid IntSet where
-    mempty = empty
-    mappend = union
-    mconcat = unions
-
-{--------------------------------------------------------------------
   Show
 --------------------------------------------------------------------}
 instance Show IntSet where
-  showsPrec d s  = showSet (toList s)
+  showsPrec p xs = showParen (p > 10) $
+    showString "fromList " . shows (toList xs)
 
 showSet :: [Int] -> ShowS
 showSet []     
@@ -604,6 +596,24 @@ showSet (x:xs)
     showTail (x:xs) = showChar ',' . shows x . showTail xs
 
 {--------------------------------------------------------------------
+  Read
+--------------------------------------------------------------------}
+instance Read IntSet where
+#ifdef __GLASGOW_HASKELL__
+  readPrec = parens $ prec 10 $ do
+    Ident "fromList" <- lexP
+    xs <- readPrec
+    return (fromList xs)
+
+  readListPrec = readListPrecDefault
+#else
+  readsPrec p = readParen (p > 10) $ \ r -> do
+    ("fromList",s) <- lex r
+    (xs,t) <- reads s
+    return (fromList xs,t)
+#endif
+
+{--------------------------------------------------------------------
   Typeable
 --------------------------------------------------------------------}