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