{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
module Data.IntMap.Strict.Internal (
IntMap, Key
, empty
, singleton
, fromSet
, fromSetA
, fromSetMaybe
, fromSetMaybeA
, fromList
, fromListWith
, fromListWithKey
, fromListUpsert
, fromAscList
, fromAscListWith
, fromAscListWithKey
, fromAscListUpsert
, fromDistinctAscList
, fromDescList
, fromDescListUpsert
, insert
, insertWith
, insertWithKey
, insertLookupWithKey
, delete
, pop
, adjust
, adjustWithKey
, update
, updateWithKey
, upsert
, updateLookupWithKey
, alter
, alterF
, lookup
, (!?)
, (!)
, findWithDefault
, member
, notMember
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, null
, size
, compareSize
, union
, unionWith
, unionWithKey
, unions
, unionsWith
, difference
, (\\)
, differenceWith
, differenceWithKey
, intersection
, intersectionWith
, intersectionWithKey
, symmetricDifference
, disjoint
, compose
, mergeWithKey
, map
, mapWithKey
, traverseWithKey
, traverseMaybeWithKey
, mapAccum
, mapAccumWithKey
, mapAccumRWithKey
, mapKeys
, mapKeysWith
, mapKeysMonotonic
, foldr
, foldl
, foldrWithKey
, foldlWithKey
, foldMapWithKey
, foldr'
, foldl'
, foldrWithKey'
, foldlWithKey'
, elems
, keys
, assocs
, keysSet
, toList
, toAscList
, toDescList
, filter
, filterKeys
, filterWithKey
, restrictKeys
, withoutKeys
, partition
, partitionWithKey
, takeWhileAntitone
, dropWhileAntitone
, spanAntitone
, mapMaybe
, mapMaybeWithKey
, mapEither
, mapEitherWithKey
, split
, splitLookup
, splitRoot
, isSubmapOf, isSubmapOfBy
, isProperSubmapOf, isProperSubmapOfBy
, 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
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 :: 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
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
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
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)
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
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
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)
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
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
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)
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
alterF :: Functor f
=> (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
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 #-}
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 #-}
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 #-}
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"
{-# INLINABLE unionWithKey #-}
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 #-}
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
{-# INLINABLE differenceWithKey #-}
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 #-}
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"
{-# INLINABLE intersectionWithKey #-}
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 #-}
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
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
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)
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)
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
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__
{-# 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
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 #-}
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 #-}
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)
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
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)
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)
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)
{-# INLINABLE mapKeysWith #-}
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)
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
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
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)
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
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 #-}
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
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
fromDistinctAscList :: [(Key,a)] -> IntMap a
fromDistinctAscList :: forall a. [(Key, a)] -> IntMap a
fromDistinctAscList = [(Key, a)] -> IntMap a
forall a. [(Key, a)] -> IntMap a
fromAscList
{-# INLINE fromDistinctAscList #-}
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 #-}
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 #-}
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 #-}
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 #-}