{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE PatternGuards #-}
#if __GLASGOW_HASKELL__
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
#endif
#if defined(__GLASGOW_HASKELL__)
{-# LANGUAGE Trustworthy #-}
#endif
#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TypeFamilies #-}
#define USE_MAGIC_PROXY 1
#endif
#ifdef USE_MAGIC_PROXY
{-# LANGUAGE MagicHash #-}
#endif
{-# OPTIONS_HADDOCK not-home #-}
#include "containers.h"
#if !(WORD_SIZE_IN_BITS >= 61)
#define DEFINE_ALTERF_FALLBACK 1
#endif
module Data.Map.Internal (
Map(..)
, Size
, (!), (!?), (\\)
, null
, size
, member
, notMember
, lookup
, findWithDefault
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, empty
, singleton
, insert
, insertWith
, insertWithKey
, insertLookupWithKey
, delete
, adjust
, adjustWithKey
, update
, updateWithKey
, updateLookupWithKey
, alter
, alterF
, union
, unionWith
, unionWithKey
, unions
, unionsWith
, difference
, differenceWith
, differenceWithKey
, intersection
, intersectionWith
, intersectionWithKey
, disjoint
, SimpleWhenMissing
, SimpleWhenMatched
, runWhenMatched
, runWhenMissing
, merge
, zipWithMaybeMatched
, zipWithMatched
, mapMaybeMissing
, dropMissing
, preserveMissing
, preserveMissing'
, mapMissing
, filterMissing
, WhenMissing (..)
, WhenMatched (..)
, mergeA
, zipWithMaybeAMatched
, zipWithAMatched
, traverseMaybeMissing
, traverseMissing
, filterAMissing
, mergeWithKey
, map
, mapWithKey
, traverseWithKey
, traverseMaybeWithKey
, mapAccum
, mapAccumWithKey
, mapAccumRWithKey
, mapKeys
, mapKeysWith
, mapKeysMonotonic
, foldr
, foldl
, foldrWithKey
, foldlWithKey
, foldMapWithKey
, foldr'
, foldl'
, foldrWithKey'
, foldlWithKey'
, elems
, keys
, assocs
, keysSet
, fromSet
, toList
, fromList
, fromListWith
, fromListWithKey
, toAscList
, toDescList
, fromAscList
, fromAscListWith
, fromAscListWithKey
, fromDistinctAscList
, fromDescList
, fromDescListWith
, fromDescListWithKey
, fromDistinctDescList
, filter
, filterWithKey
, takeWhileAntitone
, dropWhileAntitone
, spanAntitone
, restrictKeys
, withoutKeys
, partition
, partitionWithKey
, mapMaybe
, mapMaybeWithKey
, mapEither
, mapEitherWithKey
, split
, splitLookup
, splitRoot
, isSubmapOf, isSubmapOfBy
, isProperSubmapOf, isProperSubmapOfBy
, lookupIndex
, findIndex
, elemAt
, updateAt
, deleteAt
, take
, drop
, splitAt
, lookupMin
, lookupMax
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, updateMin
, updateMax
, updateMinWithKey
, updateMaxWithKey
, minView
, maxView
, minViewWithKey
, maxViewWithKey
, AreWeStrict (..)
, atKeyImpl
#if __GLASGOW_HASKELL__ && MIN_VERSION_base(4,8,0)
, atKeyPlain
#endif
, bin
, balance
, balanceL
, balanceR
, delta
, insertMax
, link
, link2
, glue
, MaybeS(..)
, Identity(..)
, mapWhenMissing
, mapWhenMatched
, lmapWhenMissing
, contramapFirstWhenMatched
, contramapSecondWhenMatched
, mapGentlyWhenMissing
, mapGentlyWhenMatched
) where
#if MIN_VERSION_base(4,8,0)
import Data.Functor.Identity (Identity (..))
import Control.Applicative (liftA3)
#else
import Control.Applicative (Applicative(..), (<$>), liftA3)
import Data.Monoid (Monoid(..))
import Data.Traversable (Traversable(traverse))
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Classes
import Data.Semigroup (stimesIdempotentMonoid)
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup(stimes))
#endif
#if !(MIN_VERSION_base(4,11,0)) && MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup((<>)))
#endif
import Control.Applicative (Const (..))
import Control.DeepSeq (NFData(rnf))
import Data.Bits (shiftL, shiftR)
import qualified Data.Foldable as Foldable
#if !MIN_VERSION_base(4,8,0)
import Data.Foldable (Foldable())
#endif
import Data.Typeable
import Prelude hiding (lookup, map, filter, foldr, foldl, null, splitAt, take, drop)
import qualified Data.Set.Internal as Set
import Data.Set.Internal (Set)
import Utils.Containers.Internal.PtrEquality (ptrEq)
import Utils.Containers.Internal.StrictPair
import Utils.Containers.Internal.StrictMaybe
import Utils.Containers.Internal.BitQueue
#ifdef DEFINE_ALTERF_FALLBACK
import Utils.Containers.Internal.BitUtil (wordSize)
#endif
#if __GLASGOW_HASKELL__
import GHC.Exts (build, lazy)
#if !MIN_VERSION_base(4,8,0)
import Data.Functor ((<$))
#endif
#ifdef USE_MAGIC_PROXY
import GHC.Exts (Proxy#, proxy# )
#endif
#if __GLASGOW_HASKELL__ >= 708
import qualified GHC.Exts as GHCExts
#endif
import Text.Read hiding (lift)
import Data.Data
import qualified Control.Category as Category
#endif
#if __GLASGOW_HASKELL__ >= 708
import Data.Coerce
#endif
infixl 9 !,!?,\\
(!) :: Ord k => Map k a -> k -> a
(!) m :: Map k a
m k :: k
k = k -> Map k a -> a
forall k a. Ord k => k -> Map k a -> a
find k
k Map k a
m
#if __GLASGOW_HASKELL__
{-# INLINE (!) #-}
#endif
(!?) :: Ord k => Map k a -> k -> Maybe a
!? :: Map k a -> k -> Maybe a
(!?) m :: Map k a
m k :: k
k = k -> Map k a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
lookup k
k Map k a
m
#if __GLASGOW_HASKELL__
{-# INLINE (!?) #-}
#endif
(\\) :: Ord k => Map k a -> Map k b -> Map k a
m1 :: Map k a
m1 \\ :: Map k a -> Map k b -> Map k a
\\ m2 :: Map k b
m2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
difference Map k a
m1 Map k b
m2
#if __GLASGOW_HASKELL__
{-# INLINE (\\) #-}
#endif
data Map k a = Bin {-# UNPACK #-} !Size !k a !(Map k a) !(Map k a)
| Tip
type Size = Int
#if __GLASGOW_HASKELL__ >= 708
type role Map nominal representational
#endif
instance (Ord k) => Monoid (Map k v) where
mempty :: Map k v
mempty = Map k v
forall k a. Map k a
empty
mconcat :: [Map k v] -> Map k v
mconcat = [Map k v] -> Map k v
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
f (Map k a) -> Map k a
unions
#if !(MIN_VERSION_base(4,9,0))
mappend = union
#else
mappend :: Map k v -> Map k v -> Map k v
mappend = Map k v -> Map k v -> Map k v
forall a. Semigroup a => a -> a -> a
(<>)
instance (Ord k) => Semigroup (Map k v) where
<> :: Map k v -> Map k v -> Map k v
(<>) = Map k v -> Map k v -> Map k v
forall k v. Ord k => Map k v -> Map k v -> Map k v
union
stimes :: b -> Map k v -> Map k v
stimes = b -> Map k v -> Map k v
forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid
#endif
#if __GLASGOW_HASKELL__
instance (Data k, Data a, Ord k) => Data (Map k a) where
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k a -> c (Map k a)
gfoldl f :: forall d b. Data d => c (d -> b) -> d -> c b
f z :: forall g. g -> c g
z m :: Map k a
m = ([(k, a)] -> Map k a) -> c ([(k, a)] -> Map k a)
forall g. g -> c g
z [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
fromList c ([(k, a)] -> Map k a) -> [(k, a)] -> c (Map k a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toList Map k a
m
toConstr :: Map k a -> Constr
toConstr _ = Constr
fromListConstr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k a)
gunfold k :: forall b r. Data b => c (b -> r) -> c r
k z :: forall r. r -> c r
z c :: Constr
c = case Constr -> Int
constrIndex Constr
c of
1 -> c ([(k, a)] -> Map k a) -> c (Map k a)
forall b r. Data b => c (b -> r) -> c r
k (([(k, a)] -> Map k a) -> c ([(k, a)] -> Map k a)
forall r. r -> c r
z [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
fromList)
_ -> [Char] -> c (Map k a)
forall a. HasCallStack => [Char] -> a
error "gunfold"
dataTypeOf :: Map k a -> DataType
dataTypeOf _ = DataType
mapDataType
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k a))
dataCast2 f :: forall d e. (Data d, Data e) => c (t d e)
f = c (t k a) -> Maybe (c (Map k a))
forall k1 k2 k3 (c :: k1 -> *) (t :: k2 -> k3 -> k1)
(t' :: k2 -> k3 -> k1) (a :: k2) (b :: k3).
(Typeable t, Typeable t') =>
c (t a b) -> Maybe (c (t' a b))
gcast2 c (t k a)
forall d e. (Data d, Data e) => c (t d e)
f
fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> [Char] -> [[Char]] -> Fixity -> Constr
mkConstr DataType
mapDataType "fromList" [] Fixity
Prefix
mapDataType :: DataType
mapDataType :: DataType
mapDataType = [Char] -> [Constr] -> DataType
mkDataType "Data.Map.Internal.Map" [Constr
fromListConstr]
#endif
null :: Map k a -> Bool
null :: Map k a -> Bool
null Tip = Bool
True
null (Bin {}) = Bool
False
{-# INLINE null #-}
size :: Map k a -> Int
size :: Map k a -> Int
size Tip = 0
size (Bin sz :: Int
sz _ _ _ _) = Int
sz
{-# INLINE size #-}
lookup :: Ord k => k -> Map k a -> Maybe a
lookup :: k -> Map k a -> Maybe a
lookup = k -> Map k a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
go
where
go :: t -> Map t a -> Maybe a
go !t
_ Tip = Maybe a
forall a. Maybe a
Nothing
go k :: t
k (Bin _ kx :: t
kx x :: a
x l :: Map t a
l r :: Map t a
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of
LT -> t -> Map t a -> Maybe a
go t
k Map t a
l
GT -> t -> Map t a -> Maybe a
go t
k Map t a
r
EQ -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
#if __GLASGOW_HASKELL__
{-# INLINABLE lookup #-}
#else
{-# INLINE lookup #-}
#endif
member :: Ord k => k -> Map k a -> Bool
member :: k -> Map k a -> Bool
member = k -> Map k a -> Bool
forall t a. Ord t => t -> Map t a -> Bool
go
where
go :: t -> Map t a -> Bool
go !t
_ Tip = Bool
False
go k :: t
k (Bin _ kx :: t
kx _ l :: Map t a
l r :: Map t a
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of
LT -> t -> Map t a -> Bool
go t
k Map t a
l
GT -> t -> Map t a -> Bool
go t
k Map t a
r
EQ -> Bool
True
#if __GLASGOW_HASKELL__
{-# INLINABLE member #-}
#else
{-# INLINE member #-}
#endif
notMember :: Ord k => k -> Map k a -> Bool
notMember :: k -> Map k a -> Bool
notMember k :: k
k m :: Map k a
m = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ k -> Map k a -> Bool
forall t a. Ord t => t -> Map t a -> Bool
member k
k Map k a
m
#if __GLASGOW_HASKELL__
{-# INLINABLE notMember #-}
#else
{-# INLINE notMember #-}
#endif
find :: Ord k => k -> Map k a -> a
find :: k -> Map k a -> a
find = k -> Map k a -> a
forall k a. Ord k => k -> Map k a -> a
go
where
go :: t -> Map t p -> p
go !t
_ Tip = [Char] -> p
forall a. HasCallStack => [Char] -> a
error "Map.!: given key is not an element in the map"
go k :: t
k (Bin _ kx :: t
kx x :: p
x l :: Map t p
l r :: Map t p
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of
LT -> t -> Map t p -> p
go t
k Map t p
l
GT -> t -> Map t p -> p
go t
k Map t p
r
EQ -> p
x
#if __GLASGOW_HASKELL__
{-# INLINABLE find #-}
#else
{-# INLINE find #-}
#endif
findWithDefault :: Ord k => a -> k -> Map k a -> a
findWithDefault :: a -> k -> Map k a -> a
findWithDefault = a -> k -> Map k a -> a
forall t t. Ord t => t -> t -> Map t t -> t
go
where
go :: t -> t -> Map t t -> t
go def :: t
def !t
_ Tip = t
def
go def :: t
def k :: t
k (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of
LT -> t -> t -> Map t t -> t
go t
def t
k Map t t
l
GT -> t -> t -> Map t t -> t
go t
def t
k Map t t
r
EQ -> t
x
#if __GLASGOW_HASKELL__
{-# INLINABLE findWithDefault #-}
#else
{-# INLINE findWithDefault #-}
#endif
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)
lookupLT :: k -> Map k v -> Maybe (k, v)
lookupLT = k -> Map k v -> Maybe (k, v)
forall t t. Ord t => t -> Map t t -> Maybe (t, t)
goNothing
where
goNothing :: t -> Map t t -> Maybe (t, t)
goNothing !t
_ Tip = Maybe (t, t)
forall a. Maybe a
Nothing
goNothing k :: t
k (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) | t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
kx = t -> Map t t -> Maybe (t, t)
goNothing t
k Map t t
l
| Bool
otherwise = t -> t -> t -> Map t t -> Maybe (t, t)
forall t t. Ord t => t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
r
goJust :: t -> t -> t -> Map t t -> Maybe (t, t)
goJust !t
_ kx' :: t
kx' x' :: t
x' Tip = (t, t) -> Maybe (t, t)
forall a. a -> Maybe a
Just (t
kx', t
x')
goJust k :: t
k kx' :: t
kx' x' :: t
x' (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) | t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
kx = t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx' t
x' Map t t
l
| Bool
otherwise = t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
r
#if __GLASGOW_HASKELL__
{-# INLINABLE lookupLT #-}
#else
{-# INLINE lookupLT #-}
#endif
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)
lookupGT :: k -> Map k v -> Maybe (k, v)
lookupGT = k -> Map k v -> Maybe (k, v)
forall t t. Ord t => t -> Map t t -> Maybe (t, t)
goNothing
where
goNothing :: t -> Map t t -> Maybe (t, t)
goNothing !t
_ Tip = Maybe (t, t)
forall a. Maybe a
Nothing
goNothing k :: t
k (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) | t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
kx = t -> t -> t -> Map t t -> Maybe (t, t)
forall t t. Ord t => t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
l
| Bool
otherwise = t -> Map t t -> Maybe (t, t)
goNothing t
k Map t t
r
goJust :: t -> t -> t -> Map t t -> Maybe (t, t)
goJust !t
_ kx' :: t
kx' x' :: t
x' Tip = (t, t) -> Maybe (t, t)
forall a. a -> Maybe a
Just (t
kx', t
x')
goJust k :: t
k kx' :: t
kx' x' :: t
x' (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) | t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
kx = t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
l
| Bool
otherwise = t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx' t
x' Map t t
r
#if __GLASGOW_HASKELL__
{-# INLINABLE lookupGT #-}
#else
{-# INLINE lookupGT #-}
#endif
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)
lookupLE :: k -> Map k v -> Maybe (k, v)
lookupLE = k -> Map k v -> Maybe (k, v)
forall t t. Ord t => t -> Map t t -> Maybe (t, t)
goNothing
where
goNothing :: t -> Map t t -> Maybe (t, t)
goNothing !t
_ Tip = Maybe (t, t)
forall a. Maybe a
Nothing
goNothing k :: t
k (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of LT -> t -> Map t t -> Maybe (t, t)
goNothing t
k Map t t
l
EQ -> (t, t) -> Maybe (t, t)
forall a. a -> Maybe a
Just (t
kx, t
x)
GT -> t -> t -> t -> Map t t -> Maybe (t, t)
forall t t. Ord t => t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
r
goJust :: t -> t -> t -> Map t t -> Maybe (t, t)
goJust !t
_ kx' :: t
kx' x' :: t
x' Tip = (t, t) -> Maybe (t, t)
forall a. a -> Maybe a
Just (t
kx', t
x')
goJust k :: t
k kx' :: t
kx' x' :: t
x' (Bin _ kx :: t
kx x :: t
x l :: Map t t
l r :: Map t t
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of LT -> t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx' t
x' Map t t
l
EQ -> (t, t) -> Maybe (t, t)
forall a. a -> Maybe a
Just (t
kx, t
x)
GT -> t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx t
x Map t t
r
#if __GLASGOW_HASKELL__
{-# INLINABLE lookupLE #-}
#else
{-# INLINE lookupLE #-}
#endif
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)
lookupGE :: k -> Map k v -> Maybe (k, v)
lookupGE = k -> Map k v -> Maybe (k, v)
forall t t. Ord t => t -> Map t t -> Maybe (t, t)
goNothing
where
goNothing :: t -> Map t b -> Maybe (t, b)
goNothing !t
_ Tip = Maybe (t, b)
forall a. Maybe a
Nothing
goNothing k :: t
k (Bin _ kx :: t
kx x :: b
x l :: Map t b
l r :: Map t b
r) = case t -> t -> Ordering
forall a. Ord a => a -> a -> Ordering
compare t
k t
kx of LT -> t -> t -> b -> Map t b -> Maybe (t, b)
forall t t. Ord t => t -> t -> t -> Map t t -> Maybe (t, t)
goJust t
k t
kx b
x Map t b
l
EQ -> (t, b) -> Maybe (t, b)
forall a. a -> Maybe a
Just (t
kx, b
x)
GT -> t -> Map t b -> Maybe (t, b)
goNothing t
k Map t b
r
goJust :: a -> a -> b -> Map a b -> Maybe (a, b)
goJust !a
_ kx' :: a
kx' x' :: b
x' Tip = (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
kx', b
x')
goJust k :: a
k kx' :: a
kx' x' :: b
x' (Bin _ kx :: a
kx x :: b
x l :: Map a b
l r :: Map a b
r) = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
k a
kx of LT -> a -> a -> b -> Map a b -> Maybe (a, b)
goJust a
k a
kx b
x Map a b
l
EQ -> (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
kx, b
x)
GT -> a -> a -> b -> Map a b -> Maybe (a, b)
goJust a
k a
kx' b
x' Map a b
r
#if __GLASGOW_HASKELL__
{-# INLINABLE lookupGE #-}
#else
{-# INLINE lookupGE #-}
#endif
empty :: Map k a
empty :: Map k a
empty = Map k a
forall k a. Map k a
Tip
{-# INLINE empty #-}
singleton :: k -> a -> Map k a
singleton :: k -> a -> Map k a
singleton k :: k
k x :: a
x = Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip
{-# INLINE singleton #-}
insert :: Ord k => k -> a -> Map k a -> Map k a
insert :: k -> a -> Map k a -> Map k a
insert kx0 :: k
kx0 = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
kx0 k
kx0
where
go :: Ord k => k -> k -> a -> Map k a -> Map k a
go :: k -> k -> a -> Map k a -> Map k a
go orig :: k
orig !k
_ x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton (k -> k
forall a. a -> a
lazy k
orig) a
x
go orig :: k
orig !k
kx x :: a
x t :: Map k a
t@(Bin sz :: Int
sz ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT | Map k a
l' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y Map k a
l' Map k a
r
where !l' :: Map k a
l' = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
orig k
kx a
x Map k a
l
GT | Map k a
r' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l Map k a
r'
where !r' :: Map k a
r' = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
orig k
kx a
x Map k a
r
EQ | a
x a -> a -> Bool
forall a. a -> a -> Bool
`ptrEq` a
y Bool -> Bool -> Bool
&& (k -> k
forall a. a -> a
lazy k
orig k -> Bool -> Bool
forall a b. a -> b -> b
`seq` (k
orig k -> k -> Bool
forall a. a -> a -> Bool
`ptrEq` k
ky)) -> Map k a
t
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz (k -> k
forall a. a -> a
lazy k
orig) a
x Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE insert #-}
#else
{-# INLINE insert #-}
#endif
#ifndef __GLASGOW_HASKELL__
lazy :: a -> a
lazy a = a
#endif
insertR :: Ord k => k -> a -> Map k a -> Map k a
insertR :: k -> a -> Map k a -> Map k a
insertR kx0 :: k
kx0 = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
kx0 k
kx0
where
go :: Ord k => k -> k -> a -> Map k a -> Map k a
go :: k -> k -> a -> Map k a -> Map k a
go orig :: k
orig !k
_ x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton (k -> k
forall a. a -> a
lazy k
orig) a
x
go orig :: k
orig !k
kx x :: a
x t :: Map k a
t@(Bin _ ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT | Map k a
l' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y Map k a
l' Map k a
r
where !l' :: Map k a
l' = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
orig k
kx a
x Map k a
l
GT | Map k a
r' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l Map k a
r'
where !r' :: Map k a
r' = k -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> k -> a -> Map k a -> Map k a
go k
orig k
kx a
x Map k a
r
EQ -> Map k a
t
#if __GLASGOW_HASKELL__
{-# INLINABLE insertR #-}
#else
{-# INLINE insertR #-}
#endif
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith :: (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith = (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go
where
go :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go :: (a -> a -> a) -> k -> a -> Map k a -> Map k a
go _ !k
kx x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
go f :: a -> a -> a
f !k
kx x :: a
x (Bin sy :: Int
sy ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y ((a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go a -> a -> a
f k
kx a
x Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l ((a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go a -> a -> a
f k
kx a
x Map k a
r)
EQ -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sy k
kx (a -> a -> a
f a
x a
y) Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE insertWith #-}
#else
{-# INLINE insertWith #-}
#endif
insertWithR :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithR :: (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithR = (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go
where
go :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go :: (a -> a -> a) -> k -> a -> Map k a -> Map k a
go _ !k
kx x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
go f :: a -> a -> a
f !k
kx x :: a
x (Bin sy :: Int
sy ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y ((a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go a -> a -> a
f k
kx a
x Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l ((a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
go a -> a -> a
f k
kx a
x Map k a
r)
EQ -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sy k
ky (a -> a -> a
f a
y a
x) Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE insertWithR #-}
#else
{-# INLINE insertWithR #-}
#endif
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey :: (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey = (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go
where
go :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go :: (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go _ !k
kx x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
go f :: k -> a -> a -> a
f kx :: k
kx x :: a
x (Bin sy :: Int
sy ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y ((k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go k -> a -> a -> a
f k
kx a
x Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l ((k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go k -> a -> a -> a
f k
kx a
x Map k a
r)
EQ -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sy k
kx (k -> a -> a -> a
f k
kx a
x a
y) Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE insertWithKey #-}
#else
{-# INLINE insertWithKey #-}
#endif
insertWithKeyR :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKeyR :: (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKeyR = (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go
where
go :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go :: (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go _ !k
kx x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
go f :: k -> a -> a -> a
f kx :: k
kx x :: a
x (Bin sy :: Int
sy ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y ((k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go k -> a -> a -> a
f k
kx a
x Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l ((k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
go k -> a -> a -> a
f k
kx a
x Map k a
r)
EQ -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sy k
ky (k -> a -> a -> a
f k
ky a
y a
x) Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE insertWithKeyR #-}
#else
{-# INLINE insertWithKeyR #-}
#endif
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a
-> (Maybe a, Map k a)
insertLookupWithKey :: (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
insertLookupWithKey f0 :: k -> a -> a -> a
f0 k0 :: k
k0 x0 :: a
x0 = StrictPair (Maybe a) (Map k a) -> (Maybe a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Maybe a) (Map k a) -> (Maybe a, Map k a))
-> (Map k a -> StrictPair (Maybe a) (Map k a))
-> Map k a
-> (Maybe a, Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> a -> a
f0 k
k0 a
x0
where
go :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
go :: (k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
go _ !k
kx x :: a
x Tip = (Maybe a
forall a. Maybe a
Nothing Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x)
go f :: k -> a -> a -> a
f kx :: k
kx x :: a
x (Bin sy :: Int
sy ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
kx k
ky of
LT -> let !(found :: Maybe a
found :*: l' :: Map k a
l') = (k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> a -> a
f k
kx a
x Map k a
l
!t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y Map k a
l' Map k a
r
in (Maybe a
found Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
t')
GT -> let !(found :: Maybe a
found :*: r' :: Map k a
r') = (k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> a -> a)
-> k -> a -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> a -> a
f k
kx a
x Map k a
r
!t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l Map k a
r'
in (Maybe a
found Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
t')
EQ -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sy k
kx (k -> a -> a -> a
f k
kx a
x a
y) Map k a
l Map k a
r)
#if __GLASGOW_HASKELL__
{-# INLINABLE insertLookupWithKey #-}
#else
{-# INLINE insertLookupWithKey #-}
#endif
delete :: Ord k => k -> Map k a -> Map k a
delete :: k -> Map k a -> Map k a
delete = k -> Map k a -> Map k a
forall k a. Ord k => k -> Map k a -> Map k a
go
where
go :: Ord k => k -> Map k a -> Map k a
go :: k -> Map k a -> Map k a
go !k
_ Tip = Map k a
forall k a. Map k a
Tip
go k :: k
k t :: Map k a
t@(Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT | Map k a
l' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x Map k a
l' Map k a
r
where !l' :: Map k a
l' = k -> Map k a -> Map k a
forall k a. Ord k => k -> Map k a -> Map k a
go k
k Map k a
l
GT | Map k a
r' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r -> Map k a
t
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l Map k a
r'
where !r' :: Map k a
r' = k -> Map k a -> Map k a
forall k a. Ord k => k -> Map k a -> Map k a
go k
k Map k a
r
EQ -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE delete #-}
#else
{-# INLINE delete #-}
#endif
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a
adjust :: (a -> a) -> k -> Map k a -> Map k a
adjust f :: a -> a
f = (k -> a -> a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey (\_ x :: a
x -> a -> a
f a
x)
#if __GLASGOW_HASKELL__
{-# INLINABLE adjust #-}
#else
{-# INLINE adjust #-}
#endif
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey :: (k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey = (k -> a -> a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
go
where
go :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
go :: (k -> a -> a) -> k -> Map k a -> Map k a
go _ !k
_ Tip = Map k a
forall k a. Map k a
Tip
go f :: k -> a -> a
f k :: k
k (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x ((k -> a -> a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
go k -> a -> a
f k
k Map k a
l) Map k a
r
GT -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x Map k a
l ((k -> a -> a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
go k -> a -> a
f k
k Map k a
r)
EQ -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx (k -> a -> a
f k
kx a
x) Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE adjustWithKey #-}
#else
{-# INLINE adjustWithKey #-}
#endif
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
update :: (a -> Maybe a) -> k -> Map k a -> Map k a
update f :: a -> Maybe a
f = (k -> a -> Maybe a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey (\_ x :: a
x -> a -> Maybe a
f a
x)
#if __GLASGOW_HASKELL__
{-# INLINABLE update #-}
#else
{-# INLINE update #-}
#endif
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey :: (k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey = (k -> a -> Maybe a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
go
where
go :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
go :: (k -> a -> Maybe a) -> k -> Map k a -> Map k a
go _ !k
_ Tip = Map k a
forall k a. Map k a
Tip
go f :: k -> a -> Maybe a
f k :: k
k(Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x ((k -> a -> Maybe a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
go k -> a -> Maybe a
f k
k Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l ((k -> a -> Maybe a) -> k -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
go k -> a -> Maybe a
f k
k Map k a
r)
EQ -> case k -> a -> Maybe a
f k
kx a
x of
Just x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r
Nothing -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE updateWithKey #-}
#else
{-# INLINE updateWithKey #-}
#endif
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a,Map k a)
updateLookupWithKey :: (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
updateLookupWithKey f0 :: k -> a -> Maybe a
f0 k0 :: k
k0 = StrictPair (Maybe a) (Map k a) -> (Maybe a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Maybe a) (Map k a) -> (Maybe a, Map k a))
-> (Map k a -> StrictPair (Maybe a) (Map k a))
-> Map k a
-> (Maybe a, Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> Maybe a
f0 k
k0
where
go :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> StrictPair (Maybe a) (Map k a)
go :: (k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
go _ !k
_ Tip = (Maybe a
forall a. Maybe a
Nothing Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip)
go f :: k -> a -> Maybe a
f k :: k
k (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> let !(found :: Maybe a
found :*: l' :: Map k a
l') = (k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> Maybe a
f k
k Map k a
l
!t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x Map k a
l' Map k a
r
in (Maybe a
found Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
t')
GT -> let !(found :: Maybe a
found :*: r' :: Map k a
r') = (k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
forall k a.
Ord k =>
(k -> a -> Maybe a)
-> k -> Map k a -> StrictPair (Maybe a) (Map k a)
go k -> a -> Maybe a
f k
k Map k a
r
!t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l Map k a
r'
in (Maybe a
found Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
t')
EQ -> case k -> a -> Maybe a
f k
kx a
x of
Just x' :: a
x' -> (a -> Maybe a
forall a. a -> Maybe a
Just a
x' Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r)
Nothing -> let !glued :: Map k a
glued = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
in (a -> Maybe a
forall a. a -> Maybe a
Just a
x Maybe a -> Map k a -> StrictPair (Maybe a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
glued)
#if __GLASGOW_HASKELL__
{-# INLINABLE updateLookupWithKey #-}
#else
{-# INLINE updateLookupWithKey #-}
#endif
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
alter :: (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
alter = (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
go
where
go :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
go :: (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
go f :: Maybe a -> Maybe a
f !k
k Tip = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
Nothing -> Map k a
forall k a. Map k a
Tip
Just x :: a
x -> k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
k a
x
go f :: Maybe a -> Maybe a
f k :: k
k (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balance k
kx a
x ((Maybe a -> Maybe a) -> k -> Map k a -> Map k a
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
go Maybe a -> Maybe a
f k
k Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balance k
kx a
x Map k a
l ((Maybe a -> Maybe a) -> k -> Map k a -> Map k a
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
go Maybe a -> Maybe a
f k
k Map k a
r)
EQ -> case Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
x) of
Just x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r
Nothing -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE alter #-}
#else
{-# INLINE alter #-}
#endif
data AreWeStrict = Strict | Lazy
alterF :: (Functor f, Ord k)
=> (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
alterF :: (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
alterF f :: Maybe a -> f (Maybe a)
f k :: k
k m :: Map k a
m = AreWeStrict
-> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
AreWeStrict
-> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
atKeyImpl AreWeStrict
Lazy k
k Maybe a -> f (Maybe a)
f Map k a
m
#ifndef __GLASGOW_HASKELL__
{-# INLINE alterF #-}
#else
{-# INLINABLE [2] alterF #-}
{-# RULES
"alterF/Const" forall k (f :: Maybe a -> Const b (Maybe a)) . alterF f k = \m -> Const . getConst . f $ lookup k m
#-}
#if MIN_VERSION_base(4,8,0)
{-# RULES
"alterF/Identity" forall k f . alterF f k = atKeyIdentity k f
#-}
#endif
#endif
atKeyImpl :: (Functor f, Ord k) =>
AreWeStrict -> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
#ifdef DEFINE_ALTERF_FALLBACK
atKeyImpl strict !k f m
| wordSize < 61 && size m >= alterFCutoff = alterFFallback strict k f m
#endif
atKeyImpl :: AreWeStrict
-> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
atKeyImpl strict :: AreWeStrict
strict !k
k f :: Maybe a -> f (Maybe a)
f m :: Map k a
m = case k -> Map k a -> TraceResult a
forall k a. Ord k => k -> Map k a -> TraceResult a
lookupTrace k
k Map k a
m of
TraceResult mv :: Maybe a
mv q :: BitQueue
q -> ((Maybe a -> Map k a) -> f (Maybe a) -> f (Map k a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f (Maybe a)
f Maybe a
mv) ((Maybe a -> Map k a) -> f (Map k a))
-> (Maybe a -> Map k a) -> f (Map k a)
forall a b. (a -> b) -> a -> b
$ \ fres :: Maybe a
fres ->
case Maybe a
fres of
Nothing -> case Maybe a
mv of
Nothing -> Map k a
m
Just old :: a
old -> a -> BitQueue -> Map k a -> Map k a
forall any k a. any -> BitQueue -> Map k a -> Map k a
deleteAlong a
old BitQueue
q Map k a
m
Just new :: a
new -> case AreWeStrict
strict of
Strict -> a
new a -> Map k a -> Map k a
forall a b. a -> b -> b
`seq` case Maybe a
mv of
Nothing -> BitQueue -> k -> a -> Map k a -> Map k a
forall k a. BitQueue -> k -> a -> Map k a -> Map k a
insertAlong BitQueue
q k
k a
new Map k a
m
Just _ -> BitQueue -> a -> Map k a -> Map k a
forall a k. BitQueue -> a -> Map k a -> Map k a
replaceAlong BitQueue
q a
new Map k a
m
Lazy -> case Maybe a
mv of
Nothing -> BitQueue -> k -> a -> Map k a -> Map k a
forall k a. BitQueue -> k -> a -> Map k a -> Map k a
insertAlong BitQueue
q k
k a
new Map k a
m
Just _ -> BitQueue -> a -> Map k a -> Map k a
forall a k. BitQueue -> a -> Map k a -> Map k a
replaceAlong BitQueue
q a
new Map k a
m
{-# INLINE atKeyImpl #-}
#ifdef DEFINE_ALTERF_FALLBACK
alterFCutoff :: Int
#if WORD_SIZE_IN_BITS == 32
alterFCutoff = 55744454
#else
alterFCutoff = case wordSize of
30 -> 17637893
31 -> 31356255
32 -> 55744454
x -> (4^(x*2-2)) `quot` (3^(x*2-2))
#endif
#endif
data TraceResult a = TraceResult (Maybe a) {-# UNPACK #-} !BitQueue
lookupTrace :: Ord k => k -> Map k a -> TraceResult a
lookupTrace :: k -> Map k a -> TraceResult a
lookupTrace = BitQueueB -> k -> Map k a -> TraceResult a
forall k a. Ord k => BitQueueB -> k -> Map k a -> TraceResult a
go BitQueueB
emptyQB
where
go :: Ord k => BitQueueB -> k -> Map k a -> TraceResult a
go :: BitQueueB -> k -> Map k a -> TraceResult a
go !BitQueueB
q !k
_ Tip = Maybe a -> BitQueue -> TraceResult a
forall a. Maybe a -> BitQueue -> TraceResult a
TraceResult Maybe a
forall a. Maybe a
Nothing (BitQueueB -> BitQueue
buildQ BitQueueB
q)
go q :: BitQueueB
q k :: k
k (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> (BitQueueB -> k -> Map k a -> TraceResult a
forall k a. Ord k => BitQueueB -> k -> Map k a -> TraceResult a
go (BitQueueB -> k -> Map k a -> TraceResult a)
-> BitQueueB -> k -> Map k a -> TraceResult a
forall a b. (a -> b) -> a -> b
$! BitQueueB
q BitQueueB -> Bool -> BitQueueB
`snocQB` Bool
False) k
k Map k a
l
GT -> (BitQueueB -> k -> Map k a -> TraceResult a
forall k a. Ord k => BitQueueB -> k -> Map k a -> TraceResult a
go (BitQueueB -> k -> Map k a -> TraceResult a)
-> BitQueueB -> k -> Map k a -> TraceResult a
forall a b. (a -> b) -> a -> b
$! BitQueueB
q BitQueueB -> Bool -> BitQueueB
`snocQB` Bool
True) k
k Map k a
r
EQ -> Maybe a -> BitQueue -> TraceResult a
forall a. Maybe a -> BitQueue -> TraceResult a
TraceResult (a -> Maybe a
forall a. a -> Maybe a
Just a
x) (BitQueueB -> BitQueue
buildQ BitQueueB
q)
#if __GLASGOW_HASKELL__ >= 710
{-# INLINABLE lookupTrace #-}
#else
{-# INLINE lookupTrace #-}
#endif
insertAlong :: BitQueue -> k -> a -> Map k a -> Map k a
insertAlong :: BitQueue -> k -> a -> Map k a -> Map k a
insertAlong !BitQueue
_ kx :: k
kx x :: a
x Tip = k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
insertAlong q :: BitQueue
q kx :: k
kx x :: a
x (Bin sz :: Int
sz ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case BitQueue -> Maybe (Bool, BitQueue)
unconsQ BitQueue
q of
Just (False, tl :: BitQueue
tl) -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y (BitQueue -> k -> a -> Map k a -> Map k a
forall k a. BitQueue -> k -> a -> Map k a -> Map k a
insertAlong BitQueue
tl k
kx a
x Map k a
l) Map k a
r
Just (True,tl :: BitQueue
tl) -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l (BitQueue -> k -> a -> Map k a -> Map k a
forall k a. BitQueue -> k -> a -> Map k a -> Map k a
insertAlong BitQueue
tl k
kx a
x Map k a
r)
Nothing -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz k
kx a
x Map k a
l Map k a
r
deleteAlong :: any -> BitQueue -> Map k a -> Map k a
deleteAlong :: any -> BitQueue -> Map k a -> Map k a
deleteAlong old :: any
old !BitQueue
q0 !Map k a
m = Proxy# () -> BitQueue -> Map k a -> Map k a
forall k a. Proxy# () -> BitQueue -> Map k a -> Map k a
go (any -> Proxy# ()
forall a. a -> Proxy# ()
bogus any
old) BitQueue
q0 Map k a
m where
#ifdef USE_MAGIC_PROXY
go :: Proxy# () -> BitQueue -> Map k a -> Map k a
#else
go :: any -> BitQueue -> Map k a -> Map k a
#endif
go :: Proxy# () -> BitQueue -> Map k a -> Map k a
go !Proxy# ()
_ !BitQueue
_ Tip = Map k a
forall k a. Map k a
Tip
go foom :: Proxy# ()
foom q :: BitQueue
q (Bin _ ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case BitQueue -> Maybe (Bool, BitQueue)
unconsQ BitQueue
q of
Just (False, tl :: BitQueue
tl) -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y (Proxy# () -> BitQueue -> Map k a -> Map k a
forall k a. Proxy# () -> BitQueue -> Map k a -> Map k a
go Proxy# ()
foom BitQueue
tl Map k a
l) Map k a
r
Just (True, tl :: BitQueue
tl) -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y Map k a
l (Proxy# () -> BitQueue -> Map k a -> Map k a
forall k a. Proxy# () -> BitQueue -> Map k a -> Map k a
go Proxy# ()
foom BitQueue
tl Map k a
r)
Nothing -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
#ifdef USE_MAGIC_PROXY
{-# NOINLINE bogus #-}
bogus :: a -> Proxy# ()
bogus :: a -> Proxy# ()
bogus _ = Proxy# ()
forall k (a :: k). Proxy# a
proxy#
#else
{-# INLINE bogus #-}
bogus :: a -> a
bogus a = a
#endif
replaceAlong :: BitQueue -> a -> Map k a -> Map k a
replaceAlong :: BitQueue -> a -> Map k a -> Map k a
replaceAlong !BitQueue
_ _ Tip = Map k a
forall k a. Map k a
Tip
replaceAlong q :: BitQueue
q x :: a
x (Bin sz :: Int
sz ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r) =
case BitQueue -> Maybe (Bool, BitQueue)
unconsQ BitQueue
q of
Just (False, tl :: BitQueue
tl) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz k
ky a
y (BitQueue -> a -> Map k a -> Map k a
forall a k. BitQueue -> a -> Map k a -> Map k a
replaceAlong BitQueue
tl a
x Map k a
l) Map k a
r
Just (True,tl :: BitQueue
tl) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz k
ky a
y Map k a
l (BitQueue -> a -> Map k a -> Map k a
forall a k. BitQueue -> a -> Map k a -> Map k a
replaceAlong BitQueue
tl a
x Map k a
r)
Nothing -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz k
ky a
x Map k a
l Map k a
r
#if __GLASGOW_HASKELL__ && MIN_VERSION_base(4,8,0)
atKeyIdentity :: Ord k => k -> (Maybe a -> Identity (Maybe a)) -> Map k a -> Identity (Map k a)
atKeyIdentity :: k
-> (Maybe a -> Identity (Maybe a)) -> Map k a -> Identity (Map k a)
atKeyIdentity k :: k
k f :: Maybe a -> Identity (Maybe a)
f t :: Map k a
t = Map k a -> Identity (Map k a)
forall a. a -> Identity a
Identity (Map k a -> Identity (Map k a)) -> Map k a -> Identity (Map k a)
forall a b. (a -> b) -> a -> b
$ AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
forall k a.
Ord k =>
AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
atKeyPlain AreWeStrict
Lazy k
k ((Maybe a -> Identity (Maybe a)) -> Maybe a -> Maybe a
forall a b. Coercible a b => a -> b
coerce Maybe a -> Identity (Maybe a)
f) Map k a
t
{-# INLINABLE atKeyIdentity #-}
atKeyPlain :: Ord k => AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
atKeyPlain :: AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
atKeyPlain strict :: AreWeStrict
strict k0 :: k
k0 f0 :: Maybe a -> Maybe a
f0 t :: Map k a
t = case k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
forall k a.
Ord k =>
k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
go k
k0 Maybe a -> Maybe a
f0 Map k a
t of
AltSmaller t' :: Map k a
t' -> Map k a
t'
AltBigger t' :: Map k a
t' -> Map k a
t'
AltAdj t' :: Map k a
t' -> Map k a
t'
AltSame -> Map k a
t
where
go :: Ord k => k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
go :: k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
go !k
k f :: Maybe a -> Maybe a
f Tip = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
Nothing -> Altered k a
forall k a. Altered k a
AltSame
Just x :: a
x -> case AreWeStrict
strict of
Lazy -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltBigger (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
k a
x
Strict -> a
x a -> Altered k a -> Altered k a
forall a b. a -> b -> b
`seq` (Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltBigger (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
k a
x)
go k :: k
k f :: Maybe a -> Maybe a
f (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> case k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
forall k a.
Ord k =>
k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
go k
k Maybe a -> Maybe a
f Map k a
l of
AltSmaller l' :: Map k a
l' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltSmaller (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x Map k a
l' Map k a
r
AltBigger l' :: Map k a
l' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltBigger (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l' Map k a
r
AltAdj l' :: Map k a
l' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltAdj (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x Map k a
l' Map k a
r
AltSame -> Altered k a
forall k a. Altered k a
AltSame
GT -> case k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
forall k a.
Ord k =>
k -> (Maybe a -> Maybe a) -> Map k a -> Altered k a
go k
k Maybe a -> Maybe a
f Map k a
r of
AltSmaller r' :: Map k a
r' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltSmaller (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l Map k a
r'
AltBigger r' :: Map k a
r' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltBigger (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x Map k a
l Map k a
r'
AltAdj r' :: Map k a
r' -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltAdj (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x Map k a
l Map k a
r'
AltSame -> Altered k a
forall k a. Altered k a
AltSame
EQ -> case Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
x) of
Just x' :: a
x' -> case AreWeStrict
strict of
Lazy -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltAdj (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r
Strict -> a
x' a -> Altered k a -> Altered k a
forall a b. a -> b -> b
`seq` (Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltAdj (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r)
Nothing -> Map k a -> Altered k a
forall k a. Map k a -> Altered k a
AltSmaller (Map k a -> Altered k a) -> Map k a -> Altered k a
forall a b. (a -> b) -> a -> b
$ Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
{-# INLINE atKeyPlain #-}
data Altered k a = AltSmaller !(Map k a) | AltBigger !(Map k a) | AltAdj !(Map k a) | AltSame
#endif
#ifdef DEFINE_ALTERF_FALLBACK
alterFFallback :: (Functor f, Ord k)
=> AreWeStrict -> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
alterFFallback Lazy k f t = alterFYoneda k (\m q -> q <$> f m) t id
alterFFallback Strict k f t = alterFYoneda k (\m q -> q . forceMaybe <$> f m) t id
where
forceMaybe Nothing = Nothing
forceMaybe may@(Just !_) = may
{-# NOINLINE alterFFallback #-}
alterFYoneda :: Ord k =>
k -> (Maybe a -> (Maybe a -> b) -> f b) -> Map k a -> (Map k a -> b) -> f b
alterFYoneda = go
where
go :: Ord k =>
k -> (Maybe a -> (Maybe a -> b) -> f b) -> Map k a -> (Map k a -> b) -> f b
go !k f Tip g = f Nothing $ \ mx -> case mx of
Nothing -> g Tip
Just x -> g (singleton k x)
go k f (Bin sx kx x l r) g = case compare k kx of
LT -> go k f l (\m -> g (balance kx x m r))
GT -> go k f r (\m -> g (balance kx x l m))
EQ -> f (Just x) $ \ mx' -> case mx' of
Just x' -> g (Bin sx kx x' l r)
Nothing -> g (glue l r)
{-# INLINE alterFYoneda #-}
#endif
findIndex :: Ord k => k -> Map k a -> Int
findIndex :: k -> Map k a -> Int
findIndex = Int -> k -> Map k a -> Int
forall k a. Ord k => Int -> k -> Map k a -> Int
go 0
where
go :: Ord k => Int -> k -> Map k a -> Int
go :: Int -> k -> Map k a -> Int
go !Int
_ !k
_ Tip = [Char] -> Int
forall a. HasCallStack => [Char] -> a
error "Map.findIndex: element is not in the map"
go idx :: Int
idx k :: k
k (Bin _ kx :: k
kx _ l :: Map k a
l r :: Map k a
r) = case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> Int -> k -> Map k a -> Int
forall k a. Ord k => Int -> k -> Map k a -> Int
go Int
idx k
k Map k a
l
GT -> Int -> k -> Map k a -> Int
forall k a. Ord k => Int -> k -> Map k a -> Int
go (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) k
k Map k a
r
EQ -> Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
#if __GLASGOW_HASKELL__
{-# INLINABLE findIndex #-}
#endif
lookupIndex :: Ord k => k -> Map k a -> Maybe Int
lookupIndex :: k -> Map k a -> Maybe Int
lookupIndex = Int -> k -> Map k a -> Maybe Int
forall k a. Ord k => Int -> k -> Map k a -> Maybe Int
go 0
where
go :: Ord k => Int -> k -> Map k a -> Maybe Int
go :: Int -> k -> Map k a -> Maybe Int
go !Int
_ !k
_ Tip = Maybe Int
forall a. Maybe a
Nothing
go idx :: Int
idx k :: k
k (Bin _ kx :: k
kx _ l :: Map k a
l r :: Map k a
r) = case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> Int -> k -> Map k a -> Maybe Int
forall k a. Ord k => Int -> k -> Map k a -> Maybe Int
go Int
idx k
k Map k a
l
GT -> Int -> k -> Map k a -> Maybe Int
forall k a. Ord k => Int -> k -> Map k a -> Maybe Int
go (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) k
k Map k a
r
EQ -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$! Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
#if __GLASGOW_HASKELL__
{-# INLINABLE lookupIndex #-}
#endif
elemAt :: Int -> Map k a -> (k,a)
elemAt :: Int -> Map k a -> (k, a)
elemAt !Int
_ Tip = [Char] -> (k, a)
forall a. HasCallStack => [Char] -> a
error "Map.elemAt: index out of range"
elemAt i :: Int
i (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
= case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> Int -> Map k a -> (k, a)
forall k a. Int -> Map k a -> (k, a)
elemAt Int
i Map k a
l
GT -> Int -> Map k a -> (k, a)
forall k a. Int -> Map k a -> (k, a)
elemAt (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) Map k a
r
EQ -> (k
kx,a
x)
where
sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
take :: Int -> Map k a -> Map k a
take :: Int -> Map k a -> Map k a
take i :: Int
i m :: Map k a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m = Map k a
m
take i0 :: Int
i0 m0 :: Map k a
m0 = Int -> Map k a -> Map k a
forall k a. Int -> Map k a -> Map k a
go Int
i0 Map k a
m0
where
go :: Int -> Map k a -> Map k a
go i :: Int
i !Map k a
_ | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Map k a
forall k a. Map k a
Tip
go !Int
_ Tip = Map k a
forall k a. Map k a
Tip
go i :: Int
i (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> Int -> Map k a -> Map k a
go Int
i Map k a
l
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l (Int -> Map k a -> Map k a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) Map k a
r)
EQ -> Map k a
l
where sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
drop :: Int -> Map k a -> Map k a
drop :: Int -> Map k a -> Map k a
drop i :: Int
i m :: Map k a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m = Map k a
forall k a. Map k a
Tip
drop i0 :: Int
i0 m0 :: Map k a
m0 = Int -> Map k a -> Map k a
forall k a. Int -> Map k a -> Map k a
go Int
i0 Map k a
m0
where
go :: Int -> Map k a -> Map k a
go i :: Int
i m :: Map k a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Map k a
m
go !Int
_ Tip = Map k a
forall k a. Map k a
Tip
go i :: Int
i (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x (Int -> Map k a -> Map k a
go Int
i Map k a
l) Map k a
r
GT -> Int -> Map k a -> Map k a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) Map k a
r
EQ -> k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMin k
kx a
x Map k a
r
where sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
splitAt :: Int -> Map k a -> (Map k a, Map k a)
splitAt :: Int -> Map k a -> (Map k a, Map k a)
splitAt i0 :: Int
i0 m0 :: Map k a
m0
| Int
i0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m0 = (Map k a
m0, Map k a
forall k a. Map k a
Tip)
| Bool
otherwise = StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Map k a) (Map k a) -> (Map k a, Map k a))
-> StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. (a -> b) -> a -> b
$ Int -> Map k a -> StrictPair (Map k a) (Map k a)
forall k a. Int -> Map k a -> StrictPair (Map k a) (Map k a)
go Int
i0 Map k a
m0
where
go :: Int -> Map k a -> StrictPair (Map k a) (Map k a)
go i :: Int
i m :: Map k a
m | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
m
go !Int
_ Tip = Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip
go i :: Int
i (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
= case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> case Int -> Map k a -> StrictPair (Map k a) (Map k a)
go Int
i Map k a
l of
ll :: Map k a
ll :*: lr :: Map k a
lr -> Map k a
ll Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
lr Map k a
r
GT -> case Int -> Map k a -> StrictPair (Map k a) (Map k a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeL Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) Map k a
r of
rl :: Map k a
rl :*: rr :: Map k a
rr -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
rl Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
rr
EQ -> Map k a
l Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMin k
kx a
x Map k a
r
where sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
updateAt f :: k -> a -> Maybe a
f !Int
i t :: Map k a
t =
case Map k a
t of
Tip -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Map.updateAt: index out of range"
Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r -> case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x ((k -> a -> Maybe a) -> Int -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
updateAt k -> a -> Maybe a
f Int
i Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l ((k -> a -> Maybe a) -> Int -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
updateAt k -> a -> Maybe a
f (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) Map k a
r)
EQ -> case k -> a -> Maybe a
f k
kx a
x of
Just x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
r
Nothing -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
where
sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
deleteAt :: Int -> Map k a -> Map k a
deleteAt :: Int -> Map k a -> Map k a
deleteAt !Int
i t :: Map k a
t =
case Map k a
t of
Tip -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Map.deleteAt: index out of range"
Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r -> case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
sizeL of
LT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x (Int -> Map k a -> Map k a
forall k a. Int -> Map k a -> Map k a
deleteAt Int
i Map k a
l) Map k a
r
GT -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l (Int -> Map k a -> Map k a
forall k a. Int -> Map k a -> Map k a
deleteAt (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
sizeLInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) Map k a
r)
EQ -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
where
sizeL :: Int
sizeL = Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l
lookupMinSure :: k -> a -> Map k a -> (k, a)
lookupMinSure :: k -> a -> Map k a -> (k, a)
lookupMinSure k :: k
k a :: a
a Tip = (k
k, a
a)
lookupMinSure _ _ (Bin _ k :: k
k a :: a
a l :: Map k a
l _) = k -> a -> Map k a -> (k, a)
forall k a. k -> a -> Map k a -> (k, a)
lookupMinSure k
k a
a Map k a
l
lookupMin :: Map k a -> Maybe (k,a)
lookupMin :: Map k a -> Maybe (k, a)
lookupMin Tip = Maybe (k, a)
forall a. Maybe a
Nothing
lookupMin (Bin _ k :: k
k x :: a
x l :: Map k a
l _) = (k, a) -> Maybe (k, a)
forall a. a -> Maybe a
Just ((k, a) -> Maybe (k, a)) -> (k, a) -> Maybe (k, a)
forall a b. (a -> b) -> a -> b
$! k -> a -> Map k a -> (k, a)
forall k a. k -> a -> Map k a -> (k, a)
lookupMinSure k
k a
x Map k a
l
findMin :: Map k a -> (k,a)
findMin :: Map k a -> (k, a)
findMin t :: Map k a
t
| Just r :: (k, a)
r <- Map k a -> Maybe (k, a)
forall k a. Map k a -> Maybe (k, a)
lookupMin Map k a
t = (k, a)
r
| Bool
otherwise = [Char] -> (k, a)
forall a. HasCallStack => [Char] -> a
error "Map.findMin: empty map has no minimal element"
lookupMaxSure :: k -> a -> Map k a -> (k, a)
lookupMaxSure :: k -> a -> Map k a -> (k, a)
lookupMaxSure k :: k
k a :: a
a Tip = (k
k, a
a)
lookupMaxSure _ _ (Bin _ k :: k
k a :: a
a _ r :: Map k a
r) = k -> a -> Map k a -> (k, a)
forall k a. k -> a -> Map k a -> (k, a)
lookupMaxSure k
k a
a Map k a
r
lookupMax :: Map k a -> Maybe (k, a)
lookupMax :: Map k a -> Maybe (k, a)
lookupMax Tip = Maybe (k, a)
forall a. Maybe a
Nothing
lookupMax (Bin _ k :: k
k x :: a
x _ r :: Map k a
r) = (k, a) -> Maybe (k, a)
forall a. a -> Maybe a
Just ((k, a) -> Maybe (k, a)) -> (k, a) -> Maybe (k, a)
forall a b. (a -> b) -> a -> b
$! k -> a -> Map k a -> (k, a)
forall k a. k -> a -> Map k a -> (k, a)
lookupMaxSure k
k a
x Map k a
r
findMax :: Map k a -> (k,a)
findMax :: Map k a -> (k, a)
findMax t :: Map k a
t
| Just r :: (k, a)
r <- Map k a -> Maybe (k, a)
forall k a. Map k a -> Maybe (k, a)
lookupMax Map k a
t = (k, a)
r
| Bool
otherwise = [Char] -> (k, a)
forall a. HasCallStack => [Char] -> a
error "Map.findMax: empty map has no maximal element"
deleteMin :: Map k a -> Map k a
deleteMin :: Map k a -> Map k a
deleteMin (Bin _ _ _ Tip r :: Map k a
r) = Map k a
r
deleteMin (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x (Map k a -> Map k a
forall k a. Map k a -> Map k a
deleteMin Map k a
l) Map k a
r
deleteMin Tip = Map k a
forall k a. Map k a
Tip
deleteMax :: Map k a -> Map k a
deleteMax :: Map k a -> Map k a
deleteMax (Bin _ _ _ l :: Map k a
l Tip) = Map k a
l
deleteMax (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l (Map k a -> Map k a
forall k a. Map k a -> Map k a
deleteMax Map k a
r)
deleteMax Tip = Map k a
forall k a. Map k a
Tip
updateMin :: (a -> Maybe a) -> Map k a -> Map k a
updateMin :: (a -> Maybe a) -> Map k a -> Map k a
updateMin f :: a -> Maybe a
f m :: Map k a
m
= (k -> a -> Maybe a) -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Map k a -> Map k a
updateMinWithKey (\_ x :: a
x -> a -> Maybe a
f a
x) Map k a
m
updateMax :: (a -> Maybe a) -> Map k a -> Map k a
updateMax :: (a -> Maybe a) -> Map k a -> Map k a
updateMax f :: a -> Maybe a
f m :: Map k a
m
= (k -> a -> Maybe a) -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Map k a -> Map k a
updateMaxWithKey (\_ x :: a
x -> a -> Maybe a
f a
x) Map k a
m
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
updateMinWithKey _ Tip = Map k a
forall k a. Map k a
Tip
updateMinWithKey f :: k -> a -> Maybe a
f (Bin sx :: Int
sx kx :: k
kx x :: a
x Tip r :: Map k a
r) = case k -> a -> Maybe a
f k
kx a
x of
Nothing -> Map k a
r
Just x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
forall k a. Map k a
Tip Map k a
r
updateMinWithKey f :: k -> a -> Maybe a
f (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x ((k -> a -> Maybe a) -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Map k a -> Map k a
updateMinWithKey k -> a -> Maybe a
f Map k a
l) Map k a
r
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
updateMaxWithKey _ Tip = Map k a
forall k a. Map k a
Tip
updateMaxWithKey f :: k -> a -> Maybe a
f (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l Tip) = case k -> a -> Maybe a
f k
kx a
x of
Nothing -> Map k a
l
Just x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
x' Map k a
l Map k a
forall k a. Map k a
Tip
updateMaxWithKey f :: k -> a -> Maybe a
f (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kx a
x Map k a
l ((k -> a -> Maybe a) -> Map k a -> Map k a
forall k a. (k -> a -> Maybe a) -> Map k a -> Map k a
updateMaxWithKey k -> a -> Maybe a
f Map k a
r)
minViewWithKey :: Map k a -> Maybe ((k,a), Map k a)
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)
minViewWithKey Tip = Maybe ((k, a), Map k a)
forall a. Maybe a
Nothing
minViewWithKey (Bin _ k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r) = ((k, a), Map k a) -> Maybe ((k, a), Map k a)
forall a. a -> Maybe a
Just (((k, a), Map k a) -> Maybe ((k, a), Map k a))
-> ((k, a), Map k a) -> Maybe ((k, a), Map k a)
forall a b. (a -> b) -> a -> b
$
case k -> a -> Map k a -> Map k a -> MinView k a
forall k a. k -> a -> Map k a -> Map k a -> MinView k a
minViewSure k
k a
x Map k a
l Map k a
r of
MinView km :: k
km xm :: a
xm t :: Map k a
t -> ((k
km, a
xm), Map k a
t)
{-# INLINE minViewWithKey #-}
maxViewWithKey :: Map k a -> Maybe ((k,a), Map k a)
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)
maxViewWithKey Tip = Maybe ((k, a), Map k a)
forall a. Maybe a
Nothing
maxViewWithKey (Bin _ k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r) = ((k, a), Map k a) -> Maybe ((k, a), Map k a)
forall a. a -> Maybe a
Just (((k, a), Map k a) -> Maybe ((k, a), Map k a))
-> ((k, a), Map k a) -> Maybe ((k, a), Map k a)
forall a b. (a -> b) -> a -> b
$
case k -> a -> Map k a -> Map k a -> MaxView k a
forall k a. k -> a -> Map k a -> Map k a -> MaxView k a
maxViewSure k
k a
x Map k a
l Map k a
r of
MaxView km :: k
km xm :: a
xm t :: Map k a
t -> ((k
km, a
xm), Map k a
t)
{-# INLINE maxViewWithKey #-}
minView :: Map k a -> Maybe (a, Map k a)
minView :: Map k a -> Maybe (a, Map k a)
minView t :: Map k a
t = case Map k a -> Maybe ((k, a), Map k a)
forall k a. Map k a -> Maybe ((k, a), Map k a)
minViewWithKey Map k a
t of
Nothing -> Maybe (a, Map k a)
forall a. Maybe a
Nothing
Just ~((_, x :: a
x), t' :: Map k a
t') -> (a, Map k a) -> Maybe (a, Map k a)
forall a. a -> Maybe a
Just (a
x, Map k a
t')
maxView :: Map k a -> Maybe (a, Map k a)
maxView :: Map k a -> Maybe (a, Map k a)
maxView t :: Map k a
t = case Map k a -> Maybe ((k, a), Map k a)
forall k a. Map k a -> Maybe ((k, a), Map k a)
maxViewWithKey Map k a
t of
Nothing -> Maybe (a, Map k a)
forall a. Maybe a
Nothing
Just ~((_, x :: a
x), t' :: Map k a
t') -> (a, Map k a) -> Maybe (a, Map k a)
forall a. a -> Maybe a
Just (a
x, Map k a
t')
unions :: (Foldable f, Ord k) => f (Map k a) -> Map k a
unions :: f (Map k a) -> Map k a
unions ts :: f (Map k a)
ts
= (Map k a -> Map k a -> Map k a)
-> Map k a -> f (Map k a) -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Map k a -> Map k a -> Map k a
forall k v. Ord k => Map k v -> Map k v -> Map k v
union Map k a
forall k a. Map k a
empty f (Map k a)
ts
#if __GLASGOW_HASKELL__
{-# INLINABLE unions #-}
#endif
unionsWith :: (Foldable f, Ord k) => (a->a->a) -> f (Map k a) -> Map k a
unionsWith :: (a -> a -> a) -> f (Map k a) -> Map k a
unionsWith f :: a -> a -> a
f ts :: f (Map k a)
ts
= (Map k a -> Map k a -> Map k a)
-> Map k a -> f (Map k a) -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' ((a -> a -> a) -> Map k a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith a -> a -> a
f) Map k a
forall k a. Map k a
empty f (Map k a)
ts
#if __GLASGOW_HASKELL__
{-# INLINABLE unionsWith #-}
#endif
union :: Ord k => Map k a -> Map k a -> Map k a
union :: Map k a -> Map k a -> Map k a
union t1 :: Map k a
t1 Tip = Map k a
t1
union t1 :: Map k a
t1 (Bin _ k :: k
k x :: a
x Tip Tip) = k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
insertR k
k a
x Map k a
t1
union (Bin _ k :: k
k x :: a
x Tip Tip) t2 :: Map k a
t2 = k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert k
k a
x Map k a
t2
union Tip t2 :: Map k a
t2 = Map k a
t2
union t1 :: Map k a
t1@(Bin _ k1 :: k
k1 x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k a
t2 = case k -> Map k a -> (Map k a, Map k a)
forall k a. Ord k => k -> Map k a -> (Map k a, Map k a)
split k
k1 Map k a
t2 of
(l2 :: Map k a
l2, r2 :: Map k a
r2) | Map k a
l1l2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l1 Bool -> Bool -> Bool
&& Map k a
r1r2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r1 -> Map k a
t1
| Bool
otherwise -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k1 a
x1 Map k a
l1l2 Map k a
r1r2
where !l1l2 :: Map k a
l1l2 = Map k a -> Map k a -> Map k a
forall k v. Ord k => Map k v -> Map k v -> Map k v
union Map k a
l1 Map k a
l2
!r1r2 :: Map k a
r1r2 = Map k a -> Map k a -> Map k a
forall k v. Ord k => Map k v -> Map k v -> Map k v
union Map k a
r1 Map k a
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE union #-}
#endif
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith :: (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith _f :: a -> a -> a
_f t1 :: Map k a
t1 Tip = Map k a
t1
unionWith f :: a -> a -> a
f t1 :: Map k a
t1 (Bin _ k :: k
k x :: a
x Tip Tip) = (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithR a -> a -> a
f k
k a
x Map k a
t1
unionWith f :: a -> a -> a
f (Bin _ k :: k
k x :: a
x Tip Tip) t2 :: Map k a
t2 = (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith a -> a -> a
f k
k a
x Map k a
t2
unionWith _f :: a -> a -> a
_f Tip t2 :: Map k a
t2 = Map k a
t2
unionWith f :: a -> a -> a
f (Bin _ k1 :: k
k1 x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k a
t2 = case k -> Map k a -> (Map k a, Maybe a, Map k a)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k1 Map k a
t2 of
(l2 :: Map k a
l2, mb :: Maybe a
mb, r2 :: Map k a
r2) -> case Maybe a
mb of
Nothing -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k1 a
x1 Map k a
l1l2 Map k a
r1r2
Just x2 :: a
x2 -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k1 (a -> a -> a
f a
x1 a
x2) Map k a
l1l2 Map k a
r1r2
where !l1l2 :: Map k a
l1l2 = (a -> a -> a) -> Map k a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith a -> a -> a
f Map k a
l1 Map k a
l2
!r1r2 :: Map k a
r1r2 = (a -> a -> a) -> Map k a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith a -> a -> a
f Map k a
r1 Map k a
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE unionWith #-}
#endif
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey :: (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey _f :: k -> a -> a -> a
_f t1 :: Map k a
t1 Tip = Map k a
t1
unionWithKey f :: k -> a -> a -> a
f t1 :: Map k a
t1 (Bin _ k :: k
k x :: a
x Tip Tip) = (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKeyR k -> a -> a -> a
f k
k a
x Map k a
t1
unionWithKey f :: k -> a -> a -> a
f (Bin _ k :: k
k x :: a
x Tip Tip) t2 :: Map k a
t2 = (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey k -> a -> a -> a
f k
k a
x Map k a
t2
unionWithKey _f :: k -> a -> a -> a
_f Tip t2 :: Map k a
t2 = Map k a
t2
unionWithKey f :: k -> a -> a -> a
f (Bin _ k1 :: k
k1 x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k a
t2 = case k -> Map k a -> (Map k a, Maybe a, Map k a)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k1 Map k a
t2 of
(l2 :: Map k a
l2, mb :: Maybe a
mb, r2 :: Map k a
r2) -> case Maybe a
mb of
Nothing -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k1 a
x1 Map k a
l1l2 Map k a
r1r2
Just x2 :: a
x2 -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k1 (k -> a -> a -> a
f k
k1 a
x1 a
x2) Map k a
l1l2 Map k a
r1r2
where !l1l2 :: Map k a
l1l2 = (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey k -> a -> a -> a
f Map k a
l1 Map k a
l2
!r1r2 :: Map k a
r1r2 = (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey k -> a -> a -> a
f Map k a
r1 Map k a
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE unionWithKey #-}
#endif
difference :: Ord k => Map k a -> Map k b -> Map k a
difference :: Map k a -> Map k b -> Map k a
difference Tip _ = Map k a
forall k a. Map k a
Tip
difference t1 :: Map k a
t1 Tip = Map k a
t1
difference t1 :: Map k a
t1 (Bin _ k :: k
k _ l2 :: Map k b
l2 r2 :: Map k b
r2) = case k -> Map k a -> (Map k a, Map k a)
forall k a. Ord k => k -> Map k a -> (Map k a, Map k a)
split k
k Map k a
t1 of
(l1 :: Map k a
l1, r1 :: Map k a
r1)
| Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l1l2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
r1r2 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Map k a -> Int
forall k a. Map k a -> Int
size Map k a
t1 -> Map k a
t1
| Bool
otherwise -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l1l2 Map k a
r1r2
where
!l1l2 :: Map k a
l1l2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
difference Map k a
l1 Map k b
l2
!r1r2 :: Map k a
r1r2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
difference Map k a
r1 Map k b
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE difference #-}
#endif
withoutKeys :: Ord k => Map k a -> Set k -> Map k a
withoutKeys :: Map k a -> Set k -> Map k a
withoutKeys Tip _ = Map k a
forall k a. Map k a
Tip
withoutKeys m :: Map k a
m Set.Tip = Map k a
m
withoutKeys m :: Map k a
m (Set.Bin _ k :: k
k ls :: Set k
ls rs :: Set k
rs) = case k -> Map k a -> (Map k a, Bool, Map k a)
forall k a. Ord k => k -> Map k a -> (Map k a, Bool, Map k a)
splitMember k
k Map k a
m of
(lm :: Map k a
lm, b :: Bool
b, rm :: Map k a
rm)
| Bool -> Bool
not Bool
b Bool -> Bool -> Bool
&& Map k a
lm' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
lm Bool -> Bool -> Bool
&& Map k a
rm' Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
rm -> Map k a
m
| Bool
otherwise -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
lm' Map k a
rm'
where
!lm' :: Map k a
lm' = Map k a -> Set k -> Map k a
forall k a. Ord k => Map k a -> Set k -> Map k a
withoutKeys Map k a
lm Set k
ls
!rm' :: Map k a
rm' = Map k a -> Set k -> Map k a
forall k a. Ord k => Map k a -> Set k -> Map k a
withoutKeys Map k a
rm Set k
rs
#if __GLASGOW_HASKELL__
{-# INLINABLE withoutKeys #-}
#endif
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWith :: (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWith f :: a -> b -> Maybe a
f = SimpleWhenMissing k a a
-> SimpleWhenMissing k b a
-> SimpleWhenMatched k a b a
-> Map k a
-> Map k b
-> Map k a
forall k a c b.
Ord k =>
SimpleWhenMissing k a c
-> SimpleWhenMissing k b c
-> SimpleWhenMatched k a b c
-> Map k a
-> Map k b
-> Map k c
merge SimpleWhenMissing k a a
forall (f :: * -> *) k x. Applicative f => WhenMissing f k x x
preserveMissing SimpleWhenMissing k b a
forall (f :: * -> *) k x y. Applicative f => WhenMissing f k x y
dropMissing (SimpleWhenMatched k a b a -> Map k a -> Map k b -> Map k a)
-> SimpleWhenMatched k a b a -> Map k a -> Map k b -> Map k a
forall a b. (a -> b) -> a -> b
$
(k -> a -> b -> Maybe a) -> SimpleWhenMatched k a b a
forall (f :: * -> *) k x y z.
Applicative f =>
(k -> x -> y -> Maybe z) -> WhenMatched f k x y z
zipWithMaybeMatched (\_ x :: a
x y :: b
y -> a -> b -> Maybe a
f a
x b
y)
#if __GLASGOW_HASKELL__
{-# INLINABLE differenceWith #-}
#endif
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWithKey :: (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWithKey f :: k -> a -> b -> Maybe a
f =
SimpleWhenMissing k a a
-> SimpleWhenMissing k b a
-> SimpleWhenMatched k a b a
-> Map k a
-> Map k b
-> Map k a
forall k a c b.
Ord k =>
SimpleWhenMissing k a c
-> SimpleWhenMissing k b c
-> SimpleWhenMatched k a b c
-> Map k a
-> Map k b
-> Map k c
merge SimpleWhenMissing k a a
forall (f :: * -> *) k x. Applicative f => WhenMissing f k x x
preserveMissing SimpleWhenMissing k b a
forall (f :: * -> *) k x y. Applicative f => WhenMissing f k x y
dropMissing ((k -> a -> b -> Maybe a) -> SimpleWhenMatched k a b a
forall (f :: * -> *) k x y z.
Applicative f =>
(k -> x -> y -> Maybe z) -> WhenMatched f k x y z
zipWithMaybeMatched k -> a -> b -> Maybe a
f)
#if __GLASGOW_HASKELL__
{-# INLINABLE differenceWithKey #-}
#endif
intersection :: Ord k => Map k a -> Map k b -> Map k a
intersection :: Map k a -> Map k b -> Map k a
intersection Tip _ = Map k a
forall k a. Map k a
Tip
intersection _ Tip = Map k a
forall k a. Map k a
Tip
intersection t1 :: Map k a
t1@(Bin _ k :: k
k x :: a
x l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k b
t2
| Bool
mb = if Map k a
l1l2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l1 Bool -> Bool -> Bool
&& Map k a
r1r2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r1
then Map k a
t1
else k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k a
x Map k a
l1l2 Map k a
r1r2
| Bool
otherwise = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l1l2 Map k a
r1r2
where
!(l2 :: Map k b
l2, mb :: Bool
mb, r2 :: Map k b
r2) = k -> Map k b -> (Map k b, Bool, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Bool, Map k a)
splitMember k
k Map k b
t2
!l1l2 :: Map k a
l1l2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
intersection Map k a
l1 Map k b
l2
!r1r2 :: Map k a
r1r2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
intersection Map k a
r1 Map k b
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE intersection #-}
#endif
restrictKeys :: Ord k => Map k a -> Set k -> Map k a
restrictKeys :: Map k a -> Set k -> Map k a
restrictKeys Tip _ = Map k a
forall k a. Map k a
Tip
restrictKeys _ Set.Tip = Map k a
forall k a. Map k a
Tip
restrictKeys m :: Map k a
m@(Bin _ k :: k
k x :: a
x l1 :: Map k a
l1 r1 :: Map k a
r1) s :: Set k
s
| Bool
b = if Map k a
l1l2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l1 Bool -> Bool -> Bool
&& Map k a
r1r2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r1
then Map k a
m
else k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k a
x Map k a
l1l2 Map k a
r1r2
| Bool
otherwise = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l1l2 Map k a
r1r2
where
!(l2 :: Set k
l2, b :: Bool
b, r2 :: Set k
r2) = k -> Set k -> (Set k, Bool, Set k)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
Set.splitMember k
k Set k
s
!l1l2 :: Map k a
l1l2 = Map k a -> Set k -> Map k a
forall k a. Ord k => Map k a -> Set k -> Map k a
restrictKeys Map k a
l1 Set k
l2
!r1r2 :: Map k a
r1r2 = Map k a -> Set k -> Map k a
forall k a. Ord k => Map k a -> Set k -> Map k a
restrictKeys Map k a
r1 Set k
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE restrictKeys #-}
#endif
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith :: (a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith _f :: a -> b -> c
_f Tip _ = Map k c
forall k a. Map k a
Tip
intersectionWith _f :: a -> b -> c
_f _ Tip = Map k c
forall k a. Map k a
Tip
intersectionWith f :: a -> b -> c
f (Bin _ k :: k
k x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k b
t2 = case Maybe b
mb of
Just x2 :: b
x2 -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k (a -> b -> c
f a
x1 b
x2) Map k c
l1l2 Map k c
r1r2
Nothing -> Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 Map k c
l1l2 Map k c
r1r2
where
!(l2 :: Map k b
l2, mb :: Maybe b
mb, r2 :: Map k b
r2) = k -> Map k b -> (Map k b, Maybe b, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k Map k b
t2
!l1l2 :: Map k c
l1l2 = (a -> b -> c) -> Map k a -> Map k b -> Map k c
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith a -> b -> c
f Map k a
l1 Map k b
l2
!r1r2 :: Map k c
r1r2 = (a -> b -> c) -> Map k a -> Map k b -> Map k c
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith a -> b -> c
f Map k a
r1 Map k b
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE intersectionWith #-}
#endif
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey :: (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey _f :: k -> a -> b -> c
_f Tip _ = Map k c
forall k a. Map k a
Tip
intersectionWithKey _f :: k -> a -> b -> c
_f _ Tip = Map k c
forall k a. Map k a
Tip
intersectionWithKey f :: k -> a -> b -> c
f (Bin _ k :: k
k x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k b
t2 = case Maybe b
mb of
Just x2 :: b
x2 -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
k (k -> a -> b -> c
f k
k a
x1 b
x2) Map k c
l1l2 Map k c
r1r2
Nothing -> Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 Map k c
l1l2 Map k c
r1r2
where
!(l2 :: Map k b
l2, mb :: Maybe b
mb, r2 :: Map k b
r2) = k -> Map k b -> (Map k b, Maybe b, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k Map k b
t2
!l1l2 :: Map k c
l1l2 = (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey k -> a -> b -> c
f Map k a
l1 Map k b
l2
!r1r2 :: Map k c
r1r2 = (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey k -> a -> b -> c
f Map k a
r1 Map k b
r2
#if __GLASGOW_HASKELL__
{-# INLINABLE intersectionWithKey #-}
#endif
disjoint :: Ord k => Map k a -> Map k b -> Bool
disjoint :: Map k a -> Map k b -> Bool
disjoint Tip _ = Bool
True
disjoint _ Tip = Bool
True
disjoint (Bin 1 k :: k
k _ _ _) t :: Map k b
t = k
k k -> Map k b -> Bool
forall t a. Ord t => t -> Map t a -> Bool
`notMember` Map k b
t
disjoint (Bin _ k :: k
k _ l :: Map k a
l r :: Map k a
r) t :: Map k b
t
= Bool -> Bool
not Bool
found Bool -> Bool -> Bool
&& Map k a -> Map k b -> Bool
forall k a b. Ord k => Map k a -> Map k b -> Bool
disjoint Map k a
l Map k b
lt Bool -> Bool -> Bool
&& Map k a -> Map k b -> Bool
forall k a b. Ord k => Map k a -> Map k b -> Bool
disjoint Map k a
r Map k b
gt
where
(lt :: Map k b
lt,found :: Bool
found,gt :: Map k b
gt) = k -> Map k b -> (Map k b, Bool, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Bool, Map k a)
splitMember k
k Map k b
t
#if !MIN_VERSION_base (4,8,0)
newtype Identity a = Identity { runIdentity :: a }
#if __GLASGOW_HASKELL__ == 708
instance Functor Identity where
fmap = coerce
instance Applicative Identity where
(<*>) = coerce
pure = Identity
#else
instance Functor Identity where
fmap f (Identity a) = Identity (f a)
instance Applicative Identity where
Identity f <*> Identity x = Identity (f x)
pure = Identity
#endif
#endif
data WhenMissing f k x y = WhenMissing
{ WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree :: Map k x -> f (Map k y)
, WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey :: k -> x -> f (Maybe y)}
instance (Applicative f, Monad f) => Functor (WhenMissing f k x) where
fmap :: (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
fmap = (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
forall (f :: * -> *) a b k x.
(Applicative f, Monad f) =>
(a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
mapWhenMissing
{-# INLINE fmap #-}
instance (Applicative f, Monad f)
=> Category.Category (WhenMissing f k) where
id :: WhenMissing f k a a
id = WhenMissing f k a a
forall (f :: * -> *) k x. Applicative f => WhenMissing f k x x
preserveMissing
f :: WhenMissing f k b c
f . :: WhenMissing f k b c -> WhenMissing f k a b -> WhenMissing f k a c
. g :: WhenMissing f k a b
g = (k -> a -> f (Maybe c)) -> WhenMissing f k a c
forall (f :: * -> *) k x y.
Applicative f =>
(k -> x -> f (Maybe y)) -> WhenMissing f k x y
traverseMaybeMissing ((k -> a -> f (Maybe c)) -> WhenMissing f k a c)
-> (k -> a -> f (Maybe c)) -> WhenMissing f k a c
forall a b. (a -> b) -> a -> b
$
\ k :: k
k x :: a
x -> WhenMissing f k a b -> k -> a -> f (Maybe b)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k a b
g k
k a
x f (Maybe b) -> (Maybe b -> f (Maybe c)) -> f (Maybe c)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \y :: Maybe b
y ->
case Maybe b
y of
Nothing -> Maybe c -> f (Maybe c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe c
forall a. Maybe a
Nothing
Just q :: b
q -> WhenMissing f k b c -> k -> b -> f (Maybe c)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k b c
f k
k b
q
{-# INLINE id #-}
{-# INLINE (.) #-}
instance (Applicative f, Monad f) => Applicative (WhenMissing f k x) where
pure :: a -> WhenMissing f k x a
pure x :: a
x = (k -> x -> a) -> WhenMissing f k x a
forall (f :: * -> *) k x y.
Applicative f =>
(k -> x -> y) -> WhenMissing f k x y
mapMissing (\ _ _ -> a
x)
f :: WhenMissing f k x (a -> b)
f <*> :: WhenMissing f k x (a -> b)
-> WhenMissing f k x a -> WhenMissing f k x b
<*> g :: WhenMissing f k x a
g = (k -> x -> f (Maybe b)) -> WhenMissing f k x b
forall (f :: * -> *) k x y.
Applicative f =>
(k -> x -> f (Maybe y)) -> WhenMissing f k x y
traverseMaybeMissing ((k -> x -> f (Maybe b)) -> WhenMissing f k x b)
-> (k -> x -> f (Maybe b)) -> WhenMissing f k x b
forall a b. (a -> b) -> a -> b
$ \k :: k
k x :: x
x -> do
Maybe (a -> b)
res1 <- WhenMissing f k x (a -> b) -> k -> x -> f (Maybe (a -> b))
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k x (a -> b)
f k
k x
x
case Maybe (a -> b)
res1 of
Nothing -> Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
Just r :: a -> b
r -> (Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe b -> f (Maybe b)) -> Maybe b -> f (Maybe b)
forall a b. (a -> b) -> a -> b
$!) (Maybe b -> f (Maybe b))
-> (Maybe a -> Maybe b) -> Maybe a -> f (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
r (Maybe a -> f (Maybe b)) -> f (Maybe a) -> f (Maybe b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< WhenMissing f k x a -> k -> x -> f (Maybe a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k x a
g k
k x
x
{-# INLINE pure #-}
{-# INLINE (<*>) #-}
instance (Applicative f, Monad f) => Monad (WhenMissing f k x) where
#if !MIN_VERSION_base(4,8,0)
return = pure
#endif
m :: WhenMissing f k x a
m >>= :: WhenMissing f k x a
-> (a -> WhenMissing f k x b) -> WhenMissing f k x b
>>= f :: a -> WhenMissing f k x b
f = (k -> x -> f (Maybe b)) -> WhenMissing f k x b
forall (f :: * -> *) k x y.
Applicative f =>
(k -> x -> f (Maybe y)) -> WhenMissing f k x y
traverseMaybeMissing ((k -> x -> f (Maybe b)) -> WhenMissing f k x b)
-> (k -> x -> f (Maybe b)) -> WhenMissing f k x b
forall a b. (a -> b) -> a -> b
$ \k :: k
k x :: x
x -> do
Maybe a
res1 <- WhenMissing f k x a -> k -> x -> f (Maybe a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k x a
m k
k x
x
case Maybe a
res1 of
Nothing -> Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
Just r :: a
r -> WhenMissing f k x b -> k -> x -> f (Maybe b)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey (a -> WhenMissing f k x b
f a
r) k
k x
x
{-# INLINE (>>=) #-}
mapWhenMissing :: (Applicative f, Monad f)
=> (a -> b)
-> WhenMissing f k x a -> WhenMissing f k x b
mapWhenMissing :: (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
mapWhenMissing f :: a -> b
f t :: WhenMissing f k x a
t = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k b)
missingSubtree = \m :: Map k x
m -> WhenMissing f k x a -> Map k x -> f (Map k a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree WhenMissing f k x a
t Map k x
m f (Map k a) -> (Map k a -> f (Map k b)) -> f (Map k b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \m' :: Map k a
m' -> Map k b -> f (Map k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map k b -> f (Map k b)) -> Map k b -> f (Map k b)
forall a b. (a -> b) -> a -> b
$! (a -> b) -> Map k a -> Map k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Map k a
m'
, missingKey :: k -> x -> f (Maybe b)
missingKey = \k :: k
k x :: x
x -> WhenMissing f k x a -> k -> x -> f (Maybe a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k x a
t k
k x
x f (Maybe a) -> (Maybe a -> f (Maybe b)) -> f (Maybe b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \q :: Maybe a
q -> (Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe b -> f (Maybe b)) -> Maybe b -> f (Maybe b)
forall a b. (a -> b) -> a -> b
$! (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Maybe a
q) }
{-# INLINE mapWhenMissing #-}
mapGentlyWhenMissing :: Functor f
=> (a -> b)
-> WhenMissing f k x a -> WhenMissing f k x b
mapGentlyWhenMissing :: (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b
mapGentlyWhenMissing f :: a -> b
f t :: WhenMissing f k x a
t = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k b)
missingSubtree = \m :: Map k x
m -> (a -> b) -> Map k a -> Map k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Map k a -> Map k b) -> f (Map k a) -> f (Map k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMissing f k x a -> Map k x -> f (Map k a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree WhenMissing f k x a
t Map k x
m
, missingKey :: k -> x -> f (Maybe b)
missingKey = \k :: k
k x :: x
x -> (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Maybe a -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMissing f k x a -> k -> x -> f (Maybe a)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k x a
t k
k x
x }
{-# INLINE mapGentlyWhenMissing #-}
mapGentlyWhenMatched :: Functor f
=> (a -> b)
-> WhenMatched f k x y a -> WhenMatched f k x y b
mapGentlyWhenMatched :: (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
mapGentlyWhenMatched f :: a -> b
f t :: WhenMatched f k x y a
t = (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall k x y (f :: * -> *) z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
zipWithMaybeAMatched ((k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b)
-> (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall a b. (a -> b) -> a -> b
$
\k :: k
k x :: x
x y :: y
y -> (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Maybe a -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMatched f k x y a -> k -> x -> y -> f (Maybe a)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x y a
t k
k x
x y
y
{-# INLINE mapGentlyWhenMatched #-}
lmapWhenMissing :: (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x
lmapWhenMissing :: (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x
lmapWhenMissing f :: b -> a
f t :: WhenMissing f k a x
t = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k b -> f (Map k x)
missingSubtree = \m :: Map k b
m -> WhenMissing f k a x -> Map k a -> f (Map k x)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree WhenMissing f k a x
t ((b -> a) -> Map k b -> Map k a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> a
f Map k b
m)
, missingKey :: k -> b -> f (Maybe x)
missingKey = \k :: k
k x :: b
x -> WhenMissing f k a x -> k -> a -> f (Maybe x)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey WhenMissing f k a x
t k
k (b -> a
f b
x) }
{-# INLINE lmapWhenMissing #-}
contramapFirstWhenMatched :: (b -> a)
-> WhenMatched f k a y z
-> WhenMatched f k b y z
contramapFirstWhenMatched :: (b -> a) -> WhenMatched f k a y z -> WhenMatched f k b y z
contramapFirstWhenMatched f :: b -> a
f t :: WhenMatched f k a y z
t = (k -> b -> y -> f (Maybe z)) -> WhenMatched f k b y z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> b -> y -> f (Maybe z)) -> WhenMatched f k b y z)
-> (k -> b -> y -> f (Maybe z)) -> WhenMatched f k b y z
forall a b. (a -> b) -> a -> b
$
\k :: k
k x :: b
x y :: y
y -> WhenMatched f k a y z -> k -> a -> y -> f (Maybe z)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k a y z
t k
k (b -> a
f b
x) y
y
{-# INLINE contramapFirstWhenMatched #-}
contramapSecondWhenMatched :: (b -> a)
-> WhenMatched f k x a z
-> WhenMatched f k x b z
contramapSecondWhenMatched :: (b -> a) -> WhenMatched f k x a z -> WhenMatched f k x b z
contramapSecondWhenMatched f :: b -> a
f t :: WhenMatched f k x a z
t = (k -> x -> b -> f (Maybe z)) -> WhenMatched f k x b z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> b -> f (Maybe z)) -> WhenMatched f k x b z)
-> (k -> x -> b -> f (Maybe z)) -> WhenMatched f k x b z
forall a b. (a -> b) -> a -> b
$
\k :: k
k x :: x
x y :: b
y -> WhenMatched f k x a z -> k -> x -> a -> f (Maybe z)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x a z
t k
k x
x (b -> a
f b
y)
{-# INLINE contramapSecondWhenMatched #-}
type SimpleWhenMissing = WhenMissing Identity
newtype WhenMatched f k x y z = WhenMatched
{ WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
matchedKey :: k -> x -> y -> f (Maybe z) }
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched = WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
matchedKey
{-# INLINE runWhenMatched #-}
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)
runWhenMissing = WhenMissing f k x y -> k -> x -> f (Maybe y)
forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey
{-# INLINE runWhenMissing #-}
instance Functor f => Functor (WhenMatched f k x y) where
fmap :: (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
fmap = (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
forall (f :: * -> *) a b k x y.
Functor f =>
(a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
mapWhenMatched
{-# INLINE fmap #-}
instance (Monad f, Applicative f) => Category.Category (WhenMatched f k x) where
id :: WhenMatched f k x a a
id = (k -> x -> a -> a) -> WhenMatched f k x a a
forall (f :: * -> *) k x y z.
Applicative f =>
(k -> x -> y -> z) -> WhenMatched f k x y z
zipWithMatched (\_ _ y :: a
y -> a
y)
f :: WhenMatched f k x b c
f . :: WhenMatched f k x b c
-> WhenMatched f k x a b -> WhenMatched f k x a c
. g :: WhenMatched f k x a b
g = (k -> x -> a -> f (Maybe c)) -> WhenMatched f k x a c
forall k x y (f :: * -> *) z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
zipWithMaybeAMatched ((k -> x -> a -> f (Maybe c)) -> WhenMatched f k x a c)
-> (k -> x -> a -> f (Maybe c)) -> WhenMatched f k x a c
forall a b. (a -> b) -> a -> b
$
\k :: k
k x :: x
x y :: a
y -> do
Maybe b
res <- WhenMatched f k x a b -> k -> x -> a -> f (Maybe b)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x a b
g k
k x
x a
y
case Maybe b
res of
Nothing -> Maybe c -> f (Maybe c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe c
forall a. Maybe a
Nothing
Just r :: b
r -> WhenMatched f k x b c -> k -> x -> b -> f (Maybe c)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x b c
f k
k x
x b
r
{-# INLINE id #-}
{-# INLINE (.) #-}
instance (Monad f, Applicative f) => Applicative (WhenMatched f k x y) where
pure :: a -> WhenMatched f k x y a
pure x :: a
x = (k -> x -> y -> a) -> WhenMatched f k x y a
forall (f :: * -> *) k x y z.
Applicative f =>
(k -> x -> y -> z) -> WhenMatched f k x y z
zipWithMatched (\_ _ _ -> a
x)
fs :: WhenMatched f k x y (a -> b)
fs <*> :: WhenMatched f k x y (a -> b)
-> WhenMatched f k x y a -> WhenMatched f k x y b
<*> xs :: WhenMatched f k x y a
xs = (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall k x y (f :: * -> *) z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
zipWithMaybeAMatched ((k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b)
-> (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall a b. (a -> b) -> a -> b
$ \k :: k
k x :: x
x y :: y
y -> do
Maybe (a -> b)
res <- WhenMatched f k x y (a -> b) -> k -> x -> y -> f (Maybe (a -> b))
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x y (a -> b)
fs k
k x
x y
y
case Maybe (a -> b)
res of
Nothing -> Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
Just r :: a -> b
r -> (Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe b -> f (Maybe b)) -> Maybe b -> f (Maybe b)
forall a b. (a -> b) -> a -> b
$!) (Maybe b -> f (Maybe b))
-> (Maybe a -> Maybe b) -> Maybe a -> f (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
r (Maybe a -> f (Maybe b)) -> f (Maybe a) -> f (Maybe b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< WhenMatched f k x y a -> k -> x -> y -> f (Maybe a)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x y a
xs k
k x
x y
y
{-# INLINE pure #-}
{-# INLINE (<*>) #-}
instance (Monad f, Applicative f) => Monad (WhenMatched f k x y) where
#if !MIN_VERSION_base(4,8,0)
return = pure
#endif
m :: WhenMatched f k x y a
m >>= :: WhenMatched f k x y a
-> (a -> WhenMatched f k x y b) -> WhenMatched f k x y b
>>= f :: a -> WhenMatched f k x y b
f = (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall k x y (f :: * -> *) z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
zipWithMaybeAMatched ((k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b)
-> (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall a b. (a -> b) -> a -> b
$ \k :: k
k x :: x
x y :: y
y -> do
Maybe a
res <- WhenMatched f k x y a -> k -> x -> y -> f (Maybe a)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f k x y a
m k
k x
x y
y
case Maybe a
res of
Nothing -> Maybe b -> f (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
Just r :: a
r -> WhenMatched f k x y b -> k -> x -> y -> f (Maybe b)
forall (f :: * -> *) k x y z.
WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)
runWhenMatched (a -> WhenMatched f k x y b
f a
r) k
k x
x y
y
{-# INLINE (>>=) #-}
mapWhenMatched :: Functor f
=> (a -> b)
-> WhenMatched f k x y a
-> WhenMatched f k x y b
mapWhenMatched :: (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
mapWhenMatched f :: a -> b
f (WhenMatched g :: k -> x -> y -> f (Maybe a)
g) = (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b)
-> (k -> x -> y -> f (Maybe b)) -> WhenMatched f k x y b
forall a b. (a -> b) -> a -> b
$ \k :: k
k x :: x
x y :: y
y -> (Maybe a -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (k -> x -> y -> f (Maybe a)
g k
k x
x y
y)
{-# INLINE mapWhenMatched #-}
type SimpleWhenMatched = WhenMatched Identity
zipWithMatched :: Applicative f
=> (k -> x -> y -> z)
-> WhenMatched f k x y z
zipWithMatched :: (k -> x -> y -> z) -> WhenMatched f k x y z
zipWithMatched f :: k -> x -> y -> z
f = (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z)
-> (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall a b. (a -> b) -> a -> b
$ \ k :: k
k x :: x
x y :: y
y -> Maybe z -> f (Maybe z)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe z -> f (Maybe z)) -> (z -> Maybe z) -> z -> f (Maybe z)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z -> Maybe z
forall a. a -> Maybe a
Just (z -> f (Maybe z)) -> z -> f (Maybe z)
forall a b. (a -> b) -> a -> b
$ k -> x -> y -> z
f k
k x
x y
y
{-# INLINE zipWithMatched #-}
zipWithAMatched :: Applicative f
=> (k -> x -> y -> f z)
-> WhenMatched f k x y z
zipWithAMatched :: (k -> x -> y -> f z) -> WhenMatched f k x y z
zipWithAMatched f :: k -> x -> y -> f z
f = (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z)
-> (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall a b. (a -> b) -> a -> b
$ \ k :: k
k x :: x
x y :: y
y -> z -> Maybe z
forall a. a -> Maybe a
Just (z -> Maybe z) -> f z -> f (Maybe z)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> x -> y -> f z
f k
k x
x y
y
{-# INLINE zipWithAMatched #-}
zipWithMaybeMatched :: Applicative f
=> (k -> x -> y -> Maybe z)
-> WhenMatched f k x y z
zipWithMaybeMatched :: (k -> x -> y -> Maybe z) -> WhenMatched f k x y z
zipWithMaybeMatched f :: k -> x -> y -> Maybe z
f = (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z)
-> (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall a b. (a -> b) -> a -> b
$ \ k :: k
k x :: x
x y :: y
y -> Maybe z -> f (Maybe z)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe z -> f (Maybe z)) -> Maybe z -> f (Maybe z)
forall a b. (a -> b) -> a -> b
$ k -> x -> y -> Maybe z
f k
k x
x y
y
{-# INLINE zipWithMaybeMatched #-}
zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z))
-> WhenMatched f k x y z
zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
zipWithMaybeAMatched f :: k -> x -> y -> f (Maybe z)
f = (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall (f :: * -> *) k x y z.
(k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
WhenMatched ((k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z)
-> (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z
forall a b. (a -> b) -> a -> b
$ \ k :: k
k x :: x
x y :: y
y -> k -> x -> y -> f (Maybe z)
f k
k x
x y
y
{-# INLINE zipWithMaybeAMatched #-}
dropMissing :: Applicative f => WhenMissing f k x y
dropMissing :: WhenMissing f k x y
dropMissing = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k y)
missingSubtree = f (Map k y) -> Map k x -> f (Map k y)
forall a b. a -> b -> a
const (Map k y -> f (Map k y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k y
forall k a. Map k a
Tip)
, missingKey :: k -> x -> f (Maybe y)
missingKey = \_ _ -> Maybe y -> f (Maybe y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe y
forall a. Maybe a
Nothing }
{-# INLINE dropMissing #-}
preserveMissing :: Applicative f => WhenMissing f k x x
preserveMissing :: WhenMissing f k x x
preserveMissing = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k x)
missingSubtree = Map k x -> f (Map k x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
, missingKey :: k -> x -> f (Maybe x)
missingKey = \_ v :: x
v -> Maybe x -> f (Maybe x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> Maybe x
forall a. a -> Maybe a
Just x
v) }
{-# INLINE preserveMissing #-}
preserveMissing' :: Applicative f => WhenMissing f k x x
preserveMissing' :: WhenMissing f k x x
preserveMissing' = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k x)
missingSubtree = \t :: Map k x
t -> Map k x -> f (Map k x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map k x -> f (Map k x)) -> Map k x -> f (Map k x)
forall a b. (a -> b) -> a -> b
$! Map k x -> ()
forall k a. Map k a -> ()
forceTree Map k x
t () -> Map k x -> Map k x
forall a b. a -> b -> b
`seq` Map k x
t
, missingKey :: k -> x -> f (Maybe x)
missingKey = \_ v :: x
v -> Maybe x -> f (Maybe x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe x -> f (Maybe x)) -> Maybe x -> f (Maybe x)
forall a b. (a -> b) -> a -> b
$! x -> Maybe x
forall a. a -> Maybe a
Just (x -> Maybe x) -> x -> Maybe x
forall a b. (a -> b) -> a -> b
$! x
v }
{-# INLINE preserveMissing' #-}
forceTree :: Map k a -> ()
forceTree :: Map k a -> ()
forceTree (Bin _ _ v :: a
v l :: Map k a
l r :: Map k a
r) = a
v a -> () -> ()
forall a b. a -> b -> b
`seq` Map k a -> ()
forall k a. Map k a -> ()
forceTree Map k a
l () -> () -> ()
forall a b. a -> b -> b
`seq` Map k a -> ()
forall k a. Map k a -> ()
forceTree Map k a
r () -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceTree Tip = ()
mapMissing :: Applicative f => (k -> x -> y) -> WhenMissing f k x y
mapMissing :: (k -> x -> y) -> WhenMissing f k x y
mapMissing f :: k -> x -> y
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k y)
missingSubtree = \m :: Map k x
m -> Map k y -> f (Map k y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map k y -> f (Map k y)) -> Map k y -> f (Map k y)
forall a b. (a -> b) -> a -> b
$! (k -> x -> y) -> Map k x -> Map k y
forall k a b. (k -> a -> b) -> Map k a -> Map k b
mapWithKey k -> x -> y
f Map k x
m
, missingKey :: k -> x -> f (Maybe y)
missingKey = \ k :: k
k x :: x
x -> Maybe y -> f (Maybe y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe y -> f (Maybe y)) -> Maybe y -> f (Maybe y)
forall a b. (a -> b) -> a -> b
$ y -> Maybe y
forall a. a -> Maybe a
Just (k -> x -> y
f k
k x
x) }
{-# INLINE mapMissing #-}
mapMaybeMissing :: Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y
mapMaybeMissing :: (k -> x -> Maybe y) -> WhenMissing f k x y
mapMaybeMissing f :: k -> x -> Maybe y
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k y)
missingSubtree = \m :: Map k x
m -> Map k y -> f (Map k y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map k y -> f (Map k y)) -> Map k y -> f (Map k y)
forall a b. (a -> b) -> a -> b
$! (k -> x -> Maybe y) -> Map k x -> Map k y
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> x -> Maybe y
f Map k x
m
, missingKey :: k -> x -> f (Maybe y)
missingKey = \k :: k
k x :: x
x -> Maybe y -> f (Maybe y)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe y -> f (Maybe y)) -> Maybe y -> f (Maybe y)
forall a b. (a -> b) -> a -> b
$! k -> x -> Maybe y
f k
k x
x }
{-# INLINE mapMaybeMissing #-}
filterMissing :: Applicative f
=> (k -> x -> Bool) -> WhenMissing f k x x
filterMissing :: (k -> x -> Bool) -> WhenMissing f k x x
filterMissing f :: k -> x -> Bool
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k x)
missingSubtree = \m :: Map k x
m -> Map k x -> f (Map k x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map k x -> f (Map k x)) -> Map k x -> f (Map k x)
forall a b. (a -> b) -> a -> b
$! (k -> x -> Bool) -> Map k x -> Map k x
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey k -> x -> Bool
f Map k x
m
, missingKey :: k -> x -> f (Maybe x)
missingKey = \k :: k
k x :: x
x -> Maybe x -> f (Maybe x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe x -> f (Maybe x)) -> Maybe x -> f (Maybe x)
forall a b. (a -> b) -> a -> b
$! if k -> x -> Bool
f k
k x
x then x -> Maybe x
forall a. a -> Maybe a
Just x
x else Maybe x
forall a. Maybe a
Nothing }
{-# INLINE filterMissing #-}
filterAMissing :: Applicative f
=> (k -> x -> f Bool) -> WhenMissing f k x x
filterAMissing :: (k -> x -> f Bool) -> WhenMissing f k x x
filterAMissing f :: k -> x -> f Bool
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k x)
missingSubtree = \m :: Map k x
m -> (k -> x -> f Bool) -> Map k x -> f (Map k x)
forall (f :: * -> *) k a.
Applicative f =>
(k -> a -> f Bool) -> Map k a -> f (Map k a)
filterWithKeyA k -> x -> f Bool
f Map k x
m
, missingKey :: k -> x -> f (Maybe x)
missingKey = \k :: k
k x :: x
x -> Maybe x -> Maybe x -> Bool -> Maybe x
forall a. a -> a -> Bool -> a
bool Maybe x
forall a. Maybe a
Nothing (x -> Maybe x
forall a. a -> Maybe a
Just x
x) (Bool -> Maybe x) -> f Bool -> f (Maybe x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> x -> f Bool
f k
k x
x }
{-# INLINE filterAMissing #-}
bool :: a -> a -> Bool -> a
bool :: a -> a -> Bool -> a
bool f :: a
f _ False = a
f
bool _ t :: a
t True = a
t
traverseMissing :: Applicative f
=> (k -> x -> f y) -> WhenMissing f k x y
traverseMissing :: (k -> x -> f y) -> WhenMissing f k x y
traverseMissing f :: k -> x -> f y
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k y)
missingSubtree = (k -> x -> f y) -> Map k x -> f (Map k y)
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
traverseWithKey k -> x -> f y
f
, missingKey :: k -> x -> f (Maybe y)
missingKey = \k :: k
k x :: x
x -> y -> Maybe y
forall a. a -> Maybe a
Just (y -> Maybe y) -> f y -> f (Maybe y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> x -> f y
f k
k x
x }
{-# INLINE traverseMissing #-}
traverseMaybeMissing :: Applicative f
=> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
traverseMaybeMissing :: (k -> x -> f (Maybe y)) -> WhenMissing f k x y
traverseMaybeMissing f :: k -> x -> f (Maybe y)
f = WhenMissing :: forall (f :: * -> *) k x y.
(Map k x -> f (Map k y))
-> (k -> x -> f (Maybe y)) -> WhenMissing f k x y
WhenMissing
{ missingSubtree :: Map k x -> f (Map k y)
missingSubtree = (k -> x -> f (Maybe y)) -> Map k x -> f (Map k y)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
traverseMaybeWithKey k -> x -> f (Maybe y)
f
, missingKey :: k -> x -> f (Maybe y)
missingKey = k -> x -> f (Maybe y)
f }
{-# INLINE traverseMaybeMissing #-}
merge :: Ord k
=> SimpleWhenMissing k a c
-> SimpleWhenMissing k b c
-> SimpleWhenMatched k a b c
-> Map k a
-> Map k b
-> Map k c
merge :: SimpleWhenMissing k a c
-> SimpleWhenMissing k b c
-> SimpleWhenMatched k a b c
-> Map k a
-> Map k b
-> Map k c
merge g1 :: SimpleWhenMissing k a c
g1 g2 :: SimpleWhenMissing k b c
g2 f :: SimpleWhenMatched k a b c
f m1 :: Map k a
m1 m2 :: Map k b
m2 = Identity (Map k c) -> Map k c
forall a. Identity a -> a
runIdentity (Identity (Map k c) -> Map k c) -> Identity (Map k c) -> Map k c
forall a b. (a -> b) -> a -> b
$
SimpleWhenMissing k a c
-> SimpleWhenMissing k b c
-> SimpleWhenMatched k a b c
-> Map k a
-> Map k b
-> Identity (Map k c)
forall (f :: * -> *) k a c b.
(Applicative f, Ord k) =>
WhenMissing f k a c
-> WhenMissing f k b c
-> WhenMatched f k a b c
-> Map k a
-> Map k b
-> f (Map k c)
mergeA SimpleWhenMissing k a c
g1 SimpleWhenMissing k b c
g2 SimpleWhenMatched k a b c
f Map k a
m1 Map k b
m2
{-# INLINE merge #-}
mergeA
:: (Applicative f, Ord k)
=> WhenMissing f k a c
-> WhenMissing f k b c
-> WhenMatched f k a b c
-> Map k a
-> Map k b
-> f (Map k c)
mergeA :: WhenMissing f k a c
-> WhenMissing f k b c
-> WhenMatched f k a b c
-> Map k a
-> Map k b
-> f (Map k c)
mergeA
WhenMissing{missingSubtree :: forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree = Map k a -> f (Map k c)
g1t, missingKey :: forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
missingKey = k -> a -> f (Maybe c)
g1k}
WhenMissing{missingSubtree :: forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
missingSubtree = Map k b -> f (Map k c)
g2t}
(WhenMatched f :: k -> a -> b -> f (Maybe c)
f) = Map k a -> Map k b -> f (Map k c)
go
where
go :: Map k a -> Map k b -> f (Map k c)
go t1 :: Map k a
t1 Tip = Map k a -> f (Map k c)
g1t Map k a
t1
go Tip t2 :: Map k b
t2 = Map k b -> f (Map k c)
g2t Map k b
t2
go (Bin _ kx :: k
kx x1 :: a
x1 l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k b
t2 = case k -> Map k b -> (Map k b, Maybe b, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
kx Map k b
t2 of
(l2 :: Map k b
l2, mx2 :: Maybe b
mx2, r2 :: Map k b
r2) -> case Maybe b
mx2 of
Nothing -> (Map k c -> Maybe c -> Map k c -> Map k c)
-> f (Map k c) -> f (Maybe c) -> f (Map k c) -> f (Map k c)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 (\l' :: Map k c
l' mx' :: Maybe c
mx' r' :: Map k c
r' -> (Map k c -> Map k c -> Map k c)
-> (c -> Map k c -> Map k c -> Map k c)
-> Maybe c
-> Map k c
-> Map k c
-> Map k c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 (k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx) Maybe c
mx' Map k c
l' Map k c
r')
f (Map k c)
l1l2 (k -> a -> f (Maybe c)
g1k k
kx a
x1) f (Map k c)
r1r2
Just x2 :: b
x2 -> (Map k c -> Maybe c -> Map k c -> Map k c)
-> f (Map k c) -> f (Maybe c) -> f (Map k c) -> f (Map k c)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 (\l' :: Map k c
l' mx' :: Maybe c
mx' r' :: Map k c
r' -> (Map k c -> Map k c -> Map k c)
-> (c -> Map k c -> Map k c -> Map k c)
-> Maybe c
-> Map k c
-> Map k c
-> Map k c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 (k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx) Maybe c
mx' Map k c
l' Map k c
r')
f (Map k c)
l1l2 (k -> a -> b -> f (Maybe c)
f k
kx a
x1 b
x2) f (Map k c)
r1r2
where
!l1l2 :: f (Map k c)
l1l2 = Map k a -> Map k b -> f (Map k c)
go Map k a
l1 Map k b
l2
!r1r2 :: f (Map k c)
r1r2 = Map k a -> Map k b -> f (Map k c)
go Map k a
r1 Map k b
r2
{-# INLINE mergeA #-}
mergeWithKey :: Ord k
=> (k -> a -> b -> Maybe c)
-> (Map k a -> Map k c)
-> (Map k b -> Map k c)
-> Map k a -> Map k b -> Map k c
mergeWithKey :: (k -> a -> b -> Maybe c)
-> (Map k a -> Map k c)
-> (Map k b -> Map k c)
-> Map k a
-> Map k b
-> Map k c
mergeWithKey f :: k -> a -> b -> Maybe c
f g1 :: Map k a -> Map k c
g1 g2 :: Map k b -> Map k c
g2 = Map k a -> Map k b -> Map k c
go
where
go :: Map k a -> Map k b -> Map k c
go Tip t2 :: Map k b
t2 = Map k b -> Map k c
g2 Map k b
t2
go t1 :: Map k a
t1 Tip = Map k a -> Map k c
g1 Map k a
t1
go (Bin _ kx :: k
kx x :: a
x l1 :: Map k a
l1 r1 :: Map k a
r1) t2 :: Map k b
t2 =
case Maybe b
found of
Nothing -> case Map k a -> Map k c
g1 (k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x) of
Tip -> Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 Map k c
l' Map k c
r'
(Bin _ _ x' :: c
x' Tip Tip) -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx c
x' Map k c
l' Map k c
r'
_ -> [Char] -> Map k c
forall a. HasCallStack => [Char] -> a
error "mergeWithKey: Given function only1 does not fulfill required conditions (see documentation)"
Just x2 :: b
x2 -> case k -> a -> b -> Maybe c
f k
kx a
x b
x2 of
Nothing -> Map k c -> Map k c -> Map k c
forall k a. Map k a -> Map k a -> Map k a
link2 Map k c
l' Map k c
r'
Just x' :: c
x' -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx c
x' Map k c
l' Map k c
r'
where
(l2 :: Map k b
l2, found :: Maybe b
found, r2 :: Map k b
r2) = k -> Map k b -> (Map k b, Maybe b, Map k b)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
kx Map k b
t2
l' :: Map k c
l' = Map k a -> Map k b -> Map k c
go Map k a
l1 Map k b
l2
r' :: Map k c
r' = Map k a -> Map k b -> Map k c
go Map k a
r1 Map k b
r2
{-# INLINE mergeWithKey #-}
isSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool
isSubmapOf :: Map k a -> Map k a -> Bool
isSubmapOf m1 :: Map k a
m1 m2 :: Map k a
m2 = (a -> a -> Bool) -> Map k a -> Map k a -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Map k a
m1 Map k a
m2
#if __GLASGOW_HASKELL__
{-# INLINABLE isSubmapOf #-}
#endif
isSubmapOfBy :: Ord k => (a->b->Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy :: (a -> b -> Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy f :: a -> b -> Bool
f t1 :: Map k a
t1 t2 :: Map k b
t2
= Map k a -> Int
forall k a. Map k a -> Int
size Map k a
t1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Map k b -> Int
forall k a. Map k a -> Int
size Map k b
t2 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Map k a -> Map k b -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
submap' a -> b -> Bool
f Map k a
t1 Map k b
t2
#if __GLASGOW_HASKELL__
{-# INLINABLE isSubmapOfBy #-}
#endif
submap' :: Ord a => (b -> c -> Bool) -> Map a b -> Map a c -> Bool
submap' :: (b -> c -> Bool) -> Map a b -> Map a c -> Bool
submap' _ Tip _ = Bool
True
submap' _ _ Tip = Bool
False
submap' f :: b -> c -> Bool
f (Bin 1 kx :: a
kx x :: b
x _ _) t :: Map a c
t
= case a -> Map a c -> Maybe c
forall k a. Ord k => k -> Map k a -> Maybe a
lookup a
kx Map a c
t of
Just y :: c
y -> b -> c -> Bool
f b
x c
y
Nothing -> Bool
False
submap' f :: b -> c -> Bool
f (Bin _ kx :: a
kx x :: b
x l :: Map a b
l r :: Map a b
r) t :: Map a c
t
= case Maybe c
found of
Nothing -> Bool
False
Just y :: c
y -> b -> c -> Bool
f b
x c
y
Bool -> Bool -> Bool
&& Map a b -> Int
forall k a. Map k a -> Int
size Map a b
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Map a c -> Int
forall k a. Map k a -> Int
size Map a c
lt Bool -> Bool -> Bool
&& Map a b -> Int
forall k a. Map k a -> Int
size Map a b
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Map a c -> Int
forall k a. Map k a -> Int
size Map a c
gt
Bool -> Bool -> Bool
&& (b -> c -> Bool) -> Map a b -> Map a c -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
submap' b -> c -> Bool
f Map a b
l Map a c
lt Bool -> Bool -> Bool
&& (b -> c -> Bool) -> Map a b -> Map a c -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
submap' b -> c -> Bool
f Map a b
r Map a c
gt
where
(lt :: Map a c
lt,found :: Maybe c
found,gt :: Map a c
gt) = a -> Map a c -> (Map a c, Maybe c, Map a c)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup a
kx Map a c
t
#if __GLASGOW_HASKELL__
{-# INLINABLE submap' #-}
#endif
isProperSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool
isProperSubmapOf :: Map k a -> Map k a -> Bool
isProperSubmapOf m1 :: Map k a
m1 m2 :: Map k a
m2
= (a -> a -> Bool) -> Map k a -> Map k a -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
isProperSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Map k a
m1 Map k a
m2
#if __GLASGOW_HASKELL__
{-# INLINABLE isProperSubmapOf #-}
#endif
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
isProperSubmapOfBy :: (a -> b -> Bool) -> Map k a -> Map k b -> Bool
isProperSubmapOfBy f :: a -> b -> Bool
f t1 :: Map k a
t1 t2 :: Map k b
t2
= Map k a -> Int
forall k a. Map k a -> Int
size Map k a
t1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Map k b -> Int
forall k a. Map k a -> Int
size Map k b
t2 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Map k a -> Map k b -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
submap' a -> b -> Bool
f Map k a
t1 Map k b
t2
#if __GLASGOW_HASKELL__
{-# INLINABLE isProperSubmapOfBy #-}
#endif
filter :: (a -> Bool) -> Map k a -> Map k a
filter :: (a -> Bool) -> Map k a -> Map k a
filter p :: a -> Bool
p m :: Map k a
m
= (k -> a -> Bool) -> Map k a -> Map k a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey (\_ x :: a
x -> a -> Bool
p a
x) Map k a
m
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey _ Tip = Map k a
forall k a. Map k a
Tip
filterWithKey p :: k -> a -> Bool
p t :: Map k a
t@(Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
| k -> a -> Bool
p k
kx a
x = if Map k a
pl Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l Bool -> Bool -> Bool
&& Map k a
pr Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r
then Map k a
t
else k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
pl Map k a
pr
| Bool
otherwise = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
pl Map k a
pr
where !pl :: Map k a
pl = (k -> a -> Bool) -> Map k a -> Map k a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey k -> a -> Bool
p Map k a
l
!pr :: Map k a
pr = (k -> a -> Bool) -> Map k a -> Map k a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey k -> a -> Bool
p Map k a
r
filterWithKeyA :: Applicative f => (k -> a -> f Bool) -> Map k a -> f (Map k a)
filterWithKeyA :: (k -> a -> f Bool) -> Map k a -> f (Map k a)
filterWithKeyA _ Tip = Map k a -> f (Map k a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k a
forall k a. Map k a
Tip
filterWithKeyA p :: k -> a -> f Bool
p t :: Map k a
t@(Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) =
(Bool -> Map k a -> Map k a -> Map k a)
-> f Bool -> f (Map k a) -> f (Map k a) -> f (Map k a)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 Bool -> Map k a -> Map k a -> Map k a
combine (k -> a -> f Bool
p k
kx a
x) ((k -> a -> f Bool) -> Map k a -> f (Map k a)
forall (f :: * -> *) k a.
Applicative f =>
(k -> a -> f Bool) -> Map k a -> f (Map k a)
filterWithKeyA k -> a -> f Bool
p Map k a
l) ((k -> a -> f Bool) -> Map k a -> f (Map k a)
forall (f :: * -> *) k a.
Applicative f =>
(k -> a -> f Bool) -> Map k a -> f (Map k a)
filterWithKeyA k -> a -> f Bool
p Map k a
r)
where
combine :: Bool -> Map k a -> Map k a -> Map k a
combine True pl :: Map k a
pl pr :: Map k a
pr
| Map k a
pl Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l Bool -> Bool -> Bool
&& Map k a
pr Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r = Map k a
t
| Bool
otherwise = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
pl Map k a
pr
combine False pl :: Map k a
pl pr :: Map k a
pr = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
pl Map k a
pr
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
takeWhileAntitone _ Tip = Map k a
forall k a. Map k a
Tip
takeWhileAntitone p :: k -> Bool
p (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
| k -> Bool
p k
kx = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l ((k -> Bool) -> Map k a -> Map k a
forall k a. (k -> Bool) -> Map k a -> Map k a
takeWhileAntitone k -> Bool
p Map k a
r)
| Bool
otherwise = (k -> Bool) -> Map k a -> Map k a
forall k a. (k -> Bool) -> Map k a -> Map k a
takeWhileAntitone k -> Bool
p Map k a
l
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a
dropWhileAntitone _ Tip = Map k a
forall k a. Map k a
Tip
dropWhileAntitone p :: k -> Bool
p (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
| k -> Bool
p k
kx = (k -> Bool) -> Map k a -> Map k a
forall k a. (k -> Bool) -> Map k a -> Map k a
dropWhileAntitone k -> Bool
p Map k a
r
| Bool
otherwise = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x ((k -> Bool) -> Map k a -> Map k a
forall k a. (k -> Bool) -> Map k a -> Map k a
dropWhileAntitone k -> Bool
p Map k a
l) Map k a
r
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)
spanAntitone p0 :: k -> Bool
p0 m :: Map k a
m = StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair ((k -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
forall k a.
(k -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> Bool
p0 Map k a
m)
where
go :: (k -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go _ Tip = Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip
go p :: k -> Bool
p (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
| k -> Bool
p k
kx = let u :: Map k a
u :*: v :: Map k a
v = (k -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> Bool
p Map k a
r in k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
u Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
v
| Bool
otherwise = let u :: Map k a
u :*: v :: Map k a
v = (k -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> Bool
p Map k a
l in Map k a
u Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
v Map k a
r
partition :: (a -> Bool) -> Map k a -> (Map k a,Map k a)
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)
partition p :: a -> Bool
p m :: Map k a
m
= (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
forall k a. (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
partitionWithKey (\_ x :: a
x -> a -> Bool
p a
x) Map k a
m
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a,Map k a)
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
partitionWithKey p0 :: k -> a -> Bool
p0 t0 :: Map k a
t0 = StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Map k a) (Map k a) -> (Map k a, Map k a))
-> StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. (a -> b) -> a -> b
$ (k -> a -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
forall k a.
(k -> a -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> a -> Bool
p0 Map k a
t0
where
go :: (k -> a -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go _ Tip = (Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip)
go p :: k -> a -> Bool
p t :: Map k a
t@(Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r)
| k -> a -> Bool
p k
kx a
x = (if Map k a
l1 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l Bool -> Bool -> Bool
&& Map k a
r1 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r
then Map k a
t
else k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l1 Map k a
r1) Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l2 Map k a
r2
| Bool
otherwise = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l1 Map k a
r1 Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*:
(if Map k a
l2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
l Bool -> Bool -> Bool
&& Map k a
r2 Map k a -> Map k a -> Bool
forall a. a -> a -> Bool
`ptrEq` Map k a
r
then Map k a
t
else k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l2 Map k a
r2)
where
(l1 :: Map k a
l1 :*: l2 :: Map k a
l2) = (k -> a -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> a -> Bool
p Map k a
l
(r1 :: Map k a
r1 :*: r2 :: Map k a
r2) = (k -> a -> Bool) -> Map k a -> StrictPair (Map k a) (Map k a)
go k -> a -> Bool
p Map k a
r
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b
mapMaybe f :: a -> Maybe b
f = (k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey (\_ x :: a
x -> a -> Maybe b
f a
x)
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey _ Tip = Map k b
forall k a. Map k a
Tip
mapMaybeWithKey f :: k -> a -> Maybe b
f (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = case k -> a -> Maybe b
f k
kx a
x of
Just y :: b
y -> k -> b -> Map k b -> Map k b -> Map k b
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx b
y ((k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f Map k a
l) ((k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f Map k a
r)
Nothing -> Map k b -> Map k b -> Map k b
forall k a. Map k a -> Map k a -> Map k a
link2 ((k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f Map k a
l) ((k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f Map k a
r)
traverseMaybeWithKey :: Applicative f
=> (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
traverseMaybeWithKey :: (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
traverseMaybeWithKey = (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
go
where
go :: (k -> t -> f (Maybe a)) -> Map k t -> f (Map k a)
go _ Tip = Map k a -> f (Map k a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k a
forall k a. Map k a
Tip
go f :: k -> t -> f (Maybe a)
f (Bin _ kx :: k
kx x :: t
x Tip Tip) = Map k a -> (a -> Map k a) -> Maybe a -> Map k a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map k a
forall k a. Map k a
Tip (\x' :: a
x' -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x' Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) (Maybe a -> Map k a) -> f (Maybe a) -> f (Map k a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> t -> f (Maybe a)
f k
kx t
x
go f :: k -> t -> f (Maybe a)
f (Bin _ kx :: k
kx x :: t
x l :: Map k t
l r :: Map k t
r) = (Map k a -> Maybe a -> Map k a -> Map k a)
-> f (Map k a) -> f (Maybe a) -> f (Map k a) -> f (Map k a)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 Map k a -> Maybe a -> Map k a -> Map k a
combine ((k -> t -> f (Maybe a)) -> Map k t -> f (Map k a)
go k -> t -> f (Maybe a)
f Map k t
l) (k -> t -> f (Maybe a)
f k
kx t
x) ((k -> t -> f (Maybe a)) -> Map k t -> f (Map k a)
go k -> t -> f (Maybe a)
f Map k t
r)
where
combine :: Map k a -> Maybe a -> Map k a -> Map k a
combine !Map k a
l' mx :: Maybe a
mx !Map k a
r' = case Maybe a
mx of
Nothing -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l' Map k a
r'
Just x' :: a
x' -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x' Map k a
l' Map k a
r'
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEither f :: a -> Either b c
f m :: Map k a
m
= (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
forall k a b c.
(k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEitherWithKey (\_ x :: a
x -> a -> Either b c
f a
x) Map k a
m
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEitherWithKey f0 :: k -> a -> Either b c
f0 t0 :: Map k a
t0 = StrictPair (Map k b) (Map k c) -> (Map k b, Map k c)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Map k b) (Map k c) -> (Map k b, Map k c))
-> StrictPair (Map k b) (Map k c) -> (Map k b, Map k c)
forall a b. (a -> b) -> a -> b
$ (k -> a -> Either b c) -> Map k a -> StrictPair (Map k b) (Map k c)
forall k t a a.
(k -> t -> Either a a) -> Map k t -> StrictPair (Map k a) (Map k a)
go k -> a -> Either b c
f0 Map k a
t0
where
go :: (k -> t -> Either a a) -> Map k t -> StrictPair (Map k a) (Map k a)
go _ Tip = (Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip)
go f :: k -> t -> Either a a
f (Bin _ kx :: k
kx x :: t
x l :: Map k t
l r :: Map k t
r) = case k -> t -> Either a a
f k
kx t
x of
Left y :: a
y -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
y Map k a
l1 Map k a
r1 Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l2 Map k a
r2
Right z :: a
z -> Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l1 Map k a
r1 Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
z Map k a
l2 Map k a
r2
where
(l1 :: Map k a
l1 :*: l2 :: Map k a
l2) = (k -> t -> Either a a) -> Map k t -> StrictPair (Map k a) (Map k a)
go k -> t -> Either a a
f Map k t
l
(r1 :: Map k a
r1 :*: r2 :: Map k a
r2) = (k -> t -> Either a a) -> Map k t -> StrictPair (Map k a) (Map k a)
go k -> t -> Either a a
f Map k t
r
map :: (a -> b) -> Map k a -> Map k b
map :: (a -> b) -> Map k a -> Map k b
map f :: a -> b
f = Map k a -> Map k b
go where
go :: Map k a -> Map k b
go Tip = Map k b
forall k a. Map k a
Tip
go (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = Int -> k -> b -> Map k b -> Map k b -> Map k b
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx (a -> b
f a
x) (Map k a -> Map k b
go Map k a
l) (Map k a -> Map k b
go Map k a
r)
#ifdef __GLASGOW_HASKELL__
{-# NOINLINE [1] map #-}
{-# RULES
"map/map" forall f g xs . map f (map g xs) = map (f . g) xs
#-}
#endif
#if __GLASGOW_HASKELL__ >= 709
{-# RULES
"map/coerce" map coerce = coerce
#-}
#endif
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey _ Tip = Map k b
forall k a. Map k a
Tip
mapWithKey f :: k -> a -> b
f (Bin sx :: Int
sx kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = Int -> k -> b -> Map k b -> Map k b -> Map k b
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx (k -> a -> b
f k
kx a
x) ((k -> a -> b) -> Map k a -> Map k b
forall k a b. (k -> a -> b) -> Map k a -> Map k b
mapWithKey k -> a -> b
f Map k a
l) ((k -> a -> b) -> Map k a -> Map k b
forall k a b. (k -> a -> b) -> Map k a -> Map k b
mapWithKey k -> a -> b
f Map k a
r)
#ifdef __GLASGOW_HASKELL__
{-# NOINLINE [1] mapWithKey #-}
{-# RULES
"mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
mapWithKey (\k a -> f k (g k a)) xs
"mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
mapWithKey (\k a -> f k (g a)) xs
"map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
mapWithKey (\k a -> f (g k a)) xs
#-}
#endif
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)
traverseWithKey :: (k -> a -> t b) -> Map k a -> t (Map k b)
traverseWithKey f :: k -> a -> t b
f = Map k a -> t (Map k b)
go
where
go :: Map k a -> t (Map k b)
go Tip = Map k b -> t (Map k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k b
forall k a. Map k a
Tip
go (Bin 1 k :: k
k v :: a
v _ _) = (\v' :: b
v' -> Int -> k -> b -> Map k b -> Map k b -> Map k b
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k b
v' Map k b
forall k a. Map k a
Tip Map k b
forall k a. Map k a
Tip) (b -> Map k b) -> t b -> t (Map k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> t b
f k
k a
v
go (Bin s :: Int
s k :: k
k v :: a
v l :: Map k a
l r :: Map k a
r) = (Map k b -> b -> Map k b -> Map k b)
-> t (Map k b) -> t b -> t (Map k b) -> t (Map k b)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 ((b -> Map k b -> Map k b -> Map k b)
-> Map k b -> b -> Map k b -> Map k b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> k -> b -> Map k b -> Map k b -> Map k b
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
s k
k)) (Map k a -> t (Map k b)
go Map k a
l) (k -> a -> t b
f k
k a
v) (Map k a -> t (Map k b)
go Map k a
r)
{-# INLINE traverseWithKey #-}
mapAccum :: (a -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccum f :: a -> b -> (a, c)
f a :: a
a m :: Map k b
m
= (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumWithKey (\a' :: a
a' _ x' :: b
x' -> a -> b -> (a, c)
f a
a' b
x') a
a Map k b
m
mapAccumWithKey :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumWithKey f :: a -> k -> b -> (a, c)
f a :: a
a t :: Map k b
t
= (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumL a -> k -> b -> (a, c)
f a
a Map k b
t
mapAccumL :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccumL :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumL _ a :: a
a Tip = (a
a,Map k c
forall k a. Map k a
Tip)
mapAccumL f :: a -> k -> b -> (a, c)
f a :: a
a (Bin sx :: Int
sx kx :: k
kx x :: b
x l :: Map k b
l r :: Map k b
r) =
let (a1 :: a
a1,l' :: Map k c
l') = (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumL a -> k -> b -> (a, c)
f a
a Map k b
l
(a2 :: a
a2,x' :: c
x') = a -> k -> b -> (a, c)
f a
a1 k
kx b
x
(a3 :: a
a3,r' :: Map k c
r') = (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumL a -> k -> b -> (a, c)
f a
a2 Map k b
r
in (a
a3,Int -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx c
x' Map k c
l' Map k c
r')
mapAccumRWithKey :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumRWithKey _ a :: a
a Tip = (a
a,Map k c
forall k a. Map k a
Tip)
mapAccumRWithKey f :: a -> k -> b -> (a, c)
f a :: a
a (Bin sx :: Int
sx kx :: k
kx x :: b
x l :: Map k b
l r :: Map k b
r) =
let (a1 :: a
a1,r' :: Map k c
r') = (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumRWithKey a -> k -> b -> (a, c)
f a
a Map k b
r
(a2 :: a
a2,x' :: c
x') = a -> k -> b -> (a, c)
f a
a1 k
kx b
x
(a3 :: a
a3,l' :: Map k c
l') = (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumRWithKey a -> k -> b -> (a, c)
f a
a2 Map k b
l
in (a
a3,Int -> k -> c -> Map k c -> Map k c -> Map k c
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx c
x' Map k c
l' Map k c
r')
mapKeys :: Ord k2 => (k1->k2) -> Map k1 a -> Map k2 a
mapKeys :: (k1 -> k2) -> Map k1 a -> Map k2 a
mapKeys f :: k1 -> k2
f = [(k2, a)] -> Map k2 a
forall k a. Ord k => [(k, a)] -> Map k a
fromList ([(k2, a)] -> Map k2 a)
-> (Map k1 a -> [(k2, a)]) -> Map k1 a -> Map k2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k1 -> a -> [(k2, a)] -> [(k2, a)])
-> [(k2, a)] -> Map k1 a -> [(k2, a)]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey (\k :: k1
k x :: a
x xs :: [(k2, a)]
xs -> (k1 -> k2
f k1
k, a
x) (k2, a) -> [(k2, a)] -> [(k2, a)]
forall a. a -> [a] -> [a]
: [(k2, a)]
xs) []
#if __GLASGOW_HASKELL__
{-# INLINABLE mapKeys #-}
#endif
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1->k2) -> Map k1 a -> Map k2 a
mapKeysWith :: (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
mapKeysWith c :: a -> a -> a
c f :: k1 -> k2
f = (a -> a -> a) -> [(k2, a)] -> Map k2 a
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
fromListWith a -> a -> a
c ([(k2, a)] -> Map k2 a)
-> (Map k1 a -> [(k2, a)]) -> Map k1 a -> Map k2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k1 -> a -> [(k2, a)] -> [(k2, a)])
-> [(k2, a)] -> Map k1 a -> [(k2, a)]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey (\k :: k1
k x :: a
x xs :: [(k2, a)]
xs -> (k1 -> k2
f k1
k, a
x) (k2, a) -> [(k2, a)] -> [(k2, a)]
forall a. a -> [a] -> [a]
: [(k2, a)]
xs) []
#if __GLASGOW_HASKELL__
{-# INLINABLE mapKeysWith #-}
#endif
mapKeysMonotonic :: (k1->k2) -> Map k1 a -> Map k2 a
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a
mapKeysMonotonic _ Tip = Map k2 a
forall k a. Map k a
Tip
mapKeysMonotonic f :: k1 -> k2
f (Bin sz :: Int
sz k :: k1
k x :: a
x l :: Map k1 a
l r :: Map k1 a
r) =
Int -> k2 -> a -> Map k2 a -> Map k2 a -> Map k2 a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz (k1 -> k2
f k1
k) a
x ((k1 -> k2) -> Map k1 a -> Map k2 a
forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
mapKeysMonotonic k1 -> k2
f Map k1 a
l) ((k1 -> k2) -> Map k1 a -> Map k2 a
forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
mapKeysMonotonic k1 -> k2
f Map k1 a
r)
foldr :: (a -> b -> b) -> b -> Map k a -> b
foldr :: (a -> b -> b) -> b -> Map k a -> b
foldr f :: a -> b -> b
f z :: b
z = b -> Map k a -> b
go b
z
where
go :: b -> Map k a -> b
go z' :: b
z' Tip = b
z'
go z' :: b
z' (Bin _ _ x :: a
x l :: Map k a
l r :: Map k a
r) = b -> Map k a -> b
go (a -> b -> b
f a
x (b -> Map k a -> b
go b
z' Map k a
r)) Map k a
l
{-# INLINE foldr #-}
foldr' :: (a -> b -> b) -> b -> Map k a -> b
foldr' :: (a -> b -> b) -> b -> Map k a -> b
foldr' f :: a -> b -> b
f z :: b
z = b -> Map k a -> b
go b
z
where
go :: b -> Map k a -> b
go !b
z' Tip = b
z'
go z' :: b
z' (Bin _ _ x :: a
x l :: Map k a
l r :: Map k a
r) = b -> Map k a -> b
go (a -> b -> b
f a
x (b -> Map k a -> b
go b
z' Map k a
r)) Map k a
l
{-# INLINE foldr' #-}
foldl :: (a -> b -> a) -> a -> Map k b -> a
foldl :: (a -> b -> a) -> a -> Map k b -> a
foldl f :: a -> b -> a
f z :: a
z = a -> Map k b -> a
go a
z
where
go :: a -> Map k b -> a
go z' :: a
z' Tip = a
z'
go z' :: a
z' (Bin _ _ x :: b
x l :: Map k b
l r :: Map k b
r) = a -> Map k b -> a
go (a -> b -> a
f (a -> Map k b -> a
go a
z' Map k b
l) b
x) Map k b
r
{-# INLINE foldl #-}
foldl' :: (a -> b -> a) -> a -> Map k b -> a
foldl' :: (a -> b -> a) -> a -> Map k b -> a
foldl' f :: a -> b -> a
f z :: a
z = a -> Map k b -> a
go a
z
where
go :: a -> Map k b -> a
go !a
z' Tip = a
z'
go z' :: a
z' (Bin _ _ x :: b
x l :: Map k b
l r :: Map k b
r) = a -> Map k b -> a
go (a -> b -> a
f (a -> Map k b -> a
go a
z' Map k b
l) b
x) Map k b
r
{-# INLINE foldl' #-}
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey f :: k -> a -> b -> b
f z :: b
z = b -> Map k a -> b
go b
z
where
go :: b -> Map k a -> b
go z' :: b
z' Tip = b
z'
go z' :: b
z' (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = b -> Map k a -> b
go (k -> a -> b -> b
f k
kx a
x (b -> Map k a -> b
go b
z' Map k a
r)) Map k a
l
{-# INLINE foldrWithKey #-}
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey' f :: k -> a -> b -> b
f z :: b
z = b -> Map k a -> b
go b
z
where
go :: b -> Map k a -> b
go !b
z' Tip = b
z'
go z' :: b
z' (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = b -> Map k a -> b
go (k -> a -> b -> b
f k
kx a
x (b -> Map k a -> b
go b
z' Map k a
r)) Map k a
l
{-# INLINE foldrWithKey' #-}
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey f :: a -> k -> b -> a
f z :: a
z = a -> Map k b -> a
go a
z
where
go :: a -> Map k b -> a
go z' :: a
z' Tip = a
z'
go z' :: a
z' (Bin _ kx :: k
kx x :: b
x l :: Map k b
l r :: Map k b
r) = a -> Map k b -> a
go (a -> k -> b -> a
f (a -> Map k b -> a
go a
z' Map k b
l) k
kx b
x) Map k b
r
{-# INLINE foldlWithKey #-}
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey' f :: a -> k -> b -> a
f z :: a
z = a -> Map k b -> a
go a
z
where
go :: a -> Map k b -> a
go !a
z' Tip = a
z'
go z' :: a
z' (Bin _ kx :: k
kx x :: b
x l :: Map k b
l r :: Map k b
r) = a -> Map k b -> a
go (a -> k -> b -> a
f (a -> Map k b -> a
go a
z' Map k b
l) k
kx b
x) Map k b
r
{-# INLINE foldlWithKey' #-}
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m
foldMapWithKey :: (k -> a -> m) -> Map k a -> m
foldMapWithKey f :: k -> a -> m
f = Map k a -> m
go
where
go :: Map k a -> m
go Tip = m
forall a. Monoid a => a
mempty
go (Bin 1 k :: k
k v :: a
v _ _) = k -> a -> m
f k
k a
v
go (Bin _ k :: k
k v :: a
v l :: Map k a
l r :: Map k a
r) = Map k a -> m
go Map k a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` (k -> a -> m
f k
k a
v m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Map k a -> m
go Map k a
r)
{-# INLINE foldMapWithKey #-}
elems :: Map k a -> [a]
elems :: Map k a -> [a]
elems = (a -> [a] -> [a]) -> [a] -> Map k a -> [a]
forall a b k. (a -> b -> b) -> b -> Map k a -> b
foldr (:) []
keys :: Map k a -> [k]
keys :: Map k a -> [k]
keys = (k -> a -> [k] -> [k]) -> [k] -> Map k a -> [k]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey (\k :: k
k _ ks :: [k]
ks -> k
k k -> [k] -> [k]
forall a. a -> [a] -> [a]
: [k]
ks) []
assocs :: Map k a -> [(k,a)]
assocs :: Map k a -> [(k, a)]
assocs m :: Map k a
m
= Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toAscList Map k a
m
keysSet :: Map k a -> Set.Set k
keysSet :: Map k a -> Set k
keysSet Tip = Set k
forall a. Set a
Set.Tip
keysSet (Bin sz :: Int
sz kx :: k
kx _ l :: Map k a
l r :: Map k a
r) = Int -> k -> Set k -> Set k -> Set k
forall a. Int -> a -> Set a -> Set a -> Set a
Set.Bin Int
sz k
kx (Map k a -> Set k
forall k a. Map k a -> Set k
keysSet Map k a
l) (Map k a -> Set k
forall k a. Map k a -> Set k
keysSet Map k a
r)
fromSet :: (k -> a) -> Set.Set k -> Map k a
fromSet :: (k -> a) -> Set k -> Map k a
fromSet _ Set.Tip = Map k a
forall k a. Map k a
Tip
fromSet f :: k -> a
f (Set.Bin sz :: Int
sz x :: k
x l :: Set k
l r :: Set k
r) = Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sz k
x (k -> a
f k
x) ((k -> a) -> Set k -> Map k a
forall k a. (k -> a) -> Set k -> Map k a
fromSet k -> a
f Set k
l) ((k -> a) -> Set k -> Map k a
forall k a. (k -> a) -> Set k -> Map k a
fromSet k -> a
f Set k
r)
#if __GLASGOW_HASKELL__ >= 708
instance (Ord k) => GHCExts.IsList (Map k v) where
type Item (Map k v) = (k,v)
fromList :: [Item (Map k v)] -> Map k v
fromList = [Item (Map k v)] -> Map k v
forall k a. Ord k => [(k, a)] -> Map k a
fromList
toList :: Map k v -> [Item (Map k v)]
toList = Map k v -> [Item (Map k v)]
forall k a. Map k a -> [(k, a)]
toList
#endif
fromList :: Ord k => [(k,a)] -> Map k a
fromList :: [(k, a)] -> Map k a
fromList [] = Map k a
forall k a. Map k a
Tip
fromList [(kx :: k
kx, x :: a
x)] = Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip
fromList ((kx0 :: k
kx0, x0 :: a
x0) : xs0 :: [(k, a)]
xs0) | k -> [(k, a)] -> Bool
forall a b. Ord a => a -> [(a, b)] -> Bool
not_ordered k
kx0 [(k, a)]
xs0 = Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) k a.
(Foldable t, Ord k) =>
Map k a -> t (k, a) -> Map k a
fromList' (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx0 a
x0 Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) [(k, a)]
xs0
| Bool
otherwise = Int -> Map k a -> [(k, a)] -> Map k a
forall k t a.
(Ord k, Num t, Bits t) =>
t -> Map k a -> [(k, a)] -> Map k a
go (1::Int) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx0 a
x0 Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) [(k, a)]
xs0
where
not_ordered :: a -> [(a, b)] -> Bool
not_ordered _ [] = Bool
False
not_ordered kx :: a
kx ((ky :: a
ky,_) : _) = a
kx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
ky
{-# INLINE not_ordered #-}
fromList' :: Map k a -> t (k, a) -> Map k a
fromList' t0 :: Map k a
t0 xs :: t (k, a)
xs = (Map k a -> (k, a) -> Map k a) -> Map k a -> t (k, a) -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Map k a -> (k, a) -> Map k a
forall k a. Ord k => Map k a -> (k, a) -> Map k a
ins Map k a
t0 t (k, a)
xs
where ins :: Map k a -> (k, a) -> Map k a
ins t :: Map k a
t (k :: k
k,x :: a
x) = k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert k
k a
x Map k a
t
go :: t -> Map k a -> [(k, a)] -> Map k a
go !t
_ t :: Map k a
t [] = Map k a
t
go _ t :: Map k a
t [(kx :: k
kx, x :: a
x)] = k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMax k
kx a
x Map k a
t
go s :: t
s l :: Map k a
l xs :: [(k, a)]
xs@((kx :: k
kx, x :: a
x) : xss :: [(k, a)]
xss) | k -> [(k, a)] -> Bool
forall a b. Ord a => a -> [(a, b)] -> Bool
not_ordered k
kx [(k, a)]
xss = Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) k a.
(Foldable t, Ord k) =>
Map k a -> t (k, a) -> Map k a
fromList' Map k a
l [(k, a)]
xs
| Bool
otherwise = case t -> [(k, a)] -> (Map k a, [(k, a)], [(k, a)])
forall a k a.
(Num a, Ord k, Bits a) =>
a -> [(k, a)] -> (Map k a, [(k, a)], [(k, a)])
create t
s [(k, a)]
xss of
(r :: Map k a
r, ys :: [(k, a)]
ys, []) -> t -> Map k a -> [(k, a)] -> Map k a
go (t
s t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` 1) (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
r) [(k, a)]
ys
(r :: Map k a
r, _, ys :: [(k, a)]
ys) -> Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) k a.
(Foldable t, Ord k) =>
Map k a -> t (k, a) -> Map k a
fromList' (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
r) [(k, a)]
ys
create :: a -> [(k, a)] -> (Map k a, [(k, a)], [(k, a)])
create !a
_ [] = (Map k a
forall k a. Map k a
Tip, [], [])
create s :: a
s xs :: [(k, a)]
xs@(xp :: (k, a)
xp : xss :: [(k, a)]
xss)
| a
s a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = case (k, a)
xp of (kx :: k
kx, x :: a
x) | k -> [(k, a)] -> Bool
forall a b. Ord a => a -> [(a, b)] -> Bool
not_ordered k
kx [(k, a)]
xss -> (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip, [], [(k, a)]
xss)
| Bool
otherwise -> (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip, [(k, a)]
xss, [])
| Bool
otherwise = case a -> [(k, a)] -> (Map k a, [(k, a)], [(k, a)])
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
xs of
res :: (Map k a, [(k, a)], [(k, a)])
res@(_, [], _) -> (Map k a, [(k, a)], [(k, a)])
res
(l :: Map k a
l, [(ky :: k
ky, y :: a
y)], zs :: [(k, a)]
zs) -> (k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMax k
ky a
y Map k a
l, [], [(k, a)]
zs)
(l :: Map k a
l, ys :: [(k, a)]
ys@((ky :: k
ky, y :: a
y):yss :: [(k, a)]
yss), _) | k -> [(k, a)] -> Bool
forall a b. Ord a => a -> [(a, b)] -> Bool
not_ordered k
ky [(k, a)]
yss -> (Map k a
l, [], [(k, a)]
ys)
| Bool
otherwise -> case a -> [(k, a)] -> (Map k a, [(k, a)], [(k, a)])
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
yss of
(r :: Map k a
r, zs :: [(k, a)]
zs, ws :: [(k, a)]
ws) -> (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
ky a
y Map k a
l Map k a
r, [(k, a)]
zs, [(k, a)]
ws)
#if __GLASGOW_HASKELL__
{-# INLINABLE fromList #-}
#endif
fromListWith :: Ord k => (a -> a -> a) -> [(k,a)] -> Map k a
fromListWith :: (a -> a -> a) -> [(k, a)] -> Map k a
fromListWith f :: a -> a -> a
f xs :: [(k, a)]
xs
= (k -> a -> a -> a) -> [(k, a)] -> Map k a
forall k a. Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromListWithKey (\_ x :: a
x y :: a
y -> a -> a -> a
f a
x a
y) [(k, a)]
xs
#if __GLASGOW_HASKELL__
{-# INLINABLE fromListWith #-}
#endif
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromListWithKey f :: k -> a -> a -> a
f xs :: [(k, a)]
xs
= (Map k a -> (k, a) -> Map k a) -> Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Map k a -> (k, a) -> Map k a
ins Map k a
forall k a. Map k a
empty [(k, a)]
xs
where
ins :: Map k a -> (k, a) -> Map k a
ins t :: Map k a
t (k :: k
k,x :: a
x) = (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey k -> a -> a -> a
f k
k a
x Map k a
t
#if __GLASGOW_HASKELL__
{-# INLINABLE fromListWithKey #-}
#endif
toList :: Map k a -> [(k,a)]
toList :: Map k a -> [(k, a)]
toList = Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toAscList
toAscList :: Map k a -> [(k,a)]
toAscList :: Map k a -> [(k, a)]
toAscList = (k -> a -> [(k, a)] -> [(k, a)]) -> [(k, a)] -> Map k a -> [(k, a)]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey (\k :: k
k x :: a
x xs :: [(k, a)]
xs -> (k
k,a
x)(k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
:[(k, a)]
xs) []
toDescList :: Map k a -> [(k,a)]
toDescList :: Map k a -> [(k, a)]
toDescList = ([(k, a)] -> k -> a -> [(k, a)]) -> [(k, a)] -> Map k a -> [(k, a)]
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey (\xs :: [(k, a)]
xs k :: k
k x :: a
x -> (k
k,a
x)(k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
:[(k, a)]
xs) []
#if __GLASGOW_HASKELL__
foldrFB :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrFB :: (k -> a -> b -> b) -> b -> Map k a -> b
foldrFB = (k -> a -> b -> b) -> b -> Map k a -> b
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldrWithKey
{-# INLINE[0] foldrFB #-}
foldlFB :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlFB :: (a -> k -> b -> a) -> a -> Map k b -> a
foldlFB = (a -> k -> b -> a) -> a -> Map k b -> a
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
foldlWithKey
{-# INLINE[0] foldlFB #-}
{-# INLINE assocs #-}
{-# INLINE toList #-}
{-# NOINLINE[0] elems #-}
{-# NOINLINE[0] keys #-}
{-# NOINLINE[0] toAscList #-}
{-# NOINLINE[0] toDescList #-}
{-# RULES "Map.elems" [~1] forall m . elems m = build (\c n -> foldrFB (\_ x xs -> c x xs) n m) #-}
{-# RULES "Map.elemsBack" [1] foldrFB (\_ x xs -> x : xs) [] = elems #-}
{-# RULES "Map.keys" [~1] forall m . keys m = build (\c n -> foldrFB (\k _ xs -> c k xs) n m) #-}
{-# RULES "Map.keysBack" [1] foldrFB (\k _ xs -> k : xs) [] = keys #-}
{-# RULES "Map.toAscList" [~1] forall m . toAscList m = build (\c n -> foldrFB (\k x xs -> c (k,x) xs) n m) #-}
{-# RULES "Map.toAscListBack" [1] foldrFB (\k x xs -> (k, x) : xs) [] = toAscList #-}
{-# RULES "Map.toDescList" [~1] forall m . toDescList m = build (\c n -> foldlFB (\xs k x -> c (k,x) xs) n m) #-}
{-# RULES "Map.toDescListBack" [1] foldlFB (\xs k x -> (k, x) : xs) [] = toDescList #-}
#endif
fromAscList :: Eq k => [(k,a)] -> Map k a
fromAscList :: [(k, a)] -> Map k a
fromAscList xs :: [(k, a)]
xs
= [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
fromDistinctAscList ([(k, a)] -> [(k, a)]
forall a b. Eq a => [(a, b)] -> [(a, b)]
combineEq [(k, a)]
xs)
where
combineEq :: [(a, b)] -> [(a, b)]
combineEq xs' :: [(a, b)]
xs'
= case [(a, b)]
xs' of
[] -> []
[x :: (a, b)
x] -> [(a, b)
x]
(x :: (a, b)
x:xx :: [(a, b)]
xx) -> (a, b) -> [(a, b)] -> [(a, b)]
forall a b. Eq a => (a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a, b)
x [(a, b)]
xx
combineEq' :: (a, b) -> [(a, b)] -> [(a, b)]
combineEq' z :: (a, b)
z [] = [(a, b)
z]
combineEq' z :: (a, b)
z@(kz :: a
kz,_) (x :: (a, b)
x@(kx :: a
kx,xx :: b
xx):xs' :: [(a, b)]
xs')
| a
kxa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
kz = (a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a
kx,b
xx) [(a, b)]
xs'
| Bool
otherwise = (a, b)
z(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:(a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a, b)
x [(a, b)]
xs'
#if __GLASGOW_HASKELL__
{-# INLINABLE fromAscList #-}
#endif
fromDescList :: Eq k => [(k,a)] -> Map k a
fromDescList :: [(k, a)] -> Map k a
fromDescList xs :: [(k, a)]
xs = [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
fromDistinctDescList ([(k, a)] -> [(k, a)]
forall a b. Eq a => [(a, b)] -> [(a, b)]
combineEq [(k, a)]
xs)
where
combineEq :: [(a, b)] -> [(a, b)]
combineEq xs' :: [(a, b)]
xs'
= case [(a, b)]
xs' of
[] -> []
[x :: (a, b)
x] -> [(a, b)
x]
(x :: (a, b)
x:xx :: [(a, b)]
xx) -> (a, b) -> [(a, b)] -> [(a, b)]
forall a b. Eq a => (a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a, b)
x [(a, b)]
xx
combineEq' :: (a, b) -> [(a, b)] -> [(a, b)]
combineEq' z :: (a, b)
z [] = [(a, b)
z]
combineEq' z :: (a, b)
z@(kz :: a
kz,_) (x :: (a, b)
x@(kx :: a
kx,xx :: b
xx):xs' :: [(a, b)]
xs')
| a
kxa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
kz = (a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a
kx,b
xx) [(a, b)]
xs'
| Bool
otherwise = (a, b)
z(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:(a, b) -> [(a, b)] -> [(a, b)]
combineEq' (a, b)
x [(a, b)]
xs'
#if __GLASGOW_HASKELL__
{-# INLINABLE fromDescList #-}
#endif
fromAscListWith :: Eq k => (a -> a -> a) -> [(k,a)] -> Map k a
fromAscListWith :: (a -> a -> a) -> [(k, a)] -> Map k a
fromAscListWith f :: a -> a -> a
f xs :: [(k, a)]
xs
= (k -> a -> a -> a) -> [(k, a)] -> Map k a
forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromAscListWithKey (\_ x :: a
x y :: a
y -> a -> a -> a
f a
x a
y) [(k, a)]
xs
#if __GLASGOW_HASKELL__
{-# INLINABLE fromAscListWith #-}
#endif
fromDescListWith :: Eq k => (a -> a -> a) -> [(k,a)] -> Map k a
fromDescListWith :: (a -> a -> a) -> [(k, a)] -> Map k a
fromDescListWith f :: a -> a -> a
f xs :: [(k, a)]
xs
= (k -> a -> a -> a) -> [(k, a)] -> Map k a
forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromDescListWithKey (\_ x :: a
x y :: a
y -> a -> a -> a
f a
x a
y) [(k, a)]
xs
#if __GLASGOW_HASKELL__
{-# INLINABLE fromDescListWith #-}
#endif
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromAscListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromAscListWithKey f :: k -> a -> a -> a
f xs :: [(k, a)]
xs
= [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
fromDistinctAscList ((k -> a -> a -> a) -> [(k, a)] -> [(k, a)]
combineEq k -> a -> a -> a
f [(k, a)]
xs)
where
combineEq :: (k -> a -> a -> a) -> [(k, a)] -> [(k, a)]
combineEq _ xs' :: [(k, a)]
xs'
= case [(k, a)]
xs' of
[] -> []
[x :: (k, a)
x] -> [(k, a)
x]
(x :: (k, a)
x:xx :: [(k, a)]
xx) -> (k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k, a)
x [(k, a)]
xx
combineEq' :: (k, a) -> [(k, a)] -> [(k, a)]
combineEq' z :: (k, a)
z [] = [(k, a)
z]
combineEq' z :: (k, a)
z@(kz :: k
kz,zz :: a
zz) (x :: (k, a)
x@(kx :: k
kx,xx :: a
xx):xs' :: [(k, a)]
xs')
| k
kxk -> k -> Bool
forall a. Eq a => a -> a -> Bool
==k
kz = let yy :: a
yy = k -> a -> a -> a
f k
kx a
xx a
zz in (k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k
kx,a
yy) [(k, a)]
xs'
| Bool
otherwise = (k, a)
z(k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
:(k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k, a)
x [(k, a)]
xs'
#if __GLASGOW_HASKELL__
{-# INLINABLE fromAscListWithKey #-}
#endif
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromDescListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> Map k a
fromDescListWithKey f :: k -> a -> a -> a
f xs :: [(k, a)]
xs
= [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
fromDistinctDescList ((k -> a -> a -> a) -> [(k, a)] -> [(k, a)]
combineEq k -> a -> a -> a
f [(k, a)]
xs)
where
combineEq :: (k -> a -> a -> a) -> [(k, a)] -> [(k, a)]
combineEq _ xs' :: [(k, a)]
xs'
= case [(k, a)]
xs' of
[] -> []
[x :: (k, a)
x] -> [(k, a)
x]
(x :: (k, a)
x:xx :: [(k, a)]
xx) -> (k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k, a)
x [(k, a)]
xx
combineEq' :: (k, a) -> [(k, a)] -> [(k, a)]
combineEq' z :: (k, a)
z [] = [(k, a)
z]
combineEq' z :: (k, a)
z@(kz :: k
kz,zz :: a
zz) (x :: (k, a)
x@(kx :: k
kx,xx :: a
xx):xs' :: [(k, a)]
xs')
| k
kxk -> k -> Bool
forall a. Eq a => a -> a -> Bool
==k
kz = let yy :: a
yy = k -> a -> a -> a
f k
kx a
xx a
zz in (k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k
kx,a
yy) [(k, a)]
xs'
| Bool
otherwise = (k, a)
z(k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
:(k, a) -> [(k, a)] -> [(k, a)]
combineEq' (k, a)
x [(k, a)]
xs'
#if __GLASGOW_HASKELL__
{-# INLINABLE fromDescListWithKey #-}
#endif
fromDistinctAscList :: [(k,a)] -> Map k a
fromDistinctAscList :: [(k, a)] -> Map k a
fromDistinctAscList [] = Map k a
forall k a. Map k a
Tip
fromDistinctAscList ((kx0 :: k
kx0, x0 :: a
x0) : xs0 :: [(k, a)]
xs0) = Int -> Map k a -> [(k, a)] -> Map k a
forall t k a.
(Num t, Bits t) =>
t -> Map k a -> [(k, a)] -> Map k a
go (1::Int) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx0 a
x0 Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) [(k, a)]
xs0
where
go :: t -> Map k a -> [(k, a)] -> Map k a
go !t
_ t :: Map k a
t [] = Map k a
t
go s :: t
s l :: Map k a
l ((kx :: k
kx, x :: a
x) : xs :: [(k, a)]
xs) = case t -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a k a.
(Num a, Bits a) =>
a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create t
s [(k, a)]
xs of
(r :: Map k a
r :*: ys :: [(k, a)]
ys) -> let !t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
r
in t -> Map k a -> [(k, a)] -> Map k a
go (t
s t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` 1) Map k a
t' [(k, a)]
ys
create :: a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create !a
_ [] = (Map k a
forall k a. Map k a
Tip Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [])
create s :: a
s xs :: [(k, a)]
xs@(x' :: (k, a)
x' : xs' :: [(k, a)]
xs')
| a
s a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = case (k, a)
x' of (kx :: k
kx, x :: a
x) -> (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [(k, a)]
xs')
| Bool
otherwise = case a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
xs of
res :: StrictPair (Map k a) [(k, a)]
res@(_ :*: []) -> StrictPair (Map k a) [(k, a)]
res
(l :: Map k a
l :*: (ky :: k
ky, y :: a
y):ys :: [(k, a)]
ys) -> case a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
ys of
(r :: Map k a
r :*: zs :: [(k, a)]
zs) -> (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
ky a
y Map k a
l Map k a
r Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [(k, a)]
zs)
fromDistinctDescList :: [(k,a)] -> Map k a
fromDistinctDescList :: [(k, a)] -> Map k a
fromDistinctDescList [] = Map k a
forall k a. Map k a
Tip
fromDistinctDescList ((kx0 :: k
kx0, x0 :: a
x0) : xs0 :: [(k, a)]
xs0) = Int -> Map k a -> [(k, a)] -> Map k a
forall t k a.
(Num t, Bits t) =>
t -> Map k a -> [(k, a)] -> Map k a
go (1 :: Int) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx0 a
x0 Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) [(k, a)]
xs0
where
go :: t -> Map k a -> [(k, a)] -> Map k a
go !t
_ t :: Map k a
t [] = Map k a
t
go s :: t
s r :: Map k a
r ((kx :: k
kx, x :: a
x) : xs :: [(k, a)]
xs) = case t -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a k a.
(Num a, Bits a) =>
a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create t
s [(k, a)]
xs of
(l :: Map k a
l :*: ys :: [(k, a)]
ys) -> let !t' :: Map k a
t' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
r
in t -> Map k a -> [(k, a)] -> Map k a
go (t
s t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` 1) Map k a
t' [(k, a)]
ys
create :: a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create !a
_ [] = (Map k a
forall k a. Map k a
Tip Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [])
create s :: a
s xs :: [(k, a)]
xs@(x' :: (k, a)
x' : xs' :: [(k, a)]
xs')
| a
s a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = case (k, a)
x' of (kx :: k
kx, x :: a
x) -> (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
kx a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [(k, a)]
xs')
| Bool
otherwise = case a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
xs of
res :: StrictPair (Map k a) [(k, a)]
res@(_ :*: []) -> StrictPair (Map k a) [(k, a)]
res
(r :: Map k a
r :*: (ky :: k
ky, y :: a
y):ys :: [(k, a)]
ys) -> case a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
create (a
s a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` 1) [(k, a)]
ys of
(l :: Map k a
l :*: zs :: [(k, a)]
zs) -> (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
ky a
y Map k a
l Map k a
r Map k a -> [(k, a)] -> StrictPair (Map k a) [(k, a)]
forall a b. a -> b -> StrictPair a b
:*: [(k, a)]
zs)
split :: Ord k => k -> Map k a -> (Map k a,Map k a)
split :: k -> Map k a -> (Map k a, Map k a)
split !k
k0 t0 :: Map k a
t0 = StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Map k a) (Map k a) -> (Map k a, Map k a))
-> StrictPair (Map k a) (Map k a) -> (Map k a, Map k a)
forall a b. (a -> b) -> a -> b
$ k -> Map k a -> StrictPair (Map k a) (Map k a)
forall k a. Ord k => k -> Map k a -> StrictPair (Map k a) (Map k a)
go k
k0 Map k a
t0
where
go :: k -> Map k a -> StrictPair (Map k a) (Map k a)
go k :: k
k t :: Map k a
t =
case Map k a
t of
Tip -> Map k a
forall k a. Map k a
Tip Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
forall k a. Map k a
Tip
Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r -> case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> let (lt :: Map k a
lt :*: gt :: Map k a
gt) = k -> Map k a -> StrictPair (Map k a) (Map k a)
go k
k Map k a
l in Map k a
lt Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
gt Map k a
r
GT -> let (lt :: Map k a
lt :*: gt :: Map k a
gt) = k -> Map k a -> StrictPair (Map k a) (Map k a)
go k
k Map k a
r in k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
lt Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
gt
EQ -> (Map k a
l Map k a -> Map k a -> StrictPair (Map k a) (Map k a)
forall a b. a -> b -> StrictPair a b
:*: Map k a
r)
#if __GLASGOW_HASKELL__
{-# INLINABLE split #-}
#endif
splitLookup :: Ord k => k -> Map k a -> (Map k a,Maybe a,Map k a)
splitLookup :: k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k0 :: k
k0 m :: Map k a
m = case k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
go k
k0 Map k a
m of
StrictTriple l :: Map k a
l mv :: Maybe a
mv r :: Map k a
r -> (Map k a
l, Maybe a
mv, Map k a
r)
where
go :: Ord k => k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
go :: k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
go !k
k t :: Map k a
t =
case Map k a
t of
Tip -> Map k a
-> Maybe a -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
forall k a. Map k a
Tip Maybe a
forall a. Maybe a
Nothing Map k a
forall k a. Map k a
Tip
Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r -> case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> let StrictTriple lt :: Map k a
lt z :: Maybe a
z gt :: Map k a
gt = k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
go k
k Map k a
l
!gt' :: Map k a
gt' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
gt Map k a
r
in Map k a
-> Maybe a -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
lt Maybe a
z Map k a
gt'
GT -> let StrictTriple lt :: Map k a
lt z :: Maybe a
z gt :: Map k a
gt = k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
go k
k Map k a
r
!lt' :: Map k a
lt' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
lt
in Map k a
-> Maybe a -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
lt' Maybe a
z Map k a
gt
EQ -> Map k a
-> Maybe a -> Map k a -> StrictTriple (Map k a) (Maybe a) (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
l (a -> Maybe a
forall a. a -> Maybe a
Just a
x) Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE splitLookup #-}
#endif
splitMember :: Ord k => k -> Map k a -> (Map k a,Bool,Map k a)
splitMember :: k -> Map k a -> (Map k a, Bool, Map k a)
splitMember k0 :: k
k0 m :: Map k a
m = case k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
go k
k0 Map k a
m of
StrictTriple l :: Map k a
l mv :: Bool
mv r :: Map k a
r -> (Map k a
l, Bool
mv, Map k a
r)
where
go :: Ord k => k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
go :: k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
go !k
k t :: Map k a
t =
case Map k a
t of
Tip -> Map k a -> Bool -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
forall k a. Map k a
Tip Bool
False Map k a
forall k a. Map k a
Tip
Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r -> case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k k
kx of
LT -> let StrictTriple lt :: Map k a
lt z :: Bool
z gt :: Map k a
gt = k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
go k
k Map k a
l
!gt' :: Map k a
gt' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
gt Map k a
r
in Map k a -> Bool -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
lt Bool
z Map k a
gt'
GT -> let StrictTriple lt :: Map k a
lt z :: Bool
z gt :: Map k a
gt = k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall k a.
Ord k =>
k -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
go k
k Map k a
r
!lt' :: Map k a
lt' = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
lt
in Map k a -> Bool -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
lt' Bool
z Map k a
gt
EQ -> Map k a -> Bool -> Map k a -> StrictTriple (Map k a) Bool (Map k a)
forall a b c. a -> b -> c -> StrictTriple a b c
StrictTriple Map k a
l Bool
True Map k a
r
#if __GLASGOW_HASKELL__
{-# INLINABLE splitMember #-}
#endif
data StrictTriple a b c = StrictTriple !a !b !c
link :: k -> a -> Map k a -> Map k a -> Map k a
link :: k -> a -> Map k a -> Map k a -> Map k a
link kx :: k
kx x :: a
x Tip r :: Map k a
r = k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMin k
kx a
x Map k a
r
link kx :: k
kx x :: a
x l :: Map k a
l Tip = k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMax k
kx a
x Map k a
l
link kx :: k
kx x :: a
x l :: Map k a
l@(Bin sizeL :: Int
sizeL ky :: k
ky y :: a
y ly :: Map k a
ly ry :: Map k a
ry) r :: Map k a
r@(Bin sizeR :: Int
sizeR kz :: k
kz z :: a
z lz :: Map k a
lz rz :: Map k a
rz)
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
sizeL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeR = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
kz a
z (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
l Map k a
lz) Map k a
rz
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
sizeR Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeL = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
ly (k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link k
kx a
x Map k a
ry Map k a
r)
| Bool
otherwise = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
bin k
kx a
x Map k a
l Map k a
r
insertMax,insertMin :: k -> a -> Map k a -> Map k a
insertMax :: k -> a -> Map k a -> Map k a
insertMax kx :: k
kx x :: a
x t :: Map k a
t
= case Map k a
t of
Tip -> k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
Bin _ ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r
-> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
ky a
y Map k a
l (k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMax k
kx a
x Map k a
r)
insertMin :: k -> a -> Map k a -> Map k a
insertMin kx :: k
kx x :: a
x t :: Map k a
t
= case Map k a
t of
Tip -> k -> a -> Map k a
forall k a. k -> a -> Map k a
singleton k
kx a
x
Bin _ ky :: k
ky y :: a
y l :: Map k a
l r :: Map k a
r
-> k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y (k -> a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a
insertMin k
kx a
x Map k a
l) Map k a
r
link2 :: Map k a -> Map k a -> Map k a
link2 :: Map k a -> Map k a -> Map k a
link2 Tip r :: Map k a
r = Map k a
r
link2 l :: Map k a
l Tip = Map k a
l
link2 l :: Map k a
l@(Bin sizeL :: Int
sizeL kx :: k
kx x :: a
x lx :: Map k a
lx rx :: Map k a
rx) r :: Map k a
r@(Bin sizeR :: Int
sizeR ky :: k
ky y :: a
y ly :: Map k a
ly ry :: Map k a
ry)
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
sizeL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeR = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
ky a
y (Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
l Map k a
ly) Map k a
ry
| Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
sizeR Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeL = k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
kx a
x Map k a
lx (Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
link2 Map k a
rx Map k a
r)
| Bool
otherwise = Map k a -> Map k a -> Map k a
forall k a. Map k a -> Map k a -> Map k a
glue Map k a
l Map k a
r
glue :: Map k a -> Map k a -> Map k a
glue :: Map k a -> Map k a -> Map k a
glue Tip r :: Map k a
r = Map k a
r
glue l :: Map k a
l Tip = Map k a
l
glue l :: Map k a
l@(Bin sl :: Int
sl kl :: k
kl xl :: a
xl ll :: Map k a
ll lr :: Map k a
lr) r :: Map k a
r@(Bin sr :: Int
sr kr :: k
kr xr :: a
xr rl :: Map k a
rl rr :: Map k a
rr)
| Int
sl Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
sr = let !(MaxView km :: k
km m :: a
m l' :: Map k a
l') = k -> a -> Map k a -> Map k a -> MaxView k a
forall k a. k -> a -> Map k a -> Map k a -> MaxView k a
maxViewSure k
kl a
xl Map k a
ll Map k a
lr in k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR k
km a
m Map k a
l' Map k a
r
| Bool
otherwise = let !(MinView km :: k
km m :: a
m r' :: Map k a
r') = k -> a -> Map k a -> Map k a -> MinView k a
forall k a. k -> a -> Map k a -> Map k a -> MinView k a
minViewSure k
kr a
xr Map k a
rl Map k a
rr in k -> a -> Map k a -> Map k a -> Map k a
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL k
km a
m Map k a
l Map k a
r'
data MinView k a = MinView !k a !(Map k a)
data MaxView k a = MaxView !k a !(Map k a)
minViewSure :: k -> a -> Map k a -> Map k a -> MinView k a
minViewSure :: k -> a -> Map k a -> Map k a -> MinView k a
minViewSure = k -> a -> Map k a -> Map k a -> MinView k a
forall k a. k -> a -> Map k a -> Map k a -> MinView k a
go
where
go :: t -> t -> Map t t -> Map t t -> MinView t t
go k :: t
k x :: t
x Tip r :: Map t t
r = t -> t -> Map t t -> MinView t t
forall k a. k -> a -> Map k a -> MinView k a
MinView t
k t
x Map t t
r
go k :: t
k x :: t
x (Bin _ kl :: t
kl xl :: t
xl ll :: Map t t
ll lr :: Map t t
lr) r :: Map t t
r =
case t -> t -> Map t t -> Map t t -> MinView t t
go t
kl t
xl Map t t
ll Map t t
lr of
MinView km :: t
km xm :: t
xm l' :: Map t t
l' -> t -> t -> Map t t -> MinView t t
forall k a. k -> a -> Map k a -> MinView k a
MinView t
km t
xm (t -> t -> Map t t -> Map t t -> Map t t
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceR t
k t
x Map t t
l' Map t t
r)
{-# NOINLINE minViewSure #-}
maxViewSure :: k -> a -> Map k a -> Map k a -> MaxView k a
maxViewSure :: k -> a -> Map k a -> Map k a -> MaxView k a
maxViewSure = k -> a -> Map k a -> Map k a -> MaxView k a
forall k a. k -> a -> Map k a -> Map k a -> MaxView k a
go
where
go :: t -> t -> Map t t -> Map t t -> MaxView t t
go k :: t
k x :: t
x l :: Map t t
l Tip = t -> t -> Map t t -> MaxView t t
forall k a. k -> a -> Map k a -> MaxView k a
MaxView t
k t
x Map t t
l
go k :: t
k x :: t
x l :: Map t t
l (Bin _ kr :: t
kr xr :: t
xr rl :: Map t t
rl rr :: Map t t
rr) =
case t -> t -> Map t t -> Map t t -> MaxView t t
go t
kr t
xr Map t t
rl Map t t
rr of
MaxView km :: t
km xm :: t
xm r' :: Map t t
r' -> t -> t -> Map t t -> MaxView t t
forall k a. k -> a -> Map k a -> MaxView k a
MaxView t
km t
xm (t -> t -> Map t t -> Map t t -> Map t t
forall k a. k -> a -> Map k a -> Map k a -> Map k a
balanceL t
k t
x Map t t
l Map t t
r')
{-# NOINLINE maxViewSure #-}
deleteFindMin :: Map k a -> ((k,a),Map k a)
deleteFindMin :: Map k a -> ((k, a), Map k a)
deleteFindMin t :: Map k a
t = case Map k a -> Maybe ((k, a), Map k a)
forall k a. Map k a -> Maybe ((k, a), Map k a)
minViewWithKey Map k a
t of
Nothing -> ([Char] -> (k, a)
forall a. HasCallStack => [Char] -> a
error "Map.deleteFindMin: can not return the minimal element of an empty map", Map k a
forall k a. Map k a
Tip)
Just res :: ((k, a), Map k a)
res -> ((k, a), Map k a)
res
deleteFindMax :: Map k a -> ((k,a),Map k a)
deleteFindMax :: Map k a -> ((k, a), Map k a)
deleteFindMax t :: Map k a
t = case Map k a -> Maybe ((k, a), Map k a)
forall k a. Map k a -> Maybe ((k, a), Map k a)
maxViewWithKey Map k a
t of
Nothing -> ([Char] -> (k, a)
forall a. HasCallStack => [Char] -> a
error "Map.deleteFindMax: can not return the maximal element of an empty map", Map k a
forall k a. Map k a
Tip)
Just res :: ((k, a), Map k a)
res -> ((k, a), Map k a)
res
delta,ratio :: Int
delta :: Int
delta = 3
ratio :: Int
ratio = 2
balance :: k -> a -> Map k a -> Map k a -> Map k a
balance :: k -> a -> Map k a -> Map k a -> Map k a
balance k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r = case Map k a
l of
Tip -> case Map k a
r of
Tip -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip
(Bin _ _ _ Tip Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 2 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
r
(Bin _ rk :: k
rk rx :: a
rx Tip rr :: Map k a
rr@(Bin _ _ _ _ _)) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) Map k a
rr
(Bin _ rk :: k
rk rx :: a
rx (Bin _ rlk :: k
rlk rlx :: a
rlx _ _) Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
rk a
rx Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
(Bin rs :: Int
rs rk :: k
rk rx :: a
rx rl :: Map k a
rl@(Bin rls :: Int
rls rlk :: k
rlk rlx :: a
rlx rll :: Map k a
rll rlr :: Map k a
rlr) rr :: Map k a
rr@(Bin rrs :: Int
rrs _ _ _ _))
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) k
k a
x Map k a
forall k a. Map k a
Tip Map k a
rl) Map k a
rr
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rll) k
k a
x Map k a
forall k a. Map k a
Tip Map k a
rll) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rlr) k
rk a
rx Map k a
rlr Map k a
rr)
(Bin ls :: Int
ls lk :: k
lk lx :: a
lx ll :: Map k a
ll lr :: Map k a
lr) -> case Map k a
r of
Tip -> case (Map k a
ll, Map k a
lr) of
(Tip, Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 2 k
k a
x Map k a
l Map k a
forall k a. Map k a
Tip
(Tip, (Bin _ lrk :: k
lrk lrx :: a
lrx _ _)) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
lk a
lx Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
((Bin _ _ _ _ _), Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
((Bin lls :: Int
lls _ _ _ _), (Bin lrs :: Int
lrs lrk :: k
lrk lrx :: a
lrx lrl :: Map k a
lrl lrr :: Map k a
lrr))
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) k
k a
x Map k a
lr Map k a
forall k a. Map k a
Tip)
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrl) k
lk a
lx Map k a
ll Map k a
lrl) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrr) k
k a
x Map k a
lrr Map k a
forall k a. Map k a
Tip)
(Bin rs :: Int
rs rk :: k
rk rx :: a
rx rl :: Map k a
rl rr :: Map k a
rr)
| Int
rs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
ls -> case (Map k a
rl, Map k a
rr) of
(Bin rls :: Int
rls rlk :: k
rlk rlx :: a
rlx rll :: Map k a
rll rlr :: Map k a
rlr, Bin rrs :: Int
rrs _ _ _ _)
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) k
k a
x Map k a
l Map k a
rl) Map k a
rr
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rll) k
k a
x Map k a
l Map k a
rll) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rlr) k
rk a
rx Map k a
rlr Map k a
rr)
(_, _) -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Failure in Data.Map.balance"
| Int
ls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rs -> case (Map k a
ll, Map k a
lr) of
(Bin lls :: Int
lls _ _ _ _, Bin lrs :: Int
lrs lrk :: k
lrk lrx :: a
lrx lrl :: Map k a
lrl lrr :: Map k a
lrr)
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) k
k a
x Map k a
lr Map k a
r)
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrl) k
lk a
lx Map k a
ll Map k a
lrl) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrr) k
k a
x Map k a
lrr Map k a
r)
(_, _) -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Failure in Data.Map.balance"
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
k a
x Map k a
l Map k a
r
{-# NOINLINE balance #-}
balanceL :: k -> a -> Map k a -> Map k a -> Map k a
balanceL :: k -> a -> Map k a -> Map k a -> Map k a
balanceL k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r = case Map k a
r of
Tip -> case Map k a
l of
Tip -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip
(Bin _ _ _ Tip Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 2 k
k a
x Map k a
l Map k a
forall k a. Map k a
Tip
(Bin _ lk :: k
lk lx :: a
lx Tip (Bin _ lrk :: k
lrk lrx :: a
lrx _ _)) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
lk a
lx Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
(Bin _ lk :: k
lk lx :: a
lx ll :: Map k a
ll@(Bin _ _ _ _ _) Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
(Bin ls :: Int
ls lk :: k
lk lx :: a
lx ll :: Map k a
ll@(Bin lls :: Int
lls _ _ _ _) lr :: Map k a
lr@(Bin lrs :: Int
lrs lrk :: k
lrk lrx :: a
lrx lrl :: Map k a
lrl lrr :: Map k a
lrr))
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) k
k a
x Map k a
lr Map k a
forall k a. Map k a
Tip)
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrl) k
lk a
lx Map k a
ll Map k a
lrl) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrr) k
k a
x Map k a
lrr Map k a
forall k a. Map k a
Tip)
(Bin rs :: Int
rs _ _ _ _) -> case Map k a
l of
Tip -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
k a
x Map k a
forall k a. Map k a
Tip Map k a
r
(Bin ls :: Int
ls lk :: k
lk lx :: a
lx ll :: Map k a
ll lr :: Map k a
lr)
| Int
ls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rs -> case (Map k a
ll, Map k a
lr) of
(Bin lls :: Int
lls _ _ _ _, Bin lrs :: Int
lrs lrk :: k
lrk lrx :: a
lrx lrl :: Map k a
lrl lrr :: Map k a
lrr)
| Int
lrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
lls -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
lk a
lx Map k a
ll (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lrs) k
k a
x Map k a
lr Map k a
r)
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
lrk a
lrx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
llsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrl) k
lk a
lx Map k a
ll Map k a
lrl) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
lrr) k
k a
x Map k a
lrr Map k a
r)
(_, _) -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Failure in Data.Map.balanceL"
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
k a
x Map k a
l Map k a
r
{-# NOINLINE balanceL #-}
balanceR :: k -> a -> Map k a -> Map k a -> Map k a
balanceR :: k -> a -> Map k a -> Map k a -> Map k a
balanceR k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r = case Map k a
l of
Tip -> case Map k a
r of
Tip -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip
(Bin _ _ _ Tip Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 2 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
r
(Bin _ rk :: k
rk rx :: a
rx Tip rr :: Map k a
rr@(Bin _ _ _ _ _)) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) Map k a
rr
(Bin _ rk :: k
rk rx :: a
rx (Bin _ rlk :: k
rlk rlx :: a
rlx _ _) Tip) -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 3 k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
k a
x Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin 1 k
rk a
rx Map k a
forall k a. Map k a
Tip Map k a
forall k a. Map k a
Tip)
(Bin rs :: Int
rs rk :: k
rk rx :: a
rx rl :: Map k a
rl@(Bin rls :: Int
rls rlk :: k
rlk rlx :: a
rlx rll :: Map k a
rll rlr :: Map k a
rlr) rr :: Map k a
rr@(Bin rrs :: Int
rrs _ _ _ _))
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) k
k a
x Map k a
forall k a. Map k a
Tip Map k a
rl) Map k a
rr
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rll) k
k a
x Map k a
forall k a. Map k a
Tip Map k a
rll) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rlr) k
rk a
rx Map k a
rlr Map k a
rr)
(Bin ls :: Int
ls _ _ _ _) -> case Map k a
r of
Tip -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ls) k
k a
x Map k a
l Map k a
forall k a. Map k a
Tip
(Bin rs :: Int
rs rk :: k
rk rx :: a
rx rl :: Map k a
rl rr :: Map k a
rr)
| Int
rs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
deltaInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
ls -> case (Map k a
rl, Map k a
rr) of
(Bin rls :: Int
rls rlk :: k
rlk rlx :: a
rlx rll :: Map k a
rll rlr :: Map k a
rlr, Bin rrs :: Int
rrs _ _ _ _)
| Int
rls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ratioInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rrs -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rk a
rx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rls) k
k a
x Map k a
l Map k a
rl) Map k a
rr
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
rlk a
rlx (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rll) k
k a
x Map k a
l Map k a
rll) (Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rrsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Map k a -> Int
forall k a. Map k a -> Int
size Map k a
rlr) k
rk a
rx Map k a
rlr Map k a
rr)
(_, _) -> [Char] -> Map k a
forall a. HasCallStack => [Char] -> a
error "Failure in Data.Map.balanceR"
| Bool
otherwise -> Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
rs) k
k a
x Map k a
l Map k a
r
{-# NOINLINE balanceR #-}
bin :: k -> a -> Map k a -> Map k a -> Map k a
bin :: k -> a -> Map k a -> Map k a -> Map k a
bin k :: k
k x :: a
x l :: Map k a
l r :: Map k a
r
= Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin (Map k a -> Int
forall k a. Map k a -> Int
size Map k a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map k a -> Int
forall k a. Map k a -> Int
size Map k a
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) k
k a
x Map k a
l Map k a
r
{-# INLINE bin #-}
instance (Eq k,Eq a) => Eq (Map k a) where
t1 :: Map k a
t1 == :: Map k a -> Map k a -> Bool
== t2 :: Map k a
t2 = (Map k a -> Int
forall k a. Map k a -> Int
size Map k a
t1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Map k a -> Int
forall k a. Map k a -> Int
size Map k a
t2) Bool -> Bool -> Bool
&& (Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toAscList Map k a
t1 [(k, a)] -> [(k, a)] -> Bool
forall a. Eq a => a -> a -> Bool
== Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toAscList Map k a
t2)
instance (Ord k, Ord v) => Ord (Map k v) where
compare :: Map k v -> Map k v -> Ordering
compare m1 :: Map k v
m1 m2 :: Map k v
m2 = [(k, v)] -> [(k, v)] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Map k v -> [(k, v)]
forall k a. Map k a -> [(k, a)]
toAscList Map k v
m1) (Map k v -> [(k, v)]
forall k a. Map k a -> [(k, a)]
toAscList Map k v
m2)
#if MIN_VERSION_base(4,9,0)
instance Eq2 Map where
liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> Map a c -> Map b d -> Bool
liftEq2 eqk :: a -> b -> Bool
eqk eqv :: c -> d -> Bool
eqv m :: Map a c
m n :: Map b d
n =
Map a c -> Int
forall k a. Map k a -> Int
size Map a c
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Map b d -> Int
forall k a. Map k a -> Int
size Map b d
n Bool -> Bool -> Bool
&& ((a, c) -> (b, d) -> Bool) -> [(a, c)] -> [(b, d)] -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((a -> b -> Bool) -> (c -> d -> Bool) -> (a, c) -> (b, d) -> Bool
forall (f :: * -> * -> *) a b c d.
Eq2 f =>
(a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
liftEq2 a -> b -> Bool
eqk c -> d -> Bool
eqv) (Map a c -> [(a, c)]
forall k a. Map k a -> [(k, a)]
toList Map a c
m) (Map b d -> [(b, d)]
forall k a. Map k a -> [(k, a)]
toList Map b d
n)
instance Eq k => Eq1 (Map k) where
liftEq :: (a -> b -> Bool) -> Map k a -> Map k b -> Bool
liftEq = (k -> k -> Bool) -> (a -> b -> Bool) -> Map k a -> Map k b -> Bool
forall (f :: * -> * -> *) a b c d.
Eq2 f =>
(a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
liftEq2 k -> k -> Bool
forall a. Eq a => a -> a -> Bool
(==)
instance Ord2 Map where
liftCompare2 :: (a -> b -> Ordering)
-> (c -> d -> Ordering) -> Map a c -> Map b d -> Ordering
liftCompare2 cmpk :: a -> b -> Ordering
cmpk cmpv :: c -> d -> Ordering
cmpv m :: Map a c
m n :: Map b d
n =
((a, c) -> (b, d) -> Ordering) -> [(a, c)] -> [(b, d)] -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering)
-> (c -> d -> Ordering) -> (a, c) -> (b, d) -> Ordering
forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 a -> b -> Ordering
cmpk c -> d -> Ordering
cmpv) (Map a c -> [(a, c)]
forall k a. Map k a -> [(k, a)]
toList Map a c
m) (Map b d -> [(b, d)]
forall k a. Map k a -> [(k, a)]
toList Map b d
n)
instance Ord k => Ord1 (Map k) where
liftCompare :: (a -> b -> Ordering) -> Map k a -> Map k b -> Ordering
liftCompare = (k -> k -> Ordering)
-> (a -> b -> Ordering) -> Map k a -> Map k b -> Ordering
forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
instance Show2 Map where
liftShowsPrec2 :: (Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> Map a b
-> ShowS
liftShowsPrec2 spk :: Int -> a -> ShowS
spk slk :: [a] -> ShowS
slk spv :: Int -> b -> ShowS
spv slv :: [b] -> ShowS
slv d :: Int
d m :: Map a b
m =
(Int -> [(a, b)] -> ShowS) -> [Char] -> Int -> [(a, b)] -> ShowS
forall a. (Int -> a -> ShowS) -> [Char] -> Int -> a -> ShowS
showsUnaryWith ((Int -> (a, b) -> ShowS)
-> ([(a, b)] -> ShowS) -> Int -> [(a, b)] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> (a, b) -> ShowS
sp [(a, b)] -> ShowS
sl) "fromList" Int
d (Map a b -> [(a, b)]
forall k a. Map k a -> [(k, a)]
toList Map a b
m)
where
sp :: Int -> (a, b) -> ShowS
sp = (Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> (a, b)
-> ShowS
forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> f a b
-> ShowS
liftShowsPrec2 Int -> a -> ShowS
spk [a] -> ShowS
slk Int -> b -> ShowS
spv [b] -> ShowS
slv
sl :: [(a, b)] -> ShowS
sl = (Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> [(a, b)]
-> ShowS
forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> [f a b]
-> ShowS
liftShowList2 Int -> a -> ShowS
spk [a] -> ShowS
slk Int -> b -> ShowS
spv [b] -> ShowS
slv
instance Show k => Show1 (Map k) where
liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Map k a -> ShowS
liftShowsPrec = (Int -> k -> ShowS)
-> ([k] -> ShowS)
-> (Int -> a -> ShowS)
-> ([a] -> ShowS)
-> Int
-> Map k a
-> ShowS
forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> f a b
-> ShowS
liftShowsPrec2 Int -> k -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec [k] -> ShowS
forall a. Show a => [a] -> ShowS
showList
instance (Ord k, Read k) => Read1 (Map k) where
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Map k a)
liftReadsPrec rp :: Int -> ReadS a
rp rl :: ReadS [a]
rl = ([Char] -> ReadS (Map k a)) -> Int -> ReadS (Map k a)
forall a. ([Char] -> ReadS a) -> Int -> ReadS a
readsData (([Char] -> ReadS (Map k a)) -> Int -> ReadS (Map k a))
-> ([Char] -> ReadS (Map k a)) -> Int -> ReadS (Map k a)
forall a b. (a -> b) -> a -> b
$
(Int -> ReadS [(k, a)])
-> [Char] -> ([(k, a)] -> Map k a) -> [Char] -> ReadS (Map k a)
forall a t.
(Int -> ReadS a) -> [Char] -> (a -> t) -> [Char] -> ReadS t
readsUnaryWith ((Int -> ReadS (k, a)) -> ReadS [(k, a)] -> Int -> ReadS [(k, a)]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS (k, a)
rp' ReadS [(k, a)]
rl') "fromList" [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
fromList
where
rp' :: Int -> ReadS (k, a)
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (k, a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl
rl' :: ReadS [(k, a)]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [(k, a)]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp ReadS [a]
rl
#endif
instance Functor (Map k) where
fmap :: (a -> b) -> Map k a -> Map k b
fmap f :: a -> b
f m :: Map k a
m = (a -> b) -> Map k a -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
map a -> b
f Map k a
m
#ifdef __GLASGOW_HASKELL__
_ <$ :: a -> Map k b -> Map k a
<$ Tip = Map k a
forall k a. Map k a
Tip
a :: a
a <$ (Bin sx :: Int
sx kx :: k
kx _ l :: Map k b
l r :: Map k b
r) = Int -> k -> a -> Map k a -> Map k a -> Map k a
forall k a. Int -> k -> a -> Map k a -> Map k a -> Map k a
Bin Int
sx k
kx a
a (a
a a -> Map k b -> Map k a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Map k b
l) (a
a a -> Map k b -> Map k a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Map k b
r)
#endif
instance Traversable (Map k) where
traverse :: (a -> f b) -> Map k a -> f (Map k b)
traverse f :: a -> f b
f = (k -> a -> f b) -> Map k a -> f (Map k b)
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
traverseWithKey (\_ -> a -> f b
f)
{-# INLINE traverse #-}
instance Foldable.Foldable (Map k) where
fold :: Map k m -> m
fold = Map k m -> m
forall a k. Monoid a => Map k a -> a
go
where go :: Map k a -> a
go Tip = a
forall a. Monoid a => a
mempty
go (Bin 1 _ v :: a
v _ _) = a
v
go (Bin _ _ v :: a
v l :: Map k a
l r :: Map k a
r) = Map k a -> a
go Map k a
l a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` (a
v a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` Map k a -> a
go Map k a
r)
{-# INLINABLE fold #-}
foldr :: (a -> b -> b) -> b -> Map k a -> b
foldr = (a -> b -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> b) -> b -> Map k a -> b
foldr
{-# INLINE foldr #-}
foldl :: (b -> a -> b) -> b -> Map k a -> b
foldl = (b -> a -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> a) -> a -> Map k b -> a
foldl
{-# INLINE foldl #-}
foldMap :: (a -> m) -> Map k a -> m
foldMap f :: a -> m
f t :: Map k a
t = Map k a -> m
go Map k a
t
where go :: Map k a -> m
go Tip = m
forall a. Monoid a => a
mempty
go (Bin 1 _ v :: a
v _ _) = a -> m
f a
v
go (Bin _ _ v :: a
v l :: Map k a
l r :: Map k a
r) = Map k a -> m
go Map k a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` (a -> m
f a
v m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Map k a -> m
go Map k a
r)
{-# INLINE foldMap #-}
foldl' :: (b -> a -> b) -> b -> Map k a -> b
foldl' = (b -> a -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> a) -> a -> Map k b -> a
foldl'
{-# INLINE foldl' #-}
foldr' :: (a -> b -> b) -> b -> Map k a -> b
foldr' = (a -> b -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> b) -> b -> Map k a -> b
foldr'
{-# INLINE foldr' #-}
#if MIN_VERSION_base(4,8,0)
length :: Map k a -> Int
length = Map k a -> Int
forall k a. Map k a -> Int
size
{-# INLINE length #-}
null :: Map k a -> Bool
null = Map k a -> Bool
forall k a. Map k a -> Bool
null
{-# INLINE null #-}
toList :: Map k a -> [a]
toList = Map k a -> [a]
forall k a. Map k a -> [a]
elems
{-# INLINE toList #-}
elem :: a -> Map k a -> Bool
elem = a -> Map k a -> Bool
forall t k. Eq t => t -> Map k t -> Bool
go
where go :: t -> Map k t -> Bool
go !t
_ Tip = Bool
False
go x :: t
x (Bin _ _ v :: t
v l :: Map k t
l r :: Map k t
r) = t
x t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
v Bool -> Bool -> Bool
|| t -> Map k t -> Bool
go t
x Map k t
l Bool -> Bool -> Bool
|| t -> Map k t -> Bool
go t
x Map k t
r
{-# INLINABLE elem #-}
maximum :: Map k a -> a
maximum = Map k a -> a
forall p k. Ord p => Map k p -> p
start
where start :: Map k p -> p
start Tip = [Char] -> p
forall a. HasCallStack => [Char] -> a
error "Data.Foldable.maximum (for Data.Map): empty map"
start (Bin _ _ v :: p
v l :: Map k p
l r :: Map k p
r) = p -> Map k p -> p
forall t k. Ord t => t -> Map k t -> t
go (p -> Map k p -> p
forall t k. Ord t => t -> Map k t -> t
go p
v Map k p
l) Map k p
r
go :: t -> Map k t -> t
go !t
m Tip = t
m
go m :: t
m (Bin _ _ v :: t
v l :: Map k t
l r :: Map k t
r) = t -> Map k t -> t
go (t -> Map k t -> t
go (t -> t -> t
forall a. Ord a => a -> a -> a
max t
m t
v) Map k t
l) Map k t
r
{-# INLINABLE maximum #-}
minimum :: Map k a -> a
minimum = Map k a -> a
forall p k. Ord p => Map k p -> p
start
where start :: Map k p -> p
start Tip = [Char] -> p
forall a. HasCallStack => [Char] -> a
error "Data.Foldable.minimum (for Data.Map): empty map"
start (Bin _ _ v :: p
v l :: Map k p
l r :: Map k p
r) = p -> Map k p -> p
forall t k. Ord t => t -> Map k t -> t
go (p -> Map k p -> p
forall t k. Ord t => t -> Map k t -> t
go p
v Map k p
l) Map k p
r
go :: t -> Map k t -> t
go !t
m Tip = t
m
go m :: t
m (Bin _ _ v :: t
v l :: Map k t
l r :: Map k t
r) = t -> Map k t -> t
go (t -> Map k t -> t
go (t -> t -> t
forall a. Ord a => a -> a -> a
min t
m t
v) Map k t
l) Map k t
r
{-# INLINABLE minimum #-}
sum :: Map k a -> a
sum = (a -> a -> a) -> a -> Map k a -> a
forall a b k. (a -> b -> a) -> a -> Map k b -> a
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) 0
{-# INLINABLE sum #-}
product :: Map k a -> a
product = (a -> a -> a) -> a -> Map k a -> a
forall a b k. (a -> b -> a) -> a -> Map k b -> a
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) 1
{-# INLINABLE product #-}
#endif
instance (NFData k, NFData a) => NFData (Map k a) where
rnf :: Map k a -> ()
rnf Tip = ()
rnf (Bin _ kx :: k
kx x :: a
x l :: Map k a
l r :: Map k a
r) = k -> ()
forall a. NFData a => a -> ()
rnf k
kx () -> () -> ()
forall a b. a -> b -> b
`seq` a -> ()
forall a. NFData a => a -> ()
rnf a
x () -> () -> ()
forall a b. a -> b -> b
`seq` Map k a -> ()
forall a. NFData a => a -> ()
rnf Map k a
l () -> () -> ()
forall a b. a -> b -> b
`seq` Map k a -> ()
forall a. NFData a => a -> ()
rnf Map k a
r
instance (Ord k, Read k, Read e) => Read (Map k e) where
#ifdef __GLASGOW_HASKELL__
readPrec :: ReadPrec (Map k e)
readPrec = ReadPrec (Map k e) -> ReadPrec (Map k e)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (Map k e) -> ReadPrec (Map k e))
-> ReadPrec (Map k e) -> ReadPrec (Map k e)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (Map k e) -> ReadPrec (Map k e)
forall a. Int -> ReadPrec a -> ReadPrec a
prec 10 (ReadPrec (Map k e) -> ReadPrec (Map k e))
-> ReadPrec (Map k e) -> ReadPrec (Map k e)
forall a b. (a -> b) -> a -> b
$ do
Ident "fromList" <- ReadPrec Lexeme
lexP
[(k, e)]
xs <- ReadPrec [(k, e)]
forall a. Read a => ReadPrec a
readPrec
Map k e -> ReadPrec (Map k e)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(k, e)] -> Map k e
forall k a. Ord k => [(k, a)] -> Map k a
fromList [(k, e)]
xs)
readListPrec :: ReadPrec [Map k e]
readListPrec = ReadPrec [Map k e]
forall a. Read a => ReadPrec [a]
readListPrecDefault
#else
readsPrec p = readParen (p > 10) $ \ r -> do
("fromList",s) <- lex r
(xs,t) <- reads s
return (fromList xs,t)
#endif
instance (Show k, Show a) => Show (Map k a) where
showsPrec :: Int -> Map k a -> ShowS
showsPrec d :: Int
d m :: Map k a
m = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
[Char] -> ShowS
showString "fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, a)] -> ShowS
forall a. Show a => a -> ShowS
shows (Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toList Map k a
m)
INSTANCE_TYPEABLE2(Map)
splitRoot :: Map k b -> [Map k b]
splitRoot :: Map k b -> [Map k b]
splitRoot orig :: Map k b
orig =
case Map k b
orig of
Tip -> []
Bin _ k :: k
k v :: b
v l :: Map k b
l r :: Map k b
r -> [Map k b
l, k -> b -> Map k b
forall k a. k -> a -> Map k a
singleton k
k b
v, Map k b
r]
{-# INLINE splitRoot #-}