    {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}

    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Data.Word64Map.Strict.Internal
    -- Copyright   :  (c) Daan Leijen 2002
    --                (c) Andriy Palamarchuk 2008
    -- License     :  BSD-style
    -- Maintainer  :  libraries@haskell.org
    -- Portability :  portable
    --
    --
    -- = Finite Int Maps (strict interface)
    --
    -- The @'Word64Map' v@ type represents a finite map (sometimes called a dictionary)
    -- from key of type @Int@ to values of type @v@.
    --
    -- Each function in this module is careful to force values before installing
    -- them in an 'Word64Map'. This is usually more efficient when laziness is not
    -- necessary. When laziness /is/ required, use the functions in
    -- "Data.Word64Map.Lazy".
    --
    -- In particular, the functions in this module obey the following law:
    --
    --  - If all values stored in all maps in the arguments are in WHNF, then all
    --    values stored in all maps in the results will be in WHNF once those maps
    --    are evaluated.
    --
    -- For a walkthrough of the most commonly used functions see the
    -- <https://haskell-containers.readthedocs.io/en/latest/map.html maps introduction>.
    --
    -- This module is intended to be imported qualified, to avoid name clashes with
    -- Prelude functions:
    --
    -- > import Data.Word64Map.Strict (Word64Map)
    -- > import qualified Data.Word64Map.Strict as Word64Map
    --
    -- Note that the implementation is generally /left-biased/. Functions that take
    -- two maps as arguments and combine them, such as `union` and `intersection`,
    -- prefer the values in the first argument to those in the second.
    --
    --
    -- == Detailed performance information
    --
    -- The amortized running time is given for each operation, with \(n\) referring to
    -- the number of entries in the map and \(W\) referring to the number of bits in
    -- an 'Int' (32 or 64).
    --
    -- Benchmarks comparing "Data.Word64Map.Strict" with other dictionary
    -- implementations can be found at https://github.com/haskell-perf/dictionaries.
    --
    --
    -- == Warning
    --
    -- The 'Word64Map' type is shared between the lazy and strict modules, meaning that
    -- the same 'Word64Map' value can be passed to functions in both modules. This
    -- means that the 'Functor', 'Traversable' and 'Data.Data.Data' instances are
    -- the same as for the "Data.Word64Map.Lazy" module, so if they are used the
    -- resulting map may contain suspended values (thunks).
    --
    --
    -- == Implementation
    --
    -- The implementation is based on /big-endian patricia trees/.  This data
    -- structure performs especially well on binary operations like 'union' and
    -- 'intersection'. Additionally, benchmarks show that it is also (much) faster
    -- on insertions and deletions when compared to a generic size-balanced map
    -- implementation (see "Data.Map").
    --
    --    * Chris Okasaki and Andy Gill,  \"/Fast Mergeable Integer Maps/\",
    --      Workshop on ML, September 1998, pages 77-86,
    --      <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.5452>
    --
    --    * D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric/\",
    --      Journal of the ACM, 15(4), October 1968, pages 514-534.
    --
    -----------------------------------------------------------------------------

    -- See the notes at the beginning of Data.Word64Map.Internal.

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

        -- * Construction
        , empty
        , singleton
        , fromSet

        -- ** From Unordered Lists
        , fromList
        , fromListWith
        , fromListWithKey

        -- ** From Ascending Lists
        , fromAscList
        , fromAscListWith
        , fromAscListWithKey
        , fromDistinctAscList

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

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

        -- * Query
        -- ** Lookup
        , lookup
        , (!?)
        , (!)
        , findWithDefault
        , member
        , notMember
        , lookupLT
        , lookupGT
        , lookupLE
        , lookupGE

        -- ** Size
        , null
        , size

        -- * Combine

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

        -- ** Difference
        , difference
        , (\\)
        , differenceWith
        , differenceWithKey

        -- ** Intersection
        , intersection
        , intersectionWith
        , intersectionWithKey

        -- ** Disjoint
        , disjoint

        -- ** Compose
        , compose

        -- ** Universal combining function
        , 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

        -- ** Lists
        , toList

    -- ** Ordered lists
        , toAscList
        , toDescList

        -- * 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
        ) where

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

    import qualified GHC.Data.Word64Map.Internal as L
    import GHC.Data.Word64Map.Internal
      ( Word64Map (..)
      , Key
      , mask
      , branchMask
      , nomatch
      , zero
      , natFromInt
      , intFromNat
      , bin
      , binCheckLeft
      , binCheckRight
      , link
      , linkWithMask

      , (\\)
      , (!)
      , (!?)
      , empty
      , assocs
      , filter
      , filterWithKey
      , findMin
      , findMax
      , foldMapWithKey
      , foldr
      , foldl
      , foldr'
      , foldl'
      , foldlWithKey
      , foldrWithKey
      , foldlWithKey'
      , foldrWithKey'
      , keysSet
      , mergeWithKey'
      , compose
      , delete
      , deleteMin
      , deleteMax
      , deleteFindMax
      , deleteFindMin
      , difference
      , elems
      , intersection
      , disjoint
      , isProperSubmapOf
      , isProperSubmapOfBy
      , isSubmapOf
      , isSubmapOfBy
      , lookup
      , lookupLE
      , lookupGE
      , lookupLT
      , lookupGT
      , lookupMin
      , lookupMax
      , minView
      , maxView
      , minViewWithKey
      , maxViewWithKey
      , keys
      , mapKeys
      , mapKeysMonotonic
      , member
      , notMember
      , null
      , partition
      , partitionWithKey
      , takeWhileAntitone
      , dropWhileAntitone
      , spanAntitone
      , restrictKeys
      , size
      , split
      , splitLookup
      , splitRoot
      , toAscList
      , toDescList
      , toList
      , union
      , unions
      , withoutKeys
      )
    import qualified GHC.Data.Word64Set.Internal as Word64Set
    import GHC.Utils.Containers.Internal.BitUtil
    import GHC.Utils.Containers.Internal.StrictPair
    import qualified Data.Foldable as Foldable

    {--------------------------------------------------------------------
      Query
    --------------------------------------------------------------------}

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

    {--------------------------------------------------------------------
      Construction
    --------------------------------------------------------------------}
    -- | \(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. Key -> a -> Word64Map a
    singleton Key
    k !a
    x
      = Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    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. Key -> a -> Word64Map a -> Word64Map a
    insert !Key
    k !a
    x Word64Map a
    t =
      case Word64Map a
    t of
        Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
          | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x) Key
    p Word64Map a
    t
          | Key -> Key -> Bool
    zero Key
    k Key
    m      -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m (Key -> a -> Word64Map a -> Word64Map a
    forall a. Key -> a -> Word64Map a -> Word64Map a
    insert Key
    k a
    x Word64Map a
    l) Word64Map a
    r
          | Bool
    otherwise     -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l (Key -> a -> Word64Map a -> Word64Map a
    forall a. Key -> a -> Word64Map a -> Word64Map a
    insert Key
    k a
    x Word64Map a
    r)
        Tip Key
    ky a
    _
          | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x
          | Bool
    otherwise     -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x) Key
    ky Word64Map a
    t
        Word64Map a
    Nil -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    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) -> Key -> a -> Word64Map a -> Word64Map a
    insertWith a -> a -> a
    f Key
    k a
    x Word64Map a
    t
      = (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey (\Key
    _ a
    x' a
    y' -> a -> a -> a
    f a
    x' a
    y') Key
    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"
    --
    -- If the key exists in the map, this function is lazy in @value@ but strict
    -- in the result of @f@.

    insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey :: forall a.
    (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey Key -> a -> a -> a
    f !Key
    k a
    x Word64Map a
    t =
      case Word64Map a
    t of
        Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
          | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    singleton Key
    k a
    x) Key
    p Word64Map a
    t
          | Key -> Key -> Bool
    zero Key
    k Key
    m      -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m ((Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey Key -> a -> a -> a
    f Key
    k a
    x Word64Map a
    l) Word64Map a
    r
          | Bool
    otherwise     -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l ((Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey Key -> a -> a -> a
    f Key
    k a
    x Word64Map a
    r)
        Tip Key
    ky a
    y
          | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! Key -> a -> a -> a
    f Key
    k a
    x a
    y
          | Bool
    otherwise     -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    singleton Key
    k a
    x) Key
    ky Word64Map a
    t
        Word64Map a
    Nil -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    singleton Key
    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.
    (Key -> a -> a -> a)
    -> Key -> a -> Word64Map a -> (Maybe a, Word64Map a)
    insertLookupWithKey Key -> a -> a -> a
    f0 !Key
    k0 a
    x0 Word64Map a
    t0 = StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a)
    forall a b. StrictPair a b -> (a, b)
    toPair (StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a))
    -> StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a)
    forall a b. (a -> b) -> a -> b
    $ (Key -> a -> a -> a)
    -> Key -> a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall {t}.
    (Key -> t -> t -> t)
    -> Key -> t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    go Key -> a -> a -> a
    f0 Key
    k0 a
    x0 Word64Map a
    t0
      where
        go :: (Key -> t -> t -> t)
    -> Key -> t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    go Key -> t -> t -> t
    f Key
    k t
    x Word64Map t
    t =
          case Word64Map t
    t of
            Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r
              | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> Maybe t
    forall a. Maybe a
    Nothing Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Word64Map t -> Key -> Word64Map t -> Word64Map t
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    singleton Key
    k t
    x) Key
    p Word64Map t
    t
              | Key -> Key -> Bool
    zero Key
    k Key
    m      -> let (Maybe t
    found :*: Word64Map t
    l') = (Key -> t -> t -> t)
    -> Key -> t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    go Key -> t -> t -> t
    f Key
    k t
    x Word64Map t
    l in (Maybe t
    found Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map t -> Word64Map t -> Word64Map t
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map t
    l' Word64Map t
    r)
              | Bool
    otherwise     -> let (Maybe t
    found :*: Word64Map t
    r') = (Key -> t -> t -> t)
    -> Key -> t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    go Key -> t -> t -> t
    f Key
    k t
    x Word64Map t
    r in (Maybe t
    found Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map t -> Word64Map t -> Word64Map t
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r')
            Tip Key
    ky t
    y
              | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> (t -> Maybe t
    forall a. a -> Maybe a
    Just t
    y Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: (Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    Tip Key
    k (t -> Word64Map t) -> t -> Word64Map t
    forall a b. (a -> b) -> a -> b
    $! Key -> t -> t -> t
    f Key
    k t
    x t
    y))
              | Bool
    otherwise     -> (Maybe t
    forall a. Maybe a
    Nothing Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Word64Map t -> Key -> Word64Map t -> Word64Map t
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    singleton Key
    k t
    x) Key
    ky Word64Map t
    t)
            Word64Map t
    Nil -> Maybe t
    forall a. Maybe a
    Nothing Maybe t -> Word64Map t -> StrictPair (Maybe t) (Word64Map t)
    forall a b. a -> b -> StrictPair a b
    :*: (Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    singleton Key
    k t
    x)

    {--------------------------------------------------------------------
      Deletion
    --------------------------------------------------------------------}
    -- | \(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) -> Key -> Word64Map a -> Word64Map a
    adjust a -> a
    f Key
    k Word64Map a
    m
      = (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    adjustWithKey (\Key
    _ a
    x -> a -> a
    f a
    x) Key
    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. (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    adjustWithKey Key -> a -> a
    f !Key
    k Word64Map a
    t =
      case Word64Map a
    t of
        Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
          | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> Word64Map a
    t
          | Key -> Key -> Bool
    zero Key
    k Key
    m      -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m ((Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    adjustWithKey Key -> a -> a
    f Key
    k Word64Map a
    l) Word64Map a
    r
          | Bool
    otherwise     -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l ((Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> a) -> Key -> Word64Map a -> Word64Map a
    adjustWithKey Key -> a -> a
    f Key
    k Word64Map a
    r)
        Tip Key
    ky a
    y
          | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    ky (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! Key -> a -> a
    f Key
    k a
    y
          | Bool
    otherwise     -> Word64Map a
    t
        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) -> Key -> Word64Map a -> Word64Map a
    update a -> Maybe a
    f
      = (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    updateWithKey (\Key
    _ 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.
    (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    updateWithKey Key -> a -> Maybe a
    f !Key
    k Word64Map a
    t =
      case Word64Map a
    t of
        Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
          | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> Word64Map a
    t
          | Key -> Key -> Bool
    zero Key
    k Key
    m      -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckLeft Key
    p Key
    m ((Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    updateWithKey Key -> a -> Maybe a
    f Key
    k Word64Map a
    l) Word64Map a
    r
          | Bool
    otherwise     -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckRight Key
    p Key
    m Word64Map a
    l ((Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    updateWithKey Key -> a -> Maybe a
    f Key
    k Word64Map a
    r)
        Tip Key
    ky a
    y
          | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> case Key -> a -> Maybe a
    f Key
    k a
    y of
                               Just !a
    y' -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    ky a
    y'
                               Maybe a
    Nothing -> Word64Map a
    forall a. Word64Map a
    Nil
          | Bool
    otherwise     -> Word64Map a
    t
        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.
    (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> (Maybe a, Word64Map a)
    updateLookupWithKey Key -> a -> Maybe a
    f0 !Key
    k0 Word64Map a
    t0 = StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a)
    forall a b. StrictPair a b -> (a, b)
    toPair (StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a))
    -> StrictPair (Maybe a) (Word64Map a) -> (Maybe a, Word64Map a)
    forall a b. (a -> b) -> a -> b
    $ (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall {a}.
    (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    go Key -> a -> Maybe a
    f0 Key
    k0 Word64Map a
    t0
      where
        go :: (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    go Key -> a -> Maybe a
    f Key
    k Word64Map a
    t =
          case Word64Map a
    t of
            Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
              | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> (Maybe a
    forall a. Maybe a
    Nothing Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    t)
              | Key -> Key -> Bool
    zero Key
    k Key
    m      -> let (Maybe a
    found :*: Word64Map a
    l') = (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    go Key -> a -> Maybe a
    f Key
    k Word64Map a
    l in (Maybe a
    found Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckLeft Key
    p Key
    m Word64Map a
    l' Word64Map a
    r)
              | Bool
    otherwise     -> let (Maybe a
    found :*: Word64Map a
    r') = (Key -> a -> Maybe a)
    -> Key -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    go Key -> a -> Maybe a
    f Key
    k Word64Map a
    r in (Maybe a
    found Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckRight Key
    p Key
    m Word64Map a
    l Word64Map a
    r')
            Tip Key
    ky a
    y
              | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> case Key -> a -> Maybe a
    f Key
    k a
    y of
                                   Just !a
    y' -> (a -> Maybe a
    forall a. a -> Maybe a
    Just a
    y Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    ky a
    y')
                                   Maybe a
    Nothing  -> (a -> Maybe a
    forall a. a -> Maybe a
    Just a
    y Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    forall a. Word64Map a
    Nil)
              | Bool
    otherwise     -> (Maybe a
    forall a. Maybe a
    Nothing Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    t)
            Word64Map a
    Nil -> (Maybe a
    forall a. Maybe a
    Nothing Maybe a -> Word64Map a -> StrictPair (Maybe a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: 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) -> Key -> Word64Map a -> Word64Map a
    alter Maybe a -> Maybe a
    f !Key
    k Word64Map a
    t =
      case Word64Map a
    t of
        Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r
          | Key -> Key -> Key -> Bool
    nomatch Key
    k Key
    p Key
    m -> case Maybe a -> Maybe a
    f Maybe a
    forall a. Maybe a
    Nothing of
                               Maybe a
    Nothing -> Word64Map a
    t
                               Just !a
    x  -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x) Key
    p Word64Map a
    t
          | Key -> Key -> Bool
    zero Key
    k Key
    m      -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckLeft Key
    p Key
    m ((Maybe a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    forall a. (Maybe a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    alter Maybe a -> Maybe a
    f Key
    k Word64Map a
    l) Word64Map a
    r
          | Bool
    otherwise     -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckRight Key
    p Key
    m Word64Map a
    l ((Maybe a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    forall a. (Maybe a -> Maybe a) -> Key -> Word64Map a -> Word64Map a
    alter Maybe a -> Maybe a
    f Key
    k Word64Map a
    r)
        Tip Key
    ky a
    y
          | Key
    kKey -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Key
    ky         -> case Maybe a -> Maybe a
    f (a -> Maybe a
    forall a. a -> Maybe a
    Just a
    y) of
                               Just !a
    x -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    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 -> Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Key -> Word64Map a -> Word64Map a
    link Key
    k (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x) Key
    ky Word64Map a
    t
                               Maybe a
    Nothing -> Word64Map a
    t
        Word64Map a
    Nil               -> case Maybe a -> Maybe a
    f Maybe a
    forall a. Maybe a
    Nothing of
                               Just !a
    x -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x
                               Maybe a
    Nothing -> Word64Map a
    forall a. Word64Map a
    Nil

    -- | \(O(\log n)\). 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 modified from 'Control.Lens.At'.
    alterF :: forall (f :: * -> *) a.
    Functor f =>
    (Maybe a -> f (Maybe a)) -> Key -> Word64Map a -> f (Word64Map a)
    alterF Maybe a -> f (Maybe a)
    f Key
    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 (Key -> Word64Map a -> Word64Map a
    forall a. Key -> Word64Map a -> Word64Map a
    delete Key
    k Word64Map a
    m)) Maybe a
    mv
        Just !a
    v' -> Key -> a -> Word64Map a -> Word64Map a
    forall a. Key -> a -> Word64Map a -> Word64Map a
    insert Key
    k a
    v' Word64Map a
    m
      where mv :: Maybe a
    mv = Key -> Word64Map a -> Maybe a
    forall a. Key -> Word64Map a -> Maybe a
    lookup Key
    k Word64Map a
    m

    {--------------------------------------------------------------------
      Union
    --------------------------------------------------------------------}
    -- | 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 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
      = (Key -> a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
    unionWithKey (\Key
    _ 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.
    (Key -> a -> a -> a) -> Word64Map a -> Word64Map a -> Word64Map a
    unionWithKey Key -> a -> a -> a
    f Word64Map a
    m1 Word64Map a
    m2
      = (Key -> Key -> 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.
    (Key -> Key -> 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' Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin (\(Tip Key
    k1 a
    x1) (Tip Key
    _k2 a
    x2) -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k1 (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! Key -> a -> a -> a
    f Key
    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 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
      = (Key -> a -> b -> Maybe a)
    -> Word64Map a -> Word64Map b -> Word64Map a
    forall a b.
    (Key -> a -> b -> Maybe a)
    -> Word64Map a -> Word64Map b -> Word64Map a
    differenceWithKey (\Key
    _ 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.
    (Key -> a -> b -> Maybe a)
    -> Word64Map a -> Word64Map b -> Word64Map a
    differenceWithKey Key -> a -> b -> Maybe a
    f Word64Map a
    m1 Word64Map b
    m2
      = (Key -> a -> b -> Maybe a)
    -> (Word64Map a -> Word64Map a)
    -> (Word64Map b -> Word64Map a)
    -> Word64Map a
    -> Word64Map b
    -> Word64Map a
    forall a b c.
    (Key -> a -> b -> Maybe c)
    -> (Word64Map a -> Word64Map c)
    -> (Word64Map b -> Word64Map c)
    -> Word64Map a
    -> Word64Map b
    -> Word64Map c
    mergeWithKey Key -> 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

    {--------------------------------------------------------------------
      Intersection
    --------------------------------------------------------------------}

    -- | \(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
      = (Key -> a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
    forall a b c.
    (Key -> a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
    intersectionWithKey (\Key
    _ 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.
    (Key -> a -> b -> c) -> Word64Map a -> Word64Map b -> Word64Map c
    intersectionWithKey Key -> a -> b -> c
    f Word64Map a
    m1 Word64Map b
    m2
      = (Key -> Key -> 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.
    (Key -> Key -> 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' Key -> Key -> Word64Map c -> Word64Map c -> Word64Map c
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin (\(Tip Key
    k1 a
    x1) (Tip Key
    _k2 b
    x2) -> Key -> c -> Word64Map c
    forall a. Key -> a -> Word64Map a
    Tip Key
    k1 (c -> Word64Map c) -> c -> Word64Map c
    forall a b. (a -> b) -> a -> b
    $! Key -> a -> b -> c
    f Key
    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.
    (Key -> a -> b -> Maybe c)
    -> (Word64Map a -> Word64Map c)
    -> (Word64Map b -> Word64Map c)
    -> Word64Map a
    -> Word64Map b
    -> Word64Map c
    mergeWithKey Key -> a -> b -> Maybe c
    f Word64Map a -> Word64Map c
    g1 Word64Map b -> Word64Map c
    g2 = (Key -> Key -> 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.
    (Key -> Key -> 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' Key -> Key -> Word64Map c -> Word64Map c -> Word64Map c
    forall a. Key -> Key -> 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 Key
    k1 a
    x1) (Tip Key
    _k2 b
    x2) -> case Key -> a -> b -> Maybe c
    f Key
    k1 a
    x1 b
    x2 of Maybe c
    Nothing -> Word64Map c
    forall a. Word64Map a
    Nil
                                                                      Just !c
    x -> Key -> c -> Word64Map c
    forall a. Key -> a -> Word64Map a
    Tip Key
    k1 c
    x
            {-# INLINE combine #-}
    {-# INLINE mergeWithKey #-}

    {--------------------------------------------------------------------
      Min\/Max
    --------------------------------------------------------------------}

    -- | \(O(\log n)\). Update the value at the minimal key.
    --
    -- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
    -- > updateMinWithKey (\ _ _ -> Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"

    updateMinWithKey :: (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMinWithKey :: forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMinWithKey Key -> a -> Maybe a
    f Word64Map a
    t =
      case Word64Map a
    t of Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    0 -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckRight Key
    p Key
    m Word64Map a
    l ((Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    go Key -> a -> Maybe a
    f Word64Map a
    r)
                Word64Map a
    _ -> (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    go Key -> a -> Maybe a
    f Word64Map a
    t
      where
        go :: (Key -> t -> Maybe t) -> Word64Map t -> Word64Map t
    go Key -> t -> Maybe t
    f' (Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r) = Key -> Key -> Word64Map t -> Word64Map t -> Word64Map t
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckLeft Key
    p Key
    m ((Key -> t -> Maybe t) -> Word64Map t -> Word64Map t
    go Key -> t -> Maybe t
    f' Word64Map t
    l) Word64Map t
    r
        go Key -> t -> Maybe t
    f' (Tip Key
    k t
    y) = case Key -> t -> Maybe t
    f' Key
    k t
    y of
                            Just !t
    y' -> Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    Tip Key
    k t
    y'
                            Maybe t
    Nothing -> Word64Map t
    forall a. Word64Map a
    Nil
        go Key -> t -> Maybe t
    _ Word64Map t
    Nil = [Char] -> Word64Map t
    forall a. HasCallStack => [Char] -> a
    error [Char]
    "updateMinWithKey Nil"

    -- | \(O(\log n)\). Update the value at the maximal key.
    --
    -- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
    -- > updateMaxWithKey (\ _ _ -> Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"

    updateMaxWithKey :: (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMaxWithKey :: forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMaxWithKey Key -> a -> Maybe a
    f Word64Map a
    t =
      case Word64Map a
    t of Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    0 -> Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckLeft Key
    p Key
    m ((Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    go Key -> a -> Maybe a
    f Word64Map a
    l) Word64Map a
    r
                Word64Map a
    _ -> (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    go Key -> a -> Maybe a
    f Word64Map a
    t
      where
        go :: (Key -> t -> Maybe t) -> Word64Map t -> Word64Map t
    go Key -> t -> Maybe t
    f' (Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r) = Key -> Key -> Word64Map t -> Word64Map t -> Word64Map t
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    binCheckRight Key
    p Key
    m Word64Map t
    l ((Key -> t -> Maybe t) -> Word64Map t -> Word64Map t
    go Key -> t -> Maybe t
    f' Word64Map t
    r)
        go Key -> t -> Maybe t
    f' (Tip Key
    k t
    y) = case Key -> t -> Maybe t
    f' Key
    k t
    y of
                            Just !t
    y' -> Key -> t -> Word64Map t
    forall a. Key -> a -> Word64Map a
    Tip Key
    k t
    y'
                            Maybe t
    Nothing -> Word64Map t
    forall a. Word64Map a
    Nil
        go Key -> t -> Maybe t
    _ Word64Map t
    Nil = [Char] -> Word64Map t
    forall a. HasCallStack => [Char] -> a
    error [Char]
    "updateMaxWithKey Nil"

    -- | \(O(\log n)\). Update the value at the maximal key.
    --
    -- > updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
    -- > updateMax (\ _ -> Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"

    updateMax :: (a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMax :: forall a. (a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMax a -> Maybe a
    f = (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMaxWithKey ((a -> Maybe a) -> Key -> a -> Maybe a
    forall a b. a -> b -> a
    const a -> Maybe a
    f)

    -- | \(O(\log n)\). Update the value at the minimal key.
    --
    -- > updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
    -- > updateMin (\ _ -> Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"

    updateMin :: (a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMin :: forall a. (a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMin a -> Maybe a
    f = (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    forall a. (Key -> a -> Maybe a) -> Word64Map a -> Word64Map a
    updateMinWithKey ((a -> Maybe a) -> Key -> a -> Maybe a
    forall a b. a -> b -> a
    const a -> Maybe a
    f)

    {--------------------------------------------------------------------
      Mapping
    --------------------------------------------------------------------}
    -- | \(O(n)\). Map a function over all values in the map.
    --
    -- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]

    map :: (a -> b) -> Word64Map a -> Word64Map b
    map :: forall a b. (a -> b) -> Word64Map a -> Word64Map b
    map a -> b
    f = Word64Map a -> Word64Map b
    go
      where
        go :: Word64Map a -> Word64Map b
    go (Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r) = Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m (Word64Map a -> Word64Map b
    go Word64Map a
    l) (Word64Map a -> Word64Map b
    go Word64Map a
    r)
        go (Tip Key
    k a
    x)     = Key -> b -> Word64Map b
    forall a. Key -> a -> Word64Map a
    Tip Key
    k (b -> Word64Map b) -> b -> Word64Map b
    forall a b. (a -> b) -> a -> b
    $! a -> b
    f a
    x
        go Word64Map a
    Nil           = Word64Map b
    forall a. Word64Map a
    Nil

    {-# NOINLINE [1] map #-}
    {-# RULES
    "map/map" forall f g xs . map f (map g xs) = map (\x -> f $! g x) xs
    "map/mapL" forall f g xs . map f (L.map g xs) = map (\x -> f (g x)) xs
     #-}

    -- | \(O(n)\). Map a function over all values in the map.
    --
    -- > let f key x = (show key) ++ ":" ++ x
    -- > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]

    mapWithKey :: (Key -> a -> b) -> Word64Map a -> Word64Map b
    mapWithKey :: forall a b. (Key -> a -> b) -> Word64Map a -> Word64Map b
    mapWithKey Key -> a -> b
    f Word64Map a
    t
      = case Word64Map a
    t of
          Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r -> Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m ((Key -> a -> b) -> Word64Map a -> Word64Map b
    forall a b. (Key -> a -> b) -> Word64Map a -> Word64Map b
    mapWithKey Key -> a -> b
    f Word64Map a
    l) ((Key -> a -> b) -> Word64Map a -> Word64Map b
    forall a b. (Key -> a -> b) -> Word64Map a -> Word64Map b
    mapWithKey Key -> a -> b
    f Word64Map a
    r)
          Tip Key
    k a
    x     -> Key -> b -> Word64Map b
    forall a. Key -> a -> Word64Map a
    Tip Key
    k (b -> Word64Map b) -> b -> Word64Map b
    forall a b. (a -> b) -> a -> b
    $! Key -> a -> b
    f Key
    k a
    x
          Word64Map a
    Nil         -> Word64Map b
    forall a. Word64Map a
    Nil

    -- Pay close attention to strictness here. We need to force the
    -- intermediate result for map f . map g, and we need to refrain
    -- from forcing it for map f . L.map g, etc.
    --
    -- TODO Consider moving map and mapWithKey to Word64Map.Internal so we can write
    -- non-orphan RULES for things like L.map f (map g xs). We'd need a new function
    -- for this, and we'd have to pay attention to simplifier phases. Something like
    --
    -- lsmap :: (b -> c) -> (a -> b) -> Word64Map a -> Word64Map c
    -- lsmap _ _ Nil = Nil
    -- lsmap f g (Tip k x) = let !gx = g x in Tip k (f gx)
    -- lsmap f g (Bin p m l r) = Bin p m (lsmap f g l) (lsmap f g r)
    {-# NOINLINE [1] mapWithKey #-}
    {-# RULES
    "mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
      mapWithKey (\k a -> f k $! g k a) xs
    "mapWithKey/mapWithKeyL" forall f g xs . mapWithKey f (L.mapWithKey g xs) =
      mapWithKey (\k a -> f k (g k a)) xs
    "mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
      mapWithKey (\k a -> f k $! g a) xs
    "mapWithKey/mapL" forall f g xs . mapWithKey f (L.map g xs) =
      mapWithKey (\k a -> f k (g a)) xs
    "map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
      mapWithKey (\k a -> f $! g k a) xs
    "map/mapWithKeyL" forall f g xs . map f (L.mapWithKey g xs) =
      mapWithKey (\k a -> f (g k a)) xs
     #-}

    -- | \(O(n)\).
    -- @'traverseWithKey' f s == 'fromList' <$> 'traverse' (\(k, v) -> (,) k <$> f k v) ('toList' m)@
    -- That is, behaves exactly like a regular 'traverse' except that the traversing
    -- function also has access to the key associated with a value.
    --
    -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
    -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
    traverseWithKey :: Applicative t => (Key -> a -> t b) -> Word64Map a -> t (Word64Map b)
    traverseWithKey :: forall (t :: * -> *) a b.
    Applicative t =>
    (Key -> a -> t b) -> Word64Map a -> t (Word64Map b)
    traverseWithKey Key -> a -> t b
    f = Word64Map a -> t (Word64Map b)
    go
      where
        go :: Word64Map a -> t (Word64Map b)
    go Word64Map a
    Nil = Word64Map b -> t (Word64Map b)
    forall a. a -> t a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Word64Map b
    forall a. Word64Map a
    Nil
        go (Tip Key
    k a
    v) = (\ !b
    v' -> Key -> b -> Word64Map b
    forall a. Key -> a -> Word64Map a
    Tip Key
    k b
    v') (b -> Word64Map b) -> t b -> t (Word64Map b)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Key -> a -> t b
    f Key
    k a
    v
        go (Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r)
          | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    0     = (Word64Map b -> Word64Map b -> Word64Map b)
    -> t (Word64Map b) -> t (Word64Map b) -> t (Word64Map b)
    forall a b c. (a -> b -> c) -> t a -> t b -> t 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 (Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m)) (Word64Map a -> t (Word64Map b)
    go Word64Map a
    r) (Word64Map a -> t (Word64Map b)
    go Word64Map a
    l)
          | Bool
    otherwise = (Word64Map b -> Word64Map b -> Word64Map b)
    -> t (Word64Map b) -> t (Word64Map b) -> t (Word64Map b)
    forall a b c. (a -> b -> c) -> t a -> t b -> t c
    forall (f :: * -> *) a b c.
    Applicative f =>
    (a -> b -> c) -> f a -> f b -> f c
    liftA2 (Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m) (Word64Map a -> t (Word64Map b)
    go Word64Map a
    l) (Word64Map a -> t (Word64Map b)
    go Word64Map a
    r)
    {-# INLINE traverseWithKey #-}

    -- | \(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 =>
    (Key -> a -> f (Maybe b)) -> Word64Map a -> f (Word64Map b)
    traverseMaybeWithKey Key -> 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 Key
    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 (Key -> b -> Word64Map b
    forall a. Key -> a -> Word64Map a
    Tip Key
    k (b -> Word64Map b) -> b -> Word64Map b
    forall a b. (a -> b) -> a -> b
    $!) (Maybe b -> Word64Map b) -> f (Maybe b) -> f (Word64Map b)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Key -> a -> f (Maybe b)
    f Key
    k a
    x
        go (Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r)
          | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    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 (Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin Key
    p Key
    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 (Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin Key
    p Key
    m) (Word64Map a -> f (Word64Map b)
    go Word64Map a
    l) (Word64Map a -> f (Word64Map b)
    go Word64Map a
    r)

    -- | \(O(n)\). The function @'mapAccum'@ threads an accumulating
    -- argument through the map in ascending order of keys.
    --
    -- > let f a b = (a ++ b, b ++ "X")
    -- > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])

    mapAccum :: (a -> b -> (a,c)) -> a -> Word64Map b -> (a,Word64Map c)
    mapAccum :: forall a b c.
    (a -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccum a -> b -> (a, c)
    f = (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    forall a b c.
    (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccumWithKey (\a
    a' Key
    _ b
    x -> a -> b -> (a, c)
    f a
    a' b
    x)

    -- | \(O(n)\). The function @'mapAccumWithKey'@ threads an accumulating
    -- argument through the map in ascending order of keys.
    --
    -- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
    -- > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])

    mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> Word64Map b -> (a,Word64Map c)
    mapAccumWithKey :: forall a b c.
    (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccumWithKey a -> Key -> b -> (a, c)
    f a
    a Word64Map b
    t
      = (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    forall a b c.
    (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccumL a -> Key -> b -> (a, c)
    f a
    a Word64Map b
    t

    -- | \(O(n)\). The function @'mapAccumL'@ threads an accumulating
    -- argument through the map in ascending order of keys.  Strict in
    -- the accumulating argument and the both elements of the
    -- result of the function.
    mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> Word64Map b -> (a,Word64Map c)
    mapAccumL :: forall a b c.
    (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccumL a -> Key -> b -> (a, c)
    f0 a
    a0 Word64Map b
    t0 = StrictPair a (Word64Map c) -> (a, Word64Map c)
    forall a b. StrictPair a b -> (a, b)
    toPair (StrictPair a (Word64Map c) -> (a, Word64Map c))
    -> StrictPair a (Word64Map c) -> (a, Word64Map c)
    forall a b. (a -> b) -> a -> b
    $ (a -> Key -> b -> (a, c))
    -> a -> Word64Map b -> StrictPair a (Word64Map c)
    forall {t} {t} {a}.
    (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go a -> Key -> b -> (a, c)
    f0 a
    a0 Word64Map b
    t0
      where
        go :: (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    t
          = case Word64Map t
    t of
              Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r
                | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    0 ->
                    let (t
    a1 :*: Word64Map a
    r') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    r
                        (t
    a2 :*: Word64Map a
    l') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a1 Word64Map t
    l
                    in (t
    a2 t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l' Word64Map a
    r')
                | Bool
    otherwise ->
                    let (t
    a1 :*: Word64Map a
    l') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    l
                        (t
    a2 :*: Word64Map a
    r') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a1 Word64Map t
    r
                    in (t
    a2 t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l' Word64Map a
    r')
              Tip Key
    k t
    x     -> let !(t
    a',!a
    x') = t -> Key -> t -> (t, a)
    f t
    a Key
    k t
    x in (t
    a' t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x')
              Word64Map t
    Nil         -> (t
    a t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    forall a. Word64Map a
    Nil)

    -- | \(O(n)\). The function @'mapAccumRWithKey'@ threads an accumulating
    -- argument through the map in descending order of keys.
    mapAccumRWithKey :: (a -> Key -> b -> (a,c)) -> a -> Word64Map b -> (a,Word64Map c)
    mapAccumRWithKey :: forall a b c.
    (a -> Key -> b -> (a, c)) -> a -> Word64Map b -> (a, Word64Map c)
    mapAccumRWithKey a -> Key -> b -> (a, c)
    f0 a
    a0 Word64Map b
    t0 = StrictPair a (Word64Map c) -> (a, Word64Map c)
    forall a b. StrictPair a b -> (a, b)
    toPair (StrictPair a (Word64Map c) -> (a, Word64Map c))
    -> StrictPair a (Word64Map c) -> (a, Word64Map c)
    forall a b. (a -> b) -> a -> b
    $ (a -> Key -> b -> (a, c))
    -> a -> Word64Map b -> StrictPair a (Word64Map c)
    forall {t} {t} {a}.
    (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go a -> Key -> b -> (a, c)
    f0 a
    a0 Word64Map b
    t0
      where
        go :: (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    t
          = case Word64Map t
    t of
              Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r
                | Key
    m Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Key
    0 ->
                  let (t
    a1 :*: Word64Map a
    l') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    l
                      (t
    a2 :*: Word64Map a
    r') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a1 Word64Map t
    r
                  in (t
    a2 t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l' Word64Map a
    r')
                | Bool
    otherwise ->
                  let (t
    a1 :*: Word64Map a
    r') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a Word64Map t
    r
                      (t
    a2 :*: Word64Map a
    l') = (t -> Key -> t -> (t, a))
    -> t -> Word64Map t -> StrictPair t (Word64Map a)
    go t -> Key -> t -> (t, a)
    f t
    a1 Word64Map t
    l
                  in (t
    a2 t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m Word64Map a
    l' Word64Map a
    r')
              Tip Key
    k t
    x     -> let !(t
    a',!a
    x') = t -> Key -> t -> (t, a)
    f t
    a Key
    k t
    x in (t
    a' t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    x')
              Word64Map t
    Nil         -> (t
    a t -> Word64Map a -> StrictPair t (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    forall a. Word64Map a
    Nil)

    -- | \(O(n \log n)\).
    -- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
    --
    -- The size of the result may be smaller if @f@ maps two or more distinct
    -- keys to the same new key.  In this case the associated values will be
    -- combined using @c@.
    --
    -- > mapKeysWith (++) (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
    -- > mapKeysWith (++) (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"

    mapKeysWith :: (a -> a -> a) -> (Key->Key) -> Word64Map a -> Word64Map a
    mapKeysWith :: forall a.
    (a -> a -> a) -> (Key -> Key) -> Word64Map a -> Word64Map a
    mapKeysWith a -> a -> a
    c Key -> Key
    f = (a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a. (a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromListWith a -> a -> a
    c ([(Key, a)] -> Word64Map a)
    -> (Word64Map a -> [(Key, a)]) -> Word64Map a -> Word64Map a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Key -> a -> [(Key, a)] -> [(Key, a)])
    -> [(Key, a)] -> Word64Map a -> [(Key, a)]
    forall a b. (Key -> a -> b -> b) -> b -> Word64Map a -> b
    foldrWithKey (\Key
    k a
    x [(Key, a)]
    xs -> (Key -> Key
    f Key
    k, a
    x) (Key, a) -> [(Key, a)] -> [(Key, a)]
    forall a. a -> [a] -> [a]
    : [(Key, a)]
    xs) []

    {--------------------------------------------------------------------
      Filter
    --------------------------------------------------------------------}
    -- | \(O(n)\). Map values and collect the 'Just' results.
    --
    -- > let f x = if x == "a" then Just "new a" else Nothing
    -- > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"

    mapMaybe :: (a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybe :: forall a b. (a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybe a -> Maybe b
    f = (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    forall a b. (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybeWithKey (\Key
    _ a
    x -> a -> Maybe b
    f a
    x)

    -- | \(O(n)\). Map keys\/values and collect the 'Just' results.
    --
    -- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing
    -- > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"

    mapMaybeWithKey :: (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybeWithKey :: forall a b. (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybeWithKey Key -> a -> Maybe b
    f (Bin Key
    p Key
    m Word64Map a
    l Word64Map a
    r)
      = Key -> Key -> Word64Map b -> Word64Map b -> Word64Map b
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin Key
    p Key
    m ((Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    forall a b. (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybeWithKey Key -> a -> Maybe b
    f Word64Map a
    l) ((Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    forall a b. (Key -> a -> Maybe b) -> Word64Map a -> Word64Map b
    mapMaybeWithKey Key -> a -> Maybe b
    f Word64Map a
    r)
    mapMaybeWithKey Key -> a -> Maybe b
    f (Tip Key
    k a
    x) = case Key -> a -> Maybe b
    f Key
    k a
    x of
      Just !b
    y  -> Key -> b -> Word64Map b
    forall a. Key -> a -> Word64Map a
    Tip Key
    k b
    y
      Maybe b
    Nothing -> Word64Map b
    forall a. Word64Map a
    Nil
    mapMaybeWithKey Key -> a -> Maybe b
    _ Word64Map a
    Nil = Word64Map b
    forall a. Word64Map a
    Nil

    -- | \(O(n)\). Map values and separate the 'Left' and 'Right' results.
    --
    -- > let f a = if a < "c" then Left a else Right a
    -- > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
    -- >     == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
    -- >
    -- > mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
    -- >     == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])

    mapEither :: (a -> Either b c) -> Word64Map a -> (Word64Map b, Word64Map c)
    mapEither :: forall a b c.
    (a -> Either b c) -> Word64Map a -> (Word64Map b, Word64Map c)
    mapEither a -> Either b c
    f Word64Map a
    m
      = (Key -> a -> Either b c)
    -> Word64Map a -> (Word64Map b, Word64Map c)
    forall a b c.
    (Key -> a -> Either b c)
    -> Word64Map a -> (Word64Map b, Word64Map c)
    mapEitherWithKey (\Key
    _ a
    x -> a -> Either b c
    f a
    x) Word64Map a
    m

    -- | \(O(n)\). Map keys\/values and separate the 'Left' and 'Right' results.
    --
    -- > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a)
    -- > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
    -- >     == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
    -- >
    -- > mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
    -- >     == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])

    mapEitherWithKey :: (Key -> a -> Either b c) -> Word64Map a -> (Word64Map b, Word64Map c)
    mapEitherWithKey :: forall a b c.
    (Key -> a -> Either b c)
    -> Word64Map a -> (Word64Map b, Word64Map c)
    mapEitherWithKey Key -> a -> Either b c
    f0 Word64Map a
    t0 = StrictPair (Word64Map b) (Word64Map c)
    -> (Word64Map b, Word64Map c)
    forall a b. StrictPair a b -> (a, b)
    toPair (StrictPair (Word64Map b) (Word64Map c)
     -> (Word64Map b, Word64Map c))
    -> StrictPair (Word64Map b) (Word64Map c)
    -> (Word64Map b, Word64Map c)
    forall a b. (a -> b) -> a -> b
    $ (Key -> a -> Either b c)
    -> Word64Map a -> StrictPair (Word64Map b) (Word64Map c)
    forall {t} {a} {a}.
    (Key -> t -> Either a a)
    -> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
    go Key -> a -> Either b c
    f0 Word64Map a
    t0
      where
        go :: (Key -> t -> Either a a)
    -> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
    go Key -> t -> Either a a
    f (Bin Key
    p Key
    m Word64Map t
    l Word64Map t
    r)
          = Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin Key
    p Key
    m Word64Map a
    l1 Word64Map a
    r1 Word64Map a
    -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    bin Key
    p Key
    m Word64Map a
    l2 Word64Map a
    r2
          where
            (Word64Map a
    l1 :*: Word64Map a
    l2) = (Key -> t -> Either a a)
    -> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
    go Key -> t -> Either a a
    f Word64Map t
    l
            (Word64Map a
    r1 :*: Word64Map a
    r2) = (Key -> t -> Either a a)
    -> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
    go Key -> t -> Either a a
    f Word64Map t
    r
        go Key -> t -> Either a a
    f (Tip Key
    k t
    x) = case Key -> t -> Either a a
    f Key
    k t
    x of
          Left !a
    y  -> (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    y Word64Map a
    -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    forall a. Word64Map a
    Nil)
          Right !a
    z -> (Word64Map a
    forall a. Word64Map a
    Nil Word64Map a
    -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    k a
    z)
        go Key -> t -> Either a a
    _ Word64Map t
    Nil = (Word64Map a
    forall a. Word64Map a
    Nil Word64Map a
    -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
    forall a b. a -> b -> StrictPair a b
    :*: Word64Map a
    forall a. Word64Map a
    Nil)

    {--------------------------------------------------------------------
      Conversions
    --------------------------------------------------------------------}

    -- | \(O(n)\). Build a map from a set of keys and a function which for each key
    -- computes its value.
    --
    -- > fromSet (\k -> replicate k 'a') (Data.Word64Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
    -- > fromSet undefined Data.Word64Set.empty == empty

    fromSet :: (Key -> a) -> Word64Set.Word64Set -> Word64Map a
    fromSet :: forall a. (Key -> a) -> Word64Set -> Word64Map a
    fromSet Key -> a
    _ Word64Set
    Word64Set.Nil = Word64Map a
    forall a. Word64Map a
    Nil
    fromSet Key -> a
    f (Word64Set.Bin Key
    p Key
    m Word64Set
    l Word64Set
    r) = Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    p Key
    m ((Key -> a) -> Word64Set -> Word64Map a
    forall a. (Key -> a) -> Word64Set -> Word64Map a
    fromSet Key -> a
    f Word64Set
    l) ((Key -> a) -> Word64Set -> Word64Map a
    forall a. (Key -> a) -> Word64Set -> Word64Map a
    fromSet Key -> a
    f Word64Set
    r)
    fromSet Key -> a
    f (Word64Set.Tip Key
    kx Key
    bm) = (Key -> a) -> Key -> Key -> Key -> Word64Map a
    forall {a}. (Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    f Key
    kx Key
    bm (Key
    Word64Set.suffixBitMask Key -> Key -> Key
    forall a. Num a => a -> a -> a
    + Key
    1)
      where -- This is slightly complicated, as we to convert the dense
            -- representation of Word64Set into tree representation of Word64Map.
            --
            -- We are given a nonzero bit mask 'bmask' of 'bits' bits with prefix 'prefix'.
            -- We split bmask into halves corresponding to left and right subtree.
            -- If they are both nonempty, we create a Bin node, otherwise exactly
            -- one of them is nonempty and we construct the Word64Map from that half.
            buildTree :: (Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    g !Key
    prefix !Key
    bmask Key
    bits = case Key
    bits of
              Key
    0 -> Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    prefix (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! Key -> a
    g Key
    prefix
              Key
    _ -> case Key -> Key
    intFromNat ((Key -> Key
    natFromInt Key
    bits) Key -> Int -> Key
    `shiftRL` Int
    1) of
                     Key
    bits2 | Key
    bmask Key -> Key -> Key
    forall a. Bits a => a -> a -> a
    .&. ((Key
    1 Key -> Int -> Key
    `shiftLL` Key -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Key
    bits2) Key -> Key -> Key
    forall a. Num a => a -> a -> a
    - Key
    1) Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    == Key
    0 ->
                               (Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    g (Key
    prefix Key -> Key -> Key
    forall a. Num a => a -> a -> a
    + Key
    bits2) (Key
    bmask Key -> Int -> Key
    `shiftRL` Key -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Key
    bits2) Key
    bits2
                           | (Key
    bmask Key -> Int -> Key
    `shiftRL` Key -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Key
    bits2) Key -> Key -> Key
    forall a. Bits a => a -> a -> a
    .&. ((Key
    1 Key -> Int -> Key
    `shiftLL` Key -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Key
    bits2) Key -> Key -> Key
    forall a. Num a => a -> a -> a
    - Key
    1) Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    == Key
    0 ->
                               (Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    g Key
    prefix Key
    bmask Key
    bits2
                           | Bool
    otherwise ->
                               Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    Bin Key
    prefix Key
    bits2 ((Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    g Key
    prefix Key
    bmask Key
    bits2) ((Key -> a) -> Key -> Key -> Key -> Word64Map a
    buildTree Key -> a
    g (Key
    prefix Key -> Key -> Key
    forall a. Num a => a -> a -> a
    + Key
    bits2) (Key
    bmask Key -> Int -> Key
    `shiftRL` Key -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Key
    bits2) Key
    bits2)

    {--------------------------------------------------------------------
      Lists
    --------------------------------------------------------------------}
    -- | \(O(n \min(n,W))\). Create a map from a list of key\/value pairs.
    --
    -- > fromList [] == empty
    -- > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
    -- > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]

    fromList :: [(Key,a)] -> Word64Map a
    fromList :: forall a. [(Key, a)] -> Word64Map a
    fromList [(Key, a)]
    xs
      = (Word64Map a -> (Key, a) -> Word64Map a)
    -> Word64Map a -> [(Key, a)] -> Word64Map a
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    Foldable.foldl' Word64Map a -> (Key, a) -> Word64Map a
    forall {a}. Word64Map a -> (Key, a) -> Word64Map a
    ins Word64Map a
    forall a. Word64Map a
    empty [(Key, a)]
    xs
      where
        ins :: Word64Map a -> (Key, a) -> Word64Map a
    ins Word64Map a
    t (Key
    k,a
    x)  = Key -> a -> Word64Map a -> Word64Map a
    forall a. Key -> a -> Word64Map a -> Word64Map a
    insert Key
    k a
    x Word64Map a
    t

    -- | \(O(n \min(n,W))\). Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'.
    --
    -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
    -- > fromListWith (++) [] == empty

    fromListWith :: (a -> a -> a) -> [(Key,a)] -> Word64Map a
    fromListWith :: forall a. (a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromListWith a -> a -> a
    f [(Key, a)]
    xs
      = (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a. (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromListWithKey (\Key
    _ a
    x a
    y -> a -> a -> a
    f a
    x a
    y) [(Key, a)]
    xs

    -- | \(O(n \min(n,W))\). Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'.
    --
    -- > let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
    -- > fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
    -- > fromListWithKey f [] == empty

    fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> Word64Map a
    fromListWithKey :: forall a. (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromListWithKey Key -> a -> a -> a
    f [(Key, a)]
    xs
      = (Word64Map a -> (Key, a) -> Word64Map a)
    -> Word64Map a -> [(Key, a)] -> Word64Map a
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    Foldable.foldl' Word64Map a -> (Key, a) -> Word64Map a
    ins Word64Map a
    forall a. Word64Map a
    empty [(Key, a)]
    xs
      where
        ins :: Word64Map a -> (Key, a) -> Word64Map a
    ins Word64Map a
    t (Key
    k,a
    x) = (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    forall a.
    (Key -> a -> a -> a) -> Key -> a -> Word64Map a -> Word64Map a
    insertWithKey Key -> a -> a -> a
    f Key
    k a
    x Word64Map a
    t

    -- | \(O(n)\). Build a map from a list of key\/value pairs where
    -- the keys are in ascending order.
    --
    -- > fromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
    -- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]

    fromAscList :: [(Key,a)] -> Word64Map a
    fromAscList :: forall a. [(Key, a)] -> Word64Map a
    fromAscList = Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a.
    Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromMonoListWithKey Distinct
    Nondistinct (\Key
    _ a
    x a
    _ -> a
    x)
    {-# NOINLINE fromAscList #-}

    -- | \(O(n)\). Build a map from a list of key\/value pairs where
    -- the keys are in ascending order, with a combining function on equal keys.
    -- /The precondition (input list is ascending) is not checked./
    --
    -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]

    fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> Word64Map a
    fromAscListWith :: forall a. (a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromAscListWith a -> a -> a
    f = Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a.
    Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromMonoListWithKey Distinct
    Nondistinct (\Key
    _ a
    x a
    y -> a -> a -> a
    f a
    x a
    y)
    {-# NOINLINE fromAscListWith #-}

    -- | \(O(n)\). Build a map from a list of key\/value pairs where
    -- the keys are in ascending order, with a combining function on equal keys.
    -- /The precondition (input list is ascending) is not checked./
    --
    -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]

    fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> Word64Map a
    fromAscListWithKey :: forall a. (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromAscListWithKey Key -> a -> a -> a
    f = Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a.
    Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromMonoListWithKey Distinct
    Nondistinct Key -> a -> a -> a
    f
    {-# NOINLINE fromAscListWithKey #-}

    -- | \(O(n)\). Build a map from a list of key\/value pairs where
    -- the keys are in ascending order and all distinct.
    -- /The precondition (input list is strictly ascending) is not checked./
    --
    -- > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]

    fromDistinctAscList :: [(Key,a)] -> Word64Map a
    fromDistinctAscList :: forall a. [(Key, a)] -> Word64Map a
    fromDistinctAscList = Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    forall a.
    Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromMonoListWithKey Distinct
    Distinct (\Key
    _ a
    x a
    _ -> a
    x)
    {-# NOINLINE fromDistinctAscList #-}

    -- | \(O(n)\). Build a map from a list of key\/value pairs with monotonic keys
    -- and a combining function.
    --
    -- The precise conditions under which this function works are subtle:
    -- For any branch mask, keys with the same prefix w.r.t. the branch
    -- mask must occur consecutively in the list.

    fromMonoListWithKey :: Distinct -> (Key -> a -> a -> a) -> [(Key,a)] -> Word64Map a
    fromMonoListWithKey :: forall a.
    Distinct -> (Key -> a -> a -> a) -> [(Key, a)] -> Word64Map a
    fromMonoListWithKey Distinct
    distinct Key -> a -> a -> a
    f = [(Key, a)] -> Word64Map a
    go
      where
        go :: [(Key, a)] -> Word64Map a
    go []              = Word64Map a
    forall a. Word64Map a
    Nil
        go ((Key
    kx,a
    vx) : [(Key, a)]
    zs1) = Key -> a -> [(Key, a)] -> Word64Map a
    addAll' Key
    kx a
    vx [(Key, a)]
    zs1

        -- `addAll'` collects all keys equal to `kx` into a single value,
        -- and then proceeds with `addAll`.
        addAll' :: Key -> a -> [(Key, a)] -> Word64Map a
    addAll' !Key
    kx a
    vx []
            = Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    kx (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! a
    vx
        addAll' !Key
    kx a
    vx ((Key
    ky,a
    vy) : [(Key, a)]
    zs)
            | Distinct
    Nondistinct <- Distinct
    distinct, Key
    kx Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    == Key
    ky
            = let !v :: a
    v = Key -> a -> a -> a
    f Key
    kx a
    vy a
    vx in Key -> a -> [(Key, a)] -> Word64Map a
    addAll' Key
    ky a
    v [(Key, a)]
    zs
            -- inlined: | otherwise = addAll kx (Tip kx $! vx) (ky : zs)
            | Key
    m <- Key -> Key -> Key
    branchMask Key
    kx Key
    ky
            , Inserted Word64Map a
    ty [(Key, a)]
    zs' <- Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' Key
    m Key
    ky a
    vy [(Key, a)]
    zs
            = Key -> Word64Map a -> [(Key, a)] -> Word64Map a
    addAll Key
    kx (Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    linkWithMask Key
    m Key
    ky Word64Map a
    ty {-kx-} (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    kx (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! a
    vx)) [(Key, a)]
    zs'

        -- for `addAll` and `addMany`, kx is /a/ key inside the tree `tx`
        -- `addAll` consumes the rest of the list, adding to the tree `tx`
        addAll :: Key -> Word64Map a -> [(Key, a)] -> Word64Map a
    addAll !Key
    _kx !Word64Map a
    tx []
            = Word64Map a
    tx
        addAll !Key
    kx !Word64Map a
    tx ((Key
    ky,a
    vy) : [(Key, a)]
    zs)
            | Key
    m <- Key -> Key -> Key
    branchMask Key
    kx Key
    ky
            , Inserted Word64Map a
    ty [(Key, a)]
    zs' <- Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' Key
    m Key
    ky a
    vy [(Key, a)]
    zs
            = Key -> Word64Map a -> [(Key, a)] -> Word64Map a
    addAll Key
    kx (Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    linkWithMask Key
    m Key
    ky Word64Map a
    ty {-kx-} Word64Map a
    tx) [(Key, a)]
    zs'

        -- `addMany'` is similar to `addAll'`, but proceeds with `addMany'`.
        addMany' :: Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' !Key
    _m !Key
    kx a
    vx []
            = Word64Map a -> [(Key, a)] -> Inserted a
    forall a. Word64Map a -> [(Key, a)] -> Inserted a
    Inserted (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    kx (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! a
    vx) []
        addMany' !Key
    m !Key
    kx a
    vx zs0 :: [(Key, a)]
    zs0@((Key
    ky,a
    vy) : [(Key, a)]
    zs)
            | Distinct
    Nondistinct <- Distinct
    distinct, Key
    kx Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    == Key
    ky
            = let !v :: a
    v = Key -> a -> a -> a
    f Key
    kx a
    vy a
    vx in Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' Key
    m Key
    ky a
    v [(Key, a)]
    zs
            -- inlined: | otherwise = addMany m kx (Tip kx $! vx) (ky : zs)
            | Key -> Key -> Key
    mask Key
    kx Key
    m Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Key -> Key -> Key
    mask Key
    ky Key
    m
            = Word64Map a -> [(Key, a)] -> Inserted a
    forall a. Word64Map a -> [(Key, a)] -> Inserted a
    Inserted (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    kx (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! a
    vx) [(Key, a)]
    zs0
            | Key
    mxy <- Key -> Key -> Key
    branchMask Key
    kx Key
    ky
            , Inserted Word64Map a
    ty [(Key, a)]
    zs' <- Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' Key
    mxy Key
    ky a
    vy [(Key, a)]
    zs
            = Key -> Key -> Word64Map a -> [(Key, a)] -> Inserted a
    addMany Key
    m Key
    kx (Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    linkWithMask Key
    mxy Key
    ky Word64Map a
    ty {-kx-} (Key -> a -> Word64Map a
    forall a. Key -> a -> Word64Map a
    Tip Key
    kx (a -> Word64Map a) -> a -> Word64Map a
    forall a b. (a -> b) -> a -> b
    $! a
    vx)) [(Key, a)]
    zs'

        -- `addAll` adds to `tx` all keys whose prefix w.r.t. `m` agrees with `kx`.
        addMany :: Key -> Key -> Word64Map a -> [(Key, a)] -> Inserted a
    addMany !Key
    _m !Key
    _kx Word64Map a
    tx []
            = Word64Map a -> [(Key, a)] -> Inserted a
    forall a. Word64Map a -> [(Key, a)] -> Inserted a
    Inserted Word64Map a
    tx []
        addMany !Key
    m !Key
    kx Word64Map a
    tx zs0 :: [(Key, a)]
    zs0@((Key
    ky,a
    vy) : [(Key, a)]
    zs)
            | Key -> Key -> Key
    mask Key
    kx Key
    m Key -> Key -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Key -> Key -> Key
    mask Key
    ky Key
    m
            = Word64Map a -> [(Key, a)] -> Inserted a
    forall a. Word64Map a -> [(Key, a)] -> Inserted a
    Inserted Word64Map a
    tx [(Key, a)]
    zs0
            | Key
    mxy <- Key -> Key -> Key
    branchMask Key
    kx Key
    ky
            , Inserted Word64Map a
    ty [(Key, a)]
    zs' <- Key -> Key -> a -> [(Key, a)] -> Inserted a
    addMany' Key
    mxy Key
    ky a
    vy [(Key, a)]
    zs
            = Key -> Key -> Word64Map a -> [(Key, a)] -> Inserted a
    addMany Key
    m Key
    kx (Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    forall a. Key -> Key -> Word64Map a -> Word64Map a -> Word64Map a
    linkWithMask Key
    mxy Key
    ky Word64Map a
    ty {-kx-} Word64Map a
    tx) [(Key, a)]
    zs'
    {-# INLINE fromMonoListWithKey #-}

    data Inserted a = Inserted !(Word64Map a) ![(Key,a)]

    data Distinct = Distinct | Nondistinct