{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
#ifdef __GLASGOW_HASKELL__
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
#endif
{-# OPTIONS_HADDOCK not-home #-}
module Data.Set.Internal (
Set(..)
, Size
, (\\)
, null
, size
, member
, notMember
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, isSubsetOf
, isProperSubsetOf
, disjoint
, empty
, singleton
, insert
, delete
, pop
, alterF
, powerSet
, union
, unions
, difference
, intersection
, intersections
, symmetricDifference
, cartesianProduct
, disjointUnion
, Intersection(..)
, filter
, filterA
, takeWhileAntitone
, dropWhileAntitone
, spanAntitone
, mapMaybe
, partition
, split
, splitMember
, splitRoot
, lookupIndex
, findIndex
, elemAt
, deleteAt
, take
, drop
, splitAt
, map
, mapMonotonic
, foldr
, foldl
, foldr'
, foldl'
, fold
, lookupMin
, lookupMax
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, maxView
, minView
, elems
, toList
, fromList
, toAscList
, toDescList
, fromAscList
, fromDistinctAscList
, fromDescList
, fromDistinctDescList
, showTree
, showTreeWith
, valid
, WhenMissing(..)
, SimpleWhenMissing
, dropMissing
, preserveMissing
, filterMissing
, filterAMissing
, whenMissing
, runWhenMissing
, WhenMatched(..)
, SimpleWhenMatched
, dropMatched
, preserveMatched
, filterMatched
, filterAMatched
, runWhenMatched
, merge
, mergeA
, bin
, balanced
, link
, link2
, balanceL
, balanceR
, glue
, insertMin
, insertMax
) where
import Utils.Containers.Internal.Prelude hiding
(filter,foldl,foldl',foldr,null,map,take,drop,splitAt)
import Prelude ()
import Control.Applicative (Const(..), liftA3)
import qualified Data.List as List
import Data.Semigroup (Semigroup(..), stimesIdempotentMonoid, stimesIdempotent)
import Data.Functor.Classes
import Data.Functor.Identity (Identity(..))
import qualified Data.Foldable as Foldable
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
import Data.List.NonEmpty (NonEmpty(..))
import Utils.Containers.Internal.Strict
(StrictPair(..), StrictTriple(..), toPair)
import Utils.Containers.Internal.PtrEquality
import Utils.Containers.Internal.EqOrdUtil (EqM(..), OrdM(..))
#if defined(__GLASGOW_HASKELL__) || defined(__MHS__)
import Text.Read ( readPrec, Read (..), Lexeme (..), parens, prec
, lexP, readListPrecDefault )
#endif
#if __GLASGOW_HASKELL__
import GHC.Exts ( build, lazy )
import qualified GHC.Exts as GHCExts
import Data.Data
# if __GLASGOW_HASKELL__ >= 914
import Language.Haskell.TH.Lift (Lift)
# else
import Language.Haskell.TH.Syntax (Lift)
import Language.Haskell.TH ()
# endif
import Data.Coerce (coerce)
#endif
infixl 9 \\
(\\) :: Ord a => Set a -> Set a -> Set a
Set a
m1 \\ :: forall a. Ord a => Set a -> Set a -> Set a
\\ Set a
m2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
difference Set a
m1 Set a
m2
{-# INLINABLE (\\) #-}
data Set a = Bin {-# UNPACK #-} !Size !a !(Set a) !(Set a)
| Tip
type Size = Int
#ifdef __GLASGOW_HASKELL__
type role Set nominal
deriving instance Lift a => Lift (Set a)
#endif
instance Ord a => Monoid (Set a) where
mempty :: Set a
mempty = Set a
forall a. Set a
empty
mconcat :: [Set a] -> Set a
mconcat = [Set a] -> Set a
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
unions
#if !MIN_VERSION_base(4,11,0)
mappend = (<>)
#endif
instance Ord a => Semigroup (Set a) where
<> :: Set a -> Set a -> Set a
(<>) = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
union
stimes :: forall b. Integral b => b -> Set a -> Set a
stimes = b -> Set a -> Set a
forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid
instance Foldable.Foldable Set where
fold :: forall m. Monoid m => Set m -> m
fold = Set m -> m
forall m. Monoid m => Set m -> m
go
where go :: Set t -> t
go Set t
Tip = t
forall a. Monoid a => a
mempty
go (Bin Int
1 t
k Set t
_ Set t
_) = t
k
go (Bin Int
_ t
k Set t
l Set t
r) = Set t -> t
go Set t
l t -> t -> t
forall a. Monoid a => a -> a -> a
`mappend` (t
k t -> t -> t
forall a. Monoid a => a -> a -> a
`mappend` Set t -> t
go Set t
r)
{-# INLINABLE fold #-}
foldr :: forall a b. (a -> b -> b) -> b -> Set a -> b
foldr = (a -> b -> b) -> b -> Set a -> b
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr
{-# INLINE foldr #-}
foldl :: forall b a. (b -> a -> b) -> b -> Set a -> b
foldl = (b -> a -> b) -> b -> Set a -> b
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl
{-# INLINE foldl #-}
foldMap :: forall m a. Monoid m => (a -> m) -> Set a -> m
foldMap a -> m
f Set a
t = Set a -> m
go Set a
t
where go :: Set a -> m
go Set a
Tip = m
forall a. Monoid a => a
mempty
go (Bin Int
1 a
k Set a
_ Set a
_) = a -> m
f a
k
go (Bin Int
_ a
k Set a
l Set a
r) = Set a -> m
go Set a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` (a -> m
f a
k m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Set a -> m
go Set a
r)
{-# INLINE foldMap #-}
foldl' :: forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' = (b -> a -> b) -> b -> Set a -> b
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl'
{-# INLINE foldl' #-}
foldr' :: forall a b. (a -> b -> b) -> b -> Set a -> b
foldr' = (a -> b -> b) -> b -> Set a -> b
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr'
{-# INLINE foldr' #-}
length :: forall a. Set a -> Int
length = Set a -> Int
forall a. Set a -> Int
size
{-# INLINE length #-}
null :: forall a. Set a -> Bool
null = Set a -> Bool
forall a. Set a -> Bool
null
{-# INLINE null #-}
toList :: forall a. Set a -> [a]
toList = Set a -> [a]
forall a. Set a -> [a]
toList
{-# INLINE toList #-}
elem :: forall a. Eq a => a -> Set a -> Bool
elem = a -> Set a -> Bool
forall a. Eq a => a -> Set a -> Bool
go
where go :: t -> Set t -> Bool
go !t
_ Set t
Tip = Bool
False
go t
x (Bin Int
_ t
y Set t
l Set t
r) = t
x t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
y Bool -> Bool -> Bool
|| t -> Set t -> Bool
go t
x Set t
l Bool -> Bool -> Bool
|| t -> Set t -> Bool
go t
x Set t
r
{-# INLINABLE elem #-}
minimum :: forall a. Ord a => Set a -> a
minimum = Set a -> a
forall a. Set a -> a
findMin
{-# INLINE minimum #-}
maximum :: forall a. Ord a => Set a -> a
maximum = Set a -> a
forall a. Set a -> a
findMax
{-# INLINE maximum #-}
sum :: forall a. Num a => Set a -> a
sum = (a -> a -> a) -> a -> Set a -> a
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINABLE sum #-}
product :: forall a. Num a => Set a -> a
product = (a -> a -> a) -> a -> Set a -> a
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
{-# INLINABLE product #-}
#if __GLASGOW_HASKELL__
instance (Data a, Ord a) => Data (Set a) where
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Set a -> c (Set a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z Set a
set = ([a] -> Set a) -> c ([a] -> Set a)
forall g. g -> c g
z [a] -> Set a
forall a. Ord a => [a] -> Set a
fromList c ([a] -> Set a) -> [a] -> c (Set a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` (Set a -> [a]
forall a. Set a -> [a]
toList Set a
set)
toConstr :: Set a -> Constr
toConstr Set a
_ = Constr
fromListConstr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Set a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Int
constrIndex Constr
c of
Int
1 -> c ([a] -> Set a) -> c (Set a)
forall b r. Data b => c (b -> r) -> c r
k (([a] -> Set a) -> c ([a] -> Set a)
forall r. r -> c r
z [a] -> Set a
forall a. Ord a => [a] -> Set a
fromList)
Int
_ -> [Char] -> c (Set a)
forall a. HasCallStack => [Char] -> a
error [Char]
"gunfold"
dataTypeOf :: Set a -> DataType
dataTypeOf Set a
_ = DataType
setDataType
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Set a))
dataCast1 forall d. Data d => c (t d)
f = c (t a) -> Maybe (c (Set a))
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
(a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
f
fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> [Char] -> [[Char]] -> Fixity -> Constr
mkConstr DataType
setDataType [Char]
"fromList" [] Fixity
Prefix
setDataType :: DataType
setDataType :: DataType
setDataType = [Char] -> [Constr] -> DataType
mkDataType [Char]
"Data.Set.Internal.Set" [Constr
fromListConstr]
#endif
null :: Set a -> Bool
null :: forall a. Set a -> Bool
null Set a
Tip = Bool
True
null (Bin {}) = Bool
False
{-# INLINE null #-}
size :: Set a -> Int
size :: forall a. Set a -> Int
size Set a
Tip = Int
0
size (Bin Int
sz a
_ Set a
_ Set a
_) = Int
sz
{-# INLINE size #-}
member :: Ord a => a -> Set a -> Bool
member :: forall a. Ord a => a -> Set a -> Bool
member = a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
go
where
go :: t -> Set t -> Bool
go !t
_ Set t
Tip = Bool
False
go t
x (Bin Int
_ t
y Set t
l Set t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
x t
y of
Ordering
LT -> t -> Set t -> Bool
go t
x Set t
l
Ordering
GT -> t -> Set t -> Bool
go t
x Set t
r
Ordering
EQ -> Bool
True
{-# INLINABLE member #-}
notMember :: Ord a => a -> Set a -> Bool
notMember :: forall a. Ord a => a -> Set a -> Bool
notMember a
a Set a
t = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
member a
a Set a
t
{-# INLINABLE notMember #-}
lookupLT :: Ord a => a -> Set a -> Maybe a
lookupLT :: forall a. Ord a => a -> Set a -> Maybe a
lookupLT = a -> Set a -> Maybe a
forall a. Ord a => a -> Set a -> Maybe a
goNothing
where
goNothing :: a -> Set a -> Maybe a
goNothing !a
_ Set a
Tip = Maybe a
forall a. Maybe a
Nothing
goNothing a
x (Bin Int
_ a
y Set a
l Set a
r) | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
y = a -> Set a -> Maybe a
goNothing a
x Set a
l
| Bool
otherwise = a -> a -> Set a -> Maybe a
forall {t}. Ord t => t -> t -> Set t -> Maybe t
goJust a
x a
y Set a
r
goJust :: t -> t -> Set t -> Maybe t
goJust !t
_ t
best Set t
Tip = t -> Maybe t
forall a. a -> Maybe a
Just t
best
goJust t
x t
best (Bin Int
_ t
y Set t
l Set t
r) | t
x t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
y = t -> t -> Set t -> Maybe t
goJust t
x t
best Set t
l
| Bool
otherwise = t -> t -> Set t -> Maybe t
goJust t
x t
y Set t
r
{-# INLINABLE lookupLT #-}
lookupGT :: Ord a => a -> Set a -> Maybe a
lookupGT :: forall a. Ord a => a -> Set a -> Maybe a
lookupGT = a -> Set a -> Maybe a
forall a. Ord a => a -> Set a -> Maybe a
goNothing
where
goNothing :: t -> Set t -> Maybe t
goNothing !t
_ Set t
Tip = Maybe t
forall a. Maybe a
Nothing
goNothing t
x (Bin Int
_ t
y Set t
l Set t
r) | t
x t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
y = t -> t -> Set t -> Maybe t
forall {t}. Ord t => t -> t -> Set t -> Maybe t
goJust t
x t
y Set t
l
| Bool
otherwise = t -> Set t -> Maybe t
goNothing t
x Set t
r
goJust :: t -> t -> Set t -> Maybe t
goJust !t
_ t
best Set t
Tip = t -> Maybe t
forall a. a -> Maybe a
Just t
best
goJust t
x t
best (Bin Int
_ t
y Set t
l Set t
r) | t
x t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
y = t -> t -> Set t -> Maybe t
goJust t
x t
y Set t
l
| Bool
otherwise = t -> t -> Set t -> Maybe t
goJust t
x t
best Set t
r
{-# INLINABLE lookupGT #-}
lookupLE :: Ord a => a -> Set a -> Maybe a
lookupLE :: forall a. Ord a => a -> Set a -> Maybe a
lookupLE = a -> Set a -> Maybe a
forall a. Ord a => a -> Set a -> Maybe a
goNothing
where
goNothing :: a -> Set a -> Maybe a
goNothing !a
_ Set a
Tip = Maybe a
forall a. Maybe a
Nothing
goNothing a
x (Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of Ordering
LT -> a -> Set a -> Maybe a
goNothing a
x Set a
l
Ordering
EQ -> a -> Maybe a
forall a. a -> Maybe a
Just a
y
Ordering
GT -> a -> a -> Set a -> Maybe a
forall {t}. Ord t => t -> t -> Set t -> Maybe t
goJust a
x a
y Set a
r
goJust :: t -> t -> Set t -> Maybe t
goJust !t
_ t
best Set t
Tip = t -> Maybe t
forall a. a -> Maybe a
Just t
best
goJust t
x t
best (Bin Int
_ t
y Set t
l Set t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
x t
y of Ordering
LT -> t -> t -> Set t -> Maybe t
goJust t
x t
best Set t
l
Ordering
EQ -> t -> Maybe t
forall a. a -> Maybe a
Just t
y
Ordering
GT -> t -> t -> Set t -> Maybe t
goJust t
x t
y Set t
r
{-# INLINABLE lookupLE #-}
lookupGE :: Ord a => a -> Set a -> Maybe a
lookupGE :: forall a. Ord a => a -> Set a -> Maybe a
lookupGE = a -> Set a -> Maybe a
forall a. Ord a => a -> Set a -> Maybe a
goNothing
where
goNothing :: t -> Set t -> Maybe t
goNothing !t
_ Set t
Tip = Maybe t
forall a. Maybe a
Nothing
goNothing t
x (Bin Int
_ t
y Set t
l Set t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
x t
y of Ordering
LT -> t -> t -> Set t -> Maybe t
forall {t}. Ord t => t -> t -> Set t -> Maybe t
goJust t
x t
y Set t
l
Ordering
EQ -> t -> Maybe t
forall a. a -> Maybe a
Just t
y
Ordering
GT -> t -> Set t -> Maybe t
goNothing t
x Set t
r
goJust :: a -> a -> Set a -> Maybe a
goJust !a
_ a
best Set a
Tip = a -> Maybe a
forall a. a -> Maybe a
Just a
best
goJust a
x a
best (Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of Ordering
LT -> a -> a -> Set a -> Maybe a
goJust a
x a
y Set a
l
Ordering
EQ -> a -> Maybe a
forall a. a -> Maybe a
Just a
y
Ordering
GT -> a -> a -> Set a -> Maybe a
goJust a
x a
best Set a
r
{-# INLINABLE lookupGE #-}
empty :: Set a
empty :: forall a. Set a
empty = Set a
forall a. Set a
Tip
{-# INLINE empty #-}
singleton :: a -> Set a
singleton :: forall a. a -> Set a
singleton a
x = Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip
{-# INLINE singleton #-}
insert :: Ord a => a -> Set a -> Set a
insert :: forall a. Ord a => a -> Set a -> Set a
insert a
x0 = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
x0 a
x0
where
go :: Ord a => a -> a -> Set a -> Set a
go :: forall a. Ord a => a -> a -> Set a -> Set a
go a
orig !a
_ Set a
Tip = a -> Set a
forall a. a -> Set a
singleton (a -> a
forall a. a -> a
lazy a
orig)
go a
orig !a
x t :: Set a
t@(Bin Int
sz a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT | Set a
l' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y Set a
l' Set a
r
where !l' :: Set a
l' = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
orig a
x Set a
l
Ordering
GT | Set a
r' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l Set a
r'
where !r' :: Set a
r' = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
orig a
x Set a
r
Ordering
EQ | a -> a
forall a. a -> a
lazy a
orig a -> Bool -> Bool
forall a b. a -> b -> b
`seq` (a
orig a -> a -> Bool
forall a. a -> a -> Bool
`ptrEq` a
y) -> Set a
t
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
sz (a -> a
forall a. a -> a
lazy a
orig) Set a
l Set a
r
{-# INLINABLE insert #-}
#ifndef __GLASGOW_HASKELL__
lazy :: a -> a
lazy a = a
#endif
insertR :: Ord a => a -> Set a -> Set a
insertR :: forall a. Ord a => a -> Set a -> Set a
insertR a
x0 = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
x0 a
x0
where
go :: Ord a => a -> a -> Set a -> Set a
go :: forall a. Ord a => a -> a -> Set a -> Set a
go a
orig !a
_ Set a
Tip = a -> Set a
forall a. a -> Set a
singleton (a -> a
forall a. a -> a
lazy a
orig)
go a
orig !a
x t :: Set a
t@(Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT | Set a
l' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y Set a
l' Set a
r
where !l' :: Set a
l' = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
orig a
x Set a
l
Ordering
GT | Set a
r' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l Set a
r'
where !r' :: Set a
r' = a -> a -> Set a -> Set a
forall a. Ord a => a -> a -> Set a -> Set a
go a
orig a
x Set a
r
Ordering
EQ -> Set a
t
{-# INLINABLE insertR #-}
delete :: Ord a => a -> Set a -> Set a
delete :: forall a. Ord a => a -> Set a -> Set a
delete = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
go
where
go :: Ord a => a -> Set a -> Set a
go :: forall a. Ord a => a -> Set a -> Set a
go !a
_ Set a
Tip = Set a
forall a. Set a
Tip
go a
x t :: Set a
t@(Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT | Set a
l' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l' Set a
r
where !l' :: Set a
l' = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
go a
x Set a
l
Ordering
GT | Set a
r' Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r -> Set a
t
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y Set a
l Set a
r'
where !r' :: Set a
r' = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
go a
x Set a
r
Ordering
EQ -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
{-# INLINABLE delete #-}
pop :: Ord a => a -> Set a -> Maybe (Set a)
pop :: forall a. Ord a => a -> Set a -> Maybe (Set a)
pop a
x0 Set a
t0 = case a -> Set a -> StrictPair Bool (Set a)
forall {a}. Ord a => a -> Set a -> StrictPair Bool (Set a)
go a
x0 Set a
t0 of
Bool
True :*: Set a
t -> Set a -> Maybe (Set a)
forall a. a -> Maybe a
Just Set a
t
StrictPair Bool (Set a)
_ -> Maybe (Set a)
forall a. Maybe a
Nothing
where
go :: a -> Set a -> StrictPair Bool (Set a)
go !a
x (Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT -> case a -> Set a -> StrictPair Bool (Set a)
go a
x Set a
l of
Bool
True :*: Set a
l' -> Bool
True Bool -> Set a -> StrictPair Bool (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l' Set a
r
StrictPair Bool (Set a)
q -> StrictPair Bool (Set a)
q
Ordering
EQ -> Bool
True Bool -> Set a -> StrictPair Bool (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
Ordering
GT -> case a -> Set a -> StrictPair Bool (Set a)
go a
x Set a
r of
Bool
True :*: Set a
r' -> Bool
True Bool -> Set a -> StrictPair Bool (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y Set a
l Set a
r'
StrictPair Bool (Set a)
q -> StrictPair Bool (Set a)
q
go !a
_ Set a
Tip = Bool
False Bool -> Set a -> StrictPair Bool (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
forall a. Set a
Tip
{-# INLINABLE pop #-}
alterF :: (Ord a, Functor f) => (Bool -> f Bool) -> a -> Set a -> f (Set a)
alterF :: forall a (f :: * -> *).
(Ord a, Functor f) =>
(Bool -> f Bool) -> a -> Set a -> f (Set a)
alterF Bool -> f Bool
f a
k Set a
s = (Bool -> Set a) -> f Bool -> f (Set a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Set a
choose (Bool -> f Bool
f Bool
member_)
where
MemberIndex Bool
member_ Int
i = a -> Set a -> MemberIndex
forall a. Ord a => a -> Set a -> MemberIndex
memberIndex a
k Set a
s
inserted :: Set a
inserted = if Bool
member_ then Set a
s else Int -> a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a
insertAt Int
i a
k Set a
s
deleted :: Set a
deleted = if Bool
member_ then Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
deleteAt Int
i Set a
s else Set a
s
choose :: Bool -> Set a
choose Bool
True = Set a
inserted
choose Bool
False = Set a
deleted
#ifdef __GLASGOW_HASKELL__
{-# INLINE [2] alterF #-}
{-# RULES
"alterF/Const" forall k (f :: Bool -> Const a Bool) . alterF f k = \s -> Const . getConst . f $ member k s
#-}
#endif
data MemberIndex = MemberIndex !Bool {-# UNPACK #-} !Int
memberIndex :: Ord a => a -> Set a -> MemberIndex
memberIndex :: forall a. Ord a => a -> Set a -> MemberIndex
memberIndex = Int -> a -> Set a -> MemberIndex
forall {a}. Ord a => Int -> a -> Set a -> MemberIndex
go Int
0
where
go :: Int -> a -> Set a -> MemberIndex
go !Int
i !a
_ Set a
Tip = Bool -> Int -> MemberIndex
MemberIndex Bool
False Int
i
go !Int
i !a
x (Bin Int
_ a
y Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT -> Int -> a -> Set a -> MemberIndex
go Int
i a
x Set a
l
Ordering
EQ -> Bool -> Int -> MemberIndex
MemberIndex Bool
True (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l)
Ordering
GT -> Int -> a -> Set a -> MemberIndex
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
x Set a
r
{-# INLINABLE memberIndex #-}
insertAt :: Int -> a -> Set a -> Set a
insertAt :: forall a. Int -> a -> Set a -> Set a
insertAt !Int
_ !a
x Set a
Tip = a -> Set a
forall a. a -> Set a
singleton a
x
insertAt !Int
i !a
x (Bin Int
_ a
y Set a
l Set a
r)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
sizeL = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y (Int -> a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a
insertAt Int
i a
x Set a
l) Set a
r
| Bool
otherwise = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l (Int -> a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a
insertAt (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) a
x Set a
r)
where
!sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
isProperSubsetOf :: Ord a => Set a -> Set a -> Bool
isProperSubsetOf :: forall a. Ord a => Set a -> Set a -> Bool
isProperSubsetOf Set a
s1 Set a
s2
= Set a -> Int
forall a. Set a -> Int
size Set a
s1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Set a -> Int
forall a. Set a -> Int
size Set a
s2 Bool -> Bool -> Bool
&& Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
isSubsetOfX Set a
s1 Set a
s2
{-# INLINABLE isProperSubsetOf #-}
isSubsetOf :: Ord a => Set a -> Set a -> Bool
isSubsetOf :: forall a. Ord a => Set a -> Set a -> Bool
isSubsetOf Set a
t1 Set a
t2
= Set a -> Int
forall a. Set a -> Int
size Set a
t1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Set a -> Int
forall a. Set a -> Int
size Set a
t2 Bool -> Bool -> Bool
&& Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
isSubsetOfX Set a
t1 Set a
t2
{-# INLINABLE isSubsetOf #-}
isSubsetOfX :: Ord a => Set a -> Set a -> Bool
isSubsetOfX :: forall a. Ord a => Set a -> Set a -> Bool
isSubsetOfX Set a
Tip Set a
_ = Bool
True
isSubsetOfX Set a
_ Set a
Tip = Bool
False
isSubsetOfX (Bin Int
1 a
x Set a
_ Set a
_) Set a
t = a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
member a
x Set a
t
isSubsetOfX (Bin Int
_ a
x Set a
l Set a
r) Set a
t
= Bool
found Bool -> Bool -> Bool
&&
Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Set a -> Int
forall a. Set a -> Int
size Set a
lt Bool -> Bool -> Bool
&& Set a -> Int
forall a. Set a -> Int
size Set a
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Set a -> Int
forall a. Set a -> Int
size Set a
gt Bool -> Bool -> Bool
&&
Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
isSubsetOfX Set a
l Set a
lt Bool -> Bool -> Bool
&& Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
isSubsetOfX Set a
r Set a
gt
where
(Set a
lt,Bool
found,Set a
gt) = a -> Set a -> (Set a, Bool, Set a)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x Set a
t
{-# INLINABLE isSubsetOfX #-}
disjoint :: Ord a => Set a -> Set a -> Bool
disjoint :: forall a. Ord a => Set a -> Set a -> Bool
disjoint Set a
Tip Set a
_ = Bool
True
disjoint Set a
_ Set a
Tip = Bool
True
disjoint (Bin Int
1 a
x Set a
_ Set a
_) Set a
t = a
x a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`notMember` Set a
t
disjoint (Bin Int
_ a
x Set a
l Set a
r) Set a
t
= Bool -> Bool
not Bool
found Bool -> Bool -> Bool
&& Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
disjoint Set a
l Set a
lt Bool -> Bool -> Bool
&& Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
disjoint Set a
r Set a
gt
where
(Set a
lt,Bool
found,Set a
gt) = a -> Set a -> (Set a, Bool, Set a)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x Set a
t
lookupMinSure :: a -> Set a -> a
lookupMinSure :: forall a. a -> Set a -> a
lookupMinSure a
x Set a
Tip = a
x
lookupMinSure a
_ (Bin Int
_ a
x Set a
l Set a
_) = a -> Set a -> a
forall a. a -> Set a -> a
lookupMinSure a
x Set a
l
lookupMin :: Set a -> Maybe a
lookupMin :: forall a. Set a -> Maybe a
lookupMin Set a
Tip = Maybe a
forall a. Maybe a
Nothing
lookupMin (Bin Int
_ a
x Set a
l Set a
_) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> Set a -> a
forall a. a -> Set a -> a
lookupMinSure a
x Set a
l
{-# INLINE lookupMin #-}
findMin :: Set a -> a
findMin :: forall a. Set a -> a
findMin Set a
t
| Just a
r <- Set a -> Maybe a
forall a. Set a -> Maybe a
lookupMin Set a
t = a
r
| Bool
otherwise = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.findMin: empty set has no minimal element"
lookupMaxSure :: a -> Set a -> a
lookupMaxSure :: forall a. a -> Set a -> a
lookupMaxSure a
x Set a
Tip = a
x
lookupMaxSure a
_ (Bin Int
_ a
x Set a
_ Set a
r) = a -> Set a -> a
forall a. a -> Set a -> a
lookupMaxSure a
x Set a
r
lookupMax :: Set a -> Maybe a
lookupMax :: forall a. Set a -> Maybe a
lookupMax Set a
Tip = Maybe a
forall a. Maybe a
Nothing
lookupMax (Bin Int
_ a
x Set a
_ Set a
r) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> Set a -> a
forall a. a -> Set a -> a
lookupMaxSure a
x Set a
r
{-# INLINE lookupMax #-}
findMax :: Set a -> a
findMax :: forall a. Set a -> a
findMax Set a
t
| Just a
r <- Set a -> Maybe a
forall a. Set a -> Maybe a
lookupMax Set a
t = a
r
| Bool
otherwise = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.findMax: empty set has no maximal element"
deleteMin :: Set a -> Set a
deleteMin :: forall a. Set a -> Set a
deleteMin (Bin Int
_ a
_ Set a
Tip Set a
r) = Set a
r
deleteMin (Bin Int
_ a
x Set a
l Set a
r) = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
x (Set a -> Set a
forall a. Set a -> Set a
deleteMin Set a
l) Set a
r
deleteMin Set a
Tip = Set a
forall a. Set a
Tip
deleteMax :: Set a -> Set a
deleteMax :: forall a. Set a -> Set a
deleteMax (Bin Int
_ a
_ Set a
l Set a
Tip) = Set a
l
deleteMax (Bin Int
_ a
x Set a
l Set a
r) = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
x Set a
l (Set a -> Set a
forall a. Set a -> Set a
deleteMax Set a
r)
deleteMax Set a
Tip = Set a
forall a. Set a
Tip
unions :: (Foldable f, Ord a) => f (Set a) -> Set a
unions :: forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
unions = (Set a -> Set a -> Set a) -> Set a -> f (Set a) -> Set a
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
union Set a
forall a. Set a
empty
{-# INLINE unions #-}
union :: Ord a => Set a -> Set a -> Set a
union :: forall a. Ord a => Set a -> Set a -> Set a
union Set a
t1 Set a
Tip = Set a
t1
union Set a
t1 (Bin Int
1 a
x Set a
_ Set a
_) = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
insertR a
x Set a
t1
union (Bin Int
1 a
x Set a
_ Set a
_) Set a
t2 = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
insert a
x Set a
t2
union Set a
Tip Set a
t2 = Set a
t2
union t1 :: Set a
t1@(Bin Int
_ a
x Set a
l1 Set a
r1) Set a
t2 = case a -> Set a -> StrictPair (Set a) (Set a)
forall a. Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS a
x Set a
t2 of
(Set a
l2 :*: Set a
r2)
| Set a
l1l2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l1 Bool -> Bool -> Bool
&& Set a
r1r2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r1 -> Set a
t1
| Bool
otherwise -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l1l2 Set a
r1r2
where !l1l2 :: Set a
l1l2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
union Set a
l1 Set a
l2
!r1r2 :: Set a
r1r2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
union Set a
r1 Set a
r2
{-# INLINABLE union #-}
difference :: Ord a => Set a -> Set a -> Set a
difference :: forall a. Ord a => Set a -> Set a -> Set a
difference Set a
Tip Set a
_ = Set a
forall a. Set a
Tip
difference Set a
t1 Set a
Tip = Set a
t1
difference Set a
t1 (Bin Int
_ a
x Set a
l2 Set a
r2) = case a -> Set a -> (Set a, Set a)
forall a. Ord a => a -> Set a -> (Set a, Set a)
split a
x Set a
t1 of
(Set a
l1, Set a
r1)
| Set a -> Int
forall a. Set a -> Int
size Set a
l1l2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
r1r2 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Set a -> Int
forall a. Set a -> Int
size Set a
t1 -> Set a
t1
| Bool
otherwise -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l1l2 Set a
r1r2
where !l1l2 :: Set a
l1l2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
difference Set a
l1 Set a
l2
!r1r2 :: Set a
r1r2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
difference Set a
r1 Set a
r2
{-# INLINABLE difference #-}
intersection :: Ord a => Set a -> Set a -> Set a
intersection :: forall a. Ord a => Set a -> Set a -> Set a
intersection Set a
Tip Set a
_ = Set a
forall a. Set a
Tip
intersection Set a
_ Set a
Tip = Set a
forall a. Set a
Tip
intersection t1 :: Set a
t1@(Bin Int
_ a
x Set a
l1 Set a
r1) Set a
t2
| Bool
b = if Set a
l1l2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l1 Bool -> Bool -> Bool
&& Set a
r1r2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r1
then Set a
t1
else a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l1l2 Set a
r1r2
| Bool
otherwise = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l1l2 Set a
r1r2
where
!(Set a
l2, Bool
b, Set a
r2) = a -> Set a -> (Set a, Bool, Set a)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x Set a
t2
!l1l2 :: Set a
l1l2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
intersection Set a
l1 Set a
l2
!r1r2 :: Set a
r1r2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
intersection Set a
r1 Set a
r2
{-# INLINABLE intersection #-}
intersections :: Ord a => NonEmpty (Set a) -> Set a
intersections :: forall a. Ord a => NonEmpty (Set a) -> Set a
intersections (Set a
s0 :| [Set a]
ss)
| Set a -> Bool
forall a. Set a -> Bool
null Set a
s0 = Set a
forall a. Set a
empty
| Bool
otherwise = (Set a -> (Set a -> Set a) -> Set a -> Set a)
-> (Set a -> Set a) -> [Set a] -> Set a -> Set a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr Set a -> (Set a -> Set a) -> Set a -> Set a
forall {a} {a}.
Ord a =>
Set a -> (Set a -> Set a) -> Set a -> Set a
go Set a -> Set a
forall a. a -> a
id [Set a]
ss Set a
s0
where
go :: Set a -> (Set a -> Set a) -> Set a -> Set a
go Set a
s Set a -> Set a
r Set a
acc
| Set a -> Bool
forall a. Set a -> Bool
null Set a
acc' = Set a
forall a. Set a
empty
| Bool
otherwise = Set a -> Set a
r Set a
acc'
where
acc' :: Set a
acc' = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
intersection Set a
acc Set a
s
{-# INLINABLE intersections #-}
newtype Intersection a = Intersection { forall a. Intersection a -> Set a
getIntersection :: Set a }
deriving (Int -> Intersection a -> ShowS
[Intersection a] -> ShowS
Intersection a -> [Char]
(Int -> Intersection a -> ShowS)
-> (Intersection a -> [Char])
-> ([Intersection a] -> ShowS)
-> Show (Intersection a)
forall a. Show a => Int -> Intersection a -> ShowS
forall a. Show a => [Intersection a] -> ShowS
forall a. Show a => Intersection a -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Intersection a -> ShowS
showsPrec :: Int -> Intersection a -> ShowS
$cshow :: forall a. Show a => Intersection a -> [Char]
show :: Intersection a -> [Char]
$cshowList :: forall a. Show a => [Intersection a] -> ShowS
showList :: [Intersection a] -> ShowS
Show, Intersection a -> Intersection a -> Bool
(Intersection a -> Intersection a -> Bool)
-> (Intersection a -> Intersection a -> Bool)
-> Eq (Intersection a)
forall a. Eq a => Intersection a -> Intersection a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Intersection a -> Intersection a -> Bool
== :: Intersection a -> Intersection a -> Bool
$c/= :: forall a. Eq a => Intersection a -> Intersection a -> Bool
/= :: Intersection a -> Intersection a -> Bool
Eq, Eq (Intersection a)
Eq (Intersection a) =>
(Intersection a -> Intersection a -> Ordering)
-> (Intersection a -> Intersection a -> Bool)
-> (Intersection a -> Intersection a -> Bool)
-> (Intersection a -> Intersection a -> Bool)
-> (Intersection a -> Intersection a -> Bool)
-> (Intersection a -> Intersection a -> Intersection a)
-> (Intersection a -> Intersection a -> Intersection a)
-> Ord (Intersection a)
Intersection a -> Intersection a -> Bool
Intersection a -> Intersection a -> Ordering
Intersection a -> Intersection a -> Intersection a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Intersection a)
forall a. Ord a => Intersection a -> Intersection a -> Bool
forall a. Ord a => Intersection a -> Intersection a -> Ordering
forall a.
Ord a =>
Intersection a -> Intersection a -> Intersection a
$ccompare :: forall a. Ord a => Intersection a -> Intersection a -> Ordering
compare :: Intersection a -> Intersection a -> Ordering
$c< :: forall a. Ord a => Intersection a -> Intersection a -> Bool
< :: Intersection a -> Intersection a -> Bool
$c<= :: forall a. Ord a => Intersection a -> Intersection a -> Bool
<= :: Intersection a -> Intersection a -> Bool
$c> :: forall a. Ord a => Intersection a -> Intersection a -> Bool
> :: Intersection a -> Intersection a -> Bool
$c>= :: forall a. Ord a => Intersection a -> Intersection a -> Bool
>= :: Intersection a -> Intersection a -> Bool
$cmax :: forall a.
Ord a =>
Intersection a -> Intersection a -> Intersection a
max :: Intersection a -> Intersection a -> Intersection a
$cmin :: forall a.
Ord a =>
Intersection a -> Intersection a -> Intersection a
min :: Intersection a -> Intersection a -> Intersection a
Ord)
instance (Ord a) => Semigroup (Intersection a) where
(Intersection Set a
a) <> :: Intersection a -> Intersection a -> Intersection a
<> (Intersection Set a
b) = Set a -> Intersection a
forall a. Set a -> Intersection a
Intersection (Set a -> Intersection a) -> Set a -> Intersection a
forall a b. (a -> b) -> a -> b
$ Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
intersection Set a
a Set a
b
{-# INLINABLE (<>) #-}
stimes :: forall b. Integral b => b -> Intersection a -> Intersection a
stimes = b -> Intersection a -> Intersection a
forall b a. Integral b => b -> a -> a
stimesIdempotent
{-# INLINABLE stimes #-}
sconcat :: NonEmpty (Intersection a) -> Intersection a
sconcat =
#ifdef __GLASGOW_HASKELL__
(NonEmpty (Set a) -> Set a)
-> NonEmpty (Intersection a) -> Intersection a
forall a b. Coercible a b => a -> b
coerce NonEmpty (Set a) -> Set a
forall a. Ord a => NonEmpty (Set a) -> Set a
intersections
#else
Intersection . intersections . fmap getIntersection
#endif
{-# INLINABLE sconcat #-}
symmetricDifference :: Ord a => Set a -> Set a -> Set a
symmetricDifference :: forall a. Ord a => Set a -> Set a -> Set a
symmetricDifference Set a
Tip Set a
t2 = Set a
t2
symmetricDifference Set a
t1 Set a
Tip = Set a
t1
symmetricDifference (Bin Int
_ a
x Set a
l1 Set a
r1) Set a
t2
| Bool
found = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l1l2 Set a
r1r2
| Bool
otherwise = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l1l2 Set a
r1r2
where
!(Set a
l2, Bool
found, Set a
r2) = a -> Set a -> (Set a, Bool, Set a)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x Set a
t2
!l1l2 :: Set a
l1l2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
symmetricDifference Set a
l1 Set a
l2
!r1r2 :: Set a
r1r2 = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
symmetricDifference Set a
r1 Set a
r2
{-# INLINABLE symmetricDifference #-}
filter :: (a -> Bool) -> Set a -> Set a
filter :: forall a. (a -> Bool) -> Set a -> Set a
filter a -> Bool
_ Set a
Tip = Set a
forall a. Set a
Tip
filter a -> Bool
p t :: Set a
t@(Bin Int
_ a
x Set a
l Set a
r)
| a -> Bool
p a
x = if Set a
l Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l' Bool -> Bool -> Bool
&& Set a
r Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r'
then Set a
t
else a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l' Set a
r'
| Bool
otherwise = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l' Set a
r'
where
!l' :: Set a
l' = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
filter a -> Bool
p Set a
l
!r' :: Set a
r' = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
filter a -> Bool
p Set a
r
filterA :: Applicative f => (a -> f Bool) -> Set a -> f (Set a)
filterA :: forall (f :: * -> *) a.
Applicative f =>
(a -> f Bool) -> Set a -> f (Set a)
filterA a -> f Bool
p = Set a -> f (Set a)
go
where
go :: Set a -> f (Set a)
go t :: Set a
t@(Bin Int
1 a
x Set a
_ Set a
_) = Bool -> Set a
finish (Bool -> Set a) -> f Bool -> f (Set a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f Bool
p a
x
where
finish :: Bool -> Set a
finish Bool
False = Set a
forall a. Set a
Tip
finish Bool
True = Set a
t
go t :: Set a
t@(Bin Int
_ a
x Set a
l Set a
r) = (Set a -> Bool -> Set a -> Set a)
-> f (Set a) -> f Bool -> f (Set a) -> f (Set a)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 Set a -> Bool -> Set a -> Set a
doLink (Set a -> f (Set a)
go Set a
l) (a -> f Bool
p a
x) (Set a -> f (Set a)
go Set a
r)
where
doLink :: Set a -> Bool -> Set a -> Set a
doLink !Set a
l' Bool
keepx !Set a
r'
| Bool
keepx = if Set a
l Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l' Bool -> Bool -> Bool
&& Set a
r Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r'
then Set a
t
else a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l' Set a
r'
| Bool
otherwise = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l' Set a
r'
go Set a
Tip = Set a -> f (Set a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set a
forall a. Set a
Tip
{-# INLINABLE filterA #-}
partition :: (a -> Bool) -> Set a -> (Set a,Set a)
partition :: forall a. (a -> Bool) -> Set a -> (Set a, Set a)
partition a -> Bool
p0 Set a
t0 = StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Set a) (Set a) -> (Set a, Set a))
-> StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
forall {a}. (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p0 Set a
t0
where
go :: (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
_ Set a
Tip = (Set a
forall a. Set a
Tip Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
forall a. Set a
Tip)
go a -> Bool
p t :: Set a
t@(Bin Int
_ a
x Set a
l Set a
r) = case ((a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p Set a
l, (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p Set a
r) of
((Set a
l1 :*: Set a
l2), (Set a
r1 :*: Set a
r2))
| a -> Bool
p a
x -> (if Set a
l1 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l Bool -> Bool -> Bool
&& Set a
r1 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r
then Set a
t
else a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l1 Set a
r1) Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l2 Set a
r2
| Bool
otherwise -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l1 Set a
r1 Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*:
(if Set a
l2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
l Bool -> Bool -> Bool
&& Set a
r2 Set a -> Set a -> Bool
forall a. a -> a -> Bool
`ptrEq` Set a
r
then Set a
t
else a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x Set a
l2 Set a
r2)
mapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set b
mapMaybe :: forall b a. Ord b => (a -> Maybe b) -> Set a -> Set b
mapMaybe a -> Maybe b
f Set a
t = SetBuilder b -> Set b
forall a. SetBuilder a -> Set a
finishB ((SetBuilder b -> a -> SetBuilder b)
-> SetBuilder b -> Set a -> SetBuilder b
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' SetBuilder b -> a -> SetBuilder b
go SetBuilder b
forall a. SetBuilder a
emptyB Set a
t)
where go :: SetBuilder b -> a -> SetBuilder b
go SetBuilder b
b a
x = case a -> Maybe b
f a
x of
Maybe b
Nothing -> SetBuilder b
b
Just b
x' -> b -> SetBuilder b -> SetBuilder b
forall a. Ord a => a -> SetBuilder a -> SetBuilder a
insertB b
x' SetBuilder b
b
{-# INLINABLE mapMaybe #-}
map :: Ord b => (a->b) -> Set a -> Set b
map :: forall b a. Ord b => (a -> b) -> Set a -> Set b
map a -> b
f Set a
t = SetBuilder b -> Set b
forall a. SetBuilder a -> Set a
finishB ((SetBuilder b -> a -> SetBuilder b)
-> SetBuilder b -> Set a -> SetBuilder b
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' (\SetBuilder b
b a
x -> b -> SetBuilder b -> SetBuilder b
forall a. Ord a => a -> SetBuilder a -> SetBuilder a
insertB (a -> b
f a
x) SetBuilder b
b) SetBuilder b
forall a. SetBuilder a
emptyB Set a
t)
{-# INLINABLE map #-}
mapMonotonic :: (a->b) -> Set a -> Set b
mapMonotonic :: forall a b. (a -> b) -> Set a -> Set b
mapMonotonic a -> b
_ Set a
Tip = Set b
forall a. Set a
Tip
mapMonotonic a -> b
f (Bin Int
sz a
x Set a
l Set a
r) = Int -> b -> Set b -> Set b -> Set b
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
sz (a -> b
f a
x) ((a -> b) -> Set a -> Set b
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic a -> b
f Set a
l) ((a -> b) -> Set a -> Set b
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic a -> b
f Set a
r)
{-# DEPRECATED fold "Use Data.Set.foldr instead" #-}
fold :: (a -> b -> b) -> b -> Set a -> b
fold :: forall a b. (a -> b -> b) -> b -> Set a -> b
fold = (a -> b -> b) -> b -> Set a -> b
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr
{-# INLINE fold #-}
foldr :: (a -> b -> b) -> b -> Set a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Set a -> b
foldr a -> b -> b
f b
z = b -> Set a -> b
go b
z
where
go :: b -> Set a -> b
go b
z' Set a
Tip = b
z'
go b
z' (Bin Int
_ a
x Set a
l Set a
r) = b -> Set a -> b
go (a -> b -> b
f a
x (b -> Set a -> b
go b
z' Set a
r)) Set a
l
{-# INLINE foldr #-}
foldr' :: (a -> b -> b) -> b -> Set a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Set a -> b
foldr' a -> b -> b
f b
z = b -> Set a -> b
go b
z
where
go :: b -> Set a -> b
go !b
z' Set a
Tip = b
z'
go b
z' (Bin Int
_ a
x Set a
l Set a
r) = b -> Set a -> b
go (a -> b -> b
f a
x (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$! b -> Set a -> b
go b
z' Set a
r) Set a
l
{-# INLINE foldr' #-}
foldl :: (a -> b -> a) -> a -> Set b -> a
foldl :: forall b a. (b -> a -> b) -> b -> Set a -> b
foldl a -> b -> a
f a
z = a -> Set b -> a
go a
z
where
go :: a -> Set b -> a
go a
z' Set b
Tip = a
z'
go a
z' (Bin Int
_ b
x Set b
l Set b
r) = a -> Set b -> a
go (a -> b -> a
f (a -> Set b -> a
go a
z' Set b
l) b
x) Set b
r
{-# INLINE foldl #-}
foldl' :: (a -> b -> a) -> a -> Set b -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Set a -> b
foldl' a -> b -> a
f a
z = a -> Set b -> a
go a
z
where
go :: a -> Set b -> a
go !a
z' Set b
Tip = a
z'
go a
z' (Bin Int
_ b
x Set b
l Set b
r) =
let !z'' :: a
z'' = a -> Set b -> a
go a
z' Set b
l
in a -> Set b -> a
go (a -> b -> a
f a
z'' b
x) Set b
r
{-# INLINE foldl' #-}
elems :: Set a -> [a]
elems :: forall a. Set a -> [a]
elems = Set a -> [a]
forall a. Set a -> [a]
toAscList
#ifdef __GLASGOW_HASKELL__
instance (Ord a) => GHCExts.IsList (Set a) where
type Item (Set a) = a
fromList :: [Item (Set a)] -> Set a
fromList = [a] -> Set a
[Item (Set a)] -> Set a
forall a. Ord a => [a] -> Set a
fromList
toList :: Set a -> [Item (Set a)]
toList = Set a -> [a]
Set a -> [Item (Set a)]
forall a. Set a -> [a]
toList
#endif
toList :: Set a -> [a]
toList :: forall a. Set a -> [a]
toList = Set a -> [a]
forall a. Set a -> [a]
toAscList
toAscList :: Set a -> [a]
toAscList :: forall a. Set a -> [a]
toAscList = (a -> [a] -> [a]) -> [a] -> Set a -> [a]
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr (:) []
toDescList :: Set a -> [a]
toDescList :: forall a. Set a -> [a]
toDescList = ([a] -> a -> [a]) -> [a] -> Set a -> [a]
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl ((a -> [a] -> [a]) -> [a] -> a -> [a]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) []
#if __GLASGOW_HASKELL__
foldrFB :: (a -> b -> b) -> b -> Set a -> b
foldrFB :: forall a b. (a -> b -> b) -> b -> Set a -> b
foldrFB = (a -> b -> b) -> b -> Set a -> b
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr
{-# INLINE[0] foldrFB #-}
foldlFB :: (a -> b -> a) -> a -> Set b -> a
foldlFB :: forall b a. (b -> a -> b) -> b -> Set a -> b
foldlFB = (a -> b -> a) -> a -> Set b -> a
forall b a. (b -> a -> b) -> b -> Set a -> b
foldl
{-# INLINE[0] foldlFB #-}
{-# INLINE elems #-}
{-# INLINE toList #-}
{-# NOINLINE[0] toAscList #-}
{-# NOINLINE[0] toDescList #-}
{-# RULES "Set.toAscList" [~1] forall s . toAscList s = build (\c n -> foldrFB c n s) #-}
{-# RULES "Set.toAscListBack" [1] foldrFB (:) [] = toAscList #-}
{-# RULES "Set.toDescList" [~1] forall s . toDescList s = build (\c n -> foldlFB (\xs x -> c x xs) n s) #-}
{-# RULES "Set.toDescListBack" [1] foldlFB (\xs x -> x : xs) [] = toDescList #-}
#endif
fromList :: Ord a => [a] -> Set a
fromList :: forall a. Ord a => [a] -> Set a
fromList [a]
xs = SetBuilder a -> Set a
forall a. SetBuilder a -> Set a
finishB ((SetBuilder a -> a -> SetBuilder a)
-> SetBuilder a -> [a] -> SetBuilder a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' ((a -> SetBuilder a -> SetBuilder a)
-> SetBuilder a -> a -> SetBuilder a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> SetBuilder a -> SetBuilder a
forall a. Ord a => a -> SetBuilder a -> SetBuilder a
insertB) SetBuilder a
forall a. SetBuilder a
emptyB [a]
xs)
{-# INLINE fromList #-}
fromAscList :: Eq a => [a] -> Set a
fromAscList :: forall a. Eq a => [a] -> Set a
fromAscList [a]
xs = Stack a -> Set a
forall a. Stack a -> Set a
ascLinkAll ((Stack a -> a -> Stack a) -> Stack a -> [a] -> Stack a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Stack a -> a -> Stack a
forall {a}. Eq a => Stack a -> a -> Stack a
next Stack a
forall a. Stack a
Nada [a]
xs)
where
next :: Stack a -> a -> Stack a
next Stack a
stk !a
y = case Stack a
stk of
Push a
x Set a
l Stack a
stk'
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
l Stack a
stk'
| Set a
Tip <- Set a
l -> Stack a -> Int -> Set a -> a -> Stack a
forall a. Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop Stack a
stk' Int
1 (a -> Set a
forall a. a -> Set a
singleton a
x) a
y
| Bool
otherwise -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk
Stack a
Nada -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk
{-# INLINE fromAscList #-}
fromDescList :: Eq a => [a] -> Set a
fromDescList :: forall a. Eq a => [a] -> Set a
fromDescList [a]
xs = Stack a -> Set a
forall a. Stack a -> Set a
descLinkAll ((Stack a -> a -> Stack a) -> Stack a -> [a] -> Stack a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Stack a -> a -> Stack a
forall {a}. Eq a => Stack a -> a -> Stack a
next Stack a
forall a. Stack a
Nada [a]
xs)
where
next :: Stack a -> a -> Stack a
next Stack a
stk !a
y = case Stack a
stk of
Push a
x Set a
r Stack a
stk'
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
r Stack a
stk'
| Set a
Tip <- Set a
r -> a -> Int -> Set a -> Stack a -> Stack a
forall a. a -> Int -> Set a -> Stack a -> Stack a
descLinkTop a
y Int
1 (a -> Set a
forall a. a -> Set a
singleton a
x) Stack a
stk'
| Bool
otherwise -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk
Stack a
Nada -> a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk
{-# INLINE fromDescList #-}
fromDistinctAscList :: [a] -> Set a
fromDistinctAscList :: forall a. [a] -> Set a
fromDistinctAscList [a]
xs = Stack a -> Set a
forall a. Stack a -> Set a
ascLinkAll ((Stack a -> a -> Stack a) -> Stack a -> [a] -> Stack a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Stack a -> a -> Stack a
forall a. Stack a -> a -> Stack a
next Stack a
forall a. Stack a
Nada [a]
xs)
where
next :: Stack a -> a -> Stack a
next :: forall a. Stack a -> a -> Stack a
next (Push a
x Set a
Tip Stack a
stk) !a
y = Stack a -> Int -> Set a -> a -> Stack a
forall a. Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop Stack a
stk Int
1 (a -> Set a
forall a. a -> Set a
singleton a
x) a
y
next Stack a
stk !a
x = a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
x Set a
forall a. Set a
Tip Stack a
stk
{-# INLINE fromDistinctAscList #-}
ascLinkTop :: Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop :: forall a. Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop (Push a
x l :: Set a
l@(Bin Int
lsz a
_ Set a
_ Set a
_) Stack a
stk) !Int
rsz Set a
r a
y
| Int
lsz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
rsz = Stack a -> Int -> Set a -> a -> Stack a
forall a. Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop Stack a
stk Int
sz (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
sz a
x Set a
l Set a
r) a
y
where
sz :: Int
sz = Int
lsz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rsz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
ascLinkTop Stack a
stk !Int
_ Set a
r a
y = a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
r Stack a
stk
ascLinkAll :: Stack a -> Set a
ascLinkAll :: forall a. Stack a -> Set a
ascLinkAll Stack a
stk = (Set a -> a -> Set a -> Set a) -> Set a -> Stack a -> Set a
forall b a. (b -> a -> Set a -> b) -> b -> Stack a -> b
foldl'Stack (\Set a
r a
x Set a
l -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l Set a
r) Set a
forall a. Set a
Tip Stack a
stk
{-# INLINABLE ascLinkAll #-}
fromDistinctDescList :: [a] -> Set a
fromDistinctDescList :: forall a. [a] -> Set a
fromDistinctDescList [a]
xs = Stack a -> Set a
forall a. Stack a -> Set a
descLinkAll ((Stack a -> a -> Stack a) -> Stack a -> [a] -> Stack a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Stack a -> a -> Stack a
forall a. Stack a -> a -> Stack a
next Stack a
forall a. Stack a
Nada [a]
xs)
where
next :: Stack a -> a -> Stack a
next :: forall a. Stack a -> a -> Stack a
next (Push a
y Set a
Tip Stack a
stk) !a
x = a -> Int -> Set a -> Stack a -> Stack a
forall a. a -> Int -> Set a -> Stack a -> Stack a
descLinkTop a
x Int
1 (a -> Set a
forall a. a -> Set a
singleton a
y) Stack a
stk
next Stack a
stk !a
y = a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk
{-# INLINE fromDistinctDescList #-}
descLinkTop :: a -> Int -> Set a -> Stack a -> Stack a
descLinkTop :: forall a. a -> Int -> Set a -> Stack a -> Stack a
descLinkTop a
x !Int
lsz Set a
l (Push a
y r :: Set a
r@(Bin Int
rsz a
_ Set a
_ Set a
_) Stack a
stk)
| Int
lsz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
rsz = a -> Int -> Set a -> Stack a -> Stack a
forall a. a -> Int -> Set a -> Stack a -> Stack a
descLinkTop a
x Int
sz (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
sz a
y Set a
l Set a
r) Stack a
stk
where
sz :: Int
sz = Int
lsz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rsz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
descLinkTop a
y !Int
_ Set a
r Stack a
stk = a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
r Stack a
stk
descLinkAll :: Stack a -> Set a
descLinkAll :: forall a. Stack a -> Set a
descLinkAll Stack a
stk = (Set a -> a -> Set a -> Set a) -> Set a -> Stack a -> Set a
forall b a. (b -> a -> Set a -> b) -> b -> Stack a -> b
foldl'Stack (\Set a
l a
x Set a
r -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
x Set a
l Set a
r) Set a
forall a. Set a
Tip Stack a
stk
{-# INLINABLE descLinkAll #-}
data Stack a = Push !a !(Set a) !(Stack a) | Nada
foldl'Stack :: (b -> a -> Set a -> b) -> b -> Stack a -> b
foldl'Stack :: forall b a. (b -> a -> Set a -> b) -> b -> Stack a -> b
foldl'Stack b -> a -> Set a -> b
f = b -> Stack a -> b
go
where
go :: b -> Stack a -> b
go !b
z Stack a
Nada = b
z
go b
z (Push a
x Set a
t Stack a
stk) = b -> Stack a -> b
go (b -> a -> Set a -> b
f b
z a
x Set a
t) Stack a
stk
{-# INLINE foldl'Stack #-}
iterDown :: Set a -> Stack a -> Stack a
iterDown :: forall a. Set a -> Stack a -> Stack a
iterDown (Bin Int
_ a
x Set a
l Set a
r) Stack a
stk = Set a -> Stack a -> Stack a
forall a. Set a -> Stack a -> Stack a
iterDown Set a
l (a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
x Set a
r Stack a
stk)
iterDown Set a
Tip Stack a
stk = Stack a
stk
iterator :: Set a -> Stack a
iterator :: forall a. Set a -> Stack a
iterator Set a
s = Set a -> Stack a -> Stack a
forall a. Set a -> Stack a -> Stack a
iterDown Set a
s Stack a
forall a. Stack a
Nada
iterNext :: Stack a -> Maybe (StrictPair a (Stack a))
iterNext :: forall a. Stack a -> Maybe (StrictPair a (Stack a))
iterNext (Push a
x Set a
r Stack a
stk) = StrictPair a (Stack a) -> Maybe (StrictPair a (Stack a))
forall a. a -> Maybe a
Just (StrictPair a (Stack a) -> Maybe (StrictPair a (Stack a)))
-> StrictPair a (Stack a) -> Maybe (StrictPair a (Stack a))
forall a b. (a -> b) -> a -> b
$! a
x a -> Stack a -> StrictPair a (Stack a)
forall a b. a -> b -> StrictPair a b
:*: Set a -> Stack a -> Stack a
forall a. Set a -> Stack a -> Stack a
iterDown Set a
r Stack a
stk
iterNext Stack a
Nada = Maybe (StrictPair a (Stack a))
forall a. Maybe a
Nothing
{-# INLINE iterNext #-}
iterNull :: Stack a -> Bool
iterNull :: forall a. Stack a -> Bool
iterNull (Push a
_ Set a
_ Stack a
_) = Bool
False
iterNull Stack a
Nada = Bool
True
instance Eq a => Eq (Set a) where
Set a
s1 == :: Set a -> Set a -> Bool
== Set a
s2 = (a -> a -> Bool) -> Set a -> Set a -> Bool
forall a b. (a -> b -> Bool) -> Set a -> Set b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Set a
s1 Set a
s2
{-# INLINABLE (==) #-}
instance Eq1 Set where
liftEq :: forall a b. (a -> b -> Bool) -> Set a -> Set b -> Bool
liftEq a -> b -> Bool
eq Set a
s1 Set b
s2 = Set a -> Int
forall a. Set a -> Int
size Set a
s1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Set b -> Int
forall a. Set a -> Int
size Set b
s2 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Set a -> Set b -> Bool
forall a b. (a -> b -> Bool) -> Set a -> Set b -> Bool
sameSizeLiftEq a -> b -> Bool
eq Set a
s1 Set b
s2
{-# INLINE liftEq #-}
sameSizeLiftEq :: (a -> b -> Bool) -> Set a -> Set b -> Bool
sameSizeLiftEq :: forall a b. (a -> b -> Bool) -> Set a -> Set b -> Bool
sameSizeLiftEq a -> b -> Bool
eq Set a
s1 Set b
s2 =
case EqM (Stack b) -> Stack b -> StrictPair Bool (Stack b)
forall a. EqM a -> a -> StrictPair Bool a
runEqM ((a -> EqM (Stack b)) -> Set a -> EqM (Stack b)
forall m a. Monoid m => (a -> m) -> Set a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> EqM (Stack b)
f Set a
s1) (Set b -> Stack b
forall a. Set a -> Stack a
iterator Set b
s2) of Bool
e :*: Stack b
_ -> Bool
e
where
f :: a -> EqM (Stack b)
f a
x = (Stack b -> StrictPair Bool (Stack b)) -> EqM (Stack b)
forall a. (a -> StrictPair Bool a) -> EqM a
EqM ((Stack b -> StrictPair Bool (Stack b)) -> EqM (Stack b))
-> (Stack b -> StrictPair Bool (Stack b)) -> EqM (Stack b)
forall a b. (a -> b) -> a -> b
$ \Stack b
it -> case Stack b -> Maybe (StrictPair b (Stack b))
forall a. Stack a -> Maybe (StrictPair a (Stack a))
iterNext Stack b
it of
Maybe (StrictPair b (Stack b))
Nothing -> Bool
False Bool -> Stack b -> StrictPair Bool (Stack b)
forall a b. a -> b -> StrictPair a b
:*: Stack b
it
Just (b
y :*: Stack b
it') -> a -> b -> Bool
eq a
x b
y Bool -> Stack b -> StrictPair Bool (Stack b)
forall a b. a -> b -> StrictPair a b
:*: Stack b
it'
{-# INLINE sameSizeLiftEq #-}
instance Ord a => Ord (Set a) where
compare :: Set a -> Set a -> Ordering
compare Set a
s1 Set a
s2 = (a -> a -> Ordering) -> Set a -> Set a -> Ordering
forall a b. (a -> b -> Ordering) -> Set a -> Set b -> Ordering
liftCmp a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Set a
s1 Set a
s2
{-# INLINABLE compare #-}
instance Ord1 Set where
liftCompare :: forall a b. (a -> b -> Ordering) -> Set a -> Set b -> Ordering
liftCompare = (a -> b -> Ordering) -> Set a -> Set b -> Ordering
forall a b. (a -> b -> Ordering) -> Set a -> Set b -> Ordering
liftCmp
{-# INLINE liftCompare #-}
liftCmp :: (a -> b -> Ordering) -> Set a -> Set b -> Ordering
liftCmp :: forall a b. (a -> b -> Ordering) -> Set a -> Set b -> Ordering
liftCmp a -> b -> Ordering
cmp Set a
s1 Set b
s2 = case OrdM (Stack b) -> Stack b -> StrictPair Ordering (Stack b)
forall a. OrdM a -> a -> StrictPair Ordering a
runOrdM ((a -> OrdM (Stack b)) -> Set a -> OrdM (Stack b)
forall m a. Monoid m => (a -> m) -> Set a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> OrdM (Stack b)
f Set a
s1) (Set b -> Stack b
forall a. Set a -> Stack a
iterator Set b
s2) of
Ordering
o :*: Stack b
it -> Ordering
o Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> if Stack b -> Bool
forall a. Stack a -> Bool
iterNull Stack b
it then Ordering
EQ else Ordering
LT
where
f :: a -> OrdM (Stack b)
f a
x = (Stack b -> StrictPair Ordering (Stack b)) -> OrdM (Stack b)
forall a. (a -> StrictPair Ordering a) -> OrdM a
OrdM ((Stack b -> StrictPair Ordering (Stack b)) -> OrdM (Stack b))
-> (Stack b -> StrictPair Ordering (Stack b)) -> OrdM (Stack b)
forall a b. (a -> b) -> a -> b
$ \Stack b
it -> case Stack b -> Maybe (StrictPair b (Stack b))
forall a. Stack a -> Maybe (StrictPair a (Stack a))
iterNext Stack b
it of
Maybe (StrictPair b (Stack b))
Nothing -> Ordering
GT Ordering -> Stack b -> StrictPair Ordering (Stack b)
forall a b. a -> b -> StrictPair a b
:*: Stack b
it
Just (b
y :*: Stack b
it') -> a -> b -> Ordering
cmp a
x b
y Ordering -> Stack b -> StrictPair Ordering (Stack b)
forall a b. a -> b -> StrictPair a b
:*: Stack b
it'
{-# INLINE liftCmp #-}
instance Show a => Show (Set a) where
showsPrec :: Int -> Set a -> ShowS
showsPrec Int
p Set a
xs = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
[Char] -> ShowS
showString [Char]
"fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => a -> ShowS
shows (Set a -> [a]
forall a. Set a -> [a]
toList Set a
xs)
instance Show1 Set where
liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Set a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d Set a
m =
(Int -> [a] -> ShowS) -> [Char] -> Int -> [a] -> ShowS
forall a. (Int -> a -> ShowS) -> [Char] -> Int -> a -> ShowS
showsUnaryWith ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> [a] -> ShowS
forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> [a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) [Char]
"fromList" Int
d (Set a -> [a]
forall a. Set a -> [a]
toList Set a
m)
instance (Read a, Ord a) => Read (Set a) where
#if defined(__GLASGOW_HASKELL__) || defined(__MHS__)
readPrec :: ReadPrec (Set a)
readPrec = ReadPrec (Set a) -> ReadPrec (Set a)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (Set a) -> ReadPrec (Set a))
-> ReadPrec (Set a) -> ReadPrec (Set a)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (Set a) -> ReadPrec (Set a)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (Set a) -> ReadPrec (Set a))
-> ReadPrec (Set a) -> ReadPrec (Set a)
forall a b. (a -> b) -> a -> b
$ do
Ident "fromList" <- ReadPrec Lexeme
lexP
xs <- readPrec
return (fromList xs)
readListPrec :: ReadPrec [Set a]
readListPrec = ReadPrec [Set a]
forall a. Read a => ReadPrec [a]
readListPrecDefault
#else
readsPrec p = readParen (p > 10) $ \ r -> do
("fromList",s) <- lex r
(xs,t) <- reads s
return (fromList xs,t)
#endif
instance NFData a => NFData (Set a) where
rnf :: Set a -> ()
rnf Set a
Tip = ()
rnf (Bin Int
_ a
y Set a
l Set a
r) = a -> ()
forall a. NFData a => a -> ()
rnf a
y () -> () -> ()
forall a b. a -> b -> b
`seq` Set a -> ()
forall a. NFData a => a -> ()
rnf Set a
l () -> () -> ()
forall a b. a -> b -> b
`seq` Set a -> ()
forall a. NFData a => a -> ()
rnf Set a
r
instance NFData1 Set where
liftRnf :: forall a. (a -> ()) -> Set a -> ()
liftRnf a -> ()
rnfx = Set a -> ()
go
where
go :: Set a -> ()
go Set a
Tip = ()
go (Bin Int
_ a
y Set a
l Set a
r) = a -> ()
rnfx a
y () -> () -> ()
forall a b. a -> b -> b
`seq` Set a -> ()
go Set a
l () -> () -> ()
forall a b. a -> b -> b
`seq` Set a -> ()
go Set a
r
split :: Ord a => a -> Set a -> (Set a,Set a)
split :: forall a. Ord a => a -> Set a -> (Set a, Set a)
split a
x Set a
t = StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Set a) (Set a) -> (Set a, Set a))
-> StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. (a -> b) -> a -> b
$ a -> Set a -> StrictPair (Set a) (Set a)
forall a. Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS a
x Set a
t
{-# INLINABLE split #-}
splitS :: Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS :: forall a. Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS a
_ Set a
Tip = (Set a
forall a. Set a
Tip Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
forall a. Set a
Tip)
splitS a
x (Bin Int
_ a
y Set a
l Set a
r)
= case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT -> let (Set a
lt :*: Set a
gt) = a -> Set a -> StrictPair (Set a) (Set a)
forall a. Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS a
x Set a
l in (Set a
lt Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
y Set a
gt Set a
r)
Ordering
GT -> let (Set a
lt :*: Set a
gt) = a -> Set a -> StrictPair (Set a) (Set a)
forall a. Ord a => a -> Set a -> StrictPair (Set a) (Set a)
splitS a
x Set a
r in (a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
y Set a
l Set a
lt Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
gt)
Ordering
EQ -> (Set a
l Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
r)
{-# INLINABLE splitS #-}
splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
splitMember :: forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x0 Set a
t = case a -> Set a -> StrictTriple (Set a) Bool (Set a)
forall {a}.
Ord a =>
a -> Set a -> StrictTriple (Set a) Bool (Set a)
go a
x0 Set a
t of
TripleS Set a
lt Bool
found Set a
gt -> (Set a
lt, Bool
found, Set a
gt)
where
go :: a -> Set a -> StrictTriple (Set a) Bool (Set a)
go a
_ Set a
Tip = Set a -> Bool -> Set a -> StrictTriple (Set a) Bool (Set a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS Set a
forall a. Set a
Tip Bool
False Set a
forall a. Set a
Tip
go a
x (Bin Int
_ a
y Set a
l Set a
r) =
case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of
Ordering
LT -> case a -> Set a -> StrictTriple (Set a) Bool (Set a)
go a
x Set a
l of
TripleS Set a
lt Bool
found Set a
gt -> Set a -> Bool -> Set a -> StrictTriple (Set a) Bool (Set a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS Set a
lt Bool
found (a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
y Set a
gt Set a
r)
Ordering
GT -> case a -> Set a -> StrictTriple (Set a) Bool (Set a)
go a
x Set a
r of
TripleS Set a
lt Bool
found Set a
gt -> Set a -> Bool -> Set a -> StrictTriple (Set a) Bool (Set a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS (a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
y Set a
l Set a
lt) Bool
found Set a
gt
Ordering
EQ -> Set a -> Bool -> Set a -> StrictTriple (Set a) Bool (Set a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS Set a
l Bool
True Set a
r
{-# INLINABLE splitMember #-}
findIndex :: Ord a => a -> Set a -> Int
findIndex :: forall a. Ord a => a -> Set a -> Int
findIndex = Int -> a -> Set a -> Int
forall a. Ord a => Int -> a -> Set a -> Int
go Int
0
where
go :: Ord a => Int -> a -> Set a -> Int
go :: forall a. Ord a => Int -> a -> Set a -> Int
go !Int
_ !a
_ Set a
Tip = [Char] -> Int
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.findIndex: element is not in the set"
go Int
idx a
x (Bin Int
_ a
kx Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
kx of
Ordering
LT -> Int -> a -> Set a -> Int
forall a. Ord a => Int -> a -> Set a -> Int
go Int
idx a
x Set a
l
Ordering
GT -> Int -> a -> Set a -> Int
forall a. Ord a => Int -> a -> Set a -> Int
go (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
x Set a
r
Ordering
EQ -> Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l
{-# INLINABLE findIndex #-}
lookupIndex :: Ord a => a -> Set a -> Maybe Int
lookupIndex :: forall a. Ord a => a -> Set a -> Maybe Int
lookupIndex = Int -> a -> Set a -> Maybe Int
forall a. Ord a => Int -> a -> Set a -> Maybe Int
go Int
0
where
go :: Ord a => Int -> a -> Set a -> Maybe Int
go :: forall a. Ord a => Int -> a -> Set a -> Maybe Int
go !Int
_ !a
_ Set a
Tip = Maybe Int
forall a. Maybe a
Nothing
go Int
idx a
x (Bin Int
_ a
kx Set a
l Set a
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
kx of
Ordering
LT -> Int -> a -> Set a -> Maybe Int
forall a. Ord a => Int -> a -> Set a -> Maybe Int
go Int
idx a
x Set a
l
Ordering
GT -> Int -> a -> Set a -> Maybe Int
forall a. Ord a => Int -> a -> Set a -> Maybe Int
go (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
x Set a
r
Ordering
EQ -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$! Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
l
{-# INLINABLE lookupIndex #-}
elemAt :: Int -> Set a -> a
elemAt :: forall a. Int -> Set a -> a
elemAt !Int
_ Set a
Tip = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.elemAt: index out of range"
elemAt Int
i (Bin Int
_ a
x Set a
l Set a
r)
= case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
Ordering
LT -> Int -> Set a -> a
forall a. Int -> Set a -> a
elemAt Int
i Set a
l
Ordering
GT -> Int -> Set a -> a
forall a. Int -> Set a -> a
elemAt (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Set a
r
Ordering
EQ -> a
x
where
sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
deleteAt :: Int -> Set a -> Set a
deleteAt :: forall a. Int -> Set a -> Set a
deleteAt !Int
i Set a
t =
case Set a
t of
Set a
Tip -> [Char] -> Set a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.deleteAt: index out of range"
Bin Int
_ a
x Set a
l Set a
r -> case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
Ordering
LT -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
x (Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
deleteAt Int
i Set a
l) Set a
r
Ordering
GT -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
x Set a
l (Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
deleteAt (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Set a
r)
Ordering
EQ -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
where
sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
take :: Int -> Set a -> Set a
take :: forall a. Int -> Set a -> Set a
take Int
i Set a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Set a -> Int
forall a. Set a -> Int
size Set a
m = Set a
m
take Int
i0 Set a
m0 = Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
go Int
i0 Set a
m0
where
go :: Int -> Set a -> Set a
go Int
i !Set a
_ | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Set a
forall a. Set a
Tip
go !Int
_ Set a
Tip = Set a
forall a. Set a
Tip
go Int
i (Bin Int
_ a
x Set a
l Set a
r) =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
Ordering
LT -> Int -> Set a -> Set a
go Int
i Set a
l
Ordering
GT -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l (Int -> Set a -> Set a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Set a
r)
Ordering
EQ -> Set a
l
where sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
drop :: Int -> Set a -> Set a
drop :: forall a. Int -> Set a -> Set a
drop Int
i Set a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Set a -> Int
forall a. Set a -> Int
size Set a
m = Set a
forall a. Set a
Tip
drop Int
i0 Set a
m0 = Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
go Int
i0 Set a
m0
where
go :: Int -> Set a -> Set a
go Int
i Set a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Set a
m
go !Int
_ Set a
Tip = Set a
forall a. Set a
Tip
go Int
i (Bin Int
_ a
x Set a
l Set a
r) =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
Ordering
LT -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
x (Int -> Set a -> Set a
go Int
i Set a
l) Set a
r
Ordering
GT -> Int -> Set a -> Set a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Set a
r
Ordering
EQ -> a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x Set a
r
where sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
splitAt :: Int -> Set a -> (Set a, Set a)
splitAt :: forall a. Int -> Set a -> (Set a, Set a)
splitAt Int
i0 Set a
m0
| Int
i0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Set a -> Int
forall a. Set a -> Int
size Set a
m0 = (Set a
m0, Set a
forall a. Set a
Tip)
| Bool
otherwise = StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Set a) (Set a) -> (Set a, Set a))
-> StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. (a -> b) -> a -> b
$ Int -> Set a -> StrictPair (Set a) (Set a)
forall {a}. Int -> Set a -> StrictPair (Set a) (Set a)
go Int
i0 Set a
m0
where
go :: Int -> Set a -> StrictPair (Set a) (Set a)
go Int
i Set a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Set a
forall a. Set a
Tip Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
m
go !Int
_ Set a
Tip = Set a
forall a. Set a
Tip Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
forall a. Set a
Tip
go Int
i (Bin Int
_ a
x Set a
l Set a
r)
= case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
Ordering
LT -> case Int -> Set a -> StrictPair (Set a) (Set a)
go Int
i Set a
l of
Set a
ll :*: Set a
lr -> Set a
ll Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
x Set a
lr Set a
r
Ordering
GT -> case Int -> Set a -> StrictPair (Set a) (Set a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Set a
r of
Set a
rl :*: Set a
rr -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l Set a
rl Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
rr
Ordering
EQ -> Set a
l Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x Set a
r
where sizeL :: Int
sizeL = Set a -> Int
forall a. Set a -> Int
size Set a
l
takeWhileAntitone :: (a -> Bool) -> Set a -> Set a
takeWhileAntitone :: forall a. (a -> Bool) -> Set a -> Set a
takeWhileAntitone a -> Bool
_ Set a
Tip = Set a
forall a. Set a
Tip
takeWhileAntitone a -> Bool
p (Bin Int
_ a
x Set a
l Set a
r)
| a -> Bool
p a
x = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l ((a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
takeWhileAntitone a -> Bool
p Set a
r)
| Bool
otherwise = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
takeWhileAntitone a -> Bool
p Set a
l
dropWhileAntitone :: (a -> Bool) -> Set a -> Set a
dropWhileAntitone :: forall a. (a -> Bool) -> Set a -> Set a
dropWhileAntitone a -> Bool
_ Set a
Tip = Set a
forall a. Set a
Tip
dropWhileAntitone a -> Bool
p (Bin Int
_ a
x Set a
l Set a
r)
| a -> Bool
p a
x = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
dropWhileAntitone a -> Bool
p Set a
r
| Bool
otherwise = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
x ((a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
dropWhileAntitone a -> Bool
p Set a
l) Set a
r
spanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a)
spanAntitone :: forall a. (a -> Bool) -> Set a -> (Set a, Set a)
spanAntitone a -> Bool
p0 Set a
m = StrictPair (Set a) (Set a) -> (Set a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair ((a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
forall {a}. (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p0 Set a
m)
where
go :: (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
_ Set a
Tip = Set a
forall a. Set a
Tip Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
forall a. Set a
Tip
go a -> Bool
p (Bin Int
_ a
x Set a
l Set a
r)
| a -> Bool
p a
x = let Set a
u :*: Set a
v = (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p Set a
r in a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l Set a
u Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: Set a
v
| Bool
otherwise = let Set a
u :*: Set a
v = (a -> Bool) -> Set a -> StrictPair (Set a) (Set a)
go a -> Bool
p Set a
l in Set a
u Set a -> Set a -> StrictPair (Set a) (Set a)
forall a b. a -> b -> StrictPair a b
:*: a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
linkR a
x Set a
v Set a
r
data SetBuilder a
= BAsc !(Stack a)
| BSet !(Set a)
emptyB :: SetBuilder a
emptyB :: forall a. SetBuilder a
emptyB = Stack a -> SetBuilder a
forall a. Stack a -> SetBuilder a
BAsc Stack a
forall a. Stack a
Nada
insertB :: Ord a => a -> SetBuilder a -> SetBuilder a
insertB :: forall a. Ord a => a -> SetBuilder a -> SetBuilder a
insertB !a
y SetBuilder a
b = case SetBuilder a
b of
BAsc Stack a
stk -> case Stack a
stk of
Push a
x Set a
l Stack a
stk' -> case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
y a
x of
Ordering
LT -> Set a -> SetBuilder a
forall a. Set a -> SetBuilder a
BSet (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
insert a
y (Stack a -> Set a
forall a. Stack a -> Set a
ascLinkAll Stack a
stk))
Ordering
EQ -> Stack a -> SetBuilder a
forall a. Stack a -> SetBuilder a
BAsc (a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
l Stack a
stk')
Ordering
GT -> case Set a
l of
Set a
Tip -> Stack a -> SetBuilder a
forall a. Stack a -> SetBuilder a
BAsc (Stack a -> Int -> Set a -> a -> Stack a
forall a. Stack a -> Int -> Set a -> a -> Stack a
ascLinkTop Stack a
stk' Int
1 (a -> Set a
forall a. a -> Set a
singleton a
x) a
y)
Bin{} -> Stack a -> SetBuilder a
forall a. Stack a -> SetBuilder a
BAsc (a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
stk)
Stack a
Nada -> Stack a -> SetBuilder a
forall a. Stack a -> SetBuilder a
BAsc (a -> Set a -> Stack a -> Stack a
forall a. a -> Set a -> Stack a -> Stack a
Push a
y Set a
forall a. Set a
Tip Stack a
forall a. Stack a
Nada)
BSet Set a
m -> Set a -> SetBuilder a
forall a. Set a -> SetBuilder a
BSet (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
insert a
y Set a
m)
{-# INLINE insertB #-}
finishB :: SetBuilder a -> Set a
finishB :: forall a. SetBuilder a -> Set a
finishB (BAsc Stack a
stk) = Stack a -> Set a
forall a. Stack a -> Set a
ascLinkAll Stack a
stk
finishB (BSet Set a
s) = Set a
s
{-# INLINABLE finishB #-}
link :: a -> Set a -> Set a -> Set a
link :: forall a. a -> Set a -> Set a -> Set a
link a
x Set a
Tip Set a
r = a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x Set a
r
link a
x Set a
l Set a
Tip = a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMax a
x Set a
l
link a
x l :: Set a
l@(Bin Int
lsz a
lx Set a
ll Set a
lr) r :: Set a
r@(Bin Int
rsz a
rx Set a
rl Set a
rr)
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rsz = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
rx (a -> Int -> Set a -> Set a -> Set a
forall a. a -> Int -> Set a -> Set a -> Set a
linkR_ a
x Int
lsz Set a
l Set a
rl) Set a
rr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
lsz = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
lx Set a
ll (a -> Set a -> Int -> Set a -> Set a
forall a. a -> Set a -> Int -> Set a -> Set a
linkL_ a
x Set a
lr Int
rsz Set a
r)
| Bool
otherwise = Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lszInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsz) a
x Set a
l Set a
r
linkL :: a -> Set a -> Set a -> Set a
linkL :: forall a. a -> Set a -> Set a -> Set a
linkL a
x Set a
l Set a
r = case Set a
r of
Set a
Tip -> a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMax a
x Set a
l
Bin Int
rsz a
_ Set a
_ Set a
_ -> a -> Set a -> Int -> Set a -> Set a
forall a. a -> Set a -> Int -> Set a -> Set a
linkL_ a
x Set a
l Int
rsz Set a
r
linkL_ :: a -> Set a -> Int -> Set a -> Set a
linkL_ :: forall a. a -> Set a -> Int -> Set a -> Set a
linkL_ a
x Set a
l !Int
rsz Set a
r = case Set a
l of
Bin Int
lsz a
lx Set a
ll Set a
lr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
lsz -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
lx Set a
ll (a -> Set a -> Int -> Set a -> Set a
forall a. a -> Set a -> Int -> Set a -> Set a
linkL_ a
x Set a
lr Int
rsz Set a
r)
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lszInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsz) a
x Set a
l Set a
r
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsz) a
x Set a
forall a. Set a
Tip Set a
r
linkR :: a -> Set a -> Set a -> Set a
linkR :: forall a. a -> Set a -> Set a -> Set a
linkR a
x Set a
l Set a
r = case Set a
l of
Set a
Tip -> a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x Set a
r
Bin Int
lsz a
_ Set a
_ Set a
_ -> a -> Int -> Set a -> Set a -> Set a
forall a. a -> Int -> Set a -> Set a -> Set a
linkR_ a
x Int
lsz Set a
l Set a
r
linkR_ :: a -> Int -> Set a -> Set a -> Set a
linkR_ :: forall a. a -> Int -> Set a -> Set a -> Set a
linkR_ a
x !Int
lsz Set a
l Set a
r = case Set a
r of
Bin Int
rsz a
rx Set a
rl Set a
rr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rsz -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
rx (a -> Int -> Set a -> Set a -> Set a
forall a. a -> Int -> Set a -> Set a -> Set a
linkR_ a
x Int
lsz Set a
l Set a
rl) Set a
rr
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lszInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsz) a
x Set a
l Set a
r
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsz) a
x Set a
l Set a
forall a. Set a
Tip
insertMax :: a -> Set a -> Set a
insertMax :: forall a. a -> Set a -> Set a
insertMax a
x Set a
t
= case Set a
t of
Set a
Tip -> a -> Set a
forall a. a -> Set a
singleton a
x
Bin Int
_ a
y Set a
l Set a
r
-> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
y Set a
l (a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMax a
x Set a
r)
insertMin :: a -> Set a -> Set a
insertMin :: forall a. a -> Set a -> Set a
insertMin a
x Set a
t
= case Set a
t of
Set a
Tip -> a -> Set a
forall a. a -> Set a
singleton a
x
Bin Int
_ a
y Set a
l Set a
r
-> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
y (a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x Set a
l) Set a
r
link2 :: Set a -> Set a -> Set a
link2 :: forall a. Set a -> Set a -> Set a
link2 Set a
Tip Set a
r = Set a
r
link2 Set a
l Set a
Tip = Set a
l
link2 l :: Set a
l@(Bin Int
lsz a
lx Set a
ll Set a
lr) r :: Set a
r@(Bin Int
rsz a
rx Set a
rl Set a
rr)
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rsz = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
rx (Int -> Set a -> Set a -> Set a
forall a. Int -> Set a -> Set a -> Set a
link2R_ Int
lsz Set a
l Set a
rl) Set a
rr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
lsz = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
lx Set a
ll (Set a -> Int -> Set a -> Set a
forall a. Set a -> Int -> Set a -> Set a
link2L_ Set a
lr Int
rsz Set a
r)
| Bool
otherwise = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
link2L_ :: Set a -> Int -> Set a -> Set a
link2L_ :: forall a. Set a -> Int -> Set a -> Set a
link2L_ Set a
l !Int
rsz Set a
r = case Set a
l of
Bin Int
lsz a
lx Set a
ll Set a
lr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
lsz -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR a
lx Set a
ll (Set a -> Int -> Set a -> Set a
forall a. Set a -> Int -> Set a -> Set a
link2L_ Set a
lr Int
rsz Set a
r)
| Bool
otherwise -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
Set a
Tip -> Set a
r
link2R_ :: Int -> Set a -> Set a -> Set a
link2R_ :: forall a. Int -> Set a -> Set a -> Set a
link2R_ !Int
lsz Set a
l Set a
r = case Set a
r of
Bin Int
rsz a
rx Set a
rl Set a
rr
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rsz -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL a
rx (Int -> Set a -> Set a -> Set a
forall a. Int -> Set a -> Set a -> Set a
link2R_ Int
lsz Set a
l Set a
rl) Set a
rr
| Bool
otherwise -> Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
glue Set a
l Set a
r
Set a
Tip -> Set a
l
glue :: Set a -> Set a -> Set a
glue :: forall a. Set a -> Set a -> Set a
glue Set a
Tip Set a
r = Set a
r
glue Set a
l Set a
Tip = Set a
l
glue l :: Set a
l@(Bin Int
sl a
xl Set a
ll Set a
lr) r :: Set a
r@(Bin Int
sr a
xr Set a
rl Set a
rr)
| Int
sl Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
sr = let !(a
m :*: Set a
l') = a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
maxViewSure a
xl Set a
ll Set a
lr in Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
slInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sr) a
m Set a
l' Set a
r
| Bool
otherwise = let !(a
m :*: Set a
r') = a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
minViewSure a
xr Set a
rl Set a
rr in Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
slInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sr) a
m Set a
l Set a
r'
deleteFindMin :: Set a -> (a,Set a)
deleteFindMin :: forall a. Set a -> (a, Set a)
deleteFindMin Set a
t
| Just (a, Set a)
r <- Set a -> Maybe (a, Set a)
forall a. Set a -> Maybe (a, Set a)
minView Set a
t = (a, Set a)
r
| Bool
otherwise = ([Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.deleteFindMin: can not return the minimal element of an empty set", Set a
forall a. Set a
Tip)
deleteFindMax :: Set a -> (a,Set a)
deleteFindMax :: forall a. Set a -> (a, Set a)
deleteFindMax Set a
t
| Just (a, Set a)
r <- Set a -> Maybe (a, Set a)
forall a. Set a -> Maybe (a, Set a)
maxView Set a
t = (a, Set a)
r
| Bool
otherwise = ([Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Set.deleteFindMax: can not return the maximal element of an empty set", Set a
forall a. Set a
Tip)
minViewSure :: a -> Set a -> Set a -> StrictPair a (Set a)
minViewSure :: forall a. a -> Set a -> Set a -> StrictPair a (Set a)
minViewSure = a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
go
where
go :: t -> Set t -> Set t -> StrictPair t (Set t)
go t
x Set t
Tip Set t
r = t
x t -> Set t -> StrictPair t (Set t)
forall a b. a -> b -> StrictPair a b
:*: Set t
r
go t
x (Bin Int
_ t
xl Set t
ll Set t
lr) Set t
r =
case t -> Set t -> Set t -> StrictPair t (Set t)
go t
xl Set t
ll Set t
lr of
t
xm :*: Set t
l' -> t
xm t -> Set t -> StrictPair t (Set t)
forall a b. a -> b -> StrictPair a b
:*: t -> Set t -> Set t -> Set t
forall a. a -> Set a -> Set a -> Set a
balanceR t
x Set t
l' Set t
r
minView :: Set a -> Maybe (a, Set a)
minView :: forall a. Set a -> Maybe (a, Set a)
minView Set a
Tip = Maybe (a, Set a)
forall a. Maybe a
Nothing
minView (Bin Int
_ a
x Set a
l Set a
r) = (a, Set a) -> Maybe (a, Set a)
forall a. a -> Maybe a
Just ((a, Set a) -> Maybe (a, Set a)) -> (a, Set a) -> Maybe (a, Set a)
forall a b. (a -> b) -> a -> b
$! StrictPair a (Set a) -> (a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair a (Set a) -> (a, Set a))
-> StrictPair a (Set a) -> (a, Set a)
forall a b. (a -> b) -> a -> b
$ a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
minViewSure a
x Set a
l Set a
r
maxViewSure :: a -> Set a -> Set a -> StrictPair a (Set a)
maxViewSure :: forall a. a -> Set a -> Set a -> StrictPair a (Set a)
maxViewSure = a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
go
where
go :: t -> Set t -> Set t -> StrictPair t (Set t)
go t
x Set t
l Set t
Tip = t
x t -> Set t -> StrictPair t (Set t)
forall a b. a -> b -> StrictPair a b
:*: Set t
l
go t
x Set t
l (Bin Int
_ t
xr Set t
rl Set t
rr) =
case t -> Set t -> Set t -> StrictPair t (Set t)
go t
xr Set t
rl Set t
rr of
t
xm :*: Set t
r' -> t
xm t -> Set t -> StrictPair t (Set t)
forall a b. a -> b -> StrictPair a b
:*: t -> Set t -> Set t -> Set t
forall a. a -> Set a -> Set a -> Set a
balanceL t
x Set t
l Set t
r'
maxView :: Set a -> Maybe (a, Set a)
maxView :: forall a. Set a -> Maybe (a, Set a)
maxView Set a
Tip = Maybe (a, Set a)
forall a. Maybe a
Nothing
maxView (Bin Int
_ a
x Set a
l Set a
r) = (a, Set a) -> Maybe (a, Set a)
forall a. a -> Maybe a
Just ((a, Set a) -> Maybe (a, Set a)) -> (a, Set a) -> Maybe (a, Set a)
forall a b. (a -> b) -> a -> b
$! StrictPair a (Set a) -> (a, Set a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair a (Set a) -> (a, Set a))
-> StrictPair a (Set a) -> (a, Set a)
forall a b. (a -> b) -> a -> b
$ a -> Set a -> Set a -> StrictPair a (Set a)
forall a. a -> Set a -> Set a -> StrictPair a (Set a)
maxViewSure a
x Set a
l Set a
r
delta,ratio :: Int
delta :: Int
delta = Int
3
ratio :: Int
ratio = Int
2
balanceL :: a -> Set a -> Set a -> Set a
balanceL :: forall a. a -> Set a -> Set a -> Set a
balanceL a
x Set a
l Set a
r = case (Set a
l, Set a
r) of
(Bin Int
ls a
_ Set a
_ Set a
_, Bin Int
rs a
_ Set a
_ Set a
_)
| Int
ls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rs -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
x Set a
l Set a
r
(Set a, Set a)
_ -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceL_ a
x Set a
l Set a
r
{-# INLINE balanceL #-}
balanceL_ :: a -> Set a -> Set a -> Set a
balanceL_ :: forall a. a -> Set a -> Set a -> Set a
balanceL_ a
x Set a
l Set a
r = case Set a
r of
Set a
Tip -> case Set a
l of
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip
(Bin Int
_ a
_ Set a
Tip Set a
Tip) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
2 a
x Set a
l Set a
forall a. Set a
Tip
(Bin Int
_ a
lx Set a
Tip (Bin Int
_ a
lrx Set a
_ Set a
_)) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
3 a
lrx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
lx Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip)
(Bin Int
_ a
lx ll :: Set a
ll@(Bin Int
_ a
_ Set a
_ Set a
_) Set a
Tip) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
3 a
lx Set a
ll (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip)
(Bin Int
ls a
lx ll :: Set a
ll@(Bin Int
lls a
_ Set a
_ Set a
_) lr :: Set a
lr@(Bin Int
lrs a
lrx Set a
lrl Set a
lrr))
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) a
lx Set a
ll (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) a
x Set a
lr Set a
forall a. Set a
Tip)
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) a
lrx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
lrl) a
lx Set a
ll Set a
lrl) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
lrr) a
x Set a
lrr Set a
forall a. Set a
Tip)
(Bin Int
rs a
_ Set a
_ Set a
_) -> case Set a
l of
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
x Set a
forall a. Set a
Tip Set a
r
(Bin Int
ls a
lx Set a
ll Set a
lr) -> case (Set a
ll, Set a
lr) of
(Bin Int
lls a
_ Set a
_ Set a
_, Bin Int
lrs a
lrx Set a
lrl Set a
lrr)
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
lx Set a
ll (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) a
x Set a
lr Set a
r)
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
lrx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
lrl) a
lx Set a
ll Set a
lrl) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
lrr) a
x Set a
lrr Set a
r)
(Set a
_, Set a
_) -> [Char] -> Set a
forall a. HasCallStack => [Char] -> a
error [Char]
"Failure in Data.Set.balanceL_"
{-# NOINLINE balanceL_ #-}
balanceR :: a -> Set a -> Set a -> Set a
balanceR :: forall a. a -> Set a -> Set a -> Set a
balanceR a
x Set a
l Set a
r = case (Set a
l, Set a
r) of
(Bin Int
ls a
_ Set a
_ Set a
_, Bin Int
rs a
_ Set a
_ Set a
_)
| Int
rs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
ls -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
x Set a
l Set a
r
(Set a, Set a)
_ -> a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
balanceR_ a
x Set a
l Set a
r
{-# INLINE balanceR #-}
balanceR_ :: a -> Set a -> Set a -> Set a
balanceR_ :: forall a. a -> Set a -> Set a -> Set a
balanceR_ a
x Set a
l Set a
r = case Set a
l of
Set a
Tip -> case Set a
r of
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip
(Bin Int
_ a
_ Set a
Tip Set a
Tip) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
2 a
x Set a
forall a. Set a
Tip Set a
r
(Bin Int
_ a
rx Set a
Tip rr :: Set a
rr@(Bin Int
_ a
_ Set a
_ Set a
_)) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
3 a
rx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip) Set a
rr
(Bin Int
_ a
rx (Bin Int
_ a
rlx Set a
_ Set a
_) Set a
Tip) -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
3 a
rlx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
x Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin Int
1 a
rx Set a
forall a. Set a
Tip Set a
forall a. Set a
Tip)
(Bin Int
rs a
rx rl :: Set a
rl@(Bin Int
rls a
rlx Set a
rll Set a
rlr) rr :: Set a
rr@(Bin Int
rrs a
_ Set a
_ Set a
_))
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
rx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) a
x Set a
forall a. Set a
Tip Set a
rl) Set a
rr
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
rlx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
rll) a
x Set a
forall a. Set a
Tip Set a
rll) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
rlr) a
rx Set a
rlr Set a
rr)
(Bin Int
ls a
_ Set a
_ Set a
_) -> case Set a
r of
Set a
Tip -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) a
x Set a
l Set a
forall a. Set a
Tip
(Bin Int
rs a
rx Set a
rl Set a
rr) -> case (Set a
rl, Set a
rr) of
(Bin Int
rls a
rlx Set a
rll Set a
rlr, Bin Int
rrs a
_ Set a
_ Set a
_)
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
rx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) a
x Set a
l Set a
rl) Set a
rr
| Bool
otherwise -> Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) a
rlx (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
rll) a
x Set a
l Set a
rll) (Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Set a -> Int
forall a. Set a -> Int
size Set a
rlr) a
rx Set a
rlr Set a
rr)
(Set a
_, Set a
_) -> [Char] -> Set a
forall a. HasCallStack => [Char] -> a
error [Char]
"Failure in Data.Set.balanceR_"
{-# NOINLINE balanceR_ #-}
bin :: a -> Set a -> Set a -> Set a
bin :: forall a. a -> Set a -> Set a -> Set a
bin a
x Set a
l Set a
r
= Int -> a -> Set a -> Set a -> Set a
forall a. Int -> a -> Set a -> Set a -> Set a
Bin (Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
x Set a
l Set a
r
{-# INLINE bin #-}
splitRoot :: Set a -> [Set a]
splitRoot :: forall a. Set a -> [Set a]
splitRoot Set a
orig =
case Set a
orig of
Set a
Tip -> []
Bin Int
_ a
v Set a
l Set a
r -> [Set a
l, a -> Set a
forall a. a -> Set a
singleton a
v, Set a
r]
{-# INLINE splitRoot #-}
powerSet :: Set a -> Set (Set a)
powerSet :: forall a. Set a -> Set (Set a)
powerSet Set a
xs0 = Set a -> Set (Set a) -> Set (Set a)
forall a. a -> Set a -> Set a
insertMin Set a
forall a. Set a
empty ((a -> Set (Set a) -> Set (Set a))
-> Set (Set a) -> Set a -> Set (Set a)
forall a b. (a -> b -> b) -> b -> Set a -> b
foldr' a -> Set (Set a) -> Set (Set a)
forall {a}. a -> Set (Set a) -> Set (Set a)
step Set (Set a)
forall a. Set a
Tip Set a
xs0) where
step :: a -> Set (Set a) -> Set (Set a)
step a
x Set (Set a)
pxs = Set a -> Set (Set a) -> Set (Set a)
forall a. a -> Set a -> Set a
insertMin (a -> Set a
forall a. a -> Set a
singleton a
x) (a -> Set a -> Set a
forall a. a -> Set a -> Set a
insertMin a
x (Set a -> Set a) -> Set (Set a) -> Set (Set a)
forall a b. (a -> b) -> Set a -> Set b
`mapMonotonic` Set (Set a)
pxs) Set (Set a) -> Set (Set a) -> Set (Set a)
forall a. Set a -> Set a -> Set a
`glue` Set (Set a)
pxs
cartesianProduct :: Set a -> Set b -> Set (a, b)
cartesianProduct :: forall a b. Set a -> Set b -> Set (a, b)
cartesianProduct !Set a
_as Set b
Tip = Set (a, b)
forall a. Set a
Tip
cartesianProduct Set a
as (Bin Int
1 b
b Set b
_ Set b
_) = (a -> (a, b)) -> Set a -> Set (a, b)
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic ((a -> b -> (a, b)) -> b -> a -> (a, b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) b
b) Set a
as
cartesianProduct Set a
as Set b
bs =
MergeSet (a, b) -> Set (a, b)
forall a. MergeSet a -> Set a
getMergeSet (MergeSet (a, b) -> Set (a, b)) -> MergeSet (a, b) -> Set (a, b)
forall a b. (a -> b) -> a -> b
$ (a -> MergeSet (a, b)) -> Set a -> MergeSet (a, b)
forall m a. Monoid m => (a -> m) -> Set a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\a
a -> Set (a, b) -> MergeSet (a, b)
forall a. Set a -> MergeSet a
MergeSet (Set (a, b) -> MergeSet (a, b)) -> Set (a, b) -> MergeSet (a, b)
forall a b. (a -> b) -> a -> b
$ (b -> (a, b)) -> Set b -> Set (a, b)
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic ((,) a
a) Set b
bs) Set a
as
newtype MergeSet a = MergeSet { forall a. MergeSet a -> Set a
getMergeSet :: Set a }
instance Semigroup (MergeSet a) where
MergeSet Set a
xs <> :: MergeSet a -> MergeSet a -> MergeSet a
<> MergeSet Set a
ys = Set a -> MergeSet a
forall a. Set a -> MergeSet a
MergeSet (Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
xs Set a
ys)
instance Monoid (MergeSet a) where
mempty :: MergeSet a
mempty = Set a -> MergeSet a
forall a. Set a -> MergeSet a
MergeSet Set a
forall a. Set a
empty
#if !MIN_VERSION_base(4,11,0)
mappend = (<>)
#endif
disjointUnion :: Set a -> Set b -> Set (Either a b)
disjointUnion :: forall a b. Set a -> Set b -> Set (Either a b)
disjointUnion Set a
as Set b
bs = Set (Either a b) -> Set (Either a b) -> Set (Either a b)
forall a. Set a -> Set a -> Set a
link2 ((a -> Either a b) -> Set a -> Set (Either a b)
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic a -> Either a b
forall a b. a -> Either a b
Left Set a
as) ((b -> Either a b) -> Set b -> Set (Either a b)
forall a b. (a -> b) -> Set a -> Set b
mapMonotonic b -> Either a b
forall a b. b -> Either a b
Right Set b
bs)
data WhenMissing f a = WhenMissing
{ forall (f :: * -> *) a. WhenMissing f a -> Set a -> f (Set a)
missingSubtree :: Set a -> f (Set a)
, forall (f :: * -> *) a. WhenMissing f a -> a -> f Bool
missingElem :: a -> f Bool
}
type SimpleWhenMissing = WhenMissing Identity
runWhenMissing :: WhenMissing f a -> a -> f Bool
runWhenMissing :: forall (f :: * -> *) a. WhenMissing f a -> a -> f Bool
runWhenMissing = WhenMissing f a -> a -> f Bool
forall (f :: * -> *) a. WhenMissing f a -> a -> f Bool
missingElem
newtype WhenMatched f a = WhenMatched { forall (f :: * -> *) a. WhenMatched f a -> a -> f Bool
matchedElem :: a -> f Bool }
type SimpleWhenMatched = WhenMatched Identity
runWhenMatched :: WhenMatched f a -> a -> f Bool
runWhenMatched :: forall (f :: * -> *) a. WhenMatched f a -> a -> f Bool
runWhenMatched = WhenMatched f a -> a -> f Bool
forall (f :: * -> *) a. WhenMatched f a -> a -> f Bool
matchedElem
dropMatched :: Applicative f => WhenMatched f a
dropMatched :: forall (f :: * -> *) a. Applicative f => WhenMatched f a
dropMatched = (a -> f Bool) -> WhenMatched f a
forall (f :: * -> *) a. (a -> f Bool) -> WhenMatched f a
WhenMatched (\a
_ -> Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
{-# INLINE dropMatched #-}
preserveMatched :: Applicative f => WhenMatched f a
preserveMatched :: forall (f :: * -> *) a. Applicative f => WhenMatched f a
preserveMatched = (a -> f Bool) -> WhenMatched f a
forall (f :: * -> *) a. (a -> f Bool) -> WhenMatched f a
WhenMatched (\a
_ -> Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)
{-# INLINE preserveMatched #-}
filterMatched :: Applicative f => (a -> Bool) -> WhenMatched f a
filterMatched :: forall (f :: * -> *) a.
Applicative f =>
(a -> Bool) -> WhenMatched f a
filterMatched a -> Bool
f = (a -> f Bool) -> WhenMatched f a
forall (f :: * -> *) a. (a -> f Bool) -> WhenMatched f a
WhenMatched (Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> f Bool) -> (a -> Bool) -> a -> f Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
{-# INLINE filterMatched #-}
filterAMatched :: (a -> f Bool) -> WhenMatched f a
filterAMatched :: forall a (f :: * -> *). (a -> f Bool) -> WhenMatched f a
filterAMatched = (a -> f Bool) -> WhenMatched f a
forall (f :: * -> *) a. (a -> f Bool) -> WhenMatched f a
WhenMatched
whenMissing :: (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a
whenMissing :: forall a (f :: * -> *).
(a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a
whenMissing = ((Set a -> f (Set a)) -> (a -> f Bool) -> WhenMissing f a)
-> (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Set a -> f (Set a)) -> (a -> f Bool) -> WhenMissing f a
forall (f :: * -> *) a.
(Set a -> f (Set a)) -> (a -> f Bool) -> WhenMissing f a
WhenMissing
dropMissing :: Applicative f => WhenMissing f a
dropMissing :: forall (f :: * -> *) a. Applicative f => WhenMissing f a
dropMissing = WhenMissing
{ missingSubtree :: Set a -> f (Set a)
missingSubtree = \Set a
_ -> Set a -> f (Set a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set a
forall a. Set a
Tip
, missingElem :: a -> f Bool
missingElem = \a
_ -> Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
}
{-# INLINE dropMissing #-}
preserveMissing :: Applicative f => WhenMissing f a
preserveMissing :: forall (f :: * -> *) a. Applicative f => WhenMissing f a
preserveMissing = WhenMissing
{ missingSubtree :: Set a -> f (Set a)
missingSubtree = Set a -> f (Set a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
, missingElem :: a -> f Bool
missingElem = \a
_ -> Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
}
{-# INLINE preserveMissing #-}
filterMissing :: Applicative f => (a -> Bool) -> WhenMissing f a
filterMissing :: forall (f :: * -> *) a.
Applicative f =>
(a -> Bool) -> WhenMissing f a
filterMissing a -> Bool
f = WhenMissing
{ missingSubtree :: Set a -> f (Set a)
missingSubtree = Set a -> f (Set a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set a -> f (Set a)) -> (Set a -> Set a) -> Set a -> f (Set a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
filter a -> Bool
f
, missingElem :: a -> f Bool
missingElem = Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> f Bool) -> (a -> Bool) -> a -> f Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f
}
{-# INLINE filterMissing #-}
filterAMissing :: Applicative f => (a -> f Bool) -> WhenMissing f a
filterAMissing :: forall (f :: * -> *) a.
Applicative f =>
(a -> f Bool) -> WhenMissing f a
filterAMissing a -> f Bool
f = WhenMissing
{ missingSubtree :: Set a -> f (Set a)
missingSubtree = (a -> f Bool) -> Set a -> f (Set a)
forall (f :: * -> *) a.
Applicative f =>
(a -> f Bool) -> Set a -> f (Set a)
filterA a -> f Bool
f
, missingElem :: a -> f Bool
missingElem = a -> f Bool
f
}
{-# INLINE filterAMissing #-}
merge
:: Ord a
=> SimpleWhenMissing a
-> SimpleWhenMissing a
-> SimpleWhenMatched a
-> Set a
-> Set a
-> Set a
merge :: forall a.
Ord a =>
SimpleWhenMissing a
-> SimpleWhenMissing a
-> SimpleWhenMatched a
-> Set a
-> Set a
-> Set a
merge SimpleWhenMissing a
g1 SimpleWhenMissing a
g2 SimpleWhenMatched a
f = \Set a
s1 Set a
s2 -> Identity (Set a) -> Set a
forall a. Identity a -> a
runIdentity (SimpleWhenMissing a
-> SimpleWhenMissing a
-> SimpleWhenMatched a
-> Set a
-> Set a
-> Identity (Set a)
forall (f :: * -> *) a.
(Applicative f, Ord a) =>
WhenMissing f a
-> WhenMissing f a
-> WhenMatched f a
-> Set a
-> Set a
-> f (Set a)
mergeA SimpleWhenMissing a
g1 SimpleWhenMissing a
g2 SimpleWhenMatched a
f Set a
s1 Set a
s2)
{-# INLINE merge #-}
mergeA
:: (Applicative f, Ord a)
=> WhenMissing f a
-> WhenMissing f a
-> WhenMatched f a
-> Set a
-> Set a
-> f (Set a)
mergeA :: forall (f :: * -> *) a.
(Applicative f, Ord a) =>
WhenMissing f a
-> WhenMissing f a
-> WhenMatched f a
-> Set a
-> Set a
-> f (Set a)
mergeA
WhenMissing{missingSubtree :: forall (f :: * -> *) a. WhenMissing f a -> Set a -> f (Set a)
missingSubtree = Set a -> f (Set a)
g1t, missingElem :: forall (f :: * -> *) a. WhenMissing f a -> a -> f Bool
missingElem = a -> f Bool
g1k}
WhenMissing{missingSubtree :: forall (f :: * -> *) a. WhenMissing f a -> Set a -> f (Set a)
missingSubtree = Set a -> f (Set a)
g2t}
WhenMatched{matchedElem :: forall (f :: * -> *) a. WhenMatched f a -> a -> f Bool
matchedElem = a -> f Bool
f} = Set a -> Set a -> f (Set a)
go
where
go :: Set a -> Set a -> f (Set a)
go Set a
t1 Set a
Tip = Set a -> f (Set a)
g1t Set a
t1
go Set a
Tip Set a
t2 = Set a -> f (Set a)
g2t Set a
t2
go (Bin Int
_ a
x1 Set a
l1 Set a
r1) Set a
t2 = case a -> Set a -> (Set a, Bool, Set a)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
splitMember a
x1 Set a
t2 of
(Set a
l2, Bool
found, Set a
r2)
| Bool
found -> (Set a -> Bool -> Set a -> Set a)
-> f (Set a) -> f Bool -> f (Set a) -> f (Set a)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 Set a -> Bool -> Set a -> Set a
doLink f (Set a)
l1l2 (a -> f Bool
f a
x1) f (Set a)
r1r2
| Bool
otherwise -> (Set a -> Bool -> Set a -> Set a)
-> f (Set a) -> f Bool -> f (Set a) -> f (Set a)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 Set a -> Bool -> Set a -> Set a
doLink f (Set a)
l1l2 (a -> f Bool
g1k a
x1) f (Set a)
r1r2
where
doLink :: Set a -> Bool -> Set a -> Set a
doLink Set a
l' Bool
True Set a
r' = a -> Set a -> Set a -> Set a
forall a. a -> Set a -> Set a -> Set a
link a
x1 Set a
l' Set a
r'
doLink Set a
l' Bool
False Set a
r' = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
link2 Set a
l' Set a
r'
l1l2 :: f (Set a)
l1l2 = Set a -> Set a -> f (Set a)
go Set a
l1 Set a
l2
r1r2 :: f (Set a)
r1r2 = Set a -> Set a -> f (Set a)
go Set a
r1 Set a
r2
{-# INLINE mergeA #-}
showTree :: Show a => Set a -> String
showTree :: forall a. Show a => Set a -> [Char]
showTree Set a
s
= Bool -> Bool -> Set a -> [Char]
forall a. Show a => Bool -> Bool -> Set a -> [Char]
showTreeWith Bool
True Bool
False Set a
s
showTreeWith :: Show a => Bool -> Bool -> Set a -> String
showTreeWith :: forall a. Show a => Bool -> Bool -> Set a -> [Char]
showTreeWith Bool
hang Bool
wide Set a
t
| Bool
hang = (Bool -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> Set a -> ShowS
showsTreeHang Bool
wide [] Set a
t) [Char]
""
| Bool
otherwise = (Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
showsTree Bool
wide [] [] Set a
t) [Char]
""
showsTree :: Show a => Bool -> [String] -> [String] -> Set a -> ShowS
showsTree :: forall a. Show a => Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
showsTree Bool
wide [[Char]]
lbars [[Char]]
rbars Set a
t
= case Set a
t of
Set a
Tip -> [[Char]] -> ShowS
showsBars [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"|\n"
Bin Int
_ a
x Set a
Tip Set a
Tip
-> [[Char]] -> ShowS
showsBars [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"\n"
Bin Int
_ a
x Set a
l Set a
r
-> Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
showsTree Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
rbars) ([[Char]] -> [[Char]]
withEmpty [[Char]]
rbars) Set a
r ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> ShowS
showWide Bool
wide [[Char]]
rbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[[Char]] -> ShowS
showsBars [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> ShowS
showWide Bool
wide [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> [[Char]] -> Set a -> ShowS
showsTree Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
lbars) ([[Char]] -> [[Char]]
withBar [[Char]]
lbars) Set a
l
showsTreeHang :: Show a => Bool -> [String] -> Set a -> ShowS
showsTreeHang :: forall a. Show a => Bool -> [[Char]] -> Set a -> ShowS
showsTreeHang Bool
wide [[Char]]
bars Set a
t
= case Set a
t of
Set a
Tip -> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"|\n"
Bin Int
_ a
x Set a
Tip Set a
Tip
-> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"\n"
Bin Int
_ a
x Set a
l Set a
r
-> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> ShowS
showWide Bool
wide [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> Set a -> ShowS
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
bars) Set a
l ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> ShowS
showWide Bool
wide [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Bool -> [[Char]] -> Set a -> ShowS
forall a. Show a => Bool -> [[Char]] -> Set a -> ShowS
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
bars) Set a
r
showWide :: Bool -> [String] -> String -> String
showWide :: Bool -> [[Char]] -> ShowS
showWide Bool
wide [[Char]]
bars
| Bool
wide = [Char] -> ShowS
showString ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse [[Char]]
bars)) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
"|\n"
| Bool
otherwise = ShowS
forall a. a -> a
id
showsBars :: [String] -> ShowS
showsBars :: [[Char]] -> ShowS
showsBars [[Char]]
bars
= case [[Char]]
bars of
[] -> ShowS
forall a. a -> a
id
[Char]
_ : [[Char]]
tl -> [Char] -> ShowS
showString ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse [[Char]]
tl)) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
node
node :: String
node :: [Char]
node = [Char]
"+--"
withBar, withEmpty :: [String] -> [String]
withBar :: [[Char]] -> [[Char]]
withBar [[Char]]
bars = [Char]
"| "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars
withEmpty :: [[Char]] -> [[Char]]
withEmpty [[Char]]
bars = [Char]
" "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars
valid :: Ord a => Set a -> Bool
valid :: forall a. Ord a => Set a -> Bool
valid Set a
t
= Set a -> Bool
forall a. Set a -> Bool
balanced Set a
t Bool -> Bool -> Bool
&& Set a -> Bool
forall a. Ord a => Set a -> Bool
ordered Set a
t Bool -> Bool -> Bool
&& Set a -> Bool
forall a. Set a -> Bool
validsize Set a
t
ordered :: Ord a => Set a -> Bool
ordered :: forall a. Ord a => Set a -> Bool
ordered Set a
t
= (a -> Bool) -> (a -> Bool) -> Set a -> Bool
forall {a}. Ord a => (a -> Bool) -> (a -> Bool) -> Set a -> Bool
bounded (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
True) (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
True) Set a
t
where
bounded :: (a -> Bool) -> (a -> Bool) -> Set a -> Bool
bounded a -> Bool
lo a -> Bool
hi Set a
t'
= case Set a
t' of
Set a
Tip -> Bool
True
Bin Int
_ a
x Set a
l Set a
r -> (a -> Bool
lo a
x) Bool -> Bool -> Bool
&& (a -> Bool
hi a
x) Bool -> Bool -> Bool
&& (a -> Bool) -> (a -> Bool) -> Set a -> Bool
bounded a -> Bool
lo (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
x) Set a
l Bool -> Bool -> Bool
&& (a -> Bool) -> (a -> Bool) -> Set a -> Bool
bounded (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
x) a -> Bool
hi Set a
r
balanced :: Set a -> Bool
balanced :: forall a. Set a -> Bool
balanced Set a
t
= case Set a
t of
Set a
Tip -> Bool
True
Bin Int
_ a
_ Set a
l Set a
r -> (Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set a -> Int
forall a. Set a -> Int
size Set a
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 Bool -> Bool -> Bool
|| (Set a -> Int
forall a. Set a -> Int
size Set a
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Set a -> Int
forall a. Set a -> Int
size Set a
r Bool -> Bool -> Bool
&& Set a -> Int
forall a. Set a -> Int
size Set a
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Set a -> Int
forall a. Set a -> Int
size Set a
l)) Bool -> Bool -> Bool
&&
Set a -> Bool
forall a. Set a -> Bool
balanced Set a
l Bool -> Bool -> Bool
&& Set a -> Bool
forall a. Set a -> Bool
balanced Set a
r
validsize :: Set a -> Bool
validsize :: forall a. Set a -> Bool
validsize Set a
t
= (Set a -> Maybe Int
forall {a}. Set a -> Maybe Int
realsize Set a
t Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just (Set a -> Int
forall a. Set a -> Int
size Set a
t))
where
realsize :: Set a -> Maybe Int
realsize Set a
t'
= case Set a
t' of
Set a
Tip -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
0
Bin Int
sz a
_ Set a
l Set a
r -> case (Set a -> Maybe Int
realsize Set a
l,Set a -> Maybe Int
realsize Set a
r) of
(Just Int
n,Just Int
m) | Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
sz
(Maybe Int, Maybe Int)
_ -> Maybe Int
forall a. Maybe a
Nothing