-- | /O(log n)/. Performs a 'split' but also returns whether the pivot
-- key was found in the original map.
-splitLookup :: Key -> IntMap a -> (Maybe a,IntMap a,IntMap a)
+splitLookup :: Key -> IntMap a -> (IntMap a,Maybe a,IntMap a)
splitLookup k t
= case t of
Bin p m l r
- | zero k m -> let (found,lt,gt) = splitLookup k l in (found,lt,union gt r)
- | otherwise -> let (found,lt,gt) = splitLookup k r in (found,union l lt,gt)
+ | zero k m -> let (lt,found,gt) = splitLookup k l in (lt,found,union gt r)
+ | otherwise -> let (lt,found,gt) = splitLookup k r in (union l lt,found,gt)
Tip ky y
- | k>ky -> (Nothing,t,Nil)
- | k<ky -> (Nothing,Nil,t)
- | otherwise -> (Just y,Nil,Nil)
- Nil -> (Nothing,Nil,Nil)
+ | k>ky -> (t,Nothing,Nil)
+ | k<ky -> (Nil,Nothing,t)
+ | otherwise -> (Nil,Just y,Nil)
+ Nil -> (Nil,Nothing,Nil)
{--------------------------------------------------------------------
Fold
-- | /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
Nothing -> merge tl tr
Just y -> join kx (f kx y x) tl tr
where
- (found,lt,gt) = splitLookup kx t
+ (lt,found,gt) = splitLookup kx t
tl = intersectWithKey f lt l
tr = intersectWithKey f gt r
Nothing -> False
Just y -> f x y && submap' f l lt && submap' f r gt
where
- (found,lt,gt) = splitLookup kx t
+ (lt,found,gt) = splitLookup kx t
-- | /O(n+m)/. Is this a proper submap? (ie. a submap but not equal).
-- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@).
-- | /O(log n)/. The expression (@'splitLookup' k map@) splits a map just
-- like 'split' but also returns @'lookup' k map@.
-splitLookup :: Ord k => k -> Map k a -> (Maybe a,Map k a,Map k a)
-splitLookup k Tip = (Nothing,Tip,Tip)
+splitLookup :: Ord k => k -> Map k a -> (Map k a,Maybe a,Map k a)
+splitLookup k Tip = (Tip,Nothing,Tip)
splitLookup k (Bin sx kx x l r)
= case compare k kx of
- LT -> let (z,lt,gt) = splitLookup k l in (z,lt,join kx x gt r)
- GT -> let (z,lt,gt) = splitLookup k r in (z,join kx x l lt,gt)
- EQ -> (Just x,l,r)
+ LT -> let (lt,z,gt) = splitLookup k l in (lt,z,join kx x gt r)
+ GT -> let (lt,z,gt) = splitLookup k r in (join kx x l lt,z,gt)
+ EQ -> (l,Just x,r)
{--------------------------------------------------------------------
Utility functions that maintain the balance properties of the tree.
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
-- | /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.