) where
import Prelude hiding (filter,foldr,null,map)
-import Data.Monoid
import qualified Data.List as List
import Data.Typeable
isSubsetOfX (Bin _ x l r) t
= found && isSubsetOfX l lt && isSubsetOfX r gt
where
- (found,lt,gt) = splitMember x t
+ (lt,found,gt) = splitMember x t
{--------------------------------------------------------------------
| found = join x tl tr
| otherwise = merge tl tr
where
- (found,lt,gt) = splitMember x t
+ (lt,found,gt) = splitMember x t
tl = intersect' lt l
tr = intersect' gt r
{--------------------------------------------------------------------
Lists
--------------------------------------------------------------------}
--- | /O(n)/. Convert the set to an ascending list of elements.
+-- | /O(n)/. Convert the set to a list of elements.
toList :: Set a -> [a]
toList s
= toAscList s
compare s1 s2 = compare (toAscList s1) (toAscList s2)
{--------------------------------------------------------------------
- Monoid
---------------------------------------------------------------------}
-
-instance Ord a => Monoid (Set a) where
- mempty = empty
- mappend = union
- mconcat = unions
-
-{--------------------------------------------------------------------
Show
--------------------------------------------------------------------}
instance Show a => Show (Set a) where
-- | /O(log n)/. Performs a 'split' but also returns whether the pivot
-- element was found in the original set.
-splitMember :: Ord a => a -> Set a -> (Bool,Set a,Set a)
-splitMember x Tip = (False,Tip,Tip)
+splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
+splitMember x Tip = (Tip,False,Tip)
splitMember x (Bin sy y l r)
= case compare x y of
- LT -> let (found,lt,gt) = splitMember x l in (found,lt,join y gt r)
- GT -> let (found,lt,gt) = splitMember x r in (found,join y l lt,gt)
- EQ -> (True,l,r)
+ LT -> let (lt,found,gt) = splitMember x l in (lt,found,join y gt r)
+ GT -> let (lt,found,gt) = splitMember x r in (join y l lt,found,gt)
+ EQ -> (l,True,r)
{--------------------------------------------------------------------
Utility functions that maintain the balance properties of the tree.