#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
#endif
module Data.Dependent.Map
( DMap
, DSum(..), Key(..)
, GCompare(..), GOrdering(..)
, (!), (\\)
, null
, size
, member
, notMember
, lookup
, findWithDefault
, empty
, singleton
, insert
, insertWith
, insertWith'
, insertWithKey
, insertWithKey'
, insertLookupWithKey
, insertLookupWithKey'
, delete
, adjust
, adjustWithKey
, update
, updateWithKey
, updateLookupWithKey
, alter
, union
, unionWithKey
, unions
, unionsWithKey
, difference
, differenceWithKey
, intersection
, intersectionWithKey
, mapWithKey
, mapAccumLWithKey
, mapAccumRWithKey
, mapKeysWith
, mapKeysMonotonic
, foldWithKey
, foldrWithKey
, foldlWithKey
, keys
, assocs
, toList
, fromList
, fromListWithKey
, toAscList
, toDescList
, fromAscList
, fromAscListWithKey
, fromDistinctAscList
, filter
, filterWithKey
, partitionWithKey
, mapMaybeWithKey
, mapEitherWithKey
, split
, splitLookup
, isSubmapOf, isSubmapOfBy
, isProperSubmapOf, isProperSubmapOfBy
, lookupIndex
, findIndex
, elemAt
, updateAt
, deleteAt
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, updateMinWithKey
, updateMaxWithKey
, minViewWithKey
, maxViewWithKey
, showTree
, showTreeWith
, valid
) where
import Prelude hiding (null, lookup)
import Data.Dependent.Map.Internal
#if !MIN_VERSION_base(4,7,0)
import Data.Dependent.Map.Typeable ()
#endif
import Data.Dependent.Sum
import Data.GADT.Compare
import Data.Maybe (isJust)
import Data.Monoid
import Text.Read
instance (GCompare k) => Monoid (DMap k) where
mempty = empty
mappend = union
mconcat = unions
infixl 9 !,\\
(!) :: GCompare k => DMap k -> k v -> v
(!) m k = find k m
(\\) :: GCompare k => DMap k -> DMap k -> DMap k
m1 \\ m2 = difference m1 m2
member :: GCompare k => k a -> DMap k -> Bool
member k = isJust . lookup k
notMember :: GCompare k => k v -> DMap k -> Bool
notMember k m = not (member k m)
find :: GCompare k => k v -> DMap k -> v
find k m = case lookup k m of
Nothing -> error "DMap.find: element not in the map"
Just v -> v
findWithDefault :: GCompare k => v -> k v -> DMap k -> v
findWithDefault def k m = case lookup k m of
Nothing -> def
Just v -> v
insert :: forall k v. GCompare k => k v -> v -> DMap k -> DMap k
insert kx x = kx `seq` go
where
go :: DMap k -> DMap k
go Tip = singleton kx x
go (Bin sz ky y l r) = case gcompare kx ky of
GLT -> balance ky y (go l) r
GGT -> balance ky y l (go r)
GEQ -> Bin sz kx x l r
insertWith :: GCompare k => (v -> v -> v) -> k v -> v -> DMap k -> DMap k
insertWith f = insertWithKey (\_ x' y' -> f x' y')
insertWith' :: GCompare k => (v -> v -> v) -> k v -> v -> DMap k -> DMap k
insertWith' f = insertWithKey' (\_ x' y' -> f x' y')
insertWithKey :: forall k v. GCompare k => (k v -> v -> v -> v) -> k v -> v -> DMap k -> DMap k
insertWithKey f kx x = kx `seq` go
where
go :: DMap k -> DMap k
go Tip = singleton kx x
go (Bin sy ky y l r) =
case gcompare kx ky of
GLT -> balance ky y (go l) r
GGT -> balance ky y l (go r)
GEQ -> Bin sy kx (f kx x y) l r
insertWithKey' :: forall k v. GCompare k => (k v -> v -> v -> v) -> k v -> v -> DMap k -> DMap k
insertWithKey' f kx x = kx `seq` go
where
go :: DMap k -> DMap k
go Tip = singleton kx $! x
go (Bin sy ky y l r) =
case gcompare kx ky of
GLT -> balance ky y (go l) r
GGT -> balance ky y l (go r)
GEQ -> let x' = f kx x y in seq x' (Bin sy kx x' l r)
insertLookupWithKey :: forall k v. GCompare k => (k v -> v -> v -> v) -> k v -> v -> DMap k
-> (Maybe v, DMap k)
insertLookupWithKey f kx x = kx `seq` go
where
go :: DMap k -> (Maybe v, DMap k)
go Tip = (Nothing, singleton kx x)
go (Bin sy ky y l r) =
case gcompare kx ky of
GLT -> let (found, l') = go l
in (found, balance ky y l' r)
GGT -> let (found, r') = go r
in (found, balance ky y l r')
GEQ -> (Just y, Bin sy kx (f kx x y) l r)
insertLookupWithKey' :: forall k v. GCompare k => (k v -> v -> v -> v) -> k v -> v -> DMap k
-> (Maybe v, DMap k)
insertLookupWithKey' f kx x = kx `seq` go
where
go :: DMap k -> (Maybe v, DMap k)
go Tip = x `seq` (Nothing, singleton kx x)
go (Bin sy ky y l r) =
case gcompare kx ky of
GLT -> let (found, l') = go l
in (found, balance ky y l' r)
GGT -> let (found, r') = go r
in (found, balance ky y l r')
GEQ -> let x' = f kx x y in x' `seq` (Just y, Bin sy kx x' l r)
delete :: forall k v. GCompare k => k v -> DMap k -> DMap k
delete k = k `seq` go
where
go :: DMap k -> DMap k
go Tip = Tip
go (Bin _ kx x l r) =
case gcompare k kx of
GLT -> balance kx x (go l) r
GGT -> balance kx x l (go r)
GEQ -> glue l r
adjust :: GCompare k => (v -> v) -> k v -> DMap k -> DMap k
adjust f = adjustWithKey (\_ x -> f x)
adjustWithKey :: GCompare k => (k v -> v -> v) -> k v -> DMap k -> DMap k
adjustWithKey f = updateWithKey (\k' x' -> Just (f k' x'))
update :: GCompare k => (v -> Maybe v) -> k v -> DMap k -> DMap k
update f = updateWithKey (\_ x -> f x)
updateWithKey :: forall k v. GCompare k => (k v -> v -> Maybe v) -> k v -> DMap k -> DMap k
updateWithKey f k = k `seq` go
where
go :: DMap k -> DMap k
go Tip = Tip
go (Bin sx kx x l r) =
case gcompare k kx of
GLT -> balance kx x (go l) r
GGT -> balance kx x l (go r)
GEQ -> case f kx x of
Just x' -> Bin sx kx x' l r
Nothing -> glue l r
updateLookupWithKey :: forall k v. GCompare k => (k v -> v -> Maybe v) -> k v -> DMap k -> (Maybe v,DMap k)
updateLookupWithKey f k = k `seq` go
where
go :: DMap k -> (Maybe v, DMap k)
go Tip = (Nothing,Tip)
go (Bin sx kx x l r) =
case gcompare k kx of
GLT -> let (found,l') = go l in (found,balance kx x l' r)
GGT -> let (found,r') = go r in (found,balance kx x l r')
GEQ -> case f kx x of
Just x' -> (Just x',Bin sx kx x' l r)
Nothing -> (Just x,glue l r)
alter :: forall k v. GCompare k => (Maybe v -> Maybe v) -> k v -> DMap k -> DMap k
alter f k = k `seq` go
where
go :: DMap k -> DMap k
go Tip = case f Nothing of
Nothing -> Tip
Just x -> singleton k x
go (Bin sx kx x l r) = case gcompare k kx of
GLT -> balance kx x (go l) r
GGT -> balance kx x l (go r)
GEQ -> case f (Just x) of
Just x' -> Bin sx kx x' l r
Nothing -> glue l r
findIndex :: GCompare k => k v -> DMap k -> Int
findIndex k t
= case lookupIndex k t of
Nothing -> error "Map.findIndex: element is not in the map"
Just idx -> idx
lookupIndex :: forall k v. GCompare k => k v -> DMap k -> Maybe Int
lookupIndex k = k `seq` go 0
where
go :: Int -> DMap k -> Maybe Int
go !idx Tip = idx `seq` Nothing
go !idx (Bin _ kx _ l r)
= case gcompare k kx of
GLT -> go idx l
GGT -> go (idx + size l + 1) r
GEQ -> Just (idx + size l)
elemAt :: Int -> DMap k -> DSum k
elemAt _ Tip = error "Map.elemAt: index out of range"
elemAt i (Bin _ kx x l r)
= case compare i sizeL of
LT -> elemAt i l
GT -> elemAt (isizeL1) r
EQ -> kx :=> x
where
sizeL = size l
updateAt :: (forall v. k v -> v -> Maybe v) -> Int -> DMap k -> DMap k
updateAt f i0 t = i0 `seq` go i0 t
where
go _ Tip = error "Map.updateAt: index out of range"
go i (Bin sx kx x l r) = case compare i sizeL of
LT -> balance kx x (go i l) r
GT -> balance kx x l (go (isizeL1) r)
EQ -> case f kx x of
Just x' -> Bin sx kx x' l r
Nothing -> glue l r
where
sizeL = size l
deleteAt :: Int -> DMap k -> DMap k
deleteAt i m
= updateAt (\_ _ -> Nothing) i m
findMin :: DMap k -> DSum k
findMin (Bin _ kx x Tip _) = kx :=> x
findMin (Bin _ _ _ l _) = findMin l
findMin Tip = error "Map.findMin: empty map has no minimal element"
findMax :: DMap k -> DSum k
findMax (Bin _ kx x _ Tip) = kx :=> x
findMax (Bin _ _ _ _ r) = findMax r
findMax Tip = error "Map.findMax: empty map has no maximal element"
deleteMin :: DMap k -> DMap k
deleteMin (Bin _ _ _ Tip r) = r
deleteMin (Bin _ kx x l r) = balance kx x (deleteMin l) r
deleteMin Tip = Tip
deleteMax :: DMap k -> DMap k
deleteMax (Bin _ _ _ l Tip) = l
deleteMax (Bin _ kx x l r) = balance kx x l (deleteMax r)
deleteMax Tip = Tip
updateMinWithKey :: (forall v. k v -> v -> Maybe v) -> DMap k -> DMap k
updateMinWithKey f = go
where
go (Bin sx kx x Tip r) = case f kx x of
Nothing -> r
Just x' -> Bin sx kx x' Tip r
go (Bin _ kx x l r) = balance kx x (go l) r
go Tip = Tip
updateMaxWithKey :: (forall v. k v -> v -> Maybe v) -> DMap k -> DMap k
updateMaxWithKey f = go
where
go (Bin sx kx x l Tip) = case f kx x of
Nothing -> l
Just x' -> Bin sx kx x' l Tip
go (Bin _ kx x l r) = balance kx x l (go r)
go Tip = Tip
minViewWithKey :: DMap k -> Maybe (DSum k, DMap k)
minViewWithKey Tip = Nothing
minViewWithKey x = Just (deleteFindMin x)
maxViewWithKey :: DMap k -> Maybe (DSum k, DMap k)
maxViewWithKey Tip = Nothing
maxViewWithKey x = Just (deleteFindMax x)
unions :: GCompare k => [DMap k] -> DMap k
unions ts
= foldlStrict union empty ts
unionsWithKey :: GCompare k => (forall v. k v -> v -> v -> v) -> [DMap k] -> DMap k
unionsWithKey f ts
= foldlStrict (unionWithKey f) empty ts
union :: GCompare k => DMap k -> DMap k -> DMap k
union Tip t2 = t2
union t1 Tip = t1
union t1 t2 = hedgeUnionL (const LT) (const GT) t1 t2
hedgeUnionL :: GCompare k
=> (Key k -> Ordering) -> (Key k -> Ordering) -> DMap k -> DMap k
-> DMap k
hedgeUnionL _ _ t1 Tip
= t1
hedgeUnionL cmplo cmphi Tip (Bin _ kx x l r)
= join kx x (filterGt cmplo l) (filterLt cmphi r)
hedgeUnionL cmplo cmphi (Bin _ kx x l r) t2
= join kx x (hedgeUnionL cmplo cmpkx l (trim cmplo cmpkx t2))
(hedgeUnionL cmpkx cmphi r (trim cmpkx cmphi t2))
where
cmpkx k = compare (Key kx) k
unionWithKey :: GCompare k => (forall v. k v -> v -> v -> v) -> DMap k -> DMap k -> DMap k
unionWithKey _ Tip t2 = t2
unionWithKey _ t1 Tip = t1
unionWithKey f t1 t2 = hedgeUnionWithKey f (const LT) (const GT) t1 t2
hedgeUnionWithKey :: forall k. GCompare k
=> (forall v. k v -> v -> v -> v)
-> (Key k -> Ordering) -> (Key k -> Ordering)
-> DMap k -> DMap k
-> DMap k
hedgeUnionWithKey _ _ _ t1 Tip
= t1
hedgeUnionWithKey _ cmplo cmphi Tip (Bin _ kx x l r)
= join kx x (filterGt cmplo l) (filterLt cmphi r)
hedgeUnionWithKey f cmplo cmphi (Bin _ (kx :: k tx) x l r) t2
= join kx newx (hedgeUnionWithKey f cmplo cmpkx l lt)
(hedgeUnionWithKey f cmpkx cmphi r gt)
where
cmpkx k = compare (Key kx) k
lt = trim cmplo cmpkx t2
(found,gt) = trimLookupLo (Key kx) cmphi t2
newx :: tx
newx = case found of
Nothing -> x
Just (ky :=> y) -> case geq kx ky of
Just Refl -> f kx x y
Nothing -> error "DMap.union: inconsistent GEq instance"
difference :: GCompare k => DMap k -> DMap k -> DMap k
difference Tip _ = Tip
difference t1 Tip = t1
difference t1 t2 = hedgeDiff (const LT) (const GT) t1 t2
hedgeDiff :: GCompare k
=> (Key k -> Ordering) -> (Key k -> Ordering) -> DMap k -> DMap k
-> DMap k
hedgeDiff _ _ Tip _
= Tip
hedgeDiff cmplo cmphi (Bin _ kx x l r) Tip
= join kx x (filterGt cmplo l) (filterLt cmphi r)
hedgeDiff cmplo cmphi t (Bin _ kx _ l r)
= merge (hedgeDiff cmplo cmpkx (trim cmplo cmpkx t) l)
(hedgeDiff cmpkx cmphi (trim cmpkx cmphi t) r)
where
cmpkx k = compare (Key kx) k
differenceWithKey :: GCompare k => (forall v. k v -> v -> v -> Maybe v) -> DMap k -> DMap k -> DMap k
differenceWithKey _ Tip _ = Tip
differenceWithKey _ t1 Tip = t1
differenceWithKey f t1 t2 = hedgeDiffWithKey f (const LT) (const GT) t1 t2
hedgeDiffWithKey :: GCompare k
=> (forall v. k v -> v -> v -> Maybe v)
-> (Key k -> Ordering) -> (Key k -> Ordering)
-> DMap k -> DMap k
-> DMap k
hedgeDiffWithKey _ _ _ Tip _
= Tip
hedgeDiffWithKey _ cmplo cmphi (Bin _ kx x l r) Tip
= join kx x (filterGt cmplo l) (filterLt cmphi r)
hedgeDiffWithKey f cmplo cmphi t (Bin _ kx x l r)
= case found of
Nothing -> merge tl tr
Just (ky :=> y) ->
case geq kx ky of
Nothing -> error "DMap.difference: inconsistent GEq instance"
Just Refl ->
case f ky y x of
Nothing -> merge tl tr
Just z -> join ky z tl tr
where
cmpkx k = compare (Key kx) k
lt = trim cmplo cmpkx t
(found,gt) = trimLookupLo (Key kx) cmphi t
tl = hedgeDiffWithKey f cmplo cmpkx lt l
tr = hedgeDiffWithKey f cmpkx cmphi gt r
intersection :: GCompare k => DMap k -> DMap k -> DMap k
intersection m1 m2
= intersectionWithKey (\_ x _ -> x) m1 m2
intersectionWithKey :: GCompare k => (forall v. k v -> v -> v -> v) -> DMap k -> DMap k -> DMap k
intersectionWithKey _ Tip _ = Tip
intersectionWithKey _ _ Tip = Tip
intersectionWithKey f t1@(Bin s1 k1 x1 l1 r1) t2@(Bin s2 k2 x2 l2 r2) =
if s1 >= s2 then
let (lt,found,gt) = splitLookupWithKey k2 t1
tl = intersectionWithKey f lt l2
tr = intersectionWithKey f gt r2
in case found of
Just (k,x) -> join k (f k x x2) tl tr
Nothing -> merge tl tr
else let (lt,found,gt) = splitLookup k1 t2
tl = intersectionWithKey f l1 lt
tr = intersectionWithKey f r1 gt
in case found of
Just x -> join k1 (f k1 x1 x) tl tr
Nothing -> merge tl tr
isSubmapOf :: (GCompare k,EqTag k) => DMap k -> DMap k -> Bool
isSubmapOf m1 m2 = isSubmapOfBy eqTagged m1 m2
isSubmapOfBy :: GCompare k => (forall v. k v -> k v -> v -> v -> Bool) -> DMap k -> DMap k -> Bool
isSubmapOfBy f t1 t2
= (size t1 <= size t2) && (submap' f t1 t2)
submap' :: GCompare k => (forall v. k v -> k v -> v -> v -> Bool) -> DMap k -> DMap k -> Bool
submap' _ Tip _ = True
submap' _ _ Tip = False
submap' f (Bin _ kx x l r) t
= case found of
Nothing -> False
Just (ky, y) -> f kx ky x y && submap' f l lt && submap' f r gt
where
(lt,found,gt) = splitLookupWithKey kx t
isProperSubmapOf :: (GCompare k, EqTag k) => DMap k -> DMap k -> Bool
isProperSubmapOf m1 m2
= isProperSubmapOfBy eqTagged m1 m2
isProperSubmapOfBy :: GCompare k => (forall v. k v -> k v -> v -> v -> Bool) -> DMap k -> DMap k -> Bool
isProperSubmapOfBy f t1 t2
= (size t1 < size t2) && (submap' f t1 t2)
filterWithKey :: GCompare k => (forall v. k v -> v -> Bool) -> DMap k -> DMap k
filterWithKey p = go
where
go Tip = Tip
go (Bin _ kx x l r)
| p kx x = join kx x (go l) (go r)
| otherwise = merge (go l) (go r)
partitionWithKey :: GCompare k => (forall v. k v -> v -> Bool) -> DMap k -> (DMap k,DMap k)
partitionWithKey _ Tip = (Tip,Tip)
partitionWithKey p (Bin _ kx x l r)
| p kx x = (join kx x l1 r1,merge l2 r2)
| otherwise = (merge l1 r1,join kx x l2 r2)
where
(l1,l2) = partitionWithKey p l
(r1,r2) = partitionWithKey p r
mapMaybeWithKey :: GCompare k => (forall v. k v -> v -> Maybe v) -> DMap k -> DMap k
mapMaybeWithKey f = go
where
go Tip = Tip
go (Bin _ kx x l r) = case f kx x of
Just y -> join kx y (go l) (go r)
Nothing -> merge (go l) (go r)
mapEitherWithKey :: GCompare k =>
(forall v. k v -> v -> Either v v) -> DMap k -> (DMap k, DMap k)
mapEitherWithKey _ Tip = (Tip, Tip)
mapEitherWithKey f (Bin _ kx x l r) = case f kx x of
Left y -> (join kx y l1 r1, merge l2 r2)
Right z -> (merge l1 r1, join kx z l2 r2)
where
(l1,l2) = mapEitherWithKey f l
(r1,r2) = mapEitherWithKey f r
mapWithKey :: (forall v. k v -> v -> v) -> DMap k -> DMap k
mapWithKey f = go
where
go Tip = Tip
go (Bin sx kx x l r) = Bin sx kx (f kx x) (go l) (go r)
mapAccumLWithKey :: (forall v. a -> k v -> v -> (a,v)) -> a -> DMap k -> (a,DMap k)
mapAccumLWithKey f = go
where
go a Tip = (a,Tip)
go a (Bin sx kx x l r) =
let (a1,l') = go a l
(a2,x') = f a1 kx x
(a3,r') = go a2 r
in (a3,Bin sx kx x' l' r')
mapAccumRWithKey :: (forall v. a -> k v -> v -> (a,v)) -> a -> DMap k -> (a, DMap k)
mapAccumRWithKey f = go
where
go a Tip = (a,Tip)
go a (Bin sx kx x l r) =
let (a1,r') = go a r
(a2,x') = f a1 kx x
(a3,l') = go a2 l
in (a3,Bin sx kx x' l' r')
mapKeysWith :: GCompare k2 => (forall v. k2 v -> v -> v -> v) -> (forall v. k1 v -> k2 v) -> DMap k1 -> DMap k2
mapKeysWith c f = fromListWithKey c . map fFirst . toList
where fFirst (x :=> y) = (f x :=> y)
mapKeysMonotonic :: (forall v. k1 v -> k2 v) -> DMap k1 -> DMap k2
mapKeysMonotonic _ Tip = Tip
mapKeysMonotonic f (Bin sz k x l r) =
Bin sz (f k) x (mapKeysMonotonic f l) (mapKeysMonotonic f r)
foldWithKey :: (forall v. k v -> v -> b -> b) -> b -> DMap k -> b
foldWithKey = foldrWithKey
foldrWithKey :: (forall v. k v -> v -> b -> b) -> b -> DMap k -> b
foldrWithKey f = go
where
go z Tip = z
go z (Bin _ kx x l r) = go (f kx x (go z r)) l
foldlWithKey :: (forall v. b -> k v -> v -> b) -> b -> DMap k -> b
foldlWithKey f = go
where
go z Tip = z
go z (Bin _ kx x l r) = go (f (go z l) kx x) r
keys :: DMap k -> [Key k]
keys m
= [Key k | (k :=> _) <- assocs m]
assocs :: DMap k -> [DSum k]
assocs m
= toList m
fromList :: GCompare k => [DSum k] -> DMap k
fromList xs
= foldlStrict ins empty xs
where
ins :: GCompare k => DMap k -> DSum k -> DMap k
ins t (k :=> x) = insert k x t
fromListWithKey :: GCompare k => (forall v. k v -> v -> v -> v) -> [DSum k] -> DMap k
fromListWithKey f xs
= foldlStrict (ins f) empty xs
where
ins :: GCompare k => (forall v. k v -> v -> v -> v) -> DMap k -> DSum k -> DMap k
ins f t (k :=> x) = insertWithKey f k x t
toList :: DMap k -> [DSum k]
toList t = toAscList t
toAscList :: DMap k -> [DSum k]
toAscList t = foldrWithKey (\k x xs -> (k :=> x):xs) [] t
toDescList :: DMap k -> [DSum k]
toDescList t = foldlWithKey (\xs k x -> (k :=> x):xs) [] t
fromAscList :: GEq k => [DSum k] -> DMap k
fromAscList xs
= fromAscListWithKey (\_ x _ -> x) xs
fromAscListWithKey :: GEq k => (forall v. k v -> v -> v -> v) -> [DSum k] -> DMap k
fromAscListWithKey f xs
= fromDistinctAscList (combineEq f xs)
where
combineEq _ xs'
= case xs' of
[] -> []
[x] -> [x]
(x:xx) -> combineEq' f x xx
combineEq' :: GEq k => (forall v. k v -> v -> v -> v) -> DSum k -> [DSum k] -> [DSum k]
combineEq' f z [] = [z]
combineEq' f z@(kz :=> zz) (x@(kx :=> xx):xs') =
case geq kx kz of
Just Refl -> let yy = f kx xx zz in combineEq' f (kx :=> yy) xs'
Nothing -> z : combineEq' f x xs'
fromDistinctAscList :: [DSum k] -> DMap k
fromDistinctAscList xs
= build const (length xs) xs
where
build :: (DMap k -> [DSum k] -> b) -> Int -> [DSum k] -> b
build c 0 xs' = c Tip xs'
build c 5 xs' = case xs' of
((k1:=>x1):(k2:=>x2):(k3:=>x3):(k4:=>x4):(k5:=>x5):xx)
-> c (bin k4 x4 (bin k2 x2 (singleton k1 x1) (singleton k3 x3)) (singleton k5 x5)) xx
_ -> error "fromDistinctAscList build"
build c n xs' = seq nr $ build (buildR nr c) nl xs'
where
nl = n `div` 2
nr = n nl 1
buildR :: Int -> (DMap k -> [DSum k] -> b) -> DMap k -> [DSum k] -> b
buildR n c l ((k:=>x):ys) = build (buildB l k x c) n ys
buildR _ _ _ [] = error "fromDistinctAscList buildR []"
buildB :: DMap k -> k v -> v -> (DMap k -> a -> b) -> DMap k -> a -> b
buildB l k x c r zs = c (bin k x l r) zs
split :: forall k v. GCompare k => k v -> DMap k -> (DMap k,DMap k)
split k = go
where
go :: DMap k -> (DMap k,DMap k)
go Tip = (Tip, Tip)
go (Bin _ kx x l r) = case gcompare k kx of
GLT -> let (lt,gt) = go l in (lt,join kx x gt r)
GGT -> let (lt,gt) = go r in (join kx x l lt,gt)
GEQ -> (l,r)
splitLookup :: forall k v. GCompare k => k v -> DMap k -> (DMap k,Maybe v,DMap k)
splitLookup k = go
where
go :: DMap k -> (DMap k,Maybe v,DMap k)
go Tip = (Tip,Nothing,Tip)
go (Bin _ kx x l r) = case gcompare k kx of
GLT -> let (lt,z,gt) = go l in (lt,z,join kx x gt r)
GGT -> let (lt,z,gt) = go r in (join kx x l lt,z,gt)
GEQ -> (l,Just x,r)
splitLookupWithKey :: forall k v. GCompare k => k v -> DMap k -> (DMap k,Maybe (k v, v),DMap k)
splitLookupWithKey k = go
where
go :: DMap k -> (DMap k,Maybe (k v, v),DMap k)
go Tip = (Tip,Nothing,Tip)
go (Bin _ kx x l r) = case gcompare k kx of
GLT -> let (lt,z,gt) = go l in (lt,z,join kx x gt r)
GGT -> let (lt,z,gt) = go r in (join kx x l lt,z,gt)
GEQ -> (l,Just (kx, x),r)
instance EqTag k => Eq (DMap k) where
t1 == t2 = (size t1 == size t2) && (toAscList t1 == toAscList t2)
instance OrdTag k => Ord (DMap k) where
compare m1 m2 = compare (toAscList m1) (toAscList m2)
instance (GCompare f, ReadTag f) => Read (DMap f) where
readPrec = parens $ prec 10 $ do
Ident "fromList" <- lexP
xs <- readPrec
return (fromList xs)
readListPrec = readListPrecDefault
instance ShowTag k => Show (DMap k) where
showsPrec p m = showParen (p>10)
( showString "fromList "
. showsPrec 11 (toList m)
)
showTree :: ShowTag k => DMap k -> String
showTree m
= showTreeWith showElem True False m
where
showElem :: ShowTag k => k v -> v -> String
showElem k x = show (k :=> x)
showTreeWith :: (forall v. k v -> v -> String) -> Bool -> Bool -> DMap k -> String
showTreeWith showelem hang wide t
| hang = (showsTreeHang showelem wide [] t) ""
| otherwise = (showsTree showelem wide [] [] t) ""
showsTree :: (forall v. k v -> v -> String) -> Bool -> [String] -> [String] -> DMap k -> ShowS
showsTree showelem wide lbars rbars t
= case t of
Tip -> showsBars lbars . showString "|\n"
Bin _ kx x Tip Tip
-> showsBars lbars . showString (showelem kx x) . showString "\n"
Bin _ kx x l r
-> showsTree showelem wide (withBar rbars) (withEmpty rbars) r .
showWide wide rbars .
showsBars lbars . showString (showelem kx x) . showString "\n" .
showWide wide lbars .
showsTree showelem wide (withEmpty lbars) (withBar lbars) l
showsTreeHang :: (forall v. k v -> v -> String) -> Bool -> [String] -> DMap k -> ShowS
showsTreeHang showelem wide bars t
= case t of
Tip -> showsBars bars . showString "|\n"
Bin _ kx x Tip Tip
-> showsBars bars . showString (showelem kx x) . showString "\n"
Bin _ kx x l r
-> showsBars bars . showString (showelem kx x) . showString "\n" .
showWide wide bars .
showsTreeHang showelem wide (withBar bars) l .
showWide wide bars .
showsTreeHang showelem wide (withEmpty bars) r
showWide :: Bool -> [String] -> String -> String
showWide wide bars
| wide = showString (concat (reverse bars)) . showString "|\n"
| otherwise = id
showsBars :: [String] -> ShowS
showsBars bars
= case bars of
[] -> id
_ -> showString (concat (reverse (tail bars))) . showString node
node :: String
node = "+--"
withBar, withEmpty :: [String] -> [String]
withBar bars = "| ":bars
withEmpty bars = " ":bars
valid :: GCompare k => DMap k -> Bool
valid t
= balanced t && ordered t && validsize t
ordered :: GCompare k => DMap k -> Bool
ordered t
= bounded (const True) (const True) t
where
bounded :: GCompare k => (Key k -> Bool) -> (Key k -> Bool) -> DMap k -> Bool
bounded lo hi t'
= case t' of
Tip -> True
Bin _ kx _ l r -> (lo (Key kx)) && (hi (Key kx)) && bounded lo (< Key kx) l && bounded (> Key kx) hi r
balanced :: DMap k -> Bool
balanced t
= case t of
Tip -> True
Bin _ _ _ l r -> (size l + size r <= 1 || (size l <= delta*size r && size r <= delta*size l)) &&
balanced l && balanced r
validsize :: DMap k -> Bool
validsize t
= (realsize t == Just (size t))
where
realsize t'
= case t' of
Tip -> Just 0
Bin sz _ _ l r -> case (realsize l,realsize r) of
(Just n,Just m) | n+m+1 == sz -> Just sz
_ -> Nothing
foldlStrict :: (a -> b -> a) -> a -> [b] -> a
foldlStrict f = go
where
go z [] = z
go z (x:xs) = z `seq` go (f z x) xs