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

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


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

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


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


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

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

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

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

    -- * Construction
    , empty
    , singleton

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

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

    -- * Combine

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

    -- ** Difference
    , difference
    , differenceWith
    , differenceWithKey

    -- ** Intersection
    , intersection
    , intersectionWith
    , intersectionWithKey

    -- ** Compose
    , compose

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

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

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

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

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

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

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

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

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

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

    , takeWhileAntitone
    , dropWhileAntitone
    , spanAntitone

    , mapMaybe
    , mapMaybeWithKey
    , mapEither
    , mapEitherWithKey

    , split
    , splitLookup
    , splitRoot

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

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

    -- * Debugging
    , showTree
    , showTreeWith

    -- * Internal types
    , Mask, Prefix, Nat

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

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

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

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

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

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

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


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

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

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

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


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

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

type Prefix = Word64
type Mask   = Word64


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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


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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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


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


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

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


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


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


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


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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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

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


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


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


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


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


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

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

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

    go Word64Map a
t1' (Tip Word64
k2 b
x2) = Word64Map a -> f (Word64Map c)
merge1 Word64Map a
t1'
      where
        merge1 :: Word64Map a -> f (Word64Map c)
merge1 t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1)
          | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k2 Word64
p1 Word64
m1 = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1) Word64
k2 ((Word64 -> b -> f (Maybe c)) -> Word64 -> b -> f (Word64Map c)
forall {f :: * -> *} {t} {a}.
Functor f =>
(Word64 -> t -> f (Maybe a)) -> Word64 -> t -> f (Word64Map a)
subsingletonBy Word64 -> b -> f (Maybe c)
g2k Word64
k2 b
x2)
          | Word64 -> Word64 -> Bool
zero Word64
k2 Word64
m1       = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p1 Word64
m1 (Word64Map a -> f (Word64Map c)
merge1 Word64Map a
l1) (Word64Map a -> f (Word64Map c)
g1t Word64Map a
r1)
          | Bool
otherwise        = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p1 Word64
m1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
l1) (Word64Map a -> f (Word64Map c)
merge1 Word64Map a
r1)
        merge1 (Tip Word64
k1 a
x1)   = Word64 -> a -> Word64 -> b -> f (Word64Map c)
mergeTips Word64
k1 a
x1 Word64
k2 b
x2
        merge1 Word64Map a
Nil           = (Word64 -> b -> f (Maybe c)) -> Word64 -> b -> f (Word64Map c)
forall {f :: * -> *} {t} {a}.
Functor f =>
(Word64 -> t -> f (Maybe a)) -> Word64 -> t -> f (Word64Map a)
subsingletonBy Word64 -> b -> f (Maybe c)
g2k Word64
k2 b
x2

    go t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) t2 :: Word64Map b
t2@(Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
      | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = f (Word64Map c)
merge1
      | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = f (Word64Map c)
merge2
      | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2       = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p1 Word64
m1 (Word64Map a -> Word64Map b -> f (Word64Map c)
go Word64Map a
l1 Word64Map b
l2) (Word64Map a -> Word64Map b -> f (Word64Map c)
go Word64Map a
r1 Word64Map b
r2)
      | Bool
otherwise      = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1) Word64
p2 (Word64Map b -> f (Word64Map c)
g2t Word64Map b
t2)
      where
        merge1 :: f (Word64Map c)
merge1 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p2 Word64
p1 Word64
m1  = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1) Word64
p2 (Word64Map b -> f (Word64Map c)
g2t Word64Map b
t2)
               | Word64 -> Word64 -> Bool
zero Word64
p2 Word64
m1        = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p1 Word64
m1 (Word64Map a -> Word64Map b -> f (Word64Map c)
go  Word64Map a
l1 Word64Map b
t2) (Word64Map a -> f (Word64Map c)
g1t Word64Map a
r1)
               | Bool
otherwise         = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p1 Word64
m1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
l1)    (Word64Map a -> Word64Map b -> f (Word64Map c)
go  Word64Map a
r1 Word64Map b
t2)
        merge2 :: f (Word64Map c)
merge2 | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2  = Word64
-> f (Word64Map c) -> Word64 -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 (Word64Map a -> f (Word64Map c)
g1t Word64Map a
t1) Word64
p2 (Word64Map b -> f (Word64Map c)
g2t Word64Map b
t2)
               | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2        = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p2 Word64
m2 (Word64Map a -> Word64Map b -> f (Word64Map c)
go  Word64Map a
t1 Word64Map b
l2) (Word64Map b -> f (Word64Map c)
g2t    Word64Map b
r2)
               | Bool
otherwise         = Word64
-> Word64 -> f (Word64Map c) -> f (Word64Map c) -> f (Word64Map c)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p2 Word64
m2 (Word64Map b -> f (Word64Map c)
g2t    Word64Map b
l2) (Word64Map a -> Word64Map b -> f (Word64Map c)
go  Word64Map a
t1 Word64Map b
r2)

    subsingletonBy :: (Word64 -> t -> f (Maybe a)) -> Word64 -> t -> f (Word64Map a)
subsingletonBy Word64 -> t -> f (Maybe a)
gk Word64
k t
x = Word64Map a -> (a -> Word64Map a) -> Maybe a -> Word64Map a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map a
forall a. Word64Map a
Nil (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k) (Maybe a -> Word64Map a) -> f (Maybe a) -> f (Word64Map a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> t -> f (Maybe a)
gk Word64
k t
x
    {-# INLINE subsingletonBy #-}

    mergeTips :: Word64 -> a -> Word64 -> b -> f (Word64Map c)
mergeTips Word64
k1 a
x1 Word64
k2 b
x2
      | Word64
k1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
k2  = Word64Map c -> (c -> Word64Map c) -> Maybe c -> Word64Map c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map c
forall a. Word64Map a
Nil (Word64 -> c -> Word64Map c
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1) (Maybe c -> Word64Map c) -> f (Maybe c) -> f (Word64Map c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> a -> b -> f (Maybe c)
f Word64
k1 a
x1 b
x2
      | Word64
k1 Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<  Word64
k2  = (Maybe c -> Maybe c -> Word64Map c)
-> f (Maybe c) -> f (Maybe c) -> f (Word64Map c)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Word64 -> Word64 -> Maybe c -> Maybe c -> Word64Map c
forall {a}. Word64 -> Word64 -> Maybe a -> Maybe a -> Word64Map a
subdoubleton Word64
k1 Word64
k2) (Word64 -> a -> f (Maybe c)
g1k Word64
k1 a
x1) (Word64 -> b -> f (Maybe c)
g2k Word64
k2 b
x2)
        {-
        = link_ k1 k2 <$> subsingletonBy g1k k1 x1 <*> subsingletonBy g2k k2 x2
        -}
      | Bool
otherwise = (Maybe c -> Maybe c -> Word64Map c)
-> f (Maybe c) -> f (Maybe c) -> f (Word64Map c)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Word64 -> Word64 -> Maybe c -> Maybe c -> Word64Map c
forall {a}. Word64 -> Word64 -> Maybe a -> Maybe a -> Word64Map a
subdoubleton Word64
k2 Word64
k1) (Word64 -> b -> f (Maybe c)
g2k Word64
k2 b
x2) (Word64 -> a -> f (Maybe c)
g1k Word64
k1 a
x1)
    {-# INLINE mergeTips #-}

    subdoubleton :: Word64 -> Word64 -> Maybe a -> Maybe a -> Word64Map a
subdoubleton Word64
_ Word64
_   Maybe a
Nothing Maybe a
Nothing     = Word64Map a
forall a. Word64Map a
Nil
    subdoubleton Word64
_ Word64
k2  Maybe a
Nothing (Just a
y2)   = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k2 a
y2
    subdoubleton Word64
k1 Word64
_  (Just a
y1) Maybe a
Nothing   = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1 a
y1
    subdoubleton Word64
k1 Word64
k2 (Just a
y1) (Just a
y2) = Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
k1 (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k1 a
y1) Word64
k2 (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k2 a
y2)
    {-# INLINE subdoubleton #-}

    -- A variant of 'link_' which makes sure to execute side-effects
    -- in the right order.
    linkA
        :: Applicative f
        => Prefix -> f (Word64Map a)
        -> Prefix -> f (Word64Map a)
        -> f (Word64Map a)
    linkA :: forall (f :: * -> *) a.
Applicative f =>
Word64
-> f (Word64Map a) -> Word64 -> f (Word64Map a) -> f (Word64Map a)
linkA Word64
p1 f (Word64Map a)
t1 Word64
p2 f (Word64Map a)
t2
      | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m = Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p Word64
m f (Word64Map a)
t1 f (Word64Map a)
t2
      | Bool
otherwise = Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p Word64
m f (Word64Map a)
t2 f (Word64Map a)
t1
      where
        m :: Word64
m = Word64 -> Word64 -> Word64
branchMask Word64
p1 Word64
p2
        p :: Word64
p = Word64 -> Word64 -> Word64
mask Word64
p1 Word64
m
    {-# INLINE linkA #-}

    -- A variant of 'bin' that ensures that effects for negative keys are executed
    -- first.
    binA
        :: Applicative f
        => Prefix
        -> Mask
        -> f (Word64Map a)
        -> f (Word64Map a)
        -> f (Word64Map a)
    binA :: forall (f :: * -> *) a.
Applicative f =>
Word64
-> Word64 -> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
binA Word64
p Word64
m f (Word64Map a)
a f (Word64Map a)
b
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = (Word64Map a -> Word64Map a -> Word64Map a)
-> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> Word64Map a -> Word64Map a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m)) f (Word64Map a)
b f (Word64Map a)
a
      | Bool
otherwise = (Word64Map a -> Word64Map a -> Word64Map a)
-> f (Word64Map a) -> f (Word64Map a) -> f (Word64Map a)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2       (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m)  f (Word64Map a)
a f (Word64Map a)
b
    {-# INLINE binA #-}
{-# INLINE mergeA #-}


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

-- | \(O(\min(n,W))\). 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. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
updateMinWithKey Word64 -> a -> Maybe a
f Word64Map a
t =
  case Word64Map a
t of Bin Word64
p Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l ((Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
go Word64 -> a -> Maybe a
f Word64Map a
r)
            Word64Map a
_ -> (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
go Word64 -> a -> Maybe a
f Word64Map a
t
  where
    go :: (Word64 -> t -> Maybe t) -> Word64Map t -> Word64Map t
go Word64 -> t -> Maybe t
f' (Bin Word64
p Word64
m Word64Map t
l Word64Map t
r) = Word64 -> Word64 -> Word64Map t -> Word64Map t -> Word64Map t
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m ((Word64 -> t -> Maybe t) -> Word64Map t -> Word64Map t
go Word64 -> t -> Maybe t
f' Word64Map t
l) Word64Map t
r
    go Word64 -> t -> Maybe t
f' (Tip Word64
k t
y) = case Word64 -> t -> Maybe t
f' Word64
k t
y of
                        Just t
y' -> Word64 -> t -> Word64Map t
forall a. Word64 -> a -> Word64Map a
Tip Word64
k t
y'
                        Maybe t
Nothing -> Word64Map t
forall a. Word64Map a
Nil
    go Word64 -> t -> Maybe t
_ Word64Map t
Nil = [Char] -> Word64Map t
forall a. HasCallStack => [Char] -> a
error [Char]
"updateMinWithKey Nil"

-- | \(O(\min(n,W))\). 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. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
updateMaxWithKey Word64 -> a -> Maybe a
f Word64Map a
t =
  case Word64Map a
t of Bin Word64
p Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m ((Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
go Word64 -> a -> Maybe a
f Word64Map a
l) Word64Map a
r
            Word64Map a
_ -> (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
go Word64 -> a -> Maybe a
f Word64Map a
t
  where
    go :: (Word64 -> t -> Maybe t) -> Word64Map t -> Word64Map t
go Word64 -> t -> Maybe t
f' (Bin Word64
p Word64
m Word64Map t
l Word64Map t
r) = Word64 -> Word64 -> Word64Map t -> Word64Map t -> Word64Map t
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map t
l ((Word64 -> t -> Maybe t) -> Word64Map t -> Word64Map t
go Word64 -> t -> Maybe t
f' Word64Map t
r)
    go Word64 -> t -> Maybe t
f' (Tip Word64
k t
y) = case Word64 -> t -> Maybe t
f' Word64
k t
y of
                        Just t
y' -> Word64 -> t -> Word64Map t
forall a. Word64 -> a -> Word64Map a
Tip Word64
k t
y'
                        Maybe t
Nothing -> Word64Map t
forall a. Word64Map a
Nil
    go Word64 -> t -> Maybe t
_ Word64Map t
Nil = [Char] -> Word64Map t
forall a. HasCallStack => [Char] -> a
error [Char]
"updateMaxWithKey Nil"


data View a = View {-# UNPACK #-} !Key a !(Word64Map a)

-- | \(O(\min(n,W))\). Retrieves the maximal (key,value) pair of the map, and
-- the map stripped of that element, or 'Nothing' if passed an empty map.
--
-- > maxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
-- > maxViewWithKey empty == Nothing

maxViewWithKey :: Word64Map a -> Maybe ((Key, a), Word64Map a)
maxViewWithKey :: forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
maxViewWithKey Word64Map a
t = case Word64Map a
t of
  Word64Map a
Nil -> Maybe ((Word64, a), Word64Map a)
forall a. Maybe a
Nothing
  Word64Map a
_ -> ((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a)
forall a. a -> Maybe a
Just (((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a))
-> ((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a)
forall a b. (a -> b) -> a -> b
$ case Word64Map a -> View a
forall a. Word64Map a -> View a
maxViewWithKeySure Word64Map a
t of
                View Word64
k a
v Word64Map a
t' -> ((Word64
k, a
v), Word64Map a
t')
{-# INLINE maxViewWithKey #-}

maxViewWithKeySure :: Word64Map a -> View a
maxViewWithKeySure :: forall a. Word64Map a -> View a
maxViewWithKeySure Word64Map a
t =
  case Word64Map a
t of
    Word64Map a
Nil -> [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"maxViewWithKeySure Nil"
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
      case Word64Map a -> View a
forall a. Word64Map a -> View a
go Word64Map a
l of View Word64
k a
a Word64Map a
l' -> Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
a (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m Word64Map a
l' Word64Map a
r)
    Word64Map a
_ -> Word64Map a -> View a
forall a. Word64Map a -> View a
go Word64Map a
t
  where
    go :: Word64Map a -> View a
go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) =
        case Word64Map a -> View a
go Word64Map a
r of View Word64
k a
a Word64Map a
r' -> Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
a (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l Word64Map a
r')
    go (Tip Word64
k a
y) = Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
y Word64Map a
forall a. Word64Map a
Nil
    go Word64Map a
Nil = [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"maxViewWithKey_go Nil"
-- See note on NOINLINE at minViewWithKeySure
{-# NOINLINE maxViewWithKeySure #-}

-- | \(O(\min(n,W))\). Retrieves the minimal (key,value) pair of the map, and
-- the map stripped of that element, or 'Nothing' if passed an empty map.
--
-- > minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
-- > minViewWithKey empty == Nothing

minViewWithKey :: Word64Map a -> Maybe ((Key, a), Word64Map a)
minViewWithKey :: forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
minViewWithKey Word64Map a
t =
  case Word64Map a
t of
    Word64Map a
Nil -> Maybe ((Word64, a), Word64Map a)
forall a. Maybe a
Nothing
    Word64Map a
_ -> ((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a)
forall a. a -> Maybe a
Just (((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a))
-> ((Word64, a), Word64Map a) -> Maybe ((Word64, a), Word64Map a)
forall a b. (a -> b) -> a -> b
$ case Word64Map a -> View a
forall a. Word64Map a -> View a
minViewWithKeySure Word64Map a
t of
                  View Word64
k a
v Word64Map a
t' -> ((Word64
k, a
v), Word64Map a
t')
-- We inline this to give GHC the best possible chance of
-- getting rid of the Maybe, pair, and Int constructors, as
-- well as a thunk under the Just. That is, we really want to
-- be certain this inlines!
{-# INLINE minViewWithKey #-}

minViewWithKeySure :: Word64Map a -> View a
minViewWithKeySure :: forall a. Word64Map a -> View a
minViewWithKeySure Word64Map a
t =
  case Word64Map a
t of
    Word64Map a
Nil -> [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"minViewWithKeySure Nil"
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
      case Word64Map a -> View a
forall a. Word64Map a -> View a
go Word64Map a
r of
        View Word64
k a
a Word64Map a
r' -> Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
a (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
p Word64
m Word64Map a
l Word64Map a
r')
    Word64Map a
_ -> Word64Map a -> View a
forall a. Word64Map a -> View a
go Word64Map a
t
  where
    go :: Word64Map a -> View a
go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) =
        case Word64Map a -> View a
go Word64Map a
l of View Word64
k a
a Word64Map a
l' -> Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
a (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
p Word64
m Word64Map a
l' Word64Map a
r)
    go (Tip Word64
k a
y) = Word64 -> a -> Word64Map a -> View a
forall a. Word64 -> a -> Word64Map a -> View a
View Word64
k a
y Word64Map a
forall a. Word64Map a
Nil
    go Word64Map a
Nil = [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"minViewWithKey_go Nil"
-- There's never anything significant to be gained by inlining
-- this. Sufficiently recent GHC versions will inline the wrapper
-- anyway, which should be good enough.
{-# NOINLINE minViewWithKeySure #-}

-- | \(O(\min(n,W))\). 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 = (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
updateMaxWithKey ((a -> Maybe a) -> Word64 -> a -> Maybe a
forall a b. a -> b -> a
const a -> Maybe a
f)

-- | \(O(\min(n,W))\). 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 = (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Maybe a) -> Word64Map a -> Word64Map a
updateMinWithKey ((a -> Maybe a) -> Word64 -> a -> Maybe a
forall a b. a -> b -> a
const a -> Maybe a
f)

-- | \(O(\min(n,W))\). Retrieves the maximal key of the map, and the map
-- stripped of that element, or 'Nothing' if passed an empty map.
maxView :: Word64Map a -> Maybe (a, Word64Map a)
maxView :: forall a. Word64Map a -> Maybe (a, Word64Map a)
maxView Word64Map a
t = (((Word64, a), Word64Map a) -> (a, Word64Map a))
-> Maybe ((Word64, a), Word64Map a) -> Maybe (a, Word64Map a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\((Word64
_, a
x), Word64Map a
t') -> (a
x, Word64Map a
t')) (Word64Map a -> Maybe ((Word64, a), Word64Map a)
forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
maxViewWithKey Word64Map a
t)

-- | \(O(\min(n,W))\). Retrieves the minimal key of the map, and the map
-- stripped of that element, or 'Nothing' if passed an empty map.
minView :: Word64Map a -> Maybe (a, Word64Map a)
minView :: forall a. Word64Map a -> Maybe (a, Word64Map a)
minView Word64Map a
t = (((Word64, a), Word64Map a) -> (a, Word64Map a))
-> Maybe ((Word64, a), Word64Map a) -> Maybe (a, Word64Map a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\((Word64
_, a
x), Word64Map a
t') -> (a
x, Word64Map a
t')) (Word64Map a -> Maybe ((Word64, a), Word64Map a)
forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
minViewWithKey Word64Map a
t)

-- | \(O(\min(n,W))\). Delete and find the maximal element.
-- This function throws an error if the map is empty. Use 'maxViewWithKey'
-- if the map may be empty.
deleteFindMax :: Word64Map a -> ((Key, a), Word64Map a)
deleteFindMax :: forall a. Word64Map a -> ((Word64, a), Word64Map a)
deleteFindMax = ((Word64, a), Word64Map a)
-> Maybe ((Word64, a), Word64Map a) -> ((Word64, a), Word64Map a)
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> ((Word64, a), Word64Map a)
forall a. HasCallStack => [Char] -> a
error [Char]
"deleteFindMax: empty map has no maximal element") (Maybe ((Word64, a), Word64Map a) -> ((Word64, a), Word64Map a))
-> (Word64Map a -> Maybe ((Word64, a), Word64Map a))
-> Word64Map a
-> ((Word64, a), Word64Map a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64Map a -> Maybe ((Word64, a), Word64Map a)
forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
maxViewWithKey

-- | \(O(\min(n,W))\). Delete and find the minimal element.
-- This function throws an error if the map is empty. Use 'minViewWithKey'
-- if the map may be empty.
deleteFindMin :: Word64Map a -> ((Key, a), Word64Map a)
deleteFindMin :: forall a. Word64Map a -> ((Word64, a), Word64Map a)
deleteFindMin = ((Word64, a), Word64Map a)
-> Maybe ((Word64, a), Word64Map a) -> ((Word64, a), Word64Map a)
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> ((Word64, a), Word64Map a)
forall a. HasCallStack => [Char] -> a
error [Char]
"deleteFindMin: empty map has no minimal element") (Maybe ((Word64, a), Word64Map a) -> ((Word64, a), Word64Map a))
-> (Word64Map a -> Maybe ((Word64, a), Word64Map a))
-> Word64Map a
-> ((Word64, a), Word64Map a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64Map a -> Maybe ((Word64, a), Word64Map a)
forall a. Word64Map a -> Maybe ((Word64, a), Word64Map a)
minViewWithKey

-- | \(O(\min(n,W))\). The minimal key of the map. Returns 'Nothing' if the map is empty.
lookupMin :: Word64Map a -> Maybe (Key, a)
lookupMin :: forall a. Word64Map a -> Maybe (Word64, a)
lookupMin Word64Map a
Nil = Maybe (Word64, a)
forall a. Maybe a
Nothing
lookupMin (Tip Word64
k a
v) = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
k,a
v)
lookupMin (Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r)
  | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
go Word64Map a
r
  | Bool
otherwise = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
go Word64Map a
l
    where go :: Word64Map b -> Maybe (Word64, b)
go (Tip Word64
k b
v)      = (Word64, b) -> Maybe (Word64, b)
forall a. a -> Maybe a
Just (Word64
k,b
v)
          go (Bin Word64
_ Word64
_ Word64Map b
l' Word64Map b
_) = Word64Map b -> Maybe (Word64, b)
go Word64Map b
l'
          go Word64Map b
Nil            = Maybe (Word64, b)
forall a. Maybe a
Nothing

-- | \(O(\min(n,W))\). The minimal key of the map. Calls 'error' if the map is empty.
-- Use 'minViewWithKey' if the map may be empty.
findMin :: Word64Map a -> (Key, a)
findMin :: forall a. Word64Map a -> (Word64, a)
findMin Word64Map a
t
  | Just (Word64, a)
r <- Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
lookupMin Word64Map a
t = (Word64, a)
r
  | Bool
otherwise = [Char] -> (Word64, a)
forall a. HasCallStack => [Char] -> a
error [Char]
"findMin: empty map has no minimal element"

-- | \(O(\min(n,W))\). The maximal key of the map. Returns 'Nothing' if the map is empty.
lookupMax :: Word64Map a -> Maybe (Key, a)
lookupMax :: forall a. Word64Map a -> Maybe (Word64, a)
lookupMax Word64Map a
Nil = Maybe (Word64, a)
forall a. Maybe a
Nothing
lookupMax (Tip Word64
k a
v) = (Word64, a) -> Maybe (Word64, a)
forall a. a -> Maybe a
Just (Word64
k,a
v)
lookupMax (Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r)
  | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
go Word64Map a
l
  | Bool
otherwise = Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
go Word64Map a
r
    where go :: Word64Map b -> Maybe (Word64, b)
go (Tip Word64
k b
v)      = (Word64, b) -> Maybe (Word64, b)
forall a. a -> Maybe a
Just (Word64
k,b
v)
          go (Bin Word64
_ Word64
_ Word64Map b
_ Word64Map b
r') = Word64Map b -> Maybe (Word64, b)
go Word64Map b
r'
          go Word64Map b
Nil            = Maybe (Word64, b)
forall a. Maybe a
Nothing

-- | \(O(\min(n,W))\). The maximal key of the map. Calls 'error' if the map is empty.
-- Use 'maxViewWithKey' if the map may be empty.
findMax :: Word64Map a -> (Key, a)
findMax :: forall a. Word64Map a -> (Word64, a)
findMax Word64Map a
t
  | Just (Word64, a)
r <- Word64Map a -> Maybe (Word64, a)
forall a. Word64Map a -> Maybe (Word64, a)
lookupMax Word64Map a
t = (Word64, a)
r
  | Bool
otherwise = [Char] -> (Word64, a)
forall a. HasCallStack => [Char] -> a
error [Char]
"findMax: empty map has no maximal element"

-- | \(O(\min(n,W))\). Delete the minimal key. Returns an empty map if the map is empty.
--
-- Note that this is a change of behaviour for consistency with 'Data.Map.Map' &#8211;
-- versions prior to 0.5 threw an error if the 'Word64Map' was already empty.
deleteMin :: Word64Map a -> Word64Map a
deleteMin :: forall a. Word64Map a -> Word64Map a
deleteMin = Word64Map a
-> ((a, Word64Map a) -> Word64Map a)
-> Maybe (a, Word64Map a)
-> Word64Map a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map a
forall a. Word64Map a
Nil (a, Word64Map a) -> Word64Map a
forall a b. (a, b) -> b
snd (Maybe (a, Word64Map a) -> Word64Map a)
-> (Word64Map a -> Maybe (a, Word64Map a))
-> Word64Map a
-> Word64Map a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64Map a -> Maybe (a, Word64Map a)
forall a. Word64Map a -> Maybe (a, Word64Map a)
minView

-- | \(O(\min(n,W))\). Delete the maximal key. Returns an empty map if the map is empty.
--
-- Note that this is a change of behaviour for consistency with 'Data.Map.Map' &#8211;
-- versions prior to 0.5 threw an error if the 'Word64Map' was already empty.
deleteMax :: Word64Map a -> Word64Map a
deleteMax :: forall a. Word64Map a -> Word64Map a
deleteMax = Word64Map a
-> ((a, Word64Map a) -> Word64Map a)
-> Maybe (a, Word64Map a)
-> Word64Map a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64Map a
forall a. Word64Map a
Nil (a, Word64Map a) -> Word64Map a
forall a b. (a, b) -> b
snd (Maybe (a, Word64Map a) -> Word64Map a)
-> (Word64Map a -> Maybe (a, Word64Map a))
-> Word64Map a
-> Word64Map a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64Map a -> Maybe (a, Word64Map a)
forall a. Word64Map a -> Maybe (a, Word64Map a)
maxView


{--------------------------------------------------------------------
  Submap
--------------------------------------------------------------------}
-- | \(O(n+m)\). Is this a proper submap? (ie. a submap but not equal).
-- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@).
isProperSubmapOf :: Eq a => Word64Map a -> Word64Map a -> Bool
isProperSubmapOf :: forall a. Eq a => Word64Map a -> Word64Map a -> Bool
isProperSubmapOf Word64Map a
m1 Word64Map a
m2
  = (a -> a -> Bool) -> Word64Map a -> Word64Map a -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isProperSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Word64Map a
m1 Word64Map a
m2

{- | \(O(n+m)\). Is this a proper submap? (ie. a submap but not equal).
 The expression (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when
 @keys m1@ and @keys m2@ are not equal,
 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
 applied to their respective values. For example, the following
 expressions are all 'True':

  > isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
  > isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])

 But the following are all 'False':

  > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
  > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
  > isProperSubmapOfBy (<)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
-}
isProperSubmapOfBy :: (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isProperSubmapOfBy a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
t2
  = case (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
t2 of
      Ordering
LT -> Bool
True
      Ordering
_  -> Bool
False

submapCmp :: (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp :: forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) (Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
  | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = Ordering
GT
  | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = Ordering
submapCmpLt
  | Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2       = Ordering
submapCmpEq
  | Bool
otherwise      = Ordering
GT  -- disjoint
  where
    submapCmpLt :: Ordering
submapCmpLt | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
p1 Word64
p2 Word64
m2  = Ordering
GT
                | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2        = (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
l2
                | Bool
otherwise         = (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
r2
    submapCmpEq :: Ordering
submapCmpEq = case ((a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate Word64Map a
l1 Word64Map b
l2, (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
forall a b.
(a -> b -> Bool) -> Word64Map a -> Word64Map b -> Ordering
submapCmp a -> b -> Bool
predicate Word64Map a
r1 Word64Map b
r2) of
                    (Ordering
GT,Ordering
_ ) -> Ordering
GT
                    (Ordering
_ ,Ordering
GT) -> Ordering
GT
                    (Ordering
EQ,Ordering
EQ) -> Ordering
EQ
                    (Ordering, Ordering)
_       -> Ordering
LT

submapCmp a -> b -> Bool
_         (Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) Word64Map b
_  = Ordering
GT
submapCmp a -> b -> Bool
predicate (Tip Word64
kx a
x) (Tip Word64
ky b
y)
  | (Word64
kx Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky) Bool -> Bool -> Bool
&& a -> b -> Bool
predicate a
x b
y = Ordering
EQ
  | Bool
otherwise                   = Ordering
GT  -- disjoint
submapCmp a -> b -> Bool
predicate (Tip Word64
k a
x) Word64Map b
t
  = case Word64 -> Word64Map b -> Maybe b
forall a. Word64 -> Word64Map a -> Maybe a
lookup Word64
k Word64Map b
t of
     Just b
y | a -> b -> Bool
predicate a
x b
y -> Ordering
LT
     Maybe b
_                      -> Ordering
GT -- disjoint
submapCmp a -> b -> Bool
_    Word64Map a
Nil Word64Map b
Nil = Ordering
EQ
submapCmp a -> b -> Bool
_    Word64Map a
Nil Word64Map b
_   = Ordering
LT

-- | \(O(n+m)\). Is this a submap?
-- Defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@).
isSubmapOf :: Eq a => Word64Map a -> Word64Map a -> Bool
isSubmapOf :: forall a. Eq a => Word64Map a -> Word64Map a -> Bool
isSubmapOf Word64Map a
m1 Word64Map a
m2
  = (a -> a -> Bool) -> Word64Map a -> Word64Map a -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Word64Map a
m1 Word64Map a
m2

{- | \(O(n+m)\).
 The expression (@'isSubmapOfBy' f m1 m2@) returns 'True' if
 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
 applied to their respective values. For example, the following
 expressions are all 'True':

  > isSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
  > isSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
  > isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])

 But the following are all 'False':

  > isSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
  > isSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
  > isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
-}
isSubmapOfBy :: (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy :: forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> b -> Bool
predicate t1 :: Word64Map a
t1@(Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) (Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
  | Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2  = Bool
False
  | Word64 -> Word64 -> Bool
shorter Word64
m2 Word64
m1  = Word64 -> Word64 -> Word64 -> Bool
match Word64
p1 Word64
p2 Word64
m2 Bool -> Bool -> Bool
&&
                       if Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m2
                       then (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
l2
                       else (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> b -> Bool
predicate Word64Map a
t1 Word64Map b
r2
  | Bool
otherwise      = (Word64
p1Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==Word64
p2) Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> b -> Bool
predicate Word64Map a
l1 Word64Map b
l2 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
isSubmapOfBy a -> b -> Bool
predicate Word64Map a
r1 Word64Map b
r2
isSubmapOfBy a -> b -> Bool
_         (Bin Word64
_ Word64
_ Word64Map a
_ Word64Map a
_) Word64Map b
_ = Bool
False
isSubmapOfBy a -> b -> Bool
predicate (Tip Word64
k a
x) Word64Map b
t     = case Word64 -> Word64Map b -> Maybe b
forall a. Word64 -> Word64Map a -> Maybe a
lookup Word64
k Word64Map b
t of
                                         Just b
y  -> a -> b -> Bool
predicate a
x b
y
                                         Maybe b
Nothing -> Bool
False
isSubmapOfBy a -> b -> Bool
_         Word64Map a
Nil Word64Map b
_           = Bool
True

{--------------------------------------------------------------------
  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 Word64
p Word64
m Word64Map a
l Word64Map a
r) = Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m (Word64Map a -> Word64Map b
go Word64Map a
l) (Word64Map a -> Word64Map b
go Word64Map a
r)
    go (Tip Word64
k a
x)     = Word64 -> b -> Word64Map b
forall a. Word64 -> a -> Word64Map a
Tip Word64
k (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 (f . g) xs
"map/coerce" map coerce = coerce
 #-}

-- | \(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. (Word64 -> a -> b) -> Word64Map a -> Word64Map b
mapWithKey Word64 -> a -> b
f Word64Map a
t
  = case Word64Map a
t of
      Bin Word64
p Word64
m Word64Map a
l Word64Map a
r -> Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m ((Word64 -> a -> b) -> Word64Map a -> Word64Map b
forall a b. (Word64 -> a -> b) -> Word64Map a -> Word64Map b
mapWithKey Word64 -> a -> b
f Word64Map a
l) ((Word64 -> a -> b) -> Word64Map a -> Word64Map b
forall a b. (Word64 -> a -> b) -> Word64Map a -> Word64Map b
mapWithKey Word64 -> a -> b
f Word64Map a
r)
      Tip Word64
k a
x     -> Word64 -> b -> Word64Map b
forall a. Word64 -> a -> Word64Map a
Tip Word64
k (Word64 -> a -> b
f Word64
k a
x)
      Word64Map a
Nil         -> Word64Map b
forall a. Word64Map a
Nil

{-# NOINLINE [1] mapWithKey #-}
{-# RULES
"mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
  mapWithKey (\k a -> f k (g k a)) xs
"mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
  mapWithKey (\k a -> f k (g a)) xs
"map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
  mapWithKey (\k a -> f (g k a)) xs
 #-}

-- | \(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 =>
(Word64 -> a -> t b) -> Word64Map a -> t (Word64Map b)
traverseWithKey Word64 -> 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 Word64
k a
v) = Word64 -> b -> Word64Map b
forall a. Word64 -> a -> Word64Map a
Tip Word64
k (b -> Word64Map b) -> t b -> t (Word64Map b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> a -> t b
f Word64
k a
v
    go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = (Word64Map b -> Word64Map b -> Word64Map b)
-> 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 (Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m)) (Word64Map a -> 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 (Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m) (Word64Map a -> t (Word64Map b)
go Word64Map a
l) (Word64Map a -> t (Word64Map b)
go Word64Map a
r)
{-# INLINE traverseWithKey #-}

-- | \(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 -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumWithKey (\a
a' Word64
_ 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 -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumWithKey a -> Word64 -> b -> (a, c)
f a
a Word64Map b
t
  = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> 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.
mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> Word64Map b -> (a,Word64Map c)
mapAccumL :: forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> b -> (a, c)
f a
a Word64Map b
t
  = case Word64Map b
t of
      Bin Word64
p Word64
m Word64Map b
l Word64Map b
r
        | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
            let (a
a1,Word64Map c
r') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> b -> (a, c)
f a
a Word64Map b
r
                (a
a2,Word64Map c
l') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> b -> (a, c)
f a
a1 Word64Map b
l
            in (a
a2,Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map c
l' Word64Map c
r')
        | Bool
otherwise  ->
            let (a
a1,Word64Map c
l') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> b -> (a, c)
f a
a Word64Map b
l
                (a
a2,Word64Map c
r') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumL a -> Word64 -> b -> (a, c)
f a
a1 Word64Map b
r
            in (a
a2,Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map c
l' Word64Map c
r')
      Tip Word64
k b
x     -> let (a
a',c
x') = a -> Word64 -> b -> (a, c)
f a
a Word64
k b
x in (a
a',Word64 -> c -> Word64Map c
forall a. Word64 -> a -> Word64Map a
Tip Word64
k c
x')
      Word64Map b
Nil         -> (a
a,Word64Map c
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 -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumRWithKey a -> Word64 -> b -> (a, c)
f a
a Word64Map b
t
  = case Word64Map b
t of
      Bin Word64
p Word64
m Word64Map b
l Word64Map b
r
        | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
            let (a
a1,Word64Map c
l') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumRWithKey a -> Word64 -> b -> (a, c)
f a
a Word64Map b
l
                (a
a2,Word64Map c
r') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumRWithKey a -> Word64 -> b -> (a, c)
f a
a1 Word64Map b
r
            in (a
a2,Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map c
l' Word64Map c
r')
        | Bool
otherwise  ->
            let (a
a1,Word64Map c
r') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumRWithKey a -> Word64 -> b -> (a, c)
f a
a Word64Map b
r
                (a
a2,Word64Map c
l') = (a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
forall a b c.
(a -> Word64 -> b -> (a, c))
-> a -> Word64Map b -> (a, Word64Map c)
mapAccumRWithKey a -> Word64 -> b -> (a, c)
f a
a1 Word64Map b
l
            in (a
a2,Word64 -> Word64 -> Word64Map c -> Word64Map c -> Word64Map c
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map c
l' Word64Map c
r')
      Tip Word64
k b
x     -> let (a
a',c
x') = a -> Word64 -> b -> (a, c)
f a
a Word64
k b
x in (a
a',Word64 -> c -> Word64Map c
forall a. Word64 -> a -> Word64Map a
Tip Word64
k c
x')
      Word64Map b
Nil         -> (a
a,Word64Map c
forall a. Word64Map a
Nil)

-- | \(O(n \min(n,W))\).
-- @'mapKeys' 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 value at the greatest of the
-- original keys is retained.
--
-- > mapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
-- > mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
-- > mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"

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

-- | \(O(n \min(n,W))\).
-- @'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) -> (Word64 -> Word64) -> Word64Map a -> Word64Map a
mapKeysWith a -> a -> a
c Word64 -> Word64
f
  = (a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a. (a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromListWith a -> a -> a
c ([(Word64, a)] -> Word64Map a)
-> (Word64Map a -> [(Word64, a)]) -> Word64Map a -> Word64Map a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64 -> a -> [(Word64, a)] -> [(Word64, a)])
-> [(Word64, a)] -> Word64Map a -> [(Word64, a)]
forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey (\Word64
k a
x [(Word64, a)]
xs -> (Word64 -> Word64
f Word64
k, a
x) (Word64, a) -> [(Word64, a)] -> [(Word64, a)]
forall a. a -> [a] -> [a]
: [(Word64, a)]
xs) []

-- | \(O(n \min(n,W))\).
-- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@
-- is strictly monotonic.
-- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@.
-- /The precondition is not checked./
-- Semi-formally, we have:
--
-- > and [x < y ==> f x < f y | x <- ls, y <- ls]
-- >                     ==> mapKeysMonotonic f s == mapKeys f s
-- >     where ls = keys s
--
-- This means that @f@ maps distinct original keys to distinct resulting keys.
-- This function has slightly better performance than 'mapKeys'.
--
-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]

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

{--------------------------------------------------------------------
  Filter
--------------------------------------------------------------------}
-- | \(O(n)\). Filter all values that satisfy some predicate.
--
-- > filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
-- > filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty
-- > filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty

filter :: (a -> Bool) -> Word64Map a -> Word64Map a
filter :: forall a. (a -> Bool) -> Word64Map a -> Word64Map a
filter a -> Bool
p Word64Map a
m
  = (Word64 -> a -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> a -> Bool) -> Word64Map a -> Word64Map a
filterWithKey (\Word64
_ a
x -> a -> Bool
p a
x) Word64Map a
m

-- | \(O(n)\). Filter all keys\/values that satisfy some predicate.
--
-- > filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"

filterWithKey :: (Key -> a -> Bool) -> Word64Map a -> Word64Map a
filterWithKey :: forall a. (Word64 -> a -> Bool) -> Word64Map a -> Word64Map a
filterWithKey Word64 -> a -> Bool
predicate = Word64Map a -> Word64Map a
go
    where
    go :: Word64Map a -> Word64Map a
go Word64Map a
Nil           = Word64Map a
forall a. Word64Map a
Nil
    go t :: Word64Map a
t@(Tip Word64
k a
x)   = if Word64 -> a -> Bool
predicate Word64
k a
x then Word64Map a
t else Word64Map a
forall a. Word64Map a
Nil
    go (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r) = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m (Word64Map a -> Word64Map a
go Word64Map a
l) (Word64Map a -> Word64Map a
go Word64Map a
r)

-- | \(O(n)\). Partition the map according to some predicate. The first
-- map contains all elements that satisfy the predicate, the second all
-- elements that fail the predicate. See also 'split'.
--
-- > partition (> "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
-- > partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
-- > partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])

partition :: (a -> Bool) -> Word64Map a -> (Word64Map a,Word64Map a)
partition :: forall a. (a -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
partition a -> Bool
p Word64Map a
m
  = (Word64 -> a -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
forall a.
(Word64 -> a -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
partitionWithKey (\Word64
_ a
x -> a -> Bool
p a
x) Word64Map a
m

-- | \(O(n)\). Partition the map according to some predicate. The first
-- map contains all elements that satisfy the predicate, the second all
-- elements that fail the predicate. See also 'split'.
--
-- > partitionWithKey (\ k _ -> k > 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
-- > partitionWithKey (\ k _ -> k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
-- > partitionWithKey (\ k _ -> k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])

partitionWithKey :: (Key -> a -> Bool) -> Word64Map a -> (Word64Map a,Word64Map a)
partitionWithKey :: forall a.
(Word64 -> a -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
partitionWithKey Word64 -> a -> Bool
predicate0 Word64Map a
t0 = StrictPair (Word64Map a) (Word64Map a)
-> (Word64Map a, Word64Map a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Word64Map a) (Word64Map a)
 -> (Word64Map a, Word64Map a))
-> StrictPair (Word64Map a) (Word64Map a)
-> (Word64Map a, Word64Map a)
forall a b. (a -> b) -> a -> b
$ (Word64 -> a -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
(Word64 -> a -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> a -> Bool
predicate0 Word64Map a
t0
  where
    go :: (Word64 -> a -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> a -> Bool
predicate Word64Map a
t =
      case Word64Map a
t of
        Bin Word64
p Word64
m Word64Map a
l Word64Map a
r ->
          let (Word64Map a
l1 :*: Word64Map a
l2) = (Word64 -> a -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> a -> Bool
predicate Word64Map a
l
              (Word64Map a
r1 :*: Word64Map a
r2) = (Word64 -> a -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> a -> Bool
predicate Word64Map a
r
          in Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l1 Word64Map a
r1 Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l2 Word64Map a
r2
        Tip Word64
k a
x
          | Word64 -> a -> Bool
predicate Word64
k a
x -> (Word64Map a
t Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
forall a. Word64Map a
Nil)
          | Bool
otherwise     -> (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
t)
        Word64Map a
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)

-- | \(O(\min(n,W))\). Take while a predicate on the keys holds.
-- The user is responsible for ensuring that for all @Int@s, @j \< k ==\> p j \>= p k@.
-- See note at 'spanAntitone'.
--
-- @
-- takeWhileAntitone p = 'fromDistinctAscList' . 'Data.List.takeWhile' (p . fst) . 'toList'
-- takeWhileAntitone p = 'filterWithKey' (\\k _ -> p k)
-- @
--
-- @since 0.6.7
takeWhileAntitone :: (Key -> Bool) -> Word64Map a -> Word64Map a
takeWhileAntitone :: forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
takeWhileAntitone Word64 -> Bool
predicate Word64Map a
t =
  case Word64Map a
t of
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
        if Word64 -> Bool
predicate Word64
0 -- handle negative numbers.
        then Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m ((Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
l) Word64Map a
r
        else (Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
r
    Word64Map a
_ -> (Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
t
  where
    go :: (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Bool
predicate' (Word64 -> Bool) -> Word64 -> Bool
forall a b. (a -> b) -> a -> b
$! Word64
pWord64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
m = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l ((Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' Word64Map a
r)
      | Bool
otherwise         = (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' Word64Map a
l
    go Word64 -> Bool
predicate' t' :: Word64Map a
t'@(Tip Word64
ky a
_)
      | Word64 -> Bool
predicate' Word64
ky = Word64Map a
t'
      | Bool
otherwise     = Word64Map a
forall a. Word64Map a
Nil
    go Word64 -> Bool
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil

-- | \(O(\min(n,W))\). Drop while a predicate on the keys holds.
-- The user is responsible for ensuring that for all @Int@s, @j \< k ==\> p j \>= p k@.
-- See note at 'spanAntitone'.
--
-- @
-- dropWhileAntitone p = 'fromDistinctAscList' . 'Data.List.dropWhile' (p . fst) . 'toList'
-- dropWhileAntitone p = 'filterWithKey' (\\k _ -> not (p k))
-- @
--
-- @since 0.6.7
dropWhileAntitone :: (Key -> Bool) -> Word64Map a -> Word64Map a
dropWhileAntitone :: forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
dropWhileAntitone Word64 -> Bool
predicate Word64Map a
t =
  case Word64Map a
t of
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
        if Word64 -> Bool
predicate Word64
0 -- handle negative numbers.
        then (Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
l
        else Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l ((Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
r)
    Word64Map a
_ -> (Word64 -> Bool) -> Word64Map a -> Word64Map a
forall a. (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate Word64Map a
t
  where
    go :: (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Bool
predicate' (Word64 -> Bool) -> Word64 -> Bool
forall a b. (a -> b) -> a -> b
$! Word64
pWord64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
m = (Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' Word64Map a
r
      | Bool
otherwise         = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m ((Word64 -> Bool) -> Word64Map a -> Word64Map a
go Word64 -> Bool
predicate' Word64Map a
l) Word64Map a
r
    go Word64 -> Bool
predicate' t' :: Word64Map a
t'@(Tip Word64
ky a
_)
      | Word64 -> Bool
predicate' Word64
ky = Word64Map a
forall a. Word64Map a
Nil
      | Bool
otherwise     = Word64Map a
t'
    go Word64 -> Bool
_ Word64Map a
Nil = Word64Map a
forall a. Word64Map a
Nil

-- | \(O(\min(n,W))\). Divide a map at the point where a predicate on the keys stops holding.
-- The user is responsible for ensuring that for all @Int@s, @j \< k ==\> p j \>= p k@.
--
-- @
-- spanAntitone p xs = ('takeWhileAntitone' p xs, 'dropWhileAntitone' p xs)
-- spanAntitone p xs = 'partitionWithKey' (\\k _ -> p k) xs
-- @
--
-- Note: if @p@ is not actually antitone, then @spanAntitone@ will split the map
-- at some /unspecified/ point.
--
-- @since 0.6.7
spanAntitone :: (Key -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
spanAntitone :: forall a.
(Word64 -> Bool) -> Word64Map a -> (Word64Map a, Word64Map a)
spanAntitone Word64 -> Bool
predicate Word64Map a
t =
  case Word64Map a
t of
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
        if Word64 -> Bool
predicate Word64
0 -- handle negative numbers.
        then
          case (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
(Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate Word64Map a
l of
            (Word64Map a
lt :*: Word64Map a
gt) ->
              let !lt' :: Word64Map a
lt' = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
lt Word64Map a
r
              in (Word64Map a
lt', Word64Map a
gt)
        else
          case (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
(Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate Word64Map a
r of
            (Word64Map a
lt :*: Word64Map a
gt) ->
              let !gt' :: Word64Map a
gt' = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l Word64Map a
gt
              in (Word64Map a
lt, Word64Map a
gt')
    Word64Map a
_ -> case (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
(Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate Word64Map a
t of
          (Word64Map a
lt :*: Word64Map a
gt) -> (Word64Map a
lt, Word64Map a
gt)
  where
    go :: (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate' (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Bool
predicate' (Word64 -> Bool) -> Word64 -> Bool
forall a b. (a -> b) -> a -> b
$! Word64
pWord64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
m = case (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate' Word64Map a
r of (Word64Map a
lt :*: Word64Map a
gt) -> Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l Word64Map a
lt Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
gt
      | Bool
otherwise         = case (Word64 -> Bool)
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> Bool
predicate' Word64Map a
l of (Word64Map a
lt :*: Word64Map a
gt) -> Word64Map a
lt Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
gt Word64Map a
r
    go Word64 -> Bool
predicate' t' :: Word64Map a
t'@(Tip Word64
ky a
_)
      | Word64 -> Bool
predicate' Word64
ky = (Word64Map a
t' Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
forall a. Word64Map a
Nil)
      | Bool
otherwise     = (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
t')
    go Word64 -> Bool
_ Word64Map a
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)

-- | \(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 = (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
forall a b. (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybeWithKey (\Word64
_ 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. (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybeWithKey Word64 -> a -> Maybe b
f (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  = Word64 -> Word64 -> Word64Map b -> Word64Map b -> Word64Map b
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m ((Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
forall a b. (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybeWithKey Word64 -> a -> Maybe b
f Word64Map a
l) ((Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
forall a b. (Word64 -> a -> Maybe b) -> Word64Map a -> Word64Map b
mapMaybeWithKey Word64 -> a -> Maybe b
f Word64Map a
r)
mapMaybeWithKey Word64 -> a -> Maybe b
f (Tip Word64
k a
x) = case Word64 -> a -> Maybe b
f Word64
k a
x of
  Just b
y  -> Word64 -> b -> Word64Map b
forall a. Word64 -> a -> Word64Map a
Tip Word64
k b
y
  Maybe b
Nothing -> Word64Map b
forall a. Word64Map a
Nil
mapMaybeWithKey Word64 -> 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
  = (Word64 -> a -> Either b c)
-> Word64Map a -> (Word64Map b, Word64Map c)
forall a b c.
(Word64 -> a -> Either b c)
-> Word64Map a -> (Word64Map b, Word64Map c)
mapEitherWithKey (\Word64
_ 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.
(Word64 -> a -> Either b c)
-> Word64Map a -> (Word64Map b, Word64Map c)
mapEitherWithKey Word64 -> 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
$ (Word64 -> a -> Either b c)
-> Word64Map a -> StrictPair (Word64Map b) (Word64Map c)
forall {t} {a} {a}.
(Word64 -> t -> Either a a)
-> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> a -> Either b c
f0 Word64Map a
t0
  where
    go :: (Word64 -> t -> Either a a)
-> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> t -> Either a a
f (Bin Word64
p Word64
m Word64Map t
l Word64Map t
r) =
      Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l1 Word64Map a
r1 Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l2 Word64Map a
r2
      where
        (Word64Map a
l1 :*: Word64Map a
l2) = (Word64 -> t -> Either a a)
-> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> t -> Either a a
f Word64Map t
l
        (Word64Map a
r1 :*: Word64Map a
r2) = (Word64 -> t -> Either a a)
-> Word64Map t -> StrictPair (Word64Map a) (Word64Map a)
go Word64 -> t -> Either a a
f Word64Map t
r
    go Word64 -> t -> Either a a
f (Tip Word64
k t
x) = case Word64 -> t -> Either a a
f Word64
k t
x of
      Left a
y  -> (Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
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
:*: Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
z)
    go Word64 -> 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)

-- | \(O(\min(n,W))\). The expression (@'split' k map@) is a pair @(map1,map2)@
-- where all keys in @map1@ are lower than @k@ and all keys in
-- @map2@ larger than @k@. Any key equal to @k@ is found in neither @map1@ nor @map2@.
--
-- > split 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
-- > split 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
-- > split 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
-- > split 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
-- > split 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)

split :: Key -> Word64Map a -> (Word64Map a, Word64Map a)
split :: forall a. Word64 -> Word64Map a -> (Word64Map a, Word64Map a)
split Word64
k Word64Map a
t =
  case Word64Map a
t of
    Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
        if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 -- handle negative numbers.
        then
          case Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k Word64Map a
l of
            (Word64Map a
lt :*: Word64Map a
gt) ->
              let !lt' :: Word64Map a
lt' = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
lt Word64Map a
r
              in (Word64Map a
lt', Word64Map a
gt)
        else
          case Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k Word64Map a
r of
            (Word64Map a
lt :*: Word64Map a
gt) ->
              let !gt' :: Word64Map a
gt' = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l Word64Map a
gt
              in (Word64Map a
lt, Word64Map a
gt')
    Word64Map a
_ -> case Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall {a}.
Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k Word64Map a
t of
          (Word64Map a
lt :*: Word64Map a
gt) -> (Word64Map a
lt, Word64Map a
gt)
  where
    go :: Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k' t' :: Word64Map a
t'@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k' Word64
p Word64
m = if Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
p then Word64Map a
t' Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
forall a. Word64Map a
Nil else 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
t'
      | Word64 -> Word64 -> Bool
zero Word64
k' Word64
m = case Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k' Word64Map a
l of (Word64Map a
lt :*: Word64Map a
gt) -> Word64Map a
lt Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
gt Word64Map a
r
      | Bool
otherwise = case Word64 -> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
go Word64
k' Word64Map a
r of (Word64Map a
lt :*: Word64Map a
gt) -> Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l Word64Map a
lt Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
gt
    go Word64
k' t' :: Word64Map a
t'@(Tip Word64
ky a
_)
      | Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
ky   = (Word64Map a
t' Word64Map a
-> Word64Map a -> StrictPair (Word64Map a) (Word64Map a)
forall a b. a -> b -> StrictPair a b
:*: Word64Map a
forall a. Word64Map a
Nil)
      | Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
ky   = (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
t')
      | Bool
otherwise = (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)
    go Word64
_ Word64Map a
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)


data SplitLookup a = SplitLookup !(Word64Map a) !(Maybe a) !(Word64Map a)

mapLT :: (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapLT :: forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapLT Word64Map a -> Word64Map a
f (SplitLookup Word64Map a
lt Maybe a
fnd Word64Map a
gt) = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup (Word64Map a -> Word64Map a
f Word64Map a
lt) Maybe a
fnd Word64Map a
gt
{-# INLINE mapLT #-}

mapGT :: (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapGT :: forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapGT Word64Map a -> Word64Map a
f (SplitLookup Word64Map a
lt Maybe a
fnd Word64Map a
gt) = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
lt Maybe a
fnd (Word64Map a -> Word64Map a
f Word64Map a
gt)
{-# INLINE mapGT #-}

-- | \(O(\min(n,W))\). Performs a 'split' but also returns whether the pivot
-- key was found in the original map.
--
-- > splitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
-- > splitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
-- > splitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
-- > splitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
-- > splitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)

splitLookup :: Key -> Word64Map a -> (Word64Map a, Maybe a, Word64Map a)
splitLookup :: forall a.
Word64 -> Word64Map a -> (Word64Map a, Maybe a, Word64Map a)
splitLookup Word64
k Word64Map a
t =
  case
    case Word64Map a
t of
      Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
        | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 ->
          if Word64
k Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
0 -- handle negative numbers.
          then (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapLT ((Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> Word64Map a -> Word64Map a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m) Word64Map a
r) (Word64 -> Word64Map a -> SplitLookup a
forall {a}. Word64 -> Word64Map a -> SplitLookup a
go Word64
k Word64Map a
l)
          else (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapGT (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l) (Word64 -> Word64Map a -> SplitLookup a
forall {a}. Word64 -> Word64Map a -> SplitLookup a
go Word64
k Word64Map a
r)
      Word64Map a
_ -> Word64 -> Word64Map a -> SplitLookup a
forall {a}. Word64 -> Word64Map a -> SplitLookup a
go Word64
k Word64Map a
t
  of SplitLookup Word64Map a
lt Maybe a
fnd Word64Map a
gt -> (Word64Map a
lt, Maybe a
fnd, Word64Map a
gt)
  where
    go :: Word64 -> Word64Map a -> SplitLookup a
go Word64
k' t' :: Word64Map a
t'@(Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
      | Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
k' Word64
p Word64
m =
          if Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
p
          then Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
t' Maybe a
forall a. Maybe a
Nothing Word64Map a
forall a. Word64Map a
Nil
          else Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
forall a. Word64Map a
Nil Maybe a
forall a. Maybe a
Nothing Word64Map a
t'
      | Word64 -> Word64 -> Bool
zero Word64
k' Word64
m = (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapGT ((Word64Map a -> Word64Map a -> Word64Map a)
-> Word64Map a -> Word64Map a -> Word64Map a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m) Word64Map a
r) (Word64 -> Word64Map a -> SplitLookup a
go Word64
k' Word64Map a
l)
      | Bool
otherwise = (Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
forall a.
(Word64Map a -> Word64Map a) -> SplitLookup a -> SplitLookup a
mapLT (Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
p Word64
m Word64Map a
l) (Word64 -> Word64Map a -> SplitLookup a
go Word64
k' Word64Map a
r)
    go Word64
k' t' :: Word64Map a
t'@(Tip Word64
ky a
y)
      | Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
ky   = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
t'  Maybe a
forall a. Maybe a
Nothing  Word64Map a
forall a. Word64Map a
Nil
      | Word64
k' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
ky   = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
forall a. Word64Map a
Nil Maybe a
forall a. Maybe a
Nothing  Word64Map a
t'
      | Bool
otherwise = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
forall a. Word64Map a
Nil (a -> Maybe a
forall a. a -> Maybe a
Just a
y) Word64Map a
forall a. Word64Map a
Nil
    go Word64
_ Word64Map a
Nil      = Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
forall a. Word64Map a -> Maybe a -> Word64Map a -> SplitLookup a
SplitLookup Word64Map a
forall a. Word64Map a
Nil Maybe a
forall a. Maybe a
Nothing  Word64Map a
forall a. Word64Map a
Nil

{--------------------------------------------------------------------
  Fold
--------------------------------------------------------------------}
-- | \(O(n)\). Fold the values in the map using the given right-associative
-- binary operator, such that @'foldr' f z == 'Prelude.foldr' f z . 'elems'@.
--
-- For example,
--
-- > elems map = foldr (:) [] map
--
-- > let f a len = len + (length a)
-- > foldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
foldr :: (a -> b -> b) -> b -> Word64Map a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr a -> b -> b
f b
z = \Word64Map a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
l) Word64Map a
r -- put negative numbers before
      | Bool
otherwise -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
r) Word64Map a
l
    Word64Map a
_ -> b -> Word64Map a -> b
go b
z Word64Map a
t
  where
    go :: b -> Word64Map a -> b
go b
z' Word64Map a
Nil           = b
z'
    go b
z' (Tip Word64
_ a
x)     = a -> b -> b
f a
x b
z'
    go b
z' (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z' Word64Map a
r) Word64Map a
l
{-# INLINE foldr #-}

-- | \(O(n)\). A strict version of 'foldr'. Each application of the operator is
-- evaluated before using the result in the next application. This
-- function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Word64Map a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr' a -> b -> b
f b
z = \Word64Map a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
l) Word64Map a
r -- put negative numbers before
      | Bool
otherwise -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
r) Word64Map a
l
    Word64Map a
_ -> b -> Word64Map a -> b
go b
z Word64Map a
t
  where
    go :: b -> Word64Map a -> b
go !b
z' Word64Map a
Nil          = b
z'
    go b
z' (Tip Word64
_ a
x)     = a -> b -> b
f a
x b
z'
    go b
z' (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z' Word64Map a
r) Word64Map a
l
{-# INLINE foldr' #-}

-- | \(O(n)\). Fold the values in the map using the given left-associative
-- binary operator, such that @'foldl' f z == 'Prelude.foldl' f z . 'elems'@.
--
-- For example,
--
-- > elems = reverse . foldl (flip (:)) []
--
-- > let f len a = len + (length a)
-- > foldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
foldl :: (a -> b -> a) -> a -> Word64Map b -> a
foldl :: forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl a -> b -> a
f a
z = \Word64Map b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map b
t of
    Bin Word64
_ Word64
m Word64Map b
l Word64Map b
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
r) Word64Map b
l -- put negative numbers before
      | Bool
otherwise -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
l) Word64Map b
r
    Word64Map b
_ -> a -> Word64Map b -> a
go a
z Word64Map b
t
  where
    go :: a -> Word64Map b -> a
go a
z' Word64Map b
Nil           = a
z'
    go a
z' (Tip Word64
_ b
x)     = a -> b -> a
f a
z' b
x
    go a
z' (Bin Word64
_ Word64
_ Word64Map b
l Word64Map b
r) = a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z' Word64Map b
l) Word64Map b
r
{-# INLINE foldl #-}

-- | \(O(n)\). A strict version of 'foldl'. Each application of the operator is
-- evaluated before using the result in the next application. This
-- function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Word64Map b -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Word64Map a -> b
foldl' a -> b -> a
f a
z = \Word64Map b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map b
t of
    Bin Word64
_ Word64
m Word64Map b
l Word64Map b
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
r) Word64Map b
l -- put negative numbers before
      | Bool
otherwise -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
l) Word64Map b
r
    Word64Map b
_ -> a -> Word64Map b -> a
go a
z Word64Map b
t
  where
    go :: a -> Word64Map b -> a
go !a
z' Word64Map b
Nil          = a
z'
    go a
z' (Tip Word64
_ b
x)     = a -> b -> a
f a
z' b
x
    go a
z' (Bin Word64
_ Word64
_ Word64Map b
l Word64Map b
r) = a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z' Word64Map b
l) Word64Map b
r
{-# INLINE foldl' #-}

-- | \(O(n)\). Fold the keys and values in the map using the given right-associative
-- binary operator, such that
-- @'foldrWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@.
--
-- For example,
--
-- > keys map = foldrWithKey (\k x ks -> k:ks) [] map
--
-- > let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
-- > foldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
foldrWithKey :: (Key -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey :: forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey Word64 -> a -> b -> b
f b
z = \Word64Map a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
l) Word64Map a
r -- put negative numbers before
      | Bool
otherwise -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
r) Word64Map a
l
    Word64Map a
_ -> b -> Word64Map a -> b
go b
z Word64Map a
t
  where
    go :: b -> Word64Map a -> b
go b
z' Word64Map a
Nil           = b
z'
    go b
z' (Tip Word64
kx a
x)    = Word64 -> a -> b -> b
f Word64
kx a
x b
z'
    go b
z' (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z' Word64Map a
r) Word64Map a
l
{-# INLINE foldrWithKey #-}

-- | \(O(n)\). A strict version of 'foldrWithKey'. Each application of the operator is
-- evaluated before using the result in the next application. This
-- function is strict in the starting value.
foldrWithKey' :: (Key -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey' :: forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey' Word64 -> a -> b -> b
f b
z = \Word64Map a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map a
t of
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
l) Word64Map a
r -- put negative numbers before
      | Bool
otherwise -> b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z Word64Map a
r) Word64Map a
l
    Word64Map a
_ -> b -> Word64Map a -> b
go b
z Word64Map a
t
  where
    go :: b -> Word64Map a -> b
go !b
z' Word64Map a
Nil          = b
z'
    go b
z' (Tip Word64
kx a
x)    = Word64 -> a -> b -> b
f Word64
kx a
x b
z'
    go b
z' (Bin Word64
_ Word64
_ Word64Map a
l Word64Map a
r) = b -> Word64Map a -> b
go (b -> Word64Map a -> b
go b
z' Word64Map a
r) Word64Map a
l
{-# INLINE foldrWithKey' #-}

-- | \(O(n)\). Fold the keys and values in the map using the given left-associative
-- binary operator, such that
-- @'foldlWithKey' f z == 'Prelude.foldl' (\\z' (kx, x) -> f z' kx x) z . 'toAscList'@.
--
-- For example,
--
-- > keys = reverse . foldlWithKey (\ks k x -> k:ks) []
--
-- > let f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
-- > foldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
foldlWithKey :: (a -> Key -> b -> a) -> a -> Word64Map b -> a
foldlWithKey :: forall a b. (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
foldlWithKey a -> Word64 -> b -> a
f a
z = \Word64Map b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map b
t of
    Bin Word64
_ Word64
m Word64Map b
l Word64Map b
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
r) Word64Map b
l -- put negative numbers before
      | Bool
otherwise -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
l) Word64Map b
r
    Word64Map b
_ -> a -> Word64Map b -> a
go a
z Word64Map b
t
  where
    go :: a -> Word64Map b -> a
go a
z' Word64Map b
Nil           = a
z'
    go a
z' (Tip Word64
kx b
x)    = a -> Word64 -> b -> a
f a
z' Word64
kx b
x
    go a
z' (Bin Word64
_ Word64
_ Word64Map b
l Word64Map b
r) = a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z' Word64Map b
l) Word64Map b
r
{-# INLINE foldlWithKey #-}

-- | \(O(n)\). A strict version of 'foldlWithKey'. Each application of the operator is
-- evaluated before using the result in the next application. This
-- function is strict in the starting value.
foldlWithKey' :: (a -> Key -> b -> a) -> a -> Word64Map b -> a
foldlWithKey' :: forall a b. (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
foldlWithKey' a -> Word64 -> b -> a
f a
z = \Word64Map b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case Word64Map b
t of
    Bin Word64
_ Word64
m Word64Map b
l Word64Map b
r
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
r) Word64Map b
l -- put negative numbers before
      | Bool
otherwise -> a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z Word64Map b
l) Word64Map b
r
    Word64Map b
_ -> a -> Word64Map b -> a
go a
z Word64Map b
t
  where
    go :: a -> Word64Map b -> a
go !a
z' Word64Map b
Nil          = a
z'
    go a
z' (Tip Word64
kx b
x)    = a -> Word64 -> b -> a
f a
z' Word64
kx b
x
    go a
z' (Bin Word64
_ Word64
_ Word64Map b
l Word64Map b
r) = a -> Word64Map b -> a
go (a -> Word64Map b -> a
go a
z' Word64Map b
l) Word64Map b
r
{-# INLINE foldlWithKey' #-}

-- | \(O(n)\). Fold the keys and values in the map using the given monoid, such that
--
-- @'foldMapWithKey' f = 'Prelude.fold' . 'mapWithKey' f@
--
-- This can be an asymptotically faster than 'foldrWithKey' or 'foldlWithKey' for some monoids.
--
-- @since 0.5.4
foldMapWithKey :: Monoid m => (Key -> a -> m) -> Word64Map a -> m
foldMapWithKey :: forall m a. Monoid m => (Word64 -> a -> m) -> Word64Map a -> m
foldMapWithKey Word64 -> a -> m
f = Word64Map a -> m
go
  where
    go :: Word64Map a -> m
go Word64Map a
Nil           = m
forall a. Monoid a => a
mempty
    go (Tip Word64
kx a
x)    = Word64 -> a -> m
f Word64
kx a
x
    go (Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r)
      | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0     = Word64Map a -> m
go Word64Map a
r m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Word64Map a -> m
go Word64Map a
l
      | Bool
otherwise = Word64Map a -> m
go Word64Map a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Word64Map a -> m
go Word64Map a
r
{-# INLINE foldMapWithKey #-}

{--------------------------------------------------------------------
  List variations
--------------------------------------------------------------------}
-- | \(O(n)\).
-- Return all elements of the map in the ascending order of their keys.
-- Subject to list fusion.
--
-- > elems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
-- > elems empty == []

elems :: Word64Map a -> [a]
elems :: forall a. Word64Map a -> [a]
elems = (a -> [a] -> [a]) -> [a] -> Word64Map a -> [a]
forall a b. (a -> b -> b) -> b -> Word64Map a -> b
foldr (:) []

-- | \(O(n)\). Return all keys of the map in ascending order. Subject to list
-- fusion.
--
-- > keys (fromList [(5,"a"), (3,"b")]) == [3,5]
-- > keys empty == []

keys  :: Word64Map a -> [Key]
keys :: forall a. Word64Map a -> [Word64]
keys = (Word64 -> a -> [Word64] -> [Word64])
-> [Word64] -> Word64Map a -> [Word64]
forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey (\Word64
k a
_ [Word64]
ks -> Word64
k Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
ks) []

-- | \(O(n)\). An alias for 'toAscList'. Returns all key\/value pairs in the
-- map in ascending key order. Subject to list fusion.
--
-- > assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
-- > assocs empty == []

assocs :: Word64Map a -> [(Key,a)]
assocs :: forall a. Word64Map a -> [(Word64, a)]
assocs = Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toAscList

-- | \(O(n \min(n,W))\). The set of all keys of the map.
--
-- > keysSet (fromList [(5,"a"), (3,"b")]) == Data.Word64Set.fromList [3,5]
-- > keysSet empty == Data.Word64Set.empty

keysSet :: Word64Map a -> Word64Set.Word64Set
keysSet :: forall a. Word64Map a -> Word64Set
keysSet Word64Map a
Nil = Word64Set
Word64Set.Nil
keysSet (Tip Word64
kx a
_) = Word64 -> Word64Set
Word64Set.singleton Word64
kx
keysSet (Bin Word64
p Word64
m Word64Map a
l Word64Map a
r)
  | Word64
m Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.suffixBitMask Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0 = Word64 -> Word64 -> Word64Set -> Word64Set -> Word64Set
Word64Set.Bin Word64
p Word64
m (Word64Map a -> Word64Set
forall a. Word64Map a -> Word64Set
keysSet Word64Map a
l) (Word64Map a -> Word64Set
forall a. Word64Map a -> Word64Set
keysSet Word64Map a
r)
  | Bool
otherwise = Word64 -> Word64 -> Word64Set
Word64Set.Tip (Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
Word64Set.prefixBitMask) (Word64 -> Word64Map a -> Word64
forall {a}. Word64 -> Word64Map a -> Word64
computeBm (Word64 -> Word64Map a -> Word64
forall {a}. Word64 -> Word64Map a -> Word64
computeBm Word64
0 Word64Map a
l) Word64Map a
r)
  where computeBm :: Word64 -> Word64Map a -> Word64
computeBm !Word64
acc (Bin Word64
_ Word64
_ Word64Map a
l' Word64Map a
r') = Word64 -> Word64Map a -> Word64
computeBm (Word64 -> Word64Map a -> Word64
computeBm Word64
acc Word64Map a
l') Word64Map a
r'
        computeBm Word64
acc (Tip Word64
kx a
_) = Word64
acc Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64 -> Word64
Word64Set.bitmapOf Word64
kx
        computeBm Word64
_   Word64Map a
Nil = [Char] -> Word64
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Word64Set.keysSet: Nil"

-- | \(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. (Word64 -> a) -> Word64Set -> Word64Map a
fromSet Word64 -> a
_ Word64Set
Word64Set.Nil = Word64Map a
forall a. Word64Map a
Nil
fromSet Word64 -> a
f (Word64Set.Bin Word64
p Word64
m Word64Set
l Word64Set
r) = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m ((Word64 -> a) -> Word64Set -> Word64Map a
forall a. (Word64 -> a) -> Word64Set -> Word64Map a
fromSet Word64 -> a
f Word64Set
l) ((Word64 -> a) -> Word64Set -> Word64Map a
forall a. (Word64 -> a) -> Word64Set -> Word64Map a
fromSet Word64 -> a
f Word64Set
r)
fromSet Word64 -> a
f (Word64Set.Tip Word64
kx Word64
bm) = (Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
forall {a}.
(Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
f Word64
kx Word64
bm (Word64
Word64Set.suffixBitMask Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
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 :: (Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
g !Word64
prefix !Word64
bmask Word64
bits = case Word64
bits of
      Word64
0 -> Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
prefix (Word64 -> a
g Word64
prefix)
      Word64
_ -> case Word64 -> Word64
intFromNat ((Word64 -> Word64
natFromInt Word64
bits) Word64 -> Int -> Word64
`shiftRL` Int
1) of
        Word64
bits2
          | Word64
bmask Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. ((Word64
1 Word64 -> Int -> Word64
`shiftLL` Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
bits2) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0 ->
              (Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
g (Word64
prefix Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
bits2) (Word64
bmask Word64 -> Int -> Word64
`shiftRL` Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
bits2) Word64
bits2
          | (Word64
bmask Word64 -> Int -> Word64
`shiftRL` Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
bits2) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. ((Word64
1 Word64 -> Int -> Word64
`shiftLL` Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
bits2) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0 ->
              (Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
g Word64
prefix Word64
bmask Word64
bits2
          | Bool
otherwise ->
              Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
prefix Word64
bits2
                ((Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
g Word64
prefix Word64
bmask Word64
bits2)
                ((Word64 -> a) -> Word64 -> Word64 -> Word64 -> Word64Map a
buildTree Word64 -> a
g (Word64
prefix Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
bits2) (Word64
bmask Word64 -> Int -> Word64
`shiftRL` Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
bits2) Word64
bits2)

{--------------------------------------------------------------------
  Lists
--------------------------------------------------------------------}

-- | @since 0.5.6.2
instance GHCExts.IsList (Word64Map a) where
  type Item (Word64Map a) = (Key,a)
  fromList :: [Item (Word64Map a)] -> Word64Map a
fromList = [(Word64, a)] -> Word64Map a
[Item (Word64Map a)] -> Word64Map a
forall a. [(Word64, a)] -> Word64Map a
fromList
  toList :: Word64Map a -> [Item (Word64Map a)]
toList   = Word64Map a -> [(Word64, a)]
Word64Map a -> [Item (Word64Map a)]
forall a. Word64Map a -> [(Word64, a)]
toList

-- | \(O(n)\). Convert the map to a list of key\/value pairs. Subject to list
-- fusion.
--
-- > toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
-- > toList empty == []

toList :: Word64Map a -> [(Key,a)]
toList :: forall a. Word64Map a -> [(Word64, a)]
toList = Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toAscList

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

toAscList :: Word64Map a -> [(Key,a)]
toAscList :: forall a. Word64Map a -> [(Word64, a)]
toAscList = (Word64 -> a -> [(Word64, a)] -> [(Word64, a)])
-> [(Word64, a)] -> Word64Map a -> [(Word64, a)]
forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey (\Word64
k a
x [(Word64, a)]
xs -> (Word64
k,a
x)(Word64, a) -> [(Word64, a)] -> [(Word64, a)]
forall a. a -> [a] -> [a]
:[(Word64, a)]
xs) []

-- | \(O(n)\). Convert the map to a list of key\/value pairs where the keys
-- are in descending order. Subject to list fusion.
--
-- > toDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]

toDescList :: Word64Map a -> [(Key,a)]
toDescList :: forall a. Word64Map a -> [(Word64, a)]
toDescList = ([(Word64, a)] -> Word64 -> a -> [(Word64, a)])
-> [(Word64, a)] -> Word64Map a -> [(Word64, a)]
forall a b. (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
foldlWithKey (\[(Word64, a)]
xs Word64
k a
x -> (Word64
k,a
x)(Word64, a) -> [(Word64, a)] -> [(Word64, a)]
forall a. a -> [a] -> [a]
:[(Word64, a)]
xs) []

-- List fusion for the list generating functions.
-- The foldrFB and foldlFB are fold{r,l}WithKey equivalents, used for list fusion.
-- They are important to convert unfused methods back, see mapFB in prelude.
foldrFB :: (Key -> a -> b -> b) -> b -> Word64Map a -> b
foldrFB :: forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrFB = (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
forall a b. (Word64 -> a -> b -> b) -> b -> Word64Map a -> b
foldrWithKey
{-# INLINE[0] foldrFB #-}
foldlFB :: (a -> Key -> b -> a) -> a -> Word64Map b -> a
foldlFB :: forall a b. (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
foldlFB = (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
forall a b. (a -> Word64 -> b -> a) -> a -> Word64Map b -> a
foldlWithKey
{-# INLINE[0] foldlFB #-}

-- Inline assocs and toList, so that we need to fuse only toAscList.
{-# INLINE assocs #-}
{-# INLINE toList #-}

-- The fusion is enabled up to phase 2 included. If it does not succeed,
-- convert in phase 1 the expanded elems,keys,to{Asc,Desc}List calls back to
-- elems,keys,to{Asc,Desc}List.  In phase 0, we inline fold{lr}FB (which were
-- used in a list fusion, otherwise it would go away in phase 1), and let compiler
-- do whatever it wants with elems,keys,to{Asc,Desc}List -- it was forbidden to
-- inline it before phase 0, otherwise the fusion rules would not fire at all.
{-# NOINLINE[0] elems #-}
{-# NOINLINE[0] keys #-}
{-# NOINLINE[0] toAscList #-}
{-# NOINLINE[0] toDescList #-}
{-# RULES "Word64Map.elems" [~1] forall m . elems m = build (\c n -> foldrFB (\_ x xs -> c x xs) n m) #-}
{-# RULES "Word64Map.elemsBack" [1] foldrFB (\_ x xs -> x : xs) [] = elems #-}
{-# RULES "Word64Map.keys" [~1] forall m . keys m = build (\c n -> foldrFB (\k _ xs -> c k xs) n m) #-}
{-# RULES "Word64Map.keysBack" [1] foldrFB (\k _ xs -> k : xs) [] = keys #-}
{-# RULES "Word64Map.toAscList" [~1] forall m . toAscList m = build (\c n -> foldrFB (\k x xs -> c (k,x) xs) n m) #-}
{-# RULES "Word64Map.toAscListBack" [1] foldrFB (\k x xs -> (k, x) : xs) [] = toAscList #-}
{-# RULES "Word64Map.toDescList" [~1] forall m . toDescList m = build (\c n -> foldlFB (\xs k x -> c (k,x) xs) n m) #-}
{-# RULES "Word64Map.toDescListBack" [1] foldlFB (\xs k x -> (k, x) : xs) [] = toDescList #-}


-- | \(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. [(Word64, a)] -> Word64Map a
fromList [(Word64, a)]
xs
  = (Word64Map a -> (Word64, a) -> Word64Map a)
-> Word64Map a -> [(Word64, 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 -> (Word64, a) -> Word64Map a
forall {a}. Word64Map a -> (Word64, a) -> Word64Map a
ins Word64Map a
forall a. Word64Map a
empty [(Word64, a)]
xs
  where
    ins :: Word64Map a -> (Word64, a) -> Word64Map a
ins Word64Map a
t (Word64
k,a
x)  = Word64 -> a -> Word64Map a -> Word64Map a
forall a. Word64 -> a -> Word64Map a -> Word64Map a
insert Word64
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,"c")] == fromList [(3, "ab"), (5, "cba")]
-- > fromListWith (++) [] == empty

fromListWith :: (a -> a -> a) -> [(Key,a)] -> Word64Map a
fromListWith :: forall a. (a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromListWith a -> a -> a
f [(Word64, a)]
xs
  = (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a. (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromListWithKey (\Word64
_ a
x a
y -> a -> a -> a
f a
x a
y) [(Word64, 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. (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromListWithKey Word64 -> a -> a -> a
f [(Word64, a)]
xs
  = (Word64Map a -> (Word64, a) -> Word64Map a)
-> Word64Map a -> [(Word64, 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 -> (Word64, a) -> Word64Map a
ins Word64Map a
forall a. Word64Map a
empty [(Word64, a)]
xs
  where
    ins :: Word64Map a -> (Word64, a) -> Word64Map a
ins Word64Map a
t (Word64
k,a
x) = (Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
forall a.
(Word64 -> a -> a -> a)
-> Word64 -> a -> Word64Map a -> Word64Map a
insertWithKey Word64 -> a -> a -> a
f Word64
k a
x Word64Map a
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. [(Word64, a)] -> Word64Map a
fromAscList = Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a.
Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromMonoListWithKey Distinct
Nondistinct (\Word64
_ 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) -> [(Word64, a)] -> Word64Map a
fromAscListWith a -> a -> a
f = Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a.
Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromMonoListWithKey Distinct
Nondistinct (\Word64
_ 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./
--
-- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
-- > fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "5:b|a")]

fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> Word64Map a
fromAscListWithKey :: forall a. (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromAscListWithKey Word64 -> a -> a -> a
f = Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a.
Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromMonoListWithKey Distinct
Nondistinct Word64 -> 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. [(Word64, a)] -> Word64Map a
fromDistinctAscList = Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
forall a.
Distinct -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromMonoListWithKey Distinct
Distinct (\Word64
_ 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 -> (Word64 -> a -> a -> a) -> [(Word64, a)] -> Word64Map a
fromMonoListWithKey Distinct
distinct Word64 -> a -> a -> a
f = [(Word64, a)] -> Word64Map a
go
  where
    go :: [(Word64, a)] -> Word64Map a
go []              = Word64Map a
forall a. Word64Map a
Nil
    go ((Word64
kx,a
vx) : [(Word64, a)]
zs1) = Word64 -> a -> [(Word64, a)] -> Word64Map a
addAll' Word64
kx a
vx [(Word64, a)]
zs1

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

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

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

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

data Distinct = Distinct | Nondistinct

{--------------------------------------------------------------------
  Eq
--------------------------------------------------------------------}
instance Eq a => Eq (Word64Map a) where
  Word64Map a
t1 == :: Word64Map a -> Word64Map a -> Bool
== Word64Map a
t2  = Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
equal Word64Map a
t1 Word64Map a
t2
  Word64Map a
t1 /= :: Word64Map a -> Word64Map a -> Bool
/= Word64Map a
t2  = Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
nequal Word64Map a
t1 Word64Map a
t2

equal :: Eq a => Word64Map a -> Word64Map a -> Bool
equal :: forall a. Eq a => Word64Map a -> Word64Map a -> Bool
equal (Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) (Bin Word64
p2 Word64
m2 Word64Map a
l2 Word64Map a
r2)
  = (Word64
m1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
m2) Bool -> Bool -> Bool
&& (Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2) Bool -> Bool -> Bool
&& (Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
equal Word64Map a
l1 Word64Map a
l2) Bool -> Bool -> Bool
&& (Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
equal Word64Map a
r1 Word64Map a
r2)
equal (Tip Word64
kx a
x) (Tip Word64
ky a
y)
  = (Word64
kx Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky) Bool -> Bool -> Bool
&& (a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
y)
equal Word64Map a
Nil Word64Map a
Nil = Bool
True
equal Word64Map a
_   Word64Map a
_   = Bool
False

nequal :: Eq a => Word64Map a -> Word64Map a -> Bool
nequal :: forall a. Eq a => Word64Map a -> Word64Map a -> Bool
nequal (Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) (Bin Word64
p2 Word64
m2 Word64Map a
l2 Word64Map a
r2)
  = (Word64
m1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
m2) Bool -> Bool -> Bool
|| (Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
p2) Bool -> Bool -> Bool
|| (Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
nequal Word64Map a
l1 Word64Map a
l2) Bool -> Bool -> Bool
|| (Word64Map a -> Word64Map a -> Bool
forall a. Eq a => Word64Map a -> Word64Map a -> Bool
nequal Word64Map a
r1 Word64Map a
r2)
nequal (Tip Word64
kx a
x) (Tip Word64
ky a
y)
  = (Word64
kx Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
ky) Bool -> Bool -> Bool
|| (a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
y)
nequal Word64Map a
Nil Word64Map a
Nil = Bool
False
nequal Word64Map a
_   Word64Map a
_   = Bool
True

-- | @since 0.5.9
instance Eq1 Word64Map where
  liftEq :: forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
liftEq a -> b -> Bool
eq (Bin Word64
p1 Word64
m1 Word64Map a
l1 Word64Map a
r1) (Bin Word64
p2 Word64
m2 Word64Map b
l2 Word64Map b
r2)
    = (Word64
m1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
m2) Bool -> Bool -> Bool
&& (Word64
p1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p2) Bool -> Bool -> Bool
&& ((a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq Word64Map a
l1 Word64Map b
l2) Bool -> Bool -> Bool
&& ((a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall a b. (a -> b -> Bool) -> Word64Map a -> Word64Map b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq Word64Map a
r1 Word64Map b
r2)
  liftEq a -> b -> Bool
eq (Tip Word64
kx a
x) (Tip Word64
ky b
y)
    = (Word64
kx Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
ky) Bool -> Bool -> Bool
&& (a -> b -> Bool
eq a
x b
y)
  liftEq a -> b -> Bool
_eq Word64Map a
Nil Word64Map b
Nil = Bool
True
  liftEq a -> b -> Bool
_eq Word64Map a
_   Word64Map b
_   = Bool
False

{--------------------------------------------------------------------
  Ord
--------------------------------------------------------------------}

instance Ord a => Ord (Word64Map a) where
    compare :: Word64Map a -> Word64Map a -> Ordering
compare Word64Map a
m1 Word64Map a
m2 = [(Word64, a)] -> [(Word64, a)] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
m1) (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
m2)

-- | @since 0.5.9
instance Ord1 Word64Map where
  liftCompare :: forall a b.
(a -> b -> Ordering) -> Word64Map a -> Word64Map b -> Ordering
liftCompare a -> b -> Ordering
cmp Word64Map a
m Word64Map b
n =
    ((Word64, a) -> (Word64, b) -> Ordering)
-> [(Word64, a)] -> [(Word64, b)] -> Ordering
forall a b. (a -> b -> Ordering) -> [a] -> [b] -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering) -> (Word64, a) -> (Word64, b) -> Ordering
forall a b.
(a -> b -> Ordering) -> (Word64, a) -> (Word64, b) -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
cmp) (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
m) (Word64Map b -> [(Word64, b)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map b
n)

{--------------------------------------------------------------------
  Functor
--------------------------------------------------------------------}

instance Functor Word64Map where
    fmap :: forall a b. (a -> b) -> Word64Map a -> Word64Map b
fmap = (a -> b) -> Word64Map a -> Word64Map b
forall a b. (a -> b) -> Word64Map a -> Word64Map b
map

    a
a <$ :: forall a b. a -> Word64Map b -> Word64Map a
<$ Bin Word64
p Word64
m Word64Map b
l Word64Map b
r = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m (a
a a -> Word64Map b -> Word64Map a
forall a b. a -> Word64Map b -> Word64Map a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Word64Map b
l) (a
a a -> Word64Map b -> Word64Map a
forall a b. a -> Word64Map b -> Word64Map a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Word64Map b
r)
    a
a <$ Tip Word64
k b
_     = Word64 -> a -> Word64Map a
forall a. Word64 -> a -> Word64Map a
Tip Word64
k a
a
    a
_ <$ Word64Map b
Nil         = Word64Map a
forall a. Word64Map a
Nil

{--------------------------------------------------------------------
  Show
--------------------------------------------------------------------}

instance Show a => Show (Word64Map a) where
  showsPrec :: Int -> Word64Map a -> [Char] -> [Char]
showsPrec Int
d Word64Map a
m   = Bool -> ([Char] -> [Char]) -> [Char] -> [Char]
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (([Char] -> [Char]) -> [Char] -> [Char])
-> ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$
    [Char] -> [Char] -> [Char]
showString [Char]
"fromList " ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Word64, a)] -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
m)

-- | @since 0.5.9
instance Show1 Word64Map where
    liftShowsPrec :: forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char])
-> Int
-> Word64Map a
-> [Char]
-> [Char]
liftShowsPrec Int -> a -> [Char] -> [Char]
sp [a] -> [Char] -> [Char]
sl Int
d Word64Map a
m =
        (Int -> [(Word64, a)] -> [Char] -> [Char])
-> [Char] -> Int -> [(Word64, a)] -> [Char] -> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> [Char] -> Int -> a -> [Char] -> [Char]
showsUnaryWith ((Int -> (Word64, a) -> [Char] -> [Char])
-> ([(Word64, a)] -> [Char] -> [Char])
-> Int
-> [(Word64, a)]
-> [Char]
-> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> [a] -> [Char] -> [Char]
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> f a -> [Char] -> [Char]
liftShowsPrec Int -> (Word64, a) -> [Char] -> [Char]
sp' [(Word64, a)] -> [Char] -> [Char]
sl') [Char]
"fromList" Int
d (Word64Map a -> [(Word64, a)]
forall a. Word64Map a -> [(Word64, a)]
toList Word64Map a
m)
      where
        sp' :: Int -> (Word64, a) -> [Char] -> [Char]
sp' = (Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char])
-> Int
-> (Word64, a)
-> [Char]
-> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char])
-> Int
-> (Word64, a)
-> [Char]
-> [Char]
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> f a -> [Char] -> [Char]
liftShowsPrec Int -> a -> [Char] -> [Char]
sp [a] -> [Char] -> [Char]
sl
        sl' :: [(Word64, a)] -> [Char] -> [Char]
sl' = (Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> [(Word64, a)] -> [Char] -> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> [(Word64, a)] -> [Char] -> [Char]
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> [f a] -> [Char] -> [Char]
liftShowList Int -> a -> [Char] -> [Char]
sp [a] -> [Char] -> [Char]
sl

{--------------------------------------------------------------------
  Read
--------------------------------------------------------------------}
instance (Read e) => Read (Word64Map e) where
  readPrec :: ReadPrec (Word64Map e)
readPrec = ReadPrec (Word64Map e) -> ReadPrec (Word64Map e)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (Word64Map e) -> ReadPrec (Word64Map e))
-> ReadPrec (Word64Map e) -> ReadPrec (Word64Map e)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (Word64Map e) -> ReadPrec (Word64Map e)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (Word64Map e) -> ReadPrec (Word64Map e))
-> ReadPrec (Word64Map e) -> ReadPrec (Word64Map e)
forall a b. (a -> b) -> a -> b
$ do
    Ident "fromList" <- ReadPrec Lexeme
lexP
    xs <- readPrec
    return (fromList xs)

  readListPrec :: ReadPrec [Word64Map e]
readListPrec = ReadPrec [Word64Map e]
forall a. Read a => ReadPrec [a]
readListPrecDefault

-- | @since 0.5.9
instance Read1 Word64Map where
    liftReadsPrec :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Word64Map a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = ([Char] -> ReadS (Word64Map a)) -> Int -> ReadS (Word64Map a)
forall a. ([Char] -> ReadS a) -> Int -> ReadS a
readsData (([Char] -> ReadS (Word64Map a)) -> Int -> ReadS (Word64Map a))
-> ([Char] -> ReadS (Word64Map a)) -> Int -> ReadS (Word64Map a)
forall a b. (a -> b) -> a -> b
$
        (Int -> ReadS [(Word64, a)])
-> [Char]
-> ([(Word64, a)] -> Word64Map a)
-> [Char]
-> ReadS (Word64Map a)
forall a t.
(Int -> ReadS a) -> [Char] -> (a -> t) -> [Char] -> ReadS t
readsUnaryWith ((Int -> ReadS (Word64, a))
-> ReadS [(Word64, a)] -> Int -> ReadS [(Word64, a)]
forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS [a]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS (Word64, a)
rp' ReadS [(Word64, a)]
rl') [Char]
"fromList" [(Word64, a)] -> Word64Map a
forall a. [(Word64, a)] -> Word64Map a
fromList
      where
        rp' :: Int -> ReadS (Word64, a)
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Word64, a)
forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Word64, a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl
        rl' :: ReadS [(Word64, a)]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [(Word64, a)]
forall a. (Int -> ReadS a) -> ReadS [a] -> ReadS [(Word64, a)]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp ReadS [a]
rl

{--------------------------------------------------------------------
  Helpers
--------------------------------------------------------------------}
{--------------------------------------------------------------------
  Link
--------------------------------------------------------------------}
link :: Prefix -> Word64Map a -> Prefix -> Word64Map a -> Word64Map a
link :: forall a.
Word64 -> Word64Map a -> Word64 -> Word64Map a -> Word64Map a
link Word64
p1 Word64Map a
t1 Word64
p2 Word64Map a
t2 = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
linkWithMask (Word64 -> Word64 -> Word64
branchMask Word64
p1 Word64
p2) Word64
p1 Word64Map a
t1 {-p2-} Word64Map a
t2
{-# INLINE link #-}

-- `linkWithMask` is useful when the `branchMask` has already been computed
linkWithMask :: Mask -> Prefix -> Word64Map a -> Word64Map a -> Word64Map a
linkWithMask :: forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
linkWithMask Word64
m Word64
p1 Word64Map a
t1 {-p2-} Word64Map a
t2
  | Word64 -> Word64 -> Bool
zero Word64
p1 Word64
m = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
t1 Word64Map a
t2
  | Bool
otherwise = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
t2 Word64Map a
t1
  where
    p :: Word64
p = Word64 -> Word64 -> Word64
mask Word64
p1 Word64
m
{-# INLINE linkWithMask #-}

{--------------------------------------------------------------------
  @bin@ assures that we never have empty trees within a tree.
--------------------------------------------------------------------}
bin :: Prefix -> Mask -> Word64Map a -> Word64Map a -> Word64Map a
bin :: forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
bin Word64
_ Word64
_ Word64Map a
l Word64Map a
Nil = Word64Map a
l
bin Word64
_ Word64
_ Word64Map a
Nil Word64Map a
r = Word64Map a
r
bin Word64
p Word64
m Word64Map a
l Word64Map a
r   = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
{-# INLINE bin #-}

-- binCheckLeft only checks that the left subtree is non-empty
binCheckLeft :: Prefix -> Mask -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft :: forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckLeft Word64
_ Word64
_ Word64Map a
Nil Word64Map a
r = Word64Map a
r
binCheckLeft Word64
p Word64
m Word64Map a
l Word64Map a
r   = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
{-# INLINE binCheckLeft #-}

-- binCheckRight only checks that the right subtree is non-empty
binCheckRight :: Prefix -> Mask -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight :: forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
binCheckRight Word64
_ Word64
_ Word64Map a
l Word64Map a
Nil = Word64Map a
l
binCheckRight Word64
p Word64
m Word64Map a
l Word64Map a
r   = Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
forall a.
Word64 -> Word64 -> Word64Map a -> Word64Map a -> Word64Map a
Bin Word64
p Word64
m Word64Map a
l Word64Map a
r
{-# INLINE binCheckRight #-}

{--------------------------------------------------------------------
  Endian independent bit twiddling
--------------------------------------------------------------------}

-- | Should this key follow the left subtree of a 'Bin' with switching
-- bit @m@? N.B., the answer is only valid when @match i p m@ is true.
zero :: Key -> Mask -> Bool
zero :: Word64 -> Word64 -> Bool
zero Word64
i Word64
m
  = (Word64 -> Word64
natFromInt Word64
i) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. (Word64 -> Word64
natFromInt Word64
m) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0
{-# INLINE zero #-}

nomatch,match :: Key -> Prefix -> Mask -> Bool

-- | Does the key @i@ differ from the prefix @p@ before getting to
-- the switching bit @m@?
nomatch :: Word64 -> Word64 -> Word64 -> Bool
nomatch Word64
i Word64
p Word64
m
  = (Word64 -> Word64 -> Word64
mask Word64
i Word64
m) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
p
{-# INLINE nomatch #-}

-- | Does the key @i@ match the prefix @p@ (up to but not including
-- bit @m@)?
match :: Word64 -> Word64 -> Word64 -> Bool
match Word64
i Word64
p Word64
m
  = (Word64 -> Word64 -> Word64
mask Word64
i Word64
m) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
p
{-# INLINE match #-}


-- | The prefix of key @i@ up to (but not including) the switching
-- bit @m@.
mask :: Key -> Mask -> Prefix
mask :: Word64 -> Word64 -> Word64
mask Word64
i Word64
m
  = Word64 -> Word64 -> Word64
maskW (Word64 -> Word64
natFromInt Word64
i) (Word64 -> Word64
natFromInt Word64
m)
{-# INLINE mask #-}


{--------------------------------------------------------------------
  Big endian operations
--------------------------------------------------------------------}

-- | The prefix of key @i@ up to (but not including) the switching
-- bit @m@.
maskW :: Nat -> Nat -> Prefix
maskW :: Word64 -> Word64 -> Word64
maskW Word64
i Word64
m
  = Word64 -> Word64
intFromNat (Word64
i Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. ((-Word64
m) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
m))
{-# INLINE maskW #-}

-- | Does the left switching bit specify a shorter prefix?
shorter :: Mask -> Mask -> Bool
shorter :: Word64 -> Word64 -> Bool
shorter Word64
m1 Word64
m2
  = (Word64 -> Word64
natFromInt Word64
m1) Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> (Word64 -> Word64
natFromInt Word64
m2)
{-# INLINE shorter #-}

-- | The first switching bit where the two prefixes disagree.
branchMask :: Prefix -> Prefix -> Mask
branchMask :: Word64 -> Word64 -> Word64
branchMask Word64
p1 Word64
p2
  = Word64 -> Word64
intFromNat (Word64 -> Word64
highestBitMask (Word64 -> Word64
natFromInt Word64
p1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64 -> Word64
natFromInt Word64
p2))
{-# INLINE branchMask #-}

{--------------------------------------------------------------------
  Utilities
--------------------------------------------------------------------}

-- | \(O(1)\).  Decompose a map into pieces based on the structure
-- of the underlying tree. This function is useful for consuming a
-- map in parallel.
--
-- No guarantee is made as to the sizes of the pieces; an internal, but
-- deterministic process determines this.  However, it is guaranteed that the
-- pieces returned will be in ascending order (all elements in the first submap
-- less than all elements in the second, and so on).
--
-- Examples:
--
-- > splitRoot (fromList (zip [1..6::Int] ['a'..])) ==
-- >   [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d'),(5,'e'),(6,'f')]]
--
-- > splitRoot empty == []
--
--  Note that the current implementation does not return more than two submaps,
--  but you should not depend on this behaviour because it can change in the
--  future without notice.
splitRoot :: Word64Map a -> [Word64Map a]
splitRoot :: forall a. Word64Map a -> [Word64Map a]
splitRoot Word64Map a
orig =
  case Word64Map a
orig of
    Word64Map a
Nil -> []
    x :: Word64Map a
x@(Tip Word64
_ a
_) -> [Word64Map a
x]
    Bin Word64
_ Word64
m Word64Map a
l Word64Map a
r | Word64
m Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0 -> [Word64Map a
r, Word64Map a
l]
                | Bool
otherwise -> [Word64Map a
l, Word64Map a
r]
{-# INLINE splitRoot #-}


{--------------------------------------------------------------------
  Debugging
--------------------------------------------------------------------}

-- | \(O(n \min(n,W))\). Show the tree that implements the map. The tree is shown
-- in a compressed, hanging format.
showTree :: Show a => Word64Map a -> String
showTree :: forall a. Show a => Word64Map a -> [Char]
showTree Word64Map a
s
  = Bool -> Bool -> Word64Map a -> [Char]
forall a. Show a => Bool -> Bool -> Word64Map a -> [Char]
showTreeWith Bool
True Bool
False Word64Map a
s


{- | \(O(n \min(n,W))\). The expression (@'showTreeWith' hang wide map@) shows
 the tree that implements the map. If @hang@ is
 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If
 @wide@ is 'True', an extra wide version is shown.
-}
showTreeWith :: Show a => Bool -> Bool -> Word64Map a -> String
showTreeWith :: forall a. Show a => Bool -> Bool -> Word64Map a -> [Char]
showTreeWith Bool
hang Bool
wide Word64Map a
t
  | Bool
hang      = (Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTreeHang Bool
wide [] Word64Map a
t) [Char]
""
  | Bool
otherwise = (Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTree Bool
wide [] [] Word64Map a
t) [Char]
""

showsTree :: Show a => Bool -> [String] -> [String] -> Word64Map a -> ShowS
showsTree :: forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTree Bool
wide [[Char]]
lbars [[Char]]
rbars Word64Map a
t = case Word64Map a
t of
  Bin Word64
p Word64
m Word64Map a
l Word64Map a
r ->
    Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTree Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
rbars) ([[Char]] -> [[Char]]
withEmpty [[Char]]
rbars) Word64Map a
r ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> [Char] -> [Char]
showWide Bool
wide [[Char]]
rbars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
lbars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString (Word64 -> Word64 -> [Char]
showBin Word64
p Word64
m) ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"\n" ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> [Char] -> [Char]
showWide Bool
wide [[Char]]
lbars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTree Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
lbars) ([[Char]] -> [[Char]]
withBar [[Char]]
lbars) Word64Map a
l
  Tip Word64
k a
x ->
    [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
lbars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    [Char] -> [Char] -> [Char]
showString [Char]
" " ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows Word64
k ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
":=" ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows a
x ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"\n"
  Word64Map a
Nil -> [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
lbars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"|\n"

showsTreeHang :: Show a => Bool -> [String] -> Word64Map a -> ShowS
showsTreeHang :: forall a.
Show a =>
Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTreeHang Bool
wide [[Char]]
bars Word64Map a
t = case Word64Map a
t of
  Bin Word64
p Word64
m Word64Map a
l Word64Map a
r ->
    [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
bars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString (Word64 -> Word64 -> [Char]
showBin Word64
p Word64
m) ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"\n" ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> [Char] -> [Char]
showWide Bool
wide [[Char]]
bars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
bars) Word64Map a
l ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> [Char] -> [Char]
showWide Bool
wide [[Char]]
bars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> Word64Map a -> [Char] -> [Char]
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
bars) Word64Map a
r
  Tip Word64
k a
x ->
    [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
bars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    [Char] -> [Char] -> [Char]
showString [Char]
" " ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows Word64
k ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
":=" ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows a
x ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"\n"
  Word64Map a
Nil -> [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
bars ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"|\n"

showBin :: Prefix -> Mask -> String
showBin :: Word64 -> Word64 -> [Char]
showBin Word64
_ Word64
_
  = [Char]
"*" -- ++ show (p,m)

showWide :: Bool -> [String] -> String -> String
showWide :: Bool -> [[Char]] -> [Char] -> [Char]
showWide Bool
wide [[Char]]
bars
  | Bool
wide      = [Char] -> [Char] -> [Char]
showString ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse [[Char]]
bars)) ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
"|\n"
  | Bool
otherwise = [Char] -> [Char]
forall a. a -> a
id

showsBars :: [String] -> ShowS
showsBars :: [[Char]] -> [Char] -> [Char]
showsBars [[Char]]
bars
  = case [[Char]]
bars of
      [] -> [Char] -> [Char]
forall a. a -> a
id
      [Char]
_ : [[Char]]
tl -> [Char] -> [Char] -> [Char]
showString ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse [[Char]]
tl)) ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [Char]
showString [Char]
node

node :: String
node :: [Char]
node = [Char]
"+--"

withBar, withEmpty :: [String] -> [String]
withBar :: [[Char]] -> [[Char]]
withBar [[Char]]
bars   = [Char]
"|  "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars
withEmpty :: [[Char]] -> [[Char]]
withEmpty [[Char]]
bars = [Char]
"   "[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
bars