{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.IntMap.Strict.Internal
-- Copyright   :  (c) Daan Leijen 2002
--                (c) Andriy Palamarchuk 2008
-- 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 (strict interface internals)
--
-- The @'IntMap' v@ type represents a finite map (sometimes called a dictionary)
-- from key 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>.
--
-----------------------------------------------------------------------------

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

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

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

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

    -- ** From Ordered Lists
    , fromAscList
    , fromAscListWith
    , fromAscListWithKey
    , fromAscListUpsert
    , fromDistinctAscList
    , fromDescList
    , fromDescListUpsert

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

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

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

    -- ** Size
    , null
    , size
    , compareSize

    -- * Combine

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

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

    -- ** Intersection
    , intersection
    , intersectionWith
    , intersectionWithKey

    -- ** Symmetric difference
    , symmetricDifference

    -- ** Disjoint
    , disjoint

    -- ** Compose
    , compose

    -- ** Universal combining function
    , mergeWithKey

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

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

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

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

    -- ** Lists
    , toList

-- ** Ordered lists
    , toAscList
    , toDescList

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

import Utils.Containers.Internal.Prelude hiding
  (lookup,map,filter,foldr,foldl,foldl',null)
import Prelude ()

import qualified Data.IntMap.Internal as L
import Data.IntSet.Internal.IntTreeCommons
  (Key, nomatch, left, signBranch, branchMask)
import Data.IntMap.Internal
  ( IntMap (..)
  , bin
  , binCheckL
  , binCheckR
  , link
  , linkKey
  , MonoState(..)
  , Stack(..)
  , ascLinkTop
  , ascLinkAll
  , descInsert
  , descLinkTop
  , descLinkAll
  , IntMapBuilder(..)
  , BStack(..)
  , emptyB
  , insertB
  , finishB
  , moveToB
  , MoveResult(..)
  , treeFromIntSetTip

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

#ifdef __GLASGOW_HASKELL__
import Data.Coerce
#endif

{--------------------------------------------------------------------
  Construction
--------------------------------------------------------------------}
-- | \(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. Key -> a -> IntMap a
singleton Key
k !a
x
  = Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x
{-# INLINE singleton #-}

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

insert :: Key -> a -> IntMap a -> IntMap a
insert :: forall a. Key -> a -> IntMap a -> IntMap a
insert !Key
k !a
x IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Key -> Prefix -> Bool
nomatch Key
k Prefix
p -> Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x) Prefix
p IntMap a
t
      | Key -> Prefix -> Bool
left Key
k Prefix
p    -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p (Key -> a -> IntMap a -> IntMap a
forall a. Key -> a -> IntMap a -> IntMap a
insert Key
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 (Key -> a -> IntMap a -> IntMap a
forall a. Key -> a -> IntMap a -> IntMap a
insert Key
k a
x IntMap a
r)
    Tip Key
ky a
_
      | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x
      | Bool
otherwise     -> Key -> IntMap a -> Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Key -> IntMap a -> IntMap a
link Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x) Key
ky IntMap a
t
    IntMap a
Nil -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x

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

insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWith :: forall a. (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWith a -> a -> a
f Key
k a
x IntMap a
t
  = (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
forall a. (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey (\Key
_ a
x' a
y' -> a -> a -> a
f a
x' a
y') Key
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"
--
-- If the key exists in the map, this function is lazy in @value@ but strict
-- in the result of @f@.
--
-- Also see the performance note on 'fromListWith'.

insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey :: forall a. (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey Key -> a -> a -> a
f !Key
k a
x IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Key -> Prefix -> Bool
nomatch Key
k Prefix
p -> Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
singleton Key
k a
x) Prefix
p IntMap a
t
      | Key -> Prefix -> Bool
left Key
k Prefix
p    -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p ((Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
forall a. (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey Key -> a -> a -> a
f Key
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 ((Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
forall a. (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey Key -> a -> a -> a
f Key
k a
x IntMap a
r)
    Tip Key
ky a
y
      | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Key -> a -> a -> a
f Key
k a
x a
y
      | Bool
otherwise     -> Key -> IntMap a -> Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Key -> IntMap a -> IntMap a
link Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
singleton Key
k a
x) Key
ky IntMap a
t
    IntMap a
Nil -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
singleton Key
k a
x

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

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


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

adjust ::  (a -> a) -> Key -> IntMap a -> IntMap a
adjust :: forall a. (a -> a) -> Key -> IntMap a -> IntMap a
adjust a -> a
f Key
k IntMap a
m
  = (Key -> a -> a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> a) -> Key -> IntMap a -> IntMap a
adjustWithKey (\Key
_ a
x -> a -> a
f a
x) Key
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. (Key -> a -> a) -> Key -> IntMap a -> IntMap a
adjustWithKey Key -> a -> a
f !Key
k IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Key -> Prefix -> Bool
nomatch Key
k Prefix
p -> IntMap a
t
      | Key -> Prefix -> Bool
left Key
k Prefix
p    -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
Bin Prefix
p ((Key -> a -> a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> a) -> Key -> IntMap a -> IntMap a
adjustWithKey Key -> a -> a
f Key
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 ((Key -> a -> a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> a) -> Key -> IntMap a -> IntMap a
adjustWithKey Key -> a -> a
f Key
k IntMap a
r)
    Tip Key
ky a
y
      | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
ky (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Key -> a -> a
f Key
k a
y
      | Bool
otherwise     -> IntMap a
t
    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) -> Key -> IntMap a -> IntMap a
update a -> Maybe a
f
  = (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey (\Key
_ a
x -> a -> Maybe a
f a
x)

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

updateWithKey ::  (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey :: forall a. (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey Key -> a -> Maybe a
f !Key
k IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Key -> Prefix -> Bool
nomatch Key
k Prefix
p -> IntMap a
t
      | Key -> Prefix -> Bool
left Key
k Prefix
p    -> Prefix -> IntMap a -> IntMap a -> IntMap a
forall a. Prefix -> IntMap a -> IntMap a -> IntMap a
binCheckL Prefix
p ((Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey Key -> a -> Maybe a
f Key
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 ((Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey Key -> a -> Maybe a
f Key
k IntMap a
r)
    Tip Key
ky a
y
      | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> case Key -> a -> Maybe a
f Key
k a
y of
                           Just !a
y' -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
ky a
y'
                           Maybe a
Nothing -> IntMap a
forall a. IntMap a
Nil
      | Bool
otherwise     -> IntMap a
t
    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) -> Key -> IntMap a -> IntMap a
upsert Maybe a -> a
f !Key
k t :: IntMap a
t@(Bin Prefix
p IntMap a
l IntMap a
r)
  | Key -> Prefix -> Bool
nomatch Key
k Prefix
p = Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing) Prefix
p IntMap a
t
  | Key -> Prefix -> Bool
left Key
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) -> Key -> IntMap a -> IntMap a
forall a. (Maybe a -> a) -> Key -> IntMap a -> IntMap a
upsert Maybe a -> a
f Key
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) -> Key -> IntMap a -> IntMap a
forall a. (Maybe a -> a) -> Key -> IntMap a -> IntMap a
upsert Maybe a -> a
f Key
k IntMap a
r)
upsert Maybe a -> a
f !Key
k t :: IntMap a
t@(Tip Key
ky a
y)
  | Key
k Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
ky = Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
ky (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Maybe a -> a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
y)
  | Bool
otherwise = Key -> IntMap a -> Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Key -> IntMap a -> IntMap a
link Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing) Key
ky IntMap a
t
upsert Maybe a -> a
f !Key
k IntMap a
Nil = Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Maybe a -> a
f Maybe a
forall a. Maybe a
Nothing

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

updateLookupWithKey ::  (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a)
updateLookupWithKey :: forall a.
(Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)
updateLookupWithKey Key -> a -> Maybe a
f0 !Key
k0 IntMap a
t0 = StrictPair (Maybe a) (IntMap a) -> (Maybe a, IntMap a)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair (Maybe a) (IntMap a) -> (Maybe a, IntMap a))
-> StrictPair (Maybe a) (IntMap a) -> (Maybe a, IntMap a)
forall a b. (a -> b) -> a -> b
$ (Key -> a -> Maybe a)
-> Key -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall {a}.
(Key -> a -> Maybe a)
-> Key -> IntMap a -> StrictPair (Maybe a) (IntMap a)
go Key -> a -> Maybe a
f0 Key
k0 IntMap a
t0
  where
    go :: (Key -> a -> Maybe a)
-> Key -> IntMap a -> StrictPair (Maybe a) (IntMap a)
go Key -> a -> Maybe a
f Key
k IntMap a
t =
      case IntMap a
t of
        Bin Prefix
p IntMap a
l IntMap a
r
          | Key -> Prefix -> Bool
nomatch Key
k Prefix
p -> (Maybe a
forall a. Maybe a
Nothing Maybe a -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t)
          | Key -> Prefix -> Bool
left Key
k Prefix
p    -> let (Maybe a
found :*: IntMap a
l') = (Key -> a -> Maybe a)
-> Key -> IntMap a -> StrictPair (Maybe a) (IntMap a)
go Key -> a -> Maybe a
f Key
k IntMap a
l in (Maybe a
found Maybe a -> IntMap a -> StrictPair (Maybe 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
l' IntMap a
r)
          | Bool
otherwise   -> let (Maybe a
found :*: IntMap a
r') = (Key -> a -> Maybe a)
-> Key -> IntMap a -> StrictPair (Maybe a) (IntMap a)
go Key -> a -> Maybe a
f Key
k IntMap a
r in (Maybe a
found Maybe a -> IntMap a -> StrictPair (Maybe 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
binCheckR Prefix
p IntMap a
l IntMap a
r')
        Tip Key
ky a
y
          | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> case Key -> a -> Maybe a
f Key
k a
y of
                               Just !a
y' -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y Maybe a -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
ky a
y')
                               Maybe a
Nothing  -> (a -> Maybe a
forall a. a -> Maybe a
Just a
y Maybe a -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)
          | Bool
otherwise     -> (Maybe a
forall a. Maybe a
Nothing Maybe a -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
t)
        IntMap a
Nil -> (Maybe a
forall a. Maybe a
Nothing Maybe a -> IntMap a -> StrictPair (Maybe a) (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: 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) -> Key -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f !Key
k IntMap a
t =
  case IntMap a
t of
    Bin Prefix
p IntMap a
l IntMap a
r
      | Key -> Prefix -> Bool
nomatch Key
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  -> Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Prefix -> IntMap a -> IntMap a
linkKey Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x) Prefix
p IntMap a
t
      | Key -> Prefix -> Bool
left Key
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) -> Key -> IntMap a -> IntMap a
forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f Key
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) -> Key -> IntMap a -> IntMap a
forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
alter Maybe a -> Maybe a
f Key
k IntMap a
r)
    Tip Key
ky a
y
      | Key
kKey -> Key -> Bool
forall a. Eq a => a -> a -> Bool
==Key
ky         -> case Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
y) of
                           Just !a
x -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
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 -> Key -> IntMap a -> Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> Key -> IntMap a -> IntMap a
link Key
k (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x) Key
ky IntMap a
t
                           Maybe a
Nothing -> IntMap a
t
    IntMap a
Nil               -> case Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing of
                           Just !a
x -> Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
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 modified from 'Control.Lens.At'.
alterF :: forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
alterF Maybe a -> f (Maybe a)
f Key
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 (Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> IntMap a
delete Key
k IntMap a
m)) Maybe a
mv
    Just !a
v' -> Key -> a -> IntMap a -> IntMap a
forall a. Key -> a -> IntMap a -> IntMap a
insert Key
k a
v' IntMap a
m
  where mv :: Maybe a
mv = Key -> IntMap a -> Maybe a
forall a. Key -> IntMap a -> Maybe a
lookup Key
k IntMap a
m
{-# INLINE alterF #-}

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

unionsWith :: Foldable f => (a->a->a) -> f (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 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 = (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
forall a. (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey (\Key
_ 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. (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey Key -> 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 Key
k1 a
x1) (Tip Key
_k2 a
x2) = Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k1 (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$! Key -> a -> a -> a
f Key
k1 a
x1 a
x2
    f' IntMap a
_ IntMap a
_ = [Char] -> IntMap a
forall a. HasCallStack => [Char] -> a
error [Char]
"not Tip"
-- See Note [INLINABLE to expose unfoldings] in Data.IntMap.Internal
{-# INLINABLE unionWithKey #-}

{--------------------------------------------------------------------
  Difference
--------------------------------------------------------------------}

-- | \(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 = (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
forall a b.
(Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey (\Key
_ 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.
(Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey Key -> a -> b -> Maybe a
f IntMap a
m1 IntMap b
m2
  = (Key -> a -> b -> Maybe a)
-> (IntMap a -> IntMap a)
-> (IntMap b -> IntMap a)
-> IntMap a
-> IntMap b
-> IntMap a
forall a b c.
(Key -> a -> b -> Maybe c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey Key -> 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
-- See Note [INLINABLE to expose unfoldings] in Data.IntMap.Internal
{-# INLINABLE differenceWithKey #-}

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

-- | \(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 = (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
forall a b c.
(Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWithKey (\Key
_ 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.
(Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
intersectionWithKey Key -> 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 Key
k1 a
x1) (Tip Key
_k2 b
x2) = Key -> c -> IntMap c
forall a. Key -> a -> IntMap a
Tip Key
k1 (c -> IntMap c) -> c -> IntMap c
forall a b. (a -> b) -> a -> b
$! Key -> a -> b -> c
f Key
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] in Data.IntMap.Internal
{-# INLINABLE intersectionWithKey #-}

{--------------------------------------------------------------------
  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@.

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.
(Key -> a -> b -> Maybe c)
-> (IntMap a -> IntMap c)
-> (IntMap b -> IntMap c)
-> IntMap a
-> IntMap b
-> IntMap c
mergeWithKey Key -> 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 Key
k1 a
x1) (Tip Key
_k2 b
x2) = case Key -> a -> b -> Maybe c
f Key
k1 a
x1 b
x2 of
          Maybe c
Nothing -> IntMap c
forall a. IntMap a
Nil
          Just !c
x -> Key -> c -> IntMap c
forall a. Key -> a -> IntMap a
Tip Key
k1 c
x
        combine IntMap a
_ IntMap b
_ = [Char] -> IntMap c
forall a. HasCallStack => [Char] -> a
error [Char]
"not Tip"
        {-# INLINE combine #-}
{-# INLINE mergeWithKey #-}

{--------------------------------------------------------------------
  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. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
updateMinWithKey Key -> 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 ((Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
go Key -> a -> Maybe a
f IntMap a
r)
            IntMap a
_ -> (Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
go Key -> a -> Maybe a
f IntMap a
t
  where
    go :: (Key -> t -> Maybe t) -> IntMap t -> IntMap t
go Key -> 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 ((Key -> t -> Maybe t) -> IntMap t -> IntMap t
go Key -> t -> Maybe t
f' IntMap t
l) IntMap t
r
    go Key -> t -> Maybe t
f' (Tip Key
k t
y) = case Key -> t -> Maybe t
f' Key
k t
y of
                        Just !t
y' -> Key -> t -> IntMap t
forall a. Key -> a -> IntMap a
Tip Key
k t
y'
                        Maybe t
Nothing -> IntMap t
forall a. IntMap a
Nil
    go Key -> 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. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
updateMaxWithKey Key -> 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 ((Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
go Key -> a -> Maybe a
f IntMap a
l) IntMap a
r
            IntMap a
_ -> (Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
go Key -> a -> Maybe a
f IntMap a
t
  where
    go :: (Key -> t -> Maybe t) -> IntMap t -> IntMap t
go Key -> 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 ((Key -> t -> Maybe t) -> IntMap t -> IntMap t
go Key -> t -> Maybe t
f' IntMap t
r)
    go Key -> t -> Maybe t
f' (Tip Key
k t
y) = case Key -> t -> Maybe t
f' Key
k t
y of
                        Just !t
y' -> Key -> t -> IntMap t
forall a. Key -> a -> IntMap a
Tip Key
k t
y'
                        Maybe t
Nothing -> IntMap t
forall a. IntMap a
Nil
    go Key -> t -> Maybe t
_ IntMap t
Nil = IntMap t
forall a. IntMap a
Nil

-- | \(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 = (Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
updateMaxWithKey ((a -> Maybe a) -> Key -> 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 = (Key -> a -> Maybe a) -> IntMap a -> IntMap a
forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
updateMinWithKey ((a -> Maybe a) -> Key -> a -> Maybe a
forall a b. a -> b -> a
const a -> Maybe a
f)


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

map :: (a -> b) -> 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 Key
k a
x)   = Key -> b -> IntMap b
forall a. Key -> a -> IntMap a
Tip Key
k (b -> IntMap b) -> b -> IntMap b
forall a b. (a -> b) -> a -> b
$! 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 (\x -> f $! g x) xs
"map/mapL" forall f g xs . map f (L.map g xs) = map (\x -> f (g x)) xs
 #-}
#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. (Key -> a -> b) -> IntMap a -> IntMap b
mapWithKey Key -> 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 ((Key -> a -> b) -> IntMap a -> IntMap b
forall a b. (Key -> a -> b) -> IntMap a -> IntMap b
mapWithKey Key -> a -> b
f IntMap a
l) ((Key -> a -> b) -> IntMap a -> IntMap b
forall a b. (Key -> a -> b) -> IntMap a -> IntMap b
mapWithKey Key -> a -> b
f IntMap a
r)
      Tip Key
k a
x   -> Key -> b -> IntMap b
forall a. Key -> a -> IntMap a
Tip Key
k (b -> IntMap b) -> b -> IntMap b
forall a b. (a -> b) -> a -> b
$! Key -> a -> b
f Key
k a
x
      IntMap a
Nil       -> IntMap b
forall a. IntMap a
Nil

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

-- | \(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 -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumWithKey (\a
a' Key
_ b
x -> a -> b -> (a, c)
f a
a' b
x)

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

mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
mapAccumWithKey :: forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumWithKey a -> Key -> b -> (a, c)
f a
a IntMap b
t
  = (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Key -> 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.  Strict in
-- the accumulating argument and the both elements of the
-- result of the function.
mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
mapAccumL :: forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumL a -> Key -> b -> (a, c)
f0 a
a0 IntMap b
t0 = StrictPair a (IntMap c) -> (a, IntMap c)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair a (IntMap c) -> (a, IntMap c))
-> StrictPair a (IntMap c) -> (a, IntMap c)
forall a b. (a -> b) -> a -> b
$ (a -> Key -> b -> (a, c))
-> a -> IntMap b -> StrictPair a (IntMap c)
forall {a} {a} {a}.
(a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> b -> (a, c)
f0 a
a0 IntMap b
t0
  where
    go :: (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
t
      = case IntMap a
t of
          Bin Prefix
p IntMap a
l IntMap a
r
            | Prefix -> Bool
signBranch Prefix
p ->
                let (a
a1 :*: IntMap a
r') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
r
                    (a
a2 :*: IntMap a
l') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a1 IntMap a
l
                in (a
a2 a -> IntMap a -> StrictPair 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
l' IntMap a
r')
            | Bool
otherwise ->
                let (a
a1 :*: IntMap a
l') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
l
                    (a
a2 :*: IntMap a
r') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a1 IntMap a
r
                in (a
a2 a -> IntMap a -> StrictPair 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
l' IntMap a
r')
          Tip Key
k a
x     -> let !(a
a',!a
x') = a -> Key -> a -> (a, a)
f a
a Key
k a
x in (a
a' a -> IntMap a -> StrictPair a (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x')
          IntMap a
Nil         -> (a
a a -> IntMap a -> StrictPair a (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
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 -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumRWithKey a -> Key -> b -> (a, c)
f0 a
a0 IntMap b
t0 = StrictPair a (IntMap c) -> (a, IntMap c)
forall a b. StrictPair a b -> (a, b)
toPair (StrictPair a (IntMap c) -> (a, IntMap c))
-> StrictPair a (IntMap c) -> (a, IntMap c)
forall a b. (a -> b) -> a -> b
$ (a -> Key -> b -> (a, c))
-> a -> IntMap b -> StrictPair a (IntMap c)
forall {a} {a} {a}.
(a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> b -> (a, c)
f0 a
a0 IntMap b
t0
  where
    go :: (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
t
      = case IntMap a
t of
          Bin Prefix
p IntMap a
l IntMap a
r
            | Prefix -> Bool
signBranch Prefix
p ->
              let (a
a1 :*: IntMap a
l') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
l
                  (a
a2 :*: IntMap a
r') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a1 IntMap a
r
              in (a
a2 a -> IntMap a -> StrictPair 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
l' IntMap a
r')
            | Bool
otherwise ->
              let (a
a1 :*: IntMap a
r') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a IntMap a
r
                  (a
a2 :*: IntMap a
l') = (a -> Key -> a -> (a, a))
-> a -> IntMap a -> StrictPair a (IntMap a)
go a -> Key -> a -> (a, a)
f a
a1 IntMap a
l
              in (a
a2 a -> IntMap a -> StrictPair 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
l' IntMap a
r')
          Tip Key
k a
x     -> let !(a
a',!a
x') = a -> Key -> a -> (a, a)
f a
a Key
k a
x in (a
a' a -> IntMap a -> StrictPair a (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
k a
x')
          IntMap a
Nil         -> (a
a a -> IntMap a -> StrictPair a (IntMap a)
forall a b. a -> b -> StrictPair a b
:*: IntMap a
forall a. IntMap a
Nil)

-- | \(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) -> (Key -> Key) -> IntMap a -> IntMap a
mapKeysWith a -> a -> a
c Key -> Key
f IntMap a
t =
  IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> Key -> a -> IntMapBuilder a)
-> IntMapBuilder a -> IntMap a -> IntMapBuilder a
forall a b. (a -> Key -> b -> a) -> a -> IntMap b -> a
foldlWithKey' (\IntMapBuilder a
b Key
kx a
x -> (a -> a -> a) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
forall a.
(a -> a -> a) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB a -> a -> a
c (Key -> Key
f Key
kx) a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB IntMap a
t)
-- See Note [INLINABLE to expose unfoldings] in Data.IntMap.Internal
{-# INLINABLE mapKeysWith #-}

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

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

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

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

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

-- | \(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. (Key -> a) -> IntSet -> IntMap a
fromSet =
  (((Key -> Identity a) -> IntSet -> Identity (IntMap a))
-> (Key -> a) -> IntSet -> IntMap a
forall {a}.
((Key -> Identity a) -> IntSet -> Identity (IntMap a))
-> (Key -> 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)
    (Key -> Identity a) -> IntSet -> Identity (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> 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 =>
(Key -> f a) -> IntSet -> f (IntMap a)
fromSetA Key -> 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 Key -> 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)) ((Key -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> f a) -> IntSet -> f (IntMap a)
fromSetA Key -> f a
f IntSet
r) ((Key -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> f a) -> IntSet -> f (IntMap a)
fromSetA Key -> 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) ((Key -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> f a) -> IntSet -> f (IntMap a)
fromSetA Key -> f a
f IntSet
l) ((Key -> f a) -> IntSet -> f (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> f a) -> IntSet -> f (IntMap a)
fromSetA Key -> f a
f IntSet
r)
fromSetA Key -> f a
f (IntSet.Tip Key
kx BitMap
bm) =
  (Key -> f (IntMap a))
-> (Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a))
-> Key
-> BitMap
-> f (IntMap a)
forall a.
(Key -> a) -> (Prefix -> a -> a -> a) -> Key -> BitMap -> a
treeFromIntSetTip
    (\Key
kx' -> (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
kx' (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$!) (a -> IntMap a) -> f a -> f (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> f a
f Key
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))
    Key
kx
    BitMap
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. (Key -> Maybe a) -> IntSet -> IntMap a
fromSetMaybe =
  (((Key -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a))
-> (Key -> Maybe a) -> IntSet -> IntMap a
forall {a}.
((Key -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a))
-> (Key -> 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)
    (Key -> Identity (Maybe a)) -> IntSet -> Identity (IntMap a)
forall (f :: * -> *) a.
Applicative f =>
(Key -> 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 =>
(Key -> f (Maybe a)) -> IntSet -> f (IntMap a)
fromSetMaybeA Key -> 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 Key
kx BitMap
bm) =
     (Key -> f (IntMap a))
-> (Prefix -> f (IntMap a) -> f (IntMap a) -> f (IntMap a))
-> Key
-> BitMap
-> f (IntMap a)
forall a.
(Key -> a) -> (Prefix -> a -> a -> a) -> Key -> BitMap -> a
treeFromIntSetTip
       (\Key
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 (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
kx' (a -> IntMap a) -> a -> IntMap a
forall a b. (a -> b) -> a -> b
$!) (Maybe a -> IntMap a) -> f (Maybe a) -> f (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> f (Maybe a)
f Key
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))
       Key
kx
       BitMap
bm
{-# INLINABLE fromSetMaybeA #-}

{--------------------------------------------------------------------
  Lists
--------------------------------------------------------------------}
-- | \(O(n \min(n,W))\). Create a map from a list of key\/value pairs.
--
-- 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. [(Key, a)] -> IntMap a
fromList [(Key, a)]
xs = IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> (Key, a) -> IntMapBuilder a)
-> IntMapBuilder a -> [(Key, 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 (Key
kx,!a
x) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
forall a. Key -> a -> IntMapBuilder a -> IntMapBuilder a
insertB Key
kx a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB [(Key, 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) -> [(Key, a)] -> IntMap a
fromListWith a -> a -> a
f [(Key, a)]
xs
  = (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
forall a. (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromListWithKey (\Key
_ a
x a
y -> a -> a -> a
f a
x a
y) [(Key, 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. (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromListWithKey Key -> a -> a -> a
f [(Key, a)]
xs =
  IntMapBuilder a -> IntMap a
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder a -> (Key, a) -> IntMapBuilder a)
-> IntMapBuilder a -> [(Key, 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 (Key
kx,a
x) -> (a -> a -> a) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
forall a.
(a -> a -> a) -> Key -> a -> IntMapBuilder a -> IntMapBuilder a
insertWithB (Key -> a -> a -> a
f Key
kx) Key
kx a
x IntMapBuilder a
b) IntMapBuilder a
forall a. IntMapBuilder a
emptyB [(Key, 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) -> [(Key, a)] -> IntMap b
fromListUpsert a -> Maybe b -> b
f [(Key, a)]
xs =
  IntMapBuilder b -> IntMap b
forall a. IntMapBuilder a -> IntMap a
finishB ((IntMapBuilder b -> (Key, a) -> IntMapBuilder b)
-> IntMapBuilder b -> [(Key, 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 (Key
kx, a
x) -> (Maybe b -> b) -> Key -> IntMapBuilder b -> IntMapBuilder b
forall a.
(Maybe a -> a) -> Key -> IntMapBuilder a -> IntMapBuilder a
upsertB (a -> Maybe b -> b
f a
x) Key
kx IntMapBuilder b
b) IntMapBuilder b
forall a. IntMapBuilder a
emptyB [(Key, 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. [(Key, a)] -> IntMap a
fromAscList [(Key, a)]
xs = (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
forall a. (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromAscListWithKey (\Key
_ a
x a
_ -> a
x) [(Key, 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) -> [(Key, a)] -> IntMap a
fromAscListWith a -> a -> a
f [(Key, a)]
xs = (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
forall a. (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromAscListWithKey (\Key
_ a
x a
y -> a -> a -> a
f a
x a
y) [(Key, 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] in Data.IntMap.Internal.
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
fromAscListWithKey :: forall a. (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromAscListWithKey Key -> a -> a -> a
f [(Key, a)]
xs = MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState a -> (Key, a) -> MonoState a)
-> MonoState a -> [(Key, 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 -> (Key, a) -> MonoState a
next MonoState a
forall a. MonoState a
MSNada [(Key, a)]
xs)
  where
    next :: MonoState a -> (Key, a) -> MonoState a
next MonoState a
s (!Key
ky, a
y) = case MonoState a
s of
      MonoState a
MSNada -> Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky a
y Stack a
forall a. Stack a
Nada
      MSPush Key
kx a
x Stack a
stk
        | Key
kx Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
ky -> Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky (Key -> a -> a -> a
f Key
ky a
y a
x) Stack a
stk
        | Bool
otherwise -> let m :: Key
m = Key -> Key -> Key
branchMask Key
kx Key
ky
                       in Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky a
y (Stack a -> Key -> IntMap a -> Key -> Stack a
forall a. Stack a -> Key -> IntMap a -> Key -> Stack a
ascLinkTop Stack a
stk Key
kx (Key -> a -> IntMap a
forall a. Key -> a -> IntMap a
Tip Key
kx a
x) Key
m)
    msPush' :: Key -> a -> Stack a -> MonoState a
msPush' Key
ky !a
y = Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
MSPush Key
ky a
y
{-# 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) -> [(Key, a)] -> IntMap b
fromAscListUpsert a -> Maybe b -> b
f [(Key, a)]
xs = MonoState b -> IntMap b
forall a. MonoState a -> IntMap a
ascLinkAll ((MonoState b -> (Key, a) -> MonoState b)
-> MonoState b -> [(Key, 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 -> (Key, a) -> MonoState b
next MonoState b
forall a. MonoState a
MSNada [(Key, a)]
xs)
  where
    next :: MonoState b -> (Key, a) -> MonoState b
next MonoState b
s (!Key
ky, a
y) = case MonoState b
s of
      MonoState b
MSNada -> Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) Stack b
forall a. Stack a
Nada
      MSPush Key
kx b
x Stack b
stk
        | Key
kx Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
ky -> Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
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 :: Key
m = Key -> Key -> Key
branchMask Key
kx Key
ky
            in Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) (Stack b -> Key -> IntMap b -> Key -> Stack b
forall a. Stack a -> Key -> IntMap a -> Key -> Stack a
ascLinkTop Stack b
stk Key
kx (Key -> b -> IntMap b
forall a. Key -> a -> IntMap a
Tip Key
kx b
x) Key
m)
    msPush' :: Key -> a -> Stack a -> MonoState a
msPush' Key
ky !a
y = Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
MSPush Key
ky a
y
{-# 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
-- See Note on Data.IntMap.Internal.fromDistinctAscList.
fromDistinctAscList :: forall a. [(Key, a)] -> IntMap a
fromDistinctAscList = [(Key, a)] -> IntMap a
forall a. [(Key, 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. [(Key, a)] -> IntMap a
fromDescList [(Key, a)]
xs =
  MonoState a -> IntMap a
forall a. MonoState a -> IntMap a
descLinkAll ((MonoState a -> (Key, a) -> MonoState a)
-> MonoState a -> [(Key, 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 (!Key
ky, !a
y) -> Key -> a -> MonoState a -> MonoState a
forall a. Key -> a -> MonoState a -> MonoState a
descInsert Key
ky a
y MonoState a
s) MonoState a
forall a. MonoState a
MSNada [(Key, 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) -> [(Key, a)] -> IntMap b
fromDescListUpsert a -> Maybe b -> b
f [(Key, a)]
xs = MonoState b -> IntMap b
forall a. MonoState a -> IntMap a
descLinkAll ((MonoState b -> (Key, a) -> MonoState b)
-> MonoState b -> [(Key, 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 -> (Key, a) -> MonoState b
next MonoState b
forall a. MonoState a
MSNada [(Key, a)]
xs)
  where
    next :: MonoState b -> (Key, a) -> MonoState b
next MonoState b
s (!Key
ky, a
y) = case MonoState b
s of
      MonoState b
MSNada -> Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) Stack b
forall a. Stack a
Nada
      MSPush Key
kx b
x Stack b
stk
        | Key
kx Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
ky -> Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
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 :: Key
m = Key -> Key -> Key
branchMask Key
kx Key
ky
            in Key -> b -> Stack b -> MonoState b
forall {a}. Key -> a -> Stack a -> MonoState a
msPush' Key
ky (a -> Maybe b -> b
f a
y Maybe b
forall a. Maybe a
Nothing) (Key -> IntMap b -> Key -> Stack b -> Stack b
forall a. Key -> IntMap a -> Key -> Stack a -> Stack a
descLinkTop Key
kx (Key -> b -> IntMap b
forall a. Key -> a -> IntMap a
Tip Key
kx b
x) Key
m Stack b
stk)
    msPush' :: Key -> a -> Stack a -> MonoState a
msPush' Key
ky !a
y = Key -> a -> Stack a -> MonoState a
forall {a}. Key -> a -> Stack a -> MonoState a
MSPush Key
ky a
y
{-# INLINE fromDescListUpsert #-} -- Inline for list fusion

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

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