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