{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TypeFamilies #-}

{-# OPTIONS_HADDOCK not-home #-}
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}


-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Word64Map.Internal
-- Copyright   :  (c) Daan Leijen 2002
--                (c) Andriy Palamarchuk 2008
--                (c) wren romano 2016
-- License     :  BSD-style
-- Maintainer  :  libraries@haskell.org
-- Portability :  portable
--
-- = WARNING
--
-- This module is considered __internal__.
--
-- The Package Versioning Policy __does not apply__.
--
-- The contents of this module may change __in any way whatsoever__
-- and __without any warning__ between minor versions of this package.
--
-- Authors importing this module are expected to track development
-- closely.
--
-- = Description
--
-- This defines the data structures and core (hidden) manipulations
-- on representations.
--
-- @since 0.5.9
-----------------------------------------------------------------------------

-- [Note: INLINE bit fiddling]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- It is essential that the bit fiddling functions like mask, zero, branchMask
-- etc are inlined. If they do not, the memory allocation skyrockets. The GHC
-- usually gets it right, but it is disastrous if it does not. Therefore we
-- explicitly mark these functions INLINE.


-- [Note: Local 'go' functions and capturing]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Care must be taken when using 'go' function which captures an argument.
-- Sometimes (for example when the argument is passed to a data constructor,
-- as in insert), GHC heap-allocates more than necessary. Therefore C-- code
-- must be checked for increased allocation when creating and modifying such
-- functions.


-- [Note: Order of constructors]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- The order of constructors of Word64Map matters when considering performance.
-- Currently in GHC 7.0, when type has 3 constructors, they are matched from
-- the first to the last -- the best performance is achieved when the
-- constructors are ordered by frequency.
-- On GHC 7.0, reordering constructors from Nil | Tip | Bin to Bin | Tip | Nil
-- improves the benchmark by circa 10%.
--

module GHC.Data.Word64Map.Internal (
    -- * Map type
      Word64Map(..), Key          -- instance Eq,Show

    -- * Operators
    , (!), (!?), (\\)

    -- * Query
    , null
    , size
    , member
    , notMember
    , lookup
    , findWithDefault
    , lookupLT
    , lookupGT
    , lookupLE
    , lookupGE
    , disjoint

    -- * Construction
    , empty
    , singleton

    -- ** Insertion
    , insert
    , insertWith
    , insertWithKey
    , insertLookupWithKey

    -- ** Delete\/Update
    , delete
    , adjust
    , adjustWithKey
    , update
    , updateWithKey
    , updateLookupWithKey
    , alter
    , alterF

    -- * Combine

    -- ** Union
    , union
    , unionWith
    , unionWithKey
    , unions
    , unionsWith

    -- ** Difference
    , difference
    , differenceWith
    , differenceWithKey

    -- ** Intersection
    , intersection
    , intersectionWith
    , intersectionWithKey

    -- ** Compose
    , compose

    -- ** General combining function
    , SimpleWhenMissing
    , SimpleWhenMatched
    , runWhenMatched
    , runWhenMissing
    , merge
    -- *** @WhenMatched@ tactics
    , zipWithMaybeMatched
    , zipWithMatched
    -- *** @WhenMissing@ tactics
    , mapMaybeMissing
    , dropMissing
    , preserveMissing
    , mapMissing
    , filterMissing

    -- ** Applicative general combining function
    , WhenMissing (..)
    , WhenMatched (..)
    , mergeA
    -- *** @WhenMatched@ tactics
    -- | The tactics described for 'merge' work for
    -- 'mergeA' as well. Furthermore, the following
    -- are available.
    , zipWithMaybeAMatched
    , zipWithAMatched
    -- *** @WhenMissing@ tactics
    -- | The tactics described for 'merge' work for
    -- 'mergeA' as well. Furthermore, the following
    -- are available.
    , traverseMaybeMissing
    , traverseMissing
    , filterAMissing

    -- ** Deprecated general combining function
    , mergeWithKey
    , mergeWithKey'

    -- * Traversal
    -- ** Map
    , map
    , mapWithKey
    , traverseWithKey
    , traverseMaybeWithKey
    , mapAccum
    , mapAccumWithKey
    , mapAccumRWithKey
    , mapKeys
    , mapKeysWith
    , mapKeysMonotonic

    -- * Folds
    , foldr
    , foldl
    , foldrWithKey
    , foldlWithKey
    , foldMapWithKey

    -- ** Strict folds
    , foldr'
    , foldl'
    , foldrWithKey'
    , foldlWithKey'

    -- * Conversion
    , elems
    , keys
    , assocs
    , keysSet
    , fromSet

    -- ** Lists
    , toList
    , fromList
    , fromListWith
    , fromListWithKey

    -- ** Ordered lists
    , toAscList
    , toDescList
    , fromAscList
    , fromAscListWith
    , fromAscListWithKey
    , fromDistinctAscList

    -- * Filter
    , filter
    , filterWithKey
    , restrictKeys
    , withoutKeys
    , partition
    , partitionWithKey

    , takeWhileAntitone
    , dropWhileAntitone
    , spanAntitone

    , mapMaybe
    , mapMaybeWithKey
    , mapEither
    , mapEitherWithKey

    , split
    , splitLookup
    , splitRoot

    -- * Submap
    , isSubmapOf, isSubmapOfBy
    , isProperSubmapOf, isProperSubmapOfBy

    -- * Min\/Max
    , lookupMin
    , lookupMax
    , findMin
    , findMax
    , deleteMin
    , deleteMax
    , deleteFindMin
    , deleteFindMax
    , updateMin
    , updateMax
    , updateMinWithKey
    , updateMaxWithKey
    , minView
    , maxView
    , minViewWithKey
    , maxViewWithKey

    -- * Debugging
    , showTree
    , showTreeWith

    -- * Internal types
    , Mask, Prefix, Nat

    -- * Utility
    , natFromInt
    , intFromNat
    , link
    , linkWithMask
    , bin
    , binCheckLeft
    , binCheckRight
    , zero
    , nomatch
    , match
    , mask
    , maskW
    , shorter
    , branchMask
    , highestBitMask

    -- * Used by "Word64Map.Merge.Lazy" and "Word64Map.Merge.Strict"
    , mapWhenMissing
    , mapWhenMatched
    , lmapWhenMissing
    , contramapFirstWhenMatched
    , contramapSecondWhenMatched
    , mapGentlyWhenMissing
    , mapGentlyWhenMatched
    ) where

import GHC.Prelude.Basic hiding
  (lookup, filter, foldr, foldl, foldl', null, map)

import Data.Functor.Identity (Identity (..))
import Data.Semigroup (Semigroup(stimes,(<>)),stimesIdempotentMonoid)
import Data.Functor.Classes

import Control.DeepSeq (NFData(rnf))
import qualified Data.Foldable as Foldable
import Data.Maybe (fromMaybe)

import GHC.Data.Word64Set.Internal (Key)
import qualified GHC.Data.Word64Set.Internal as Word64Set
import GHC.Utils.Containers.Internal.BitUtil
import GHC.Utils.Containers.Internal.StrictPair

#ifdef __GLASGOW_HASKELL__
import Data.Coerce
import Data.Data (Data(..), Constr, mkConstr, constrIndex, Fixity(Prefix),
                  DataType, mkDataType, gcast1)
import GHC.Exts (build)
import qualified GHC.Exts as GHCExts
import Text.Read
#endif
import qualified Control.Category as Category
import Data.Word


-- A "Nat" is a 64 bit machine word (an unsigned Int64)
type Nat = Word64

natFromInt :: Key -> Nat
natFromInt :: Word64 -> Word64
natFromInt = Word64 -> Word64
forall a. a -> a
id
{-# INLINE natFromInt #-}

intFromNat :: Nat -> Key
intFromNat :: Word64 -> Word64
intFromNat = Word64 -> Word64
forall a. a -> a
id
{-# INLINE intFromNat #-}

{--------------------------------------------------------------------
  Types
--------------------------------------------------------------------}


-- | A map of integers to values @a@.

-- See Note: Order of constructors
data Word64Map a = Bin {-# UNPACK #-} !Prefix
                    {-# UNPACK #-} !Mask
                    !(Word64Map a)
                    !(Word64Map a)
-- Fields:
--   prefix: The most significant bits shared by all keys in this Bin.
--   mask: The switching bit to determine if a key should follow the left
--         or right subtree of a 'Bin'.
-- Invariant: Nil is never found as a child of Bin.
-- Invariant: The Mask is a power of 2. It is the largest bit position at which
--            two keys of the map differ.
-- Invariant: Prefix is the common high-order bits that all elements share to
--            the left of the Mask bit.
-- Invariant: In (Bin prefix mask left right), left consists of the elements that
--            don't have the mask bit set; right is all the elements that do.
              | Tip {-# UNPACK #-} !Key a
              | Nil

type Prefix = Word64
type Mask   = Word64


-- Some stuff from "Data.Word64Set.Internal", for 'restrictKeys' and
-- 'withoutKeys' to use.
type Word64SetPrefix = Word64
type Word64SetBitMap = Word64

bitmapOf :: Word64 -> Word64SetBitMap
bitmapOf :: Word64 -> Word64
bitmapOf Word64
x = Word64 -> Int -> Word64
shiftLL Word64
1 (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
x Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.suffixBitMask))
{-# INLINE bitmapOf #-}

{--------------------------------------------------------------------
  Operators
--------------------------------------------------------------------}

-- | \(O(\min(n,W))\). Find the value at a key.
-- Calls 'error' when the element can not be found.
--
-- > fromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
-- > fromList [(5,'a'), (3,'b')] ! 5 == 'a'

(!) :: Word64Map a -> Key -> a
! :: forall a. Word64Map a -> Word64 -> a
(!) Word64Map a
m Word64
k = Word64 -> Word64Map a -> a
forall a. Word64 -> Word64Map a -> a
find Word64
k Word64Map a
m

-- | \(O(\min(n,W))\). Find the value at a key.
-- Returns 'Nothing' when the element can not be found.
--
-- > fromList [(5,'a'), (3,'b')] !? 1 == Nothing
-- > fromList [(5,'a'), (3,'b')] !? 5 == Just 'a'
--
-- @since 0.5.11

(!?) :: Word64Map a -> Key -> Maybe a
!? :: forall a. Word64Map a -> Word64 -> Maybe a
(!?) Word64Map a
m Word64
k = Word64 -> Word64Map a -> Maybe a
forall a. Word64 -> Word64Map a -> Maybe a
lookup Word64
k Word64Map a
m

-- | Same as 'difference'.
(\\) :: Word64Map a -> Word64Map b -> Word64Map a
Word64Map a
m1 \\ :: forall a b. Word64Map a -> Word64Map b -> Word64Map a
\\ Word64Map b
m2 = Word64Map a -> Word64Map b -> Word64Map a
forall a b. Word64Map a -> Word64Map b -> Word64Map a
difference Word64Map a
m1 Word64Map b
m2

infixl 9 !?,\\{-This comment teaches CPP correct behaviour -}

{--------------------------------------------------------------------
  Types
--------------------------------------------------------------------}

instance Monoid (Word64Map a) where
    mempty :: Word64Map a
mempty  = Word64Map a
forall a. Word64Map a
empty
    mconcat :: [Word64Map a] -> Word64Map a
mconcat = [Word64Map a] -> Word64Map a
forall (f :: * -> *) a.
Foldable f =>
f (Word64Map a) -> Word64Map a
unions
    mappend :: Word64Map a -> Word64Map a -> Word64Map a
mappend = Word64Map a -> Word64Map a -> Word64Map a
forall a. Semigroup a => a -> a -> a
(<>)

-- | @since 0.5.7
instance Semigroup (Word64Map a) where
    <> :: Word64Map a -> Word64Map a -> Word64Map a
(<>)    = Word64Map a -> Word64Map a -> Word64Map a
forall a. Word64Map a -> Word64Map a -> Word64Map a
union
    stimes :: forall b. Integral b => b -> Word64Map a -> Word64Map a
stimes  = b -> Word64Map a -> Word64Map a
forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid

-- | Folds in order of increasing key.
instance Foldable.Foldable Word64Map where
  fold :: forall m. Monoid m => Word64Map m -> m
fold = Word64Map m -> m
forall m. Monoid m => Word64Map m -> m
go
    where go :: Word64Map t -> t
go Word64Map t
Nil = t
forall a. Monoid a => a
mempty
          go (Tip Word64
_ t
v) = t
v
          go (Bin Word64
_ Word64
m Word64Map t
l Word64Map t
r)
            | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = Word64Map t -> t
go Word64Map t
r t -> t -> t
forall a. Monoid a => a -> a -> a
`mappend` Word64Map t -> t
go Word64Map t
l
            | Bool
otherwise = Word64Map t -> t
go Word64Map t
l t -> t -> t
forall a. Monoid a => a -> a -> a
`mappend` Word64Map t -> t
go Word64Map t
r
  {-# INLINABLE fold #-}
  foldr :: forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr = (a -> b -> b) -> b -> Word64Map a -> b
forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr
  {-# INLINE foldr #-}
  foldl :: forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl = (b -> a -> b) -> b -> Word64Map a -> b
forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl
  {-# INLINE foldl #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> Word64Map a -> m
foldMap a -> m
f Word64Map a
t = Word64Map a -> m
go Word64Map a
t
    where go :: Word64Map a -> m
go Word64Map a
Nil = m
forall a. Monoid a => a
mempty
          go (Tip Word64
_ a
v) = a -> m
f a
v
          go (Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r)
            | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = Word64Map a -> m
go Word64Map a
r m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Word64Map a -> m
go Word64Map a
l
            | Bool
otherwise = Word64Map a -> m
go Word64Map a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Word64Map a -> m
go Word64Map a
r
  {-# INLINE foldMap #-}
  foldl' :: forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl' = (b -> a -> b) -> b -> Word64Map a -> b
forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl'
  {-# INLINE foldl' #-}
  foldr' :: forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr' = (a -> b -> b) -> b -> Word64Map a -> b
forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr'
  {-# INLINE foldr' #-}
  length :: forall a. Word64Map a -> Int
length = Word64Map a -> Int
forall a. Word64Map a -> Int
size
  {-# INLINE length #-}
  null :: forall a. Word64Map a -> Bool
null   = Word64Map a -> Bool
forall a. Word64Map a -> Bool
null
  {-# INLINE null #-}
  toList :: forall a. Word64Map a -> [a]
toList = Word64Map a -> [a]
forall a. Word64Map a -> [a]
elems -- NB: Foldable.toList /= Word64Map.toList
  {-# INLINE toList #-}
  elem :: forall a. Eq a => a -> Word64Map a -> Bool
elem = a -> Word64Map a -> Bool
forall a. Eq a => a -> Word64Map a -> Bool
go
    where go :: t -> Word64Map t -> Bool
go !t
_ Word64Map t
Nil = Bool
False
          go t
x (Tip Word64
_ t
y) = t
x t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
y
          go t
x (Bin Word64
_ Word64
_ Word64Map t
l Word64Map t
r) = t -> Word64Map t -> Bool
go t
x Word64Map t
l Bool -> Bool -> Bool
|| t -> Word64Map t -> Bool
go t
x Word64Map t
r
  {-# INLINABLE elem #-}
  maximum :: forall a. Ord a => Word64Map a -> a
maximum = Word64Map a -> a
forall a. Ord a => Word64Map a -> a
start
    where start :: Word64Map t -> t
start Word64Map t
Nil = [Char] -> t
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Foldable.maximum (for Data.Word64Map): empty map"
          start (Tip Word64
_ t
y) = t
y
          start (Bin Word64
_ Word64
m Word64Map t
l Word64Map t
r)
            | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = t -> Word64Map t -> t
forall {t}. Ord t => t -> Word64Map t -> t
go (Word64Map t -> t
start Word64Map t
r) Word64Map t
l
            | Bool
otherwise = t -> Word64Map t -> t
forall {t}. Ord t => t -> Word64Map t -> t
go (Word64Map t -> t
start Word64Map t
l) Word64Map t
r

          go :: t -> Word64Map t -> t
go !t
m Word64Map t
Nil = t
m
          go t
m (Tip Word64
_ t
y) = t -> t -> t
forall a. Ord a => a -> a -> a
max t
m t
y
          go t
m (Bin Word64
_ Word64
_ Word64Map t
l Word64Map t
r) = t -> Word64Map t -> t
go (t -> Word64Map t -> t
go t
m Word64Map t
l) Word64Map t
r
  {-# INLINABLE maximum #-}
  minimum :: forall a. Ord a => Word64Map a -> a
minimum = Word64Map a -> a
forall a. Ord a => Word64Map a -> a
start
    where start :: Word64Map t -> t
start Word64Map t
Nil = [Char] -> t
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Foldable.minimum (for Data.Word64Map): empty map"
          start (Tip Word64
_ t
y) = t
y
          start (Bin Word64
_ Word64
m Word64Map t
l Word64Map t
r)
            | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = t -> Word64Map t -> t
forall {t}. Ord t => t -> Word64Map t -> t
go (Word64Map t -> t
start Word64Map t
r) Word64Map t
l
            | Bool
otherwise = t -> Word64Map t -> t
forall {t}. Ord t => t -> Word64Map t -> t
go (Word64Map t -> t
start Word64Map t
l) Word64Map t
r

          go :: t -> Word64Map t -> t
go !t
m Word64Map t
Nil = t
m
          go t
m (Tip Word64
_ t
y) = t -> t -> t
forall a. Ord a => a -> a -> a
min t
m t
y
          go t
m (Bin Word64
_ Word64
_ Word64Map t
l Word64Map t
r) = t -> Word64Map t -> t
go (t -> Word64Map t -> t
go t
m Word64Map t
l) Word64Map t
r
  {-# INLINABLE minimum #-}
  sum :: forall a. Num a => Word64Map a -> a
sum = (a -> a -> a) -> a -> Word64Map a -> a
forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
  {-# INLINABLE sum #-}
  product :: forall a. Num a => Word64Map a -> a
product = (a -> a -> a) -> a -> Word64Map a -> a
forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
  {-# INLINABLE product #-}

-- | Traverses in order of increasing key.
instance Traversable Word64Map where
    traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Word64Map a -> f (Word64Map b)
traverse a -> f b
f = (Word64 -> a -> f b) -> Word64Map a -> f (Word64Map b)
forall (t :: * -> *) a b.
Applicative t =>
(Word64 -> a -> t b) -> Word64Map a -> t (Word64Map b)
traverseWithKey (\Word64
_ -> a -> f b
f)
    {-# INLINE traverse #-}

instance NFData a => NFData (Word64Map a) where
    rnf :: Word64Map a -> ()
rnf Word64Map a
Nil = ()
    rnf (Tip Word64
_ a
v) = a -> ()
forall a. NFData a => a -> ()
rnf a
v
    rnf (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = Word64Map a -> ()
forall a. NFData a => a -> ()
rnf Word64Map a
l () -> () -> ()
forall a b. a -> b -> b
`seq` Word64Map a -> ()
forall a. NFData a => a -> ()
rnf Word64Map a
r

#if __GLASGOW_HASKELL__

{--------------------------------------------------------------------
  A Data instance
--------------------------------------------------------------------}

-- This instance preserves data abstraction at the cost of inefficiency.
-- We provide limited reflection services for the sake of data abstraction.

instance Data a => Data (Word64Map a) where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Word64Map a -> c (Word64Map a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z Word64Map a
im = ([(Word64, a)] -> Word64Map a) -> c ([(Word64, a)] -> Word64Map a)
forall g. g -> c g
z [(Word64, a)] -> Word64Map a
forall a. [(Word64, a)] -> Word64Map a
fromList c ([(Word64, a)] -> Word64Map a)
-> [(Word64, a)] -> c (Word64Map a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
im)
  toConstr :: Word64Map a -> Constr
toConstr Word64Map a
_     = Constr
fromListConstr
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Word64Map a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c  = case Constr -> Int
constrIndex Constr
c of
    Int
1 -> c ([(Word64, a)] -> Word64Map a) -> c (Word64Map a)
forall b r. Data b => c (b -> r) -> c r
k (([(Word64, a)] -> Word64Map a) -> c ([(Word64, a)] -> Word64Map a)
forall r. r -> c r
z [(Word64, a)] -> Word64Map a
forall a. [(Word64, a)] -> Word64Map a
fromList)
    Int
_ -> [Char] -> c (Word64Map a)
forall a. HasCallStack => [Char] -> a
error [Char]
"gunfold"
  dataTypeOf :: Word64Map a -> DataType
dataTypeOf Word64Map a
_   = DataType
intMapDataType
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Word64Map a))
dataCast1 forall d. Data d => c (t d)
f    = c (t a) -> Maybe (c (Word64Map a))
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
f

fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> [Char] -> [[Char]] -> Fixity -> Constr
mkConstr DataType
intMapDataType [Char]
"fromList" [] Fixity
Prefix

intMapDataType :: DataType
intMapDataType :: DataType
intMapDataType = [Char] -> [Constr] -> DataType
mkDataType [Char]
"Data.Word64Map.Internal.Word64Map" [Constr
fromListConstr]

#endif

{--------------------------------------------------------------------
  Query
--------------------------------------------------------------------}
-- | \(O(1)\). Is the map empty?
--
-- > Data.Word64Map.null (empty)           == True
-- > Data.Word64Map.null (singleton 1 'a') == False

null :: Word64Map a -> Bool
null :: forall a. Word64Map a -> Bool
null Word64Map a
Nil = Bool
True
null Word64Map a
_   = Bool
False
{-# INLINE null #-}

-- | \(O(n)\). Number of elements in the map.
--
-- > size empty                                   == 0
-- > size (singleton 1 'a')                       == 1
-- > size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
size :: Word64Map a -> Int
size :: forall a. Word64Map a -> Int
size = Int -> Word64Map a -> Int
forall {t} {a}. Num t => t -> Word64Map a -> t
go Int
0
  where
    go :: t -> Word64Map a -> t
go !t
acc (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = t -> Word64Map a -> t
go (t -> Word64Map a -> t
go t
acc Word64Map a
l) Word64Map a
r
    go t
acc (Tip Word64
_ a
_) = t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
acc
    go t
acc Word64Map a
Nil = t
acc

-- | \(O(\min(n,W))\). Is the key a member of the map?
--
-- > member 5 (fromList [(5,'a'), (3,'b')]) == True
-- > member 1 (fromList [(5,'a'), (3,'b')]) == False

-- See Note: Local 'go' functions and capturing]
member :: Key -> Word64Map a -> Bool
member :: forall a. Word64 -> Word64Map a -> Bool
member !Word64
k = Word64Map a -> Bool
go
  where
    go :: Word64Map a -> Bool
go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = Bool
False
                     | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Bool
go Word64Map a
l
                     | Bool
otherwise = Word64Map a -> Bool
go Word64Map a
r
    go (Tip Word64
kx a
_) = Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kx
    go Word64Map a
Nil = Bool
False

-- | \(O(\min(n,W))\). Is the key not a member of the map?
--
-- > notMember 5 (fromList [(5,'a'), (3,'b')]) == False
-- > notMember 1 (fromList [(5,'a'), (3,'b')]) == True

notMember :: Key -> Word64Map a -> Bool
notMember :: forall a. Word64 -> Word64Map a -> Bool
notMember Word64
k Word64Map a
m = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Word64 -> Word64Map a -> Bool
forall a. Word64 -> Word64Map a -> Bool
member Word64
k Word64Map a
m

-- | \(O(\min(n,W))\). Lookup the value at a key in the map. See also 'Data.Map.lookup'.

-- See Note: Local 'go' functions and capturing
lookup :: Key -> Word64Map a -> Maybe a
lookup :: forall a. Word64 -> Word64Map a -> Maybe a
lookup !Word64
k = Word64Map a -> Maybe a
go
  where
    go :: Word64Map a -> Maybe a
go (Bin Word64
_p Word64
m Word64Map a
l Word64Map a
r) | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Maybe a
go Word64Map a
l
                      | Bool
otherwise = Word64Map a -> Maybe a
go Word64Map a
r
    go (Tip Word64
kx a
x) | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kx   = a -> Maybe a
forall a. a -> Maybe a
Just a
x
                  | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
    go Word64Map a
Nil = Maybe a
forall a. Maybe a
Nothing

-- See Note: Local 'go' functions and capturing]
find :: Key -> Word64Map a -> a
find :: forall a. Word64 -> Word64Map a -> a
find !Word64
k = Word64Map a -> a
go
  where
    go :: Word64Map a -> a
go (Bin Word64
_p Word64
m Word64Map a
l Word64Map a
r) | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> a
go Word64Map a
l
                      | Bool
otherwise = Word64Map a -> a
go Word64Map a
r
    go (Tip Word64
kx a
x) | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kx   = a
x
                  | Bool
otherwise = a
not_found
    go Word64Map a
Nil = a
not_found

    not_found :: a
not_found = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char]
"Word64Map.!: key " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Word64 -> [Char]
forall a. Show a => a -> [Char]
show Word64
k [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is not an element of the map")

-- | \(O(\min(n,W))\). The expression @('findWithDefault' def k map)@
-- returns the value at key @k@ or returns @def@ when the key is not an
-- element of the map.
--
-- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
-- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'

-- See Note: Local 'go' functions and capturing]
findWithDefault :: a -> Key -> Word64Map a -> a
findWithDefault :: forall a. a -> Word64 -> Word64Map a -> a
findWithDefault a
def !Word64
k = Word64Map a -> a
go
  where
    go :: Word64Map a -> a
go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = a
def
                     | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> a
go Word64Map a
l
                     | Bool
otherwise = Word64Map a -> a
go Word64Map a
r
    go (Tip Word64
kx a
x) | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kx   = a
x
                  | Bool
otherwise = a
def
    go Word64Map a
Nil = a
def

-- | \(O(\min(n,W))\). Find largest key smaller than the given one and return the
-- corresponding (key, value) pair.
--
-- > lookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
-- > lookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')

-- See Note: Local 'go' functions and capturing.
lookupLT :: Key -> Word64Map a -> Maybe (Key, a)
lookupLT :: forall a. Word64 -> Word64Map a -> Maybe (Word64, a)
lookupLT !Word64
k Word64Map a
t = case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 then Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
r Word64Map a
l else Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
r
    Word64Map a
_ -> Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
t
  where
    go :: Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
p then Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def else Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
r
      | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def Word64Map a
l
      | Bool
otherwise = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
l Word64Map a
r
    go Word64Map a
def (Tip Word64
ky a
y)
      | Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
ky   = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def
      | Bool
otherwise = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
    go Word64Map a
def Word64Map a
Nil = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def

-- | \(O(\min(n,W))\). Find smallest key greater than the given one and return the
-- corresponding (key, value) pair.
--
-- > lookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
-- > lookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing

-- See Note: Local 'go' functions and capturing.
lookupGT :: Key -> Word64Map a -> Maybe (Key, a)
lookupGT :: forall a. Word64 -> Word64Map a -> Maybe (Word64, a)
lookupGT !Word64
k Word64Map a
t = case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 then Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
l else Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
l Word64Map a
r
    Word64Map a
_ -> Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
t
  where
    go :: Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
p then Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
l else Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def
      | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
r Word64Map a
l
      | Bool
otherwise = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def Word64Map a
r
    go Word64Map a
def (Tip Word64
ky a
y)
      | Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
ky   = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def
      | Bool
otherwise = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
    go Word64Map a
def Word64Map a
Nil = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def

-- | \(O(\min(n,W))\). Find largest key smaller or equal to the given one and return
-- the corresponding (key, value) pair.
--
-- > lookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
-- > lookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
-- > lookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')

-- See Note: Local 'go' functions and capturing.
lookupLE :: Key -> Word64Map a -> Maybe (Key, a)
lookupLE :: forall a. Word64 -> Word64Map a -> Maybe (Word64, a)
lookupLE !Word64
k Word64Map a
t = case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 then Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
r Word64Map a
l else Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
r
    Word64Map a
_ -> Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
t
  where
    go :: Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
p then Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def else Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
r
      | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def Word64Map a
l
      | Bool
otherwise = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
l Word64Map a
r
    go Word64Map a
def (Tip Word64
ky a
y)
      | Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
ky    = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def
      | Bool
otherwise = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
    go Word64Map a
def Word64Map a
Nil = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
def

-- | \(O(\min(n,W))\). Find smallest key greater or equal to the given one and return
-- the corresponding (key, value) pair.
--
-- > lookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
-- > lookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
-- > lookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing

-- See Note: Local 'go' functions and capturing.
lookupGE :: Key -> Word64Map a -> Maybe (Key, a)
lookupGE :: forall a. Word64 -> Word64Map a -> Maybe (Word64, a)
lookupGE !Word64
k Word64Map a
t = case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 then Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
l else Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
l Word64Map a
r
    Word64Map a
_ -> Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
forall a. Word64Map a
Nil Word64Map a
t
  where
    go :: Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
p then Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
l else Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def
      | Word64 -> Word64 -> Bool
zero Word64
k Word64
m  = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
r Word64Map a
l
      | Bool
otherwise = Word64Map a -> Word64Map a -> Maybe (Word64, a)
go Word64Map a
def Word64Map a
r
    go Word64Map a
def (Tip Word64
ky a
y)
      | Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
ky    = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def
      | Bool
otherwise = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
    go Word64Map a
def Word64Map a
Nil = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
def


-- Helper function for lookupGE and lookupGT. It assumes that if a Bin node is
-- given, it has m > 0.
unsafeFindMin :: Word64Map a -> Maybe (Key, a)
unsafeFindMin :: forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
Nil = Maybe (Word64, a)
forall a. Maybe a
Nothing
unsafeFindMin (Tip Word64
ky a
y) = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
unsafeFindMin (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
_) = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMin Word64Map a
l

-- Helper function for lookupLE and lookupLT. It assumes that if a Bin node is
-- given, it has m > 0.
unsafeFindMax :: Word64Map a -> Maybe (Key, a)
unsafeFindMax :: forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
Nil = Maybe (Word64, a)
forall a. Maybe a
Nothing
unsafeFindMax (Tip Word64
ky a
y) = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
ky, a
y)
unsafeFindMax (Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
r) = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
unsafeFindMax Word64Map a
r

{--------------------------------------------------------------------
  Disjoint
--------------------------------------------------------------------}
-- | \(O(n+m)\). Check whether the key sets of two maps are disjoint
-- (i.e. their 'intersection' is empty).
--
-- > disjoint (fromList [(2,'a')]) (fromList [(1,()), (3,())])   == True
-- > disjoint (fromList [(2,'a')]) (fromList [(1,'a'), (2,'b')]) == False
-- > disjoint (fromList [])        (fromList [])                 == True
--
-- > disjoint a b == null (intersection a b)
--
-- @since 0.6.2.1
disjoint :: Word64Map a -> Word64Map b -> Bool
disjoint :: forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
Nil Word64Map b
_ = Bool
True
disjoint Word64Map a
_ Word64Map b
Nil = Bool
True
disjoint (Tip Word64
kx a
_) Word64Map b
ys = Word64 -> Word64Map b -> Bool
forall a. Word64 -> Word64Map a -> Bool
notMember Word64
kx Word64Map b
ys
disjoint Word64Map a
xs (Tip Word64
ky b
_) = Word64 -> Word64Map a -> Bool
forall a. Word64 -> Word64Map a -> Bool
notMember Word64
ky Word64Map a
xs
disjoint t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) t2 :: Word64Map b
t2@(Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
  | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2 = Bool
disjoint1
  | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1 = Bool
disjoint2
  | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2      = Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
l1 Word64Map b
l2 Bool -> Bool -> Bool
&& Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
r1 Word64Map b
r2
  | Bool
otherwise     = Bool
True
  where
    disjoint1 :: Bool
disjoint1 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p2 Word64
p1 Word64
m1 = Bool
True
              | Word64 -> Word64 -> Bool
zero Word64
p2 Word64
m1       = Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
l1 Word64Map b
t2
              | Bool
otherwise        = Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
r1 Word64Map b
t2
    disjoint2 :: Bool
disjoint2 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2 = Bool
True
              | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2       = Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
t1 Word64Map b
l2
              | Bool
otherwise        = Word64Map a -> Word64Map b -> Bool
forall a b. Word64Map a -> Word64Map b -> Bool
disjoint Word64Map a
t1 Word64Map b
r2

{--------------------------------------------------------------------
  Compose
--------------------------------------------------------------------}
-- | Relate the keys of one map to the values of
-- the other, by using the values of the former as keys for lookups
-- in the latter.
--
-- Complexity: \( O(n * \min(m,W)) \), where \(m\) is the size of the first argument
--
-- > compose (fromList [('a', "A"), ('b', "B")]) (fromList [(1,'a'),(2,'b'),(3,'z')]) = fromList [(1,"A"),(2,"B")]
--
-- @
-- ('compose' bc ab '!?') = (bc '!?') <=< (ab '!?')
-- @
--
-- __Note:__ Prior to v0.6.4, "Data.Word64Map.Strict" exposed a version of
-- 'compose' that forced the values of the output 'Word64Map'. This version does
-- not force these values.
--
-- @since 0.6.3.1
compose :: Word64Map c -> Word64Map Word64 -> Word64Map c
compose :: forall c. Word64Map c -> Word64Map Word64 -> Word64Map c
compose Word64Map c
bc !Word64Map Word64
ab
  | Word64Map c -> Bool
forall a. Word64Map a -> Bool
null Word64Map c
bc = Word64Map c
forall a. Word64Map a
empty
  | Bool
otherwise = (Word64 -> Maybe c) -> Word64Map Word64 -> Word64Map c
forall a b. (a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybe (Word64Map c
bc Word64Map c -> Word64 -> Maybe c
forall a. Word64Map a -> Word64 -> Maybe a
!?) Word64Map Word64
ab

{--------------------------------------------------------------------
  Construction
--------------------------------------------------------------------}
-- | \(O(1)\). The empty map.
--
-- > empty      == fromList []
-- > size empty == 0

empty :: Word64Map a
empty :: forall a. Word64Map a
empty
  = Word64Map a
forall a. Word64Map a
Nil
{-# INLINE empty #-}

-- | \(O(1)\). A map of one element.
--
-- > singleton 1 'a'        == fromList [(1, 'a')]
-- > size (singleton 1 'a') == 1

singleton :: Key -> a -> Word64Map a
singleton :: forall a. Word64 -> a -> Word64Map a
singleton Word64
k a
x
  = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x
{-# INLINE singleton #-}

{--------------------------------------------------------------------
  Insert
--------------------------------------------------------------------}
-- | \(O(\min(n,W))\). Insert a new key\/value pair in the map.
-- If the key is already present in the map, the associated value is
-- replaced with the supplied value, i.e. 'insert' is equivalent to
-- @'insertWith' 'const'@.
--
-- > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
-- > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
-- > insert 5 'x' empty                         == singleton 5 'x'

insert :: Key -> a -> Word64Map a -> Word64Map a
insert :: forall a. Word64 -> a -> Word64Map a -> Word64Map a
insert !Word64
k a
x t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
p Word64Map a
t
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m (Word64 -> a -> Word64Map a -> Word64Map a
forall a. Word64 -> a -> Word64Map a -> Word64Map a
insert Word64
k a
x Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l (Word64 -> a -> Word64Map a -> Word64Map a
forall a. Word64 -> a -> Word64Map a -> Word64Map a
insert Word64
k a
x Word64Map a
r)
insert Word64
k a
x t :: Word64Map a
t@(Tip Word64
ky a
_)
  | Word64
kWord64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==Word64
ky         = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x
  | Bool
otherwise     = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
ky Word64Map a
t
insert Word64
k a
x Word64Map a
Nil = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x

-- right-biased insertion, used by 'union'
-- | \(O(\min(n,W))\). Insert with a combining function.
-- @'insertWith' f key value mp@
-- will insert the pair (key, value) into @mp@ if key does
-- not exist in the map. If the key does exist, the function will
-- insert @f new_value old_value@.
--
-- > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
-- > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
-- > insertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"

insertWith :: (a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
insertWith :: forall a.
(a -> a -> a) -> Word64 -> a -> Word64Map a -> Word64Map a
insertWith a -> a -> a
f Word64
k a
x Word64Map a
t
  = (Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
insertWithKey (\Word64
_ a
x' a
y' -> a -> a -> a
f a
x' a
y') Word64
k a
x Word64Map a
t

-- | \(O(\min(n,W))\). Insert with a combining function.
-- @'insertWithKey' f key value mp@
-- will insert the pair (key, value) into @mp@ if key does
-- not exist in the map. If the key does exist, the function will
-- insert @f key new_value old_value@.
--
-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
-- > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
-- > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
-- > insertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"

insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
insertWithKey :: forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
insertWithKey Word64 -> a -> a -> a
f !Word64
k a
x t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
p Word64Map a
t
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m ((Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
insertWithKey Word64 -> a -> a -> a
f Word64
k a
x Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l ((Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
insertWithKey Word64 -> a -> a -> a
f Word64
k a
x Word64Map a
r)
insertWithKey Word64 -> a -> a -> a
f Word64
k a
x t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky       = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k (Word64 -> a -> a -> a
f Word64
k a
x a
y)
  | Bool
otherwise     = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
ky Word64Map a
t
insertWithKey Word64 -> a -> a -> a
_ Word64
k a
x Word64Map a
Nil = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x

-- | \(O(\min(n,W))\). The expression (@'insertLookupWithKey' f k x map@)
-- is a pair where the first element is equal to (@'lookup' k map@)
-- and the second element equal to (@'insertWithKey' f k x map@).
--
-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
-- > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
-- > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
-- > insertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
--
-- This is how to define @insertLookup@ using @insertLookupWithKey@:
--
-- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t
-- > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
-- > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])

insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> (Maybe a, Word64Map a)
insertLookupWithKey :: forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> (Maybe a, Word64Map a)
insertLookupWithKey Word64 -> a -> a -> a
f !Word64
k a
x t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = (Maybe a
forall a. Maybe a
Nothing,Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
p Word64Map a
t)
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = let (Maybe a
found,Word64Map a
l') = (Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> (Maybe a, Word64Map a)
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> (Maybe a, Word64Map a)
insertLookupWithKey Word64 -> a -> a -> a
f Word64
k a
x Word64Map a
l
                    in (Maybe a
found,Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l' Word64Map a
r)
  | Bool
otherwise     = let (Maybe a
found,Word64Map a
r') = (Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> (Maybe a, Word64Map a)
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> (Maybe a, Word64Map a)
insertLookupWithKey Word64 -> a -> a -> a
f Word64
k a
x Word64Map a
r
                    in (Maybe a
found,Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l Word64Map a
r')
insertLookupWithKey Word64 -> a -> a -> a
f Word64
k a
x t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky       = (a -> Maybe a
forall a. a -> Maybe a
Just a
y,Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k (Word64 -> a -> a -> a
f Word64
k a
x a
y))
  | Bool
otherwise     = (Maybe a
forall a. Maybe a
Nothing,Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
ky Word64Map a
t)
insertLookupWithKey Word64 -> a -> a -> a
_ Word64
k a
x Word64Map a
Nil = (Maybe a
forall a. Maybe a
Nothing,Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x)


{--------------------------------------------------------------------
  Deletion
--------------------------------------------------------------------}
-- | \(O(\min(n,W))\). Delete a key and its value from the map. When the key is not
-- a member of the map, the original map is returned.
--
-- > delete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
-- > delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
-- > delete 5 empty                         == empty

delete :: Key -> Word64Map a -> Word64Map a
delete :: forall a. Word64 -> Word64Map a -> Word64Map a
delete !Word64
k t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = Word64Map a
t
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
delete Word64
k Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
delete Word64
k Word64Map a
r)
delete Word64
k t :: Word64Map a
t@(Tip Word64
ky a
_)
  | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky       = Word64Map a
forall a. Word64Map a
Nil
  | Bool
otherwise     = Word64Map a
t
delete Word64
_k Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil

-- | \(O(\min(n,W))\). Adjust a value at a specific key. When the key is not
-- a member of the map, the original map is returned.
--
-- > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
-- > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
-- > adjust ("new " ++) 7 empty                         == empty

adjust ::  (a -> a) -> Key -> Word64Map a -> Word64Map a
adjust :: forall a. (a -> a) -> Word64 -> Word64Map a -> Word64Map a
adjust a -> a
f Word64
k Word64Map a
m
  = (Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
adjustWithKey (\Word64
_ a
x -> a -> a
f a
x) Word64
k Word64Map a
m

-- | \(O(\min(n,W))\). Adjust a value at a specific key. When the key is not
-- a member of the map, the original map is returned.
--
-- > let f key x = (show key) ++ ":new " ++ x
-- > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
-- > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
-- > adjustWithKey f 7 empty                         == empty

adjustWithKey ::  (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
adjustWithKey :: forall a.
(Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
adjustWithKey Word64 -> a -> a
f !Word64
k (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m ((Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
adjustWithKey Word64 -> a -> a
f Word64
k Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l ((Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a) -> Word64 -> Word64Map a -> Word64Map a
adjustWithKey Word64 -> a -> a
f Word64
k Word64Map a
r)
adjustWithKey Word64 -> a -> a
f Word64
k t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky       = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
ky (Word64 -> a -> a
f Word64
k a
y)
  | Bool
otherwise     = Word64Map a
t
adjustWithKey Word64 -> a -> a
_ Word64
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil


-- | \(O(\min(n,W))\). The expression (@'update' f k map@) updates the value @x@
-- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is
-- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
--
-- > let f x = if x == "a" then Just "new a" else Nothing
-- > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
-- > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
-- > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"

update ::  (a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
update :: forall a. (a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
update a -> Maybe a
f
  = (Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
updateWithKey (\Word64
_ a
x -> a -> Maybe a
f a
x)

-- | \(O(\min(n,W))\). The expression (@'update' f k map@) updates the value @x@
-- at @k@ (if it is in the map). If (@f k x@) is 'Nothing', the element is
-- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
--
-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
-- > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
-- > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
-- > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"

updateWithKey ::  (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
updateWithKey :: forall a.
(Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
updateWithKey Word64 -> a -> Maybe a
f !Word64
k (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m ((Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
updateWithKey Word64 -> a -> Maybe a
f Word64
k Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l ((Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
updateWithKey Word64 -> a -> Maybe a
f Word64
k Word64Map a
r)
updateWithKey Word64 -> a -> Maybe a
f Word64
k t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
k Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky       = case (Word64 -> a -> Maybe a
f Word64
k a
y) of
                      Just a
y' -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
ky a
y'
                      Maybe a
Nothing -> Word64Map a
forall a. Word64Map a
Nil
  | Bool
otherwise     = Word64Map a
t
updateWithKey Word64 -> a -> Maybe a
_ Word64
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil

-- | \(O(\min(n,W))\). Lookup and update.
-- The function returns original value, if it is updated.
-- This is different behavior than 'Data.Map.updateLookupWithKey'.
-- Returns the original key value if the map entry is deleted.
--
-- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
-- > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
-- > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
-- > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")

updateLookupWithKey ::  (Key -> a -> Maybe a) -> Key -> Word64Map a -> (Maybe a,Word64Map a)
updateLookupWithKey :: forall a.
(Word64 -> a -> Maybe a)
-> Word64 -> Word64Map a -> (Maybe a, Word64Map a)
updateLookupWithKey Word64 -> a -> Maybe a
f !Word64
k (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = let !(Maybe a
found,Word64Map a
l') = (Word64 -> a -> Maybe a)
-> Word64 -> Word64Map a -> (Maybe a, Word64Map a)
forall a.
(Word64 -> a -> Maybe a)
-> Word64 -> Word64Map a -> (Maybe a, Word64Map a)
updateLookupWithKey Word64 -> a -> Maybe a
f Word64
k Word64Map a
l
                    in (Maybe a
found,Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m Word64Map a
l' Word64Map a
r)
  | Bool
otherwise     = let !(Maybe a
found,Word64Map a
r') = (Word64 -> a -> Maybe a)
-> Word64 -> Word64Map a -> (Maybe a, Word64Map a)
forall a.
(Word64 -> a -> Maybe a)
-> Word64 -> Word64Map a -> (Maybe a, Word64Map a)
updateLookupWithKey Word64 -> a -> Maybe a
f Word64
k Word64Map a
r
                    in (Maybe a
found,Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l Word64Map a
r')
updateLookupWithKey Word64 -> a -> Maybe a
f Word64
k t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
kWord64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==Word64
ky         = case (Word64 -> a -> Maybe a
f Word64
k a
y) of
                      Just a
y' -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y,Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
ky a
y')
                      Maybe a
Nothing -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y,Word64Map a
forall a. Word64Map a
Nil)
  | Bool
otherwise     = (Maybe a
forall a. Maybe a
Nothing,Word64Map a
t)
updateLookupWithKey Word64 -> a -> Maybe a
_ Word64
_ Word64Map a
Nil = (Maybe a
forall a. Maybe a
Nothing,Word64Map a
forall a. Word64Map a
Nil)



-- | \(O(\min(n,W))\). The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof.
-- 'alter' can be used to insert, delete, or update a value in an 'Word64Map'.
-- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.
alter :: (Maybe a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
alter :: forall a.
(Maybe a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
alter Maybe a -> Maybe a
f !Word64
k t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k Word64
p Word64
m = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                      Maybe a
Nothing -> Word64Map a
t
                      Just a
x -> Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
p Word64Map a
t
  | Word64 -> Word64 -> Bool
zero Word64
k Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m ((Maybe a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Maybe a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
alter Maybe a -> Maybe a
f Word64
k Word64Map a
l) Word64Map a
r
  | Bool
otherwise     = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l ((Maybe a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
forall a.
(Maybe a -> Maybe a) -> Word64 -> Word64Map a -> Word64Map a
alter Maybe a -> Maybe a
f Word64
k Word64Map a
r)
alter Maybe a -> Maybe a
f Word64
k t :: Word64Map a
t@(Tip Word64
ky a
y)
  | Word64
kWord64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==Word64
ky         = case Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
y) of
                      Just a
x -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
ky a
x
                      Maybe a
Nothing -> Word64Map a
forall a. Word64Map a
Nil
  | Bool
otherwise     = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                      Just a
x -> Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x) Word64
ky Word64Map a
t
                      Maybe a
Nothing -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
ky a
y
alter Maybe a -> Maybe a
f Word64
k Word64Map a
Nil     = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                      Just a
x -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
x
                      Maybe a
Nothing -> Word64Map a
forall a. Word64Map a
Nil

-- | \(O(\min(n,W))\). The expression (@'alterF' f k map@) alters the value @x@ at
-- @k@, or absence thereof.  'alterF' can be used to inspect, insert, delete,
-- or update a value in an 'Word64Map'.  In short : @'lookup' k <$> 'alterF' f k m = f
-- ('lookup' k m)@.
--
-- Example:
--
-- @
-- interactiveAlter :: Int -> Word64Map String -> IO (Word64Map String)
-- interactiveAlter k m = alterF f k m where
--   f Nothing = do
--      putStrLn $ show k ++
--          " was not found in the map. Would you like to add it?"
--      getUserResponse1 :: IO (Maybe String)
--   f (Just old) = do
--      putStrLn $ "The key is currently bound to " ++ show old ++
--          ". Would you like to change or delete it?"
--      getUserResponse2 :: IO (Maybe String)
-- @
--
-- 'alterF' is the most general operation for working with an individual
-- key that may or may not be in a given map.
--
-- Note: 'alterF' is a flipped version of the @at@ combinator from
-- @Control.Lens.At@.
--
-- @since 0.5.8

alterF :: Functor f
       => (Maybe a -> f (Maybe a)) -> Key -> Word64Map a -> f (Word64Map a)
-- This implementation was stolen from 'Control.Lens.At'.
alterF :: forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f (Maybe a))
-> Word64 -> Word64Map a -> f (Word64Map a)
alterF Maybe a -> f (Maybe a)
f Word64
k Word64Map a
m = ((Maybe a -> Word64Map a) -> f (Maybe a) -> f (Word64Map a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f (Maybe a)
f Maybe a
mv) ((Maybe a -> Word64Map a) -> f (Word64Map a))
-> (Maybe a -> Word64Map a) -> f (Word64Map a)
forall a b. (a -> b) -> a -> b
$ \Maybe a
fres ->
  case Maybe a
fres of
    Maybe a
Nothing -> Word64Map a -> (a -> Word64Map a) -> Maybe a -> Word64Map a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map a
m (Word64Map a -> a -> Word64Map a
forall a b. a -> b -> a
const (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
delete Word64
k Word64Map a
m)) Maybe a
mv
    Just a
v' -> Word64 -> a -> Word64Map a -> Word64Map a
forall a. Word64 -> a -> Word64Map a -> Word64Map a
insert Word64
k a
v' Word64Map a
m
  where mv :: Maybe a
mv = Word64 -> Word64Map a -> Maybe a
forall a. Word64 -> Word64Map a -> Maybe a
lookup Word64
k Word64Map a
m

{--------------------------------------------------------------------
  Union
--------------------------------------------------------------------}
-- | The union of a list of maps.
--
-- > unions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
-- >     == fromList [(3, "b"), (5, "a"), (7, "C")]
-- > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
-- >     == fromList [(3, "B3"), (5, "A3"), (7, "C")]

unions :: Foldable f => f (Word64Map a) -> Word64Map a
unions :: forall (f :: * -> *) a.
Foldable f =>
f (Word64Map a) -> Word64Map a
unions f (Word64Map a)
xs
  = (Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> f (Word64Map a) -> Word64Map a
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' Word64Map a -> Word64Map a -> Word64Map a
forall a. Word64Map a -> Word64Map a -> Word64Map a
union Word64Map a
forall a. Word64Map a
empty f (Word64Map a)
xs

-- | The union of a list of maps, with a combining operation.
--
-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
-- >     == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]

unionsWith :: Foldable f => (a->a->a) -> f (Word64Map a) -> Word64Map a
unionsWith :: forall (f :: * -> *) a.
Foldable f =>
(a -> a -> a) -> f (Word64Map a) -> Word64Map a
unionsWith a -> a -> a
f f (Word64Map a)
ts
  = (Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> f (Word64Map a) -> Word64Map a
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' ((a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
(a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
unionWith a -> a -> a
f) Word64Map a
forall a. Word64Map a
empty f (Word64Map a)
ts

-- | \(O(n+m)\). The (left-biased) union of two maps.
-- It prefers the first map when duplicate keys are encountered,
-- i.e. (@'union' == 'unionWith' 'const'@).
--
-- > union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]

union :: Word64Map a -> Word64Map a -> Word64Map a
union :: forall a. Word64Map a -> Word64Map a -> Word64Map a
union Word64Map a
m1 Word64Map a
m2
  = (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a)
-> Word64Map a
-> Word64Map a
-> Word64Map a
forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64Map a -> Word64Map a -> Word64Map a
forall a b. a -> b -> a
const Word64Map a -> Word64Map a
forall a. a -> a
id Word64Map a -> Word64Map a
forall a. a -> a
id Word64Map a
m1 Word64Map a
m2

-- | \(O(n+m)\). The union with a combining function.
--
-- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]

unionWith :: (a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
unionWith :: forall a.
(a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
unionWith a -> a -> a
f Word64Map a
m1 Word64Map a
m2
  = (Word64 -> a -> a -> a)
-> Word64Map a -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a -> a)
-> Word64Map a -> Word64Map a -> Word64Map a
unionWithKey (\Word64
_ a
x a
y -> a -> a -> a
f a
x a
y) Word64Map a
m1 Word64Map a
m2

-- | \(O(n+m)\). The union with a combining function.
--
-- > let f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
-- > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]

unionWithKey :: (Key -> a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
unionWithKey :: forall a.
(Word64 -> a -> a -> a)
-> Word64Map a -> Word64Map a -> Word64Map a
unionWithKey Word64 -> a -> a -> a
f Word64Map a
m1 Word64Map a
m2
  = (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map a)
-> Word64Map a
-> Word64Map a
-> Word64Map a
forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin (\(Tip Word64
k1 a
x1) (Tip Word64
_k2 a
x2) -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1 (Word64 -> a -> a -> a
f Word64
k1 a
x1 a
x2)) Word64Map a -> Word64Map a
forall a. a -> a
id Word64Map a -> Word64Map a
forall a. a -> a
id Word64Map a
m1 Word64Map a
m2

{--------------------------------------------------------------------
  Difference
--------------------------------------------------------------------}
-- | \(O(n+m)\). Difference between two maps (based on keys).
--
-- > difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"

difference :: Word64Map a -> Word64Map b -> Word64Map a
difference :: forall a b. Word64Map a -> Word64Map b -> Word64Map a
difference Word64Map a
m1 Word64Map b
m2
  = (Word64 -> a -> b -> Maybe a)
-> (Word64Map a -> Word64Map a)
-> (Word64Map b -> Word64Map a)
-> Word64Map a
-> Word64Map b
-> Word64Map a
forall a b c.
(Word64 -> a -> b -> Maybe c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey (\Word64
_ a
_ b
_ -> Maybe a
forall a. Maybe a
Nothing) Word64Map a -> Word64Map a
forall a. a -> a
id (Word64Map a -> Word64Map b -> Word64Map a
forall a b. a -> b -> a
const Word64Map a
forall a. Word64Map a
Nil) Word64Map a
m1 Word64Map b
m2

-- | \(O(n+m)\). Difference with a combining function.
--
-- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
-- > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
-- >     == singleton 3 "b:B"

differenceWith :: (a -> b -> Maybe a) -> Word64Map a -> Word64Map b -> Word64Map a
differenceWith :: forall a b.
(a -> b -> Maybe a) -> Word64Map a -> Word64Map b -> Word64Map a
differenceWith a -> b -> Maybe a
f Word64Map a
m1 Word64Map b
m2
  = (Word64 -> a -> b -> Maybe a)
-> Word64Map a -> Word64Map b -> Word64Map a
forall a b.
(Word64 -> a -> b -> Maybe a)
-> Word64Map a -> Word64Map b -> Word64Map a
differenceWithKey (\Word64
_ a
x b
y -> a -> b -> Maybe a
f a
x b
y) Word64Map a
m1 Word64Map b
m2

-- | \(O(n+m)\). Difference with a combining function. When two equal keys are
-- encountered, the combining function is applied to the key and both values.
-- If it returns 'Nothing', the element is discarded (proper set difference).
-- If it returns (@'Just' y@), the element is updated with a new value @y@.
--
-- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
-- > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
-- >     == singleton 3 "3:b|B"

differenceWithKey :: (Key -> a -> b -> Maybe a) -> Word64Map a -> Word64Map b -> Word64Map a
differenceWithKey :: forall a b.
(Word64 -> a -> b -> Maybe a)
-> Word64Map a -> Word64Map b -> Word64Map a
differenceWithKey Word64 -> a -> b -> Maybe a
f Word64Map a
m1 Word64Map b
m2
  = (Word64 -> a -> b -> Maybe a)
-> (Word64Map a -> Word64Map a)
-> (Word64Map b -> Word64Map a)
-> Word64Map a
-> Word64Map b
-> Word64Map a
forall a b c.
(Word64 -> a -> b -> Maybe c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey Word64 -> a -> b -> Maybe a
f Word64Map a -> Word64Map a
forall a. a -> a
id (Word64Map a -> Word64Map b -> Word64Map a
forall a b. a -> b -> a
const Word64Map a
forall a. Word64Map a
Nil) Word64Map a
m1 Word64Map b
m2


-- TODO(wrengr): re-verify that asymptotic bound
-- | \(O(n+m)\). Remove all the keys in a given set from a map.
--
-- @
-- m \`withoutKeys\` s = 'filterWithKey' (\\k _ -> k ``Word64Set.notMember`` s) m
-- @
--
-- @since 0.5.8
withoutKeys :: Word64Map a -> Word64Set.Word64Set -> Word64Map a
withoutKeys :: forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) t2 :: Word64Set
t2@(Word64Set.Bin Word64
p2 Word64
m2 Word64Set
l2 Word64Set
r2)
    | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = Word64Map a
difference1
    | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = Word64Map a
difference2
    | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2       = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p1 Word64
m1 (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
l1 Word64Set
l2) (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
r1 Word64Set
r2)
    | Bool
otherwise      = Word64Map a
t1
    where
    difference1 :: Word64Map a
difference1
        | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p2 Word64
p1 Word64
m1  = Word64Map a
t1
        | Word64 -> Word64 -> Bool
zero Word64
p2 Word64
m1        = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p1 Word64
m1 (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
l1 Word64Set
t2) Word64Map a
r1
        | Bool
otherwise         = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p1 Word64
m1 Word64Map a
l1 (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
r1 Word64Set
t2)
    difference2 :: Word64Map a
difference2
        | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2  = Word64Map a
t1
        | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2        = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
t1 Word64Set
l2
        | Bool
otherwise         = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
withoutKeys Word64Map a
t1 Word64Set
r2
withoutKeys t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
_ Word64Map a
_) (Word64Set.Tip Word64
p2 Word64
bm2) =
    let minbit :: Word64
minbit = Word64 -> Word64
bitmapOf Word64
p1
        lt_minbit :: Word64
lt_minbit = Word64
minbit Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1
        maxbit :: Word64
maxbit = Word64 -> Word64
bitmapOf (Word64
p1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
m1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
m1 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1)))
        gt_maxbit :: Word64
gt_maxbit = (-Word64
maxbit) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
maxbit
    -- TODO(wrengr): should we manually inline/unroll 'updatePrefix'
    -- and 'withoutBM' here, in order to avoid redundant case analyses?
    in Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
forall a.
Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
updatePrefix Word64
p2 Word64Map a
t1 ((Word64Map a -> Word64Map a) -> Word64Map a)
-> (Word64Map a -> Word64Map a) -> Word64Map a
forall a b. (a -> b) -> a -> b
$ Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
withoutBM (Word64
bm2 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
lt_minbit Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
gt_maxbit)
withoutKeys t1 :: Word64Map a
t1@(Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) Word64Set
Word64Set.Nil = Word64Map a
t1
withoutKeys t1 :: Word64Map a
t1@(Tip Word64
k1 a
_) Word64Set
t2
    | Word64
k1 Word64 -> Word64Set -> Bool
`Word64Set.member` Word64Set
t2 = Word64Map a
forall a. Word64Map a
Nil
    | Bool
otherwise = Word64Map a
t1
withoutKeys Word64Map a
Nil Word64Set
_ = Word64Map a
forall a. Word64Map a
Nil


updatePrefix
    :: Word64SetPrefix -> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
updatePrefix :: forall a.
Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
updatePrefix !Word64
kp t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) Word64Map a -> Word64Map a
f
    | Word64
m Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.suffixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
0 =
        if Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kp then Word64Map a -> Word64Map a
f Word64Map a
t else Word64Map a
t
    | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
kp Word64
p Word64
m = Word64Map a
t
    | Word64 -> Word64 -> Bool
zero Word64
kp Word64
m      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m (Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
forall a.
Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
updatePrefix Word64
kp Word64Map a
l Word64Map a -> Word64Map a
f) Word64Map a
r
    | Bool
otherwise      = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l (Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
forall a.
Word64
-> Word64Map a -> (Word64Map a -> Word64Map a) -> Word64Map a
updatePrefix Word64
kp Word64Map a
r Word64Map a -> Word64Map a
f)
updatePrefix Word64
kp t :: Word64Map a
t@(Tip Word64
kx a
_) Word64Map a -> Word64Map a
f
    | Word64
kx Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kp = Word64Map a -> Word64Map a
f Word64Map a
t
    | Bool
otherwise = Word64Map a
t
updatePrefix Word64
_ Word64Map a
Nil Word64Map a -> Word64Map a
_ = Word64Map a
forall a. Word64Map a
Nil


withoutBM :: Word64SetBitMap -> Word64Map a -> Word64Map a
withoutBM :: forall a. Word64 -> Word64Map a -> Word64Map a
withoutBM Word64
0 Word64Map a
t = Word64Map a
t
withoutBM Word64
bm (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) =
    let leftBits :: Word64
leftBits = Word64 -> Word64
bitmapOf (Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
m) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1
        bmL :: Word64
bmL = Word64
bm Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
leftBits
        bmR :: Word64
bmR = Word64
bm Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
bmL -- = (bm .&. complement leftBits)
    in  Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
withoutBM Word64
bmL Word64Map a
l) (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
withoutBM Word64
bmR Word64Map a
r)
withoutBM Word64
bm t :: Word64Map a
t@(Tip Word64
k a
_)
    -- TODO(wrengr): need we manually inline 'Word64Set.Member' here?
    | Word64
k Word64 -> Word64Set -> Bool
`Word64Set.member` Word64 -> Word64 -> Word64Set
Word64Set.Tip (Word64
k Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask) Word64
bm = Word64Map a
forall a. Word64Map a
Nil
    | Bool
otherwise = Word64Map a
t
withoutBM Word64
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil


{--------------------------------------------------------------------
  Intersection
--------------------------------------------------------------------}
-- | \(O(n+m)\). The (left-biased) intersection of two maps (based on keys).
--
-- > intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"

intersection :: Word64Map a -> Word64Map b -> Word64Map a
intersection :: forall a b. Word64Map a -> Word64Map b -> Word64Map a
intersection Word64Map a
m1 Word64Map b
m2
  = (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a)
-> (Word64Map a -> Word64Map b -> Word64Map a)
-> (Word64Map a -> Word64Map a)
-> (Word64Map b -> Word64Map a)
-> Word64Map a
-> Word64Map b
-> Word64Map a
forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64Map a -> Word64Map b -> Word64Map a
forall a b. a -> b -> a
const (Word64Map a -> Word64Map a -> Word64Map a
forall a b. a -> b -> a
const Word64Map a
forall a. Word64Map a
Nil) (Word64Map a -> Word64Map b -> Word64Map a
forall a b. a -> b -> a
const Word64Map a
forall a. Word64Map a
Nil) Word64Map a
m1 Word64Map b
m2


-- TODO(wrengr): re-verify that asymptotic bound
-- | \(O(n+m)\). The restriction of a map to the keys in a set.
--
-- @
-- m \`restrictKeys\` s = 'filterWithKey' (\\k _ -> k ``Word64Set.member`` s) m
-- @
--
-- @since 0.5.8
restrictKeys :: Word64Map a -> Word64Set.Word64Set -> Word64Map a
restrictKeys :: forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) t2 :: Word64Set
t2@(Word64Set.Bin Word64
p2 Word64
m2 Word64Set
l2 Word64Set
r2)
    | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = Word64Map a
intersection1
    | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = Word64Map a
intersection2
    | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2       = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p1 Word64
m1 (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
l1 Word64Set
l2) (Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
r1 Word64Set
r2)
    | Bool
otherwise      = Word64Map a
forall a. Word64Map a
Nil
    where
    intersection1 :: Word64Map a
intersection1
        | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p2 Word64
p1 Word64
m1  = Word64Map a
forall a. Word64Map a
Nil
        | Word64 -> Word64 -> Bool
zero Word64
p2 Word64
m1        = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
l1 Word64Set
t2
        | Bool
otherwise         = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
r1 Word64Set
t2
    intersection2 :: Word64Map a
intersection2
        | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2  = Word64Map a
forall a. Word64Map a
Nil
        | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2        = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
t1 Word64Set
l2
        | Bool
otherwise         = Word64Map a -> Word64Set -> Word64Map a
forall a. Word64Map a -> Word64Set -> Word64Map a
restrictKeys Word64Map a
t1 Word64Set
r2
restrictKeys t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
_ Word64Map a
_) (Word64Set.Tip Word64
p2 Word64
bm2) =
    let minbit :: Word64
minbit = Word64 -> Word64
bitmapOf Word64
p1
        ge_minbit :: Word64
ge_minbit = Word64 -> Word64
forall a. Bits a => a -> a
complement (Word64
minbit Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1)
        maxbit :: Word64
maxbit = Word64 -> Word64
bitmapOf (Word64
p1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
m1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
m1 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1)))
        le_maxbit :: Word64
le_maxbit = Word64
maxbit Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
maxbit Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1)
    -- TODO(wrengr): should we manually inline/unroll 'lookupPrefix'
    -- and 'restrictBM' here, in order to avoid redundant case analyses?
    in Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
restrictBM (Word64
bm2 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
ge_minbit Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
le_maxbit) (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
lookupPrefix Word64
p2 Word64Map a
t1)
restrictKeys (Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) Word64Set
Word64Set.Nil = Word64Map a
forall a. Word64Map a
Nil
restrictKeys t1 :: Word64Map a
t1@(Tip Word64
k1 a
_) Word64Set
t2
    | Word64
k1 Word64 -> Word64Set -> Bool
`Word64Set.member` Word64Set
t2 = Word64Map a
t1
    | Bool
otherwise = Word64Map a
forall a. Word64Map a
Nil
restrictKeys Word64Map a
Nil Word64Set
_ = Word64Map a
forall a. Word64Map a
Nil


-- | \(O(\min(n,W))\). Restrict to the sub-map with all keys matching
-- a key prefix.
lookupPrefix :: Word64SetPrefix -> Word64Map a -> Word64Map a
lookupPrefix :: forall a. Word64 -> Word64Map a -> Word64Map a
lookupPrefix !Word64
kp t :: Word64Map a
t@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
    | Word64
m Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.suffixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
0 =
        if Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kp then Word64Map a
t else Word64Map a
forall a. Word64Map a
Nil
    | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
kp Word64
p Word64
m = Word64Map a
forall a. Word64Map a
Nil
    | Word64 -> Word64 -> Bool
zero Word64
kp Word64
m      = Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
lookupPrefix Word64
kp Word64Map a
l
    | Bool
otherwise      = Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
lookupPrefix Word64
kp Word64Map a
r
lookupPrefix Word64
kp t :: Word64Map a
t@(Tip Word64
kx a
_)
    | (Word64
kx Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
kp = Word64Map a
t
    | Bool
otherwise = Word64Map a
forall a. Word64Map a
Nil
lookupPrefix Word64
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil


restrictBM :: Word64SetBitMap -> Word64Map a -> Word64Map a
restrictBM :: forall a. Word64 -> Word64Map a -> Word64Map a
restrictBM Word64
0 Word64Map a
_ = Word64Map a
forall a. Word64Map a
Nil
restrictBM Word64
bm (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) =
    let leftBits :: Word64
leftBits = Word64 -> Word64
bitmapOf (Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
m) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1
        bmL :: Word64
bmL = Word64
bm Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
leftBits
        bmR :: Word64
bmR = Word64
bm Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
bmL -- = (bm .&. complement leftBits)
    in  Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
restrictBM Word64
bmL Word64Map a
l) (Word64 -> Word64Map a -> Word64Map a
forall a. Word64 -> Word64Map a -> Word64Map a
restrictBM Word64
bmR Word64Map a
r)
restrictBM Word64
bm t :: Word64Map a
t@(Tip Word64
k a
_)
    -- TODO(wrengr): need we manually inline 'Word64Set.Member' here?
    | Word64
k Word64 -> Word64Set -> Bool
`Word64Set.member` Word64 -> Word64 -> Word64Set
Word64Set.Tip (Word64
k Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask) Word64
bm = Word64Map a
t
    | Bool
otherwise = Word64Map a
forall a. Word64Map a
Nil
restrictBM Word64
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil


-- | \(O(n+m)\). The intersection with a combining function.
--
-- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"

intersectionWith :: (a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
intersectionWith :: forall a b c.
(a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
intersectionWith a -> b -> c
f Word64Map a
m1 Word64Map b
m2
  = (Word64 -> a -> b -> c)
-> Word64Map a -> Word64Map b -> Word64Map c
forall a b c.
(Word64 -> a -> b -> c)
-> Word64Map a -> Word64Map b -> Word64Map c
intersectionWithKey (\Word64
_ a
x b
y -> a -> b -> c
f a
x b
y) Word64Map a
m1 Word64Map b
m2

-- | \(O(n+m)\). The intersection with a combining function.
--
-- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
-- > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"

intersectionWithKey :: (Key -> a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
intersectionWithKey :: forall a b c.
(Word64 -> a -> b -> c)
-> Word64Map a -> Word64Map b -> Word64Map c
intersectionWithKey Word64 -> a -> b -> c
f Word64Map a
m1 Word64Map b
m2
  = (Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin (\(Tip Word64
k1 a
x1) (Tip Word64
_k2 b
x2) -> Word64 -> c -> Word64Map c
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1 (Word64 -> a -> b -> c
f Word64
k1 a
x1 b
x2)) (Word64Map c -> Word64Map a -> Word64Map c
forall a b. a -> b -> a
const Word64Map c
forall a. Word64Map a
Nil) (Word64Map c -> Word64Map b -> Word64Map c
forall a b. a -> b -> a
const Word64Map c
forall a. Word64Map a
Nil) Word64Map a
m1 Word64Map b
m2

{--------------------------------------------------------------------
  MergeWithKey
--------------------------------------------------------------------}

-- | \(O(n+m)\). A high-performance universal combining function. Using
-- 'mergeWithKey', all combining functions can be defined without any loss of
-- efficiency (with exception of 'union', 'difference' and 'intersection',
-- where sharing of some nodes is lost with 'mergeWithKey').
--
-- Please make sure you know what is going on when using 'mergeWithKey',
-- otherwise you can be surprised by unexpected code growth or even
-- corruption of the data structure.
--
-- When 'mergeWithKey' is given three arguments, it is inlined to the call
-- site. You should therefore use 'mergeWithKey' only to define your custom
-- combining functions. For example, you could define 'unionWithKey',
-- 'differenceWithKey' and 'intersectionWithKey' as
--
-- > myUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) id id m1 m2
-- > myDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
-- > myIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) (const empty) (const empty) m1 m2
--
-- When calling @'mergeWithKey' combine only1 only2@, a function combining two
-- 'Word64Map's is created, such that
--
-- * if a key is present in both maps, it is passed with both corresponding
--   values to the @combine@ function. Depending on the result, the key is either
--   present in the result with specified value, or is left out;
--
-- * a nonempty subtree present only in the first map is passed to @only1@ and
--   the output is added to the result;
--
-- * a nonempty subtree present only in the second map is passed to @only2@ and
--   the output is added to the result.
--
-- The @only1@ and @only2@ methods /must return a map with a subset (possibly empty) of the keys of the given map/.
-- The values can be modified arbitrarily. Most common variants of @only1@ and
-- @only2@ are 'id' and @'const' 'empty'@, but for example @'map' f@ or
-- @'filterWithKey' f@ could be used for any @f@.

mergeWithKey :: (Key -> a -> b -> Maybe c) -> (Word64Map a -> Word64Map c) -> (Word64Map b -> Word64Map c)
             -> Word64Map a -> Word64Map b -> Word64Map c
mergeWithKey :: forall a b c.
(Word64 -> a -> b -> Maybe c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey Word64 -> a -> b -> Maybe c
f Word64Map a -> Word64Map c
g1 Word64Map b -> Word64Map c
g2 = (Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64Map a -> Word64Map b -> Word64Map c
combine Word64Map a -> Word64Map c
g1 Word64Map b -> Word64Map c
g2
  where -- We use the lambda form to avoid non-exhaustive pattern matches warning.
        combine :: Word64Map a -> Word64Map b -> Word64Map c
combine = \(Tip Word64
k1 a
x1) (Tip Word64
_k2 b
x2) ->
          case Word64 -> a -> b -> Maybe c
f Word64
k1 a
x1 b
x2 of
            Maybe c
Nothing -> Word64Map c
forall a. Word64Map a
Nil
            Just c
x -> Word64 -> c -> Word64Map c
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1 c
x
        {-# INLINE combine #-}
{-# INLINE mergeWithKey #-}

-- Slightly more general version of mergeWithKey. It differs in the following:
--
-- * the combining function operates on maps instead of keys and values. The
--   reason is to enable sharing in union, difference and intersection.
--
-- * mergeWithKey' is given an equivalent of bin. The reason is that in union*,
--   Bin constructor can be used, because we know both subtrees are nonempty.

mergeWithKey' :: (Prefix -> Mask -> Word64Map c -> Word64Map c -> Word64Map c)
              -> (Word64Map a -> Word64Map b -> Word64Map c) -> (Word64Map a -> Word64Map c) -> (Word64Map b -> Word64Map c)
              -> Word64Map a -> Word64Map b -> Word64Map c
mergeWithKey' :: forall c a b.
(Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c)
-> (Word64Map a -> Word64Map b -> Word64Map c)
-> (Word64Map a -> Word64Map c)
-> (Word64Map b -> Word64Map c)
-> Word64Map a
-> Word64Map b
-> Word64Map c
mergeWithKey' Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64Map a -> Word64Map b -> Word64Map c
f Word64Map a -> Word64Map c
g1 Word64Map b -> Word64Map c
g2 = Word64Map a -> Word64Map b -> Word64Map c
go
  where
    go :: Word64Map a -> Word64Map b -> Word64Map c
go t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) t2 :: Word64Map b
t2@(Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
      | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = Word64Map c
merge1
      | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = Word64Map c
merge2
      | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2       = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p1 Word64
m1 (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
l1 Word64Map b
l2) (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
r1 Word64Map b
r2)
      | Bool
otherwise      = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
p1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
p2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
      where
        merge1 :: Word64Map c
merge1 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p2 Word64
p1 Word64
m1  = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
p1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
p2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
               | Word64 -> Word64 -> Bool
zero Word64
p2 Word64
m1        = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p1 Word64
m1 (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
l1 Word64Map b
t2) (Word64Map a -> Word64Map c
g1 Word64Map a
r1)
               | Bool
otherwise         = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p1 Word64
m1 (Word64Map a -> Word64Map c
g1 Word64Map a
l1) (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
r1 Word64Map b
t2)
        merge2 :: Word64Map c
merge2 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2  = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
p1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
p2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
               | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2        = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p2 Word64
m2 (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
t1 Word64Map b
l2) (Word64Map b -> Word64Map c
g2 Word64Map b
r2)
               | Bool
otherwise         = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p2 Word64
m2 (Word64Map b -> Word64Map c
g2 Word64Map b
l2) (Word64Map a -> Word64Map b -> Word64Map c
go Word64Map a
t1 Word64Map b
r2)

    go t1' :: Word64Map a
t1'@(Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) t2' :: Word64Map b
t2'@(Tip Word64
k2' b
_) = Word64Map b -> Word64 -> Word64Map a -> Word64Map c
merge0 Word64Map b
t2' Word64
k2' Word64Map a
t1'
      where
        merge0 :: Word64Map b -> Word64 -> Word64Map a -> Word64Map c
merge0 Word64Map b
t2 Word64
k2 t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1)
          | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k2 Word64
p1 Word64
m1 = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
p1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
k2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
          | Word64 -> Word64 -> Bool
zero Word64
k2 Word64
m1 = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p1 Word64
m1 (Word64Map b -> Word64 -> Word64Map a -> Word64Map c
merge0 Word64Map b
t2 Word64
k2 Word64Map a
l1) (Word64Map a -> Word64Map c
g1 Word64Map a
r1)
          | Bool
otherwise  = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p1 Word64
m1 (Word64Map a -> Word64Map c
g1 Word64Map a
l1) (Word64Map b -> Word64 -> Word64Map a -> Word64Map c
merge0 Word64Map b
t2 Word64
k2 Word64Map a
r1)
        merge0 Word64Map b
t2 Word64
k2 t1 :: Word64Map a
t1@(Tip Word64
k1 a
_)
          | Word64
k1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
k2 = Word64Map a -> Word64Map b -> Word64Map c
f Word64Map a
t1 Word64Map b
t2
          | Bool
otherwise = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
k1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
k2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
        merge0 Word64Map b
t2 Word64
_  Word64Map a
Nil = Word64Map b -> Word64Map c
g2 Word64Map b
t2

    go t1 :: Word64Map a
t1@(Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) Word64Map b
Nil = Word64Map a -> Word64Map c
g1 Word64Map a
t1

    go t1' :: Word64Map a
t1'@(Tip Word64
k1' a
_) Word64Map b
t2' = Word64Map a -> Word64 -> Word64Map b -> Word64Map c
merge0 Word64Map a
t1' Word64
k1' Word64Map b
t2'
      where
        merge0 :: Word64Map a -> Word64 -> Word64Map b -> Word64Map c
merge0 Word64Map a
t1 Word64
k1 t2 :: Word64Map b
t2@(Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
          | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k1 Word64
p2 Word64
m2 = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
k1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
p2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
          | Word64 -> Word64 -> Bool
zero Word64
k1 Word64
m2 = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p2 Word64
m2 (Word64Map a -> Word64 -> Word64Map b -> Word64Map c
merge0 Word64Map a
t1 Word64
k1 Word64Map b
l2) (Word64Map b -> Word64Map c
g2 Word64Map b
r2)
          | Bool
otherwise  = Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
bin' Word64
p2 Word64
m2 (Word64Map b -> Word64Map c
g2 Word64Map b
l2) (Word64Map a -> Word64 -> Word64Map b -> Word64Map c
merge0 Word64Map a
t1 Word64
k1 Word64Map b
r2)
        merge0 Word64Map a
t1 Word64
k1 t2 :: Word64Map b
t2@(Tip Word64
k2 b
_)
          | Word64
k1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
k2 = Word64Map a -> Word64Map b -> Word64Map c
f Word64Map a
t1 Word64Map b
t2
          | Bool
otherwise = Word64 -> Word64Map c -> Word64 -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
k1 (Word64Map a -> Word64Map c
g1 Word64Map a
t1) Word64
k2 (Word64Map b -> Word64Map c
g2 Word64Map b
t2)
        merge0 Word64Map a
t1 Word64
_  Word64Map b
Nil = Word64Map a -> Word64Map c
g1 Word64Map a
t1

    go Word64Map a
Nil Word64Map b
t2 = Word64Map b -> Word64Map c
g2 Word64Map b
t2

    maybe_link :: Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
maybe_link Word64
_ Word64Map a
Nil Word64
_ Word64Map a
t2 = Word64Map a
t2
    maybe_link Word64
_ Word64Map a
t1 Word64
_ Word64Map a
Nil = Word64Map a
t1
    maybe_link Word64
p1 Word64Map a
t1 Word64
p2 Word64Map a
t2 = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
p1 Word64Map a
t1 Word64
p2 Word64Map a
t2
    {-# INLINE maybe_link #-}
{-# INLINE mergeWithKey' #-}


{--------------------------------------------------------------------
  mergeA
--------------------------------------------------------------------}

-- | A tactic for dealing with keys present in one map but not the
-- other in 'merge' or 'mergeA'.
--
-- A tactic of type @WhenMissing f k x z@ is an abstract representation
-- of a function of type @Key -> x -> f (Maybe z)@.
--
-- @since 0.5.9

data WhenMissing f x y = WhenMissing
  { forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree :: Word64Map x -> f (Word64Map y)
  , forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey :: Key -> x -> f (Maybe y)}

-- | @since 0.5.9
instance (Applicative f, Monad f) => Functor (WhenMissing f x) where
  fmap :: forall a b. (a -> b) -> WhenMissing f x a -> WhenMissing f x b
fmap = (a -> b) -> WhenMissing f x a -> WhenMissing f x b
forall (f :: * -> *) a b x.
(Applicative f, Monad f) =>
(a -> b) -> WhenMissing f x a -> WhenMissing f x b
mapWhenMissing
  {-# INLINE fmap #-}


-- | @since 0.5.9
instance (Applicative f, Monad f) => Category.Category (WhenMissing f)
  where
    id :: forall a. WhenMissing f a a
id = WhenMissing f a a
forall (f :: * -> *) x. Applicative f => WhenMissing f x x
preserveMissing
    WhenMissing f b c
f . :: forall b c a.
WhenMissing f b c -> WhenMissing f a b -> WhenMissing f a c
. WhenMissing f a b
g =
      (Word64 -> a -> f (Maybe c)) -> WhenMissing f a c
forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing ((Word64 -> a -> f (Maybe c)) -> WhenMissing f a c)
-> (Word64 -> a -> f (Maybe c)) -> WhenMissing f a c
forall a b. (a -> b) -> a -> b
$ \ Word64
k a
x -> do
        y <- WhenMissing f a b -> Word64 -> a -> f (Maybe b)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f a b
g Word64
k a
x
        case y of
          Maybe b
Nothing -> Maybe c -> f (Maybe c)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe c
forall a. Maybe a
Nothing
          Just b
q  -> WhenMissing f b c -> Word64 -> b -> f (Maybe c)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f b c
f Word64
k b
q
    {-# INLINE id #-}
    {-# INLINE (.) #-}


-- | Equivalent to @ReaderT k (ReaderT x (MaybeT f))@.
--
-- @since 0.5.9
instance (Applicative f, Monad f) => Applicative (WhenMissing f x) where
  pure :: forall a. a -> WhenMissing f x a
pure a
x = (Word64 -> x -> a) -> WhenMissing f x a
forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> y) -> WhenMissing f x y
mapMissing (\ Word64
_ x
_ -> a
x)
  WhenMissing f x (a -> b)
f <*> :: forall a b.
WhenMissing f x (a -> b) -> WhenMissing f x a -> WhenMissing f x b
<*> WhenMissing f x a
g =
    (Word64 -> x -> f (Maybe b)) -> WhenMissing f x b
forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing ((Word64 -> x -> f (Maybe b)) -> WhenMissing f x b)
-> (Word64 -> x -> f (Maybe b)) -> WhenMissing f x b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x -> do
      res1 <- WhenMissing f x (a -> b) -> Word64 -> x -> f (Maybe (a -> b))
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f x (a -> b)
f Word64
k x
x
      case res1 of
        Maybe (a -> b)
Nothing -> Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
        Just a -> b
r  -> (Maybe b -> f (Maybe b)
forall a. a -> f a
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 a b. (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 x a -> Word64 -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f x a
g Word64
k x
x
  {-# INLINE pure #-}
  {-# INLINE (<*>) #-}


-- | Equivalent to @ReaderT k (ReaderT x (MaybeT f))@.
--
-- @since 0.5.9
instance (Applicative f, Monad f) => Monad (WhenMissing f x) where
  WhenMissing f x a
m >>= :: forall a b.
WhenMissing f x a -> (a -> WhenMissing f x b) -> WhenMissing f x b
>>= a -> WhenMissing f x b
f =
    (Word64 -> x -> f (Maybe b)) -> WhenMissing f x b
forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing ((Word64 -> x -> f (Maybe b)) -> WhenMissing f x b)
-> (Word64 -> x -> f (Maybe b)) -> WhenMissing f x b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x -> do
      res1 <- WhenMissing f x a -> Word64 -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f x a
m Word64
k x
x
      case res1 of
        Maybe a
Nothing -> Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
        Just a
r  -> WhenMissing f x b -> Word64 -> x -> f (Maybe b)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey (a -> WhenMissing f x b
f a
r) Word64
k x
x
  {-# INLINE (>>=) #-}


-- | Map covariantly over a @'WhenMissing' f x@.
--
-- @since 0.5.9
mapWhenMissing
  :: (Applicative f, Monad f)
  => (a -> b)
  -> WhenMissing f x a
  -> WhenMissing f x b
mapWhenMissing :: forall (f :: * -> *) a b x.
(Applicative f, Monad f) =>
(a -> b) -> WhenMissing f x a -> WhenMissing f x b
mapWhenMissing a -> b
f WhenMissing f x a
t = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map b)
missingSubtree = \Word64Map x
m -> WhenMissing f x a -> Word64Map x -> f (Word64Map a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree WhenMissing f x a
t Word64Map x
m f (Word64Map a)
-> (Word64Map a -> f (Word64Map b)) -> f (Word64Map b)
forall a b. f a -> (a -> f b) -> f b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64Map a
m' -> Word64Map b -> f (Word64Map b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64Map b -> f (Word64Map b)) -> Word64Map b -> f (Word64Map b)
forall a b. (a -> b) -> a -> b
$! (a -> b) -> Word64Map a -> Word64Map b
forall a b. (a -> b) -> Word64Map a -> Word64Map b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Word64Map a
m'
  , missingKey :: Word64 -> x -> f (Maybe b)
missingKey     = \Word64
k x
x -> WhenMissing f x a -> Word64 -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f x a
t Word64
k x
x f (Maybe a) -> (Maybe a -> f (Maybe b)) -> f (Maybe b)
forall a b. f a -> (a -> f b) -> f b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe a
q -> (Maybe b -> f (Maybe b)
forall a. a -> f a
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 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 #-}


-- | Map covariantly over a @'WhenMissing' f x@, using only a
-- 'Functor f' constraint.
mapGentlyWhenMissing
  :: Functor f
  => (a -> b)
  -> WhenMissing f x a
  -> WhenMissing f x b
mapGentlyWhenMissing :: forall (f :: * -> *) a b x.
Functor f =>
(a -> b) -> WhenMissing f x a -> WhenMissing f x b
mapGentlyWhenMissing a -> b
f WhenMissing f x a
t = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map b)
missingSubtree = \Word64Map x
m -> (a -> b) -> Word64Map a -> Word64Map b
forall a b. (a -> b) -> Word64Map a -> Word64Map b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Word64Map a -> Word64Map b) -> f (Word64Map a) -> f (Word64Map b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMissing f x a -> Word64Map x -> f (Word64Map a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree WhenMissing f x a
t Word64Map x
m
  , missingKey :: Word64 -> x -> f (Maybe b)
missingKey     = \Word64
k x
x -> (a -> b) -> Maybe a -> Maybe b
forall 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 -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMissing f x a -> Word64 -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f x a
t Word64
k x
x }
{-# INLINE mapGentlyWhenMissing #-}


-- | Map covariantly over a @'WhenMatched' f k x@, using only a
-- 'Functor f' constraint.
mapGentlyWhenMatched
  :: Functor f
  => (a -> b)
  -> WhenMatched f x y a
  -> WhenMatched f x y b
mapGentlyWhenMatched :: forall (f :: * -> *) a b x y.
Functor f =>
(a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
mapGentlyWhenMatched a -> b
f WhenMatched f x y a
t =
  (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x y
y -> (a -> b) -> Maybe a -> Maybe b
forall 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 -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMatched f x y a -> Word64 -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
t Word64
k x
x y
y
{-# INLINE mapGentlyWhenMatched #-}


-- | Map contravariantly over a @'WhenMissing' f _ x@.
--
-- @since 0.5.9
lmapWhenMissing :: (b -> a) -> WhenMissing f a x -> WhenMissing f b x
lmapWhenMissing :: forall b a (f :: * -> *) x.
(b -> a) -> WhenMissing f a x -> WhenMissing f b x
lmapWhenMissing b -> a
f WhenMissing f a x
t = WhenMissing
  { missingSubtree :: Word64Map b -> f (Word64Map x)
missingSubtree = \Word64Map b
m -> WhenMissing f a x -> Word64Map a -> f (Word64Map x)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree WhenMissing f a x
t ((b -> a) -> Word64Map b -> Word64Map a
forall a b. (a -> b) -> Word64Map a -> Word64Map b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> a
f Word64Map b
m)
  , missingKey :: Word64 -> b -> f (Maybe x)
missingKey     = \Word64
k b
x -> WhenMissing f a x -> Word64 -> a -> f (Maybe x)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey WhenMissing f a x
t Word64
k (b -> a
f b
x) }
{-# INLINE lmapWhenMissing #-}


-- | Map contravariantly over a @'WhenMatched' f _ y z@.
--
-- @since 0.5.9
contramapFirstWhenMatched
  :: (b -> a)
  -> WhenMatched f a y z
  -> WhenMatched f b y z
contramapFirstWhenMatched :: forall b a (f :: * -> *) y z.
(b -> a) -> WhenMatched f a y z -> WhenMatched f b y z
contramapFirstWhenMatched b -> a
f WhenMatched f a y z
t =
  (Word64 -> b -> y -> f (Maybe z)) -> WhenMatched f b y z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> b -> y -> f (Maybe z)) -> WhenMatched f b y z)
-> (Word64 -> b -> y -> f (Maybe z)) -> WhenMatched f b y z
forall a b. (a -> b) -> a -> b
$ \Word64
k b
x y
y -> WhenMatched f a y z -> Word64 -> a -> y -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f a y z
t Word64
k (b -> a
f b
x) y
y
{-# INLINE contramapFirstWhenMatched #-}


-- | Map contravariantly over a @'WhenMatched' f x _ z@.
--
-- @since 0.5.9
contramapSecondWhenMatched
  :: (b -> a)
  -> WhenMatched f x a z
  -> WhenMatched f x b z
contramapSecondWhenMatched :: forall b a (f :: * -> *) x z.
(b -> a) -> WhenMatched f x a z -> WhenMatched f x b z
contramapSecondWhenMatched b -> a
f WhenMatched f x a z
t =
  (Word64 -> x -> b -> f (Maybe z)) -> WhenMatched f x b z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> b -> f (Maybe z)) -> WhenMatched f x b z)
-> (Word64 -> x -> b -> f (Maybe z)) -> WhenMatched f x b z
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x b
y -> WhenMatched f x a z -> Word64 -> x -> a -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x a z
t Word64
k x
x (b -> a
f b
y)
{-# INLINE contramapSecondWhenMatched #-}


-- | A tactic for dealing with keys present in one map but not the
-- other in 'merge'.
--
-- A tactic of type @SimpleWhenMissing x z@ is an abstract
-- representation of a function of type @Key -> x -> Maybe z@.
--
-- @since 0.5.9
type SimpleWhenMissing = WhenMissing Identity


-- | A tactic for dealing with keys present in both maps in 'merge'
-- or 'mergeA'.
--
-- A tactic of type @WhenMatched f x y z@ is an abstract representation
-- of a function of type @Key -> x -> y -> f (Maybe z)@.
--
-- @since 0.5.9
newtype WhenMatched f x y z = WhenMatched
  { forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
matchedKey :: Key -> x -> y -> f (Maybe z) }


-- | Along with zipWithMaybeAMatched, witnesses the isomorphism
-- between @WhenMatched f x y z@ and @Key -> x -> y -> f (Maybe z)@.
--
-- @since 0.5.9
runWhenMatched :: WhenMatched f x y z -> Key -> x -> y -> f (Maybe z)
runWhenMatched :: forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched = WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
matchedKey
{-# INLINE runWhenMatched #-}


-- | Along with traverseMaybeMissing, witnesses the isomorphism
-- between @WhenMissing f x y@ and @Key -> x -> f (Maybe y)@.
--
-- @since 0.5.9
runWhenMissing :: WhenMissing f x y -> Key-> x -> f (Maybe y)
runWhenMissing :: forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
runWhenMissing = WhenMissing f x y -> Word64 -> x -> f (Maybe y)
forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey
{-# INLINE runWhenMissing #-}


-- | @since 0.5.9
instance Functor f => Functor (WhenMatched f x y) where
  fmap :: forall a b. (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
fmap = (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
forall (f :: * -> *) a b x y.
Functor f =>
(a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
mapWhenMatched
  {-# INLINE fmap #-}


-- | @since 0.5.9
instance (Monad f, Applicative f) => Category.Category (WhenMatched f x)
  where
    id :: forall a. WhenMatched f x a a
id = (Word64 -> x -> a -> a) -> WhenMatched f x a a
forall (f :: * -> *) x y z.
Applicative f =>
(Word64 -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched (\Word64
_ x
_ a
y -> a
y)
    WhenMatched f x b c
f . :: forall b c a.
WhenMatched f x b c -> WhenMatched f x a b -> WhenMatched f x a c
. WhenMatched f x a b
g =
      (Word64 -> x -> a -> f (Maybe c)) -> WhenMatched f x a c
forall x y (f :: * -> *) z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Word64 -> x -> a -> f (Maybe c)) -> WhenMatched f x a c)
-> (Word64 -> x -> a -> f (Maybe c)) -> WhenMatched f x a c
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x a
y -> do
        res <- WhenMatched f x a b -> Word64 -> x -> a -> f (Maybe b)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x a b
g Word64
k x
x a
y
        case res of
          Maybe b
Nothing -> Maybe c -> f (Maybe c)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe c
forall a. Maybe a
Nothing
          Just b
r  -> WhenMatched f x b c -> Word64 -> x -> b -> f (Maybe c)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x b c
f Word64
k x
x b
r
    {-# INLINE id #-}
    {-# INLINE (.) #-}


-- | Equivalent to @ReaderT Key (ReaderT x (ReaderT y (MaybeT f)))@
--
-- @since 0.5.9
instance (Monad f, Applicative f) => Applicative (WhenMatched f x y) where
  pure :: forall a. a -> WhenMatched f x y a
pure a
x = (Word64 -> x -> y -> a) -> WhenMatched f x y a
forall (f :: * -> *) x y z.
Applicative f =>
(Word64 -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched (\Word64
_ x
_ y
_ -> a
x)
  WhenMatched f x y (a -> b)
fs <*> :: forall a b.
WhenMatched f x y (a -> b)
-> WhenMatched f x y a -> WhenMatched f x y b
<*> WhenMatched f x y a
xs =
    (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x y
y -> do
      res <- WhenMatched f x y (a -> b)
-> Word64 -> x -> y -> f (Maybe (a -> b))
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y (a -> b)
fs Word64
k x
x y
y
      case res of
        Maybe (a -> b)
Nothing -> Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
        Just a -> b
r  -> (Maybe b -> f (Maybe b)
forall a. a -> f a
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 a b. (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 x y a -> Word64 -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
xs Word64
k x
x y
y
  {-# INLINE pure #-}
  {-# INLINE (<*>) #-}


-- | Equivalent to @ReaderT Key (ReaderT x (ReaderT y (MaybeT f)))@
--
-- @since 0.5.9
instance (Monad f, Applicative f) => Monad (WhenMatched f x y) where
  WhenMatched f x y a
m >>= :: forall a b.
WhenMatched f x y a
-> (a -> WhenMatched f x y b) -> WhenMatched f x y b
>>= a -> WhenMatched f x y b
f =
    (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x y
y -> do
      res <- WhenMatched f x y a -> Word64 -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
m Word64
k x
x y
y
      case res of
        Maybe a
Nothing -> Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
        Just a
r  -> WhenMatched f x y b -> Word64 -> x -> y -> f (Maybe b)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
runWhenMatched (a -> WhenMatched f x y b
f a
r) Word64
k x
x y
y
  {-# INLINE (>>=) #-}


-- | Map covariantly over a @'WhenMatched' f x y@.
--
-- @since 0.5.9
mapWhenMatched
  :: Functor f
  => (a -> b)
  -> WhenMatched f x y a
  -> WhenMatched f x y b
mapWhenMatched :: forall (f :: * -> *) a b x y.
Functor f =>
(a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
mapWhenMatched a -> b
f (WhenMatched Word64 -> x -> y -> f (Maybe a)
g) =
  (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Word64 -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Word64
k x
x y
y -> (Maybe a -> Maybe b) -> f (Maybe a) -> f (Maybe b)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Maybe a -> Maybe b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (Word64 -> x -> y -> f (Maybe a)
g Word64
k x
x y
y)
{-# INLINE mapWhenMatched #-}


-- | A tactic for dealing with keys present in both maps in 'merge'.
--
-- A tactic of type @SimpleWhenMatched x y z@ is an abstract
-- representation of a function of type @Key -> x -> y -> Maybe z@.
--
-- @since 0.5.9
type SimpleWhenMatched = WhenMatched Identity


-- | When a key is found in both maps, apply a function to the key
-- and values and use the result in the merged map.
--
-- > zipWithMatched
-- >   :: (Key -> x -> y -> z)
-- >   -> SimpleWhenMatched x y z
--
-- @since 0.5.9
zipWithMatched
  :: Applicative f
  => (Key -> x -> y -> z)
  -> WhenMatched f x y z
zipWithMatched :: forall (f :: * -> *) x y z.
Applicative f =>
(Word64 -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched Word64 -> x -> y -> z
f = (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Word64
k x
x y
y -> Maybe z -> f (Maybe z)
forall a. a -> f a
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
$ Word64 -> x -> y -> z
f Word64
k x
x y
y
{-# INLINE zipWithMatched #-}


-- | When a key is found in both maps, apply a function to the key
-- and values to produce an action and use its result in the merged
-- map.
--
-- @since 0.5.9
zipWithAMatched
  :: Applicative f
  => (Key -> x -> y -> f z)
  -> WhenMatched f x y z
zipWithAMatched :: forall (f :: * -> *) x y z.
Applicative f =>
(Word64 -> x -> y -> f z) -> WhenMatched f x y z
zipWithAMatched Word64 -> x -> y -> f z
f = (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Word64
k x
x 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
<$> Word64 -> x -> y -> f z
f Word64
k x
x y
y
{-# INLINE zipWithAMatched #-}


-- | When a key is found in both maps, apply a function to the key
-- and values and maybe use the result in the merged map.
--
-- > zipWithMaybeMatched
-- >   :: (Key -> x -> y -> Maybe z)
-- >   -> SimpleWhenMatched x y z
--
-- @since 0.5.9
zipWithMaybeMatched
  :: Applicative f
  => (Key -> x -> y -> Maybe z)
  -> WhenMatched f x y z
zipWithMaybeMatched :: forall (f :: * -> *) x y z.
Applicative f =>
(Word64 -> x -> y -> Maybe z) -> WhenMatched f x y z
zipWithMaybeMatched Word64 -> x -> y -> Maybe z
f = (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Word64
k x
x y
y -> Maybe z -> f (Maybe z)
forall a. a -> f a
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
$ Word64 -> x -> y -> Maybe z
f Word64
k x
x y
y
{-# INLINE zipWithMaybeMatched #-}


-- | When a key is found in both maps, apply a function to the key
-- and values, perform the resulting action, and maybe use the
-- result in the merged map.
--
-- This is the fundamental 'WhenMatched' tactic.
--
-- @since 0.5.9
zipWithMaybeAMatched
  :: (Key -> x -> y -> f (Maybe z))
  -> WhenMatched f x y z
zipWithMaybeAMatched :: forall x y (f :: * -> *) z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched Word64 -> x -> y -> f (Maybe z)
f = (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Word64 -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Word64
k x
x y
y -> Word64 -> x -> y -> f (Maybe z)
f Word64
k x
x y
y
{-# INLINE zipWithMaybeAMatched #-}


-- | Drop all the entries whose keys are missing from the other
-- map.
--
-- > dropMissing :: SimpleWhenMissing x y
--
-- prop> dropMissing = mapMaybeMissing (\_ _ -> Nothing)
--
-- but @dropMissing@ is much faster.
--
-- @since 0.5.9
dropMissing :: Applicative f => WhenMissing f x y
dropMissing :: forall (f :: * -> *) x y. Applicative f => WhenMissing f x y
dropMissing = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map y)
missingSubtree = f (Word64Map y) -> Word64Map x -> f (Word64Map y)
forall a b. a -> b -> a
const (Word64Map y -> f (Word64Map y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64Map y
forall a. Word64Map a
Nil)
  , missingKey :: Word64 -> x -> f (Maybe y)
missingKey     = \Word64
_ x
_ -> Maybe y -> f (Maybe y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe y
forall a. Maybe a
Nothing }
{-# INLINE dropMissing #-}


-- | Preserve, unchanged, the entries whose keys are missing from
-- the other map.
--
-- > preserveMissing :: SimpleWhenMissing x x
--
-- prop> preserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -> Just x)
--
-- but @preserveMissing@ is much faster.
--
-- @since 0.5.9
preserveMissing :: Applicative f => WhenMissing f x x
preserveMissing :: forall (f :: * -> *) x. Applicative f => WhenMissing f x x
preserveMissing = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map x)
missingSubtree = Word64Map x -> f (Word64Map x)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  , missingKey :: Word64 -> x -> f (Maybe x)
missingKey     = \Word64
_ x
v -> Maybe x -> f (Maybe x)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> Maybe x
forall a. a -> Maybe a
Just x
v) }
{-# INLINE preserveMissing #-}


-- | Map over the entries whose keys are missing from the other map.
--
-- > mapMissing :: (k -> x -> y) -> SimpleWhenMissing x y
--
-- prop> mapMissing f = mapMaybeMissing (\k x -> Just $ f k x)
--
-- but @mapMissing@ is somewhat faster.
--
-- @since 0.5.9
mapMissing :: Applicative f => (Key -> x -> y) -> WhenMissing f x y
mapMissing :: forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> y) -> WhenMissing f x y
mapMissing Word64 -> x -> y
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map y)
missingSubtree = \Word64Map x
m -> Word64Map y -> f (Word64Map y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64Map y -> f (Word64Map y)) -> Word64Map y -> f (Word64Map y)
forall a b. (a -> b) -> a -> b
$! (Word64 -> x -> y) -> Word64Map x -> Word64Map y
forall a b. (Word64 -> a -> b) -> Word64Map a -> Word64Map b
mapWithKey Word64 -> x -> y
f Word64Map x
m
  , missingKey :: Word64 -> x -> f (Maybe y)
missingKey     = \Word64
k x
x -> Maybe y -> f (Maybe y)
forall a. a -> f a
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 (Word64 -> x -> y
f Word64
k x
x) }
{-# INLINE mapMissing #-}


-- | Map over the entries whose keys are missing from the other
-- map, optionally removing some. This is the most powerful
-- 'SimpleWhenMissing' tactic, but others are usually more efficient.
--
-- > mapMaybeMissing :: (Key -> x -> Maybe y) -> SimpleWhenMissing x y
--
-- prop> mapMaybeMissing f = traverseMaybeMissing (\k x -> pure (f k x))
--
-- but @mapMaybeMissing@ uses fewer unnecessary 'Applicative'
-- operations.
--
-- @since 0.5.9
mapMaybeMissing
  :: Applicative f => (Key -> x -> Maybe y) -> WhenMissing f x y
mapMaybeMissing :: forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> Maybe y) -> WhenMissing f x y
mapMaybeMissing Word64 -> x -> Maybe y
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map y)
missingSubtree = \Word64Map x
m -> Word64Map y -> f (Word64Map y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64Map y -> f (Word64Map y)) -> Word64Map y -> f (Word64Map y)
forall a b. (a -> b) -> a -> b
$! (Word64 -> x -> Maybe y) -> Word64Map x -> Word64Map y
forall a b. (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybeWithKey Word64 -> x -> Maybe y
f Word64Map x
m
  , missingKey :: Word64 -> x -> f (Maybe y)
missingKey     = \Word64
k x
x -> Maybe y -> f (Maybe y)
forall a. a -> f a
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
$! Word64 -> x -> Maybe y
f Word64
k x
x }
{-# INLINE mapMaybeMissing #-}


-- | Filter the entries whose keys are missing from the other map.
--
-- > filterMissing :: (k -> x -> Bool) -> SimpleWhenMissing x x
--
-- prop> filterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -> guard (f k x) *> Just x
--
-- but this should be a little faster.
--
-- @since 0.5.9
filterMissing
  :: Applicative f => (Key -> x -> Bool) -> WhenMissing f x x
filterMissing :: forall (f :: * -> *) x.
Applicative f =>
(Word64 -> x -> Bool) -> WhenMissing f x x
filterMissing Word64 -> x -> Bool
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map x)
missingSubtree = \Word64Map x
m -> Word64Map x -> f (Word64Map x)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64Map x -> f (Word64Map x)) -> Word64Map x -> f (Word64Map x)
forall a b. (a -> b) -> a -> b
$! (Word64 -> x -> Bool) -> Word64Map x -> Word64Map x
forall a. (Word64 -> a -> Bool) -> Word64Map a -> Word64Map a
filterWithKey Word64 -> x -> Bool
f Word64Map x
m
  , missingKey :: Word64 -> x -> f (Maybe x)
missingKey     = \Word64
k x
x -> Maybe x -> f (Maybe x)
forall a. a -> f a
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 Word64 -> x -> Bool
f Word64
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 #-}


-- | Filter the entries whose keys are missing from the other map
-- using some 'Applicative' action.
--
-- > filterAMissing f = Merge.Lazy.traverseMaybeMissing $
-- >   \k x -> (\b -> guard b *> Just x) <$> f k x
--
-- but this should be a little faster.
--
-- @since 0.5.9
filterAMissing
  :: Applicative f => (Key -> x -> f Bool) -> WhenMissing f x x
filterAMissing :: forall (f :: * -> *) x.
Applicative f =>
(Word64 -> x -> f Bool) -> WhenMissing f x x
filterAMissing Word64 -> x -> f Bool
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map x)
missingSubtree = \Word64Map x
m -> (Word64 -> x -> f Bool) -> Word64Map x -> f (Word64Map x)
forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> x -> f Bool
f Word64Map x
m
  , missingKey :: Word64 -> x -> f (Maybe x)
missingKey     = \Word64
k 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
<$> Word64 -> x -> f Bool
f Word64
k x
x }
{-# INLINE filterAMissing #-}


-- | \(O(n)\). Filter keys and values using an 'Applicative' predicate.
filterWithKeyA
  :: Applicative f => (Key -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA :: forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> a -> f Bool
_ Word64Map a
Nil           = Word64Map a -> f (Word64Map a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64Map a
forall a. Word64Map a
Nil
filterWithKeyA Word64 -> a -> f Bool
f t :: Word64Map a
t@(Tip Word64
k a
x)   = (\Bool
b -> if Bool
b then Word64Map a
t else Word64Map a
forall a. Word64Map a
Nil) (Bool -> Word64Map a) -> f Bool -> f (Word64Map a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> a -> f Bool
f Word64
k a
x
filterWithKeyA Word64 -> a -> f Bool
f (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = (Word64Map a -> Word64Map a -> Word64Map a)
-> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> Word64Map a -> Word64Map a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m)) ((Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> a -> f Bool
f Word64Map a
r) ((Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> a -> f Bool
f Word64Map a
l)
  | Bool
otherwise = (Word64Map a -> Word64Map a -> Word64Map a)
-> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m) ((Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> a -> f Bool
f Word64Map a
l) ((Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
(Word64 -> a -> f Bool) -> Word64Map a -> f (Word64Map a)
filterWithKeyA Word64 -> a -> f Bool
f Word64Map a
r)

-- | This wasn't in Data.Bool until 4.7.0, so we define it here
bool :: a -> a -> Bool -> a
bool :: forall a. a -> a -> Bool -> a
bool a
f a
_ Bool
False = a
f
bool a
_ a
t Bool
True  = a
t


-- | Traverse over the entries whose keys are missing from the other
-- map.
--
-- @since 0.5.9
traverseMissing
  :: Applicative f => (Key -> x -> f y) -> WhenMissing f x y
traverseMissing :: forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> f y) -> WhenMissing f x y
traverseMissing Word64 -> x -> f y
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map y)
missingSubtree = (Word64 -> x -> f y) -> Word64Map x -> f (Word64Map y)
forall (t :: * -> *) a b.
Applicative t =>
(Word64 -> a -> t b) -> Word64Map a -> t (Word64Map b)
traverseWithKey Word64 -> x -> f y
f
  , missingKey :: Word64 -> x -> f (Maybe y)
missingKey = \Word64
k 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
<$> Word64 -> x -> f y
f Word64
k x
x }
{-# INLINE traverseMissing #-}


-- | Traverse over the entries whose keys are missing from the other
-- map, optionally producing values to put in the result. This is
-- the most powerful 'WhenMissing' tactic, but others are usually
-- more efficient.
--
-- @since 0.5.9
traverseMaybeMissing
  :: Applicative f => (Key -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing :: forall (f :: * -> *) x y.
Applicative f =>
(Word64 -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing Word64 -> x -> f (Maybe y)
f = WhenMissing
  { missingSubtree :: Word64Map x -> f (Word64Map y)
missingSubtree = (Word64 -> x -> f (Maybe y)) -> Word64Map x -> f (Word64Map y)
forall (f :: * -> *) a b.
Applicative f =>
(Word64 -> a -> f (Maybe b)) -> Word64Map a -> f (Word64Map b)
traverseMaybeWithKey Word64 -> x -> f (Maybe y)
f
  , missingKey :: Word64 -> x -> f (Maybe y)
missingKey = Word64 -> x -> f (Maybe y)
f }
{-# INLINE traverseMaybeMissing #-}


-- | \(O(n)\). Traverse keys\/values and collect the 'Just' results.
--
-- @since 0.6.4
traverseMaybeWithKey
  :: Applicative f => (Key -> a -> f (Maybe b)) -> Word64Map a -> f (Word64Map b)
traverseMaybeWithKey :: forall (f :: * -> *) a b.
Applicative f =>
(Word64 -> a -> f (Maybe b)) -> Word64Map a -> f (Word64Map b)
traverseMaybeWithKey Word64 -> a -> f (Maybe b)
f = Word64Map a -> f (Word64Map b)
go
    where
    go :: Word64Map a -> f (Word64Map b)
go Word64Map a
Nil           = Word64Map b -> f (Word64Map b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64Map b
forall a. Word64Map a
Nil
    go (Tip Word64
k a
x)     = Word64Map b -> (b -> Word64Map b) -> Maybe b -> Word64Map b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map b
forall a. Word64Map a
Nil (Word64 -> b -> Word64Map b
forall a. Word64 -> a -> Word64Map a
Tip Word64
k) (Maybe b -> Word64Map b) -> f (Maybe b) -> f (Word64Map b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> a -> f (Maybe b)
f Word64
k a
x
    go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = (Word64Map b -> Word64Map b -> Word64Map b)
-> f (Word64Map b) -> f (Word64Map b) -> f (Word64Map b)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((Word64Map b -> Word64Map b -> Word64Map b)
-> Word64Map b -> Word64Map b -> Word64Map b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m)) (Word64Map a -> f (Word64Map b)
go Word64Map a
r) (Word64Map a -> f (Word64Map b)
go Word64Map a
l)
      | Bool
otherwise = (Word64Map b -> Word64Map b -> Word64Map b)
-> f (Word64Map b) -> f (Word64Map b) -> f (Word64Map b)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m) (Word64Map a -> f (Word64Map b)
go Word64Map a
l) (Word64Map a -> f (Word64Map b)
go Word64Map a
r)


-- | Merge two maps.
--
-- 'merge' takes two 'WhenMissing' tactics, a 'WhenMatched' tactic
-- and two maps. It uses the tactics to merge the maps. Its behavior
-- is best understood via its fundamental tactics, 'mapMaybeMissing'
-- and 'zipWithMaybeMatched'.
--
-- Consider
--
-- @
-- merge (mapMaybeMissing g1)
--              (mapMaybeMissing g2)
--              (zipWithMaybeMatched f)
--              m1 m2
-- @
--
-- Take, for example,
--
-- @
-- m1 = [(0, \'a\'), (1, \'b\'), (3, \'c\'), (4, \'d\')]
-- m2 = [(1, "one"), (2, "two"), (4, "three")]
-- @
--
-- 'merge' will first \"align\" these maps by key:
--
-- @
-- m1 = [(0, \'a\'), (1, \'b\'),               (3, \'c\'), (4, \'d\')]
-- m2 =           [(1, "one"), (2, "two"),           (4, "three")]
-- @
--
-- It will then pass the individual entries and pairs of entries
-- to @g1@, @g2@, or @f@ as appropriate:
--
-- @
-- maybes = [g1 0 \'a\', f 1 \'b\' "one", g2 2 "two", g1 3 \'c\', f 4 \'d\' "three"]
-- @
--
-- This produces a 'Maybe' for each key:
--
-- @
-- keys =     0        1          2           3        4
-- results = [Nothing, Just True, Just False, Nothing, Just True]
-- @
--
-- Finally, the @Just@ results are collected into a map:
--
-- @
-- return value = [(1, True), (2, False), (4, True)]
-- @
--
-- The other tactics below are optimizations or simplifications of
-- 'mapMaybeMissing' for special cases. Most importantly,
--
-- * 'dropMissing' drops all the keys.
-- * 'preserveMissing' leaves all the entries alone.
--
-- When 'merge' is given three arguments, it is inlined at the call
-- site. To prevent excessive inlining, you should typically use
-- 'merge' to define your custom combining functions.
--
--
-- Examples:
--
-- prop> unionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
-- prop> intersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
-- prop> differenceWith f = merge diffPreserve diffDrop f
-- prop> symmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -> Nothing)
-- prop> mapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
--
-- @since 0.5.9
merge
  :: SimpleWhenMissing a c -- ^ What to do with keys in @m1@ but not @m2@
  -> SimpleWhenMissing b c -- ^ What to do with keys in @m2@ but not @m1@
  -> SimpleWhenMatched a b c -- ^ What to do with keys in both @m1@ and @m2@
  -> Word64Map a -- ^ Map @m1@
  -> Word64Map b -- ^ Map @m2@
  -> Word64Map c
merge :: forall a c b.
SimpleWhenMissing a c
-> SimpleWhenMissing b c
-> SimpleWhenMatched a b c
-> Word64Map a
-> Word64Map b
-> Word64Map c
merge SimpleWhenMissing a c
g1 SimpleWhenMissing b c
g2 SimpleWhenMatched a b c
f Word64Map a
m1 Word64Map b
m2 =
  Identity (Word64Map c) -> Word64Map c
forall a. Identity a -> a
runIdentity (Identity (Word64Map c) -> Word64Map c)
-> Identity (Word64Map c) -> Word64Map c
forall a b. (a -> b) -> a -> b
$ SimpleWhenMissing a c
-> SimpleWhenMissing b c
-> SimpleWhenMatched a b c
-> Word64Map a
-> Word64Map b
-> Identity (Word64Map c)
forall (f :: * -> *) a c b.
Applicative f =>
WhenMissing f a c
-> WhenMissing f b c
-> WhenMatched f a b c
-> Word64Map a
-> Word64Map b
-> f (Word64Map c)
mergeA SimpleWhenMissing a c
g1 SimpleWhenMissing b c
g2 SimpleWhenMatched a b c
f Word64Map a
m1 Word64Map b
m2
{-# INLINE merge #-}


-- | An applicative version of 'merge'.
--
-- 'mergeA' takes two 'WhenMissing' tactics, a 'WhenMatched'
-- tactic and two maps. It uses the tactics to merge the maps.
-- Its behavior is best understood via its fundamental tactics,
-- 'traverseMaybeMissing' and 'zipWithMaybeAMatched'.
--
-- Consider
--
-- @
-- mergeA (traverseMaybeMissing g1)
--               (traverseMaybeMissing g2)
--               (zipWithMaybeAMatched f)
--               m1 m2
-- @
--
-- Take, for example,
--
-- @
-- m1 = [(0, \'a\'), (1, \'b\'), (3,\'c\'), (4, \'d\')]
-- m2 = [(1, "one"), (2, "two"), (4, "three")]
-- @
--
-- 'mergeA' will first \"align\" these maps by key:
--
-- @
-- m1 = [(0, \'a\'), (1, \'b\'),               (3, \'c\'), (4, \'d\')]
-- m2 =           [(1, "one"), (2, "two"),           (4, "three")]
-- @
--
-- It will then pass the individual entries and pairs of entries
-- to @g1@, @g2@, or @f@ as appropriate:
--
-- @
-- actions = [g1 0 \'a\', f 1 \'b\' "one", g2 2 "two", g1 3 \'c\', f 4 \'d\' "three"]
-- @
--
-- Next, it will perform the actions in the @actions@ list in order from
-- left to right.
--
-- @
-- keys =     0        1          2           3        4
-- results = [Nothing, Just True, Just False, Nothing, Just True]
-- @
--
-- Finally, the @Just@ results are collected into a map:
--
-- @
-- return value = [(1, True), (2, False), (4, True)]
-- @
--
-- The other tactics below are optimizations or simplifications of
-- 'traverseMaybeMissing' for special cases. Most importantly,
--
-- * 'dropMissing' drops all the keys.
-- * 'preserveMissing' leaves all the entries alone.
-- * 'mapMaybeMissing' does not use the 'Applicative' context.
--
-- When 'mergeA' is given three arguments, it is inlined at the call
-- site. To prevent excessive inlining, you should generally only use
-- 'mergeA' to define custom combining functions.
--
-- @since 0.5.9
mergeA
  :: (Applicative f)
  => WhenMissing f a c -- ^ What to do with keys in @m1@ but not @m2@
  -> WhenMissing f b c -- ^ What to do with keys in @m2@ but not @m1@
  -> WhenMatched f a b c -- ^ What to do with keys in both @m1@ and @m2@
  -> Word64Map a -- ^ Map @m1@
  -> Word64Map b -- ^ Map @m2@
  -> f (Word64Map c)
mergeA :: forall (f :: * -> *) a c b.
Applicative f =>
WhenMissing f a c
-> WhenMissing f b c
-> WhenMatched f a b c
-> Word64Map a
-> Word64Map b
-> f (Word64Map c)
mergeA
    WhenMissing{missingSubtree :: forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree = Word64Map a -> f (Word64Map c)
g1t, missingKey :: forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey = Word64 -> a -> f (Maybe c)
g1k}
    WhenMissing{missingSubtree :: forall (f :: * -> *) x y.
WhenMissing f x y -> Word64Map x -> f (Word64Map y)
missingSubtree = Word64Map b -> f (Word64Map c)
g2t, missingKey :: forall (f :: * -> *) x y.
WhenMissing f x y -> Word64 -> x -> f (Maybe y)
missingKey = Word64 -> b -> f (Maybe c)
g2k}
    WhenMatched{matchedKey :: forall (f :: * -> *) x y z.
WhenMatched f x y z -> Word64 -> x -> y -> f (Maybe z)
matchedKey = Word64 -> a -> b -> f (Maybe c)
f}
    = Word64Map a -> Word64Map b -> f (Word64Map c)
go
  where
    go :: Word64Map a -> Word64Map b -> f (Word64Map c)
go Word64Map a
t1  Word64Map b
Nil = Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1
    go Word64Map a
Nil Word64Map b
t2  = Word64Map b -> f (Word64Map c)
g2t Word64Map b
t2

    -- This case is already covered below.
    -- go (Tip k1 x1) (Tip k2 x2) = mergeTips k1 x1 k2 x2

    go (Tip Word64
k1 a
x1) Word64Map b
t2' = Word64Map b -> f (Word64Map c)
merge2 Word64Map b
t2'
      where
        merge2 :: Word64Map b -> f (Word64Map c)
merge2 t2 :: Word64Map b
t2@(Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
          | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k1 Word64
p2 Word64
m2 = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
k1 ((Word64 -> a -> f (Maybe c)) -> Word64 -> a -> f (Word64Map c)
forall {f :: * -> *} {t} {a}.
Functor f =>
(Word64 -> t -> f (Maybe a)) -> Word64 -> t -> f (Word64Map a)
subsingletonBy Word64 -> a -> f (Maybe c)
g1k Word64
k1 a
x1) Word64
p2 (Word64Map b -> f (Word64Map c)
g2t Word64Map b
t2)
          | Word64 -> Word64 -> Bool
zero Word64
k1 Word64
m2       = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p2 Word64
m2 (Word64Map b -> f (Word64Map c)
merge2 Word64Map b
l2) (Word64Map b -> f (Word64Map c)
g2t Word64Map b
r2)
          | Bool
otherwise        = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p2 Word64
m2 (Word64Map b -> f (Word64Map c)
g2t Word64Map b
l2) (Word64Map b -> f (Word64Map c)
merge2 Word64Map b
r2)
        merge2 (Tip Word64
k2 b
x2)   = Word64 -> a -> Word64 -> b -> f (Word64Map c)
mergeTips Word64
k1 a
x1 Word64
k2 b
x2
        merge2 Word64Map b
Nil           = (Word64 -> a -> f (Maybe c)) -> Word64 -> a -> f (Word64Map c)
forall {f :: * -> *} {t} {a}.
Functor f =>
(Word64 -> t -> f (Maybe a)) -> Word64 -> t -> f (Word64Map a)
subsingletonBy Word64 -> a -> f (Maybe c)
g1k Word64
k1 a
x1

    go Word64Map a
t1' (Tip Word64
k2 b
x2) = Word64Map a -> f (Word64Map c)
merge1 Word64Map a
t1'
      where
        merge1 :: Word64Map a -> f (Word64Map c)
merge1 t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1)
          | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k2 Word64
p1 Word64
m1 = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1