{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
#ifdef __GLASGOW_HASKELL__
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Trustworthy #-}
#endif

{-# OPTIONS_HADDOCK not-home #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.IntMap.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.
--
--
-- = Finite Int Maps (lazy interface internals)
--
-- The @'IntMap' v@ type represents a finite map (sometimes called a dictionary)
-- from keys of type @Int@ to values of type @v@.
--
--
-- == Implementation
--
-- The implementation is based on /big-endian patricia trees/.  This data
-- structure performs especially well on binary operations like 'union'
-- and 'intersection'. Additionally, benchmarks show that it is also
-- (much) faster on insertions and deletions when compared to a generic
-- size-balanced map implementation (see "Data.Map").
--
--    * Chris Okasaki and Andy Gill,
--      \"/Fast Mergeable Integer Maps/\",
--      Workshop on ML, September 1998, pages 77-86,
--      <https://web.archive.org/web/20150417234429/https://ittc.ku.edu/~andygill/papers/IntMap98.pdf>.
--
--    * D.R. Morrison,
--      \"/PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric/\",
--      Journal of the ACM, 15(4), October 1968, pages 514-534,
--      <https://doi.org/10.1145/321479.321481>.
--
-- @since 0.5.9
-----------------------------------------------------------------------------

-- [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 IntMap 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 Data.IntMap.Internal (
    -- * Map type
      IntMap(..)          -- instance Eq,Show
    , Key

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

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

    -- * Construction
    , empty
    , singleton
    , fromSet
    , fromSetA
    , fromSetMaybe
    , fromSetMaybeA

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

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

    -- * Combine

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

    -- ** Difference
    , difference
    , differenceWith
    , differenceWithKey

    -- ** Intersection
    , intersection
    , intersectionWith
    , intersectionWithKey

    -- ** Symmetric difference
    , symmetricDifference

    -- ** Compose
    , compose

    -- ** General combining function
    , SimpleWhenMissing
    , SimpleWhenMatched
    , runWhenMatched
    , runWhenMissing
    , merge
    -- *** @WhenMatched@ tactics
    , dropMatched
    , 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
    , whenMissing

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

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

    -- ** Ordered lists
    , toAscList
    , toDescList
    , fromAscList
    , fromAscListWith
    , fromAscListWithKey
    , fromAscListUpsert
    , fromDistinctAscList
    , fromDescList
    , fromDescListUpsert

    -- * Filter
    , filter
    , filterKeys
    , 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

    -- * Utility
    , link
    , linkKey
    , bin
    , binCheckL
    , binCheckR
    , MonoState(..)
    , Stack(..)
    , ascLinkTop
    , ascLinkAll
    , descInsert
    , descLinkTop
    , descLinkAll
    , IntMapBuilder(..)
    , BStack(..)
    , emptyB
    , insertB
    , finishB
    , moveToB
    , MoveResult(..)
    , treeFromIntSetTip

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

import Data.Functor.Identity (Identity (..))
import Data.Semigroup (Semigroup(stimes))
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup((<>)))
#endif
import Data.Semigroup (stimesIdempotentMonoid)
import Data.Functor.Classes

import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
import Data.Bits
import qualified Data.Foldable as Foldable
import Data.Maybe (fromMaybe)
import Utils.Containers.Internal.Prelude hiding
  (lookup, map, filter, foldr, foldl, foldl', foldMap, null)
import Prelude ()

import Data.IntSet.Internal (IntSet)
import qualified Data.IntSet.Internal as IntSet
import Data.IntSet.Internal.IntTreeCommons
  ( Key
  , Prefix(..)
  , nomatch
  , left
  , signBranch
  , mask
  , branchMask
  , branchPrefix
  , TreeTreeBranch(..)
  , treeTreeBranch
  , i2w
  , Order(..)
  )
import Utils.Containers.Internal.BitUtil (shiftLL, shiftRL, iShiftRL, wordSize)
import Utils.Containers.Internal.Strict
  (StrictPair(..), StrictTriple(..), toPair)

#ifdef __GLASGOW_HASKELL__
import Data.Coerce
import Data.Data (Data(..), Constr, mkConstr, constrIndex,
                  DataType, mkDataType, gcast1)
import qualified Data.Data as Data
import GHC.Exts (build)
import qualified GHC.Exts as GHCExts
#  if __GLASGOW_HASKELL__ >= 914
import Language.Haskell.TH.Lift (Lift)
#  else
import Language.Haskell.TH.Syntax (Lift)
-- See Note [ Template Haskell Dependencies ]
import Language.Haskell.TH ()
#  endif
#endif
#if defined(__GLASGOW_HASKELL__) || defined(__MHS__)
import Text.Read
#endif
import qualified Control.Category as Category


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


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

-- See Note: Order of constructors
data IntMap a = Bin {-# UNPACK #-} !Prefix
                    !(IntMap a)
                    !(IntMap a)
              | Tip {-# UNPACK #-} !Key a
              | Nil

--
-- Note [IntMap structure and invariants]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- * Nil is never found as a child of Bin.
--
-- * The Prefix of a Bin indicates the common high-order bits that all keys in
--   the Bin share.
--
-- * The least significant set bit of the Int value of a Prefix is called the
--   mask bit.
--
-- * All the bits to the left of the mask bit are called the shared prefix. All
--   keys stored in the Bin begin with the shared prefix.
--
-- * All keys in the left child of the Bin have the mask bit unset, and all keys
--   in the right child have the mask bit set. It follows that
--
--   1. The Int value of the Prefix of a Bin is the smallest key that can be
--      present in the right child of the Bin.
--
--   2. All keys in the right child of a Bin are greater than keys in the
--      left child, with one exceptional situation. If the Bin separates
--      negative and non-negative keys, the mask bit is the sign bit and the
--      left child stores the non-negative keys while the right child stores the
--      negative keys.
--
-- * All bits to the right of the mask bit are set to 0 in a Prefix.
--
--
-- As an example, consider that on a 32-bit system we have a Bin with Prefix
--
-- 0b00000000000100100000011000000000
--                         ^ mask bit
--   ^^^^^^^^^^^^^^^^^^^^^^ shared prefix
--
-- The key
-- 0b00000000000100100000010000010100 belongs under this Bin, since it matches
-- the shared prefix. The mask bit is 0, so it belongs in the left child and not
-- the right.
--
-- The key
-- 0b00000000000100000000010000010100 does not belong under this Bin, since it
-- does not match the shared prefix.

-- See Note [Okasaki-Gill] for how the implementation here relates to the one in
-- Okasaki and Gill's paper.

#ifdef __GLASGOW_HASKELL__
-- | @since 0.6.6
deriving instance Lift a => Lift (IntMap a)
#endif

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

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

(!) :: IntMap a -> Key -> a
! :: forall a. IntMap a -> Int -> a
(!) IntMap a
m Int
k = Int -> IntMap a -> a
forall a. Int -> IntMap a -> a
find Int
k IntMap 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

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

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

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

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

-- | @mempty@ = 'empty'
instance Monoid (IntMap a) where
    mempty :: IntMap a
mempty  = IntMap a
forall a. IntMap a
empty
    mconcat :: [IntMap a] -> IntMap a
mconcat = [IntMap a] -> IntMap a
forall (f :: * -> *) a. Foldable f => f (IntMap a) -> IntMap a
unions
#if !MIN_VERSION_base(4,11,0)
    mappend = (<>)
#endif

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

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

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

          go :: t -> IntMap t -> t
go !t
m IntMap t
Nil = t
m
          go t
m (Tip Int
_ t
y) = t -> t -> t
forall a. Ord a => a -> a -> a
min t
m t
y
          go t
m (Bin Prefix
_ IntMap t
l IntMap t
r) = t -> IntMap t -> t
go (t -> IntMap t -> t
go t
m IntMap t
l) IntMap t
r
  {-# INLINABLE minimum #-}
  sum :: forall a. Num a => IntMap a -> a
sum = (a -> a -> a) -> a -> IntMap a -> a
forall b a. (b -> a -> b) -> b -> IntMap a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
  {-# INLINABLE sum #-}
  product :: forall a. Num a => IntMap a -> a
product = (a -> a -> a) -> a -> IntMap a -> a
forall b a. (b -> a -> b) -> b -> IntMap 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 IntMap where
    traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IntMap a -> f (IntMap b)
traverse a -> f b
f = (Int -> a -> f b) -> IntMap a -> f (IntMap b)
forall (t :: * -> *) a b.
Applicative t =>
(Int -> a -> t b) -> IntMap a -> t (IntMap b)
traverseWithKey (\Int
_ -> a -> f b
f)
    {-# INLINE traverse #-}

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

-- | @since 0.8
instance NFData1 IntMap where
    liftRnf :: forall a. (a -> ()) -> IntMap a -> ()
liftRnf a -> ()
rnfx = IntMap a -> ()
go
      where
      go :: IntMap a -> ()
go IntMap a
Nil         = ()
      go (Tip Int
_ a
v)   = a -> ()
rnfx a
v
      go (Bin Prefix
_ IntMap a
l IntMap a
r) = IntMap a -> ()
go IntMap a
l () -> () -> ()
forall a b. a -> b -> b
`seq` IntMap a -> ()
go IntMap a
r

#if __GLASGOW_HASKELL__

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

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

instance Data a => Data (IntMap a) where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IntMap a -> c (IntMap a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z IntMap a
im = ([(Int, a)] -> IntMap a) -> c ([(Int, a)] -> IntMap a)
forall g. g -> c g
z [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
fromList c ([(Int, a)] -> IntMap a) -> [(Int, a)] -> c (IntMap a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` (IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
toList IntMap a
im)
  toConstr :: IntMap a -> Constr
toConstr IntMap a
_     = Constr
fromListConstr
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (IntMap 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 ([(Int, a)] -> IntMap a) -> c (IntMap a)
forall b r. Data b => c (b -> r) -> c r
k (([(Int, a)] -> IntMap a) -> c ([(Int, a)] -> IntMap a)
forall r. r -> c r
z [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
fromList)
    Int
_ -> [Char] -> c (IntMap a)
forall a. HasCallStack => [Char] -> a
error [Char]
"gunfold"
  dataTypeOf :: IntMap a -> DataType
dataTypeOf IntMap a
_   = DataType
intMapDataType
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (IntMap a))
dataCast1 forall d. Data d => c (t d)
f    = c (t a) -> Maybe (c (IntMap 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
Data.Prefix

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

#endif

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

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

-- | \(O(n)\). Number of entries in the map.
--
-- __Note__: Unlike @Data.Map.'Data.Map.Lazy.size'@, this is /not/ \(O(1)\).
--
-- > size empty                                   == 0
-- > size (singleton 1 'a')                       == 1
-- > size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
--
-- See also: 'compareSize'
size :: IntMap a -> Int
size :: forall a. IntMap a -> Int
size = Int -> IntMap a -> Int
forall {t} {a}. Num t => t -> IntMap a -> t
go Int
0
  where
    go :: t -> IntMap a -> t
go !t
acc (Bin Prefix
_ IntMap a
l IntMap a
r) = t -> IntMap a -> t
go (t -> IntMap a -> t
go t
acc IntMap a
l) IntMap a
r
    go t
acc (Tip Int
_ a
_) = t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
acc
    go t
acc IntMap a
Nil = t
acc

-- | \(O(\min(n,c))\). Compare the number of entries in the map to an @Int@.
--
-- @compareSize m c@ returns the same result as @compare ('size' m) c@ but is
-- more efficient when @c@ is smaller than the size of the map.
--
-- @since 0.8.1
compareSize :: IntMap a -> Int -> Ordering
compareSize :: forall a. IntMap a -> Int -> Ordering
compareSize IntMap a
Nil Int
c0 = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
0 Int
c0
compareSize IntMap a
_ Int
c0 | Int
c0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Ordering
GT
compareSize IntMap a
t Int
c0 = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
0 (IntMap a -> Int -> Int
forall {t} {a}. (Num t, Ord t) => IntMap a -> t -> t
go IntMap a
t (Int
c0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
  where
    go :: IntMap a -> t -> t
go (Bin Prefix
_ IntMap a
_ IntMap a
_) t
0 = -t
1
    go (Bin Prefix
_ IntMap a
l IntMap a
r) t
c
      | t
c' t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
0 = t
c'
      | Bool
otherwise = IntMap a -> t -> t
go IntMap a
r t
c'
      where
        c' :: t
c' = IntMap a -> t -> t
go IntMap a
l (t
c t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
    go IntMap a
_ t
c = t
c

-- | \(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 -> IntMap a -> Bool
member :: forall a. Int -> IntMap a -> Bool
member !Int
k = IntMap a -> Bool
go
  where
    go :: IntMap a -> Bool
go (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = Bool
False
      | Int -> Prefix -> Bool
left Int
k Prefix
p    = IntMap a -> Bool
go IntMap a
l
      | Bool
otherwise   = IntMap a -> Bool
go IntMap a
r
    go (Tip Int
kx a
_) = Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
kx
    go IntMap 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 -> IntMap a -> Bool
notMember :: forall a. Int -> IntMap a -> Bool
notMember Int
k IntMap a
m = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> IntMap a -> Bool
forall a. Int -> IntMap a -> Bool
member Int
k IntMap a
m

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

-- See Note: Local 'go' functions and capturing
lookup :: Key -> IntMap a -> Maybe a
lookup :: forall a. Int -> IntMap a -> Maybe a
lookup !Int
k = IntMap a -> Maybe a
go
  where
    go :: IntMap a -> Maybe a
go (Bin Prefix
p IntMap a
l IntMap a
r) | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> Maybe a
go IntMap a
l
                   | Bool
otherwise = IntMap a -> Maybe a
go IntMap a
r
    go (Tip Int
kx a
x) | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
kx   = a -> Maybe a
forall a. a -> Maybe a
Just a
x
                  | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
    go IntMap a
Nil = Maybe a
forall a. Maybe a
Nothing

-- See Note: Local 'go' functions and capturing]
find :: Key -> IntMap a -> a
find :: forall a. Int -> IntMap a -> a
find !Int
k = IntMap a -> a
go
  where
    go :: IntMap a -> a
go (Bin Prefix
p IntMap a
l IntMap a
r) | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> a
go IntMap a
l
                   | Bool
otherwise = IntMap a -> a
go IntMap a
r
    go (Tip Int
kx a
x) | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
kx   = a
x
                  | Bool
otherwise = a
not_found
    go IntMap a
Nil = a
not_found

    not_found :: a
not_found = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char]
"IntMap.!: key " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
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 -> IntMap a -> a
findWithDefault :: forall a. a -> Int -> IntMap a -> a
findWithDefault a
def !Int
k = IntMap a -> a
go
  where
    go :: IntMap a -> a
go (Bin Prefix
p IntMap a
l IntMap a
r) | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = a
def
                   | Int -> Prefix -> Bool
left Int
k Prefix
p    = IntMap a -> a
go IntMap a
l
                   | Bool
otherwise   = IntMap a -> a
go IntMap a
r
    go (Tip Int
kx a
x) | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
kx   = a
x
                  | Bool
otherwise = a
def
    go IntMap 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 -> IntMap a -> Maybe (Key, a)
lookupLT :: forall a. Int -> IntMap a -> Maybe (Int, a)
lookupLT !Int
k IntMap a
t = case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 then IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
r IntMap a
l else IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
r
    IntMap a
_ -> IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
t
  where
    go :: IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p then IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
def else IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
r
      | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def IntMap a
l
      | Bool
otherwise = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
l IntMap a
r
    go IntMap a
def (Tip Int
ky a
y)
      | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
ky   = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
def
      | Bool
otherwise = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
ky, a
y)
    go IntMap a
def IntMap a
Nil = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap 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 -> IntMap a -> Maybe (Key, a)
lookupGT :: forall a. Int -> IntMap a -> Maybe (Int, a)
lookupGT !Int
k IntMap a
t = case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 then IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
l else IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
l IntMap a
r
    IntMap a
_ -> IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
t
  where
    go :: IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p then IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
l else IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
def
      | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
r IntMap a
l
      | Bool
otherwise = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def IntMap a
r
    go IntMap a
def (Tip Int
ky a
y)
      | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
ky   = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
def
      | Bool
otherwise = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
ky, a
y)
    go IntMap a
def IntMap a
Nil = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap 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 -> IntMap a -> Maybe (Key, a)
lookupLE :: forall a. Int -> IntMap a -> Maybe (Int, a)
lookupLE !Int
k IntMap a
t = case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 then IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
r IntMap a
l else IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
r
    IntMap a
_ -> IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
t
  where
    go :: IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p then IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
def else IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
r
      | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def IntMap a
l
      | Bool
otherwise = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
l IntMap a
r
    go IntMap a
def (Tip Int
ky a
y)
      | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ky    = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap a
def
      | Bool
otherwise = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
ky, a
y)
    go IntMap a
def IntMap a
Nil = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMax IntMap 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 -> IntMap a -> Maybe (Key, a)
lookupGE :: forall a. Int -> IntMap a -> Maybe (Int, a)
lookupGE !Int
k IntMap a
t = case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 then IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
l else IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
l IntMap a
r
    IntMap a
_ -> IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
forall a. IntMap a
Nil IntMap a
t
  where
    go :: IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p then IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
l else IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
def
      | Int -> Prefix -> Bool
left Int
k Prefix
p  = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
r IntMap a
l
      | Bool
otherwise = IntMap a -> IntMap a -> Maybe (Int, a)
go IntMap a
def IntMap a
r
    go IntMap a
def (Tip Int
ky a
y)
      | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ky    = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
def
      | Bool
otherwise = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
ky, a
y)
    go IntMap a
def IntMap a
Nil = IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
unsafeFindMin IntMap a
def


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

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

{--------------------------------------------------------------------
  Disjoint
--------------------------------------------------------------------}
-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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 :: IntMap a -> IntMap b -> Bool
disjoint :: forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
Nil IntMap b
_ = Bool
True
disjoint IntMap a
_ IntMap b
Nil = Bool
True
disjoint (Tip Int
kx a
_) IntMap b
ys = Int -> IntMap b -> Bool
forall a. Int -> IntMap a -> Bool
notMember Int
kx IntMap b
ys
disjoint IntMap a
xs (Tip Int
ky b
_) = Int -> IntMap a -> Bool
forall a. Int -> IntMap a -> Bool
notMember Int
ky IntMap a
xs
disjoint t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntMap b
t2@(Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
  TreeTreeBranch
ABL -> IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
l1 IntMap b
t2
  TreeTreeBranch
ABR -> IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
r1 IntMap b
t2
  TreeTreeBranch
BAL -> IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
t1 IntMap b
l2
  TreeTreeBranch
BAR -> IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
t1 IntMap b
r2
  TreeTreeBranch
EQL -> IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
l1 IntMap b
l2 Bool -> Bool -> Bool
&& IntMap a -> IntMap b -> Bool
forall a b. IntMap a -> IntMap b -> Bool
disjoint IntMap a
r1 IntMap b
r2
  TreeTreeBranch
NOM -> Bool
True

{--------------------------------------------------------------------
  Compose
--------------------------------------------------------------------}
-- | Given maps @bc@ and @ab@, relate the keys of @ab@ to the values of @bc@,
-- by using the values of @ab@ as keys for lookups in @bc@.
--
-- 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.IntMap.Strict" exposed a version of
-- 'compose' that forced the values of the output 'IntMap'. This version does
-- not force these values.
--
-- @since 0.6.3.1
compose :: IntMap c -> IntMap Int -> IntMap c
compose :: forall c. IntMap c -> IntMap Int -> IntMap c
compose IntMap c
bc !IntMap Int
ab
  | IntMap c -> Bool
forall a. IntMap a -> Bool
null IntMap c
bc = IntMap c
forall a. IntMap a
empty
  | Bool
otherwise = (Int -> Maybe c) -> IntMap Int -> IntMap c
forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
mapMaybe (IntMap c
bc IntMap c -> Int -> Maybe c
forall a. IntMap a -> Int -> Maybe a
!?) IntMap Int
ab

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

empty :: IntMap a
empty :: forall a. IntMap a
empty
  = IntMap a
forall a. IntMap 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 -> IntMap a
singleton :: forall a. Int -> a -> IntMap a
singleton Int
k a
x
  = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
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 -> IntMap a -> IntMap a
insert :: forall a. Int -> a -> IntMap a -> IntMap a
insert !Int
k a
x t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Prefix
p IntMap a
t
  | Int -> Prefix -> Bool
left Int
k Prefix
p    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p (Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
insert Int
k a
x IntMap a
l) IntMap a
r
  | Bool
otherwise   = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l (Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
insert Int
k a
x IntMap a
r)
insert Int
k a
x t :: IntMap a
t@(Tip Int
ky a
_)
  | Int
kInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
ky         = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x
  | Bool
otherwise     = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Int
ky IntMap a
t
insert Int
k a
x IntMap a
Nil = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
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"
--
-- Also see the performance note on 'fromListWith'.

insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWith :: forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
insertWith a -> a -> a
f Int
k a
x IntMap a
t
  = (Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
forall a. (Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
insertWithKey (\Int
_ a
x' a
y' -> a -> a -> a
f a
x' a
y') Int
k a
x IntMap 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"
--
-- Also see the performance note on 'fromListWith'.

insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
insertWithKey Int -> a -> a -> a
f !Int
k a
x t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Prefix
p IntMap a
t
  | Int -> Prefix -> Bool
left Int
k Prefix
p    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p ((Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
forall a. (Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
insertWithKey Int -> a -> a -> a
f Int
k a
x IntMap a
l) IntMap a
r
  | Bool
otherwise   = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l ((Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
forall a. (Int -> a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
insertWithKey Int -> a -> a -> a
f Int
k a
x IntMap a
r)
insertWithKey Int -> a -> a -> a
f Int
k a
x t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky       = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k (Int -> a -> a -> a
f Int
k a
x a
y)
  | Bool
otherwise     = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Int
ky IntMap a
t
insertWithKey Int -> a -> a -> a
_ Int
k a
x IntMap a
Nil = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
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")])
--
-- Also see the performance note on 'fromListWith'.

insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
insertLookupWithKey :: forall a.
(Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
insertLookupWithKey Int -> a -> a -> a
f !Int
k a
x t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = (Maybe a
forall a. Maybe a
Nothing,Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Prefix
p IntMap a
t)
  | Int -> Prefix -> Bool
left Int
k Prefix
p    = let (Maybe a
found,IntMap a
l') = (Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
forall a.
(Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
insertLookupWithKey Int -> a -> a -> a
f Int
k a
x IntMap a
l
                  in (Maybe a
found,Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l' IntMap a
r)
  | Bool
otherwise   = let (Maybe a
found,IntMap a
r') = (Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
forall a.
(Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
insertLookupWithKey Int -> a -> a -> a
f Int
k a
x IntMap a
r
                  in (Maybe a
found,Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r')
insertLookupWithKey Int -> a -> a -> a
f Int
k a
x t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky       = (a -> Maybe a
forall a. a -> Maybe a
Just a
y,Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k (Int -> a -> a -> a
f Int
k a
x a
y))
  | Bool
otherwise     = (Maybe a
forall a. Maybe a
Nothing,Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Int
ky IntMap a
t)
insertLookupWithKey Int -> a -> a -> a
_ Int
k a
x IntMap a
Nil = (Maybe a
forall a. Maybe a
Nothing,Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
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 -> IntMap a -> IntMap a
delete :: forall a. Int -> IntMap a -> IntMap a
delete !Int
k t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = IntMap a
t
  | Int -> Prefix -> Bool
left Int
k Prefix
p    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p (Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> IntMap a
delete Int
k IntMap a
l) IntMap a
r
  | Bool
otherwise   = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l (Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> IntMap a
delete Int
k IntMap a
r)
delete Int
k t :: IntMap a
t@(Tip Int
ky a
_)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky       = IntMap a
forall a. IntMap a
Nil
  | Bool
otherwise     = IntMap a
t
delete Int
_k IntMap a
Nil = IntMap a
forall a. IntMap a
Nil

-- | \(O(\min(n,W))\). Pop an entry from the map.
--
-- Returns @Nothing@ if the key is not in the map. Otherwise returns @Just@ the
-- value at the key and a map with the entry removed.
--
-- @
-- pop 1 (fromList [(0,"a"),(2,"b"),(4,"c")]) == Nothing
-- pop 2 (fromList [(0,"a"),(2,"b"),(4,"c")]) == Just ("b",fromList [(0,"a"),(4,"c")])
-- @
--
-- @since 0.8.1
pop :: Key -> IntMap a -> Maybe (a, IntMap a)
pop :: forall a. Int -> IntMap a -> Maybe (a, IntMap a)
pop Int
k0 IntMap a
t0 = case Int -> IntMap a -> Popped Any a
forall {a} {k}. Int -> IntMap a -> Popped k a
go Int
k0 IntMap a
t0 of
  Popped (Just a
y) IntMap a
t -> (a, IntMap a) -> Maybe (a, IntMap a)
forall a. a -> Maybe a
Just (a
y, IntMap a
t)
  Popped Any a
_ -> Maybe (a, IntMap a)
forall a. Maybe a
Nothing
  where
    go :: Int -> IntMap a -> Popped k a
go !Int
k (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped Maybe a
forall a. Maybe a
Nothing IntMap a
forall a. IntMap a
Nil
      | Int -> Prefix -> Bool
left Int
k Prefix
p = case Int -> IntMap a -> Popped k a
go Int
k IntMap a
l of
          Popped y :: Maybe a
y@(Just a
_) IntMap a
l' -> Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped Maybe a
y (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r)
          Popped k a
q -> Popped k a
q
      | Bool
otherwise = case Int -> IntMap a -> Popped k a
go Int
k IntMap a
r of
          Popped y :: Maybe a
y@(Just a
_) IntMap a
r' -> Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped Maybe a
y (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
r')
          Popped k a
q -> Popped k a
q
    go !Int
k (Tip Int
ky a
y)
      | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky = Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped (a -> Maybe a
forall a. a -> Maybe a
Just a
y) IntMap a
forall a. IntMap a
Nil
      | Bool
otherwise = Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped Maybe a
forall a. Maybe a
Nothing IntMap a
forall a. IntMap a
Nil
    go !Int
_ IntMap a
Nil = Maybe a -> IntMap a -> Popped k a
forall k a. Maybe a -> IntMap a -> Popped k a
Popped Maybe a
forall a. Maybe a
Nothing IntMap a
forall a. IntMap a
Nil

-- See Note [Popped impl] in Data.Map.Internal
data Popped k a = Popped
#if __GLASGOW_HASKELL__ >= 906
  {-# UNPACK #-}
#endif
  !(Maybe a)
  !(IntMap a)

-- | \(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 -> IntMap a -> IntMap a
adjust :: forall a. (a -> a) -> Int -> IntMap a -> IntMap a
adjust a -> a
f Int
k IntMap a
m
  = (Int -> a -> a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> a) -> Int -> IntMap a -> IntMap a
adjustWithKey (\Int
_ a
x -> a -> a
f a
x) Int
k IntMap 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 -> IntMap a -> IntMap a
adjustWithKey :: forall a. (Int -> a -> a) -> Int -> IntMap a -> IntMap a
adjustWithKey Int -> a -> a
f !Int
k (Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
left Int
k Prefix
p      = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p ((Int -> a -> a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> a) -> Int -> IntMap a -> IntMap a
adjustWithKey Int -> a -> a
f Int
k IntMap a
l) IntMap a
r
  | Bool
otherwise     = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l ((Int -> a -> a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> a) -> Int -> IntMap a -> IntMap a
adjustWithKey Int -> a -> a
f Int
k IntMap a
r)
adjustWithKey Int -> a -> a
f Int
k t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky       = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky (Int -> a -> a
f Int
k a
y)
  | Bool
otherwise     = IntMap a
t
adjustWithKey Int -> a -> a
_ Int
_ IntMap a
Nil = IntMap a
forall a. IntMap 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 -> IntMap a -> IntMap a
update :: forall a. (a -> Maybe a) -> Int -> IntMap a -> IntMap a
update a -> Maybe a
f
  = (Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
updateWithKey (\Int
_ 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 -> IntMap a -> IntMap a
updateWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
updateWithKey Int -> a -> Maybe a
f !Int
k (Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
left Int
k Prefix
p      = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
updateWithKey Int -> a -> Maybe a
f Int
k IntMap a
l) IntMap a
r
  | Bool
otherwise     = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l ((Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> Int -> IntMap a -> IntMap a
updateWithKey Int -> a -> Maybe a
f Int
k IntMap a
r)
updateWithKey Int -> a -> Maybe a
f Int
k t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky       = case (Int -> a -> Maybe a
f Int
k a
y) of
                      Just a
y' -> Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky a
y'
                      Maybe a
Nothing -> IntMap a
forall a. IntMap a
Nil
  | Bool
otherwise     = IntMap a
t
updateWithKey Int -> a -> Maybe a
_ Int
_ IntMap a
Nil = IntMap a
forall a. IntMap a
Nil

-- | \(O(\min(n,W))\). Update the value at a key or insert a value if the key is
-- not in the map.
--
-- @
-- let inc = maybe 1 (+1)
-- upsert inc 100 (fromList [(100,1),(300,2)]) == fromList [(100,2),(300,2)]
-- upsert inc 200 (fromList [(100,1),(300,2)]) == fromList [(100,1),(200,1),(300,2)]
-- @
--
-- @since 0.8.1
upsert :: (Maybe a -> a) -> Key -> IntMap a -> IntMap a
upsert :: forall a. (Maybe a -> a) -> Int -> IntMap a -> IntMap a
upsert Maybe a -> a
f !Int
k t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k (Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing)) Prefix
p IntMap a
t
  | Int -> Prefix -> Bool
left Int
k Prefix
p = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p ((Maybe a -> a) -> Int -> IntMap a -> IntMap a
forall a. (Maybe a -> a) -> Int -> IntMap a -> IntMap a
upsert Maybe a -> a
f Int
k IntMap a
l) IntMap a
r
  | Bool
otherwise = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l ((Maybe a -> a) -> Int -> IntMap a -> IntMap a
forall a. (Maybe a -> a) -> Int -> IntMap a -> IntMap a
upsert Maybe a -> a
f Int
k IntMap a
r)
upsert Maybe a -> a
f !Int
k t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky (Maybe a -> a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
y))
  | Bool
otherwise = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k (Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing)) Int
ky IntMap a
t
upsert Maybe a -> a
f !Int
k IntMap a
Nil = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k (Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing)

-- | \(O(\min(n,W))\). Look up and update.
-- This function returns the 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")
--
-- See also: 'pop'

updateLookupWithKey ::  (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a)
updateLookupWithKey :: forall a.
(Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
updateLookupWithKey Int -> a -> Maybe a
f !Int
k (Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
left Int
k Prefix
p      = let !(Maybe a
found,IntMap a
l') = (Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
forall a.
(Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
updateLookupWithKey Int -> a -> Maybe a
f Int
k IntMap a
l
                    in (Maybe a
found,Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r)
  | Bool
otherwise     = let !(Maybe a
found,IntMap a
r') = (Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
forall a.
(Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
updateLookupWithKey Int -> a -> Maybe a
f Int
k IntMap a
r
                    in (Maybe a
found,Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
r')
updateLookupWithKey Int -> a -> Maybe a
f Int
k t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
kInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
ky         = case (Int -> a -> Maybe a
f Int
k a
y) of
                      Just a
y' -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y,Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky a
y')
                      Maybe a
Nothing -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y,IntMap a
forall a. IntMap a
Nil)
  | Bool
otherwise     = (Maybe a
forall a. Maybe a
Nothing,IntMap a
t)
updateLookupWithKey Int -> a -> Maybe a
_ Int
_ IntMap a
Nil = (Maybe a
forall a. Maybe a
Nothing,IntMap a
forall a. IntMap 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 'IntMap'.
-- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.
alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
alter :: forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f !Int
k t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Int -> Prefix -> Bool
nomatch Int
k Prefix
p = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                    Maybe a
Nothing -> IntMap a
t
                    Just a
x -> Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Prefix
p IntMap a
t
  | Int -> Prefix -> Bool
left Int
k Prefix
p    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f Int
k IntMap a
l) IntMap a
r
  | Bool
otherwise   = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l ((Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f Int
k IntMap a
r)
alter Maybe a -> Maybe a
f Int
k t :: IntMap a
t@(Tip Int
ky a
y)
  | Int
kInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
ky         = case Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
y) of
                      Just a
x -> Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky a
x
                      Maybe a
Nothing -> IntMap a
forall a. IntMap a
Nil
  | Bool
otherwise     = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                      Just a
x -> Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x) Int
ky IntMap a
t
                      Maybe a
Nothing -> Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
ky a
y
alter Maybe a -> Maybe a
f Int
k IntMap a
Nil     = case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                      Just a
x -> Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
x
                      Maybe a
Nothing -> IntMap a
forall a. IntMap 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 'IntMap'.  In short : @'lookup' k \<$\> 'alterF' f k m = f
-- ('lookup' k m)@.
--
-- '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@.
--
-- === Examples
--
-- @
-- -- Lookup the value at the key, and also remove the existing value or set a new value.
-- lookupAndSet :: Key -> Maybe a -> IntMap a -> (Maybe a, IntMap a)
-- lookupAndSet k new = alterF (\\old -> (old, new)) k
-- @
--
-- @
-- -- Delete the value at the key. If it is absent the result is Nothing.
-- mustDelete :: Key -> IntMap a -> Maybe (IntMap a)
-- mustDelete = alterF (Nothing <$)
-- @
--
-- @since 0.5.8

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

{--------------------------------------------------------------------
  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 (IntMap a) -> IntMap a
unions :: forall (f :: * -> *) a. Foldable f => f (IntMap a) -> IntMap a
unions f (IntMap a)
xs
  = (IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> f (IntMap a) -> IntMap 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' IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
union IntMap a
forall a. IntMap a
empty f (IntMap a)
xs
{-# INLINE unions #-} -- Inline for list fusion

-- | 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 (IntMap a) -> IntMap a
unionsWith :: forall (f :: * -> *) a.
Foldable f =>
(a -> a -> a) -> f (IntMap a) -> IntMap a
unionsWith a -> a -> a
f f (IntMap a)
ts
  = (IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> f (IntMap a) -> IntMap 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) -> IntMap a -> IntMap a -> IntMap a
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWith a -> a -> a
f) IntMap a
forall a. IntMap a
empty f (IntMap a)
ts
{-# INLINE unionsWith #-} -- Inline for list fusion

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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 :: IntMap a -> IntMap a -> IntMap a
union :: forall a. IntMap a -> IntMap a -> IntMap a
union IntMap a
m1 IntMap a
m2
  = (Prefix -> IntMap a -> IntMap a -> IntMap a)
-> (IntMap a -> IntMap a -> IntMap a)
-> (IntMap a -> IntMap a)
-> (IntMap a -> IntMap a)
-> IntMap a
-> IntMap a
-> IntMap a
forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin IntMap a -> IntMap a -> IntMap a
forall a b. a -> b -> a
const IntMap a -> IntMap a
forall a. a -> a
id IntMap a -> IntMap a
forall a. a -> a
id IntMap a
m1 IntMap a
m2

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- The union with a combining function.
--
-- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
--
-- Also see the performance note on 'fromListWith'.

unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWith :: forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWith a -> a -> a
f = (Int -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
forall a. (Int -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey (\Int
_ a
x a
y -> a -> a -> a
f a
x a
y)
{-# INLINE unionWith #-}

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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")]
--
-- Also see the performance note on 'fromListWith'.

unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey :: forall a. (Int -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey Int -> a -> a -> a
f IntMap a
m1 IntMap a
m2
  = (Prefix -> IntMap a -> IntMap a -> IntMap a)
-> (IntMap a -> IntMap a -> IntMap a)
-> (IntMap a -> IntMap a)
-> (IntMap a -> IntMap a)
-> IntMap a
-> IntMap a
-> IntMap a
forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin IntMap a -> IntMap a -> IntMap a
f' IntMap a -> IntMap a
forall a. a -> a
id IntMap a -> IntMap a
forall a. a -> a
id IntMap a
m1 IntMap a
m2
  where
    f' :: IntMap a -> IntMap a -> IntMap a
f' (Tip Int
k1 a
x1) (Tip Int
_k2 a
x2) = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k1 (Int -> a -> a -> a
f Int
k1 a
x1 a
x2)
    f' IntMap a
_ IntMap a
_ = [Char] -> IntMap a
forall a. HasCallStack => [Char] -> a
error [Char]
"not Tip"
{-# INLINABLE unionWithKey #-} -- See Note [INLINABLE to expose unfoldings]

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

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

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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) -> IntMap a -> IntMap b -> IntMap a
differenceWith :: forall a b. (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWith a -> b -> Maybe a
f = (Int -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
forall a b.
(Int -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey (\Int
_ a
x b
y -> a -> b -> Maybe a
f a
x b
y)
{-# INLINE differenceWith #-}

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey :: forall a b.
(Int -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey Int -> a -> b -> Maybe a
f IntMap a
m1 IntMap b
m2
  = (Int -> a -> b -> Maybe a)
-> (IntMap a -> IntMap a)
-> (IntMap b -> IntMap a)
-> IntMap a
-> IntMap b
-> IntMap a
forall a b c.
(Int -> a -> b -> Maybe c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey Int -> a -> b -> Maybe a
f IntMap a -> IntMap a
forall a. a -> a
id (IntMap a -> IntMap b -> IntMap a
forall a b. a -> b -> a
const IntMap a
forall a. IntMap a
Nil) IntMap a
m1 IntMap b
m2
{-# INLINABLE differenceWithKey #-} -- See Note [INLINABLE to expose unfoldings]

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- Remove all the keys in a given set from a map.
--
-- @
-- m \`withoutKeys\` s = 'filterWithKey' (\\k _ -> k ``IntSet.notMember`` s) m
-- @
--
-- @since 0.5.8
withoutKeys :: IntMap a -> IntSet -> IntMap a
withoutKeys :: forall a. IntMap a -> IntSet -> IntMap a
withoutKeys t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntSet
t2@(IntSet.Bin Prefix
p2 IntSet
l2 IntSet
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
  TreeTreeBranch
ABL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p1 (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
l1 IntSet
t2) IntMap a
r1
  TreeTreeBranch
ABR -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p1 IntMap a
l1 (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
r1 IntSet
t2)
  TreeTreeBranch
BAL -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
t1 IntSet
l2
  TreeTreeBranch
BAR -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
t1 IntSet
r2
  TreeTreeBranch
EQL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p1 (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
l1 IntSet
l2) (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
withoutKeys IntMap a
r1 IntSet
r2)
  TreeTreeBranch
NOM -> IntMap a
t1
  where
withoutKeys t1 :: IntMap a
t1@(Bin Prefix
_ IntMap a
_ IntMap a
_) (IntSet.Tip Int
p2 Word
bm2) = IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
withoutKeysTip IntMap a
t1 Int
p2 Word
bm2
withoutKeys t1 :: IntMap a
t1@(Bin Prefix
_ IntMap a
_ IntMap a
_) IntSet
IntSet.Nil = IntMap a
t1
withoutKeys t1 :: IntMap a
t1@(Tip Int
k1 a
_) IntSet
t2
    | Int
k1 Int -> IntSet -> Bool
`IntSet.member` IntSet
t2 = IntMap a
forall a. IntMap a
Nil
    | Bool
otherwise = IntMap a
t1
withoutKeys IntMap a
Nil IntSet
_ = IntMap a
forall a. IntMap a
Nil

withoutKeysTip :: IntMap a -> Int -> IntSet.BitMap -> IntMap a
withoutKeysTip :: forall a. IntMap a -> Int -> Word -> IntMap a
withoutKeysTip t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r) !Int
p2 !Word
bm2
  | Int -> Int
IntSet.suffixOf (Prefix -> Int
unPrefix Prefix
p) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 =
      if Int -> Int
IntSet.prefixOf (Prefix -> Int
unPrefix Prefix
p) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
p2
      then IntMap a -> Word -> IntMap a
forall a. IntMap a -> Word -> IntMap a
restrictBM IntMap a
t (Word -> Word
forall a. Bits a => a -> a
complement Word
bm2)
      else IntMap a
t
  | Int -> Prefix -> Bool
nomatch Int
p2 Prefix
p = IntMap a
t
  | Int -> Prefix -> Bool
left Int
p2 Prefix
p    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p (IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
withoutKeysTip IntMap a
l Int
p2 Word
bm2) IntMap a
r
  | Bool
otherwise    = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l (IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
withoutKeysTip IntMap a
r Int
p2 Word
bm2)
withoutKeysTip t :: IntMap a
t@(Tip Int
kx a
_) !Int
p2 !Word
bm2
  | Int -> Int
IntSet.prefixOf Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
p2 Bool -> Bool -> Bool
&& Int -> Word
IntSet.bitmapOf Int
kx Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. Word
bm2 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
0 = IntMap a
forall a. IntMap a
Nil
  | Bool
otherwise = IntMap a
t
withoutKeysTip IntMap a
Nil !Int
_ !Word
_ = IntMap a
forall a. IntMap a
Nil

{--------------------------------------------------------------------
  Intersection
--------------------------------------------------------------------}
-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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 :: IntMap a -> IntMap b -> IntMap a
intersection :: forall a b. IntMap a -> IntMap b -> IntMap a
intersection IntMap a
m1 IntMap b
m2
  = (Prefix -> IntMap a -> IntMap a -> IntMap a)
-> (IntMap a -> IntMap b -> IntMap a)
-> (IntMap a -> IntMap a)
-> (IntMap b -> IntMap a)
-> IntMap a
-> IntMap b
-> IntMap a
forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin IntMap a -> IntMap b -> IntMap a
forall a b. a -> b -> a
const (IntMap a -> IntMap a -> IntMap a
forall a b. a -> b -> a
const IntMap a
forall a. IntMap a
Nil) (IntMap a -> IntMap b -> IntMap a
forall a b. a -> b -> a
const IntMap a
forall a. IntMap a
Nil) IntMap a
m1 IntMap b
m2


-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- The restriction of a map to the keys in a set.
--
-- @
-- m \`restrictKeys\` s = 'filterWithKey' (\\k _ -> k ``IntSet.member`` s) m
-- @
--
-- @since 0.5.8
restrictKeys :: IntMap a -> IntSet -> IntMap a
restrictKeys :: forall a. IntMap a -> IntSet -> IntMap a
restrictKeys t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntSet
t2@(IntSet.Bin Prefix
p2 IntSet
l2 IntSet
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
  TreeTreeBranch
ABL -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
l1 IntSet
t2
  TreeTreeBranch
ABR -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
r1 IntSet
t2
  TreeTreeBranch
BAL -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
t1 IntSet
l2
  TreeTreeBranch
BAR -> IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
t1 IntSet
r2
  TreeTreeBranch
EQL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p1 (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
l1 IntSet
l2) (IntMap a -> IntSet -> IntMap a
forall a. IntMap a -> IntSet -> IntMap a
restrictKeys IntMap a
r1 IntSet
r2)
  TreeTreeBranch
NOM -> IntMap a
forall a. IntMap a
Nil
restrictKeys t1 :: IntMap a
t1@(Bin Prefix
_ IntMap a
_ IntMap a
_) (IntSet.Tip Int
p2 Word
bm2) = IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
restrictKeysTip IntMap a
t1 Int
p2 Word
bm2
restrictKeys (Bin Prefix
_ IntMap a
_ IntMap a
_) IntSet
IntSet.Nil = IntMap a
forall a. IntMap a
Nil
restrictKeys t1 :: IntMap a
t1@(Tip Int
k1 a
_) IntSet
t2
    | Int
k1 Int -> IntSet -> Bool
`IntSet.member` IntSet
t2 = IntMap a
t1
    | Bool
otherwise = IntMap a
forall a. IntMap a
Nil
restrictKeys IntMap a
Nil IntSet
_ = IntMap a
forall a. IntMap a
Nil

restrictKeysTip :: IntMap a -> Int -> IntSet.BitMap -> IntMap a
restrictKeysTip :: forall a. IntMap a -> Int -> Word -> IntMap a
restrictKeysTip t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r) !Int
p2 !Word
bm2
  | Int -> Int
IntSet.suffixOf (Prefix -> Int
unPrefix Prefix
p) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 =
      if Int -> Int
IntSet.prefixOf (Prefix -> Int
unPrefix Prefix
p) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
p2
      then IntMap a -> Word -> IntMap a
forall a. IntMap a -> Word -> IntMap a
restrictBM IntMap a
t Word
bm2
      else IntMap a
forall a. IntMap a
Nil
  | Int -> Prefix -> Bool
nomatch Int
p2 Prefix
p = IntMap a
forall a. IntMap a
Nil
  | Int -> Prefix -> Bool
left Int
p2 Prefix
p    = IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
restrictKeysTip IntMap a
l Int
p2 Word
bm2
  | Bool
otherwise    = IntMap a -> Int -> Word -> IntMap a
forall a. IntMap a -> Int -> Word -> IntMap a
restrictKeysTip IntMap a
r Int
p2 Word
bm2
restrictKeysTip t :: IntMap a
t@(Tip Int
kx a
_) !Int
p2 !Word
bm2
  | Int -> Int
IntSet.prefixOf Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
p2 Bool -> Bool -> Bool
&& Int -> Word
IntSet.bitmapOf Int
kx Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. Word
bm2 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
0 = IntMap a
t
  | Bool
otherwise = IntMap a
forall a. IntMap a
Nil
restrictKeysTip IntMap a
Nil !Int
_ !Word
_ = IntMap a
forall a. IntMap a
Nil

-- Must be called on an IntMap whose keys fit in the given IntSet BitMap's Tip.
-- Keeps keys that match the BitMap.
-- Returns early as an optimization, i.e. if the tree can be entirely kept or
-- discarded there is no need to recursively visit the children.
restrictBM :: IntMap a -> IntSet.BitMap -> IntMap a
restrictBM :: forall a. IntMap a -> Word -> IntMap a
restrictBM t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r) !Word
bm
  | Int
bm' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = IntMap a
forall a. IntMap a
Nil
  | Int
bm' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== -Int
1 = IntMap a
t
  | Bool
otherwise = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p (IntMap a -> Word -> IntMap a
forall a. IntMap a -> Word -> IntMap a
restrictBM IntMap a
l Word
bm) (IntMap a -> Word -> IntMap a
forall a. IntMap a -> Word -> IntMap a
restrictBM IntMap a
r Word
bm)
  where
    -- Here we care about the "submask" of bm corresponding the current Bin's
    -- range. So we create bm', where this submask is at the lowest position and
    -- and all other bits are set to the highest bit of the submask (using an
    -- arithmetic shiftR). Now bm' is 0 when the submask is empty and -1 when
    -- the submask is full.
    px :: Int
px = Int -> Int
IntSet.suffixOf (Prefix -> Int
unPrefix Prefix
p)
    px1 :: Int
px1 = Int
px Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
    min_ :: Int
min_ = Int
px Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
px1
    max_ :: Int
max_ = Int
px Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
px1
    sh :: Int
sh = (Int
wordSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
max_
    bm' :: Int
bm' = (Word -> Int
w2i Word
bm Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
sh) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
sh Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
min_)
restrictBM t :: IntMap a
t@(Tip Int
k a
_) !Word
bm
  | Int -> Word
IntSet.bitmapOf Int
k Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. Word
bm Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
0 = IntMap a
t
  | Bool
otherwise = IntMap a
forall a. IntMap a
Nil
restrictBM IntMap a
Nil !Word
_ = IntMap a
forall a. IntMap a
Nil

w2i :: Word -> Int
w2i :: Word -> Int
w2i = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- The intersection with a combining function.
--
-- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"

intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWith :: forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWith a -> b -> c
f = (Int -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
forall a b c.
(Int -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWithKey (\Int
_ a
x b
y -> a -> b -> c
f a
x b
y)
{-# INLINE intersectionWith #-}

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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) -> IntMap a -> IntMap b -> IntMap c
intersectionWithKey :: forall a b c.
(Int -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWithKey Int -> a -> b -> c
f IntMap a
m1 IntMap b
m2
  = (Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin IntMap a -> IntMap b -> IntMap c
f' (IntMap c -> IntMap a -> IntMap c
forall a b. a -> b -> a
const IntMap c
forall a. IntMap a
Nil) (IntMap c -> IntMap b -> IntMap c
forall a b. a -> b -> a
const IntMap c
forall a. IntMap a
Nil) IntMap a
m1 IntMap b
m2
  where
    f' :: IntMap a -> IntMap b -> IntMap c
f' (Tip Int
k1 a
x1) (Tip Int
_k2 b
x2) = Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k1 (Int -> a -> b -> c
f Int
k1 a
x1 b
x2)
    f' IntMap a
_ IntMap b
_ = [Char] -> IntMap c
forall a. HasCallStack => [Char] -> a
error [Char]
"not Tip"
-- See Note [INLINABLE to expose unfoldings]
{-# INLINABLE intersectionWithKey #-}

{--------------------------------------------------------------------
  Symmetric difference
--------------------------------------------------------------------}

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- The symmetric difference of two maps.
--
-- The result contains entries whose keys appear in exactly one of the two maps.
--
-- @
-- symmetricDifference
--   (fromList [(0,\'q\'),(2,\'b\'),(4,\'w\'),(6,\'o\')])
--   (fromList [(0,\'e\'),(3,\'r\'),(6,\'t\'),(9,\'s\')])
-- ==
-- fromList [(2,\'b\'),(3,\'r\'),(4,\'w\'),(9,\'s\')]
-- @
--
-- @since 0.8
symmetricDifference :: IntMap a -> IntMap a -> IntMap a
symmetricDifference :: forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntMap a
t2@(Bin Prefix
p2 IntMap a
l2 IntMap a
r2) =
  case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
    TreeTreeBranch
ABL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p1 (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
l1 IntMap a
t2) IntMap a
r1
    TreeTreeBranch
ABR -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p1 IntMap a
l1 (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
r1 IntMap a
t2)
    TreeTreeBranch
BAL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p2 (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
t1 IntMap a
l2) IntMap a
r2
    TreeTreeBranch
BAR -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p2 IntMap a
l2 (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
t1 IntMap a
r2)
    TreeTreeBranch
EQL -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p1 (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
l1 IntMap a
l2) (IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
symmetricDifference IntMap a
r1 IntMap a
r2)
    TreeTreeBranch
NOM -> Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link (Prefix -> Int
unPrefix Prefix
p1) IntMap a
t1 (Prefix -> Int
unPrefix Prefix
p2) IntMap a
t2
symmetricDifference t1 :: IntMap a
t1@(Bin Prefix
_ IntMap a
_ IntMap a
_) t2 :: IntMap a
t2@(Tip Int
k2 a
_) = IntMap a -> Int -> IntMap a -> IntMap a
forall a. IntMap a -> Int -> IntMap a -> IntMap a
symDiffTip IntMap a
t2 Int
k2 IntMap a
t1
symmetricDifference t1 :: IntMap a
t1@(Bin Prefix
_ IntMap a
_ IntMap a
_) IntMap a
Nil = IntMap a
t1
symmetricDifference t1 :: IntMap a
t1@(Tip Int
k1 a
_) IntMap a
t2 = IntMap a -> Int -> IntMap a -> IntMap a
forall a. IntMap a -> Int -> IntMap a -> IntMap a
symDiffTip IntMap a
t1 Int
k1 IntMap a
t2
symmetricDifference IntMap a
Nil IntMap a
t2 = IntMap a
t2

symDiffTip :: IntMap a -> Int -> IntMap a -> IntMap a
symDiffTip :: forall a. IntMap a -> Int -> IntMap a -> IntMap a
symDiffTip !IntMap a
t1 !Int
k1 = IntMap a -> IntMap a
go
  where
    go :: IntMap a -> IntMap a
go t2 :: IntMap a
t2@(Bin Prefix
p2 IntMap a
l2 IntMap a
r2)
      | Int -> Prefix -> Bool
nomatch Int
k1 Prefix
p2 = Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k1 IntMap a
t1 Prefix
p2 IntMap a
t2
      | Int -> Prefix -> Bool
left Int
k1 Prefix
p2 = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p2 (IntMap a -> IntMap a
go IntMap a
l2) IntMap a
r2
      | Bool
otherwise = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p2 IntMap a
l2 (IntMap a -> IntMap a
go IntMap a
r2)
    go t2 :: IntMap a
t2@(Tip Int
k2 a
_)
      | Int
k1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k2 = IntMap a
forall a. IntMap a
Nil
      | Bool
otherwise = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k1 IntMap a
t1 Int
k2 IntMap a
t2
    go IntMap a
Nil = IntMap a
t1

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

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- 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').
--
-- __Warning__: 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
-- 'IntMap'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@.

-- See Note [IntMap merge complexity]
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
             -> IntMap a -> IntMap b -> IntMap c
mergeWithKey :: forall a b c.
(Int -> a -> b -> Maybe c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey Int -> a -> b -> Maybe c
f IntMap a -> IntMap c
g1 IntMap b -> IntMap c
g2 = (Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin IntMap a -> IntMap b -> IntMap c
combine IntMap a -> IntMap c
g1 IntMap b -> IntMap c
g2
  where
        combine :: IntMap a -> IntMap b -> IntMap c
combine (Tip Int
k1 a
x1) (Tip Int
_k2 b
x2) =
          case Int -> a -> b -> Maybe c
f Int
k1 a
x1 b
x2 of
            Maybe c
Nothing -> IntMap c
forall a. IntMap a
Nil
            Just c
x -> Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k1 c
x
        combine IntMap a
_ IntMap b
_ = [Char] -> IntMap c
forall a. HasCallStack => [Char] -> a
error [Char]
"not Tip"
        {-# 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 -> IntMap c -> IntMap c -> IntMap c)
              -> (IntMap a -> IntMap b -> IntMap c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
              -> IntMap a -> IntMap b -> IntMap c
mergeWithKey' :: forall c a b.
(Prefix -> IntMap c -> IntMap c -> IntMap c)
-> (IntMap a -> IntMap b -> IntMap c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey' Prefix -> IntMap c -> IntMap c -> IntMap c
bin' IntMap a -> IntMap b -> IntMap c
f IntMap a -> IntMap c
g1 IntMap b -> IntMap c
g2 = IntMap a -> IntMap b -> IntMap c
go
  where
    go :: IntMap a -> IntMap b -> IntMap c
go t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntMap b
t2@(Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
      TreeTreeBranch
ABL -> Prefix -> IntMap c -> IntMap c -> IntMap c
bin' Prefix
p1 (IntMap a -> IntMap b -> IntMap c
go IntMap a
l1 IntMap b
t2) (IntMap a -> IntMap c
g1 IntMap a
r1)
      TreeTreeBranch
ABR -> Prefix -> IntMap c -> IntMap c -> IntMap c
bin' Prefix
p1 (IntMap a -> IntMap c
g1 IntMap a
l1) (IntMap a -> IntMap b -> IntMap c
go IntMap a
r1 IntMap b
t2)
      TreeTreeBranch
BAL -> Prefix -> IntMap c -> IntMap c -> IntMap c
bin' Prefix
p2 (IntMap a -> IntMap b -> IntMap c
go IntMap a
t1 IntMap b
l2) (IntMap b -> IntMap c
g2 IntMap b
r2)
      TreeTreeBranch
BAR -> Prefix -> IntMap c -> IntMap c -> IntMap c
bin' Prefix
p2 (IntMap b -> IntMap c
g2 IntMap b
l2) (IntMap a -> IntMap b -> IntMap c
go IntMap a
t1 IntMap b
r2)
      TreeTreeBranch
EQL -> Prefix -> IntMap c -> IntMap c -> IntMap c
bin' Prefix
p1 (IntMap a -> IntMap b -> IntMap c
go IntMap a
l1 IntMap b
l2) (IntMap a -> IntMap b -> IntMap c
go IntMap a
r1 IntMap b
r2)
      TreeTreeBranch
NOM -> Int -> IntMap c -> Int -> IntMap c -> IntMap c
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
maybe_link (Prefix -> Int
unPrefix Prefix
p1) (IntMap a -> IntMap c
g1 IntMap a
t1) (Prefix -> Int
unPrefix Prefix
p2) (IntMap b -> IntMap c
g2 IntMap b
t2)

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

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

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

    go IntMap a
Nil IntMap b
Nil = IntMap c
forall a. IntMap a
Nil

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

    maybe_link :: Int -> IntMap a -> Int -> IntMap a -> IntMap a
maybe_link Int
_ IntMap a
Nil Int
_ IntMap a
t2 = IntMap a
t2
    maybe_link Int
_ IntMap a
t1 Int
_ IntMap a
Nil = IntMap a
t1
    maybe_link Int
k1 IntMap a
t1 Int
k2 IntMap a
t2 = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k1 IntMap a
t1 Int
k2 IntMap 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 -> IntMap x -> f (IntMap y)
missingSubtree :: IntMap x -> f (IntMap y)
  , forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey :: Key -> x -> f (Maybe y)}

-- | @since 0.5.9
instance 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.
Monad f =>
(a -> b) -> WhenMissing f x a -> WhenMissing f x b
mapWhenMissing
  {-# INLINE fmap #-}


-- | @since 0.5.9
instance 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 =
      (Int -> a -> f (Maybe c)) -> WhenMissing f a c
forall (f :: * -> *) x y.
Applicative f =>
(Int -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing ((Int -> a -> f (Maybe c)) -> WhenMissing f a c)
-> (Int -> a -> f (Maybe c)) -> WhenMissing f a c
forall a b. (a -> b) -> a -> b
$ \ Int
k a
x -> do
        Maybe b
y <- WhenMissing f a b -> Int -> a -> f (Maybe b)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f a b
g Int
k a
x
        case Maybe b
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 -> Int -> b -> f (Maybe c)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f b c
f Int
k b
q
    {-# INLINE id #-}
    {-# INLINE (.) #-}


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


-- | Equivalent to @ReaderT k (ReaderT x (MaybeT f))@.
--
-- @since 0.5.9
instance 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 =
    (Int -> x -> f (Maybe b)) -> WhenMissing f x b
forall (f :: * -> *) x y.
Applicative f =>
(Int -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing ((Int -> x -> f (Maybe b)) -> WhenMissing f x b)
-> (Int -> x -> f (Maybe b)) -> WhenMissing f x b
forall a b. (a -> b) -> a -> b
$ \Int
k x
x -> do
      Maybe a
res1 <- WhenMissing f x a -> Int -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f x a
m Int
k x
x
      case Maybe a
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 -> Int -> x -> f (Maybe b)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey (a -> WhenMissing f x b
f a
r) Int
k x
x
  {-# INLINE (>>=) #-}

-- | Create a @WhenMissing@ from two functions.
--
-- @whenMissing@ must be called with two functions @f@ and @g@ such that
-- @g = 'traverseMaybeWithKey' f@. @g@ may be a more efficient way of applying
-- @f@ to all key-value pairs in an @IntMap@.
--
-- __Warning__: It is the caller's responsibility to ensure the above property.
--
-- === __Examples__
--
-- @
-- preserveMissing :: Applicative f => WhenMissing f x x
-- preserveMissing = whenMissing f g
--   where
--     f _k x = pure (Just x)
--     g m = pure m
--     -- Note that this satisfies g = traverseMaybeWithKey f
-- @
--
-- @
-- import Data.Functor.Const (Const(..))
-- import Data.Monoid (All(..))
--
-- -- For a usage of this, see examples on mergeA
-- isEmpty :: WhenMissing (Const All) x y
-- isEmpty = whenMissing f g
--   where
--     f _k _x = Const (All False)
--     g m = Const (All (null m))
--     -- Note that this satisfies g = traverseMaybeWithKey f
-- @
--
-- @since 0.8.1
whenMissing
  :: (Key -> x -> f (Maybe y))
  -> (IntMap x -> f (IntMap y))
  -> WhenMissing f x y
whenMissing :: forall x (f :: * -> *) y.
(Int -> x -> f (Maybe y))
-> (IntMap x -> f (IntMap y)) -> WhenMissing f x y
whenMissing = ((IntMap x -> f (IntMap y))
 -> (Int -> x -> f (Maybe y)) -> WhenMissing f x y)
-> (Int -> x -> f (Maybe y))
-> (IntMap x -> f (IntMap y))
-> WhenMissing f x y
forall a b c. (a -> b -> c) -> b -> a -> c
flip (IntMap x -> f (IntMap y))
-> (Int -> x -> f (Maybe y)) -> WhenMissing f x y
forall (f :: * -> *) x y.
(IntMap x -> f (IntMap y))
-> (Int -> x -> f (Maybe y)) -> WhenMissing f x y
WhenMissing

-- | Map covariantly over a @'WhenMissing' f x@.
--
-- @since 0.5.9
mapWhenMissing
  :: Monad f
  => (a -> b)
  -> WhenMissing f x a
  -> WhenMissing f x b
mapWhenMissing :: forall (f :: * -> *) a b x.
Monad f =>
(a -> b) -> WhenMissing f x a -> WhenMissing f x b
mapWhenMissing a -> b
f WhenMissing f x a
t = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap b)
missingSubtree = \IntMap x
m -> WhenMissing f x a -> IntMap x -> f (IntMap a)
forall (f :: * -> *) x y.
WhenMissing f x y -> IntMap x -> f (IntMap y)
missingSubtree WhenMissing f x a
t IntMap x
m f (IntMap a) -> (IntMap a -> f (IntMap b)) -> f (IntMap b)
forall a b. f a -> (a -> f b) -> f b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \IntMap a
m' -> IntMap b -> f (IntMap b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IntMap b -> f (IntMap b)) -> IntMap b -> f (IntMap b)
forall a b. (a -> b) -> a -> b
$! (a -> b) -> IntMap a -> IntMap b
forall a b. (a -> b) -> IntMap a -> IntMap b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f IntMap a
m'
  , missingKey :: Int -> x -> f (Maybe b)
missingKey     = \Int
k x
x -> WhenMissing f x a -> Int -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f x a
t Int
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 :: IntMap x -> f (IntMap b)
missingSubtree = \IntMap x
m -> (a -> b) -> IntMap a -> IntMap b
forall a b. (a -> b) -> IntMap a -> IntMap b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (IntMap a -> IntMap b) -> f (IntMap a) -> f (IntMap b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WhenMissing f x a -> IntMap x -> f (IntMap a)
forall (f :: * -> *) x y.
WhenMissing f x y -> IntMap x -> f (IntMap y)
missingSubtree WhenMissing f x a
t IntMap x
m
  , missingKey :: Int -> x -> f (Maybe b)
missingKey     = \Int
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 -> Int -> x -> f (Maybe a)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f x a
t Int
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 =
  (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Int
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 -> Int -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
t Int
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 :: IntMap b -> f (IntMap x)
missingSubtree = \IntMap b
m -> WhenMissing f a x -> IntMap a -> f (IntMap x)
forall (f :: * -> *) x y.
WhenMissing f x y -> IntMap x -> f (IntMap y)
missingSubtree WhenMissing f a x
t ((b -> a) -> IntMap b -> IntMap a
forall a b. (a -> b) -> IntMap a -> IntMap b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> a
f IntMap b
m)
  , missingKey :: Int -> b -> f (Maybe x)
missingKey     = \Int
k b
x -> WhenMissing f a x -> Int -> a -> f (Maybe x)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey WhenMissing f a x
t Int
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 =
  (Int -> b -> y -> f (Maybe z)) -> WhenMatched f b y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> b -> y -> f (Maybe z)) -> WhenMatched f b y z)
-> (Int -> b -> y -> f (Maybe z)) -> WhenMatched f b y z
forall a b. (a -> b) -> a -> b
$ \Int
k b
x y
y -> WhenMatched f a y z -> Int -> a -> y -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f a y z
t Int
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 =
  (Int -> x -> b -> f (Maybe z)) -> WhenMatched f x b z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> b -> f (Maybe z)) -> WhenMatched f x b z)
-> (Int -> x -> b -> f (Maybe z)) -> WhenMatched f x b z
forall a b. (a -> b) -> a -> b
$ \Int
k x
x b
y -> WhenMatched f x a z -> Int -> x -> a -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x a z
t Int
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 -> Int -> 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 -> Int -> x -> y -> f (Maybe z)
runWhenMatched = WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> 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 -> Int -> x -> f (Maybe y)
runWhenMissing = WhenMissing f x y -> Int -> x -> f (Maybe y)
forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> 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 => Category.Category (WhenMatched f x)
  where
    id :: forall a. WhenMatched f x a a
id = (Int -> x -> a -> a) -> WhenMatched f x a a
forall (f :: * -> *) x y z.
Applicative f =>
(Int -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched (\Int
_ 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 =
      (Int -> x -> a -> f (Maybe c)) -> WhenMatched f x a c
forall x y (f :: * -> *) z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Int -> x -> a -> f (Maybe c)) -> WhenMatched f x a c)
-> (Int -> x -> a -> f (Maybe c)) -> WhenMatched f x a c
forall a b. (a -> b) -> a -> b
$ \Int
k x
x a
y -> do
        Maybe b
res <- WhenMatched f x a b -> Int -> x -> a -> f (Maybe b)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x a b
g Int
k x
x a
y
        case Maybe b
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 -> Int -> x -> b -> f (Maybe c)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x b c
f Int
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 (WhenMatched f x y) where
  pure :: forall a. a -> WhenMatched f x y a
pure a
x = (Int -> x -> y -> a) -> WhenMatched f x y a
forall (f :: * -> *) x y z.
Applicative f =>
(Int -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched (\Int
_ 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 =
    (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Int
k x
x y
y -> do
      Maybe (a -> b)
res <- WhenMatched f x y (a -> b) -> Int -> x -> y -> f (Maybe (a -> b))
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y (a -> b)
fs Int
k x
x y
y
      case Maybe (a -> b)
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 -> Int -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
xs Int
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 => 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 =
    (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall x y (f :: * -> *) z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched ((Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Int
k x
x y
y -> do
      Maybe a
res <- WhenMatched f x y a -> Int -> x -> y -> f (Maybe a)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched WhenMatched f x y a
m Int
k x
x y
y
      case Maybe a
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 -> Int -> x -> y -> f (Maybe b)
forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
runWhenMatched (a -> WhenMatched f x y b
f a
r) Int
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 Int -> x -> y -> f (Maybe a)
g) =
  (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b)
-> (Int -> x -> y -> f (Maybe b)) -> WhenMatched f x y b
forall a b. (a -> b) -> a -> b
$ \Int
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) (Int -> x -> y -> f (Maybe a)
g Int
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, drop the key and values.
--
-- @since 0.8.1
dropMatched :: Applicative f => WhenMatched f x y z
dropMatched :: forall (f :: * -> *) x y z. Applicative f => WhenMatched f x y z
dropMatched = (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched (\Int
_ x
_ y
_ -> Maybe z -> f (Maybe z)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe z
forall a. Maybe a
Nothing)
{-# INLINE dropMatched #-}

-- | 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 =>
(Int -> x -> y -> z) -> WhenMatched f x y z
zipWithMatched Int -> x -> y -> z
f = (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Int
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
$ Int -> x -> y -> z
f Int
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 =>
(Int -> x -> y -> f z) -> WhenMatched f x y z
zipWithAMatched Int -> x -> y -> f z
f = (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Int
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
<$> Int -> x -> y -> f z
f Int
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 =>
(Int -> x -> y -> Maybe z) -> WhenMatched f x y z
zipWithMaybeMatched Int -> x -> y -> Maybe z
f = (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Int
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
$ Int -> x -> y -> Maybe z
f Int
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.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
zipWithMaybeAMatched Int -> x -> y -> f (Maybe z)
f = (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall (f :: * -> *) x y z.
(Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
WhenMatched ((Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z)
-> (Int -> x -> y -> f (Maybe z)) -> WhenMatched f x y z
forall a b. (a -> b) -> a -> b
$ \ Int
k x
x y
y -> Int -> x -> y -> f (Maybe z)
f Int
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 :: IntMap x -> f (IntMap y)
missingSubtree = f (IntMap y) -> IntMap x -> f (IntMap y)
forall a b. a -> b -> a
const (IntMap y -> f (IntMap y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap y
forall a. IntMap a
Nil)
  , missingKey :: Int -> x -> f (Maybe y)
missingKey     = \Int
_ 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 :: IntMap x -> f (IntMap x)
missingSubtree = IntMap x -> f (IntMap x)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  , missingKey :: Int -> x -> f (Maybe x)
missingKey     = \Int
_ 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 =>
(Int -> x -> y) -> WhenMissing f x y
mapMissing Int -> x -> y
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap y)
missingSubtree = \IntMap x
m -> IntMap y -> f (IntMap y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IntMap y -> f (IntMap y)) -> IntMap y -> f (IntMap y)
forall a b. (a -> b) -> a -> b
$! (Int -> x -> y) -> IntMap x -> IntMap y
forall a b. (Int -> a -> b) -> IntMap a -> IntMap b
mapWithKey Int -> x -> y
f IntMap x
m
  , missingKey :: Int -> x -> f (Maybe y)
missingKey     = \Int
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 (Int -> x -> y
f Int
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 =>
(Int -> x -> Maybe y) -> WhenMissing f x y
mapMaybeMissing Int -> x -> Maybe y
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap y)
missingSubtree = \IntMap x
m -> IntMap y -> f (IntMap y)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IntMap y -> f (IntMap y)) -> IntMap y -> f (IntMap y)
forall a b. (a -> b) -> a -> b
$! (Int -> x -> Maybe y) -> IntMap x -> IntMap y
forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey Int -> x -> Maybe y
f IntMap x
m
  , missingKey :: Int -> x -> f (Maybe y)
missingKey     = \Int
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
$! Int -> x -> Maybe y
f Int
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 =>
(Int -> x -> Bool) -> WhenMissing f x x
filterMissing Int -> x -> Bool
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap x)
missingSubtree = \IntMap x
m -> IntMap x -> f (IntMap x)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IntMap x -> f (IntMap x)) -> IntMap x -> f (IntMap x)
forall a b. (a -> b) -> a -> b
$! (Int -> x -> Bool) -> IntMap x -> IntMap x
forall a. (Int -> a -> Bool) -> IntMap a -> IntMap a
filterWithKey Int -> x -> Bool
f IntMap x
m
  , missingKey :: Int -> x -> f (Maybe x)
missingKey     = \Int
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 Int -> x -> Bool
f Int
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 =>
(Int -> x -> f Bool) -> WhenMissing f x x
filterAMissing Int -> x -> f Bool
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap x)
missingSubtree = \IntMap x
m -> (Int -> x -> f Bool) -> IntMap x -> f (IntMap x)
forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> x -> f Bool
f IntMap x
m
  , missingKey :: Int -> x -> f (Maybe x)
missingKey     = \Int
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
<$> Int -> x -> f Bool
f Int
k x
x }
{-# INLINE filterAMissing #-}


-- | \(O(n)\). Filter keys and values using an 'Applicative' predicate.
filterWithKeyA
  :: Applicative f => (Key -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA :: forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> a -> f Bool
_ IntMap a
Nil           = IntMap a -> f (IntMap a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap a
forall a. IntMap a
Nil
filterWithKeyA Int -> a -> f Bool
f t :: IntMap a
t@(Tip Int
k a
x)   = (\Bool
b -> if Bool
b then IntMap a
t else IntMap a
forall a. IntMap a
Nil) (Bool -> IntMap a) -> f Bool -> f (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> a -> f Bool
f Int
k a
x
filterWithKeyA Int -> a -> f Bool
f (Bin Prefix
p IntMap a
l IntMap a
r)
  | Prefix -> Bool
signBranch Prefix
p = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 ((IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> IntMap a -> IntMap a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p)) ((Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> a -> f Bool
f IntMap a
r) ((Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> a -> f Bool
f IntMap a
l)
  | Bool
otherwise = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p) ((Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> a -> f Bool
f IntMap a
l) ((Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> a -> f Bool) -> IntMap a -> f (IntMap a)
filterWithKeyA Int -> a -> f Bool
f IntMap 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 =>
(Int -> x -> f y) -> WhenMissing f x y
traverseMissing Int -> x -> f y
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap y)
missingSubtree = (Int -> x -> f y) -> IntMap x -> f (IntMap y)
forall (t :: * -> *) a b.
Applicative t =>
(Int -> a -> t b) -> IntMap a -> t (IntMap b)
traverseWithKey Int -> x -> f y
f
  , missingKey :: Int -> x -> f (Maybe y)
missingKey = \Int
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
<$> Int -> x -> f y
f Int
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 =>
(Int -> x -> f (Maybe y)) -> WhenMissing f x y
traverseMaybeMissing Int -> x -> f (Maybe y)
f = WhenMissing
  { missingSubtree :: IntMap x -> f (IntMap y)
missingSubtree = (Int -> x -> f (Maybe y)) -> IntMap x -> f (IntMap y)
forall (f :: * -> *) a b.
Applicative f =>
(Int -> a -> f (Maybe b)) -> IntMap a -> f (IntMap b)
traverseMaybeWithKey Int -> x -> f (Maybe y)
f
  , missingKey :: Int -> x -> f (Maybe y)
missingKey = Int -> 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)) -> IntMap a -> f (IntMap b)
traverseMaybeWithKey :: forall (f :: * -> *) a b.
Applicative f =>
(Int -> a -> f (Maybe b)) -> IntMap a -> f (IntMap b)
traverseMaybeWithKey Int -> a -> f (Maybe b)
f = IntMap a -> f (IntMap b)
go
    where
    go :: IntMap a -> f (IntMap b)
go IntMap a
Nil           = IntMap b -> f (IntMap b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap b
forall a. IntMap a
Nil
    go (Tip Int
k a
x)     = IntMap b -> (b -> IntMap b) -> Maybe b -> IntMap b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap b
forall a. IntMap a
Nil (Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
k) (Maybe b -> IntMap b) -> f (Maybe b) -> f (IntMap b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> a -> f (Maybe b)
f Int
k a
x
    go (Bin Prefix
p IntMap a
l IntMap a
r)
      | Prefix -> Bool
signBranch Prefix
p = (IntMap b -> IntMap b -> IntMap b)
-> f (IntMap b) -> f (IntMap b) -> f (IntMap 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 ((IntMap b -> IntMap b -> IntMap b)
-> IntMap b -> IntMap b -> IntMap b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p)) (IntMap a -> f (IntMap b)
go IntMap a
r) (IntMap a -> f (IntMap b)
go IntMap a
l)
      | Bool
otherwise = (IntMap b -> IntMap b -> IntMap b)
-> f (IntMap b) -> f (IntMap b) -> f (IntMap 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 (Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p) (IntMap a -> f (IntMap b)
go IntMap a
l) (IntMap a -> f (IntMap b)
go IntMap a
r)
{-# INLINE traverseMaybeWithKey #-}

-- | Merge two maps.
--
-- 'merge' takes two 'SimpleWhenMissing' tactics, a 'SimpleWhenMatched' 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
-- @
--
-- @
-- g1 k x = if k == 2 then Just ("1" ++ x) else Nothing
-- g2 k x = if k == 3 then Just ("2" ++ x) else Nothing
-- f k x y = if k == 6 then Just ("3" ++ x ++ y) else Nothing
-- m1 = fromList [(2,"a"), (4,"b"), (6,"c"), (8,"d"), (10,"e"), (12,"f")]
-- m2 = fromList [(3,"g"), (6,"h"), (9,"i"), (12,"j")]
-- @
--
-- 'merge' will pass the keys and values to @g1@, @g2@, or @f@ as appropriate,
-- producing a @Maybe@ for each element.
--
-- @
-- m1:      [ (2, "a"),            (4, "b"),    (6, "c"), (8, "d"),           (10, "e"),    (12, "f")]
-- m2:      [            (3, "g"),              (6, "h"),           (9, "i"),               (12, "j")]
-- result:  [ g1 2 "a",  g2 3 "g", g1 4 "b", f 6 "c" "h", g1 8 "d", g2 9 "i", g1 10 "e", f 12 "f" "j"]
--        = [Just "1a", Just "2g",  Nothing,  Just "3ch",  Nothing,  Nothing,   Nothing,      Nothing]
-- @
--
-- The result map contains the @Just@ values.
--
-- >>> merge (mapMaybeMissing g1) (mapMaybeMissing g2) (zipWithMaybeMatched f) m1 m2
-- fromList [(2,"1a"), (3,"2g"), (6,"3ch")]
--
-- 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@
  -> IntMap a -- ^ Map @m1@
  -> IntMap b -- ^ Map @m2@
  -> IntMap c
merge :: forall a c b.
SimpleWhenMissing a c
-> SimpleWhenMissing b c
-> SimpleWhenMatched a b c
-> IntMap a
-> IntMap b
-> IntMap c
merge SimpleWhenMissing a c
g1 SimpleWhenMissing b c
g2 SimpleWhenMatched a b c
f = \IntMap a
m1 IntMap b
m2 ->
  Identity (IntMap c) -> IntMap c
forall a. Identity a -> a
runIdentity (Identity (IntMap c) -> IntMap c)
-> Identity (IntMap c) -> IntMap c
forall a b. (a -> b) -> a -> b
$ SimpleWhenMissing a c
-> SimpleWhenMissing b c
-> SimpleWhenMatched a b c
-> IntMap a
-> IntMap b
-> Identity (IntMap c)
forall (f :: * -> *) a c b.
Applicative f =>
WhenMissing f a c
-> WhenMissing f b c
-> WhenMatched f a b c
-> IntMap a
-> IntMap b
-> f (IntMap c)
mergeA SimpleWhenMissing a c
g1 SimpleWhenMissing b c
g2 SimpleWhenMatched a b c
f IntMap a
m1 IntMap 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'.
--
-- Behaves just like 'merge' while allowing @Applicative@ effects. Effects are
-- performed in increasing order of keys.
--
-- Consider
--
-- @
-- mergeA (traverseMaybeMissing g1)
--        (traverseMaybeMissing g2)
--        (zipWithMaybeAMatched f)
--        m1
--        m2
-- @
--
-- @
-- g1 k x = let z = if k == 2 then Just ("1" ++ x) else Nothing
--          in z <$ putStrLn ("g1 " ++ show (k, x))
-- g2 k x = let z = if k == 3 then Just ("2" ++ x) else Nothing
--          in z <$ putStrLn ("g2 " ++ show (k, x))
-- f k x y = let z = if k == 6 then Just ("3" ++ x ++ y) else Nothing
--           in z <$ putStrLn ("f " ++ show (k, x, y))
-- m1 = fromList [(2,"a"), (4,"b"), (6,"c"), (8,"d"), (10,"e"), (12,"f")]
-- m2 = fromList [(3,"g"), (6,"h"), (9,"i"), (12,"j")]
-- @
--
-- As with 'merge', the result map is @[(2,"1a"), (3,"2g"), (6,"3ch")]@.
-- Additionally, @g1@, @g2@, and @f@ perform @IO@ effects, printing in
-- increasing order of key.
--
-- >>> mergeA (traverseMaybeMissing g1) (traverseMaybeMissing g2) (zipWithMaybeAMatched f) m1 m2
-- g1 (2,"a")
-- g2 (3,"g")
-- g1 (4,"b")
-- f (6,"c","h")
-- g1 (8,"d")
-- g2 (9,"i")
-- g1 (10,"e")
-- f (12,"f","j")
-- fromList [(2,"1a"),(3,"2g"),(6,"3ch")]
--
-- 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.
--
-- === __Examples__
--
-- @
-- data Pair a = Pair !a !a deriving Functor
--
-- instance Applicative Pair where
--    pure x = Pair x x
--    liftA2 f (Pair x1 y1) (Pair x2 y2) = Pair (f x1 x2) (f y1 y2)
--
-- -- | Calculate the left-biased union and intersection of two maps.
-- unionIntersection :: IntMap a -> IntMap a -> (IntMap a, IntMap a)
-- unionIntersection m1 m2 =
--   case mergeA preserveAndDropMissing preserveAndDropMissing preserveLeftMatched m1 m2 of
--     Pair mu mi -> (mu, mi)
--   where
--     -- use Pair to build the union and intersection together
--     preserveAndDropMissing = 'whenMissing' (\\_k x -> Pair (Just x) Nothing) (\\m -> Pair m empty)
--     preserveLeftMatched = 'zipWithMaybeAMatched' (\\_k x1 _x2 -> Pair (Just x1) (Just x1))
-- @
--
-- @
-- import Data.Functor.Const (Const(..))
-- import Data.Monoid (All(..))
--
-- -- | Whether the keys of the first map are a subset of the keys of the second map.
-- keysAreSubsetOf :: IntMap a -> IntMap b -> Bool
-- keysAreSubsetOf m1 m2 =
--   getAll (getConst (mergeA isEmpty 'dropMissing' 'dropMatched' m1 m2))
--   where
--     isEmpty = 'whenMissing' (\\_k _x -> Const (All False)) (\\m -> Const (All (null m)))
-- @
--
-- @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@
  -> IntMap a -- ^ Map @m1@
  -> IntMap b -- ^ Map @m2@
  -> f (IntMap c)
mergeA :: forall (f :: * -> *) a c b.
Applicative f =>
WhenMissing f a c
-> WhenMissing f b c
-> WhenMatched f a b c
-> IntMap a
-> IntMap b
-> f (IntMap c)
mergeA
    WhenMissing{missingSubtree :: forall (f :: * -> *) x y.
WhenMissing f x y -> IntMap x -> f (IntMap y)
missingSubtree = IntMap a -> f (IntMap c)
g1t, missingKey :: forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey = Int -> a -> f (Maybe c)
g1k}
    WhenMissing{missingSubtree :: forall (f :: * -> *) x y.
WhenMissing f x y -> IntMap x -> f (IntMap y)
missingSubtree = IntMap b -> f (IntMap c)
g2t, missingKey :: forall (f :: * -> *) x y.
WhenMissing f x y -> Int -> x -> f (Maybe y)
missingKey = Int -> b -> f (Maybe c)
g2k}
    WhenMatched{matchedKey :: forall (f :: * -> *) x y z.
WhenMatched f x y z -> Int -> x -> y -> f (Maybe z)
matchedKey = Int -> a -> b -> f (Maybe c)
f}
    = IntMap a -> IntMap b -> f (IntMap c)
go
  where
    go :: IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
t1  IntMap b
Nil = IntMap a -> f (IntMap c)
g1t IntMap a
t1
    go IntMap a
Nil IntMap b
t2  = IntMap b -> f (IntMap c)
g2t IntMap b
t2

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

    go (Tip Int
k1 a
x1) IntMap b
t2' = IntMap b -> f (IntMap c)
merge2 IntMap b
t2'
      where
        merge2 :: IntMap b -> f (IntMap c)
merge2 t2 :: IntMap b
t2@(Bin Prefix
p2 IntMap b
l2 IntMap b
r2)
          | Int -> Prefix -> Bool
nomatch Int
k1 Prefix
p2 = Int -> f (IntMap c) -> Int -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Int -> f (IntMap a) -> Int -> f (IntMap a) -> f (IntMap a)
linkA Int
k1 ((Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
forall (f :: * -> *) a c.
Functor f =>
(Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
subsingletonBy Int -> a -> f (Maybe c)
g1k Int
k1 a
x1) (Prefix -> Int
unPrefix Prefix
p2) (IntMap b -> f (IntMap c)
g2t IntMap b
t2)
          | Int -> Prefix -> Bool
left Int
k1 Prefix
p2    = Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p2 (IntMap b -> f (IntMap c)
merge2 IntMap b
l2) (IntMap b -> f (IntMap c)
g2t IntMap b
r2)
          | Bool
otherwise     = Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p2 (IntMap b -> f (IntMap c)
g2t IntMap b
l2) (IntMap b -> f (IntMap c)
merge2 IntMap b
r2)
        merge2 (Tip Int
k2 b
x2)   = Int -> a -> Int -> b -> f (IntMap c)
mergeTips Int
k1 a
x1 Int
k2 b
x2
        merge2 IntMap b
Nil           = (Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
forall (f :: * -> *) a c.
Functor f =>
(Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
subsingletonBy Int -> a -> f (Maybe c)
g1k Int
k1 a
x1

    go IntMap a
t1' (Tip Int
k2 b
x2) = IntMap a -> f (IntMap c)
merge1 IntMap a
t1'
      where
        merge1 :: IntMap a -> f (IntMap c)
merge1 t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1)
          | Int -> Prefix -> Bool
nomatch Int
k2 Prefix
p1 = Int -> f (IntMap c) -> Int -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Int -> f (IntMap a) -> Int -> f (IntMap a) -> f (IntMap a)
linkA (Prefix -> Int
unPrefix Prefix
p1) (IntMap a -> f (IntMap c)
g1t IntMap a
t1) Int
k2 ((Int -> b -> f (Maybe c)) -> Int -> b -> f (IntMap c)
forall (f :: * -> *) a c.
Functor f =>
(Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
subsingletonBy Int -> b -> f (Maybe c)
g2k Int
k2 b
x2)
          | Int -> Prefix -> Bool
left Int
k2 Prefix
p1    = Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p1 (IntMap a -> f (IntMap c)
merge1 IntMap a
l1) (IntMap a -> f (IntMap c)
g1t IntMap a
r1)
          | Bool
otherwise     = Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p1 (IntMap a -> f (IntMap c)
g1t IntMap a
l1) (IntMap a -> f (IntMap c)
merge1 IntMap a
r1)
        merge1 (Tip Int
k1 a
x1)   = Int -> a -> Int -> b -> f (IntMap c)
mergeTips Int
k1 a
x1 Int
k2 b
x2
        merge1 IntMap a
Nil           = (Int -> b -> f (Maybe c)) -> Int -> b -> f (IntMap c)
forall (f :: * -> *) a c.
Functor f =>
(Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
subsingletonBy Int -> b -> f (Maybe c)
g2k Int
k2 b
x2

    go t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntMap b
t2@(Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
      TreeTreeBranch
ABL -> Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p1 (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
l1 IntMap b
t2) (IntMap a -> f (IntMap c)
g1t IntMap a
r1)
      TreeTreeBranch
ABR -> Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p1 (IntMap a -> f (IntMap c)
g1t IntMap a
l1) (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
r1 IntMap b
t2)
      TreeTreeBranch
BAL -> Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p2 (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
t1 IntMap b
l2) (IntMap b -> f (IntMap c)
g2t IntMap b
r2)
      TreeTreeBranch
BAR -> Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p2 (IntMap b -> f (IntMap c)
g2t IntMap b
l2) (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
t1 IntMap b
r2)
      TreeTreeBranch
EQL -> Prefix -> f (IntMap c) -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p1 (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
l1 IntMap b
l2) (IntMap a -> IntMap b -> f (IntMap c)
go IntMap a
r1 IntMap b
r2)
      TreeTreeBranch
NOM -> Int -> f (IntMap c) -> Int -> f (IntMap c) -> f (IntMap c)
forall (f :: * -> *) a.
Applicative f =>
Int -> f (IntMap a) -> Int -> f (IntMap a) -> f (IntMap a)
linkA (Prefix -> Int
unPrefix Prefix
p1) (IntMap a -> f (IntMap c)
g1t IntMap a
t1) (Prefix -> Int
unPrefix Prefix
p2) (IntMap b -> f (IntMap c)
g2t IntMap b
t2)

    subsingletonBy :: Functor f => (Key -> a -> f (Maybe c)) -> Key -> a -> f (IntMap c)
    subsingletonBy :: forall (f :: * -> *) a c.
Functor f =>
(Int -> a -> f (Maybe c)) -> Int -> a -> f (IntMap c)
subsingletonBy Int -> a -> f (Maybe c)
gk Int
k a
x = IntMap c -> (c -> IntMap c) -> Maybe c -> IntMap c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap c
forall a. IntMap a
Nil (Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k) (Maybe c -> IntMap c) -> f (Maybe c) -> f (IntMap c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> a -> f (Maybe c)
gk Int
k a
x
    {-# INLINE subsingletonBy #-}

    mergeTips :: Int -> a -> Int -> b -> f (IntMap c)
mergeTips Int
k1 a
x1 Int
k2 b
x2
      | Int
k1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k2  = IntMap c -> (c -> IntMap c) -> Maybe c -> IntMap c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap c
forall a. IntMap a
Nil (Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k1) (Maybe c -> IntMap c) -> f (Maybe c) -> f (IntMap c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> a -> b -> f (Maybe c)
f Int
k1 a
x1 b
x2
      | Int
k1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<  Int
k2  = (Maybe c -> Maybe c -> IntMap c)
-> f (Maybe c) -> f (Maybe c) -> f (IntMap 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 (Int -> Int -> Maybe c -> Maybe c -> IntMap c
forall {a}. Int -> Int -> Maybe a -> Maybe a -> IntMap a
subdoubleton Int
k1 Int
k2) (Int -> a -> f (Maybe c)
g1k Int
k1 a
x1) (Int -> b -> f (Maybe c)
g2k Int
k2 b
x2)
        {-
        = link_ k1 k2 <$> subsingletonBy g1k k1 x1 <*> subsingletonBy g2k k2 x2
        -}
      | Bool
otherwise = (Maybe c -> Maybe c -> IntMap c)
-> f (Maybe c) -> f (Maybe c) -> f (IntMap 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 (Int -> Int -> Maybe c -> Maybe c -> IntMap c
forall {a}. Int -> Int -> Maybe a -> Maybe a -> IntMap a
subdoubleton Int
k2 Int
k1) (Int -> b -> f (Maybe c)
g2k Int
k2 b
x2) (Int -> a -> f (Maybe c)
g1k Int
k1 a
x1)
    {-# INLINE mergeTips #-}

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

    -- | A variant of 'link_' which makes sure to execute side-effects
    -- in the right order.
    linkA
        :: Applicative f
        => Int -> f (IntMap a)
        -> Int -> f (IntMap a)
        -> f (IntMap a)
    linkA :: forall (f :: * -> *) a.
Applicative f =>
Int -> f (IntMap a) -> Int -> f (IntMap a) -> f (IntMap a)
linkA Int
k1 f (IntMap a)
t1 Int
k2 f (IntMap a)
t2
      | Int -> Word
i2w Int
k1 Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Word
i2w Int
k2 = Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p f (IntMap a)
t1 f (IntMap a)
t2
      | Bool
otherwise = Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p f (IntMap a)
t2 f (IntMap a)
t1
      where
        p :: Prefix
p = Int -> Int -> Prefix
branchPrefix Int
k1 Int
k2
    {-# INLINE linkA #-}

    -- A variant of 'bin' that ensures that effects for negative keys are executed
    -- first.
    binA
        :: Applicative f
        => Prefix
        -> f (IntMap a)
        -> f (IntMap a)
        -> f (IntMap a)
    binA :: forall (f :: * -> *) a.
Applicative f =>
Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a)
binA Prefix
p f (IntMap a)
a f (IntMap a)
b
      | Prefix -> Bool
signBranch Prefix
p = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 ((IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> IntMap a -> IntMap a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p)) f (IntMap a)
b f (IntMap a)
a
      | Bool
otherwise = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p) f (IntMap a)
a f (IntMap 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) -> IntMap a -> IntMap a
updateMinWithKey :: forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
updateMinWithKey Int -> a -> Maybe a
f IntMap a
t =
  case IntMap a
t of Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l ((Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
go Int -> a -> Maybe a
f IntMap a
r)
            IntMap a
_ -> (Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
go Int -> a -> Maybe a
f IntMap a
t
  where
    go :: (Int -> t -> Maybe t) -> IntMap t -> IntMap t
go Int -> t -> Maybe t
f' (Bin Prefix
p IntMap t
l IntMap t
r) = Prefix -> IntMap t -> IntMap t -> IntMap t
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Int -> t -> Maybe t) -> IntMap t -> IntMap t
go Int -> t -> Maybe t
f' IntMap t
l) IntMap t
r
    go Int -> t -> Maybe t
f' (Tip Int
k t
y) = case Int -> t -> Maybe t
f' Int
k t
y of
                        Just t
y' -> Int -> t -> IntMap t
forall a. Int -> a -> IntMap a
Tip Int
k t
y'
                        Maybe t
Nothing -> IntMap t
forall a. IntMap a
Nil
    go Int -> t -> Maybe t
_ IntMap t
Nil =  IntMap t
forall a. IntMap a
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) -> IntMap a -> IntMap a
updateMaxWithKey :: forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
updateMaxWithKey Int -> a -> Maybe a
f IntMap a
t =
  case IntMap a
t of Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
go Int -> a -> Maybe a
f IntMap a
l) IntMap a
r
            IntMap a
_ -> (Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
go Int -> a -> Maybe a
f IntMap a
t
  where
    go :: (Int -> t -> Maybe t) -> IntMap t -> IntMap t
go Int -> t -> Maybe t
f' (Bin Prefix
p IntMap t
l IntMap t
r) = Prefix -> IntMap t -> IntMap t -> IntMap t
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap t
l ((Int -> t -> Maybe t) -> IntMap t -> IntMap t
go Int -> t -> Maybe t
f' IntMap t
r)
    go Int -> t -> Maybe t
f' (Tip Int
k t
y) = case Int -> t -> Maybe t
f' Int
k t
y of
                        Just t
y' -> Int -> t -> IntMap t
forall a. Int -> a -> IntMap a
Tip Int
k t
y'
                        Maybe t
Nothing -> IntMap t
forall a. IntMap a
Nil
    go Int -> t -> Maybe t
_ IntMap t
Nil = IntMap t
forall a. IntMap a
Nil


data View a = View {-# UNPACK #-} !Key a !(IntMap 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 :: IntMap a -> Maybe ((Key, a), IntMap a)
maxViewWithKey :: forall a. IntMap a -> Maybe ((Int, a), IntMap a)
maxViewWithKey IntMap a
t = case IntMap a
t of
  IntMap a
Nil -> Maybe ((Int, a), IntMap a)
forall a. Maybe a
Nothing
  IntMap a
_ -> ((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a)
forall a. a -> Maybe a
Just (((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a))
-> ((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a)
forall a b. (a -> b) -> a -> b
$ case IntMap a -> View a
forall a. IntMap a -> View a
maxViewWithKeySure IntMap a
t of
                View Int
k a
v IntMap a
t' -> ((Int
k, a
v), IntMap a
t')
{-# INLINE maxViewWithKey #-}

maxViewWithKeySure :: IntMap a -> View a
maxViewWithKeySure :: forall a. IntMap a -> View a
maxViewWithKeySure IntMap a
t =
  case IntMap a
t of
    IntMap a
Nil -> [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"maxViewWithKeySure Nil"
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p ->
      case IntMap a -> View a
forall a. IntMap a -> View a
go IntMap a
l of View Int
k a
a IntMap a
l' -> Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
a (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r)
    IntMap a
_ -> IntMap a -> View a
forall a. IntMap a -> View a
go IntMap a
t
  where
    go :: IntMap a -> View a
go (Bin Prefix
p IntMap a
l IntMap a
r) =
        case IntMap a -> View a
go IntMap a
r of View Int
k a
a IntMap a
r' -> Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
a (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
r')
    go (Tip Int
k a
y) = Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
y IntMap a
forall a. IntMap a
Nil
    go IntMap 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 :: IntMap a -> Maybe ((Key, a), IntMap a)
minViewWithKey :: forall a. IntMap a -> Maybe ((Int, a), IntMap a)
minViewWithKey IntMap a
t =
  case IntMap a
t of
    IntMap a
Nil -> Maybe ((Int, a), IntMap a)
forall a. Maybe a
Nothing
    IntMap a
_ -> ((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a)
forall a. a -> Maybe a
Just (((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a))
-> ((Int, a), IntMap a) -> Maybe ((Int, a), IntMap a)
forall a b. (a -> b) -> a -> b
$ case IntMap a -> View a
forall a. IntMap a -> View a
minViewWithKeySure IntMap a
t of
                  View Int
k a
v IntMap a
t' -> ((Int
k, a
v), IntMap 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 :: IntMap a -> View a
minViewWithKeySure :: forall a. IntMap a -> View a
minViewWithKeySure IntMap a
t =
  case IntMap a
t of
    IntMap a
Nil -> [Char] -> View a
forall a. HasCallStack => [Char] -> a
error [Char]
"minViewWithKeySure Nil"
    Bin Prefix
p IntMap a
l IntMap a
r | Prefix -> Bool
signBranch Prefix
p ->
      case IntMap a -> View a
forall a. IntMap a -> View a
go IntMap a
r of
        View Int
k a
a IntMap a
r' -> Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
a (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
r')
    IntMap a
_ -> IntMap a -> View a
forall a. IntMap a -> View a
go IntMap a
t
  where
    go :: IntMap a -> View a
go (Bin Prefix
p IntMap a
l IntMap a
r) =
        case IntMap a -> View a
go IntMap a
l of View Int
k a
a IntMap a
l' -> Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
a (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r)
    go (Tip Int
k a
y) = Int -> a -> IntMap a -> View a
forall a. Int -> a -> IntMap a -> View a
View Int
k a
y IntMap a
forall a. IntMap a
Nil
    go IntMap 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) -> IntMap a -> IntMap a
updateMax :: forall a. (a -> Maybe a) -> IntMap a -> IntMap a
updateMax a -> Maybe a
f = (Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
updateMaxWithKey ((a -> Maybe a) -> Int -> 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) -> IntMap a -> IntMap a
updateMin :: forall a. (a -> Maybe a) -> IntMap a -> IntMap a
updateMin a -> Maybe a
f = (Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Int -> a -> Maybe a) -> IntMap a -> IntMap a
updateMinWithKey ((a -> Maybe a) -> Int -> 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 :: IntMap a -> Maybe (a, IntMap a)
maxView :: forall a. IntMap a -> Maybe (a, IntMap a)
maxView IntMap a
t = (((Int, a), IntMap a) -> (a, IntMap a))
-> Maybe ((Int, a), IntMap a) -> Maybe (a, IntMap a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\((Int
_, a
x), IntMap a
t') -> (a
x, IntMap a
t')) (IntMap a -> Maybe ((Int, a), IntMap a)
forall a. IntMap a -> Maybe ((Int, a), IntMap a)
maxViewWithKey IntMap 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 :: IntMap a -> Maybe (a, IntMap a)
minView :: forall a. IntMap a -> Maybe (a, IntMap a)
minView IntMap a
t = (((Int, a), IntMap a) -> (a, IntMap a))
-> Maybe ((Int, a), IntMap a) -> Maybe (a, IntMap a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\((Int
_, a
x), IntMap a
t') -> (a
x, IntMap a
t')) (IntMap a -> Maybe ((Int, a), IntMap a)
forall a. IntMap a -> Maybe ((Int, a), IntMap a)
minViewWithKey IntMap a
t)

-- | \(O(\min(n,W))\). Delete and find the maximal element.
--
-- Calls 'error' if the map is empty.
--
-- __Note__: This function is partial. Prefer 'maxViewWithKey'.
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)
deleteFindMax :: forall a. IntMap a -> ((Int, a), IntMap a)
deleteFindMax = ((Int, a), IntMap a)
-> Maybe ((Int, a), IntMap a) -> ((Int, a), IntMap a)
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> ((Int, a), IntMap a)
forall a. HasCallStack => [Char] -> a
error [Char]
"deleteFindMax: empty map has no maximal element") (Maybe ((Int, a), IntMap a) -> ((Int, a), IntMap a))
-> (IntMap a -> Maybe ((Int, a), IntMap a))
-> IntMap a
-> ((Int, a), IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> Maybe ((Int, a), IntMap a)
forall a. IntMap a -> Maybe ((Int, a), IntMap a)
maxViewWithKey

-- | \(O(\min(n,W))\). Delete and find the minimal element.
--
-- Calls 'error' if the map is empty.
--
-- __Note__: This function is partial. Prefer 'minViewWithKey'.
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)
deleteFindMin :: forall a. IntMap a -> ((Int, a), IntMap a)
deleteFindMin = ((Int, a), IntMap a)
-> Maybe ((Int, a), IntMap a) -> ((Int, a), IntMap a)
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> ((Int, a), IntMap a)
forall a. HasCallStack => [Char] -> a
error [Char]
"deleteFindMin: empty map has no minimal element") (Maybe ((Int, a), IntMap a) -> ((Int, a), IntMap a))
-> (IntMap a -> Maybe ((Int, a), IntMap a))
-> IntMap a
-> ((Int, a), IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> Maybe ((Int, a), IntMap a)
forall a. IntMap a -> Maybe ((Int, a), IntMap a)
minViewWithKey

-- The KeyValue type is used when returning a key-value pair and helps with
-- GHC optimizations.
--
-- For lookupMinSure, if the return type is (Int, a), GHC compiles it to a
-- worker $wlookupMinSure :: IntMap a -> (# Int, a #). If the return type is
-- KeyValue a instead, the worker does not box the int and returns
-- (# Int#, a #).
-- For a modern enough GHC (>=9.4), this measure turns out to be unnecessary in
-- this instance. We still use it for older GHCs and to make our intent clear.

data KeyValue a = KeyValue {-# UNPACK #-} !Key a

kvToTuple :: KeyValue a -> (Key, a)
kvToTuple :: forall a. KeyValue a -> (Int, a)
kvToTuple (KeyValue Int
k a
x) = (Int
k, a
x)
{-# INLINE kvToTuple #-}

lookupMinSure :: IntMap a -> KeyValue a
lookupMinSure :: forall a. IntMap a -> KeyValue a
lookupMinSure (Tip Int
k a
v)   = Int -> a -> KeyValue a
forall a. Int -> a -> KeyValue a
KeyValue Int
k a
v
lookupMinSure (Bin Prefix
_ IntMap a
l IntMap a
_) = IntMap a -> KeyValue a
forall a. IntMap a -> KeyValue a
lookupMinSure IntMap a
l
lookupMinSure IntMap a
Nil         = [Char] -> KeyValue a
forall a. HasCallStack => [Char] -> a
error [Char]
"lookupMinSure Nil"

-- | \(O(\min(n,W))\). The minimal key of the map. Returns 'Nothing' if the map is empty.
lookupMin :: IntMap a -> Maybe (Key, a)
lookupMin :: forall a. IntMap a -> Maybe (Int, a)
lookupMin IntMap a
Nil         = Maybe (Int, a)
forall a. Maybe a
Nothing
lookupMin (Tip Int
k a
v)   = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
k,a
v)
lookupMin (Bin Prefix
p IntMap a
l IntMap a
r) =
  (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just ((Int, a) -> Maybe (Int, a)) -> (Int, a) -> Maybe (Int, a)
forall a b. (a -> b) -> a -> b
$! KeyValue a -> (Int, a)
forall a. KeyValue a -> (Int, a)
kvToTuple (IntMap a -> KeyValue a
forall a. IntMap a -> KeyValue a
lookupMinSure (if Prefix -> Bool
signBranch Prefix
p then IntMap a
r else IntMap a
l))
{-# INLINE lookupMin #-} -- See Note [Inline lookupMin] in Data.Set.Internal

-- | \(O(\min(n,W))\). The minimal key of the map. Calls 'error' if the map is empty.
--
-- __Note__: This function is partial. Prefer 'lookupMin'.
findMin :: IntMap a -> (Key, a)
findMin :: forall a. IntMap a -> (Int, a)
findMin IntMap a
t
  | Just (Int, a)
r <- IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
lookupMin IntMap a
t = (Int, a)
r
  | Bool
otherwise = [Char] -> (Int, a)
forall a. HasCallStack => [Char] -> a
error [Char]
"findMin: empty map has no minimal element"

lookupMaxSure :: IntMap a -> KeyValue a
lookupMaxSure :: forall a. IntMap a -> KeyValue a
lookupMaxSure (Tip Int
k a
v)   = Int -> a -> KeyValue a
forall a. Int -> a -> KeyValue a
KeyValue Int
k a
v
lookupMaxSure (Bin Prefix
_ IntMap a
_ IntMap a
r) = IntMap a -> KeyValue a
forall a. IntMap a -> KeyValue a
lookupMaxSure IntMap a
r
lookupMaxSure IntMap a
Nil         = [Char] -> KeyValue a
forall a. HasCallStack => [Char] -> a
error [Char]
"lookupMaxSure Nil"

-- | \(O(\min(n,W))\). The maximal key of the map. Returns 'Nothing' if the map is empty.
lookupMax :: IntMap a -> Maybe (Key, a)
lookupMax :: forall a. IntMap a -> Maybe (Int, a)
lookupMax IntMap a
Nil         = Maybe (Int, a)
forall a. Maybe a
Nothing
lookupMax (Tip Int
k a
v)   = (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just (Int
k,a
v)
lookupMax (Bin Prefix
p IntMap a
l IntMap a
r) =
  (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just ((Int, a) -> Maybe (Int, a)) -> (Int, a) -> Maybe (Int, a)
forall a b. (a -> b) -> a -> b
$! KeyValue a -> (Int, a)
forall a. KeyValue a -> (Int, a)
kvToTuple (IntMap a -> KeyValue a
forall a. IntMap a -> KeyValue a
lookupMaxSure (if Prefix -> Bool
signBranch Prefix
p then IntMap a
l else IntMap a
r))
{-# INLINE lookupMax #-} -- See Note [Inline lookupMin] in Data.Set.Internal

-- | \(O(\min(n,W))\). The maximal key of the map. Calls 'error' if the map is empty.
--
-- __Note__: This function is partial. Prefer 'lookupMax'.
findMax :: IntMap a -> (Key, a)
findMax :: forall a. IntMap a -> (Int, a)
findMax IntMap a
t
  | Just (Int, a)
r <- IntMap a -> Maybe (Int, a)
forall a. IntMap a -> Maybe (Int, a)
lookupMax IntMap a
t = (Int, a)
r
  | Bool
otherwise = [Char] -> (Int, 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 'IntMap' was already empty.
deleteMin :: IntMap a -> IntMap a
deleteMin :: forall a. IntMap a -> IntMap a
deleteMin = IntMap a
-> ((a, IntMap a) -> IntMap a) -> Maybe (a, IntMap a) -> IntMap a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
forall a. IntMap a
Nil (a, IntMap a) -> IntMap a
forall a b. (a, b) -> b
snd (Maybe (a, IntMap a) -> IntMap a)
-> (IntMap a -> Maybe (a, IntMap a)) -> IntMap a -> IntMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> Maybe (a, IntMap a)
forall a. IntMap a -> Maybe (a, IntMap 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 'IntMap' was already empty.
deleteMax :: IntMap a -> IntMap a
deleteMax :: forall a. IntMap a -> IntMap a
deleteMax = IntMap a
-> ((a, IntMap a) -> IntMap a) -> Maybe (a, IntMap a) -> IntMap a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
forall a. IntMap a
Nil (a, IntMap a) -> IntMap a
forall a b. (a, b) -> b
snd (Maybe (a, IntMap a) -> IntMap a)
-> (IntMap a -> Maybe (a, IntMap a)) -> IntMap a -> IntMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> Maybe (a, IntMap a)
forall a. IntMap a -> Maybe (a, IntMap a)
maxView


{--------------------------------------------------------------------
  Submap
--------------------------------------------------------------------}
-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- Is this a proper submap? (ie. a submap but not equal).
-- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@).
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
isProperSubmapOf :: forall a. Eq a => IntMap a -> IntMap a -> Bool
isProperSubmapOf IntMap a
m1 IntMap a
m2
  = (a -> a -> Bool) -> IntMap a -> IntMap a -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isProperSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) IntMap a
m1 IntMap a
m2

{- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
 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) -> IntMap a -> IntMap b -> Bool
isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isProperSubmapOfBy a -> b -> Bool
predicate IntMap a
t1 IntMap b
t2
  = case (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp a -> b -> Bool
predicate IntMap a
t1 IntMap b
t2 of
      Ordering
LT -> Bool
True
      Ordering
_  -> Bool
False

submapCmp :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp :: forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp a -> b -> Bool
predicate t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) (Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
  TreeTreeBranch
ABL -> Ordering
GT
  TreeTreeBranch
ABR -> Ordering
GT
  TreeTreeBranch
BAL -> IntMap b -> Ordering
submapCmpLt IntMap b
l2
  TreeTreeBranch
BAR -> IntMap b -> Ordering
submapCmpLt IntMap b
r2
  TreeTreeBranch
EQL -> Ordering
submapCmpEq
  TreeTreeBranch
NOM -> Ordering
GT  -- disjoint
  where
    submapCmpLt :: IntMap b -> Ordering
submapCmpLt IntMap b
t = case (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp a -> b -> Bool
predicate IntMap a
t1 IntMap b
t of
                      Ordering
GT -> Ordering
GT
                      Ordering
_  -> Ordering
LT
    submapCmpEq :: Ordering
submapCmpEq = case ((a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp a -> b -> Bool
predicate IntMap a
l1 IntMap b
l2, (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp a -> b -> Bool
predicate IntMap a
r1 IntMap 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 Prefix
_ IntMap a
_ IntMap a
_) IntMap b
_  = Ordering
GT
submapCmp a -> b -> Bool
predicate (Tip Int
kx a
x) (Tip Int
ky b
y)
  | (Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
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 Int
k a
x) IntMap b
t
  = case Int -> IntMap b -> Maybe b
forall a. Int -> IntMap a -> Maybe a
lookup Int
k IntMap 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
_    IntMap a
Nil IntMap b
Nil = Ordering
EQ
submapCmp a -> b -> Bool
_    IntMap a
Nil IntMap b
_   = Ordering
LT

-- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
-- Is this a submap?
-- Defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@).
isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
isSubmapOf :: forall a. Eq a => IntMap a -> IntMap a -> Bool
isSubmapOf IntMap a
m1 IntMap a
m2
  = (a -> a -> Bool) -> IntMap a -> IntMap a -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) IntMap a
m1 IntMap a
m2

{- | \(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
 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) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy :: forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> b -> Bool
predicate t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) (Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
  TreeTreeBranch
ABL -> Bool
False
  TreeTreeBranch
ABR -> Bool
False
  TreeTreeBranch
BAL -> (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> b -> Bool
predicate IntMap a
t1 IntMap b
l2
  TreeTreeBranch
BAR -> (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> b -> Bool
predicate IntMap a
t1 IntMap b
r2
  TreeTreeBranch
EQL -> (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> b -> Bool
predicate IntMap a
l1 IntMap b
l2 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isSubmapOfBy a -> b -> Bool
predicate IntMap a
r1 IntMap b
r2
  TreeTreeBranch
NOM -> Bool
False
isSubmapOfBy a -> b -> Bool
_         (Bin Prefix
_ IntMap a
_ IntMap a
_) IntMap b
_ = Bool
False
isSubmapOfBy a -> b -> Bool
predicate (Tip Int
k a
x) IntMap b
t     = case Int -> IntMap b -> Maybe b
forall a. Int -> IntMap a -> Maybe a
lookup Int
k IntMap b
t of
                                         Just b
y  -> a -> b -> Bool
predicate a
x b
y
                                         Maybe b
Nothing -> Bool
False
isSubmapOfBy a -> b -> Bool
_         IntMap a
Nil IntMap 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) -> IntMap a -> IntMap b
map :: forall a b. (a -> b) -> IntMap a -> IntMap b
map a -> b
f = IntMap a -> IntMap b
go
  where
    go :: IntMap a -> IntMap b
go (Bin Prefix
p IntMap a
l IntMap a
r) = Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p (IntMap a -> IntMap b
go IntMap a
l) (IntMap a -> IntMap b
go IntMap a
r)
    go (Tip Int
k a
x)   = Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
k (a -> b
f a
x)
    go IntMap a
Nil         = IntMap b
forall a. IntMap a
Nil

#ifdef __GLASGOW_HASKELL__
{-# NOINLINE [1] map #-}
{-# RULES
"map/map" forall f g xs . map f (map g xs) = map (f . g) xs
"map/coerce" map coerce = coerce
 #-}
#endif

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

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

-- | \(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) -> IntMap a -> t (IntMap b)
traverseWithKey :: forall (t :: * -> *) a b.
Applicative t =>
(Int -> a -> t b) -> IntMap a -> t (IntMap b)
traverseWithKey Int -> a -> t b
f = IntMap a -> t (IntMap b)
go
  where
    go :: IntMap a -> t (IntMap b)
go IntMap a
Nil = IntMap b -> t (IntMap b)
forall a. a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap b
forall a. IntMap a
Nil
    go (Tip Int
k a
v) = Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
k (b -> IntMap b) -> t b -> t (IntMap b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> a -> t b
f Int
k a
v
    go (Bin Prefix
p IntMap a
l IntMap a
r)
      | Prefix -> Bool
signBranch Prefix
p = (IntMap b -> IntMap b -> IntMap b)
-> t (IntMap b) -> t (IntMap b) -> t (IntMap 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 ((IntMap b -> IntMap b -> IntMap b)
-> IntMap b -> IntMap b -> IntMap b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p)) (IntMap a -> t (IntMap b)
go IntMap a
r) (IntMap a -> t (IntMap b)
go IntMap a
l)
      | Bool
otherwise = (IntMap b -> IntMap b -> IntMap b)
-> t (IntMap b) -> t (IntMap b) -> t (IntMap 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 (Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p) (IntMap a -> t (IntMap b)
go IntMap a
l) (IntMap a -> t (IntMap b)
go IntMap 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 -> IntMap b -> (a,IntMap c)
mapAccum :: forall a b c. (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccum a -> b -> (a, c)
f = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumWithKey (\a
a' Int
_ 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 -> IntMap b -> (a,IntMap c)
mapAccumWithKey :: forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumWithKey a -> Int -> b -> (a, c)
f a
a IntMap b
t
  = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a IntMap 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 -> IntMap b -> (a,IntMap c)
mapAccumL :: forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a IntMap b
t
  = case IntMap b
t of
      Bin Prefix
p IntMap b
l IntMap b
r
        | Prefix -> Bool
signBranch Prefix
p ->
            let (a
a1,IntMap c
r') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a IntMap b
r
                (a
a2,IntMap c
l') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a1 IntMap b
l
            in (a
a2,Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap c
l' IntMap c
r')
        | Bool
otherwise  ->
            let (a
a1,IntMap c
l') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a IntMap b
l
                (a
a2,IntMap c
r') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Int -> b -> (a, c)
f a
a1 IntMap b
r
            in (a
a2,Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap c
l' IntMap c
r')
      Tip Int
k b
x     -> let (a
a',c
x') = a -> Int -> b -> (a, c)
f a
a Int
k b
x in (a
a',Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k c
x')
      IntMap b
Nil         -> (a
a,IntMap c
forall a. IntMap 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 -> IntMap b -> (a,IntMap c)
mapAccumRWithKey :: forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Int -> b -> (a, c)
f a
a IntMap b
t
  = case IntMap b
t of
      Bin Prefix
p IntMap b
l IntMap b
r
        | Prefix -> Bool
signBranch Prefix
p ->
            let (a
a1,IntMap c
l') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Int -> b -> (a, c)
f a
a IntMap b
l
                (a
a2,IntMap c
r') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Int -> b -> (a, c)
f a
a1 IntMap b
r
            in (a
a2,Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap c
l' IntMap c
r')
        | Bool
otherwise  ->
            let (a
a1,IntMap c
r') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Int -> b -> (a, c)
f a
a IntMap b
r
                (a
a2,IntMap c
l') = (a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Int -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Int -> b -> (a, c)
f a
a1 IntMap b
l
            in (a
a2,Prefix -> IntMap c -> IntMap c -> IntMap c
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap c
l' IntMap c
r')
      Tip Int
k b
x     -> let (a
a',c
x') = a -> Int -> b -> (a, c)
f a
a Int
k b
x in (a
a',Int -> c -> IntMap c
forall a. Int -> a -> IntMap a
Tip Int
k c
x')
      IntMap b
Nil         -> (a
a,IntMap c
forall a. IntMap a
Nil)

-- | \(O(n \min(n,W))\).
-- @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@.
--
-- If `f` is monotonically non-decreasing or monotonically non-increasing, this
-- function takes \(O(n)\) time.
--
-- 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) -> IntMap a -> IntMap a
mapKeys :: forall a. (Int -> Int) -> IntMap a -> IntMap a
mapKeys Int -> Int
f IntMap a
t = IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> Int -> a -> IntMapBuilder a)
-> IntMapBuilder a -> IntMap a -> IntMapBuilder a
forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey' (\IntMapBuilder a
b Int
kx a
x -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
forall a. Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertB (Int -> Int
f Int
kx) a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB IntMap a
t)
{-# INLINABLE mapKeys #-} -- See Note [INLINABLE to expose unfoldings]

-- | \(O(n \min(n,W))\).
-- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
--
-- If `f` is monotonically non-decreasing or monotonically non-increasing, this
-- function takes \(O(n)\) time.
--
-- 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"
--
-- Also see the performance note on 'fromListWith'.

mapKeysWith :: (a -> a -> a) -> (Key->Key) -> IntMap a -> IntMap a
mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> IntMap a -> IntMap a
mapKeysWith a -> a -> a
c Int -> Int
f IntMap a
t =
  IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> Int -> a -> IntMapBuilder a)
-> IntMapBuilder a -> IntMap a -> IntMapBuilder a
forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey' (\IntMapBuilder a
b Int
kx a
x -> (a -> a -> a) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
forall a.
(a -> a -> a) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB a -> a -> a
c (Int -> Int
f Int
kx) a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB IntMap a
t)
{-# INLINABLE mapKeysWith #-} -- See Note [INLINABLE to expose unfoldings]

-- | \(O(n)\).
-- @'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@.
-- 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'.
--
-- __Warning__: This function should be used only if @f@ is monotonically
-- strictly increasing. This precondition is not checked. Use 'mapKeys' if the
-- precondition may not hold.
--
-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]

mapKeysMonotonic :: (Key->Key) -> IntMap a -> IntMap a
mapKeysMonotonic :: forall a. (Int -> Int) -> IntMap a -> IntMap a
mapKeysMonotonic Int -> Int
f IntMap a
t =
  MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState a -> Int -> a -> MonoState a)
-> MonoState a -> IntMap a -> MonoState a
forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey' (\MonoState a
s Int
kx a
x -> MonoState a -> Int -> a -> MonoState a
forall a. MonoState a -> Int -> a -> MonoState a
ascInsert MonoState a
s (Int -> Int
f Int
kx) a
x) MonoState a
forall a. MonoState a
MSNada IntMap a
t)
{-# INLINABLE mapKeysMonotonic #-} -- See Note [INLINABLE to expose unfoldings]

{--------------------------------------------------------------------
  Filter
--------------------------------------------------------------------}
-- | \(O(n)\). Keep 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) -> IntMap a -> IntMap a
filter :: forall a. (a -> Bool) -> IntMap a -> IntMap a
filter a -> Bool
p = (Int -> a -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> a -> Bool) -> IntMap a -> IntMap a
filterWithKey (\Int
_ a
x -> a -> Bool
p a
x)
{-# INLINE filter #-}

-- | \(O(n)\). Keep all keys that satisfy some predicate.
--
-- @
-- filterKeys p = 'filterWithKey' (\\k _ -> p k)
-- @
--
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
--
-- @since 0.8

filterKeys :: (Key -> Bool) -> IntMap a -> IntMap a
filterKeys :: forall a. (Int -> Bool) -> IntMap a -> IntMap a
filterKeys Int -> Bool
predicate = (Int -> a -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> a -> Bool) -> IntMap a -> IntMap a
filterWithKey (\Int
k a
_ -> Int -> Bool
predicate Int
k)
{-# INLINE filterKeys #-}

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

filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a
filterWithKey :: forall a. (Int -> a -> Bool) -> IntMap a -> IntMap a
filterWithKey Int -> a -> Bool
predicate = IntMap a -> IntMap a
go
    where
    go :: IntMap a -> IntMap a
go IntMap a
Nil         = IntMap a
forall a. IntMap a
Nil
    go t :: IntMap a
t@(Tip Int
k a
x) = if Int -> a -> Bool
predicate Int
k a
x then IntMap a
t else IntMap a
forall a. IntMap a
Nil
    go (Bin Prefix
p IntMap a
l IntMap a
r) = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p (IntMap a -> IntMap a
go IntMap a
l) (IntMap a -> IntMap a
go IntMap a
r)
{-# INLINABLE filterWithKey #-} -- See Note [INLINABLE to expose unfoldings]

-- | \(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) -> IntMap a -> (IntMap a,IntMap a)
partition :: forall a. (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
partition a -> Bool
p = (Int -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
forall a. (Int -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
partitionWithKey (\Int
_ a
x -> a -> Bool
p a
x)
{-# INLINE partition #-}

-- | \(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) -> IntMap a -> (IntMap a,IntMap a)
partitionWithKey :: forall a. (Int -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
partitionWithKey Int -> a -> Bool
predicate0 IntMap a
t0 = StrictPair (IntMap a) (IntMap a) -> (IntMap a, IntMap a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (IntMap a) (IntMap a) -> (IntMap a, IntMap a))
-> StrictPair (IntMap a) (IntMap a) -> (IntMap a, IntMap a)
forall a b. (a -> b) -> a -> b
$ (Int -> a -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}.
(Int -> a -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> a -> Bool
predicate0 IntMap a
t0
  where
    go :: (Int -> a -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> a -> Bool
predicate IntMap a
t =
      case IntMap a
t of
        Bin Prefix
p IntMap a
l IntMap a
r ->
          let (IntMap a
l1 :*: IntMap a
l2) = (Int -> a -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> a -> Bool
predicate IntMap a
l
              (IntMap a
r1 :*: IntMap a
r2) = (Int -> a -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> a -> Bool
predicate IntMap a
r
          in Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p IntMap a
l1 IntMap a
r1 IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p IntMap a
l2 IntMap a
r2
        Tip Int
k a
x
          | Int -> a -> Bool
predicate Int
k a
x -> (IntMap a
t IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
          | Bool
otherwise     -> (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t)
        IntMap a
Nil -> (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap 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) -> IntMap a -> IntMap a
takeWhileAntitone :: forall a. (Int -> Bool) -> IntMap a -> IntMap a
takeWhileAntitone Int -> Bool
predicate IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p ->
        if Int -> Bool
predicate Int
0 -- handle negative numbers.
        then Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
l) IntMap a
r
        else (Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
r
    IntMap a
_ -> (Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
t
  where
    go :: (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Bool
predicate' (Prefix -> Int
unPrefix Prefix
p) = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l ((Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' IntMap a
r)
      | Bool
otherwise               = (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' IntMap a
l
    go Int -> Bool
predicate' t' :: IntMap a
t'@(Tip Int
ky a
_)
      | Int -> Bool
predicate' Int
ky = IntMap a
t'
      | Bool
otherwise     = IntMap a
forall a. IntMap a
Nil
    go Int -> Bool
_ IntMap a
Nil = IntMap a
forall a. IntMap 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) -> IntMap a -> IntMap a
dropWhileAntitone :: forall a. (Int -> Bool) -> IntMap a -> IntMap a
dropWhileAntitone Int -> Bool
predicate IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p ->
        if Int -> Bool
predicate Int
0 -- handle negative numbers.
        then (Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
l
        else Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l ((Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
r)
    IntMap a
_ -> (Int -> Bool) -> IntMap a -> IntMap a
forall a. (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate IntMap a
t
  where
    go :: (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Bool
predicate' (Prefix -> Int
unPrefix Prefix
p) = (Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' IntMap a
r
      | Bool
otherwise               = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Int -> Bool) -> IntMap a -> IntMap a
go Int -> Bool
predicate' IntMap a
l) IntMap a
r
    go Int -> Bool
predicate' t' :: IntMap a
t'@(Tip Int
ky a
_)
      | Int -> Bool
predicate' Int
ky = IntMap a
forall a. IntMap a
Nil
      | Bool
otherwise     = IntMap a
t'
    go Int -> Bool
_ IntMap a
Nil = IntMap a
forall a. IntMap 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) -> IntMap a -> (IntMap a, IntMap a)
spanAntitone :: forall a. (Int -> Bool) -> IntMap a -> (IntMap a, IntMap a)
spanAntitone Int -> Bool
predicate IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p ->
        if Int -> Bool
predicate Int
0 -- handle negative numbers.
        then
          case (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}.
(Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate IntMap a
l of
            (IntMap a
lt :*: IntMap a
gt) ->
              let !lt' :: IntMap a
lt' = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
lt IntMap a
r
              in (IntMap a
lt', IntMap a
gt)
        else
          case (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}.
(Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate IntMap a
r of
            (IntMap a
lt :*: IntMap a
gt) ->
              let !gt' :: IntMap a
gt' = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
gt
              in (IntMap a
lt, IntMap a
gt')
    IntMap a
_ -> case (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}.
(Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate IntMap a
t of
          (IntMap a
lt :*: IntMap a
gt) -> (IntMap a
lt, IntMap a
gt)
  where
    go :: (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate' (Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Bool
predicate' (Prefix -> Int
unPrefix Prefix
p)
      = case (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate' IntMap a
r of (IntMap a
lt :*: IntMap a
gt) -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
lt IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
gt
      | Bool
otherwise
      = case (Int -> Bool) -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int -> Bool
predicate' IntMap a
l of (IntMap a
lt :*: IntMap a
gt) -> IntMap a
lt IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
gt IntMap a
r
    go Int -> Bool
predicate' t' :: IntMap a
t'@(Tip Int
ky a
_)
      | Int -> Bool
predicate' Int
ky = (IntMap a
t' IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
      | Bool
otherwise     = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t')
    go Int -> Bool
_ IntMap a
Nil = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap 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) -> IntMap a -> IntMap b
mapMaybe :: forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
mapMaybe a -> Maybe b
f = (Int -> a -> Maybe b) -> IntMap a -> IntMap b
forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey (\Int
_ 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) -> IntMap a -> IntMap b
mapMaybeWithKey :: forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey Int -> a -> Maybe b
f (Bin Prefix
p IntMap a
l IntMap a
r)
  = Prefix -> IntMap b -> IntMap b -> IntMap b
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p ((Int -> a -> Maybe b) -> IntMap a -> IntMap b
forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey Int -> a -> Maybe b
f IntMap a
l) ((Int -> a -> Maybe b) -> IntMap a -> IntMap b
forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey Int -> a -> Maybe b
f IntMap a
r)
mapMaybeWithKey Int -> a -> Maybe b
f (Tip Int
k a
x) = case Int -> a -> Maybe b
f Int
k a
x of
  Just b
y  -> Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
k b
y
  Maybe b
Nothing -> IntMap b
forall a. IntMap a
Nil
mapMaybeWithKey Int -> a -> Maybe b
_ IntMap a
Nil = IntMap b
forall a. IntMap 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) -> IntMap a -> (IntMap b, IntMap c)
mapEither :: forall a b c. (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
mapEither a -> Either b c
f IntMap a
m
  = (Int -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
forall a b c.
(Int -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
mapEitherWithKey (\Int
_ a
x -> a -> Either b c
f a
x) IntMap 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) -> IntMap a -> (IntMap b, IntMap c)
mapEitherWithKey :: forall a b c.
(Int -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
mapEitherWithKey Int -> a -> Either b c
f0 IntMap a
t0 = StrictPair (IntMap b) (IntMap c) -> (IntMap b, IntMap c)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (IntMap b) (IntMap c) -> (IntMap b, IntMap c))
-> StrictPair (IntMap b) (IntMap c) -> (IntMap b, IntMap c)
forall a b. (a -> b) -> a -> b
$ (Int -> a -> Either b c)
-> IntMap a -> StrictPair (IntMap b) (IntMap c)
forall {t} {a} {a}.
(Int -> t -> Either a a)
-> IntMap t -> StrictPair (IntMap a) (IntMap a)
go Int -> a -> Either b c
f0 IntMap a
t0
  where
    go :: (Int -> t -> Either a a)
-> IntMap t -> StrictPair (IntMap a) (IntMap a)
go Int -> t -> Either a a
f (Bin Prefix
p IntMap t
l IntMap t
r) =
      Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p IntMap a
l1 IntMap a
r1 IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p IntMap a
l2 IntMap a
r2
      where
        (IntMap a
l1 :*: IntMap a
l2) = (Int -> t -> Either a a)
-> IntMap t -> StrictPair (IntMap a) (IntMap a)
go Int -> t -> Either a a
f IntMap t
l
        (IntMap a
r1 :*: IntMap a
r2) = (Int -> t -> Either a a)
-> IntMap t -> StrictPair (IntMap a) (IntMap a)
go Int -> t -> Either a a
f IntMap t
r
    go Int -> t -> Either a a
f (Tip Int
k t
x) = case Int -> t -> Either a a
f Int
k t
x of
      Left a
y  -> (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
y IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
      Right a
z -> (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
z)
    go Int -> t -> Either a a
_ IntMap t
Nil = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap 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 -> IntMap a -> (IntMap a, IntMap a)
split :: forall a. Int -> IntMap a -> (IntMap a, IntMap a)
split Int
k IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p ->
        if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 -- handle negative numbers.
        then
          case Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}. Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int
k IntMap a
l of
            (IntMap a
lt :*: IntMap a
gt) ->
              let !lt' :: IntMap a
lt' = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
lt IntMap a
r
              in (IntMap a
lt', IntMap a
gt)
        else
          case Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}. Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int
k IntMap a
r of
            (IntMap a
lt :*: IntMap a
gt) ->
              let !gt' :: IntMap a
gt' = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
gt
              in (IntMap a
lt, IntMap a
gt')
    IntMap a
_ -> case Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall {a}. Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int
k IntMap a
t of
          (IntMap a
lt :*: IntMap a
gt) -> (IntMap a
lt, IntMap a
gt)
  where
    go :: Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go !Int
k' t' :: IntMap a
t'@(Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k' Prefix
p = if Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p then IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t' else IntMap a
t' IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil
      | Int -> Prefix -> Bool
left Int
k' Prefix
p = case Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int
k' IntMap a
l of (IntMap a
lt :*: IntMap a
gt) -> IntMap a
lt IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
gt IntMap a
r
      | Bool
otherwise = case Int -> IntMap a -> StrictPair (IntMap a) (IntMap a)
go Int
k' IntMap a
r of (IntMap a
lt :*: IntMap a
gt) -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l IntMap a
lt IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
gt
    go Int
k' t' :: IntMap a
t'@(Tip Int
ky a
_)
      | Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ky   = (IntMap a
t' IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
      | Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ky   = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t')
      | Bool
otherwise = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
    go Int
_ IntMap a
Nil = (IntMap a
forall a. IntMap a
Nil IntMap a -> IntMap a -> StrictPair (IntMap a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)


type SplitLookup a = StrictTriple (IntMap a) (Maybe a) (IntMap a)

mapLT :: (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapLT :: forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapLT IntMap a -> IntMap a
f (TripleS IntMap a
lt Maybe a
fnd IntMap a
gt) = IntMap a
-> Maybe a
-> IntMap a
-> StrictTriple (IntMap a) (Maybe a) (IntMap a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS (IntMap a -> IntMap a
f IntMap a
lt) Maybe a
fnd IntMap a
gt
{-# INLINE mapLT #-}

mapGT :: (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapGT :: forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapGT IntMap a -> IntMap a
f (TripleS IntMap a
lt Maybe a
fnd IntMap a
gt) = IntMap a
-> Maybe a
-> IntMap a
-> StrictTriple (IntMap a) (Maybe a) (IntMap a)
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
lt Maybe a
fnd (IntMap a -> IntMap a
f IntMap 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 -> IntMap a -> (IntMap a, Maybe a, IntMap a)
splitLookup :: forall a. Int -> IntMap a -> (IntMap a, Maybe a, IntMap a)
splitLookup Int
k IntMap a
t =
  case
    case IntMap a
t of
      Bin Prefix
p IntMap a
l IntMap a
r
        | Prefix -> Bool
signBranch Prefix
p ->
          if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 -- handle negative numbers.
          then (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapLT (\IntMap a
l' -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r) (Int -> IntMap a -> SplitLookup a
forall {a}. Int -> IntMap a -> SplitLookup a
go Int
k IntMap a
l)
          else (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapGT (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l) (Int -> IntMap a -> SplitLookup a
forall {a}. Int -> IntMap a -> SplitLookup a
go Int
k IntMap a
r)
      IntMap a
_ -> Int -> IntMap a -> SplitLookup a
forall {a}. Int -> IntMap a -> SplitLookup a
go Int
k IntMap a
t
  of TripleS IntMap a
lt Maybe a
fnd IntMap a
gt -> (IntMap a
lt, Maybe a
fnd, IntMap a
gt)
  where
    go :: Int -> IntMap a -> SplitLookup a
go !Int
k' t' :: IntMap a
t'@(Bin Prefix
p IntMap a
l IntMap a
r)
      | Int -> Prefix -> Bool
nomatch Int
k' Prefix
p =
          if Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p
          then IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
forall a. IntMap a
Nil Maybe a
forall a. Maybe a
Nothing IntMap a
t'
          else IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
t' Maybe a
forall a. Maybe a
Nothing IntMap a
forall a. IntMap a
Nil
      | Int -> Prefix -> Bool
left Int
k' Prefix
p = (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapGT (\IntMap a
l' -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p IntMap a
l' IntMap a
r) (Int -> IntMap a -> SplitLookup a
go Int
k' IntMap a
l)
      | Bool
otherwise  = (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
forall a. (IntMap a -> IntMap a) -> SplitLookup a -> SplitLookup a
mapLT (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
p IntMap a
l) (Int -> IntMap a -> SplitLookup a
go Int
k' IntMap a
r)
    go Int
k' t' :: IntMap a
t'@(Tip Int
ky a
y)
      | Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ky   = IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
t'  Maybe a
forall a. Maybe a
Nothing  IntMap a
forall a. IntMap a
Nil
      | Int
k' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ky   = IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
forall a. IntMap a
Nil Maybe a
forall a. Maybe a
Nothing  IntMap a
t'
      | Bool
otherwise = IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
forall a. IntMap a
Nil (a -> Maybe a
forall a. a -> Maybe a
Just a
y) IntMap a
forall a. IntMap a
Nil
    go Int
_ IntMap a
Nil      = IntMap a -> Maybe a -> IntMap a -> SplitLookup a
forall a b c. a -> b -> c -> StrictTriple a b c
TripleS IntMap a
forall a. IntMap a
Nil Maybe a
forall a. Maybe a
Nothing  IntMap a
forall a. IntMap 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

-- See Note [IntMap folds]
foldr :: (a -> b -> b) -> b -> IntMap a -> b
foldr :: forall a b. (a -> b -> b) -> b -> IntMap a -> b
foldr a -> b -> b
f b
z = \IntMap a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap a
t of
    IntMap a
Nil -> b
z
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
l) IntMap a
r -- put negative numbers before
      | Bool
otherwise -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
r) IntMap a
l
    IntMap a
_ -> b -> IntMap a -> b
go b
z IntMap a
t
  where
    go :: b -> IntMap a -> b
go b
_ IntMap a
Nil          = [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"foldr.go: Nil"
    go b
z' (Tip Int
_ a
x)   = a -> b -> b
f a
x b
z'
    go b
z' (Bin Prefix
_ IntMap a
l IntMap a
r) = b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z' IntMap a
r) IntMap 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.

-- See Note [IntMap folds]
foldr' :: (a -> b -> b) -> b -> IntMap a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> IntMap a -> b
foldr' a -> b -> b
f b
z = \IntMap a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap a
t of
    IntMap a
Nil -> b
z
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
l) IntMap a
r -- put negative numbers before
      | Bool
otherwise -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
r) IntMap a
l
    IntMap a
_ -> b -> IntMap a -> b
go b
z IntMap a
t
  where
    go :: b -> IntMap a -> b
go !b
_ IntMap a
Nil         = [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"foldr'.go: Nil"
    go b
z' (Tip Int
_ a
x)   = a -> b -> b
f a
x b
z'
    go b
z' (Bin Prefix
_ IntMap a
l IntMap a
r) = b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z' IntMap a
r) IntMap 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

-- See Note [IntMap folds]
foldl :: (a -> b -> a) -> a -> IntMap b -> a
foldl :: forall b a. (b -> a -> b) -> b -> IntMap a -> b
foldl a -> b -> a
f a
z = \IntMap b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap b
t of
    IntMap b
Nil -> a
z
    Bin Prefix
p IntMap b
l IntMap b
r
      | Prefix -> Bool
signBranch Prefix
p -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
r) IntMap b
l -- put negative numbers before
      | Bool
otherwise -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
l) IntMap b
r
    IntMap b
_ -> a -> IntMap b -> a
go a
z IntMap b
t
  where
    go :: a -> IntMap b -> a
go a
_ IntMap b
Nil          = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"foldl.go: Nil"
    go a
z' (Tip Int
_ b
x)   = a -> b -> a
f a
z' b
x
    go a
z' (Bin Prefix
_ IntMap b
l IntMap b
r) = a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z' IntMap b
l) IntMap 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.

-- See Note [IntMap folds]
foldl' :: (a -> b -> a) -> a -> IntMap b -> a
foldl' :: forall b a. (b -> a -> b) -> b -> IntMap a -> b
foldl' a -> b -> a
f a
z = \IntMap b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap b
t of
    IntMap b
Nil -> a
z
    Bin Prefix
p IntMap b
l IntMap b
r
      | Prefix -> Bool
signBranch Prefix
p -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
r) IntMap b
l -- put negative numbers before
      | Bool
otherwise -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
l) IntMap b
r
    IntMap b
_ -> a -> IntMap b -> a
go a
z IntMap b
t
  where
    go :: a -> IntMap b -> a
go !a
_ IntMap b
Nil         = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"foldl'.go: Nil"
    go a
z' (Tip Int
_ b
x)   = a -> b -> a
f a
z' b
x
    go a
z' (Bin Prefix
_ IntMap b
l IntMap b
r) = a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z' IntMap b
l) IntMap b
r
{-# INLINE foldl' #-}

-- See Note [IntMap folds]
foldMap :: Monoid m => (a -> m) -> IntMap a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> IntMap a -> m
foldMap a -> m
f = \IntMap a
t -> -- Use lambda to be inlinable with two arguments.
  case IntMap a
t of
    IntMap a
Nil -> m
forall a. Monoid a => a
mempty
    Bin Prefix
p IntMap a
l IntMap a
r
#if MIN_VERSION_base(4,11,0)
      | Prefix -> Bool
signBranch Prefix
p -> IntMap a -> m
go IntMap a
r m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
l
      | Bool
otherwise -> IntMap a -> m
go IntMap a
l m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
r
#else
      | signBranch p -> go r `mappend` go l
      | otherwise -> go l `mappend` go r
#endif
    IntMap a
_ -> IntMap a -> m
go IntMap a
t
  where
    go :: IntMap a -> m
go IntMap a
Nil = [Char] -> m
forall a. HasCallStack => [Char] -> a
error [Char]
"foldMap.go: Nil"
    go (Tip Int
_ a
x) = a -> m
f a
x
#if MIN_VERSION_base(4,11,0)
    go (Bin Prefix
_ IntMap a
l IntMap a
r) = IntMap a -> m
go IntMap a
l m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
r
#else
    go (Bin _ l r) = go l `mappend` go r
#endif
{-# INLINE foldMap #-}

-- | \(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)"

-- See Note [IntMap folds]
foldrWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey :: forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey Int -> a -> b -> b
f b
z = \IntMap a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap a
t of
    IntMap a
Nil -> b
z
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
l) IntMap a
r -- put negative numbers before
      | Bool
otherwise -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
r) IntMap a
l
    IntMap a
_ -> b -> IntMap a -> b
go b
z IntMap a
t
  where
    go :: b -> IntMap a -> b
go b
_ IntMap a
Nil          = [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"foldrWithKey.go: Nil"
    go b
z' (Tip Int
kx a
x)  = Int -> a -> b -> b
f Int
kx a
x b
z'
    go b
z' (Bin Prefix
_ IntMap a
l IntMap a
r) = b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z' IntMap a
r) IntMap 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.

-- See Note [IntMap folds]
foldrWithKey' :: (Key -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey' :: forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey' Int -> a -> b -> b
f b
z = \IntMap a
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap a
t of
    IntMap a
Nil -> b
z
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
l) IntMap a
r -- put negative numbers before
      | Bool
otherwise -> b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z IntMap a
r) IntMap a
l
    IntMap a
_ -> b -> IntMap a -> b
go b
z IntMap a
t
  where
    go :: b -> IntMap a -> b
go !b
_ IntMap a
Nil         = [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"foldrWithKey'.go: Nil"
    go b
z' (Tip Int
kx a
x)  = Int -> a -> b -> b
f Int
kx a
x b
z'
    go b
z' (Bin Prefix
_ IntMap a
l IntMap a
r) = b -> IntMap a -> b
go (b -> IntMap a -> b
go b
z' IntMap a
r) IntMap 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)"

-- See Note [IntMap folds]
foldlWithKey :: (a -> Key -> b -> a) -> a -> IntMap b -> a
foldlWithKey :: forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey a -> Int -> b -> a
f a
z = \IntMap b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap b
t of
    IntMap b
Nil -> a
z
    Bin Prefix
p IntMap b
l IntMap b
r
      | Prefix -> Bool
signBranch Prefix
p -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
r) IntMap b
l -- put negative numbers before
      | Bool
otherwise -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
l) IntMap b
r
    IntMap b
_ -> a -> IntMap b -> a
go a
z IntMap b
t
  where
    go :: a -> IntMap b -> a
go a
_ IntMap b
Nil          = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"foldlWithKey.go: Nil"
    go a
z' (Tip Int
kx b
x)  = a -> Int -> b -> a
f a
z' Int
kx b
x
    go a
z' (Bin Prefix
_ IntMap b
l IntMap b
r) = a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z' IntMap b
l) IntMap 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.

-- See Note [IntMap folds]
foldlWithKey' :: (a -> Key -> b -> a) -> a -> IntMap b -> a
foldlWithKey' :: forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey' a -> Int -> b -> a
f a
z = \IntMap b
t ->      -- Use lambda t to be inlinable with two arguments only.
  case IntMap b
t of
    IntMap b
Nil -> a
z
    Bin Prefix
p IntMap b
l IntMap b
r
      | Prefix -> Bool
signBranch Prefix
p -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
r) IntMap b
l -- put negative numbers before
      | Bool
otherwise -> a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z IntMap b
l) IntMap b
r
    IntMap b
_ -> a -> IntMap b -> a
go a
z IntMap b
t
  where
    go :: a -> IntMap b -> a
go !a
_ IntMap b
Nil         = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"foldlWithKey'.go: Nil"
    go a
z' (Tip Int
kx b
x)  = a -> Int -> b -> a
f a
z' Int
kx b
x
    go a
z' (Bin Prefix
_ IntMap b
l IntMap b
r) = a -> IntMap b -> a
go (a -> IntMap b -> a
go a
z' IntMap b
l) IntMap 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

-- See Note [IntMap folds]
foldMapWithKey :: Monoid m => (Key -> a -> m) -> IntMap a -> m
foldMapWithKey :: forall m a. Monoid m => (Int -> a -> m) -> IntMap a -> m
foldMapWithKey Int -> a -> m
f = \IntMap a
t -> -- Use lambda to be inlinable with two arguments.
  case IntMap a
t of
    IntMap a
Nil -> m
forall a. Monoid a => a
mempty
    Bin Prefix
p IntMap a
l IntMap a
r
#if MIN_VERSION_base(4,11,0)
      | Prefix -> Bool
signBranch Prefix
p -> IntMap a -> m
go IntMap a
r m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
l
      | Bool
otherwise -> IntMap a -> m
go IntMap a
l m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
r
#else
      | signBranch p -> go r `mappend` go l
      | otherwise -> go l `mappend` go r
#endif
    IntMap a
_ -> IntMap a -> m
go IntMap a
t
  where
    go :: IntMap a -> m
go IntMap a
Nil = [Char] -> m
forall a. HasCallStack => [Char] -> a
error [Char]
"foldMap.go: Nil"
    go (Tip Int
kx a
x) = Int -> a -> m
f Int
kx a
x
#if MIN_VERSION_base(4,11,0)
    go (Bin Prefix
_ IntMap a
l IntMap a
r) = IntMap a -> m
go IntMap a
l m -> m -> m
forall a. Semigroup a => a -> a -> a
<> IntMap a -> m
go IntMap a
r
#else
    go (Bin _ l r) = go l `mappend` go r
#endif
{-# 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 :: IntMap a -> [a]
elems :: forall a. IntMap a -> [a]
elems = (a -> [a] -> [a]) -> [a] -> IntMap a -> [a]
forall a b. (a -> b -> b) -> b -> IntMap 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  :: IntMap a -> [Key]
keys :: forall a. IntMap a -> [Int]
keys = (Int -> a -> [Int] -> [Int]) -> [Int] -> IntMap a -> [Int]
forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey (\Int
k a
_ [Int]
ks -> Int
k Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
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 :: IntMap a -> [(Key,a)]
assocs :: forall a. IntMap a -> [(Int, a)]
assocs = IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
toAscList

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

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

-- | \(O(n)\). Build a map from an 'IntSet' of keys and a function which for
-- each key computes its value.
--
-- > fromSet (\k -> replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
fromSet :: (Key -> a) -> IntSet -> IntMap a
#ifdef __GLASGOW_HASKELL__
fromSet :: forall a. (Int -> a) -> IntSet -> IntMap a
fromSet =
  (((Int -> Identity a) -> IntSet -> Identity (IntMap a))
-> (Int -> a) -> IntSet -> IntMap a
forall {a}.
((Int -> Identity a) -> IntSet -> Identity (IntMap a))
-> (Int -> a) -> IntSet -> IntMap a
forall a b. Coercible a b => a -> b
coerce :: ((Key -> Identity a) -> IntSet -> Identity (IntMap a))
          -> (Key -> a) -> IntSet -> IntMap a)
    (Int -> Identity a) -> IntSet -> Identity (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA
#else
fromSet f = runIdentity . fromSetA (pure . f)
#endif

-- | \(O(n)\). Build a map from an 'IntSet' of keys and a function which for
-- each key computes its value, while within an 'Applicative' context.
--
-- The @Applicative@ actions are sequenced in order of increasing key.
--
-- > let f k = if k == 0 then Nothing else Just (6 `div` k)
-- > fromSetA f (Data.Set.fromList [1,2,3,4]) == Just (fromList [(1,6),(2,3),(3,2),(4,1)])
-- > fromSetA f (Data.Set.fromList [0,1,2]) == Nothing
--
-- @since 0.8.1
fromSetA :: Applicative f => (Key -> f a) -> IntSet -> f (IntMap a)
fromSetA :: forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA Int -> f a
_ IntSet
IntSet.Nil = IntMap a -> f (IntMap a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap a
forall a. IntMap a
Nil
fromSetA Int -> f a
f (IntSet.Bin Prefix
p IntSet
l IntSet
r)
  | Prefix -> Bool
signBranch Prefix
p = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 ((IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> IntMap a -> IntMap a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p)) ((Int -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA Int -> f a
f IntSet
r) ((Int -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA Int -> f a
f IntSet
l)
  | Bool
otherwise = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p) ((Int -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA Int -> f a
f IntSet
l) ((Int -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f a) -> IntSet -> f (IntMap a)
fromSetA Int -> f a
f IntSet
r)
fromSetA Int -> f a
f (IntSet.Tip Int
kx Word
bm) =
  (Int -> f (IntMap a))
-> (Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a))
-> Int
-> Word
-> f (IntMap a)
forall a. (Int -> a) -> (Prefix -> a -> a -> a) -> Int -> Word -> a
treeFromIntSetTip (\Int
kx' -> Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx' (a -> IntMap a) -> f a -> f (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f a
f Int
kx') (\Prefix
p -> (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p)) Int
kx Word
bm
{-# INLINABLE fromSetA #-}

-- | \(O(n)\). Build a map from an 'IntSet' of keys and a function which for each
-- key optionally computes its value.
--
-- > let f k = if even k then Just (replicate k 'a') else Nothing
-- > fromSetMaybe f (Data.IntSet.fromList [1,2,3,4]) == fromList [(2,"aa"), (4,"aaaa")]
--
-- @since 0.8.1
fromSetMaybe :: (Key -> Maybe a) -> IntSet -> IntMap a
#ifdef __GLASGOW_HASKELL__
fromSetMaybe :: forall a. (Int -> Maybe a) -> IntSet -> IntMap a
fromSetMaybe =
  (((Int -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a))
-> (Int -> Maybe a) -> IntSet -> IntMap a
forall {a}.
((Int -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a))
-> (Int -> Maybe a) -> IntSet -> IntMap a
forall a b. Coercible a b => a -> b
coerce :: ((Key -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a))
          -> (Key -> Maybe a) -> IntSet -> IntMap a)
    (Int -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Int -> f (Maybe a)) -> IntSet -> f (IntMap a)
fromSetMaybeA
#else
fromSetMaybe f s = runIdentity (fromSetMaybeA (Identity . f) s)
#endif

-- | \(O(n)\). Build a map from an 'IntSet' of keys and a function which for
-- each key optionally computes its value in an 'Applicative' context.
--
-- The @Applicative@ actions are sequenced in order of increasing key.
--
-- @since 0.8.1
fromSetMaybeA :: Applicative f => (Key -> f (Maybe a)) -> IntSet -> f (IntMap a)
fromSetMaybeA :: forall (f :: * -> *) a.
Applicative f =>
(Int -> f (Maybe a)) -> IntSet -> f (IntMap a)
fromSetMaybeA Int -> f (Maybe a)
f = IntSet -> f (IntMap a)
go
  where
   go :: IntSet -> f (IntMap a)
go IntSet
IntSet.Nil = IntMap a -> f (IntMap a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap a
forall a. IntMap a
Nil
   go (IntSet.Bin Prefix
p IntSet
l IntSet
r)
     | Prefix -> Bool
signBranch Prefix
p = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 ((IntMap a -> IntMap a -> IntMap a)
-> IntMap a -> IntMap a -> IntMap a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p)) (IntSet -> f (IntMap a)
go IntSet
r) (IntSet -> f (IntMap a)
go IntSet
l)
     | Bool
otherwise = (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p) (IntSet -> f (IntMap a)
go IntSet
l) (IntSet -> f (IntMap a)
go IntSet
r)
   go (IntSet.Tip Int
kx Word
bm) =
     (Int -> f (IntMap a))
-> (Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a))
-> Int
-> Word
-> f (IntMap a)
forall a. (Int -> a) -> (Prefix -> a -> a -> a) -> Int -> Word -> a
treeFromIntSetTip
       (\Int
kx' -> IntMap a -> (a -> IntMap a) -> Maybe a -> IntMap a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
forall a. IntMap a
Nil (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx') (Maybe a -> IntMap a) -> f (Maybe a) -> f (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (Maybe a)
f Int
kx')
       (\Prefix
p -> (IntMap a -> IntMap a -> IntMap a)
-> f (IntMap a) -> f (IntMap a) -> f (IntMap 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 (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
p))
       Int
kx
       Word
bm
{-# INLINABLE fromSetMaybeA #-}

-- Internal helper used by fromSet and friends.
treeFromIntSetTip :: (Int -> a) -> (Prefix -> a -> a -> a) -> Int -> Word -> a
treeFromIntSetTip :: forall a. (Int -> a) -> (Prefix -> a -> a -> a) -> Int -> Word -> a
treeFromIntSetTip Int -> a
tipf Prefix -> a -> a -> a
binf Int
kx Word
bm = Int -> Word -> Int -> a
buildTree Int
kx Word
bm (Int
IntSet.suffixBitMask Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
  where
    -- This is slightly complicated, as we to convert the dense
    -- representation of IntSet into tree representation of IntMap.
    --
    -- 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 IntMap from that half.
    buildTree :: Int -> Word -> Int -> a
buildTree !Int
prefix !Word
bmask Int
bits = case Int
bits of
      Int
0 -> Int -> a
tipf Int
prefix
      Int
_ -> case Int
bits Int -> Int -> Int
`iShiftRL` Int
1 of
        Int
bits2
          | Word
bmask Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. ((Word
1 Word -> Int -> Word
`shiftLL` Int
bits2) Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1) Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 ->
              Int -> Word -> Int -> a
buildTree (Int
prefix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bits2) (Word
bmask Word -> Int -> Word
`shiftRL` Int
bits2) Int
bits2
          | (Word
bmask Word -> Int -> Word
`shiftRL` Int
bits2) Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. ((Word
1 Word -> Int -> Word
`shiftLL` Int
bits2) Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1) Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 ->
              Int -> Word -> Int -> a
buildTree Int
prefix Word
bmask Int
bits2
          | Bool
otherwise ->
             Prefix -> a -> a -> a
binf (Int -> Prefix
Prefix (Int
prefix Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
bits2))
                  (Int -> Word -> Int -> a
buildTree Int
prefix Word
bmask Int
bits2)
                  (Int -> Word -> Int -> a
buildTree (Int
prefix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bits2) (Word
bmask Word -> Int -> Word
`shiftRL` Int
bits2) Int
bits2)
{-# INLINE treeFromIntSetTip #-}

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

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.6.2
instance GHCExts.IsList (IntMap a) where
  type Item (IntMap a) = (Key,a)
  fromList :: [Item (IntMap a)] -> IntMap a
fromList = [(Int, a)] -> IntMap a
[Item (IntMap a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
fromList
  toList :: IntMap a -> [Item (IntMap a)]
toList   = IntMap a -> [(Int, a)]
IntMap a -> [Item (IntMap a)]
forall a. IntMap a -> [(Int, a)]
toList
#endif

-- | \(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 :: IntMap a -> [(Key,a)]
toList :: forall a. IntMap a -> [(Int, a)]
toList = IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, 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 :: IntMap a -> [(Key,a)]
toAscList :: forall a. IntMap a -> [(Int, a)]
toAscList = (Int -> a -> [(Int, a)] -> [(Int, a)])
-> [(Int, a)] -> IntMap a -> [(Int, a)]
forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey (\Int
k a
x [(Int, a)]
xs -> (Int
k,a
x)(Int, a) -> [(Int, a)] -> [(Int, a)]
forall a. a -> [a] -> [a]
:[(Int, 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 :: IntMap a -> [(Key,a)]
toDescList :: forall a. IntMap a -> [(Int, a)]
toDescList = ([(Int, a)] -> Int -> a -> [(Int, a)])
-> [(Int, a)] -> IntMap a -> [(Int, a)]
forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlWithKey (\[(Int, a)]
xs Int
k a
x -> (Int
k,a
x)(Int, a) -> [(Int, a)] -> [(Int, a)]
forall a. a -> [a] -> [a]
:[(Int, a)]
xs) []

-- List fusion for the list generating functions.
#if __GLASGOW_HASKELL__
-- 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 -> IntMap a -> b
foldrFB :: forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrFB = (Int -> a -> b -> b) -> b -> IntMap a -> b
forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
foldrWithKey
{-# INLINE[0] foldrFB #-}
foldlFB :: (a -> Key -> b -> a) -> a -> IntMap b -> a
foldlFB :: forall a b. (a -> Int -> b -> a) -> a -> IntMap b -> a
foldlFB = (a -> Int -> b -> a) -> a -> IntMap b -> a
forall a b. (a -> Int -> b -> a) -> a -> IntMap 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 "IntMap.elems" [~1] forall m . elems m = build (\c n -> foldrFB (\_ x xs -> c x xs) n m) #-}
{-# RULES "IntMap.elemsBack" [1] foldrFB (\_ x xs -> x : xs) [] = elems #-}
{-# RULES "IntMap.keys" [~1] forall m . keys m = build (\c n -> foldrFB (\k _ xs -> c k xs) n m) #-}
{-# RULES "IntMap.keysBack" [1] foldrFB (\k _ xs -> k : xs) [] = keys #-}
{-# RULES "IntMap.toAscList" [~1] forall m . toAscList m = build (\c n -> foldrFB (\k x xs -> c (k,x) xs) n m) #-}
{-# RULES "IntMap.toAscListBack" [1] foldrFB (\k x xs -> (k, x) : xs) [] = toAscList #-}
{-# RULES "IntMap.toDescList" [~1] forall m . toDescList m = build (\c n -> foldlFB (\xs k x -> c (k,x) xs) n m) #-}
{-# RULES "IntMap.toDescListBack" [1] foldlFB (\xs k x -> (k, x) : xs) [] = toDescList #-}
#endif


-- | \(O(n \min(n,W))\). Create a map from a list of key\/value pairs.
-- If the list contains more than one value for the same key, the last value
-- for the key is retained.
--
-- If the keys are in sorted order, ascending or descending, this function
-- takes \(O(n)\) time.
--
-- > 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)] -> IntMap a
fromList :: forall a. [(Int, a)] -> IntMap a
fromList [(Int, a)]
xs = IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> (Int, a) -> IntMapBuilder a)
-> IntMapBuilder a -> [(Int, a)] -> IntMapBuilder 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' (\IntMapBuilder a
b (Int
kx,a
x) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
forall a. Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertB Int
kx a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB [(Int, a)]
xs)
{-# INLINE fromList #-} -- Inline for list fusion

-- | \(O(n \min(n,W))\). Build a map from a list of key\/value pairs with a combining function.
--
-- If the keys are in sorted order, ascending or descending, this function
-- takes \(O(n)\) time.
--
-- > fromListWith (++) [(5,"a"), (5,"b"), (3,"x"), (5,"c")] == fromList [(3, "x"), (5, "cba")]
-- > fromListWith (++) [] == empty
--
-- Note the reverse ordering of @"cba"@ in the example.
--
-- The symmetric combining function @f@ is applied in a left-fold over the list, as @f new old@.
--
-- See also: 'fromListUpsert'
--
-- === Performance
--
-- You should ensure that the given @f@ is fast with this order of arguments.
--
-- Symmetric functions may be slow in one order, and fast in another.
-- For the common case of collecting values of matching keys in a list, as above:
--
-- The complexity of @(++) a b@ is \(O(a)\), so it is fast when given a short list as its first argument.
-- Thus:
--
-- > fromListWith       (++)  (replicate 1000000 (3, "x"))   -- O(n),  fast
-- > fromListWith (flip (++)) (replicate 1000000 (3, "x"))   -- O(n²), extremely slow
--
-- because they evaluate as, respectively:
--
-- > fromList [(3, "x" ++ ("x" ++ "xxxxx..xxxxx"))]   -- O(n)
-- > fromList [(3, ("xxxxx..xxxxx" ++ "x") ++ "x")]   -- O(n²)
--
-- Thus, to get good performance with an operation like @(++)@ while also preserving
-- the same order as in the input list, reverse the input:
--
-- > fromListWith (++) (reverse [(5,"a"), (5,"b"), (5,"c")]) == fromList [(5, "abc")]
--
-- and it is always fast to combine singleton-list values @[v]@ with @fromListWith (++)@, as in:
--
-- > fromListWith (++) $ reverse $ map (\(k, v) -> (k, [v])) someListOfTuples

fromListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> IntMap a
fromListWith a -> a -> a
f [(Int, a)]
xs
  = (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
forall a. (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
fromListWithKey (\Int
_ a
x a
y -> a -> a -> a
f a
x a
y) [(Int, a)]
xs
{-# INLINE fromListWith #-} -- Inline for list fusion

-- | \(O(n \min(n,W))\). Build a map from a list of key\/value pairs with a combining function.
--
-- If the keys are in sorted order, ascending or descending, this function
-- takes \(O(n)\) time.
--
-- > 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
--
-- Also see the performance note on 'fromListWith'.
--
-- See also: 'fromListUpsert'

fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
fromListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
fromListWithKey Int -> a -> a -> a
f [(Int, a)]
xs =
  IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> (Int, a) -> IntMapBuilder a)
-> IntMapBuilder a -> [(Int, a)] -> IntMapBuilder 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' (\IntMapBuilder a
b (Int
kx,a
x) -> (a -> a -> a) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
forall a.
(a -> a -> a) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB (Int -> a -> a -> a
f Int
kx) Int
kx a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB [(Int, a)]
xs)
{-# INLINE fromListWithKey #-} -- Inline for list fusion

-- | \(O(n \min(n,W)\). Build a map from a list of key\/value pairs with a
-- combining function.
--
-- If the keys are in sorted order, ascending or descending, this function
-- takes \(O(n)\) time.
--
-- The result is equivalent to performing an @upsert@ for every key\/value in
-- the list.
--
-- @
-- fromListUpsert f = foldl' (\\m (k, x) -> 'upsert' (f x) k m) 'empty'
-- @
--
-- > let f x = maybe [x] (x:)
-- > fromListUpsert f [(5,'a'), (5,'b'), (3,'c'), (3,'d'), (5,'e')] == fromList [(3,"dc"), (5,"eba")]
--
-- @since 0.8.1
fromListUpsert :: (a -> Maybe b -> b) -> [(Key, a)] -> IntMap b
fromListUpsert :: forall a b. (a -> Maybe b -> b) -> [(Int, a)] -> IntMap b
fromListUpsert a -> Maybe b -> b
f [(Int, a)]
xs =
  IntMapBuilder b -> IntMap b
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder b -> (Int, a) -> IntMapBuilder b)
-> IntMapBuilder b -> [(Int, a)] -> IntMapBuilder b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' (\IntMapBuilder b
b (Int
kx, a
x) -> (Maybe b -> b) -> Int -> IntMapBuilder b -> IntMapBuilder b
forall a.
(Maybe a -> a) -> Int -> IntMapBuilder a -> IntMapBuilder a
upsertB (a -> Maybe b -> b
f a
x) Int
kx IntMapBuilder b
b) IntMapBuilder b
forall a. IntMapBuilder a
emptyB [(Int, a)]
xs)
{-# INLINE fromListUpsert #-}  -- INLINE for fusion

-- | \(O(n)\). Build a map from a list of key\/value pairs where
-- the keys are in ascending order.
--
-- __Warning__: This function should be used only if the keys are in
-- non-decreasing order. This precondition is not checked. Use 'fromList' if the
-- precondition may not hold.
--
-- > 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)] -> IntMap a
fromAscList :: forall a. [(Int, a)] -> IntMap a
fromAscList [(Int, a)]
xs =
  MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState a -> (Int, a) -> MonoState a)
-> MonoState a -> [(Int, a)] -> MonoState 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' (\MonoState a
s (Int
ky, a
y) -> MonoState a -> Int -> a -> MonoState a
forall a. MonoState a -> Int -> a -> MonoState a
ascInsert MonoState a
s Int
ky a
y) MonoState a
forall a. MonoState a
MSNada [(Int, a)]
xs)
{-# INLINE fromAscList #-} -- Inline for list fusion

-- | \(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.
--
-- __Warning__: This function should be used only if the keys are in
-- non-decreasing order. This precondition is not checked. Use 'fromListWith' if
-- the precondition may not hold.
--
-- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
--
-- Also see the performance note on 'fromListWith'.
--
-- See also: 'fromAscListUpsert'

fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> IntMap a
fromAscListWith a -> a -> a
f [(Int, a)]
xs = (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
forall a. (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
fromAscListWithKey (\Int
_ a
x a
y -> a -> a -> a
f a
x a
y) [(Int, a)]
xs
{-# INLINE fromAscListWith #-} -- Inline for list fusion

-- | \(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.
--
-- __Warning__: This function should be used only if the keys are in
-- non-decreasing order. This precondition is not checked. Use 'fromListWithKey'
-- if the precondition may not hold.
--
-- > let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
-- > fromAscListWithKey f [(3,"b"), (3,"a"), (5,"a"), (5,"b"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
-- > fromAscListWithKey f [] == empty
--
-- Also see the performance note on 'fromListWith'.
--
-- See also: 'fromAscListUpsert'

-- See Note [fromAscList implementation]
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
fromAscListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> IntMap a
fromAscListWithKey Int -> a -> a -> a
f [(Int, a)]
xs = MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState a -> (Int, a) -> MonoState a)
-> MonoState a -> [(Int, a)] -> MonoState 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' MonoState a -> (Int, a) -> MonoState a
next MonoState a
forall a. MonoState a
MSNada [(Int, a)]
xs)
  where
    next :: MonoState a -> (Int, a) -> MonoState a
next MonoState a
s (!Int
ky, a
y) = case MonoState a
s of
      MonoState a
MSNada -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y Stack a
forall a. Stack a
Nada
      MSPush Int
kx a
x Stack a
stk
        | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (Int -> a -> a -> a
f Int
ky a
y a
x) Stack a
stk
        | Bool
otherwise -> let m :: Int
m = Int -> Int -> Int
branchMask Int
kx Int
ky
                       in Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y (Stack a -> Int -> IntMap a -> Int -> Stack a
forall a. Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop Stack a
stk Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) Int
m)
{-# INLINE fromAscListWithKey #-} -- Inline for list fusion

-- | \(O(n)\). Build a map from an ascending list in linear time with a
-- combining function for equal keys.
--
-- __Warning__: This function should be used only if the keys are in
-- non-decreasing order. This precondition is not checked. Use 'fromListUpsert'
-- if the precondition may not hold.
--
-- > let f x = maybe [x] (x:)
-- > fromAscListUpsert f [(3,'a'), (3,'b'), (5,'c'), (5,'d'), (5,'e')] == fromList [(3,"ba"), (5,"edc")]
--
-- @since 0.8.1
fromAscListUpsert :: (a -> Maybe b -> b) -> [(Key, a)] -> IntMap b
fromAscListUpsert :: forall a b. (a -> Maybe b -> b) -> [(Int, a)] -> IntMap b
fromAscListUpsert a -> Maybe b -> b
f [(Int, a)]
xs = MonoState b -> IntMap b
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState b -> (Int, a) -> MonoState b)
-> MonoState b -> [(Int, a)] -> MonoState b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' MonoState b -> (Int, a) -> MonoState b
next MonoState b
forall a. MonoState a
MSNada [(Int, a)]
xs)
  where
    next :: MonoState b -> (Int, a) -> MonoState b
next MonoState b
s (!Int
ky, a
y) = case MonoState b
s of
      MonoState b
MSNada -> Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) Stack b
forall a. Stack a
Nada
      MSPush Int
kx b
x Stack b
stk
        | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y (b -> Maybe b
forall a. a -> Maybe a
Just b
x)) Stack b
stk
        | Bool
otherwise ->
            let m :: Int
m = Int -> Int -> Int
branchMask Int
kx Int
ky
            in Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) (Stack b -> Int -> IntMap b -> Int -> Stack b
forall a. Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop Stack b
stk Int
kx (Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
kx b
x) Int
m)
{-# INLINE fromAscListUpsert #-} -- Inline for list fusion

-- | \(O(n)\). Build a map from a list of key\/value pairs where
-- the keys are in ascending order and all distinct.
--
-- @fromDistinctAscList = 'fromAscList'@
--
-- See warning on 'fromAscList'.
--
-- This definition exists for backwards compatibility. It offers no advantage
-- over @fromAscList@.
fromDistinctAscList :: [(Key,a)] -> IntMap a
-- Note: There is nothing we can optimize compared to fromAscList.
-- The adjacent key equals check (kx == ky) might seem unnecessary for
-- fromDistinctAscList, but it guards branchMask which has undefined behavior
-- under that case. We could error on kx == ky instead, but that isn't any
-- better.
fromDistinctAscList :: forall a. [(Int, a)] -> IntMap a
fromDistinctAscList = [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
fromAscList
{-# INLINE fromDistinctAscList #-} -- Inline for list fusion

-- | \(O(n)\). Build a map from a list of key\/value pairs where
-- the keys are in descending order.
--
-- __Warning__: This function should be used only if the keys are in
-- non-increasing order. This precondition is not checked. Use 'fromList' if the
-- precondition may not hold.
--
-- > fromDescList [(5,"a"), (3,"b")]          == fromList [(3,"b"), (5,"a")]
-- > fromDescList [(5,"a"), (5,"b"), (3,"b")] == fromList [(3,"b"), (5,"b")]
--
-- @since 0.8.1
fromDescList :: [(Key,a)] -> IntMap a
fromDescList :: forall a. [(Int, a)] -> IntMap a
fromDescList [(Int, a)]
xs =
  MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
descLinkAll ((MonoState a -> (Int, a) -> MonoState a)
-> MonoState a -> [(Int, a)] -> MonoState 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' (\MonoState a
s (Int
ky, a
y) -> Int -> a -> MonoState a -> MonoState a
forall a. Int -> a -> MonoState a -> MonoState a
descInsert Int
ky a
y MonoState a
s) MonoState a
forall a. MonoState a
MSNada [(Int, a)]
xs)
{-# INLINE fromDescList #-} -- Inline for list fusion

-- | \(O(n)\). Build a map from a descending list in linear time with a
-- combining function for equal keys.
--
-- __Warning__: This function should be used only if the keys are in
-- non-increasing order. This precondition is not checked. Use 'fromListUpsert'
-- if the precondition may not hold.
--
-- > let f x = maybe [x] (x:)
-- > fromDescListUpsert f [(5,'a'), (5,'b'), (5,'c'), (3,'d'), (3,'e')] == fromList [(3,"ed"), (5,"cba")]
--
-- @since 0.8.1
fromDescListUpsert :: (a -> Maybe b -> b) -> [(Key, a)] -> IntMap b
fromDescListUpsert :: forall a b. (a -> Maybe b -> b) -> [(Int, a)] -> IntMap b
fromDescListUpsert a -> Maybe b -> b
f [(Int, a)]
xs = MonoState b -> IntMap b
forall a. MonoState a -> IntMap a
descLinkAll ((MonoState b -> (Int, a) -> MonoState b)
-> MonoState b -> [(Int, a)] -> MonoState b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' MonoState b -> (Int, a) -> MonoState b
next MonoState b
forall a. MonoState a
MSNada [(Int, a)]
xs)
  where
    next :: MonoState b -> (Int, a) -> MonoState b
next MonoState b
s (!Int
ky, a
y) = case MonoState b
s of
      MonoState b
MSNada -> Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) Stack b
forall a. Stack a
Nada
      MSPush Int
kx b
x Stack b
stk
        | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y (b -> Maybe b
forall a. a -> Maybe a
Just b
x)) Stack b
stk
        | Bool
otherwise ->
            let m :: Int
m = Int -> Int -> Int
branchMask Int
kx Int
ky
            in Int -> b -> Stack b -> MonoState b
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) (Int -> IntMap b -> Int -> Stack b -> Stack b
forall a. Int -> IntMap a -> Int -> Stack a -> Stack a
descLinkTop Int
kx (Int -> b -> IntMap b
forall a. Int -> a -> IntMap a
Tip Int
kx b
x) Int
m Stack b
stk)
{-# INLINE fromDescListUpsert #-} -- Inline for list fusion

data Stack a
  = Nada
  | Push {-# UNPACK #-} !Int !(IntMap a) !(Stack a)

data MonoState a
  = MSNada
  | MSPush {-# UNPACK #-} !Key a !(Stack a)

-- Insert an entry. The key must be >= the last inserted key. If it is equal
-- to the previous key, the previous value is replaced.
ascInsert :: MonoState a -> Int -> a -> MonoState a
ascInsert :: forall a. MonoState a -> Int -> a -> MonoState a
ascInsert MonoState a
s !Int
ky a
y = case MonoState a
s of
  MonoState a
MSNada -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y Stack a
forall a. Stack a
Nada
  MSPush Int
kx a
x Stack a
stk
    | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y Stack a
stk
    | Bool
otherwise -> let m :: Int
m = Int -> Int -> Int
branchMask Int
kx Int
ky
                   in Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y (Stack a -> Int -> IntMap a -> Int -> Stack a
forall a. Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop Stack a
stk Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) Int
m)
{-# INLINE ascInsert #-}

ascLinkTop :: Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop :: forall a. Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop Stack a
stk !Int
rk IntMap a
r !Int
rm = case Stack a
stk of
  Stack a
Nada -> Int -> IntMap a -> Stack a -> Stack a
forall a. Int -> IntMap a -> Stack a -> Stack a
Push Int
rm IntMap a
r Stack a
stk
  Push Int
m IntMap a
l Stack a
stk'
    | Int -> Word
i2w Int
m Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Word
i2w Int
rm -> let p :: Prefix
p = Int -> Int -> Prefix
mask Int
rk Int
m
                        in Stack a -> Int -> IntMap a -> Int -> Stack a
forall a. Stack a -> Int -> IntMap a -> Int -> Stack a
ascLinkTop Stack a
stk' Int
rk (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r) Int
rm
    | Bool
otherwise -> Int -> IntMap a -> Stack a -> Stack a
forall a. Int -> IntMap a -> Stack a -> Stack a
Push Int
rm IntMap a
r Stack a
stk

ascLinkAll :: MonoState a -> IntMap a
ascLinkAll :: forall a. MonoState a -> IntMap a
ascLinkAll MonoState a
s = case MonoState a
s of
  MonoState a
MSNada -> IntMap a
forall a. IntMap a
Nil
  MSPush Int
kx a
x Stack a
stk -> Stack a -> Int -> IntMap a -> IntMap a
forall a. Stack a -> Int -> IntMap a -> IntMap a
ascLinkStack Stack a
stk Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x)
{-# INLINABLE ascLinkAll #-}

ascLinkStack :: Stack a -> Int -> IntMap a -> IntMap a
ascLinkStack :: forall a. Stack a -> Int -> IntMap a -> IntMap a
ascLinkStack Stack a
stk !Int
rk IntMap a
r = case Stack a
stk of
  Stack a
Nada -> IntMap a
r
  Push Int
m IntMap a
l Stack a
stk'
    | Prefix -> Bool
signBranch Prefix
p -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
r IntMap a
l
    | Bool
otherwise -> Stack a -> Int -> IntMap a -> IntMap a
forall a. Stack a -> Int -> IntMap a -> IntMap a
ascLinkStack Stack a
stk' Int
rk (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r)
    where
      p :: Prefix
p = Int -> Int -> Prefix
mask Int
rk Int
m

-- Insert an entry. The key must be <= the last inserted key. If it is equal
-- to the previous key, the previous value is replaced.
descInsert :: Int -> a -> MonoState a -> MonoState a
descInsert :: forall a. Int -> a -> MonoState a -> MonoState a
descInsert !Int
ky a
y MonoState a
s = case MonoState a
s of
  MonoState a
MSNada -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y Stack a
forall a. Stack a
Nada
  MSPush Int
kx a
x Stack a
stk
    | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y Stack a
stk
    | Bool
otherwise -> let m :: Int
m = Int -> Int -> Int
branchMask Int
kx Int
ky
                   in Int -> a -> Stack a -> MonoState a
forall a. Int -> a -> Stack a -> MonoState a
MSPush Int
ky a
y (Int -> IntMap a -> Int -> Stack a -> Stack a
forall a. Int -> IntMap a -> Int -> Stack a -> Stack a
descLinkTop Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) Int
m Stack a
stk)
{-# INLINE descInsert #-}

descLinkTop :: Int -> IntMap a -> Int -> Stack a -> Stack a
descLinkTop :: forall a. Int -> IntMap a -> Int -> Stack a -> Stack a
descLinkTop !Int
lk IntMap a
l !Int
lm Stack a
stk = case Stack a
stk of
  Stack a
Nada -> Int -> IntMap a -> Stack a -> Stack a
forall a. Int -> IntMap a -> Stack a -> Stack a
Push Int
lm IntMap a
l Stack a
stk
  Push Int
m IntMap a
r Stack a
stk'
    | Int -> Word
i2w Int
m Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Word
i2w Int
lm -> let p :: Prefix
p = Int -> Int -> Prefix
mask Int
lk Int
m
                        in Int -> IntMap a -> Int -> Stack a -> Stack a
forall a. Int -> IntMap a -> Int -> Stack a -> Stack a
descLinkTop Int
lk (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r) Int
lm Stack a
stk'
    | Bool
otherwise -> Int -> IntMap a -> Stack a -> Stack a
forall a. Int -> IntMap a -> Stack a -> Stack a
Push Int
lm IntMap a
l Stack a
stk

descLinkAll :: MonoState a -> IntMap a
descLinkAll :: forall a. MonoState a -> IntMap a
descLinkAll MonoState a
s = case MonoState a
s of
  MonoState a
MSNada -> IntMap a
forall a. IntMap a
Nil
  MSPush Int
kx a
x Stack a
stk -> Int -> IntMap a -> Stack a -> IntMap a
forall a. Int -> IntMap a -> Stack a -> IntMap a
descLinkStack Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) Stack a
stk
{-# INLINABLE descLinkAll #-}

descLinkStack :: Int -> IntMap a -> Stack a -> IntMap a
descLinkStack :: forall a. Int -> IntMap a -> Stack a -> IntMap a
descLinkStack !Int
lk IntMap a
l Stack a
stk = case Stack a
stk of
  Stack a
Nada -> IntMap a
l
  Push Int
m IntMap a
r Stack a
stk'
    | Prefix -> Bool
signBranch Prefix
p -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
r IntMap a
l
    | Bool
otherwise -> Int -> IntMap a -> Stack a -> IntMap a
forall a. Int -> IntMap a -> Stack a -> IntMap a
descLinkStack Int
lk (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r) Stack a
stk'
    where
      p :: Prefix
p = Int -> Int -> Prefix
mask Int
lk Int
m

{--------------------------------------------------------------------
  IntMapBuilder
--------------------------------------------------------------------}

-- Note [IntMapBuilder]
-- ~~~~~~~~~~~~~~~~~~~~
-- IntMapBuilder serves as an accumulator for element-by-element construction
-- of an IntMap. It can be used in folds to construct IntMaps. This plays nicely
-- with list fusion when the structure folded over is a list, as in fromList and
-- friends.
--
-- An IntMapBuilder is either empty (BNil) or has the recently inserted Tip
-- together with a stack of trees (BTip). The structure is effectively a
-- [zipper](https://en.wikipedia.org/wiki/Zipper_(data_structure)). It always
-- has its "focus" at the last inserted entry. To insert a new entry, we need
-- to move the focus to the new entry. To do this we move up the stack to the
-- lowest common ancestor of the current position and the position of the
-- new key (implemented as moveUpB), then down to the position of the new key
-- (implemented as moveDownB).
--
-- When we are done inserting entries, we link the trees up the stack and get
-- the final result.
--
-- The advantage of this implementation is that we take the shortest path in
-- the tree from one key to the next. Unlike `insert`, we don't need to move
-- up to the root after every insertion. This is very beneficial when we have
-- runs of sorted keys, without many keys already in the tree in that range.
-- If the keys are fully sorted, inserting them all takes O(n) time instead
-- of O(n min(n,W)). But these benefits come at a small cost: when moving up
-- the tree we have to check at every point if it is time to move down. These
-- checks are absent in `insert`. So, in case we need to move up quite a lot,
-- repeated `insert` is slightly faster, but the trade-off is worthwhile since
-- such cases are pathological.

data IntMapBuilder a
  = BNil
  | BTip {-# UNPACK #-} !Int a !(BStack a)

-- BLeft: the IntMap is the left child
-- BRight: the IntMap is the right child
data BStack a
  = BNada
  | BLeft {-# UNPACK #-} !Prefix !(IntMap a) !(BStack a)
  | BRight {-# UNPACK #-} !Prefix !(IntMap a) !(BStack a)

-- Empty builder.
emptyB :: IntMapBuilder a
emptyB :: forall a. IntMapBuilder a
emptyB = IntMapBuilder a
forall a. IntMapBuilder a
BNil

-- Insert a key and value. Replaces the old value if one already exists for
-- the key.
insertB :: Key -> a -> IntMapBuilder a -> IntMapBuilder a
insertB :: forall a. Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertB !Int
ky a
y IntMapBuilder a
b = case IntMapBuilder a
b of
  IntMapBuilder a
BNil -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky a
y BStack a
forall a. BStack a
BNada
  BTip Int
kx a
x BStack a
stk -> case Int -> Int -> a -> BStack a -> MoveResult a
forall a. Int -> Int -> a -> BStack a -> MoveResult a
moveToB Int
ky Int
kx a
x BStack a
stk of
    MoveResult Maybe a
_ BStack a
stk' -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky a
y BStack a
stk'
{-# INLINE insertB #-}

-- Insert a key and value. The new value is combined with the old value if one
-- already exists for the key.
insertWithB :: (a -> a -> a) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB :: forall a.
(a -> a -> a) -> Int -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB a -> a -> a
f !Int
ky a
y IntMapBuilder a
b = case IntMapBuilder a
b of
  IntMapBuilder a
BNil -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky a
y BStack a
forall a. BStack a
BNada
  BTip Int
kx a
x BStack a
stk -> case Int -> Int -> a -> BStack a -> MoveResult a
forall a. Int -> Int -> a -> BStack a -> MoveResult a
moveToB Int
ky Int
kx a
x BStack a
stk of
    MoveResult Maybe a
m BStack a
stk' -> case Maybe a
m of
      Maybe a
Nothing -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky a
y BStack a
stk'
      Just a
x' -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky (a -> a -> a
f a
y a
x') BStack a
stk'
{-# INLINE insertWithB #-}

-- Upsert a key-value. The given function is used to generate the value based
-- on the existing value for the key.
upsertB :: (Maybe a -> a) -> Key -> IntMapBuilder a -> IntMapBuilder a
upsertB :: forall a.
(Maybe a -> a) -> Int -> IntMapBuilder a -> IntMapBuilder a
upsertB Maybe a -> a
f !Int
ky IntMapBuilder a
b = case IntMapBuilder a
b of
  IntMapBuilder a
BNil -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky (Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing) BStack a
forall a. BStack a
BNada
  BTip Int
kx a
x BStack a
stk -> case Int -> Int -> a -> BStack a -> MoveResult a
forall a. Int -> Int -> a -> BStack a -> MoveResult a
moveToB Int
ky Int
kx a
x BStack a
stk of
    MoveResult Maybe a
m BStack a
stk' -> Int -> a -> BStack a -> IntMapBuilder a
forall a. Int -> a -> BStack a -> IntMapBuilder a
BTip Int
ky (Maybe a -> a
f Maybe a
m) BStack a
stk'
{-# INLINE upsertB #-}

-- GHC >=9.6 supports unpacking sums, so we unpack the Maybe and avoid
-- allocating Justs. GHC optimizes the workers for moveUpB and moveDownB to
-- return (# (# (# #) | a #), BStack a #).
data MoveResult a
  = MoveResult
#if __GLASGOW_HASKELL__ >= 906
      {-# UNPACK #-}
#endif
      !(Maybe a)
      !(BStack a)

moveToB :: Key -> Key -> a -> BStack a -> MoveResult a
moveToB :: forall a. Int -> Int -> a -> BStack a -> MoveResult a
moveToB !Int
ky !Int
kx a
x !BStack a
stk
  | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky = Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult (a -> Maybe a
forall a. a -> Maybe a
Just a
x) BStack a
stk
  | Bool
otherwise = Int -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> Int -> IntMap a -> BStack a -> MoveResult a
moveUpB Int
ky Int
kx (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) BStack a
stk
-- Don't inline this; there is no benefit according to benchmarks.
{-# NOINLINE moveToB #-}

moveUpB :: Key -> Key -> IntMap a -> BStack a -> MoveResult a
moveUpB :: forall a. Int -> Int -> IntMap a -> BStack a -> MoveResult a
moveUpB !Int
ky !Int
kx !IntMap a
tx BStack a
stk = case BStack a
stk of
  BStack a
BNada -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult Maybe a
forall a. Maybe a
Nothing (Int -> Int -> IntMap a -> BStack a -> BStack a
forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky Int
kx IntMap a
tx BStack a
forall a. BStack a
BNada)
  BLeft Prefix
p IntMap a
l BStack a
stk'
    | Int -> Prefix -> Bool
nomatch Int
ky Prefix
p -> Int -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> Int -> IntMap a -> BStack a -> MoveResult a
moveUpB Int
ky Int
kx (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
tx) BStack a
stk'
    | Int -> Prefix -> Bool
left Int
ky Prefix
p -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> IntMap a -> BStack a -> MoveResult a
moveDownB Int
ky IntMap a
l (Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BRight Prefix
p IntMap a
tx BStack a
stk')
    | Bool
otherwise -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult Maybe a
forall a. Maybe a
Nothing (Int -> Int -> IntMap a -> BStack a -> BStack a
forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky Int
kx IntMap a
tx BStack a
stk)
  BRight Prefix
p IntMap a
r BStack a
stk'
    | Int -> Prefix -> Bool
nomatch Int
ky Prefix
p -> Int -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> Int -> IntMap a -> BStack a -> MoveResult a
moveUpB Int
ky Int
kx (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
tx IntMap a
r) BStack a
stk'
    | Int -> Prefix -> Bool
left Int
ky Prefix
p -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult Maybe a
forall a. Maybe a
Nothing (Int -> Int -> IntMap a -> BStack a -> BStack a
forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky Int
kx IntMap a
tx BStack a
stk)
    | Bool
otherwise -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> IntMap a -> BStack a -> MoveResult a
moveDownB Int
ky IntMap a
r (Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BLeft Prefix
p IntMap a
tx BStack a
stk')

moveDownB :: Key -> IntMap a -> BStack a -> MoveResult a
moveDownB :: forall a. Int -> IntMap a -> BStack a -> MoveResult a
moveDownB !Int
ky IntMap a
tx !BStack a
stk = case IntMap a
tx of
  Bin Prefix
p IntMap a
l IntMap a
r
    | Int -> Prefix -> Bool
nomatch Int
ky Prefix
p -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult Maybe a
forall a. Maybe a
Nothing (Int -> Int -> IntMap a -> BStack a -> BStack a
forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky (Prefix -> Int
unPrefix Prefix
p) IntMap a
tx BStack a
stk)
    | Int -> Prefix -> Bool
left Int
ky Prefix
p -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> IntMap a -> BStack a -> MoveResult a
moveDownB Int
ky IntMap a
l (Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BRight Prefix
p IntMap a
r BStack a
stk)
    | Bool
otherwise -> Int -> IntMap a -> BStack a -> MoveResult a
forall a. Int -> IntMap a -> BStack a -> MoveResult a
moveDownB Int
ky IntMap a
r (Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BLeft Prefix
p IntMap a
l BStack a
stk)
  Tip Int
kx a
x
    | Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult (a -> Maybe a
forall a. a -> Maybe a
Just a
x) BStack a
stk
    | Bool
otherwise -> Maybe a -> BStack a -> MoveResult a
forall a. Maybe a -> BStack a -> MoveResult a
MoveResult Maybe a
forall a. Maybe a
Nothing (Int -> Int -> IntMap a -> BStack a -> BStack a
forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky Int
kx IntMap a
tx BStack a
stk)
  IntMap a
Nil -> [Char] -> MoveResult a
forall a. HasCallStack => [Char] -> a
error [Char]
"moveDownB Tip"

linkB :: Key -> Key -> IntMap a -> BStack a -> BStack a
linkB :: forall a. Int -> Int -> IntMap a -> BStack a -> BStack a
linkB Int
ky Int
kx IntMap a
tx BStack a
stk
  | Int -> Word
i2w Int
ky Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Word
i2w Int
kx = Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BRight Prefix
p IntMap a
tx BStack a
stk
  | Bool
otherwise = Prefix -> IntMap a -> BStack a -> BStack a
forall a. Prefix -> IntMap a -> BStack a -> BStack a
BLeft Prefix
p IntMap a
tx BStack a
stk
  where
    p :: Prefix
p = Int -> Int -> Prefix
branchPrefix Int
ky Int
kx
{-# INLINE linkB #-}

-- Finalize the builder into a Map.
finishB :: IntMapBuilder a -> IntMap a
finishB :: forall a. IntMapBuilder a -> IntMap a
finishB IntMapBuilder a
b = case IntMapBuilder a
b of
  IntMapBuilder a
BNil -> IntMap a
forall a. IntMap a
Nil
  BTip Int
kx a
x BStack a
stk -> IntMap a -> BStack a -> IntMap a
forall a. IntMap a -> BStack a -> IntMap a
finishUpB (Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
kx a
x) BStack a
stk
{-# INLINABLE finishB #-}

finishUpB :: IntMap a -> BStack a -> IntMap a
finishUpB :: forall a. IntMap a -> BStack a -> IntMap a
finishUpB !IntMap a
t BStack a
stk = case BStack a
stk of
  BStack a
BNada -> IntMap a
t
  BLeft Prefix
p IntMap a
l BStack a
stk' -> IntMap a -> BStack a -> IntMap a
forall a. IntMap a -> BStack a -> IntMap a
finishUpB (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
t) BStack a
stk'
  BRight Prefix
p IntMap a
r BStack a
stk' -> IntMap a -> BStack a -> IntMap a
forall a. IntMap a -> BStack a -> IntMap a
finishUpB (Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
t IntMap a
r) BStack a
stk'

{--------------------------------------------------------------------
  Eq
--------------------------------------------------------------------}
instance Eq a => Eq (IntMap a) where
  == :: IntMap a -> IntMap a -> Bool
(==) = IntMap a -> IntMap a -> Bool
forall a. Eq a => IntMap a -> IntMap a -> Bool
equal

equal :: Eq a => IntMap a -> IntMap a -> Bool
equal :: forall a. Eq a => IntMap a -> IntMap a -> Bool
equal (Bin Prefix
p1 IntMap a
l1 IntMap a
r1) (Bin Prefix
p2 IntMap a
l2 IntMap a
r2)
  = (Prefix
p1 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p2) Bool -> Bool -> Bool
&& (IntMap a -> IntMap a -> Bool
forall a. Eq a => IntMap a -> IntMap a -> Bool
equal IntMap a
l1 IntMap a
l2) Bool -> Bool -> Bool
&& (IntMap a -> IntMap a -> Bool
forall a. Eq a => IntMap a -> IntMap a -> Bool
equal IntMap a
r1 IntMap a
r2)
equal (Tip Int
kx a
x) (Tip Int
ky a
y)
  = (Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky) Bool -> Bool -> Bool
&& (a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
y)
equal IntMap a
Nil IntMap a
Nil = Bool
True
equal IntMap a
_   IntMap a
_   = Bool
False
{-# INLINABLE equal #-}

-- | @since 0.5.9
instance Eq1 IntMap where
  liftEq :: forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
liftEq a -> b -> Bool
eq = IntMap a -> IntMap b -> Bool
go
    where
      go :: IntMap a -> IntMap b -> Bool
go (Bin Prefix
p1 IntMap a
l1 IntMap a
r1) (Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = Prefix
p1 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p2 Bool -> Bool -> Bool
&& IntMap a -> IntMap b -> Bool
go IntMap a
l1 IntMap b
l2 Bool -> Bool -> Bool
&& IntMap a -> IntMap b -> Bool
go IntMap a
r1 IntMap b
r2
      go (Tip Int
kx a
x) (Tip Int
ky b
y) = Int
kx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ky Bool -> Bool -> Bool
&& a -> b -> Bool
eq a
x b
y
      go IntMap a
Nil IntMap b
Nil = Bool
True
      go IntMap a
_   IntMap b
_   = Bool
False
  {-# INLINE liftEq #-}

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

instance Ord a => Ord (IntMap a) where
  compare :: IntMap a -> IntMap a -> Ordering
compare IntMap a
m1 IntMap a
m2 = (a -> a -> Ordering) -> IntMap a -> IntMap a -> Ordering
forall a b.
(a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
liftCmp a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare IntMap a
m1 IntMap a
m2
  {-# INLINABLE compare #-}

-- | @since 0.5.9
instance Ord1 IntMap where
  liftCompare :: forall a b.
(a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
liftCompare = (a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
forall a b.
(a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
liftCmp

liftCmp :: (a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
liftCmp :: forall a b.
(a -> b -> Ordering) -> IntMap a -> IntMap b -> Ordering
liftCmp a -> b -> Ordering
cmp IntMap a
m1 IntMap b
m2 = case (IntMap a -> (IntMap a, IntMap a)
forall a. IntMap a -> (IntMap a, IntMap a)
splitSign IntMap a
m1, IntMap b -> (IntMap b, IntMap b)
forall a. IntMap a -> (IntMap a, IntMap a)
splitSign IntMap b
m2) of
  ((IntMap a
l1, IntMap a
r1), (IntMap b
l2, IntMap b
r2)) -> case IntMap a -> IntMap b -> Order
go IntMap a
l1 IntMap b
l2 of
    Order
A_LT_B -> Ordering
LT
    Order
A_Prefix_B -> if IntMap a -> Bool
forall a. IntMap a -> Bool
null IntMap a
r1 then Ordering
LT else Ordering
GT
    Order
A_EQ_B -> case IntMap a -> IntMap b -> Order
go IntMap a
r1 IntMap b
r2 of
      Order
A_LT_B -> Ordering
LT
      Order
A_Prefix_B -> Ordering
LT
      Order
A_EQ_B -> Ordering
EQ
      Order
B_Prefix_A -> Ordering
GT
      Order
A_GT_B -> Ordering
GT
    Order
B_Prefix_A -> if IntMap b -> Bool
forall a. IntMap a -> Bool
null IntMap b
r2 then Ordering
GT else Ordering
LT
    Order
A_GT_B -> Ordering
GT
  where
    go :: IntMap a -> IntMap b -> Order
go t1 :: IntMap a
t1@(Bin Prefix
p1 IntMap a
l1 IntMap a
r1) t2 :: IntMap b
t2@(Bin Prefix
p2 IntMap b
l2 IntMap b
r2) = case Prefix -> Prefix -> TreeTreeBranch
treeTreeBranch Prefix
p1 Prefix
p2 of
      TreeTreeBranch
ABL -> case IntMap a -> IntMap b -> Order
go IntMap a
l1 IntMap b
t2 of
        Order
A_Prefix_B -> Order
A_GT_B
        Order
A_EQ_B -> Order
B_Prefix_A
        Order
o -> Order
o
      TreeTreeBranch
ABR -> Order
A_LT_B
      TreeTreeBranch
BAL -> case IntMap a -> IntMap b -> Order
go IntMap a
t1 IntMap b
l2 of
        Order
A_EQ_B -> Order
A_Prefix_B
        Order
B_Prefix_A -> Order
A_LT_B
        Order
o -> Order
o
      TreeTreeBranch
BAR -> Order
A_GT_B
      TreeTreeBranch
EQL -> case IntMap a -> IntMap b -> Order
go IntMap a
l1 IntMap b
l2 of
        Order
A_Prefix_B -> Order
A_GT_B
        Order
A_EQ_B -> IntMap a -> IntMap b -> Order
go IntMap a
r1 IntMap b
r2
        Order
B_Prefix_A -> Order
A_LT_B
        Order
o -> Order
o
      TreeTreeBranch
NOM -> if Prefix -> Int
unPrefix Prefix
p1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Prefix -> Int
unPrefix Prefix
p2 then Order
A_LT_B else Order
A_GT_B
    go (Bin Prefix
_ IntMap a
l1 IntMap a
_) (Tip Int
k2 b
x2) = case IntMap a -> KeyValue a
forall a. IntMap a -> KeyValue a
lookupMinSure IntMap a
l1 of
      KeyValue Int
k1 a
x1 -> case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
k1 Int
k2 Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> a -> b -> Ordering
cmp a
x1 b
x2 of
        Ordering
LT -> Order
A_LT_B
        Ordering
EQ -> Order
B_Prefix_A
        Ordering
GT -> Order
A_GT_B
    go (Tip Int
k1 a
x1) (Bin Prefix
_ IntMap b
l2 IntMap b
_) = case IntMap b -> KeyValue b
forall a. IntMap a -> KeyValue a
lookupMinSure IntMap b
l2 of
      KeyValue Int
k2 b
x2 -> case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
k1 Int
k2 Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> a -> b -> Ordering
cmp a
x1 b
x2 of
        Ordering
LT -> Order
A_LT_B
        Ordering
EQ -> Order
A_Prefix_B
        Ordering
GT -> Order
A_GT_B
    go (Tip Int
k1 a
x1) (Tip Int
k2 b
x2) = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
k1 Int
k2 Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> a -> b -> Ordering
cmp a
x1 b
x2 of
      Ordering
LT -> Order
A_LT_B
      Ordering
EQ -> Order
A_EQ_B
      Ordering
GT -> Order
A_GT_B
    go IntMap a
Nil IntMap b
Nil = Order
A_EQ_B
    go IntMap a
Nil IntMap b
_ = Order
A_Prefix_B
    go IntMap a
_ IntMap b
Nil = Order
B_Prefix_A
{-# INLINE liftCmp #-}

-- Split into negative and non-negative
splitSign :: IntMap a -> (IntMap a, IntMap a)
splitSign :: forall a. IntMap a -> (IntMap a, IntMap a)
splitSign t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Prefix -> Bool
signBranch Prefix
p = (IntMap a
r, IntMap a
l)
  | Prefix -> Int
unPrefix Prefix
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = (IntMap a
t, IntMap a
forall a. IntMap a
Nil)
  | Bool
otherwise = (IntMap a
forall a. IntMap a
Nil, IntMap a
t)
splitSign t :: IntMap a
t@(Tip Int
k a
_)
  | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = (IntMap a
t, IntMap a
forall a. IntMap a
Nil)
  | Bool
otherwise = (IntMap a
forall a. IntMap a
Nil, IntMap a
t)
splitSign IntMap a
Nil = (IntMap a
forall a. IntMap a
Nil, IntMap a
forall a. IntMap a
Nil)
{-# INLINE splitSign #-}

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

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

#ifdef __GLASGOW_HASKELL__
    a
a <$ :: forall a b. a -> IntMap b -> IntMap a
<$ Bin Prefix
p IntMap b
l IntMap b
r = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p (a
a a -> IntMap b -> IntMap a
forall a b. a -> IntMap b -> IntMap a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ IntMap b
l) (a
a a -> IntMap b -> IntMap a
forall a b. a -> IntMap b -> IntMap a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ IntMap b
r)
    a
a <$ Tip Int
k b
_   = Int -> a -> IntMap a
forall a. Int -> a -> IntMap a
Tip Int
k a
a
    a
_ <$ IntMap b
Nil       = IntMap a
forall a. IntMap a
Nil
#endif

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

instance Show a => Show (IntMap a) where
  showsPrec :: Int -> IntMap a -> [Char] -> [Char]
showsPrec Int
d IntMap 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
. [(Int, a)] -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows (IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
toList IntMap a
m)

-- | @since 0.5.9
instance Show1 IntMap where
    liftShowsPrec :: forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> IntMap a -> [Char] -> [Char]
liftShowsPrec Int -> a -> [Char] -> [Char]
sp [a] -> [Char] -> [Char]
sl Int
d IntMap a
m =
        (Int -> [(Int, a)] -> [Char] -> [Char])
-> [Char] -> Int -> [(Int, a)] -> [Char] -> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> [Char] -> Int -> a -> [Char] -> [Char]
showsUnaryWith ((Int -> (Int, a) -> [Char] -> [Char])
-> ([(Int, a)] -> [Char] -> [Char])
-> Int
-> [(Int, 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 -> (Int, a) -> [Char] -> [Char]
sp' [(Int, a)] -> [Char] -> [Char]
sl') [Char]
"fromList" Int
d (IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
toList IntMap a
m)
      where
        sp' :: Int -> (Int, a) -> [Char] -> [Char]
sp' = (Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> (Int, a) -> [Char] -> [Char]
forall a.
(Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> Int -> (Int, 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' :: [(Int, a)] -> [Char] -> [Char]
sl' = (Int -> a -> [Char] -> [Char])
-> ([a] -> [Char] -> [Char]) -> [(Int, 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]) -> [f a] -> [Char] -> [Char]
liftShowList Int -> a -> [Char] -> [Char]
sp [a] -> [Char] -> [Char]
sl

{--------------------------------------------------------------------
  Read
--------------------------------------------------------------------}
instance (Read e) => Read (IntMap e) where
#if defined(__GLASGOW_HASKELL__) || defined(__MHS__)
  readPrec :: ReadPrec (IntMap e)
readPrec = ReadPrec (IntMap e) -> ReadPrec (IntMap e)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (IntMap e) -> ReadPrec (IntMap e))
-> ReadPrec (IntMap e) -> ReadPrec (IntMap e)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (IntMap e) -> ReadPrec (IntMap e)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (IntMap e) -> ReadPrec (IntMap e))
-> ReadPrec (IntMap e) -> ReadPrec (IntMap e)
forall a b. (a -> b) -> a -> b
$ do
    Ident [Char]
"fromList" <- ReadPrec Lexeme
lexP
    [(Int, e)]
xs <- ReadPrec [(Int, e)]
forall a. Read a => ReadPrec a
readPrec
    IntMap e -> ReadPrec (IntMap e)
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Int, e)] -> IntMap e
forall a. [(Int, a)] -> IntMap a
fromList [(Int, e)]
xs)

  readListPrec :: ReadPrec [IntMap e]
readListPrec = ReadPrec [IntMap e]
forall a. Read a => ReadPrec [a]
readListPrecDefault
#else
  readsPrec p = readParen (p > 10) $ \ r -> do
    ("fromList",s) <- lex r
    (xs,t) <- reads s
    return (fromList xs,t)
#endif

-- | @since 0.5.9
instance Read1 IntMap where
    liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (IntMap a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = ([Char] -> ReadS (IntMap a)) -> Int -> ReadS (IntMap a)
forall a. ([Char] -> ReadS a) -> Int -> ReadS a
readsData (([Char] -> ReadS (IntMap a)) -> Int -> ReadS (IntMap a))
-> ([Char] -> ReadS (IntMap a)) -> Int -> ReadS (IntMap a)
forall a b. (a -> b) -> a -> b
$
        (Int -> ReadS [(Int, a)])
-> [Char] -> ([(Int, a)] -> IntMap a) -> [Char] -> ReadS (IntMap a)
forall a t.
(Int -> ReadS a) -> [Char] -> (a -> t) -> [Char] -> ReadS t
readsUnaryWith ((Int -> ReadS (Int, a))
-> ReadS [(Int, a)] -> Int -> ReadS [(Int, 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 (Int, a)
rp' ReadS [(Int, a)]
rl') [Char]
"fromList" [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
fromList
      where
        rp' :: Int -> ReadS (Int, a)
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Int, a)
forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Int, 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 [(Int, a)]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [(Int, a)]
forall a. (Int -> ReadS a) -> ReadS [a] -> ReadS [(Int, 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 two @IntMap@s. The maps must not be empty. The @Prefix@es of the two
-- maps must be different. @k1@ must share the prefix of @t1@. @p2@ must be the
-- prefix of @t2@.
linkKey :: Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey :: forall a. Int -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Int
k1 IntMap a
t1 Prefix
p2 IntMap a
t2 = Int -> IntMap a -> Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k1 IntMap a
t1 (Prefix -> Int
unPrefix Prefix
p2) IntMap a
t2
{-# INLINE linkKey #-}

-- | Link two @IntMap@s. The maps must not be empty. The @Prefix@es of the two
-- maps must be different. @k1@ must share the prefix of @t1@ and @k2@ must
-- share the prefix of @t2@.
link :: Int -> IntMap a -> Int -> IntMap a -> IntMap a
link :: forall a. Int -> IntMap a -> Int -> IntMap a -> IntMap a
link Int
k1 IntMap a
t1 Int
k2 IntMap a
t2
  | Int -> Word
i2w Int
k1 Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Word
i2w Int
k2 = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
t1 IntMap a
t2
  | Bool
otherwise = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
t2 IntMap a
t1
  where
    p :: Prefix
p = Int -> Int -> Prefix
branchPrefix Int
k1 Int
k2
{-# INLINE link #-}

{--------------------------------------------------------------------
  @bin@ assures that we never have empty trees within a tree.
--------------------------------------------------------------------}

bin :: Prefix -> IntMap a -> IntMap a -> IntMap a
bin :: forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
bin Prefix
_ IntMap a
l IntMap a
Nil = IntMap a
l
bin Prefix
_ IntMap a
Nil IntMap a
r = IntMap a
r
bin Prefix
p IntMap a
l IntMap a
r   = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r
{-# INLINE bin #-}

-- binCheckL only checks that the left subtree is non-empty
binCheckL :: Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL :: forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
_ IntMap a
Nil IntMap a
r = IntMap a
r
binCheckL Prefix
p IntMap a
l IntMap a
r = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r
{-# INLINE binCheckL #-}

-- binCheckR only checks that the right subtree is non-empty
binCheckR :: Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR :: forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckR Prefix
_ IntMap a
l IntMap a
Nil = IntMap a
l
binCheckR Prefix
p IntMap a
l IntMap a
r = Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p IntMap a
l IntMap a
r
{-# INLINE binCheckR #-}

{--------------------------------------------------------------------
  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 :: IntMap a -> [IntMap a]
splitRoot :: forall a. IntMap a -> [IntMap a]
splitRoot IntMap a
orig =
  case IntMap a
orig of
    IntMap a
Nil -> []
    x :: IntMap a
x@(Tip Int
_ a
_) -> [IntMap a
x]
    Bin Prefix
p IntMap a
l IntMap a
r
      | Prefix -> Bool
signBranch Prefix
p -> [IntMap a
r, IntMap a
l]
      | Bool
otherwise -> [IntMap a
l, IntMap 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 => IntMap a -> String
showTree :: forall a. Show a => IntMap a -> [Char]
showTree IntMap a
s
  = Bool -> Bool -> IntMap a -> [Char]
forall a. Show a => Bool -> Bool -> IntMap a -> [Char]
showTreeWith Bool
True Bool
False IntMap 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 -> IntMap a -> String
showTreeWith :: forall a. Show a => Bool -> Bool -> IntMap a -> [Char]
showTreeWith Bool
hang Bool
wide IntMap a
t
  | Bool
hang      = (Bool -> [[Char]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTreeHang Bool
wide [] IntMap a
t) [Char]
""
  | Bool
otherwise = (Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTree Bool
wide [] [] IntMap a
t) [Char]
""

showsTree :: Show a => Bool -> [String] -> [String] -> IntMap a -> ShowS
showsTree :: forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTree Bool
wide [[Char]]
lbars [[Char]]
rbars IntMap a
t = case IntMap a
t of
  Bin Prefix
p IntMap a
l IntMap a
r ->
    Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTree Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
rbars) ([[Char]] -> [[Char]]
withEmpty [[Char]]
rbars) IntMap 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 (Prefix -> [Char]
showBin Prefix
p) ([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]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTree Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
lbars) ([[Char]] -> [[Char]]
withBar [[Char]]
lbars) IntMap a
l
  Tip Int
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
. Int -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows Int
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"
  IntMap 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] -> IntMap a -> ShowS
showsTreeHang :: forall a.
Show a =>
Bool -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTreeHang Bool
wide [[Char]]
bars IntMap a
t = case IntMap a
t of
  Bin Prefix
p IntMap a
l IntMap 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 (Prefix -> [Char]
showBin Prefix
p) ([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]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withBar [[Char]]
bars) IntMap 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]] -> IntMap a -> [Char] -> [Char]
forall a.
Show a =>
Bool -> [[Char]] -> IntMap a -> [Char] -> [Char]
showsTreeHang Bool
wide ([[Char]] -> [[Char]]
withEmpty [[Char]]
bars) IntMap a
r
  Tip Int
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
. Int -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows Int
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"
  IntMap 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 -> String
showBin :: Prefix -> [Char]
showBin Prefix
_
  = [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

{--------------------------------------------------------------------
  Notes
--------------------------------------------------------------------}

-- Note [Okasaki-Gill]
-- ~~~~~~~~~~~~~~~~~~~
--
-- The IntMap structure is based on the map described in the paper "Fast
-- Mergeable Integer Maps" by Chris Okasaki and Andy Gill, with some
-- differences.
--
-- The paper spends most of its time describing a little-endian tree, where the
-- branching is done first on low bits then high bits. It then briefly describes
-- a big-endian tree. The implementation here is big-endian.
--
-- The definition of Okasaki and Gill's map would be written in Haskell as
--
-- data Dict a
--   = Empty
--   | Lf !Int a
--   | Br !Int !Int !(Dict a) !(Dict a)
--
-- Empty is the same as IntMap's Nil, and Lf is the same as Tip.
--
-- In Br, the first Int is the shared prefix and the second is the mask bit by
-- itself. For the big-endian map, the paper suggests that the prefix be the
-- common prefix, followed by a 0-bit, followed by all 1-bits. This is so that
-- the prefix value can be used as a point of split for binary search.
--
-- IntMap's Bin corresponds to Br, but is different because it has only one
-- Int (newtyped as Prefix). This describes both prefix and mask, so it is not
-- necessary to store them separately. This value is, in fact, one plus the
-- value suggested for the prefix in the paper. This representation is chosen
-- because it saves one word per Bin without detriment to the efficiency of
-- operations.
--
-- The implementation of operations such as lookup, insert, union, follow
-- the described implementations on Dict and split into the same cases. For
-- instance, for insert, the three cases on a Br are whether the key belongs
-- outside the map, or it belongs in the left child, or it belongs in the
-- right child. We have the same three cases for a Bin. However, the bitwise
-- operations we use to determine the case is naturally different due to the
-- difference in representation.

-- Note [IntMap merge complexity]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- The merge algorithm (used for union, intersection, etc.) is adopted from
-- Okasaki-Gill who give the complexity as O(m+n), where m and n are the sizes
-- of the two input maps. This is correct, since we visit all constructors in
-- both maps in the worst case, but we can try to find a tighter bound.
--
-- Consider that m<=n, i.e. m is the size of the smaller map and n is the size
-- of the larger. It does not matter which map is the first argument.
--
-- Now we have O(n) as one upper bound for our complexity, since O(n) is the
-- same as O(m+n) for m<=n.
--
-- Next, consider the smaller map. For this map, we will visit some
-- constructors, plus all the Bins of the larger map that lie in our way.
-- For the former, the worst case is that we visit all constructors, which is
-- O(m).
-- For the latter, the worst case is that we encounter Bins at every point
-- possible. This happens when for every key in the smaller map, the path to
-- that key's Tip in the larger map has a full length of W, with a Bin at every
-- bit position. To maximize the total number of Bins, the paths should be as
-- disjoint as possible. But even if the paths are spread out, at least O(m)
-- Bins are unavoidably shared, which extend up to a depth of lg(m) from the
-- root. Beyond this, the paths may be disjoint. This gives us a total of
-- O(m + m (W - lg m)) = O(m log (2^W / m)).
-- The number of Bins we encounter is also bounded by the total number of Bins,
-- which is n-1, but we already have O(n) as an upper bound.
--
-- Combining our bounds, we have the final complexity as
-- O(min(n, m log (2^W / m))).
--
-- Note that
-- * This is similar to the Map merge complexity, which is O(m log (n/m)).
-- * When m is a small constant the term simplifies to O(min(n, W)), which is
--   just the complexity we expect for single operations like insert and delete.

-- Note [fromAscList implementation]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- fromAscList is an implementation that builds up the result bottom-up
-- in linear time. It maintains a state (MonoState) that gets updated with
-- key-value pairs from the input list one at a time. The state contains the
-- last key-value pair, and a stack of pending trees.
--
-- For a new key-value pair, the branchMask with the previous key is computed.
-- This represents the depth of the lowest common ancestor that the tree with
-- the previous key, say tl, and the tree with the new key, tr, must have in
-- the final result. Since the keys are in ascending order we expect no more
-- keys in tl, and we can build it by moving up the stack and linking trees. We
-- know when to stop by the branchMask value. We must not link higher than that
-- depth, otherwise instead of tl we will build the parent of tl prematurely
-- before tr is ready. Once the linking is done, tl will be at the top of the
-- stack.
--
-- We also store the branchMask of a tree with its future right sibling in the
-- stack. This is an optimization, benchmarks show that this is faster than
-- recomputing the branchMask values when linking trees.
--
-- In the end, we link all the trees remaining in the stack. There is a small
-- catch: negative keys appear in the input before non-negative keys (if they
-- both appear), but the tree with negative keys and the tree with non-negative
-- keys must be the right and left child of the root respectively. So we check
-- for this and link them accordingly.
--
-- The implementation is defined as a foldl' over the input list, which makes
-- it a good consumer in list fusion.

-- Note [IntMap folds]
-- ~~~~~~~~~~~~~~~~~~~
-- Folds on IntMap are defined in a particular way for a few reasons.
--
-- foldl' :: (a -> b -> a) -> a -> IntMap b -> a
-- foldl' f z = \t ->
--   case t of
--     Nil -> z
--     Bin p l r
--       | signBranch p -> go (go z r) l
--       | otherwise -> go (go z l) r
--     _ -> go z t
--   where
--     go !_ Nil         = error "foldl'.go: Nil"
--     go z' (Tip _ x)   = f z' x
--     go z' (Bin _ l r) = go (go z' l) r
-- {-# INLINE foldl' #-}
--
-- 1. We first check if the Bin separates negative and positive keys, and fold
--    over the children accordingly. This check is not inside `go` because it
--    can only happen at the top level and we don't need to check every Bin.
-- 2. We also check for Nil at the top level instead of, say, `go z Nil = z`.
--    That's because `Nil` is also allowed only at the top-level, but more
--    importantly it allows for better optimizations if the `Nil` branch errors
--    in `go`. For example, if we have
--      maximum :: Ord a => IntMap a -> Maybe a
--      maximum = foldl' (\m x -> Just $! maybe x (max x) m) Nothing
--    because `go` certainly returns a `Just` (or errors), CPR analysis will
--    optimize it to return `(# a #)` instead of `Maybe a`. This makes it
--    satisfy the conditions for SpecConstr, which generates two specializations
--    of `go` for `Nothing` and `Just` inputs. Now both `Maybe`s have been
--    optimized out of `go`.
-- 3. The `Tip` is not matched on at the top-level to avoid using `f` more than
--    once. This allows `f` to be inlined into `go` even if `f` is big, since
--    it's likely to be the only place `f` is used, and not inlining `f` means
--    missing out on optimizations. See GHC #25259 for more on this.

-- Note [INLINABLE to expose unfoldings]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- We have many functions that could be inlined (i.e. not recursive at the top
-- level) but we mark a function with the INLINE pragma only if believe that
-- it will simplify after inlining and improve performance in most situations.
-- Otherwise, inlining just increases code size and compilation times. Fold
-- functions are good examples of functions we surely want to INLINE.
--
-- For the rest, depending on the function, e.g. if it closes over a
-- user-supplied function, it may improve performance to inline it in certain
-- situations. We want to allow the user to force inlining with GHC.Exts.inline
-- in such situations, so we mark the function as INLINABLE to make its
-- unfolding available in the interface file.
--
-- For reference see
-- https://downloads.haskell.org/ghc/9.14.1/docs/users_guide/exts/pragmas.html#inlinable-pragma.
--
-- Note that the user's ability to inline is limited to the body of the
-- function.
--
-- unionWith f = unionWithKey (\_k x y -> f x y)
-- {-# INLINABLE unionWith #-}
-- unionWithKey f = ...large rhs...
-- {-# INLINABLE unionWithKey #-}
--
-- Writing `GHC.Exts.inline unionWith` doesn't also inline the body of
-- unionWithKey. If the user wants that, they have to use unionWithKey instead.