{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Data.IntMap.NonEmpty (
NEIntMap
, Key
, pattern IsNonEmpty
, pattern IsEmpty
, nonEmptyMap
, toMap
, withNonEmpty
, insertMap
, insertMapWith
, insertMapWithKey
, insertMapMin
, insertMapMax
, unsafeFromMap
, singleton
, fromSet
, fromList
, fromListWith
, fromListWithKey
, fromAscList
, fromAscListWith
, fromAscListWithKey
, fromDistinctAscList
, insert
, insertWith
, insertWithKey
, insertLookupWithKey
, delete
, adjust
, adjustWithKey
, update
, updateWithKey
, updateLookupWithKey
, alter
, alterF
, alter'
, alterF'
, lookup
, (!?)
, (!)
, findWithDefault
, member
, notMember
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, size
, union
, unionWith
, unionWithKey
, unions
, unionsWith
, difference
, (\\)
, differenceWith
, differenceWithKey
, intersection
, intersectionWith
, intersectionWithKey
, map
, mapWithKey
, traverseWithKey1
, traverseWithKey
, mapAccum
, mapAccumWithKey
, mapAccumRWithKey
, mapKeys
, mapKeysWith
, mapKeysMonotonic
, foldr
, foldl
, foldr1
, foldl1
, foldrWithKey
, foldlWithKey
, foldMapWithKey
, foldr'
, foldr1'
, foldl'
, foldl1'
, foldrWithKey'
, foldlWithKey'
, elems
, keys
, assocs
, keysSet
, toList
, toAscList
, toDescList
, filter
, filterWithKey
, restrictKeys
, withoutKeys
, partition
, partitionWithKey
, mapMaybe
, mapMaybeWithKey
, mapEither
, mapEitherWithKey
, split
, splitLookup
, splitRoot
, isSubmapOf, isSubmapOfBy
, isProperSubmapOf, isProperSubmapOfBy
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, updateMin
, updateMax
, adjustMin
, adjustMax
, updateMinWithKey
, updateMaxWithKey
, adjustMinWithKey
, adjustMaxWithKey
, minView
, maxView
, valid
) where
import Control.Applicative
import Data.Bifunctor
import Data.Functor.Identity
import Data.IntMap.Internal (IntMap(..))
import Data.IntMap.NonEmpty.Internal
import Data.IntSet (IntSet)
import Data.IntSet.NonEmpty.Internal (NEIntSet(..))
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe hiding (mapMaybe)
import Data.Semigroup.Foldable (Foldable1)
import Data.These
import Prelude hiding (Foldable(..), map, filter, lookup)
import qualified Data.Foldable as F
import qualified Data.IntMap as M
import qualified Data.IntSet as S
import qualified Data.List.NonEmpty as NE
import qualified Data.Maybe as Maybe
import qualified Data.Semigroup.Foldable as F1
pattern IsNonEmpty :: NEIntMap a -> IntMap a
pattern $bIsNonEmpty :: forall a. NEIntMap a -> IntMap a
$mIsNonEmpty :: forall {r} {a}. IntMap a -> (NEIntMap a -> r) -> ((# #) -> r) -> r
IsNonEmpty n <- (nonEmptyMap->Just n)
where
IsNonEmpty NEIntMap a
n = forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n
pattern IsEmpty :: IntMap a
pattern $bIsEmpty :: forall a. IntMap a
$mIsEmpty :: forall {r} {a}. IntMap a -> ((# #) -> r) -> ((# #) -> r) -> r
IsEmpty <- (M.null->True)
where
IsEmpty = forall a. IntMap a
M.empty
{-# COMPLETE IsNonEmpty, IsEmpty #-}
unsafeFromMap
:: IntMap a
-> NEIntMap a
unsafeFromMap :: forall a. IntMap a -> NEIntMap a
unsafeFromMap = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty forall {a}. a
e forall a. a -> a
id
where
e :: a
e = forall a. [Char] -> a
errorWithoutStackTrace [Char]
"NEIntMap.unsafeFromMap: empty map"
{-# INLINE unsafeFromMap #-}
insertMap :: Key -> a -> IntMap a -> NEIntMap a
insertMap :: forall a. Key -> a -> IntMap a -> NEIntMap a
insertMap Key
k a
v = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) (forall a. Key -> a -> NEIntMap a -> NEIntMap a
insert Key
k a
v)
{-# INLINE insertMap #-}
insertMapWith
:: (a -> a -> a)
-> Key
-> a
-> IntMap a
-> NEIntMap a
insertMapWith :: forall a. (a -> a -> a) -> Key -> a -> IntMap a -> NEIntMap a
insertMapWith a -> a -> a
f Key
k a
v = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) (forall a. (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWith a -> a -> a
f Key
k a
v)
{-# INLINE insertMapWith #-}
insertMapWithKey
:: (Key -> a -> a -> a)
-> Key
-> a
-> IntMap a
-> NEIntMap a
insertMapWithKey :: forall a.
(Key -> a -> a -> a) -> Key -> a -> IntMap a -> NEIntMap a
insertMapWithKey Key -> a -> a -> a
f Key
k a
v = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) (forall a.
(Key -> a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWithKey Key -> a -> a -> a
f Key
k a
v)
{-# INLINE insertMapWithKey #-}
insertMapMin
:: Key
-> a
-> IntMap a
-> NEIntMap a
insertMapMin :: forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap
{-# INLINE insertMapMin #-}
insertMapMax
:: Key
-> a
-> IntMap a
-> NEIntMap a
insertMapMax :: forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMax Key
k a
v = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) NEIntMap a -> NEIntMap a
go
where
go :: NEIntMap a -> NEIntMap a
go (NEIntMap Key
k0 a
v0 IntMap a
m0) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Key -> a -> IntMap a -> IntMap a
insertMaxMap Key
k a
v forall a b. (a -> b) -> a -> b
$ IntMap a
m0
{-# INLINE insertMapMax #-}
fromSet
:: (Key -> a)
-> NEIntSet
-> NEIntMap a
fromSet :: forall a. (Key -> a) -> NEIntSet -> NEIntMap a
fromSet Key -> a
f (NEIntSet Key
k IntSet
ks) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Key -> a
f Key
k) (forall a. (Key -> a) -> IntSet -> IntMap a
M.fromSet Key -> a
f IntSet
ks)
{-# INLINE fromSet #-}
fromListWith
:: (a -> a -> a)
-> NonEmpty (Key, a)
-> NEIntMap a
fromListWith :: forall a. (a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromListWith a -> a -> a
f = forall a. (Key -> a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromListWithKey (forall a b. a -> b -> a
const a -> a -> a
f)
{-# INLINE fromListWith #-}
fromListWithKey
:: (Key -> a -> a -> a)
-> NonEmpty (Key, a)
-> NEIntMap a
fromListWithKey :: forall a. (Key -> a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromListWithKey Key -> a -> a -> a
f ((Key
k0, a
v0) :| [(Key, a)]
xs) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' NEIntMap a -> (Key, a) -> NEIntMap a
go (forall a. Key -> a -> NEIntMap a
singleton Key
k0 a
v0) [(Key, a)]
xs
where
go :: NEIntMap a -> (Key, a) -> NEIntMap a
go NEIntMap a
m (Key
k, a
v) = forall a.
(Key -> a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWithKey Key -> a -> a -> a
f Key
k a
v NEIntMap a
m
{-# INLINE go #-}
{-# INLINE fromListWithKey #-}
fromAscList
:: NonEmpty (Key, a)
-> NEIntMap a
fromAscList :: forall a. NonEmpty (Key, a) -> NEIntMap a
fromAscList = forall a. NonEmpty (Key, a) -> NEIntMap a
fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. NonEmpty (Key, b) -> NonEmpty (Key, b)
combineEq
{-# INLINE fromAscList #-}
fromAscListWith
:: (a -> a -> a)
-> NonEmpty (Key, a)
-> NEIntMap a
fromAscListWith :: forall a. (a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromAscListWith a -> a -> a
f = forall a. (Key -> a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromAscListWithKey (forall a b. a -> b -> a
const a -> a -> a
f)
{-# INLINE fromAscListWith #-}
fromAscListWithKey
:: (Key -> a -> a -> a)
-> NonEmpty (Key, a)
-> NEIntMap a
fromAscListWithKey :: forall a. (Key -> a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromAscListWithKey Key -> a -> a -> a
f = forall a. NonEmpty (Key, a) -> NEIntMap a
fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b.
(Key -> b -> b -> b) -> NonEmpty (Key, b) -> NonEmpty (Key, b)
combineEqWith Key -> a -> a -> a
f
{-# INLINE fromAscListWithKey #-}
fromDistinctAscList :: NonEmpty (Key, a) -> NEIntMap a
fromDistinctAscList :: forall a. NonEmpty (Key, a) -> NEIntMap a
fromDistinctAscList ((Key
k, a
v) :| [(Key, a)]
xs) = forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k a
v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [(Key, a)] -> IntMap a
M.fromDistinctAscList
forall a b. (a -> b) -> a -> b
$ [(Key, a)]
xs
{-# INLINE fromDistinctAscList #-}
insert
:: Key
-> a
-> NEIntMap a
-> NEIntMap a
insert :: forall a. Key -> a -> NEIntMap a -> NEIntMap a
insert Key
k a
v n :: NEIntMap a
n@(NEIntMap Key
k0 a
v0 IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Key -> a -> IntMap a -> IntMap a
M.insert Key
k a
v forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE insert #-}
insertWithKey
:: (Key -> a -> a -> a)
-> Key
-> a
-> NEIntMap a
-> NEIntMap a
insertWithKey :: forall a.
(Key -> a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWithKey Key -> a -> a -> a
f Key
k a
v n :: NEIntMap a
n@(NEIntMap Key
k0 a
v0 IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Key -> a -> a -> a
f Key
k a
v a
v0) IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v0 forall a b. (a -> b) -> a -> b
$ forall a. (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
M.insertWithKey Key -> a -> a -> a
f Key
k a
v IntMap a
m
{-# INLINE insertWithKey #-}
insertLookupWithKey
:: (Key -> a -> a -> a)
-> Key
-> a
-> NEIntMap a
-> (Maybe a, NEIntMap a)
insertLookupWithKey :: forall a.
(Key -> a -> a -> a)
-> Key -> a -> NEIntMap a -> (Maybe a, NEIntMap a)
insertLookupWithKey Key -> a -> a -> a
f Key
k a
v n :: NEIntMap a
n@(NEIntMap Key
k0 a
v0 IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> (forall a. Maybe a
Nothing, forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n )
Ordering
EQ -> (forall a. a -> Maybe a
Just a
v , forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Key -> a -> a -> a
f Key
k a
v a
v0) IntMap a
m )
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a.
(Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
M.insertLookupWithKey Key -> a -> a -> a
f Key
k a
v IntMap a
m
{-# INLINE insertLookupWithKey #-}
delete :: Key -> NEIntMap a -> IntMap a
delete :: forall a. Key -> NEIntMap a -> IntMap a
delete Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n
Ordering
EQ -> IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0 a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Key -> IntMap a -> IntMap a
M.delete Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE delete #-}
adjust
:: (a -> a)
-> Key
-> NEIntMap a
-> NEIntMap a
adjust :: forall a. (a -> a) -> Key -> NEIntMap a -> NEIntMap a
adjust a -> a
f = forall a. (Key -> a -> a) -> Key -> NEIntMap a -> NEIntMap a
adjustWithKey (forall a b. a -> b -> a
const a -> a
f)
{-# INLINE adjust #-}
adjustWithKey
:: (Key -> a -> a)
-> Key
-> NEIntMap a
-> NEIntMap a
adjustWithKey :: forall a. (Key -> a -> a) -> Key -> NEIntMap a -> NEIntMap a
adjustWithKey Key -> a -> a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> NEIntMap a
n
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 (Key -> a -> a
f Key
k0 a
v) IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> a) -> Key -> IntMap a -> IntMap a
M.adjustWithKey Key -> a -> a
f Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE adjustWithKey #-}
update
:: (a -> Maybe a)
-> Key
-> NEIntMap a
-> IntMap a
update :: forall a. (a -> Maybe a) -> Key -> NEIntMap a -> IntMap a
update a -> Maybe a
f = forall a. (Key -> a -> Maybe a) -> Key -> NEIntMap a -> IntMap a
updateWithKey (forall a b. a -> b -> a
const a -> Maybe a
f)
{-# INLINE update #-}
updateWithKey
:: (Key -> a -> Maybe a)
-> Key
-> NEIntMap a
-> IntMap a
updateWithKey :: forall a. (Key -> a -> Maybe a) -> Key -> NEIntMap a -> IntMap a
updateWithKey Key -> a -> Maybe a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n
Ordering
EQ -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
m (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0) IntMap a
m) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> a -> Maybe a
f Key
k0 forall a b. (a -> b) -> a -> b
$ a
v
Ordering
GT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0 a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
M.updateWithKey Key -> a -> Maybe a
f Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE updateWithKey #-}
updateLookupWithKey
:: (Key -> a -> Maybe a)
-> Key
-> NEIntMap a
-> (Maybe a, IntMap a)
updateLookupWithKey :: forall a.
(Key -> a -> Maybe a) -> Key -> NEIntMap a -> (Maybe a, IntMap a)
updateLookupWithKey Key -> a -> Maybe a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> (forall a. Maybe a
Nothing, forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n)
Ordering
EQ -> let u :: Maybe a
u = Key -> a -> Maybe a
f Key
k0 a
v
in (forall a. a -> Maybe a
Just a
v, forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
m (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0) IntMap a
m) Maybe a
u)
Ordering
GT -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0 a
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
(Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)
M.updateLookupWithKey Key -> a -> Maybe a
f Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE updateLookupWithKey #-}
alter
:: (Maybe a -> Maybe a)
-> Key
-> NEIntMap a
-> IntMap a
alter :: forall a. (Maybe a -> Maybe a) -> Key -> NEIntMap a -> IntMap a
alter Maybe a -> Maybe a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> (forall a b. (a -> b) -> a -> b
$ forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k ) forall a b. (a -> b) -> a -> b
$ Maybe a -> Maybe a
f forall a. Maybe a
Nothing
Ordering
EQ -> (forall a b. (a -> b) -> a -> b
$ IntMap a
m ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0) forall a b. (a -> b) -> a -> b
$ Maybe a -> Maybe a
f (forall a. a -> Maybe a
Just a
v)
Ordering
GT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0 a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
M.alter Maybe a -> Maybe a
f Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE alter #-}
alterF
:: Functor f
=> (Maybe a -> f (Maybe a))
-> Key
-> NEIntMap a
-> f (IntMap a)
alterF :: forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f (Maybe a)) -> Key -> NEIntMap a -> f (IntMap a)
alterF Maybe a -> f (Maybe a)
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> (forall a b. (a -> b) -> a -> b
$ forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k ) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f (Maybe a)
f forall a. Maybe a
Nothing
Ordering
EQ -> (forall a b. (a -> b) -> a -> b
$ IntMap a
m ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f (Maybe a)
f (forall a. a -> Maybe a
Just a
v)
Ordering
GT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k0 a
v forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
M.alterF Maybe a -> f (Maybe a)
f Key
k IntMap a
m
{-# INLINABLE [2] alterF #-}
{-# RULES
"alterF/Const" forall k (f :: Maybe a -> Const b (Maybe a)) . alterF f k = \m -> Const . getConst . f $ lookup k m
#-}
{-# RULES
"alterF/Identity" forall k (f :: Maybe a -> Identity (Maybe a)) . alterF f k = Identity . alter (runIdentity . f) k
#-}
alter'
:: (Maybe a -> a)
-> Key
-> NEIntMap a
-> NEIntMap a
alter' :: forall a. (Maybe a -> a) -> Key -> NEIntMap a -> NEIntMap a
alter' Maybe a -> a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Maybe a -> a
f forall a. Maybe a
Nothing) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 (Maybe a -> a
f (forall a. a -> Maybe a
Just a
v)) forall a b. (a -> b) -> a -> b
$ IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
M.alter (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> a
f) Key
k forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE alter' #-}
alterF'
:: Functor f
=> (Maybe a -> f a)
-> Key
-> NEIntMap a
-> f (NEIntMap a)
alterF' :: forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f a) -> Key -> NEIntMap a -> f (NEIntMap a)
alterF' Maybe a -> f a
f Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k ) (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f a
f forall a. Maybe a
Nothing
Ordering
EQ -> forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0) IntMap a
m forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a -> f a
f (forall a. a -> Maybe a
Just a
v)
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a.
Functor f =>
(Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
M.alterF (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> f a
f) Key
k IntMap a
m
{-# INLINABLE [2] alterF' #-}
{-# RULES
"alterF'/Const" forall k (f :: Maybe a -> Const b a) . alterF' f k = \m -> Const . getConst . f $ lookup k m
#-}
{-# RULES
"alterF'/Identity" forall k (f :: Maybe a -> Identity a) . alterF' f k = Identity . insertWith (\_ -> runIdentity . f . Just) k (runIdentity (f Nothing))
#-}
lookup
:: Key
-> NEIntMap a
-> Maybe a
lookup :: forall a. Key -> NEIntMap a -> Maybe a
lookup Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. a -> Maybe a
Just a
v
Ordering
GT -> forall a. Key -> IntMap a -> Maybe a
M.lookup Key
k IntMap a
m
{-# INLINE lookup #-}
(!?) :: NEIntMap a -> Key -> Maybe a
!? :: forall a. NEIntMap a -> Key -> Maybe a
(!?) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Key -> NEIntMap a -> Maybe a
lookup
{-# INLINE (!?) #-}
(!) :: NEIntMap a -> Key -> a
! :: forall a. NEIntMap a -> Key -> a
(!) NEIntMap a
m Key
k = forall a. a -> Maybe a -> a
fromMaybe forall {a}. a
e forall a b. (a -> b) -> a -> b
$ NEIntMap a
m forall a. NEIntMap a -> Key -> Maybe a
!? Key
k
where
e :: a
e = forall a. HasCallStack => [Char] -> a
error [Char]
"NEIntMap.!: given key is not an element in the map"
{-# INLINE (!) #-}
infixl 9 !?
infixl 9 !
findWithDefault
:: a
-> Key
-> NEIntMap a
-> a
findWithDefault :: forall a. a -> Key -> NEIntMap a -> a
findWithDefault a
def Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> a
def
Ordering
EQ -> a
v
Ordering
GT -> forall a. a -> Key -> IntMap a -> a
M.findWithDefault a
def Key
k IntMap a
m
{-# INLINE findWithDefault #-}
member :: Key -> NEIntMap a -> Bool
member :: forall a. Key -> NEIntMap a -> Bool
member Key
k (NEIntMap Key
k0 a
_ IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> Bool
False
Ordering
EQ -> Bool
True
Ordering
GT -> forall a. Key -> IntMap a -> Bool
M.member Key
k IntMap a
m
{-# INLINE member #-}
notMember :: Key -> NEIntMap a -> Bool
notMember :: forall a. Key -> NEIntMap a -> Bool
notMember Key
k (NEIntMap Key
k0 a
_ IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> Bool
True
Ordering
EQ -> Bool
False
Ordering
GT -> forall a. Key -> IntMap a -> Bool
M.notMember Key
k IntMap a
m
{-# INLINE notMember #-}
lookupLT :: Key -> NEIntMap a -> Maybe (Key, a)
lookupLT :: forall a. Key -> NEIntMap a -> Maybe (Key, a)
lookupLT Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. Maybe a
Nothing
Ordering
GT -> forall a. Key -> IntMap a -> Maybe (Key, a)
M.lookupLT Key
k IntMap a
m forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just (Key
k0, a
v)
{-# INLINE lookupLT #-}
lookupGT :: Key -> NEIntMap a -> Maybe (Key, a)
lookupGT :: forall a. Key -> NEIntMap a -> Maybe (Key, a)
lookupGT Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. a -> Maybe a
Just (Key
k0, a
v)
Ordering
EQ -> forall a. IntMap a -> Maybe (Key, a)
lookupMinMap IntMap a
m
Ordering
GT -> forall a. Key -> IntMap a -> Maybe (Key, a)
M.lookupGT Key
k IntMap a
m
{-# INLINE lookupGT #-}
lookupLE :: Key -> NEIntMap a -> Maybe (Key, a)
lookupLE :: forall a. Key -> NEIntMap a -> Maybe (Key, a)
lookupLE Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. a -> Maybe a
Just (Key
k0, a
v)
Ordering
GT -> forall a. Key -> IntMap a -> Maybe (Key, a)
M.lookupLE Key
k IntMap a
m forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just (Key
k0, a
v)
{-# INLINE lookupLE #-}
lookupGE :: Key -> NEIntMap a -> Maybe (Key, a)
lookupGE :: forall a. Key -> NEIntMap a -> Maybe (Key, a)
lookupGE Key
k (NEIntMap Key
k0 a
v IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. a -> Maybe a
Just (Key
k0, a
v)
Ordering
EQ -> forall a. a -> Maybe a
Just (Key
k0, a
v)
Ordering
GT -> forall a. Key -> IntMap a -> Maybe (Key, a)
M.lookupGE Key
k IntMap a
m
{-# INLINE lookupGE #-}
unionWith
:: (a -> a -> a)
-> NEIntMap a
-> NEIntMap a
-> NEIntMap a
unionWith :: forall a. (a -> a -> a) -> NEIntMap a -> NEIntMap a -> NEIntMap a
unionWith a -> a -> a
f n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap a
n2@(NEIntMap Key
k2 a
v2 IntMap a
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 a
v1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWith a -> a -> a
f IntMap a
m1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n2
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 (a -> a -> a
f a
v1 a
v2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWith a -> a -> a
f IntMap a
m1 forall a b. (a -> b) -> a -> b
$ IntMap a
m2
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k2 a
v2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWith a -> a -> a
f (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1) forall a b. (a -> b) -> a -> b
$ IntMap a
m2
{-# INLINE unionWith #-}
unionWithKey
:: (Key -> a -> a -> a)
-> NEIntMap a
-> NEIntMap a
-> NEIntMap a
unionWithKey :: forall a.
(Key -> a -> a -> a) -> NEIntMap a -> NEIntMap a -> NEIntMap a
unionWithKey Key -> a -> a -> a
f n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap a
n2@(NEIntMap Key
k2 a
v2 IntMap a
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 a
v1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWithKey Key -> a -> a -> a
f IntMap a
m1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n2
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 (Key -> a -> a -> a
f Key
k1 a
v1 a
v2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWithKey Key -> a -> a -> a
f IntMap a
m1 forall a b. (a -> b) -> a -> b
$ IntMap a
m2
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k2 a
v2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
M.unionWithKey Key -> a -> a -> a
f (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1) forall a b. (a -> b) -> a -> b
$ IntMap a
m2
{-# INLINE unionWithKey #-}
unionsWith
:: Foldable1 f
=> (a -> a -> a)
-> f (NEIntMap a)
-> NEIntMap a
unionsWith :: forall (f :: * -> *) a.
Foldable1 f =>
(a -> a -> a) -> f (NEIntMap a) -> NEIntMap a
unionsWith a -> a -> a
f (forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
F1.toNonEmpty->(NEIntMap a
m :| [NEIntMap a]
ms)) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (forall a. (a -> a -> a) -> NEIntMap a -> NEIntMap a -> NEIntMap a
unionWith a -> a -> a
f) NEIntMap a
m [NEIntMap a]
ms
{-# INLINE unionsWith #-}
difference
:: NEIntMap a
-> NEIntMap b
-> IntMap a
difference :: forall a b. NEIntMap a -> NEIntMap b -> IntMap a
difference n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap b
n2@(NEIntMap Key
k2 b
_ IntMap b
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k1 a
v1 forall a b. (a -> b) -> a -> b
$ IntMap a
m1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.difference` forall a. NEIntMap a -> IntMap a
toMap NEIntMap b
n2
Ordering
EQ -> IntMap a
m1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.difference` IntMap b
m2
Ordering
GT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.difference` IntMap b
m2
{-# INLINE difference #-}
(\\)
:: NEIntMap a
-> NEIntMap b
-> IntMap a
\\ :: forall a b. NEIntMap a -> NEIntMap b -> IntMap a
(\\) = forall a b. NEIntMap a -> NEIntMap b -> IntMap a
difference
{-# INLINE (\\) #-}
differenceWith
:: (a -> b -> Maybe a)
-> NEIntMap a
-> NEIntMap b
-> IntMap a
differenceWith :: forall a b.
(a -> b -> Maybe a) -> NEIntMap a -> NEIntMap b -> IntMap a
differenceWith a -> b -> Maybe a
f = forall a b.
(Key -> a -> b -> Maybe a) -> NEIntMap a -> NEIntMap b -> IntMap a
differenceWithKey (forall a b. a -> b -> a
const a -> b -> Maybe a
f)
{-# INLINE differenceWith #-}
differenceWithKey
:: (Key -> a -> b -> Maybe a)
-> NEIntMap a
-> NEIntMap b
-> IntMap a
differenceWithKey :: forall a b.
(Key -> a -> b -> Maybe a) -> NEIntMap a -> NEIntMap b -> IntMap a
differenceWithKey Key -> a -> b -> Maybe a
f n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap b
n2@(NEIntMap Key
k2 b
v2 IntMap b
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k1 a
v1 forall a b. (a -> b) -> a -> b
$ forall a b.
(Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
M.differenceWithKey Key -> a -> b -> Maybe a
f IntMap a
m1 (forall a. NEIntMap a -> IntMap a
toMap NEIntMap b
n2)
Ordering
EQ -> (forall a b. (a -> b) -> a -> b
$ forall a b.
(Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
M.differenceWithKey Key -> a -> b -> Maybe a
f IntMap a
m1 IntMap b
m2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k1) forall a b. (a -> b) -> a -> b
$ Key -> a -> b -> Maybe a
f Key
k1 a
v1 b
v2
Ordering
GT -> forall a b.
(Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
M.differenceWithKey Key -> a -> b -> Maybe a
f (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1) IntMap b
m2
{-# INLINE differenceWithKey #-}
intersection
:: NEIntMap a
-> NEIntMap b
-> IntMap a
intersection :: forall a b. NEIntMap a -> NEIntMap b -> IntMap a
intersection n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap b
n2@(NEIntMap Key
k2 b
_ IntMap b
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> IntMap a
m1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.intersection` forall a. NEIntMap a -> IntMap a
toMap NEIntMap b
n2
Ordering
EQ -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k1 a
v1 forall a b. (a -> b) -> a -> b
$ IntMap a
m1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.intersection` IntMap b
m2
Ordering
GT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1 forall a b. IntMap a -> IntMap b -> IntMap a
`M.intersection` IntMap b
m2
{-# INLINE intersection #-}
intersectionWith
:: (a -> b -> c)
-> NEIntMap a
-> NEIntMap b
-> IntMap c
intersectionWith :: forall a b c. (a -> b -> c) -> NEIntMap a -> NEIntMap b -> IntMap c
intersectionWith a -> b -> c
f = forall a b c.
(Key -> a -> b -> c) -> NEIntMap a -> NEIntMap b -> IntMap c
intersectionWithKey (forall a b. a -> b -> a
const a -> b -> c
f)
{-# INLINE intersectionWith #-}
intersectionWithKey
:: (Key -> a -> b -> c)
-> NEIntMap a
-> NEIntMap b
-> IntMap c
intersectionWithKey :: forall a b c.
(Key -> a -> b -> c) -> NEIntMap a -> NEIntMap b -> IntMap c
intersectionWithKey Key -> a -> b -> c
f n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap b
n2@(NEIntMap Key
k2 b
v2 IntMap b
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a b c.
(Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
M.intersectionWithKey Key -> a -> b -> c
f IntMap a
m1 (forall a. NEIntMap a -> IntMap a
toMap NEIntMap b
n2)
Ordering
EQ -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k1 (Key -> a -> b -> c
f Key
k1 a
v1 b
v2) forall a b. (a -> b) -> a -> b
$ forall a b c.
(Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
M.intersectionWithKey Key -> a -> b -> c
f IntMap a
m1 IntMap b
m2
Ordering
GT -> forall a b c.
(Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
M.intersectionWithKey Key -> a -> b -> c
f (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1) IntMap b
m2
{-# INLINE intersectionWithKey #-}
mapWithKey :: (Key -> a -> b) -> NEIntMap a -> NEIntMap b
mapWithKey :: forall a b. (Key -> a -> b) -> NEIntMap a -> NEIntMap b
mapWithKey Key -> a -> b
f (NEIntMap Key
k a
v IntMap a
m) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Key -> a -> b
f Key
k a
v) (forall a b. (Key -> a -> b) -> IntMap a -> IntMap b
M.mapWithKey Key -> a -> b
f IntMap a
m)
{-# NOINLINE [1] mapWithKey #-}
{-# RULES
"mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
mapWithKey (\k a -> f k (g k a)) xs
"mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
mapWithKey (\k a -> f k (g a)) xs
"map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
mapWithKey (\k a -> f (g k a)) xs
#-}
mapAccum
:: (a -> b -> (a, c))
-> a
-> NEIntMap b
-> (a, NEIntMap c)
mapAccum :: forall a b c.
(a -> b -> (a, c)) -> a -> NEIntMap b -> (a, NEIntMap c)
mapAccum a -> b -> (a, c)
f = forall a b c.
(a -> Key -> b -> (a, c)) -> a -> NEIntMap b -> (a, NEIntMap c)
mapAccumWithKey (\a
x Key
_ -> a -> b -> (a, c)
f a
x)
{-# INLINE mapAccum #-}
mapAccumWithKey
:: (a -> Key -> b -> (a, c))
-> a
-> NEIntMap b
-> (a, NEIntMap c)
mapAccumWithKey :: forall a b c.
(a -> Key -> b -> (a, c)) -> a -> NEIntMap b -> (a, NEIntMap c)
mapAccumWithKey a -> Key -> b -> (a, c)
f a
z0 (NEIntMap Key
k b
v IntMap b
m) = (a
z2, forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k c
v' IntMap c
m')
where
~(a
z1, c
v') = a -> Key -> b -> (a, c)
f a
z0 Key
k b
v
~(a
z2, IntMap c
m') = forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
M.mapAccumWithKey a -> Key -> b -> (a, c)
f a
z1 IntMap b
m
{-# INLINE mapAccumWithKey #-}
mapAccumRWithKey
:: (a -> Key -> b -> (a, c))
-> a
-> NEIntMap b
-> (a, NEIntMap c)
mapAccumRWithKey :: forall a b c.
(a -> Key -> b -> (a, c)) -> a -> NEIntMap b -> (a, NEIntMap c)
mapAccumRWithKey a -> Key -> b -> (a, c)
f a
z0 (NEIntMap Key
k b
v IntMap b
m) = (a
z2, forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k c
v' IntMap c
m')
where
~(a
z1, IntMap c
m') = forall a b c.
(a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
M.mapAccumRWithKey a -> Key -> b -> (a, c)
f a
z0 IntMap b
m
~(a
z2, c
v') = a -> Key -> b -> (a, c)
f a
z1 Key
k b
v
{-# INLINE mapAccumRWithKey #-}
mapKeys
:: (Key -> Key)
-> NEIntMap a
-> NEIntMap a
mapKeys :: forall a. (Key -> Key) -> NEIntMap a -> NEIntMap a
mapKeys Key -> Key
f (NEIntMap Key
k0 a
v0 IntMap a
m) = forall a. (a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromListWith forall a b. a -> b -> a
const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key -> Key
f Key
k0, a
v0) forall a. a -> [a] -> NonEmpty a
:|)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Key -> a -> b -> b) -> b -> IntMap a -> b
M.foldrWithKey (\Key
k a
v [(Key, a)]
kvs -> (Key -> Key
f Key
k, a
v) forall a. a -> [a] -> [a]
: [(Key, a)]
kvs) []
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINABLE mapKeys #-}
mapKeysWith
:: (a -> a -> a)
-> (Key -> Key)
-> NEIntMap a
-> NEIntMap a
mapKeysWith :: forall a. (a -> a -> a) -> (Key -> Key) -> NEIntMap a -> NEIntMap a
mapKeysWith a -> a -> a
c Key -> Key
f (NEIntMap Key
k0 a
v0 IntMap a
m) = forall a. (a -> a -> a) -> NonEmpty (Key, a) -> NEIntMap a
fromListWith a -> a -> a
c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key -> Key
f Key
k0, a
v0) forall a. a -> [a] -> NonEmpty a
:|)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Key -> a -> b -> b) -> b -> IntMap a -> b
M.foldrWithKey (\Key
k a
v [(Key, a)]
kvs -> (Key -> Key
f Key
k, a
v) forall a. a -> [a] -> [a]
: [(Key, a)]
kvs) []
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINABLE mapKeysWith #-}
mapKeysMonotonic
:: (Key -> Key)
-> NEIntMap a
-> NEIntMap a
mapKeysMonotonic :: forall a. (Key -> Key) -> NEIntMap a -> NEIntMap a
mapKeysMonotonic Key -> Key
f (NEIntMap Key
k a
v IntMap a
m) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap (Key -> Key
f Key
k) a
v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> Key) -> IntMap a -> IntMap a
M.mapKeysMonotonic Key -> Key
f
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE mapKeysMonotonic #-}
foldrWithKey :: (Key -> a -> b -> b) -> b -> NEIntMap a -> b
foldrWithKey :: forall a b. (Key -> a -> b -> b) -> b -> NEIntMap a -> b
foldrWithKey Key -> a -> b -> b
f b
z (NEIntMap Key
k a
v IntMap a
m) = Key -> a -> b -> b
f Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Key -> a -> b -> b) -> b -> IntMap a -> b
M.foldrWithKey Key -> a -> b -> b
f b
z forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE foldrWithKey #-}
foldlWithKey :: (a -> Key -> b -> a) -> a -> NEIntMap b -> a
foldlWithKey :: forall a b. (a -> Key -> b -> a) -> a -> NEIntMap b -> a
foldlWithKey a -> Key -> b -> a
f a
z (NEIntMap Key
k b
v IntMap b
m) = forall a b. (a -> Key -> b -> a) -> a -> IntMap b -> a
M.foldlWithKey a -> Key -> b -> a
f (a -> Key -> b -> a
f a
z Key
k b
v) IntMap b
m
{-# INLINE foldlWithKey #-}
foldr1' :: (a -> a -> a) -> NEIntMap a -> a
foldr1' :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldr1' a -> a -> a
f (NEIntMap Key
_ a
v IntMap a
m) = case forall a. IntMap a -> Maybe (a, IntMap a)
M.maxView IntMap a
m of
Maybe (a, IntMap a)
Nothing -> a
v
Just (a
y, IntMap a
m') -> let !z :: a
z = forall a b. (a -> b -> b) -> b -> IntMap a -> b
M.foldr' a -> a -> a
f a
y IntMap a
m' in a
v a -> a -> a
`f` a
z
{-# INLINE foldr1' #-}
foldl1' :: (a -> a -> a) -> NEIntMap a -> a
foldl1' :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldl1' a -> a -> a
f (NEIntMap Key
_ a
v IntMap a
m) = forall a b. (a -> b -> a) -> a -> IntMap b -> a
M.foldl' a -> a -> a
f a
v IntMap a
m
{-# INLINE foldl1' #-}
foldrWithKey' :: (Key -> a -> b -> b) -> b -> NEIntMap a -> b
foldrWithKey' :: forall a b. (Key -> a -> b -> b) -> b -> NEIntMap a -> b
foldrWithKey' Key -> a -> b -> b
f b
z (NEIntMap Key
k a
v IntMap a
m) = Key -> a -> b -> b
f Key
k a
v b
y
where
!y :: b
y = forall a b. (Key -> a -> b -> b) -> b -> IntMap a -> b
M.foldrWithKey Key -> a -> b -> b
f b
z IntMap a
m
{-# INLINE foldrWithKey' #-}
foldlWithKey' :: (a -> Key -> b -> a) -> a -> NEIntMap b -> a
foldlWithKey' :: forall a b. (a -> Key -> b -> a) -> a -> NEIntMap b -> a
foldlWithKey' a -> Key -> b -> a
f a
z (NEIntMap Key
k b
v IntMap b
m) = forall a b. (a -> Key -> b -> a) -> a -> IntMap b -> a
M.foldlWithKey' a -> Key -> b -> a
f a
x IntMap b
m
where
!x :: a
x = a -> Key -> b -> a
f a
z Key
k b
v
{-# INLINE foldlWithKey' #-}
keys :: NEIntMap a -> NonEmpty Key
keys :: forall a. NEIntMap a -> NonEmpty Key
keys (NEIntMap Key
k a
_ IntMap a
m) = Key
k forall a. a -> [a] -> NonEmpty a
:| forall a. IntMap a -> [Key]
M.keys IntMap a
m
{-# INLINE keys #-}
assocs :: NEIntMap a -> NonEmpty (Key, a)
assocs :: forall a. NEIntMap a -> NonEmpty (Key, a)
assocs = forall a. NEIntMap a -> NonEmpty (Key, a)
toList
{-# INLINE assocs #-}
keysSet :: NEIntMap a -> NEIntSet
keysSet :: forall a. NEIntMap a -> NEIntSet
keysSet (NEIntMap Key
k a
_ IntMap a
m) = Key -> IntSet -> NEIntSet
NEIntSet Key
k (forall a. IntMap a -> IntSet
M.keysSet IntMap a
m)
{-# INLINE keysSet #-}
toAscList :: NEIntMap a -> NonEmpty (Key, a)
toAscList :: forall a. NEIntMap a -> NonEmpty (Key, a)
toAscList = forall a. NEIntMap a -> NonEmpty (Key, a)
toList
{-# INLINE toAscList #-}
toDescList :: NEIntMap a -> NonEmpty (Key, a)
toDescList :: forall a. NEIntMap a -> NonEmpty (Key, a)
toDescList (NEIntMap Key
k0 a
v0 IntMap a
m) = forall a b. (a -> Key -> b -> a) -> a -> IntMap b -> a
M.foldlWithKey' forall {a} {b}. NonEmpty (a, b) -> a -> b -> NonEmpty (a, b)
go ((Key
k0, a
v0) forall a. a -> [a] -> NonEmpty a
:| []) IntMap a
m
where
go :: NonEmpty (a, b) -> a -> b -> NonEmpty (a, b)
go NonEmpty (a, b)
xs a
k b
v = (a
k, b
v) forall a. a -> NonEmpty a -> NonEmpty a
NE.<| NonEmpty (a, b)
xs
{-# INLINE toDescList #-}
filter
:: (a -> Bool)
-> NEIntMap a
-> IntMap a
filter :: forall a. (a -> Bool) -> NEIntMap a -> IntMap a
filter a -> Bool
f (NEIntMap Key
k a
v IntMap a
m)
| a -> Bool
f a
v = forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> IntMap a -> IntMap a
M.filter a -> Bool
f forall a b. (a -> b) -> a -> b
$ IntMap a
m
| Bool
otherwise = forall a. (a -> Bool) -> IntMap a -> IntMap a
M.filter a -> Bool
f IntMap a
m
{-# INLINE filter #-}
filterWithKey
:: (Key -> a -> Bool)
-> NEIntMap a
-> IntMap a
filterWithKey :: forall a. (Key -> a -> Bool) -> NEIntMap a -> IntMap a
filterWithKey Key -> a -> Bool
f (NEIntMap Key
k a
v IntMap a
m)
| Key -> a -> Bool
f Key
k a
v = forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> Bool) -> IntMap a -> IntMap a
M.filterWithKey Key -> a -> Bool
f forall a b. (a -> b) -> a -> b
$ IntMap a
m
| Bool
otherwise = forall a. (Key -> a -> Bool) -> IntMap a -> IntMap a
M.filterWithKey Key -> a -> Bool
f IntMap a
m
{-# INLINE filterWithKey #-}
restrictKeys
:: NEIntMap a
-> IntSet
-> IntMap a
restrictKeys :: forall a. NEIntMap a -> IntSet -> IntMap a
restrictKeys n :: NEIntMap a
n@(NEIntMap Key
k a
v IntMap a
m) IntSet
xs = case IntSet -> Maybe (Key, IntSet)
S.minView IntSet
xs of
Maybe (Key, IntSet)
Nothing -> forall a. IntMap a
M.empty
Just (Key
y, IntSet
ys) -> case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
y of
Ordering
LT -> IntMap a
m forall a. IntMap a -> IntSet -> IntMap a
`M.restrictKeys` IntSet
xs
Ordering
EQ -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v forall a b. (a -> b) -> a -> b
$ IntMap a
m forall a. IntMap a -> IntSet -> IntMap a
`M.restrictKeys` IntSet
ys
Ordering
GT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n forall a. IntMap a -> IntSet -> IntMap a
`M.restrictKeys` IntSet
ys
{-# INLINE restrictKeys #-}
withoutKeys
:: NEIntMap a
-> IntSet
-> IntMap a
withoutKeys :: forall a. NEIntMap a -> IntSet -> IntMap a
withoutKeys n :: NEIntMap a
n@(NEIntMap Key
k a
v IntMap a
m) IntSet
xs = case IntSet -> Maybe (Key, IntSet)
S.minView IntSet
xs of
Maybe (Key, IntSet)
Nothing -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n
Just (Key
y, IntSet
ys) -> case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
y of
Ordering
LT -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v forall a b. (a -> b) -> a -> b
$ IntMap a
m forall a. IntMap a -> IntSet -> IntMap a
`M.withoutKeys` IntSet
xs
Ordering
EQ -> IntMap a
m forall a. IntMap a -> IntSet -> IntMap a
`M.withoutKeys` IntSet
ys
Ordering
GT -> forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n forall a. IntMap a -> IntSet -> IntMap a
`M.withoutKeys` IntSet
ys
{-# INLINE withoutKeys #-}
partition
:: (a -> Bool)
-> NEIntMap a
-> These (NEIntMap a) (NEIntMap a)
partition :: forall a.
(a -> Bool) -> NEIntMap a -> These (NEIntMap a) (NEIntMap a)
partition a -> Bool
f = forall a.
(Key -> a -> Bool) -> NEIntMap a -> These (NEIntMap a) (NEIntMap a)
partitionWithKey (forall a b. a -> b -> a
const a -> Bool
f)
{-# INLINE partition #-}
partitionWithKey
:: (Key -> a -> Bool)
-> NEIntMap a
-> These (NEIntMap a) (NEIntMap a)
partitionWithKey :: forall a.
(Key -> a -> Bool) -> NEIntMap a -> These (NEIntMap a) (NEIntMap a)
partitionWithKey Key -> a -> Bool
f n :: NEIntMap a
n@(NEIntMap Key
k a
v IntMap a
m0) = case (forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m1, forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m2) of
(Maybe (NEIntMap a)
Nothing, Maybe (NEIntMap a)
Nothing)
| Key -> a -> Bool
f Key
k a
v -> forall a b. a -> These a b
This NEIntMap a
n
| Bool
otherwise -> forall a b. b -> These a b
That NEIntMap a
n
(Just NEIntMap a
n1, Maybe (NEIntMap a)
Nothing)
| Key -> a -> Bool
f Key
k a
v -> forall a b. a -> These a b
This NEIntMap a
n
| Bool
otherwise -> forall a b. a -> b -> These a b
These NEIntMap a
n1 (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v)
(Maybe (NEIntMap a)
Nothing, Just NEIntMap a
n2)
| Key -> a -> Bool
f Key
k a
v -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) NEIntMap a
n2
| Bool
otherwise -> forall a b. b -> These a b
That NEIntMap a
n
(Just NEIntMap a
n1, Just NEIntMap a
n2)
| Key -> a -> Bool
f Key
k a
v -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k a
v IntMap a
m1) NEIntMap a
n2
| Bool
otherwise -> forall a b. a -> b -> These a b
These NEIntMap a
n1 (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k a
v IntMap a
m2)
where
(IntMap a
m1, IntMap a
m2) = forall a. (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
M.partitionWithKey Key -> a -> Bool
f IntMap a
m0
{-# INLINABLE partitionWithKey #-}
mapMaybe
:: (a -> Maybe b)
-> NEIntMap a
-> IntMap b
mapMaybe :: forall a b. (a -> Maybe b) -> NEIntMap a -> IntMap b
mapMaybe a -> Maybe b
f = forall a b. (Key -> a -> Maybe b) -> NEIntMap a -> IntMap b
mapMaybeWithKey (forall a b. a -> b -> a
const a -> Maybe b
f)
{-# INLINE mapMaybe #-}
mapMaybeWithKey
:: (Key -> a -> Maybe b)
-> NEIntMap a
-> IntMap b
mapMaybeWithKey :: forall a b. (Key -> a -> Maybe b) -> NEIntMap a -> IntMap b
mapMaybeWithKey Key -> a -> Maybe b
f (NEIntMap Key
k a
v IntMap a
m) = (forall a b. (a -> b) -> a -> b
$ forall a b. (Key -> a -> Maybe b) -> IntMap a -> IntMap b
M.mapMaybeWithKey Key -> a -> Maybe b
f IntMap a
m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k)
forall a b. (a -> b) -> a -> b
$ Key -> a -> Maybe b
f Key
k a
v
{-# INLINE mapMaybeWithKey #-}
mapEither
:: (a -> Either b c)
-> NEIntMap a
-> These (NEIntMap b) (NEIntMap c)
mapEither :: forall a b c.
(a -> Either b c) -> NEIntMap a -> These (NEIntMap b) (NEIntMap c)
mapEither a -> Either b c
f = forall a b c.
(Key -> a -> Either b c)
-> NEIntMap a -> These (NEIntMap b) (NEIntMap c)
mapEitherWithKey (forall a b. a -> b -> a
const a -> Either b c
f)
{-# INLINE mapEither #-}
mapEitherWithKey
:: (Key -> a -> Either b c)
-> NEIntMap a
-> These (NEIntMap b) (NEIntMap c)
mapEitherWithKey :: forall a b c.
(Key -> a -> Either b c)
-> NEIntMap a -> These (NEIntMap b) (NEIntMap c)
mapEitherWithKey Key -> a -> Either b c
f (NEIntMap Key
k a
v IntMap a
m0) = case (forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap b
m1, forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap c
m2) of
(Maybe (NEIntMap b)
Nothing, Maybe (NEIntMap c)
Nothing) -> case Key -> a -> Either b c
f Key
k a
v of
Left b
v' -> forall a b. a -> These a b
This (forall a. Key -> a -> NEIntMap a
singleton Key
k b
v')
Right c
v' -> forall a b. b -> These a b
That (forall a. Key -> a -> NEIntMap a
singleton Key
k c
v')
(Just NEIntMap b
n1, Maybe (NEIntMap c)
Nothing) -> case Key -> a -> Either b c
f Key
k a
v of
Left b
v' -> forall a b. a -> These a b
This (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k b
v' IntMap b
m1)
Right c
v' -> forall a b. a -> b -> These a b
These NEIntMap b
n1 (forall a. Key -> a -> NEIntMap a
singleton Key
k c
v')
(Maybe (NEIntMap b)
Nothing, Just NEIntMap c
n2) -> case Key -> a -> Either b c
f Key
k a
v of
Left b
v' -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> NEIntMap a
singleton Key
k b
v') NEIntMap c
n2
Right c
v' -> forall a b. b -> These a b
That (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k c
v' IntMap c
m2)
(Just NEIntMap b
n1, Just NEIntMap c
n2) -> case Key -> a -> Either b c
f Key
k a
v of
Left b
v' -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k b
v' IntMap b
m1) NEIntMap c
n2
Right c
v' -> forall a b. a -> b -> These a b
These NEIntMap b
n1 (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k c
v' IntMap c
m2)
where
(IntMap b
m1, IntMap c
m2) = forall a b c.
(Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
M.mapEitherWithKey Key -> a -> Either b c
f IntMap a
m0
{-# INLINABLE mapEitherWithKey #-}
split
:: Key
-> NEIntMap a
-> Maybe (These (NEIntMap a) (NEIntMap a))
split :: forall a.
Key -> NEIntMap a -> Maybe (These (NEIntMap a) (NEIntMap a))
split Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v IntMap a
m0) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. b -> These a b
That NEIntMap a
n
Ordering
EQ -> forall a b. b -> These a b
That forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m0
Ordering
GT -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ case (forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m1, forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m2) of
(Maybe (NEIntMap a)
Nothing, Maybe (NEIntMap a)
Nothing) -> forall a b. a -> These a b
This (forall a. Key -> a -> NEIntMap a
singleton Key
k0 a
v)
(Just NEIntMap a
_ , Maybe (NEIntMap a)
Nothing) -> forall a b. a -> These a b
This (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k0 a
v IntMap a
m1)
(Maybe (NEIntMap a)
Nothing, Just NEIntMap a
n2) -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> NEIntMap a
singleton Key
k0 a
v) NEIntMap a
n2
(Just NEIntMap a
_ , Just NEIntMap a
n2) -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k0 a
v IntMap a
m1) NEIntMap a
n2
where
(IntMap a
m1, IntMap a
m2) = forall a. Key -> IntMap a -> (IntMap a, IntMap a)
M.split Key
k IntMap a
m0
{-# INLINABLE split #-}
splitLookup
:: Key
-> NEIntMap a
-> These a (These (NEIntMap a) (NEIntMap a))
splitLookup :: forall a.
Key -> NEIntMap a -> These a (These (NEIntMap a) (NEIntMap a))
splitLookup Key
k n :: NEIntMap a
n@(NEIntMap Key
k0 a
v0 IntMap a
m0) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a b. b -> These a b
That forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> These a b
That forall a b. (a -> b) -> a -> b
$ NEIntMap a
n
Ordering
EQ -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. a -> These a b
This a
v0) (forall a b. a -> b -> These a b
These a
v0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> These a b
That) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap forall a b. (a -> b) -> a -> b
$ IntMap a
m0
Ordering
GT -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a b. b -> These a b
That forall a b. a -> b -> These a b
These Maybe a
v forall a b. (a -> b) -> a -> b
$ case (forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m1, forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap IntMap a
m2) of
(Maybe (NEIntMap a)
Nothing, Maybe (NEIntMap a)
Nothing) -> forall a b. a -> These a b
This (forall a. Key -> a -> NEIntMap a
singleton Key
k0 a
v0)
(Just NEIntMap a
_ , Maybe (NEIntMap a)
Nothing) -> forall a b. a -> These a b
This (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k0 a
v0 IntMap a
m1)
(Maybe (NEIntMap a)
Nothing, Just NEIntMap a
n2) -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> NEIntMap a
singleton Key
k0 a
v0) NEIntMap a
n2
(Just NEIntMap a
_ , Just NEIntMap a
n2) -> forall a b. a -> b -> These a b
These (forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k0 a
v0 IntMap a
m1) NEIntMap a
n2
where
(IntMap a
m1, Maybe a
v, IntMap a
m2) = forall a. Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)
M.splitLookup Key
k IntMap a
m0
{-# INLINABLE splitLookup #-}
splitRoot
:: NEIntMap a
-> NonEmpty (NEIntMap a)
splitRoot :: forall a. NEIntMap a -> NonEmpty (NEIntMap a)
splitRoot (NEIntMap Key
k a
v IntMap a
m) = forall a. Key -> a -> NEIntMap a
singleton Key
k a
v
forall a. a -> [a] -> NonEmpty a
:| forall a b. (a -> Maybe b) -> [a] -> [b]
Maybe.mapMaybe forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap (forall a. IntMap a -> [IntMap a]
M.splitRoot IntMap a
m)
{-# INLINE splitRoot #-}
isSubmapOf :: Eq a => NEIntMap a -> NEIntMap a -> Bool
isSubmapOf :: forall a. Eq a => NEIntMap a -> NEIntMap a -> Bool
isSubmapOf = forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
isSubmapOfBy forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE isSubmapOf #-}
isSubmapOfBy
:: (a -> b -> Bool)
-> NEIntMap a
-> NEIntMap b
-> Bool
isSubmapOfBy :: forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
isSubmapOfBy a -> b -> Bool
f (NEIntMap Key
k a
v IntMap a
m0) (forall a. NEIntMap a -> IntMap a
toMap->IntMap b
m1) = Bool
kvSub
Bool -> Bool -> Bool
&& forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
M.isSubmapOfBy a -> b -> Bool
f IntMap a
m0 IntMap b
m1
where
kvSub :: Bool
kvSub = case forall a. Key -> IntMap a -> Maybe a
M.lookup Key
k IntMap b
m1 of
Just b
v0 -> a -> b -> Bool
f a
v b
v0
Maybe b
Nothing -> Bool
False
{-# INLINE isSubmapOfBy #-}
isProperSubmapOf :: Eq a => NEIntMap a -> NEIntMap a -> Bool
isProperSubmapOf :: forall a. Eq a => NEIntMap a -> NEIntMap a -> Bool
isProperSubmapOf = forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
isProperSubmapOfBy forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE isProperSubmapOf #-}
isProperSubmapOfBy
:: (a -> b -> Bool)
-> NEIntMap a
-> NEIntMap b
-> Bool
isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
isProperSubmapOfBy a -> b -> Bool
f NEIntMap a
m1 NEIntMap b
m2 = forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap a
m1) forall a. Ord a => a -> a -> Bool
< forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap b
m2)
Bool -> Bool -> Bool
&& forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
isSubmapOfBy a -> b -> Bool
f NEIntMap a
m1 NEIntMap b
m2
{-# INLINE isProperSubmapOfBy #-}
findMin :: NEIntMap a -> (Key, a)
findMin :: forall a. NEIntMap a -> (Key, a)
findMin (NEIntMap Key
k a
v IntMap a
_) = (Key
k, a
v)
{-# INLINE findMin #-}
findMax :: NEIntMap a -> (Key, a)
findMax :: forall a. NEIntMap a -> (Key, a)
findMax (NEIntMap Key
k a
v IntMap a
m) = forall a. a -> Maybe a -> a
fromMaybe (Key
k, a
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe (Key, a)
lookupMaxMap forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE findMax #-}
deleteMin :: NEIntMap a -> IntMap a
deleteMin :: forall a. NEIntMap a -> IntMap a
deleteMin (NEIntMap Key
_ a
_ IntMap a
m) = IntMap a
m
{-# INLINE deleteMin #-}
deleteMax :: NEIntMap a -> IntMap a
deleteMax :: forall a. NEIntMap a -> IntMap a
deleteMax (NEIntMap Key
k a
v IntMap a
m) = case forall a. IntMap a -> Maybe (a, IntMap a)
M.maxView IntMap a
m of
Maybe (a, IntMap a)
Nothing -> forall a. IntMap a
M.empty
Just (a
_, IntMap a
m') -> forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v IntMap a
m'
{-# INLINE deleteMax #-}
updateMin :: (a -> Maybe a) -> NEIntMap a -> IntMap a
updateMin :: forall a. (a -> Maybe a) -> NEIntMap a -> IntMap a
updateMin a -> Maybe a
f = forall a. (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMinWithKey (forall a b. a -> b -> a
const a -> Maybe a
f)
{-# INLINE updateMin #-}
adjustMin :: (a -> a) -> NEIntMap a -> NEIntMap a
adjustMin :: forall a. (a -> a) -> NEIntMap a -> NEIntMap a
adjustMin a -> a
f = forall a. (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMinWithKey (forall a b. a -> b -> a
const a -> a
f)
{-# INLINE adjustMin #-}
updateMinWithKey :: (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMinWithKey :: forall a. (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMinWithKey Key -> a -> Maybe a
f (NEIntMap Key
k a
v IntMap a
m) = (forall a b. (a -> b) -> a -> b
$ IntMap a
m) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k) forall a b. (a -> b) -> a -> b
$ Key -> a -> Maybe a
f Key
k a
v
{-# INLINE updateMinWithKey #-}
adjustMinWithKey :: (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMinWithKey :: forall a. (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMinWithKey Key -> a -> a
f (NEIntMap Key
k a
v IntMap a
m) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (Key -> a -> a
f Key
k a
v) IntMap a
m
{-# INLINE adjustMinWithKey #-}
updateMax :: (a -> Maybe a) -> NEIntMap a -> IntMap a
updateMax :: forall a. (a -> Maybe a) -> NEIntMap a -> IntMap a
updateMax a -> Maybe a
f = forall a. (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMaxWithKey (forall a b. a -> b -> a
const a -> Maybe a
f)
{-# INLINE updateMax #-}
adjustMax :: (a -> a) -> NEIntMap a -> NEIntMap a
adjustMax :: forall a. (a -> a) -> NEIntMap a -> NEIntMap a
adjustMax a -> a
f = forall a. (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMaxWithKey (forall a b. a -> b -> a
const a -> a
f)
{-# INLINE adjustMax #-}
updateMaxWithKey :: (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMaxWithKey :: forall a. (Key -> a -> Maybe a) -> NEIntMap a -> IntMap a
updateMaxWithKey Key -> a -> Maybe a
f (NEIntMap Key
k a
v IntMap a
m)
| forall a. IntMap a -> Bool
M.null IntMap a
m = forall b a. b -> (a -> b) -> Maybe a -> b
maybe IntMap a
m (forall a. Key -> a -> IntMap a
M.singleton Key
k) forall a b. (a -> b) -> a -> b
$ Key -> a -> Maybe a
f Key
k a
v
| Bool
otherwise = forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
M.updateMaxWithKey Key -> a -> Maybe a
f
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE updateMaxWithKey #-}
adjustMaxWithKey :: (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMaxWithKey :: forall a. (Key -> a -> a) -> NEIntMap a -> NEIntMap a
adjustMaxWithKey Key -> a -> a
f (NEIntMap Key
k0 a
v IntMap a
m)
| forall a. IntMap a -> Bool
M.null IntMap a
m = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 (Key -> a -> a
f Key
k0 a
v) IntMap a
m
| Bool
otherwise = forall a. Key -> a -> IntMap a -> NEIntMap a
insertMapMin Key
k0 a
v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Key -> a -> Maybe a) -> IntMap a -> IntMap a
M.updateMaxWithKey (\Key
k -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> a -> a
f Key
k)
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE adjustMaxWithKey #-}
minView :: NEIntMap a -> (a, IntMap a)
minView :: forall a. NEIntMap a -> (a, IntMap a)
minView = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> ((Key, a), IntMap a)
deleteFindMin
{-# INLINE minView #-}
deleteFindMin :: NEIntMap a -> ((Key, a), IntMap a)
deleteFindMin :: forall a. NEIntMap a -> ((Key, a), IntMap a)
deleteFindMin (NEIntMap Key
k a
v IntMap a
m) = ((Key
k, a
v), IntMap a
m)
{-# INLINE deleteFindMin #-}
maxView :: NEIntMap a -> (a, IntMap a)
maxView :: forall a. NEIntMap a -> (a, IntMap a)
maxView = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> ((Key, a), IntMap a)
deleteFindMax
{-# INLINE maxView #-}
deleteFindMax :: NEIntMap a -> ((Key, a), IntMap a)
deleteFindMax :: forall a. NEIntMap a -> ((Key, a), IntMap a)
deleteFindMax (NEIntMap Key
k a
v IntMap a
m) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((Key
k, a
v), forall a. IntMap a
M.empty) (forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe ((Key, a), IntMap a)
M.maxViewWithKey
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE deleteFindMax #-}
combineEq :: NonEmpty (Key, b) -> NonEmpty (Key, b)
combineEq :: forall b. NonEmpty (Key, b) -> NonEmpty (Key, b)
combineEq = \case
(Key, b)
x :| [] -> (Key, b)
x forall a. a -> [a] -> NonEmpty a
:| []
(Key, b)
x :| xx :: [(Key, b)]
xx@((Key, b)
_:[(Key, b)]
_) -> forall {a} {b}. Eq a => (a, b) -> [(a, b)] -> NonEmpty (a, b)
go (Key, b)
x [(Key, b)]
xx
where
go :: (a, b) -> [(a, b)] -> NonEmpty (a, b)
go (a, b)
z [] = (a, b)
z forall a. a -> [a] -> NonEmpty a
:| []
go z :: (a, b)
z@(a
kz,b
_) (x :: (a, b)
x@(a
kx,b
xx):[(a, b)]
xs')
| a
kxforall a. Eq a => a -> a -> Bool
==a
kz = (a, b) -> [(a, b)] -> NonEmpty (a, b)
go (a
kx,b
xx) [(a, b)]
xs'
| Bool
otherwise = (a, b)
z forall a. a -> NonEmpty a -> NonEmpty a
NE.<| (a, b) -> [(a, b)] -> NonEmpty (a, b)
go (a, b)
x [(a, b)]
xs'
combineEqWith
:: (Key -> b -> b -> b)
-> NonEmpty (Key, b)
-> NonEmpty (Key, b)
combineEqWith :: forall b.
(Key -> b -> b -> b) -> NonEmpty (Key, b) -> NonEmpty (Key, b)
combineEqWith Key -> b -> b -> b
f = \case
(Key, b)
x :| [] -> (Key, b)
x forall a. a -> [a] -> NonEmpty a
:| []
(Key, b)
x :| xx :: [(Key, b)]
xx@((Key, b)
_:[(Key, b)]
_) -> (Key, b) -> [(Key, b)] -> NonEmpty (Key, b)
go (Key, b)
x [(Key, b)]
xx
where
go :: (Key, b) -> [(Key, b)] -> NonEmpty (Key, b)
go (Key, b)
z [] = (Key, b)
z forall a. a -> [a] -> NonEmpty a
:| []
go z :: (Key, b)
z@(Key
kz,b
zz) (x :: (Key, b)
x@(Key
kx,b
xx):[(Key, b)]
xs')
| Key
kxforall a. Eq a => a -> a -> Bool
==Key
kz = let yy :: b
yy = Key -> b -> b -> b
f Key
kx b
xx b
zz in (Key, b) -> [(Key, b)] -> NonEmpty (Key, b)
go (Key
kx,b
yy) [(Key, b)]
xs'
| Bool
otherwise = (Key, b)
z forall a. a -> NonEmpty a -> NonEmpty a
NE.<| (Key, b) -> [(Key, b)] -> NonEmpty (Key, b)
go (Key, b)
x [(Key, b)]
xs'