{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Dependent.Map
( DMap
, (!), (\\)
, null
, size
, member
, notMember
, lookup
, findWithDefault
, empty
, singleton
, insert
, insertWith
, insertWith'
, insertWithKey
, insertWithKey'
, insertLookupWithKey
, insertLookupWithKey'
, delete
, adjust
, adjustWithKey
, adjustWithKey'
, update
, updateWithKey
, updateLookupWithKey
, alter
, alterF
, union
, unionWithKey
, unions
, unionsWithKey
, difference
, differenceWithKey
, intersection
, intersectionWithKey
, map
, ffor
, mapWithKey
, fforWithKey
, traverseWithKey_
, forWithKey_
, traverseWithKey
, forWithKey
, mapAccumLWithKey
, mapAccumRWithKey
, mapKeysWith
, mapKeysMonotonic
, foldWithKey
, foldrWithKey
, foldlWithKey
, keys
, assocs
, toList
, fromList
, fromListWithKey
, toAscList
, toDescList
, fromAscList
, fromAscListWithKey
, fromDistinctAscList
, filter
, filterWithKey
, partitionWithKey
, mapMaybe
, mapMaybeWithKey
, mapEitherWithKey
, split
, splitLookup
, isSubmapOf, isSubmapOfBy
, isProperSubmapOf, isProperSubmapOfBy
, lookupIndex
, findIndex
, elemAt
, updateAt
, deleteAt
, findMin
, findMax
, lookupMin
, lookupMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, updateMinWithKey
, updateMaxWithKey
, minViewWithKey
, maxViewWithKey
, showTree
, showTreeWith
, valid
) where
import Prelude hiding (null, lookup, map)
import qualified Prelude
import Data.Constraint.Extras (Has', has')
import Data.Dependent.Sum (DSum((:=>)))
import Data.GADT.Compare (GCompare, GEq, GOrdering(..), gcompare, geq)
import Data.GADT.Show (GRead, GShow)
import Data.Maybe (isJust)
import Data.Some (Some, mkSome)
import Data.Typeable ((:~:)(Refl))
import Text.Read (Lexeme(Ident), lexP, parens, prec, readListPrec,
readListPrecDefault, readPrec)
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup, (<>))
#endif
import Data.Dependent.Map.Internal
import Data.Dependent.Map.PtrEquality (ptrEq)
instance (GCompare k) => Monoid (DMap k f) where
mempty :: DMap k f
mempty = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
empty
mappend :: DMap k f -> DMap k f -> DMap k f
mappend = DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
union
mconcat :: [DMap k f] -> DMap k f
mconcat = [DMap k f] -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
[DMap k f] -> DMap k f
unions
instance (GCompare k) => Semigroup (DMap k f) where
<> :: DMap k f -> DMap k f -> DMap k f
(<>) = DMap k f -> DMap k f -> DMap k f
forall a. Monoid a => a -> a -> a
mappend
infixl 9 \\,!
(!) :: GCompare k => DMap k f -> k v -> f v
(!) m :: DMap k f
m k :: k v
k = k v -> DMap k f -> f v
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
k v -> DMap k f -> f v
find k v
k DMap k f
m
(\\) :: GCompare k => DMap k f -> DMap k f -> DMap k f
m1 :: DMap k f
m1 \\ :: DMap k f -> DMap k f -> DMap k f
\\ m2 :: DMap k f
m2 = DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
DMap k f -> DMap k g -> DMap k f
difference DMap k f
m1 DMap k f
m2
member :: GCompare k => k a -> DMap k f -> Bool
member :: k a -> DMap k f -> Bool
member k :: k a
k = Maybe (f a) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (f a) -> Bool)
-> (DMap k f -> Maybe (f a)) -> DMap k f -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k a -> DMap k f -> Maybe (f a)
forall k1 (k2 :: k1 -> *) (f :: k1 -> *) (v :: k1).
GCompare k2 =>
k2 v -> DMap k2 f -> Maybe (f v)
lookup k a
k
notMember :: GCompare k => k v -> DMap k f -> Bool
notMember :: k v -> DMap k f -> Bool
notMember k :: k v
k m :: DMap k f
m = Bool -> Bool
not (k v -> DMap k f -> Bool
forall k (k :: k -> *) (a :: k) (f :: k -> *).
GCompare k =>
k a -> DMap k f -> Bool
member k v
k DMap k f
m)
find :: GCompare k => k v -> DMap k f -> f v
find :: k v -> DMap k f -> f v
find k :: k v
k m :: DMap k f
m = case k v -> DMap k f -> Maybe (f v)
forall k1 (k2 :: k1 -> *) (f :: k1 -> *) (v :: k1).
GCompare k2 =>
k2 v -> DMap k2 f -> Maybe (f v)
lookup k v
k DMap k f
m of
Nothing -> [Char] -> f v
forall a. HasCallStack => [Char] -> a
error "DMap.find: element not in the map"
Just v :: f v
v -> f v
v
findWithDefault :: GCompare k => f v -> k v -> DMap k f -> f v
findWithDefault :: f v -> k v -> DMap k f -> f v
findWithDefault def :: f v
def k :: k v
k m :: DMap k f
m = case k v -> DMap k f -> Maybe (f v)
forall k1 (k2 :: k1 -> *) (f :: k1 -> *) (v :: k1).
GCompare k2 =>
k2 v -> DMap k2 f -> Maybe (f v)
lookup k v
k DMap k f
m of
Nothing -> f v
def
Just v :: f v
v -> f v
v
insert :: forall k f v. GCompare k => k v -> f v -> DMap k f -> DMap k f
insert :: k v -> f v -> DMap k f -> DMap k f
insert kx :: k v
kx x :: f v
x = k v
kx k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx f v
x
go t :: DMap k f
t@(Bin sz :: Int
sz ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> let !l' :: DMap k f
l' = DMap k f -> DMap k f
go DMap k f
l
in if DMap k f
l' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
l
then DMap k f
t
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l' DMap k f
r
GGT -> let !r' :: DMap k f
r' = DMap k f -> DMap k f
go DMap k f
r
in if DMap k f
r' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
r
then DMap k f
t
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l DMap k f
r'
GEQ
| k v
kx k v -> k v -> Bool
forall a. a -> a -> Bool
`ptrEq` k v
k v
ky Bool -> Bool -> Bool
&& f v
x f v -> f v -> Bool
forall a. a -> a -> Bool
`ptrEq` f v
f v
y -> DMap k f
t
| Bool
otherwise -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sz k v
kx f v
x DMap k f
l DMap k f
r
insertR :: forall k f v. GCompare k => k v -> f v -> DMap k f -> DMap k f
insertR :: k v -> f v -> DMap k f -> DMap k f
insertR kx :: k v
kx x :: f v
x = k v
kx k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx f v
x
go t :: DMap k f
t@(Bin sz :: Int
sz ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> let !l' :: DMap k f
l' = DMap k f -> DMap k f
go DMap k f
l
in if DMap k f
l' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
l
then DMap k f
t
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l' DMap k f
r
GGT -> let !r' :: DMap k f
r' = DMap k f -> DMap k f
go DMap k f
r
in if DMap k f
r' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
r
then DMap k f
t
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l DMap k f
r'
GEQ -> DMap k f
t
insertWith :: GCompare k => (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWith :: (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWith f :: f v -> f v -> f v
f = (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
(k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey (\_ x' :: f v
x' y' :: f v
y' -> f v -> f v -> f v
f f v
x' f v
y')
insertWith' :: GCompare k => (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWith' :: (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWith' f :: f v -> f v -> f v
f = (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
(k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey' (\_ x' :: f v
x' y' :: f v
y' -> f v -> f v -> f v
f f v
x' f v
y')
insertWithKey :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey :: (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey f :: k v -> f v -> f v -> f v
f kx :: k v
kx x :: f v
x = k v
kx k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx f v
x
go (Bin sy :: Int
sy ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
GGT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
GEQ -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sy k v
kx (k v -> f v -> f v -> f v
f k v
kx f v
x f v
f v
y) DMap k f
l DMap k f
r
insertWithKey' :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey' :: (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey' f :: k v -> f v -> f v -> f v
f kx :: k v
kx x :: f v
x = k v
kx k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx (f v -> DMap k f) -> f v -> DMap k f
forall a b. (a -> b) -> a -> b
$! f v
x
go (Bin sy :: Int
sy ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
GGT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
GEQ -> let x' :: f v
x' = k v -> f v -> f v -> f v
f k v
kx f v
x f v
f v
y in f v -> DMap k f -> DMap k f
forall a b. a -> b -> b
seq f v
x' (Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sy k v
kx f v
x' DMap k f
l DMap k f
r)
insertLookupWithKey :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f
-> (Maybe (f v), DMap k f)
insertLookupWithKey :: (k v -> f v -> f v -> f v)
-> k v -> f v -> DMap k f -> (Maybe (f v), DMap k f)
insertLookupWithKey f :: k v -> f v -> f v -> f v
f kx :: k v
kx x :: f v
x = k v
kx k v
-> (DMap k f -> (Maybe (f v), DMap k f))
-> DMap k f
-> (Maybe (f v), DMap k f)
forall a b. a -> b -> b
`seq` DMap k f -> (Maybe (f v), DMap k f)
go
where
go :: DMap k f -> (Maybe (f v), DMap k f)
go :: DMap k f -> (Maybe (f v), DMap k f)
go Tip = (Maybe (f v)
forall a. Maybe a
Nothing, k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx f v
x)
go (Bin sy :: Int
sy ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> let (found :: Maybe (f v)
found, l' :: DMap k f
l') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
l
in (Maybe (f v)
found, k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l' DMap k f
r)
GGT -> let (found :: Maybe (f v)
found, r' :: DMap k f
r') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
r
in (Maybe (f v)
found, k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l DMap k f
r')
GEQ -> (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
y, Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sy k v
kx (k v -> f v -> f v -> f v
f k v
kx f v
x f v
f v
y) DMap k f
l DMap k f
r)
insertLookupWithKey' :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f
-> (Maybe (f v), DMap k f)
insertLookupWithKey' :: (k v -> f v -> f v -> f v)
-> k v -> f v -> DMap k f -> (Maybe (f v), DMap k f)
insertLookupWithKey' f :: k v -> f v -> f v -> f v
f kx :: k v
kx x :: f v
x = k v
kx k v
-> (DMap k f -> (Maybe (f v), DMap k f))
-> DMap k f
-> (Maybe (f v), DMap k f)
forall a b. a -> b -> b
`seq` DMap k f -> (Maybe (f v), DMap k f)
go
where
go :: DMap k f -> (Maybe (f v), DMap k f)
go :: DMap k f -> (Maybe (f v), DMap k f)
go Tip = f v
x f v -> (Maybe (f v), DMap k f) -> (Maybe (f v), DMap k f)
forall a b. a -> b -> b
`seq` (Maybe (f v)
forall a. Maybe a
Nothing, k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
kx f v
x)
go (Bin sy :: Int
sy ky :: k v
ky y :: f v
y l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
kx k v
ky of
GLT -> let (found :: Maybe (f v)
found, l' :: DMap k f
l') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
l
in (Maybe (f v)
found, k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l' DMap k f
r)
GGT -> let (found :: Maybe (f v)
found, r' :: DMap k f
r') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
r
in (Maybe (f v)
found, k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
ky f v
y DMap k f
l DMap k f
r')
GEQ -> let x' :: f v
x' = k v -> f v -> f v -> f v
f k v
kx f v
x f v
f v
y in f v
x' f v -> (Maybe (f v), DMap k f) -> (Maybe (f v), DMap k f)
forall a b. a -> b -> b
`seq` (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
y, Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sy k v
kx f v
x' DMap k f
l DMap k f
r)
delete :: forall k f v. GCompare k => k v -> DMap k f -> DMap k f
delete :: k v -> DMap k f -> DMap k f
delete k :: k v
k = k v
k k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
GGT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
GEQ -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k f
l DMap k f
r
adjust :: GCompare k => (f v -> f v) -> k v -> DMap k f -> DMap k f
adjust :: (f v -> f v) -> k v -> DMap k f -> DMap k f
adjust f :: f v -> f v
f = (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
adjustWithKey (\_ x :: f v
x -> f v -> f v
f f v
x)
adjustWithKey :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
adjustWithKey :: (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
adjustWithKey f0 :: k v -> f v -> f v
f0 !k v
k0 = (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f0 k v
k0
where
go :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go :: (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go _f :: k v -> f v -> f v
_f _k :: k v
_k Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go f :: k v -> f v -> f v
f k :: k v
k (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x ((k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f k v
k DMap k f
l) DMap k f
r
GGT -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x DMap k f
l ((k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f k v
k DMap k f
r)
GEQ -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx (k v -> f v -> f v
f k v
k v
kx f v
f v
x) DMap k f
l DMap k f
r
adjustWithKey' :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
adjustWithKey' :: (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
adjustWithKey' f0 :: k v -> f v -> f v
f0 !k v
k0 = (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f0 k v
k0
where
go :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go :: (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go _f :: k v -> f v -> f v
_f _k :: k v
_k Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go f :: k v -> f v -> f v
f k :: k v
k (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x ((k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f k v
k DMap k f
l) DMap k f
r
GGT -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x DMap k f
l ((k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
GCompare k =>
(k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
go k v -> f v -> f v
f k v
k DMap k f
r)
GEQ -> let !x' :: f v
x' = k v -> f v -> f v
f k v
k v
kx f v
f v
x in Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
f v
x' DMap k f
l DMap k f
r
update :: GCompare k => (f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
update :: (f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
update f :: f v -> Maybe (f v)
f = (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
(k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
updateWithKey (\_ x :: f v
x -> f v -> Maybe (f v)
f f v
x)
updateWithKey :: forall k f v. GCompare k => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
updateWithKey :: (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
updateWithKey f :: k v -> f v -> Maybe (f v)
f k :: k v
k = k v
k k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
GGT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
GEQ -> case k v -> f v -> Maybe (f v)
f k v
k v
kx f v
f v
x of
Just x' :: f v
x' -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
f v
x' DMap k f
l DMap k f
r
Nothing -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k f
l DMap k f
r
updateLookupWithKey :: forall k f v. GCompare k => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> (Maybe (f v), DMap k f)
updateLookupWithKey :: (k v -> f v -> Maybe (f v))
-> k v -> DMap k f -> (Maybe (f v), DMap k f)
updateLookupWithKey f :: k v -> f v -> Maybe (f v)
f k :: k v
k = k v
k k v
-> (DMap k f -> (Maybe (f v), DMap k f))
-> DMap k f
-> (Maybe (f v), DMap k f)
forall a b. a -> b -> b
`seq` DMap k f -> (Maybe (f v), DMap k f)
go
where
go :: DMap k f -> (Maybe (f v), DMap k f)
go :: DMap k f -> (Maybe (f v), DMap k f)
go Tip = (Maybe (f v)
forall a. Maybe a
Nothing,DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> let (found :: Maybe (f v)
found,l' :: DMap k f
l') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
l in (Maybe (f v)
found,k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l' DMap k f
r)
GGT -> let (found :: Maybe (f v)
found,r' :: DMap k f
r') = DMap k f -> (Maybe (f v), DMap k f)
go DMap k f
r in (Maybe (f v)
found,k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l DMap k f
r')
GEQ -> case k v -> f v -> Maybe (f v)
f k v
k v
kx f v
f v
x of
Just x' :: f v
x' -> (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
x',Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
f v
x' DMap k f
l DMap k f
r)
Nothing -> (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
x,DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k f
l DMap k f
r)
alter :: forall k f v. GCompare k => (Maybe (f v) -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
alter :: (Maybe (f v) -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
alter f :: Maybe (f v) -> Maybe (f v)
f k :: k v
k = k v
k k v -> (DMap k f -> DMap k f) -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go :: DMap k f -> DMap k f
go Tip = case Maybe (f v) -> Maybe (f v)
f Maybe (f v)
forall a. Maybe a
Nothing of
Nothing -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
Just x :: f v
x -> k v -> f v -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
k f v
x
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
GGT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
GEQ -> case Maybe (f v) -> Maybe (f v)
f (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
x) of
Just x' :: f v
x' -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
f v
x' DMap k f
l DMap k f
r
Nothing -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k f
l DMap k f
r
alterF :: forall k f v g. (GCompare k, Functor f) => k v -> (Maybe (g v) -> f (Maybe (g v))) -> DMap k g -> f (DMap k g)
alterF :: k v -> (Maybe (g v) -> f (Maybe (g v))) -> DMap k g -> f (DMap k g)
alterF k :: k v
k f :: Maybe (g v) -> f (Maybe (g v))
f = DMap k g -> f (DMap k g)
go
where
go :: DMap k g -> f (DMap k g)
go :: DMap k g -> f (DMap k g)
go Tip = DMap k g -> (g v -> DMap k g) -> Maybe (g v) -> DMap k g
forall b a. b -> (a -> b) -> Maybe a -> b
maybe DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip (k v -> g v -> DMap k g
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k v
k) (Maybe (g v) -> DMap k g) -> f (Maybe (g v)) -> f (DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (g v) -> f (Maybe (g v))
f Maybe (g v)
forall a. Maybe a
Nothing
go (Bin sx :: Int
sx kx :: k v
kx x :: g v
x l :: DMap k g
l r :: DMap k g
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> (\l' :: DMap k g
l' -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx g v
x DMap k g
l' DMap k g
r) (DMap k g -> DMap k g) -> f (DMap k g) -> f (DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DMap k g -> f (DMap k g)
go DMap k g
l
GGT -> (\r' :: DMap k g
r' -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx g v
x DMap k g
l DMap k g
r') (DMap k g -> DMap k g) -> f (DMap k g) -> f (DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DMap k g -> f (DMap k g)
go DMap k g
r
GEQ -> DMap k g -> (g v -> DMap k g) -> Maybe (g v) -> DMap k g
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k g
l DMap k g
r) (\x' :: g v
x' -> Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx g v
x' DMap k g
l DMap k g
r) (Maybe (g v) -> DMap k g) -> f (Maybe (g v)) -> f (DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (g v) -> f (Maybe (g v))
f (g v -> Maybe (g v)
forall a. a -> Maybe a
Just g v
x)
findIndex :: GCompare k => k v -> DMap k f -> Int
findIndex :: k v -> DMap k f -> Int
findIndex k :: k v
k t :: DMap k f
t
= case k v -> DMap k f -> Maybe Int
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> Maybe Int
lookupIndex k v
k DMap k f
t of
Nothing -> [Char] -> Int
forall a. HasCallStack => [Char] -> a
error "Map.findIndex: element is not in the map"
Just idx :: Int
idx -> Int
idx
lookupIndex :: forall k f v. GCompare k => k v -> DMap k f -> Maybe Int
lookupIndex :: k v -> DMap k f -> Maybe Int
lookupIndex k :: k v
k = k v
k k v -> (DMap k f -> Maybe Int) -> DMap k f -> Maybe Int
forall a b. a -> b -> b
`seq` Int -> DMap k f -> Maybe Int
go 0
where
go :: Int -> DMap k f -> Maybe Int
go :: Int -> DMap k f -> Maybe Int
go !Int
idx Tip = Int
idx Int -> Maybe Int -> Maybe Int
forall a b. a -> b -> b
`seq` Maybe Int
forall a. Maybe a
Nothing
go !Int
idx (Bin _ kx :: k v
kx _ l :: DMap k f
l r :: DMap k f
r)
= case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> Int -> DMap k f -> Maybe Int
go Int
idx DMap k f
l
GGT -> Int -> DMap k f -> Maybe Int
go (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) DMap k f
r
GEQ -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l)
elemAt :: Int -> DMap k f -> DSum k f
elemAt :: Int -> DMap k f -> DSum k f
elemAt _ Tip = [Char] -> DSum k f
forall a. HasCallStack => [Char] -> a
error "Map.elemAt: index out of range"
elemAt i :: Int
i (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r)
= case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> Int -> DMap k f -> DSum k f
forall k (k :: k -> *) (f :: k -> *). Int -> DMap k f -> DSum k f
elemAt Int
i DMap k f
l
GT -> Int -> DMap k f -> DSum k f
forall k (k :: k -> *) (f :: k -> *). Int -> DMap k f -> DSum k f
elemAt (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) DMap k f
r
EQ -> k v
kx k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x
where
sizeL :: Int
sizeL = DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l
updateAt :: (forall v. k v -> f v -> Maybe (f v)) -> Int -> DMap k f -> DMap k f
updateAt :: (forall (v :: k). k v -> f v -> Maybe (f v))
-> Int -> DMap k f -> DMap k f
updateAt f :: forall (v :: k). k v -> f v -> Maybe (f v)
f i0 :: Int
i0 t :: DMap k f
t = Int
i0 Int -> DMap k f -> DMap k f
forall a b. a -> b -> b
`seq` Int -> DMap k f -> DMap k f
go Int
i0 DMap k f
t
where
go :: Int -> DMap k f -> DMap k f
go _ Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go i :: Int
i (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (Int -> DMap k f -> DMap k f
go Int
i DMap k f
l) DMap k f
r
GT -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (Int -> DMap k f -> DMap k f
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) DMap k f
r)
EQ -> case k v -> f v -> Maybe (f v)
forall (v :: k). k v -> f v -> Maybe (f v)
f k v
kx f v
x of
Just x' :: f v
x' -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x' DMap k f
l DMap k f
r
Nothing -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
glue DMap k f
l DMap k f
r
where
sizeL :: Int
sizeL = DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l
deleteAt :: Int -> DMap k f -> DMap k f
deleteAt :: Int -> DMap k f -> DMap k f
deleteAt i :: Int
i m :: DMap k f
m
= (forall (v :: k). k v -> f v -> Maybe (f v))
-> Int -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> Maybe (f v))
-> Int -> DMap k f -> DMap k f
updateAt (\_ _ -> Maybe (f v)
forall a. Maybe a
Nothing) Int
i DMap k f
m
findMin :: DMap k f -> DSum k f
findMin :: DMap k f -> DSum k f
findMin m :: DMap k f
m = case DMap k f -> Maybe (DSum k f)
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Maybe (DSum k f)
lookupMin DMap k f
m of
Just x :: DSum k f
x -> DSum k f
x
Nothing -> [Char] -> DSum k f
forall a. HasCallStack => [Char] -> a
error "Map.findMin: empty map has no minimal element"
lookupMin :: DMap k f -> Maybe (DSum k f)
lookupMin :: DMap k f -> Maybe (DSum k f)
lookupMin m :: DMap k f
m = case DMap k f
m of
Tip -> Maybe (DSum k f)
forall a. Maybe a
Nothing
Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l _ -> DSum k f -> Maybe (DSum k f)
forall a. a -> Maybe a
Just (DSum k f -> Maybe (DSum k f)) -> DSum k f -> Maybe (DSum k f)
forall a b. (a -> b) -> a -> b
$! k v -> f v -> DMap k f -> DSum k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
k v -> f v -> DMap k f -> DSum k f
go k v
kx f v
x DMap k f
l
where
go :: k v -> f v -> DMap k f -> DSum k f
go :: k v -> f v -> DMap k f -> DSum k f
go kx :: k v
kx x :: f v
x Tip = k v
kx k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x
go _ _ (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l _) = k v -> f v -> DMap k f -> DSum k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
k v -> f v -> DMap k f -> DSum k f
go k v
kx f v
x DMap k f
l
findMax :: DMap k f -> DSum k f
findMax :: DMap k f -> DSum k f
findMax m :: DMap k f
m = case DMap k f -> Maybe (DSum k f)
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Maybe (DSum k f)
lookupMax DMap k f
m of
Just x :: DSum k f
x -> DSum k f
x
Nothing -> [Char] -> DSum k f
forall a. HasCallStack => [Char] -> a
error "Map.findMax: empty map has no maximal element"
lookupMax :: DMap k f -> Maybe (DSum k f)
lookupMax :: DMap k f -> Maybe (DSum k f)
lookupMax m :: DMap k f
m = case DMap k f
m of
Tip -> Maybe (DSum k f)
forall a. Maybe a
Nothing
Bin _ kx :: k v
kx x :: f v
x _ r :: DMap k f
r -> DSum k f -> Maybe (DSum k f)
forall a. a -> Maybe a
Just (DSum k f -> Maybe (DSum k f)) -> DSum k f -> Maybe (DSum k f)
forall a b. (a -> b) -> a -> b
$! k v -> f v -> DMap k f -> DSum k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
k v -> f v -> DMap k f -> DSum k f
go k v
kx f v
x DMap k f
r
where
go :: k v -> f v -> DMap k f -> DSum k f
go :: k v -> f v -> DMap k f -> DSum k f
go kx :: k v
kx x :: f v
x Tip = k v
kx k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x
go _ _ (Bin _ kx :: k v
kx x :: f v
x _ r :: DMap k f
r) = k v -> f v -> DMap k f -> DSum k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
k v -> f v -> DMap k f -> DSum k f
go k v
kx f v
x DMap k f
r
deleteMin :: DMap k f -> DMap k f
deleteMin :: DMap k f -> DMap k f
deleteMin (Bin _ _ _ Tip r :: DMap k f
r) = DMap k f
r
deleteMin (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *). DMap k f -> DMap k f
deleteMin DMap k f
l) DMap k f
r
deleteMin Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
deleteMax :: DMap k f -> DMap k f
deleteMax :: DMap k f -> DMap k f
deleteMax (Bin _ _ _ l :: DMap k f
l Tip) = DMap k f
l
deleteMax (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *). DMap k f -> DMap k f
deleteMax DMap k f
r)
deleteMax Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
updateMinWithKey :: (forall v. k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f
updateMinWithKey :: (forall (v :: k). k v -> f v -> Maybe (f v))
-> DMap k f -> DMap k f
updateMinWithKey f :: forall (v :: k). k v -> f v -> Maybe (f v)
f = DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x Tip r :: DMap k f
r) = case k v -> f v -> Maybe (f v)
forall (v :: k). k v -> f v -> Maybe (f v)
f k v
kx f v
x of
Nothing -> DMap k f
r
Just x' :: f v
x' -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x' DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip DMap k f
r
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x (DMap k f -> DMap k f
go DMap k f
l) DMap k f
r
go Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
updateMaxWithKey :: (forall v. k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f
updateMaxWithKey :: (forall (v :: k). k v -> f v -> Maybe (f v))
-> DMap k f -> DMap k f
updateMaxWithKey f :: forall (v :: k). k v -> f v -> Maybe (f v)
f = DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l Tip) = case k v -> f v -> Maybe (f v)
forall (v :: k). k v -> f v -> Maybe (f v)
f k v
kx f v
x of
Nothing -> DMap k f
l
Just x' :: f v
x' -> Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx f v
x' DMap k f
l DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
balance k v
kx f v
x DMap k f
l (DMap k f -> DMap k f
go DMap k f
r)
go Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
unions :: GCompare k => [DMap k f] -> DMap k f
unions :: [DMap k f] -> DMap k f
unions ts :: [DMap k f]
ts
= (DMap k f -> DMap k f -> DMap k f)
-> DMap k f -> [DMap k f] -> DMap k f
forall a b. (a -> b -> a) -> a -> [b] -> a
foldlStrict DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
union DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
empty [DMap k f]
ts
unionsWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> [DMap k f] -> DMap k f
unionsWithKey :: (forall (v :: k). k v -> f v -> f v -> f v)
-> [DMap k f] -> DMap k f
unionsWithKey f :: forall (v :: k). k v -> f v -> f v -> f v
f ts :: [DMap k f]
ts
= (DMap k f -> DMap k f -> DMap k f)
-> DMap k f -> [DMap k f] -> DMap k f
forall a b. (a -> b -> a) -> a -> [b] -> a
foldlStrict ((forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
unionWithKey forall (v :: k). k v -> f v -> f v -> f v
f) DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
empty [DMap k f]
ts
union :: GCompare k => DMap k f -> DMap k f -> DMap k f
union :: DMap k f -> DMap k f -> DMap k f
union t1 :: DMap k f
t1 Tip = DMap k f
t1
union t1 :: DMap k f
t1 (Bin _ kx :: k v
kx x :: f v
x Tip Tip) = k v -> f v -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> f v -> DMap k f -> DMap k f
insertR k v
kx f v
x DMap k f
t1
union Tip t2 :: DMap k f
t2 = DMap k f
t2
union (Bin _ kx :: k v
kx x :: f v
x Tip Tip) t2 :: DMap k f
t2 = k v -> f v -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> f v -> DMap k f -> DMap k f
insert k v
kx f v
x DMap k f
t2
union t1 :: DMap k f
t1@(Bin _ k1 :: k v
k1 x1 :: f v
x1 l1 :: DMap k f
l1 r1 :: DMap k f
r1) t2 :: DMap k f
t2 = case k v -> DMap k f -> (DMap k f, DMap k f)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, DMap k f)
split k v
k1 DMap k f
t2 of
(l2 :: DMap k f
l2, r2 :: DMap k f
r2)
| DMap k f
l1 DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
l1l2 Bool -> Bool -> Bool
&& DMap k f
r1 DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
r1r2 -> DMap k f
t1
| Bool
otherwise -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 f v
x1 DMap k f
l1l2 DMap k f
r1r2
where !l1l2 :: DMap k f
l1l2 = DMap k f
l1 DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
`union` DMap k f
l2
!r1r2 :: DMap k f
r1r2 = DMap k f
r1 DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
`union` DMap k f
r2
unionWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> DMap k f -> DMap k f -> DMap k f
unionWithKey :: (forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
unionWithKey _ t1 :: DMap k f
t1 Tip = DMap k f
t1
unionWithKey _ Tip t2 :: DMap k f
t2 = DMap k f
t2
unionWithKey f :: forall (v :: k). k v -> f v -> f v -> f v
f (Bin _ k1 :: k v
k1 x1 :: f v
x1 l1 :: DMap k f
l1 r1 :: DMap k f
r1) t2 :: DMap k f
t2 = case k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
splitLookup k v
k1 DMap k f
t2 of
(l2 :: DMap k f
l2, mx2 :: Maybe (f v)
mx2, r2 :: DMap k f
r2) -> case Maybe (f v)
mx2 of
Nothing -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 f v
x1 DMap k f
l1l2 DMap k f
r1r2
Just x2 :: f v
x2 -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 (k v -> f v -> f v -> f v
forall (v :: k). k v -> f v -> f v -> f v
f k v
k1 f v
x1 f v
x2) DMap k f
l1l2 DMap k f
r1r2
where !l1l2 :: DMap k f
l1l2 = (forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
unionWithKey forall (v :: k). k v -> f v -> f v -> f v
f DMap k f
l1 DMap k f
l2
!r1r2 :: DMap k f
r1r2 = (forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DMap k f -> DMap k f
unionWithKey forall (v :: k). k v -> f v -> f v -> f v
f DMap k f
r1 DMap k f
r2
difference :: GCompare k => DMap k f -> DMap k g -> DMap k f
difference :: DMap k f -> DMap k g -> DMap k f
difference Tip _ = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
difference t1 :: DMap k f
t1 Tip = DMap k f
t1
difference t1 :: DMap k f
t1 (Bin _ k2 :: k v
k2 _x2 :: g v
_x2 l2 :: DMap k g
l2 r2 :: DMap k g
r2) = case k v -> DMap k f -> (DMap k f, DMap k f)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, DMap k f)
split k v
k2 DMap k f
t1 of
(l1 :: DMap k f
l1, r1 :: DMap k f
r1)
| DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l1l2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
r1r2 -> DMap k f
t1
| Bool
otherwise -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l1l2 DMap k f
r1r2
where
!l1l2 :: DMap k f
l1l2 = DMap k f
l1 DMap k f -> DMap k g -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
DMap k f -> DMap k g -> DMap k f
`difference` DMap k g
l2
!r1r2 :: DMap k f
r1r2 = DMap k f
r1 DMap k f -> DMap k g -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
DMap k f -> DMap k g -> DMap k f
`difference` DMap k g
r2
differenceWithKey :: GCompare k => (forall v. k v -> f v -> g v -> Maybe (f v)) -> DMap k f -> DMap k g -> DMap k f
differenceWithKey :: (forall (v :: k). k v -> f v -> g v -> Maybe (f v))
-> DMap k f -> DMap k g -> DMap k f
differenceWithKey _ Tip _ = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
differenceWithKey _ t1 :: DMap k f
t1 Tip = DMap k f
t1
differenceWithKey f :: forall (v :: k). k v -> f v -> g v -> Maybe (f v)
f (Bin _ k1 :: k v
k1 x1 :: f v
x1 l1 :: DMap k f
l1 r1 :: DMap k f
r1) t2 :: DMap k g
t2 = case k v -> DMap k g -> (DMap k g, Maybe (g v), DMap k g)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
splitLookup k v
k1 DMap k g
t2 of
(l2 :: DMap k g
l2, mx2 :: Maybe (g v)
mx2, r2 :: DMap k g
r2) -> case Maybe (g v)
mx2 of
Nothing -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 f v
x1 DMap k f
l1l2 DMap k f
r1r2
Just x2 :: g v
x2 -> case k v -> f v -> g v -> Maybe (f v)
forall (v :: k). k v -> f v -> g v -> Maybe (f v)
f k v
k1 f v
x1 g v
x2 of
Nothing -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l1l2 DMap k f
r1r2
Just x1x2 :: f v
x1x2 -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 f v
x1x2 DMap k f
l1l2 DMap k f
r1r2
where !l1l2 :: DMap k f
l1l2 = (forall (v :: k). k v -> f v -> g v -> Maybe (f v))
-> DMap k f -> DMap k g -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> g v -> Maybe (f v))
-> DMap k f -> DMap k g -> DMap k f
differenceWithKey forall (v :: k). k v -> f v -> g v -> Maybe (f v)
f DMap k f
l1 DMap k g
l2
!r1r2 :: DMap k f
r1r2 = (forall (v :: k). k v -> f v -> g v -> Maybe (f v))
-> DMap k f -> DMap k g -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> g v -> Maybe (f v))
-> DMap k f -> DMap k g -> DMap k f
differenceWithKey forall (v :: k). k v -> f v -> g v -> Maybe (f v)
f DMap k f
r1 DMap k g
r2
intersection :: GCompare k => DMap k f -> DMap k f -> DMap k f
intersection :: DMap k f -> DMap k f -> DMap k f
intersection Tip _ = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
intersection _ Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
intersection t1 :: DMap k f
t1@(Bin s1 :: Int
s1 k1 :: k v
k1 x1 :: f v
x1 l1 :: DMap k f
l1 r1 :: DMap k f
r1) t2 :: DMap k f
t2 =
let !(l2 :: DMap k f
l2, found :: Bool
found, r2 :: DMap k f
r2) = k v -> DMap k f -> (DMap k f, Bool, DMap k f)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, Bool, DMap k f)
splitMember k v
k1 DMap k f
t2
!l1l2 :: DMap k f
l1l2 = DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
intersection DMap k f
l1 DMap k f
l2
!r1r2 :: DMap k f
r1r2 = DMap k f -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DMap k f -> DMap k f
intersection DMap k f
r1 DMap k f
r2
in if Bool
found
then if DMap k f
l1l2 DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
l1 Bool -> Bool -> Bool
&& DMap k f
r1r2 DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
r1
then DMap k f
t1
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 f v
x1 DMap k f
l1l2 DMap k f
r1r2
else DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l1l2 DMap k f
r1r2
intersectionWithKey :: GCompare k => (forall v. k v -> f v -> g v -> h v) -> DMap k f -> DMap k g -> DMap k h
intersectionWithKey :: (forall (v :: k). k v -> f v -> g v -> h v)
-> DMap k f -> DMap k g -> DMap k h
intersectionWithKey _ Tip _ = DMap k h
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
intersectionWithKey _ _ Tip = DMap k h
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
intersectionWithKey f :: forall (v :: k). k v -> f v -> g v -> h v
f (Bin s1 :: Int
s1 k1 :: k v
k1 x1 :: f v
x1 l1 :: DMap k f
l1 r1 :: DMap k f
r1) t2 :: DMap k g
t2 =
let !(l2 :: DMap k g
l2, found :: Maybe (g v)
found, r2 :: DMap k g
r2) = k v -> DMap k g -> (DMap k g, Maybe (g v), DMap k g)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
splitLookup k v
k1 DMap k g
t2
!l1l2 :: DMap k h
l1l2 = (forall (v :: k). k v -> f v -> g v -> h v)
-> DMap k f -> DMap k g -> DMap k h
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *) (h :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> g v -> h v)
-> DMap k f -> DMap k g -> DMap k h
intersectionWithKey forall (v :: k). k v -> f v -> g v -> h v
f DMap k f
l1 DMap k g
l2
!r1r2 :: DMap k h
r1r2 = (forall (v :: k). k v -> f v -> g v -> h v)
-> DMap k f -> DMap k g -> DMap k h
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *) (h :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> g v -> h v)
-> DMap k f -> DMap k g -> DMap k h
intersectionWithKey forall (v :: k). k v -> f v -> g v -> h v
f DMap k f
r1 DMap k g
r2
in case Maybe (g v)
found of
Nothing -> DMap k h -> DMap k h -> DMap k h
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k h
l1l2 DMap k h
r1r2
Just x2 :: g v
x2 -> k v -> h v -> DMap k h -> DMap k h -> DMap k h
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
k1 (k v -> f v -> g v -> h v
forall (v :: k). k v -> f v -> g v -> h v
f k v
k1 f v
x1 g v
x2) DMap k h
l1l2 DMap k h
r1r2
isSubmapOf
:: forall k f
. (GCompare k, Has' Eq k f)
=> DMap k f -> DMap k f -> Bool
isSubmapOf :: DMap k f -> DMap k f -> Bool
isSubmapOf m1 :: DMap k f
m1 m2 :: DMap k f
m2 = (forall (v :: k). k v -> k v -> f v -> f v -> Bool)
-> DMap k f -> DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
isSubmapOfBy (\k :: k v
k _ x0 :: f v
x0 x1 :: f v
x1 -> k v -> (Eq (f v) => Bool) -> Bool
forall k k' (c :: k -> Constraint) (g :: k' -> k) (f :: k' -> *)
(a :: k') r.
Has' c f g =>
f a -> (c (g a) => r) -> r
has' @Eq @f k v
k (f v
x0 f v -> f v -> Bool
forall a. Eq a => a -> a -> Bool
== f v
x1)) DMap k f
m1 DMap k f
m2
isSubmapOfBy :: GCompare k => (forall v. k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
isSubmapOfBy :: (forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
isSubmapOfBy f :: forall (v :: k). k v -> k v -> f v -> g v -> Bool
f t1 :: DMap k f
t1 t2 :: DMap k g
t2
= (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= DMap k g -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k g
t2) Bool -> Bool -> Bool
&& ((forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
submap' forall (v :: k). k v -> k v -> f v -> g v -> Bool
f DMap k f
t1 DMap k g
t2)
submap' :: GCompare k => (forall v. k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
submap' :: (forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
submap' _ Tip _ = Bool
True
submap' _ _ Tip = Bool
False
submap' f :: forall (v :: k). k v -> k v -> f v -> g v -> Bool
f (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) t :: DMap k g
t
= case Maybe (k v, g v)
found of
Nothing -> Bool
False
Just (ky :: k v
ky, y :: g v
y) -> k v -> k v -> f v -> g v -> Bool
forall (v :: k). k v -> k v -> f v -> g v -> Bool
f k v
kx k v
ky f v
x g v
y Bool -> Bool -> Bool
&& (forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
submap' forall (v :: k). k v -> k v -> f v -> g v -> Bool
f DMap k f
l DMap k g
lt Bool -> Bool -> Bool
&& (forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
submap' forall (v :: k). k v -> k v -> f v -> g v -> Bool
f DMap k f
r DMap k g
gt
where
(lt :: DMap k g
lt,found :: Maybe (k v, g v)
found,gt :: DMap k g
gt) = k v -> DMap k g -> (DMap k g, Maybe (k v, g v), DMap k g)
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> DMap k f -> (DMap k f, Maybe (k v, f v), DMap k f)
splitLookupWithKey k v
kx DMap k g
t
isProperSubmapOf
:: forall k f
. (GCompare k, Has' Eq k f)
=> DMap k f -> DMap k f -> Bool
isProperSubmapOf :: DMap k f -> DMap k f -> Bool
isProperSubmapOf m1 :: DMap k f
m1 m2 :: DMap k f
m2
= (forall (v :: k). k v -> k v -> f v -> f v -> Bool)
-> DMap k f -> DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
isProperSubmapOfBy (\k :: k v
k _ x0 :: f v
x0 x1 :: f v
x1 -> k v -> (Eq (f v) => Bool) -> Bool
forall k k' (c :: k -> Constraint) (g :: k' -> k) (f :: k' -> *)
(a :: k') r.
Has' c f g =>
f a -> (c (g a) => r) -> r
has' @Eq @f k v
k (f v
x0 f v -> f v -> Bool
forall a. Eq a => a -> a -> Bool
== f v
x1)) DMap k f
m1 DMap k f
m2
isProperSubmapOfBy :: GCompare k => (forall v. k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
isProperSubmapOfBy :: (forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
isProperSubmapOfBy f :: forall (v :: k). k v -> k v -> f v -> g v -> Bool
f t1 :: DMap k f
t1 t2 :: DMap k g
t2
= (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< DMap k g -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k g
t2) Bool -> Bool -> Bool
&& ((forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> k v -> f v -> g v -> Bool)
-> DMap k f -> DMap k g -> Bool
submap' forall (v :: k). k v -> k v -> f v -> g v -> Bool
f DMap k f
t1 DMap k g
t2)
filterWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> DMap k f
filterWithKey :: (forall (v :: k). k v -> f v -> Bool) -> DMap k f -> DMap k f
filterWithKey p :: forall (v :: k). k v -> f v -> Bool
p = DMap k f -> DMap k f
go
where
go :: DMap k f -> DMap k f
go Tip = DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go t :: DMap k f
t@(Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r)
| k v -> f v -> Bool
forall (v :: k). k v -> f v -> Bool
p k v
kx f v
x = if DMap k f
l' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
l Bool -> Bool -> Bool
&& DMap k f
r' DMap k f -> DMap k f -> Bool
forall a. a -> a -> Bool
`ptrEq` DMap k f
r
then DMap k f
t
else k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l' DMap k f
r'
| Bool
otherwise = DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l' DMap k f
r'
where !l' :: DMap k f
l' = DMap k f -> DMap k f
go DMap k f
l
!r' :: DMap k f
r' = DMap k f -> DMap k f
go DMap k f
r
partitionWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f)
partitionWithKey :: (forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> (DMap k f, DMap k f)
partitionWithKey p0 :: forall (v :: k). k v -> f v -> Bool
p0 m0 :: DMap k f
m0 = (DMap k f :*: DMap k f) -> (DMap k f, DMap k f)
forall a b. (a :*: b) -> (a, b)
toPair ((forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
go forall (v :: k). k v -> f v -> Bool
p0 DMap k f
m0)
where
go :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f :*: DMap k f)
go :: (forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
go _ Tip = (DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go p :: forall (v :: k). k v -> f v -> Bool
p (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r)
| k v -> f v -> Bool
forall (v :: k). k v -> f v -> Bool
p k v
kx f v
x = (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l1 DMap k f
r1 DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l2 DMap k f
r2)
| Bool
otherwise = (DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k f
l1 DMap k f
r1 DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l2 DMap k f
r2)
where
(l1 :: DMap k f
l1 :*: l2 :: DMap k f
l2) = (forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
go forall (v :: k). k v -> f v -> Bool
p DMap k f
l
(r1 :: DMap k f
r1 :*: r2 :: DMap k f
r2) = (forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Bool)
-> DMap k f -> DMap k f :*: DMap k f
go forall (v :: k). k v -> f v -> Bool
p DMap k f
r
mapMaybe :: GCompare k => (forall v. f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybe :: (forall (v :: k). f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybe f :: forall (v :: k). f v -> Maybe (g v)
f = (forall (v :: k). k v -> f v -> Maybe (g v))
-> DMap k f -> DMap k g
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Maybe (g v))
-> DMap k f -> DMap k g
mapMaybeWithKey ((f v -> Maybe (g v)) -> k v -> f v -> Maybe (g v)
forall a b. a -> b -> a
const f v -> Maybe (g v)
forall (v :: k). f v -> Maybe (g v)
f)
mapMaybeWithKey :: GCompare k => (forall v. k v -> f v -> Maybe (g v)) -> DMap k f -> DMap k g
mapMaybeWithKey :: (forall (v :: k). k v -> f v -> Maybe (g v))
-> DMap k f -> DMap k g
mapMaybeWithKey f :: forall (v :: k). k v -> f v -> Maybe (g v)
f = DMap k f -> DMap k g
go
where
go :: DMap k f -> DMap k g
go Tip = DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> f v -> Maybe (g v)
forall (v :: k). k v -> f v -> Maybe (g v)
f k v
kx f v
x of
Just y :: g v
y -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx g v
y (DMap k f -> DMap k g
go DMap k f
l) (DMap k f -> DMap k g
go DMap k f
r)
Nothing -> DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge (DMap k f -> DMap k g
go DMap k f
l) (DMap k f -> DMap k g
go DMap k f
r)
mapEitherWithKey :: GCompare k =>
(forall v. k v -> f v -> Either (g v) (h v)) -> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey :: (forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey f0 :: forall (v :: k). k v -> f v -> Either (g v) (h v)
f0 = (DMap k g :*: DMap k h) -> (DMap k g, DMap k h)
forall a b. (a :*: b) -> (a, b)
toPair ((DMap k g :*: DMap k h) -> (DMap k g, DMap k h))
-> (DMap k f -> DMap k g :*: DMap k h)
-> DMap k f
-> (DMap k g, DMap k h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> DMap k g :*: DMap k h
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *) (h :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> DMap k g :*: DMap k h
go forall (v :: k). k v -> f v -> Either (g v) (h v)
f0
where
go :: GCompare k
=> (forall v. k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g :*: DMap k h)
go :: (forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> DMap k g :*: DMap k h
go _ Tip = (DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip DMap k g -> DMap k h -> DMap k g :*: DMap k h
forall a b. a -> b -> a :*: b
:*: DMap k h
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go f :: forall (v :: k). k v -> f v -> Either (g v) (h v)
f (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> f v -> Either (g v) (h v)
forall (v :: k). k v -> f v -> Either (g v) (h v)
f k v
kx f v
x of
Left y :: g v
y -> (k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx g v
y DMap k g
l1 DMap k g
r1 DMap k g -> DMap k h -> DMap k g :*: DMap k h
forall a b. a -> b -> a :*: b
:*: DMap k h -> DMap k h -> DMap k h
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k h
l2 DMap k h
r2)
Right z :: h v
z -> (DMap k g -> DMap k g -> DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> DMap k2 f -> DMap k2 f
merge DMap k g
l1 DMap k g
r1 DMap k g -> DMap k h -> DMap k g :*: DMap k h
forall a b. a -> b -> a :*: b
:*: k v -> h v -> DMap k h -> DMap k h -> DMap k h
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx h v
z DMap k h
l2 DMap k h
r2)
where
(l1 :: DMap k g
l1,l2 :: DMap k h
l2) = (forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g, DMap k h)
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *) (h :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey forall (v :: k). k v -> f v -> Either (g v) (h v)
f DMap k f
l
(r1 :: DMap k g
r1,r2 :: DMap k h
r2) = (forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g, DMap k h)
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *) (h :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> Either (g v) (h v))
-> DMap k f -> (DMap k g, DMap k h)
mapEitherWithKey forall (v :: k). k v -> f v -> Either (g v) (h v)
f DMap k f
r
map :: (forall v. f v -> g v) -> DMap k f -> DMap k g
map :: (forall (v :: k). f v -> g v) -> DMap k f -> DMap k g
map f :: forall (v :: k). f v -> g v
f = DMap k f -> DMap k g
go
where
go :: DMap k f -> DMap k g
go Tip = DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx (f v -> g v
forall (v :: k). f v -> g v
f f v
x) (DMap k f -> DMap k g
go DMap k f
l) (DMap k f -> DMap k g
go DMap k f
r)
ffor :: DMap k f -> (forall v. f v -> g v) -> DMap k g
ffor :: DMap k f -> (forall (v :: k). f v -> g v) -> DMap k g
ffor m :: DMap k f
m f :: forall (v :: k). f v -> g v
f = (forall (v :: k). f v -> g v) -> DMap k f -> DMap k g
forall k (f :: k -> *) (g :: k -> *) (k :: k -> *).
(forall (v :: k). f v -> g v) -> DMap k f -> DMap k g
map forall (v :: k). f v -> g v
f DMap k f
m
mapWithKey :: (forall v. k v -> f v -> g v) -> DMap k f -> DMap k g
mapWithKey :: (forall (v :: k). k v -> f v -> g v) -> DMap k f -> DMap k g
mapWithKey f :: forall (v :: k). k v -> f v -> g v
f = DMap k f -> DMap k g
go
where
go :: DMap k f -> DMap k g
go Tip = DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx (k v -> f v -> g v
forall (v :: k). k v -> f v -> g v
f k v
kx f v
x) (DMap k f -> DMap k g
go DMap k f
l) (DMap k f -> DMap k g
go DMap k f
r)
fforWithKey :: DMap k f -> (forall v. k v -> f v -> g v) -> DMap k g
fforWithKey :: DMap k f -> (forall (v :: k). k v -> f v -> g v) -> DMap k g
fforWithKey m :: DMap k f
m f :: forall (v :: k). k v -> f v -> g v
f = (forall (v :: k). k v -> f v -> g v) -> DMap k f -> DMap k g
forall k (k :: k -> *) (f :: k -> *) (g :: k -> *).
(forall (v :: k). k v -> f v -> g v) -> DMap k f -> DMap k g
mapWithKey forall (v :: k). k v -> f v -> g v
f DMap k f
m
traverseWithKey_ :: Applicative t => (forall v. k v -> f v -> t ()) -> DMap k f -> t ()
traverseWithKey_ :: (forall (v :: k). k v -> f v -> t ()) -> DMap k f -> t ()
traverseWithKey_ f :: forall (v :: k). k v -> f v -> t ()
f = DMap k f -> t ()
go
where
go :: DMap k f -> t ()
go Tip = () -> t ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
go (Bin 1 k :: k v
k v :: f v
v _ _) = k v -> f v -> t ()
forall (v :: k). k v -> f v -> t ()
f k v
k f v
v
go (Bin s :: Int
s k :: k v
k v :: f v
v l :: DMap k f
l r :: DMap k f
r) = DMap k f -> t ()
go DMap k f
l t () -> t () -> t ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> k v -> f v -> t ()
forall (v :: k). k v -> f v -> t ()
f k v
k f v
v t () -> t () -> t ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> DMap k f -> t ()
go DMap k f
r
forWithKey_ :: Applicative t => DMap k f -> (forall v. k v -> f v -> t ()) -> t ()
forWithKey_ :: DMap k f -> (forall (v :: k). k v -> f v -> t ()) -> t ()
forWithKey_ m :: DMap k f
m f :: forall (v :: k). k v -> f v -> t ()
f = (forall (v :: k). k v -> f v -> t ()) -> DMap k f -> t ()
forall k (t :: * -> *) (k :: k -> *) (f :: k -> *).
Applicative t =>
(forall (v :: k). k v -> f v -> t ()) -> DMap k f -> t ()
traverseWithKey_ forall (v :: k). k v -> f v -> t ()
f DMap k f
m
traverseWithKey :: Applicative t => (forall v. k v -> f v -> t (g v)) -> DMap k f -> t (DMap k g)
traverseWithKey :: (forall (v :: k). k v -> f v -> t (g v))
-> DMap k f -> t (DMap k g)
traverseWithKey f :: forall (v :: k). k v -> f v -> t (g v)
f = DMap k f -> t (DMap k g)
go
where
go :: DMap k f -> t (DMap k g)
go Tip = DMap k g -> t (DMap k g)
forall (f :: * -> *) a. Applicative f => a -> f a
pure DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin 1 k :: k v
k v :: f v
v _ _) = (\v' :: g v
v' -> Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin 1 k v
k g v
v' DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip) (g v -> DMap k g) -> t (g v) -> t (DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k v -> f v -> t (g v)
forall (v :: k). k v -> f v -> t (g v)
f k v
k f v
v
go (Bin s :: Int
s k :: k v
k v :: f v
v l :: DMap k f
l r :: DMap k f
r) = (g v -> DMap k g -> DMap k g -> DMap k g)
-> DMap k g -> g v -> DMap k g -> DMap k g
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
s k v
k) (DMap k g -> g v -> DMap k g -> DMap k g)
-> t (DMap k g) -> t (g v -> DMap k g -> DMap k g)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DMap k f -> t (DMap k g)
go DMap k f
l t (g v -> DMap k g -> DMap k g)
-> t (g v) -> t (DMap k g -> DMap k g)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> k v -> f v -> t (g v)
forall (v :: k). k v -> f v -> t (g v)
f k v
k f v
v t (DMap k g -> DMap k g) -> t (DMap k g) -> t (DMap k g)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DMap k f -> t (DMap k g)
go DMap k f
r
forWithKey :: Applicative t => DMap k f -> (forall v. k v -> f v -> t (g v)) -> t (DMap k g)
forWithKey :: DMap k f
-> (forall (v :: k). k v -> f v -> t (g v)) -> t (DMap k g)
forWithKey m :: DMap k f
m f :: forall (v :: k). k v -> f v -> t (g v)
f = (forall (v :: k). k v -> f v -> t (g v))
-> DMap k f -> t (DMap k g)
forall k (t :: * -> *) (k :: k -> *) (f :: k -> *) (g :: k -> *).
Applicative t =>
(forall (v :: k). k v -> f v -> t (g v))
-> DMap k f -> t (DMap k g)
traverseWithKey forall (v :: k). k v -> f v -> t (g v)
f DMap k f
m
mapAccumLWithKey :: (forall v. a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g)
mapAccumLWithKey :: (forall (v :: k). a -> k v -> f v -> (a, g v))
-> a -> DMap k f -> (a, DMap k g)
mapAccumLWithKey f :: forall (v :: k). a -> k v -> f v -> (a, g v)
f = a -> DMap k f -> (a, DMap k g)
go
where
go :: a -> DMap k f -> (a, DMap k g)
go a :: a
a Tip = (a
a,DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go a :: a
a (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
let (a1 :: a
a1,l' :: DMap k g
l') = a -> DMap k f -> (a, DMap k g)
go a
a DMap k f
l
(a2 :: a
a2,x' :: g v
x') = a -> k v -> f v -> (a, g v)
forall (v :: k). a -> k v -> f v -> (a, g v)
f a
a1 k v
kx f v
x
(a3 :: a
a3,r' :: DMap k g
r') = a -> DMap k f -> (a, DMap k g)
go a
a2 DMap k f
r
in (a
a3,Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx g v
x' DMap k g
l' DMap k g
r')
mapAccumRWithKey :: (forall v. a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g)
mapAccumRWithKey :: (forall (v :: k). a -> k v -> f v -> (a, g v))
-> a -> DMap k f -> (a, DMap k g)
mapAccumRWithKey f :: forall (v :: k). a -> k v -> f v -> (a, g v)
f = a -> DMap k f -> (a, DMap k g)
go
where
go :: a -> DMap k f -> (a, DMap k g)
go a :: a
a Tip = (a
a,DMap k g
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go a :: a
a (Bin sx :: Int
sx kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) =
let (a1 :: a
a1,r' :: DMap k g
r') = a -> DMap k f -> (a, DMap k g)
go a
a DMap k f
r
(a2 :: a
a2,x' :: g v
x') = a -> k v -> f v -> (a, g v)
forall (v :: k). a -> k v -> f v -> (a, g v)
f a
a1 k v
kx f v
x
(a3 :: a
a3,l' :: DMap k g
l') = a -> DMap k f -> (a, DMap k g)
go a
a2 DMap k f
l
in (a
a3,Int -> k v -> g v -> DMap k g -> DMap k g -> DMap k g
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sx k v
kx g v
x' DMap k g
l' DMap k g
r')
mapKeysWith :: GCompare k2 => (forall v. k2 v -> f v -> f v -> f v) -> (forall v. k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysWith :: (forall (v :: k). k2 v -> f v -> f v -> f v)
-> (forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysWith c :: forall (v :: k). k2 v -> f v -> f v -> f v
c f :: forall (v :: k). k1 v -> k2 v
f = (forall (v :: k). k2 v -> f v -> f v -> f v)
-> [DSum k2 f] -> DMap k2 f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> [DSum k f] -> DMap k f
fromListWithKey forall (v :: k). k2 v -> f v -> f v -> f v
c ([DSum k2 f] -> DMap k2 f)
-> (DMap k1 f -> [DSum k2 f]) -> DMap k1 f -> DMap k2 f
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DSum k1 f -> DSum k2 f) -> [DSum k1 f] -> [DSum k2 f]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map DSum k1 f -> DSum k2 f
fFirst ([DSum k1 f] -> [DSum k2 f])
-> (DMap k1 f -> [DSum k1 f]) -> DMap k1 f -> [DSum k2 f]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap k1 f -> [DSum k1 f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toList
where fFirst :: DSum k1 f -> DSum k2 f
fFirst (x :: k1 a
x :=> y :: f a
y) = (k1 a -> k2 a
forall (v :: k). k1 v -> k2 v
f k1 a
x k2 a -> f a -> DSum k2 f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f a
y)
mapKeysMonotonic :: (forall v. k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysMonotonic :: (forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysMonotonic _ Tip = DMap k2 f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
mapKeysMonotonic f :: forall (v :: k). k1 v -> k2 v
f (Bin sz :: Int
sz k :: k1 v
k x :: f v
x l :: DMap k1 f
l r :: DMap k1 f
r) =
Int -> k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
forall k (k :: k -> *) (v :: k) (f :: k -> *).
Int -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
Bin Int
sz (k1 v -> k2 v
forall (v :: k). k1 v -> k2 v
f k1 v
k) f v
x ((forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
forall k (k1 :: k -> *) (k2 :: k -> *) (f :: k -> *).
(forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysMonotonic forall (v :: k). k1 v -> k2 v
f DMap k1 f
l) ((forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
forall k (k1 :: k -> *) (k2 :: k -> *) (f :: k -> *).
(forall (v :: k). k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
mapKeysMonotonic forall (v :: k). k1 v -> k2 v
f DMap k1 f
r)
foldWithKey :: (forall v. k v -> f v -> b -> b) -> b -> DMap k f -> b
foldWithKey :: (forall (v :: k). k v -> f v -> b -> b) -> b -> DMap k f -> b
foldWithKey = (forall (v :: k). k v -> f v -> b -> b) -> b -> DMap k f -> b
forall k (k :: k -> *) (f :: k -> *) b.
(forall (v :: k). k v -> f v -> b -> b) -> b -> DMap k f -> b
foldrWithKey
{-# DEPRECATED foldWithKey "Use foldrWithKey instead" #-}
foldrWithKey :: (forall v. k v -> f v -> b -> b) -> b -> DMap k f -> b
foldrWithKey :: (forall (v :: k). k v -> f v -> b -> b) -> b -> DMap k f -> b
foldrWithKey f :: forall (v :: k). k v -> f v -> b -> b
f = b -> DMap k f -> b
go
where
go :: b -> DMap k f -> b
go z :: b
z Tip = b
z
go z :: b
z (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = b -> DMap k f -> b
go (k v -> f v -> b -> b
forall (v :: k). k v -> f v -> b -> b
f k v
kx f v
x (b -> DMap k f -> b
go b
z DMap k f
r)) DMap k f
l
foldlWithKey :: (forall v. b -> k v -> f v -> b) -> b -> DMap k f -> b
foldlWithKey :: (forall (v :: k). b -> k v -> f v -> b) -> b -> DMap k f -> b
foldlWithKey f :: forall (v :: k). b -> k v -> f v -> b
f = b -> DMap k f -> b
go
where
go :: b -> DMap k f -> b
go z :: b
z Tip = b
z
go z :: b
z (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = b -> DMap k f -> b
go (b -> k v -> f v -> b
forall (v :: k). b -> k v -> f v -> b
f (b -> DMap k f -> b
go b
z DMap k f
l) k v
kx f v
x) DMap k f
r
keys :: DMap k f -> [Some k]
keys :: DMap k f -> [Some k]
keys m :: DMap k f
m
= [k a -> Some k
forall k (tag :: k -> *) (a :: k). tag a -> Some tag
mkSome k a
k | (k :: k a
k :=> _) <- DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
assocs DMap k f
m]
assocs :: DMap k f -> [DSum k f]
assocs :: DMap k f -> [DSum k f]
assocs m :: DMap k f
m
= DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toList DMap k f
m
fromList :: GCompare k => [DSum k f] -> DMap k f
fromList :: [DSum k f] -> DMap k f
fromList xs :: [DSum k f]
xs
= (DMap k f -> DSum k f -> DMap k f)
-> DMap k f -> [DSum k f] -> DMap k f
forall a b. (a -> b -> a) -> a -> [b] -> a
foldlStrict DMap k f -> DSum k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> DSum k f -> DMap k f
ins DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
empty [DSum k f]
xs
where
ins :: GCompare k => DMap k f -> DSum k f -> DMap k f
ins :: DMap k f -> DSum k f -> DMap k f
ins t :: DMap k f
t (k :: k a
k :=> x :: f a
x) = k a -> f a -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
k v -> f v -> DMap k f -> DMap k f
insert k a
k f a
x DMap k f
t
fromListWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> [DSum k f] -> DMap k f
fromListWithKey :: (forall (v :: k). k v -> f v -> f v -> f v)
-> [DSum k f] -> DMap k f
fromListWithKey f :: forall (v :: k). k v -> f v -> f v -> f v
f xs :: [DSum k f]
xs
= (DMap k f -> DSum k f -> DMap k f)
-> DMap k f -> [DSum k f] -> DMap k f
forall a b. (a -> b -> a) -> a -> [b] -> a
foldlStrict ((forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DSum k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DSum k f -> DMap k f
ins forall (v :: k). k v -> f v -> f v -> f v
f) DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
empty [DSum k f]
xs
where
ins :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> DMap k f -> DSum k f -> DMap k f
ins :: (forall (v :: k). k v -> f v -> f v -> f v)
-> DMap k f -> DSum k f -> DMap k f
ins f :: forall (v :: k). k v -> f v -> f v -> f v
f t :: DMap k f
t (k :: k a
k :=> x :: f a
x) = (k a -> f a -> f a -> f a) -> k a -> f a -> DMap k f -> DMap k f
forall k (k :: k -> *) (f :: k -> *) (v :: k).
GCompare k =>
(k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
insertWithKey k a -> f a -> f a -> f a
forall (v :: k). k v -> f v -> f v -> f v
f k a
k f a
x DMap k f
t
toList :: DMap k f -> [DSum k f]
toList :: DMap k f -> [DSum k f]
toList t :: DMap k f
t = DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toAscList DMap k f
t
toAscList :: DMap k f -> [DSum k f]
toAscList :: DMap k f -> [DSum k f]
toAscList t :: DMap k f
t = (forall (v :: k). k v -> f v -> [DSum k f] -> [DSum k f])
-> [DSum k f] -> DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *) b.
(forall (v :: k). k v -> f v -> b -> b) -> b -> DMap k f -> b
foldrWithKey (\k :: k v
k x :: f v
x xs :: [DSum k f]
xs -> (k v
k k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x)DSum k f -> [DSum k f] -> [DSum k f]
forall a. a -> [a] -> [a]
:[DSum k f]
xs) [] DMap k f
t
toDescList :: DMap k f -> [DSum k f]
toDescList :: DMap k f -> [DSum k f]
toDescList t :: DMap k f
t = (forall (v :: k). [DSum k f] -> k v -> f v -> [DSum k f])
-> [DSum k f] -> DMap k f -> [DSum k f]
forall k b (k :: k -> *) (f :: k -> *).
(forall (v :: k). b -> k v -> f v -> b) -> b -> DMap k f -> b
foldlWithKey (\xs :: [DSum k f]
xs k :: k v
k x :: f v
x -> (k v
k k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x)DSum k f -> [DSum k f] -> [DSum k f]
forall a. a -> [a] -> [a]
:[DSum k f]
xs) [] DMap k f
t
fromAscList :: GEq k => [DSum k f] -> DMap k f
fromAscList :: [DSum k f] -> DMap k f
fromAscList xs :: [DSum k f]
xs
= (forall (v :: k). k v -> f v -> f v -> f v)
-> [DSum k f] -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GEq k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> [DSum k f] -> DMap k f
fromAscListWithKey (\_ x :: f v
x _ -> f v
x) [DSum k f]
xs
fromAscListWithKey :: GEq k => (forall v. k v -> f v -> f v -> f v) -> [DSum k f] -> DMap k f
fromAscListWithKey :: (forall (v :: k). k v -> f v -> f v -> f v)
-> [DSum k f] -> DMap k f
fromAscListWithKey f :: forall (v :: k). k v -> f v -> f v -> f v
f xs :: [DSum k f]
xs
= [DSum k f] -> DMap k f
forall k (k :: k -> *) (f :: k -> *). [DSum k f] -> DMap k f
fromDistinctAscList ((k Any -> f Any -> f Any -> f Any) -> [DSum k f] -> [DSum k f]
combineEq k Any -> f Any -> f Any -> f Any
forall (v :: k). k v -> f v -> f v -> f v
f [DSum k f]
xs)
where
combineEq :: (k Any -> f Any -> f Any -> f Any) -> [DSum k f] -> [DSum k f]
combineEq _ xs' :: [DSum k f]
xs'
= case [DSum k f]
xs' of
[] -> []
[x :: DSum k f
x] -> [DSum k f
x]
(x :: DSum k f
x:xx :: [DSum k f]
xx) -> (forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *).
GEq k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
combineEq' forall (v :: k). k v -> f v -> f v -> f v
f DSum k f
x [DSum k f]
xx
combineEq' :: GEq k => (forall v. k v -> f v -> f v -> f v) -> DSum k f -> [DSum k f] -> [DSum k f]
combineEq' :: (forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
combineEq' f :: forall (v :: k). k v -> f v -> f v -> f v
f z :: DSum k f
z [] = [DSum k f
z]
combineEq' f :: forall (v :: k). k v -> f v -> f v -> f v
f z :: DSum k f
z@(kz :: k a
kz :=> zz :: f a
zz) (x :: DSum k f
x@(kx :: k a
kx :=> xx :: f a
xx):xs' :: [DSum k f]
xs') =
case k a -> k a -> Maybe (a :~: a)
forall k (f :: k -> *) (a :: k) (b :: k).
GEq f =>
f a -> f b -> Maybe (a :~: b)
geq k a
kx k a
kz of
Just Refl -> let yy :: f a
yy = k a -> f a -> f a -> f a
forall (v :: k). k v -> f v -> f v -> f v
f k a
kx f a
xx f a
f a
zz in (forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *).
GEq k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
combineEq' forall (v :: k). k v -> f v -> f v -> f v
f (k a
kx k a -> f a -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f a
yy) [DSum k f]
xs'
Nothing -> DSum k f
z DSum k f -> [DSum k f] -> [DSum k f]
forall a. a -> [a] -> [a]
: (forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *).
GEq k =>
(forall (v :: k). k v -> f v -> f v -> f v)
-> DSum k f -> [DSum k f] -> [DSum k f]
combineEq' forall (v :: k). k v -> f v -> f v -> f v
f DSum k f
x [DSum k f]
xs'
fromDistinctAscList :: [DSum k f] -> DMap k f
fromDistinctAscList :: [DSum k f] -> DMap k f
fromDistinctAscList xs :: [DSum k f]
xs
= (DMap k f -> [DSum k f] -> DMap k f)
-> Int -> [DSum k f] -> DMap k f
forall k (k :: k -> *) (f :: k -> *) b.
(DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
build DMap k f -> [DSum k f] -> DMap k f
forall a b. a -> b -> a
const ([DSum k f] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [DSum k f]
xs) [DSum k f]
xs
where
build :: (DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
build :: (DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
build c :: DMap k f -> [DSum k f] -> b
c 0 xs' :: [DSum k f]
xs' = DMap k f -> [DSum k f] -> b
c DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip [DSum k f]
xs'
build c :: DMap k f -> [DSum k f] -> b
c 5 xs' :: [DSum k f]
xs' = case [DSum k f]
xs' of
((k1 :: k a
k1:=>x1 :: f a
x1):(k2 :: k a
k2:=>x2 :: f a
x2):(k3 :: k a
k3:=>x3 :: f a
x3):(k4 :: k a
k4:=>x4 :: f a
x4):(k5 :: k a
k5:=>x5 :: f a
x5):xx :: [DSum k f]
xx)
-> DMap k f -> [DSum k f] -> b
c (k a -> f a -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
bin k a
k4 f a
x4 (k a -> f a -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
bin k a
k2 f a
x2 (k a -> f a -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k a
k1 f a
x1) (k a -> f a -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k a
k3 f a
x3)) (k a -> f a -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
singleton k a
k5 f a
x5)) [DSum k f]
xx
_ -> [Char] -> b
forall a. HasCallStack => [Char] -> a
error "fromDistinctAscList build"
build c :: DMap k f -> [DSum k f] -> b
c n :: Int
n xs' :: [DSum k f]
xs' = Int -> b -> b
forall a b. a -> b -> b
seq Int
nr (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ (DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
forall k (k :: k -> *) (f :: k -> *) b.
(DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
build (Int -> (DMap k f -> [DSum k f] -> b) -> DMap k f -> [DSum k f] -> b
forall k (k :: k -> *) (f :: k -> *) b.
Int -> (DMap k f -> [DSum k f] -> b) -> DMap k f -> [DSum k f] -> b
buildR Int
nr DMap k f -> [DSum k f] -> b
c) Int
nl [DSum k f]
xs'
where
nl :: Int
nl = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2
nr :: Int
nr = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
nl Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1
buildR :: Int -> (DMap k f -> [DSum k f] -> b) -> DMap k f -> [DSum k f] -> b
buildR :: Int -> (DMap k f -> [DSum k f] -> b) -> DMap k f -> [DSum k f] -> b
buildR n :: Int
n c :: DMap k f -> [DSum k f] -> b
c l :: DMap k f
l ((k :: k a
k:=>x :: f a
x):ys :: [DSum k f]
ys) = (DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
forall k (k :: k -> *) (f :: k -> *) b.
(DMap k f -> [DSum k f] -> b) -> Int -> [DSum k f] -> b
build (DMap k f
-> k a
-> f a
-> (DMap k f -> [DSum k f] -> b)
-> DMap k f
-> [DSum k f]
-> b
forall k (k :: k -> *) (f :: k -> *) (v :: k) a b.
DMap k f
-> k v -> f v -> (DMap k f -> a -> b) -> DMap k f -> a -> b
buildB DMap k f
l k a
k f a
x DMap k f -> [DSum k f] -> b
c) Int
n [DSum k f]
ys
buildR _ _ _ [] = [Char] -> b
forall a. HasCallStack => [Char] -> a
error "fromDistinctAscList buildR []"
buildB :: DMap k f -> k v -> f v -> (DMap k f -> a -> b) -> DMap k f -> a -> b
buildB :: DMap k f
-> k v -> f v -> (DMap k f -> a -> b) -> DMap k f -> a -> b
buildB l :: DMap k f
l k :: k v
k x :: f v
x c :: DMap k f -> a -> b
c r :: DMap k f
r zs :: a
zs = DMap k f -> a -> b
c (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
bin k v
k f v
x DMap k f
l DMap k f
r) a
zs
split :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, DMap k f)
split :: k v -> DMap k f -> (DMap k f, DMap k f)
split k :: k v
k = (DMap k f :*: DMap k f) -> (DMap k f, DMap k f)
forall a b. (a :*: b) -> (a, b)
toPair ((DMap k f :*: DMap k f) -> (DMap k f, DMap k f))
-> (DMap k f -> DMap k f :*: DMap k f)
-> DMap k f
-> (DMap k f, DMap k f)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap k f -> DMap k f :*: DMap k f
go
where
go :: DMap k f -> (DMap k f :*: DMap k f)
go :: DMap k f -> DMap k f :*: DMap k f
go Tip = (DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip)
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> let !(lt :: DMap k f
lt :*: gt :: DMap k f
gt) = DMap k f -> DMap k f :*: DMap k f
go DMap k f
l in (DMap k f
lt DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
gt DMap k f
r)
GGT -> let !(lt :: DMap k f
lt :*: gt :: DMap k f
gt) = DMap k f -> DMap k f :*: DMap k f
go DMap k f
r in (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l DMap k f
lt DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: DMap k f
gt)
GEQ -> (DMap k f
l DMap k f -> DMap k f -> DMap k f :*: DMap k f
forall a b. a -> b -> a :*: b
:*: DMap k f
r)
{-# INLINABLE split #-}
splitLookup :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
splitLookup :: k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
splitLookup k :: k v
k = Triple' (DMap k f) (Maybe (f v)) (DMap k f)
-> (DMap k f, Maybe (f v), DMap k f)
forall a b c. Triple' a b c -> (a, b, c)
toTriple (Triple' (DMap k f) (Maybe (f v)) (DMap k f)
-> (DMap k f, Maybe (f v), DMap k f))
-> (DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f))
-> DMap k f
-> (DMap k f, Maybe (f v), DMap k f)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
go
where
go :: DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
go :: DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
go Tip = DMap k f
-> Maybe (f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip Maybe (f v)
forall a. Maybe a
Nothing DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> let !(Triple' lt :: DMap k f
lt z :: Maybe (f v)
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
go DMap k f
l in DMap k f
-> Maybe (f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
lt Maybe (f v)
z (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
gt DMap k f
r)
GGT -> let !(Triple' lt :: DMap k f
lt z :: Maybe (f v)
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
go DMap k f
r in DMap k f
-> Maybe (f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l DMap k f
lt) Maybe (f v)
z DMap k f
gt
GEQ -> DMap k f
-> Maybe (f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
l (f v -> Maybe (f v)
forall a. a -> Maybe a
Just f v
x) DMap k f
r
splitMember :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, Bool, DMap k f)
splitMember :: k v -> DMap k f -> (DMap k f, Bool, DMap k f)
splitMember k :: k v
k = Triple' (DMap k f) Bool (DMap k f) -> (DMap k f, Bool, DMap k f)
forall a b c. Triple' a b c -> (a, b, c)
toTriple (Triple' (DMap k f) Bool (DMap k f) -> (DMap k f, Bool, DMap k f))
-> (DMap k f -> Triple' (DMap k f) Bool (DMap k f))
-> DMap k f
-> (DMap k f, Bool, DMap k f)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap k f -> Triple' (DMap k f) Bool (DMap k f)
go
where
go :: DMap k f -> Triple' (DMap k f) Bool (DMap k f)
go :: DMap k f -> Triple' (DMap k f) Bool (DMap k f)
go Tip = DMap k f -> Bool -> DMap k f -> Triple' (DMap k f) Bool (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip Bool
False DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> let !(Triple' lt :: DMap k f
lt z :: Bool
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) Bool (DMap k f)
go DMap k f
l in DMap k f -> Bool -> DMap k f -> Triple' (DMap k f) Bool (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
lt Bool
z (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
gt DMap k f
r)
GGT -> let !(Triple' lt :: DMap k f
lt z :: Bool
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) Bool (DMap k f)
go DMap k f
r in DMap k f -> Bool -> DMap k f -> Triple' (DMap k f) Bool (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l DMap k f
lt) Bool
z DMap k f
gt
GEQ -> DMap k f -> Bool -> DMap k f -> Triple' (DMap k f) Bool (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
l Bool
True DMap k f
r
splitLookupWithKey :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, Maybe (k v, f v), DMap k f)
splitLookupWithKey :: k v -> DMap k f -> (DMap k f, Maybe (k v, f v), DMap k f)
splitLookupWithKey k :: k v
k = Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
-> (DMap k f, Maybe (k v, f v), DMap k f)
forall a b c. Triple' a b c -> (a, b, c)
toTriple (Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
-> (DMap k f, Maybe (k v, f v), DMap k f))
-> (DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f))
-> DMap k f
-> (DMap k f, Maybe (k v, f v), DMap k f)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
go
where
go :: DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
go :: DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
go Tip = DMap k f
-> Maybe (k v, f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip Maybe (k v, f v)
forall a. Maybe a
Nothing DMap k f
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
Tip
go (Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r) = case k v -> k v -> GOrdering v v
forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
gcompare k v
k k v
kx of
GLT -> let !(Triple' lt :: DMap k f
lt z :: Maybe (k v, f v)
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
go DMap k f
l in DMap k f
-> Maybe (k v, f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
lt Maybe (k v, f v)
z (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
gt DMap k f
r)
GGT -> let !(Triple' lt :: DMap k f
lt z :: Maybe (k v, f v)
z gt :: DMap k f
gt) = DMap k f -> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
go DMap k f
r in DMap k f
-> Maybe (k v, f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' (k v -> f v -> DMap k f -> DMap k f -> DMap k f
forall k1 (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
GCompare k2 =>
k2 v -> f v -> DMap k2 f -> DMap k2 f -> DMap k2 f
combine k v
kx f v
x DMap k f
l DMap k f
lt) Maybe (k v, f v)
z DMap k f
gt
GEQ -> DMap k f
-> Maybe (k v, f v)
-> DMap k f
-> Triple' (DMap k f) (Maybe (k v, f v)) (DMap k f)
forall a b c. a -> b -> c -> Triple' a b c
Triple' DMap k f
l ((k v, f v) -> Maybe (k v, f v)
forall a. a -> Maybe a
Just (k v
kx, f v
x)) DMap k f
r
instance (GEq k, Has' Eq k f) => Eq (DMap k f) where
t1 :: DMap k f
t1 == :: DMap k f -> DMap k f -> Bool
== t2 :: DMap k f
t2 = (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t2) Bool -> Bool -> Bool
&& (DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toAscList DMap k f
t1 [DSum k f] -> [DSum k f] -> Bool
forall a. Eq a => a -> a -> Bool
== DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toAscList DMap k f
t2)
instance (GCompare k, Has' Eq k f, Has' Ord k f) => Ord (DMap k f) where
compare :: DMap k f -> DMap k f -> Ordering
compare m1 :: DMap k f
m1 m2 :: DMap k f
m2 = [DSum k f] -> [DSum k f] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toAscList DMap k f
m1) (DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toAscList DMap k f
m2)
instance (GCompare k, GRead k, Has' Read k f) => Read (DMap k f) where
readPrec :: ReadPrec (DMap k f)
readPrec = ReadPrec (DMap k f) -> ReadPrec (DMap k f)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (DMap k f) -> ReadPrec (DMap k f))
-> ReadPrec (DMap k f) -> ReadPrec (DMap k f)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (DMap k f) -> ReadPrec (DMap k f)
forall a. Int -> ReadPrec a -> ReadPrec a
prec 10 (ReadPrec (DMap k f) -> ReadPrec (DMap k f))
-> ReadPrec (DMap k f) -> ReadPrec (DMap k f)
forall a b. (a -> b) -> a -> b
$ do
Ident "fromList" <- ReadPrec Lexeme
lexP
[DSum k f]
xs <- ReadPrec [DSum k f]
forall a. Read a => ReadPrec a
readPrec
DMap k f -> ReadPrec (DMap k f)
forall (m :: * -> *) a. Monad m => a -> m a
return ([DSum k f] -> DMap k f
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
[DSum k f] -> DMap k f
fromList [DSum k f]
xs)
readListPrec :: ReadPrec [DMap k f]
readListPrec = ReadPrec [DMap k f]
forall a. Read a => ReadPrec [a]
readListPrecDefault
instance (GShow k, Has' Show k f) => Show (DMap k f) where
showsPrec :: Int -> DMap k f -> ShowS
showsPrec p :: Int
p m :: DMap k f
m = Bool -> ShowS -> ShowS
showParen (Int
pInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>10)
( [Char] -> ShowS
showString "fromList "
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [DSum k f] -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec 11 (DMap k f -> [DSum k f]
forall k (k :: k -> *) (f :: k -> *). DMap k f -> [DSum k f]
toList DMap k f
m)
)
showTree :: (GShow k, Has' Show k f) => DMap k f -> String
showTree :: DMap k f -> [Char]
showTree m :: DMap k f
m
= (forall (v :: k). k v -> f v -> [Char])
-> Bool -> Bool -> DMap k f -> [Char]
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> Bool -> DMap k f -> [Char]
showTreeWith forall (v :: k). k v -> f v -> [Char]
forall k' (k :: k' -> *) (f :: k' -> *) (v :: k').
(GShow k, Has' Show k f) =>
k v -> f v -> [Char]
showElem Bool
True Bool
False DMap k f
m
where
showElem :: (GShow k, Has' Show k f) => k v -> f v -> String
showElem :: k v -> f v -> [Char]
showElem k :: k v
k x :: f v
x = DSum k f -> [Char]
forall a. Show a => a -> [Char]
show (k v
k k v -> f v -> DSum k f
forall k (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> f v
x)
showTreeWith :: (forall v. k v -> f v -> String) -> Bool -> Bool -> DMap k f -> String
showTreeWith :: (forall (v :: k). k v -> f v -> [Char])
-> Bool -> Bool -> DMap k f -> [Char]
showTreeWith showelem :: forall (v :: k). k v -> f v -> [Char]
showelem hang :: Bool
hang wide :: Bool
wide t :: DMap k f
t
| Bool
hang = ((forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
showsTreeHang forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide [] DMap k f
t) ""
| Bool
otherwise = ((forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
showsTree forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide [] [] DMap k f
t) ""
showsTree :: (forall v. k v -> f v -> String) -> Bool -> [String] -> [String] -> DMap k f -> ShowS
showsTree :: (forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
showsTree showelem :: forall (v :: k). k v -> f v -> [Char]
showelem wide :: Bool
wide lbars :: [[Char]]
lbars rbars :: [[Char]]
rbars t :: DMap k f
t
= case DMap k f
t of
Tip -> [[Char]] -> ShowS
showsBars [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "|\n"
Bin _ kx :: k v
kx x :: f v
x Tip Tip
-> [[Char]] -> ShowS
showsBars [[Char]]
lbars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString (k v -> f v -> [Char]
forall (v :: k). k v -> f v -> [Char]
showelem k v
kx f v
x) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "\n"
Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r
-> (forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
showsTree forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
rbars) ([[Char]] -> [[Char]]
withEmpty [[Char]]
rbars) DMap k f
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
. [Char] -> ShowS
showString (k v -> f v -> [Char]
forall (v :: k). k v -> f v -> [Char]
showelem k v
kx f v
x) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "\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
.
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> [[Char]] -> DMap k f -> ShowS
showsTree forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
lbars) ([[Char]] -> [[Char]]
withBar [[Char]]
lbars) DMap k f
l
showsTreeHang :: (forall v. k v -> f v -> String) -> Bool -> [String] -> DMap k f -> ShowS
showsTreeHang :: (forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
showsTreeHang showelem :: forall (v :: k). k v -> f v -> [Char]
showelem wide :: Bool
wide bars :: [[Char]]
bars t :: DMap k f
t
= case DMap k f
t of
Tip -> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "|\n"
Bin _ kx :: k v
kx x :: f v
x Tip Tip
-> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString (k v -> f v -> [Char]
forall (v :: k). k v -> f v -> [Char]
showelem k v
kx f v
x) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "\n"
Bin _ kx :: k v
kx x :: f v
x l :: DMap k f
l r :: DMap k f
r
-> [[Char]] -> ShowS
showsBars [[Char]]
bars ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString (k v -> f v -> [Char]
forall (v :: k). k v -> f v -> [Char]
showelem k v
kx f v
x) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString "\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
.
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
showsTreeHang forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
bars) DMap k f
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
.
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
forall k (k :: k -> *) (f :: k -> *).
(forall (v :: k). k v -> f v -> [Char])
-> Bool -> [[Char]] -> DMap k f -> ShowS
showsTreeHang forall (v :: k). k v -> f v -> [Char]
showelem Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
bars) DMap k f
r
showWide :: Bool -> [String] -> String -> String
showWide :: Bool -> [[Char]] -> ShowS
showWide wide :: Bool
wide bars :: [[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 "|\n"
| Bool
otherwise = ShowS
forall a. a -> a
id
showsBars :: [String] -> ShowS
showsBars :: [[Char]] -> ShowS
showsBars bars :: [[Char]]
bars
= case [[Char]]
bars of
[] -> ShowS
forall a. a -> a
id
_ -> [Char] -> ShowS
showString ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse ([[Char]] -> [[Char]]
forall a. [a] -> [a]
tail [[Char]]
bars))) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> ShowS
showString [Char]
node
node :: String
node :: [Char]
node = "+--"
withBar, withEmpty :: [String] -> [String]
withBar :: [[Char]] -> [[Char]]
withBar bars :: [[Char]]
bars = "| "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars
withEmpty :: [[Char]] -> [[Char]]
withEmpty bars :: [[Char]]
bars = " "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars
valid :: GCompare k => DMap k f -> Bool
valid :: DMap k f -> Bool
valid t :: DMap k f
t
= DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Bool
balanced DMap k f
t Bool -> Bool -> Bool
&& DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
DMap k f -> Bool
ordered DMap k f
t Bool -> Bool -> Bool
&& DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Bool
validsize DMap k f
t
ordered :: GCompare k => DMap k f -> Bool
ordered :: DMap k f -> Bool
ordered t :: DMap k f
t
= (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
bounded (Bool -> Some k -> Bool
forall a b. a -> b -> a
const Bool
True) (Bool -> Some k -> Bool
forall a b. a -> b -> a
const Bool
True) DMap k f
t
where
bounded :: GCompare k => (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
bounded :: (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
bounded lo :: Some k -> Bool
lo hi :: Some k -> Bool
hi t' :: DMap k f
t'
= case DMap k f
t' of
Tip -> Bool
True
Bin _ kx :: k v
kx _ l :: DMap k f
l r :: DMap k f
r -> Some k -> Bool
lo (k v -> Some k
forall k (tag :: k -> *) (a :: k). tag a -> Some tag
mkSome k v
kx) Bool -> Bool -> Bool
&& Some k -> Bool
hi (k v -> Some k
forall k (tag :: k -> *) (a :: k). tag a -> Some tag
mkSome k v
kx) Bool -> Bool -> Bool
&& (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
bounded Some k -> Bool
lo (Some k -> Some k -> Bool
forall a. Ord a => a -> a -> Bool
< k v -> Some k
forall k (tag :: k -> *) (a :: k). tag a -> Some tag
mkSome k v
kx) DMap k f
l Bool -> Bool -> Bool
&& (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *).
GCompare k =>
(Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
bounded (Some k -> Some k -> Bool
forall a. Ord a => a -> a -> Bool
> k v -> Some k
forall k (tag :: k -> *) (a :: k). tag a -> Some tag
mkSome k v
kx) Some k -> Bool
hi DMap k f
r
balanced :: DMap k f -> Bool
balanced :: DMap k f -> Bool
balanced t :: DMap k f
t
= case DMap k f
t of
Tip -> Bool
True
Bin _ _ _ l :: DMap k f
l r :: DMap k f
r -> (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 1 Bool -> Bool -> Bool
|| (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
r Bool -> Bool -> Bool
&& DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
l)) Bool -> Bool -> Bool
&&
DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Bool
balanced DMap k f
l Bool -> Bool -> Bool
&& DMap k f -> Bool
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Bool
balanced DMap k f
r
validsize :: DMap k f -> Bool
validsize :: DMap k f -> Bool
validsize t :: DMap k f
t
= (DMap k f -> Maybe Int
forall k (k :: k -> *) (f :: k -> *). DMap k f -> Maybe Int
realsize DMap k f
t Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just (DMap k f -> Int
forall k1 (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f -> Int
size DMap k f
t))
where
realsize :: DMap k f -> Maybe Int
realsize t' :: DMap k f
t'
= case DMap k f
t' of
Tip -> Int -> Maybe Int
forall a. a -> Maybe a
Just 0
Bin sz :: Int
sz _ _ l :: DMap k f
l r :: DMap k f
r -> case (DMap k f -> Maybe Int
realsize DMap k f
l,DMap k f -> Maybe Int
realsize DMap k f
r) of
(Just n :: Int
n,Just m :: Int
m) | Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
+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
forall a. Maybe a
Nothing
foldlStrict :: (a -> b -> a) -> a -> [b] -> a
foldlStrict :: (a -> b -> a) -> a -> [b] -> a
foldlStrict f :: a -> b -> a
f = a -> [b] -> a
go
where
go :: a -> [b] -> a
go z :: a
z [] = a
z
go z :: a
z (x :: b
x:xs :: [b]
xs) = a
z a -> a -> a
forall a b. a -> b -> b
`seq` a -> [b] -> a
go (a -> b -> a
f a
z b
x) [b]
xs