-----------------------------------------------------------------------------
-- |
-- Module      :  Data.HashMap
-- Copyright   :  (c) Milan Straka 2011
-- License     :  BSD-style
-- Maintainer  :  fox@ucw.cz
-- Stability   :  provisional
-- Portability :  portable
--
-- Persistent 'Map' based on hashing, which is defined as
--
-- @
--   data 'Map' k v = 'Data.IntMap.IntMap' (Some k v)
-- @
--
-- is an 'Data.IntMap.IntMap' indexed by hash values of keys,
-- containing a value of @Some e@. That contains either one
-- @('k', 'v')@ pair or a @'Data.Map.Map' k v@ with keys of the same hash values.
--
-- The interface of a 'Map' is a suitable subset of 'Data.IntMap.IntMap'
-- and can be used as a drop-in replacement of 'Data.Map.Map'.
--
-- The complexity of operations is determined by the complexities of
-- 'Data.IntMap.IntMap' and 'Data.Map.Map' operations. See the sources of
-- 'Map' to see which operations from @containers@ package are used.
-----------------------------------------------------------------------------

module Data.HashMap ( Map
                    , HashMap

                    -- * Operators
                    , (!), (\\)

                    -- * Query
                    , null
                    , size
                    , member
                    , notMember
                    , lookup
                    , findWithDefault

                    -- * Construction
                    , empty
                    , singleton

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

                    -- ** Delete\/Update
                    , delete
                    , adjust
                    , adjustWithKey
                    , update
                    , updateWithKey
                    , updateLookupWithKey
                    , alter

                    -- * Combine

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

                    -- ** Difference
                    , difference
                    , differenceWith
                    , differenceWithKey

                    -- ** Intersection
                    , intersection
                    , intersectionWith
                    , intersectionWithKey

                    -- * Traversal
                    -- ** Map
                    , map
                    , mapWithKey
                    , mapAccum
                    , mapAccumWithKey

                    -- ** Fold
                    , fold
                    , foldWithKey

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

                    -- ** Lists
                    , toList
                    , fromList
                    , fromListWith
                    , fromListWithKey

                    -- * Filter
                    , filter
                    , filterWithKey
                    , partition
                    , partitionWithKey

                    , mapMaybe
                    , mapMaybeWithKey
                    , mapEither
                    , mapEitherWithKey

                    -- * Submap
                    , isSubmapOf, isSubmapOfBy
                    , isProperSubmapOf, isProperSubmapOfBy
                    ) where

import Prelude hiding (lookup,map,filter,null)

import Control.Applicative (Applicative(pure,(<*>)))
import Control.DeepSeq
import Data.Hashable
import Data.Foldable (Foldable(foldMap))
import Data.List (foldl')
import Data.Monoid (Monoid(..))
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
#endif
import Data.Traversable (Traversable(traverse))
import Data.Typeable

#if __GLASGOW_HASKELL__
import Text.Read
import Data.Data (Data(..), mkNoRepType)
#endif

import qualified Data.IntMap as I
import qualified Data.Map as M
import qualified Data.Set as S


{--------------------------------------------------------------------
  Operators
--------------------------------------------------------------------}

-- | Find the value at a key.
-- Calls 'error' when the element can not be found.
(!) :: (Hashable k, Ord k) => Map k a -> k -> a
Map k a
m ! :: forall k a. (Hashable k, Ord k) => Map k a -> k -> a
! k
k = case k -> Map k a -> Maybe a
forall k a. (Hashable k, Ord k) => k -> Map k a -> Maybe a
lookup k
k Map k a
m of
          Maybe a
Nothing -> [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"HashMap.(!): key not an element of the map"
          Just a
v -> a
v

-- | Same as 'difference'.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
Map k a
m1 \\ :: forall k a b. Ord k => Map k a -> Map k b -> Map k a
\\ Map k b
m2 = Map k a -> Map k b -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
difference Map k a
m1 Map k b
m2


{--------------------------------------------------------------------
  Types
--------------------------------------------------------------------}

data Some k v = Only !k v | More !(M.Map k v) deriving (Some k v -> Some k v -> Bool
(Some k v -> Some k v -> Bool)
-> (Some k v -> Some k v -> Bool) -> Eq (Some k v)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k v. (Eq k, Eq v) => Some k v -> Some k v -> Bool
/= :: Some k v -> Some k v -> Bool
$c/= :: forall k v. (Eq k, Eq v) => Some k v -> Some k v -> Bool
== :: Some k v -> Some k v -> Bool
$c== :: forall k v. (Eq k, Eq v) => Some k v -> Some k v -> Bool
Eq, Eq (Some k v)
Eq (Some k v)
-> (Some k v -> Some k v -> Ordering)
-> (Some k v -> Some k v -> Bool)
-> (Some k v -> Some k v -> Bool)
-> (Some k v -> Some k v -> Bool)
-> (Some k v -> Some k v -> Bool)
-> (Some k v -> Some k v -> Some k v)
-> (Some k v -> Some k v -> Some k v)
-> Ord (Some k v)
Some k v -> Some k v -> Bool
Some k v -> Some k v -> Ordering
Some k v -> Some k v -> Some k v
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {k} {v}. (Ord k, Ord v) => Eq (Some k v)
forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Bool
forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Ordering
forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Some k v
min :: Some k v -> Some k v -> Some k v
$cmin :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Some k v
max :: Some k v -> Some k v -> Some k v
$cmax :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Some k v
>= :: Some k v -> Some k v -> Bool
$c>= :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Bool
> :: Some k v -> Some k v -> Bool
$c> :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Bool
<= :: Some k v -> Some k v -> Bool
$c<= :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Bool
< :: Some k v -> Some k v -> Bool
$c< :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Bool
compare :: Some k v -> Some k v -> Ordering
$ccompare :: forall k v. (Ord k, Ord v) => Some k v -> Some k v -> Ordering
Ord)

instance (NFData k, NFData v) => NFData (Some k v) where
  rnf :: Some k v -> ()
rnf (Only k
k v
v) = k -> ()
forall a. NFData a => a -> ()
rnf k
k () -> () -> ()
`seq` v -> ()
forall a. NFData a => a -> ()
rnf v
v
  rnf (More Map k v
m) = Map k v -> ()
forall a. NFData a => a -> ()
rnf Map k v
m

-- | The abstract type of a @Map@. Its interface is a suitable
-- subset of 'Data.IntMap.IntMap'.
newtype Map k v = Map (I.IntMap (Some k v)) deriving (Map k v -> Map k v -> Bool
(Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool) -> Eq (Map k v)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
/= :: Map k v -> Map k v -> Bool
$c/= :: forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
== :: Map k v -> Map k v -> Bool
$c== :: forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
Eq, Eq (Map k v)
Eq (Map k v)
-> (Map k v -> Map k v -> Ordering)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Map k v)
-> (Map k v -> Map k v -> Map k v)
-> Ord (Map k v)
Map k v -> Map k v -> Bool
Map k v -> Map k v -> Ordering
Map k v -> Map k v -> Map k v
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {k} {v}. (Ord k, Ord v) => Eq (Map k v)
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Ordering
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
min :: Map k v -> Map k v -> Map k v
$cmin :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
max :: Map k v -> Map k v -> Map k v
$cmax :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
>= :: Map k v -> Map k v -> Bool
$c>= :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
> :: Map k v -> Map k v -> Bool
$c> :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
<= :: Map k v -> Map k v -> Bool
$c<= :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
< :: Map k v -> Map k v -> Bool
$c< :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
compare :: Map k v -> Map k v -> Ordering
$ccompare :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Ordering
Ord)

-- | The @HashMap@ is a type synonym for @Map@ for backward compatibility.
-- It is deprecated and will be removed in furture releases.
{-# DEPRECATED HashMap "HashMap is deprecated. Please use Map instead." #-}
type HashMap k v = Map k v

instance (NFData k, NFData v) => NFData (Map k v) where
  rnf :: Map k v -> ()
rnf (Map IntMap (Some k v)
m) = IntMap (Some k v) -> ()
forall a. NFData a => a -> ()
rnf IntMap (Some k v)
m

instance Functor (Map k) where
  fmap :: forall a b. (a -> b) -> Map k a -> Map k b
fmap = (a -> b) -> Map k a -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
map

instance Ord k => Monoid (Map k a) where
  mempty :: Map k a
mempty  = Map k a
forall k a. Map k a
empty
  mconcat :: [Map k a] -> Map k a
mconcat = [Map k a] -> Map k a
forall k a. Ord k => [Map k a] -> Map k a
unions
#if !(MIN_VERSION_base(4,9,0))
  mappend = union
#else
  mappend :: Map k a -> Map k a -> Map k a
mappend = Map k a -> Map k a -> Map k a
forall a. Semigroup a => a -> a -> a
(<>)

instance Ord k => Semigroup (Map k a) where
  <> :: Map k a -> Map k a -> Map k a
(<>)   = Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
union
  stimes :: forall b. Integral b => b -> Map k a -> Map k a
stimes = b -> Map k a -> Map k a
forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid
#endif

instance Foldable (Map k) where
  foldMap :: forall m a. Monoid m => (a -> m) -> Map k a -> m
foldMap a -> m
f (Map IntMap (Some k a)
m) = (Some k a -> m) -> IntMap (Some k a) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Some k a -> m
forall {k}. Some k a -> m
some_fold IntMap (Some k a)
m
    where some_fold :: Some k a -> m
some_fold (Only k
_ a
x) = a -> m
f a
x
          some_fold (More Map k a
s)   = (a -> m) -> Map k a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Map k a
s

instance Traversable (Map k) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map k a -> f (Map k b)
traverse a -> f b
f (Map IntMap (Some k a)
m) = (IntMap (Some k b) -> Map k b) -> f (IntMap (Some k b) -> Map k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure IntMap (Some k b) -> Map k b
forall k v. IntMap (Some k v) -> Map k v
Map f (IntMap (Some k b) -> Map k b)
-> f (IntMap (Some k b)) -> f (Map k b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Some k a -> f (Some k b))
-> IntMap (Some k a) -> f (IntMap (Some k b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Some k a -> f (Some k b)
forall {k}. Some k a -> f (Some k b)
some_traverse IntMap (Some k a)
m
    where some_traverse :: Some k a -> f (Some k b)
some_traverse (Only k
k a
x) = (b -> Some k b) -> f (b -> Some k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k -> b -> Some k b
forall k v. k -> v -> Some k v
Only k
k) f (b -> Some k b) -> f b -> f (Some k b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
x
          some_traverse (More Map k a
s) = (Map k b -> Some k b) -> f (Map k b -> Some k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k b -> Some k b
forall k v. Map k v -> Some k v
More f (Map k b -> Some k b) -> f (Map k b) -> f (Some k b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Map k a -> f (Map k b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Map k a
s

instance (Show k, Show a) => Show (Map k a) where
  showsPrec :: Int -> Map k a -> ShowS
showsPrec Int
d Map k a
m   = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
    [Char] -> ShowS
showString [Char]
"fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, a)] -> ShowS
forall a. Show a => a -> ShowS
shows (Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toList Map k a
m)

instance (Read k, Hashable k, Ord k, Read a) => Read (Map k a) where
#ifdef __GLASGOW_HASKELL__
  readPrec :: ReadPrec (Map k a)
readPrec = ReadPrec (Map k a) -> ReadPrec (Map k a)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (Map k a) -> ReadPrec (Map k a))
-> ReadPrec (Map k a) -> ReadPrec (Map k a)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (Map k a) -> ReadPrec (Map k a)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (Map k a) -> ReadPrec (Map k a))
-> ReadPrec (Map k a) -> ReadPrec (Map k a)
forall a b. (a -> b) -> a -> b
$ do
    Ident [Char]
"fromList" <- ReadPrec Lexeme
lexP
    [(k, a)]
xs <- ReadPrec [(k, a)]
forall a. Read a => ReadPrec a
readPrec
    Map k a -> ReadPrec (Map k a)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(k, a)] -> Map k a
forall k a. (Hashable k, Ord k) => [(k, a)] -> Map k a
fromList [(k, a)]
xs)

  readListPrec :: ReadPrec [Map k a]
readListPrec = ReadPrec [Map k a]
forall a. Read a => ReadPrec [a]
readListPrecDefault
#else
  readsPrec p = readParen (p > 10) $ \ r -> do
    ("fromList",s) <- lex r
    (xs,t) <- reads s
    return (fromList xs,t)
#endif

#include "hashmap.h"
INSTANCE_TYPEABLE2(Map,mapTc,"Map")



#if __GLASGOW_HASKELL__
{--------------------------------------------------------------------
  A Data instance
--------------------------------------------------------------------}

-- This instance preserves data abstraction at the cost of inefficiency.
-- We omit reflection services for the sake of data abstraction.

instance (Data k, Hashable k, Ord k, Data a) => Data (Map k a) where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k a -> c (Map k a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z Map k a
m = ([(k, a)] -> Map k a) -> c ([(k, a)] -> Map k a)
forall g. g -> c g
z [(k, a)] -> Map k a
forall k a. (Hashable k, Ord k) => [(k, a)] -> Map k a
fromList c ([(k, a)] -> Map k a) -> [(k, a)] -> c (Map k a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` (Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toList Map k a
m)
  toConstr :: Map k a -> Constr
toConstr Map k a
_   = [Char] -> Constr
forall a. HasCallStack => [Char] -> a
error [Char]
"toConstr"
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k a)
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = [Char] -> Constr -> c (Map k a)
forall a. HasCallStack => [Char] -> a
error [Char]
"gunfold"
  dataTypeOf :: Map k a -> DataType
dataTypeOf Map k a
_ = [Char] -> DataType
mkNoRepType [Char]
"Data.HashMap.Map"
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Map k a))
dataCast1 forall d. Data d => c (t d)
f  = c (t a) -> Maybe (c (Map k a))
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
f
#endif

{--------------------------------------------------------------------
  Comparing elements
--------------------------------------------------------------------}

-- For ByteStrings, doing compare is usually faster than doing (==),
-- according to benchmarks. A Set is using compare naturally. We therefore
-- define eq :: Ord a => a -> a -> Bool, which serves as (==).

{-# INLINE eq #-}
eq :: Ord a => a -> a -> Bool
eq :: forall a. Ord a => a -> a -> Bool
eq a
x a
y = a
x a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ


{--------------------------------------------------------------------
  Query
--------------------------------------------------------------------}
-- | Is the map empty?
null :: Map k a -> Bool
null :: forall k a. Map k a -> Bool
null (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Bool
forall a. IntMap a -> Bool
I.null IntMap (Some k a)
m

-- | Number of elements in the map.
size :: Map k a -> Int
size :: forall k a. Map k a -> Int
size (Map IntMap (Some k a)
m) = (Some k a -> Int -> Int) -> Int -> IntMap (Some k a) -> Int
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr (Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (Int -> Int -> Int) -> (Some k a -> Int) -> Some k a -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Some k a -> Int
forall {k} {a}. Some k a -> Int
some_size) Int
0 IntMap (Some k a)
m
  where some_size :: Some k a -> Int
some_size (Only k
_ a
_) = Int
1
        some_size (More Map k a
s) = Map k a -> Int
forall k a. Map k a -> Int
M.size Map k a
s

-- | Is the key a member of the map?
member :: (Hashable k, Ord k) => k -> Map k a -> Bool
member :: forall k a. (Hashable k, Ord k) => k -> Map k a -> Bool
member k
k Map k a
m = case k -> Map k a -> Maybe a
forall k a. (Hashable k, Ord k) => k -> Map k a -> Maybe a
lookup k
k Map k a
m of
               Maybe a
Nothing -> Bool
False
               Just a
_  -> Bool
True

-- | Is the key not a member of the map?
notMember :: (Hashable k, Ord k) => k -> Map k a -> Bool
notMember :: forall k a. (Hashable k, Ord k) => k -> Map k a -> Bool
notMember k
k Map k a
m = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ k -> Map k a -> Bool
forall k a. (Hashable k, Ord k) => k -> Map k a -> Bool
member k
k Map k a
m

some_lookup :: Ord k => k -> Some k a -> Maybe a
some_lookup :: forall k a. Ord k => k -> Some k a -> Maybe a
some_lookup k
k (Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = a -> Maybe a
forall a. a -> Maybe a
Just a
x
                          | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
some_lookup k
k (More Map k a
s) = k -> Map k a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup k
k Map k a
s

-- | Lookup the value at a key in the map.
lookup :: (Hashable k, Ord k) => k -> Map k a -> Maybe a
lookup :: forall k a. (Hashable k, Ord k) => k -> Map k a -> Maybe a
lookup k
k (Map IntMap (Some k a)
m) = Int -> IntMap (Some k a) -> Maybe (Some k a)
forall a. Int -> IntMap a -> Maybe a
I.lookup (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m Maybe (Some k a) -> (Some k a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= k -> Some k a -> Maybe a
forall k a. Ord k => k -> Some k a -> Maybe a
some_lookup k
k

-- | The expression @('findWithDefault' def k map)@ returns the value at key
-- @k@ or returns @def@ when the key is not an element of the map.
findWithDefault :: (Hashable k, Ord k) => a -> k -> Map k a -> a
findWithDefault :: forall k a. (Hashable k, Ord k) => a -> k -> Map k a -> a
findWithDefault a
def k
k Map k a
m = case k -> Map k a -> Maybe a
forall k a. (Hashable k, Ord k) => k -> Map k a -> Maybe a
lookup k
k Map k a
m of
                            Maybe a
Nothing -> a
def
                            Just a
x  -> a
x


{--------------------------------------------------------------------
  Construction
--------------------------------------------------------------------}
-- | The empty map.
empty :: Map k a
empty :: forall k a. Map k a
empty = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map IntMap (Some k a)
forall a. IntMap a
I.empty

-- | A map of one element.
singleton :: Hashable k => k -> a -> Map k a
singleton :: forall k a. Hashable k => k -> a -> Map k a
singleton k
k a
x = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  Int -> Some k a -> IntMap (Some k a)
forall a. Int -> a -> IntMap a
I.singleton (k -> Int
forall a. Hashable a => a -> Int
hash k
k) (Some k a -> IntMap (Some k a)) -> Some k a -> IntMap (Some k a)
forall a b. (a -> b) -> a -> b
$ (k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x)


{--------------------------------------------------------------------
  Insert
--------------------------------------------------------------------}
-- | Insert a new key\/value pair in the map.  If the key is already present in
-- the map, the associated value is replaced with the supplied value, i.e.
-- 'insert' is equivalent to @'insertWith' 'const'@.
insert :: (Hashable k, Ord k)
       => k -> a -> Map k a -> Map k a
insert :: forall k a. (Hashable k, Ord k) => k -> a -> Map k a -> Map k a
insert k
k a
x (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> Int -> Some k a -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
I.insertWith Some k a -> Some k a -> Some k a
forall {p}. p -> Some k a -> Some k a
some_insert (k -> Int
forall a. Hashable a => a -> Int
hash k
k) (k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x) IntMap (Some k a)
m
  where some_insert :: p -> Some k a -> Some k a
some_insert p
_ (Only k
k' a
x') | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x
                                   | Bool
otherwise = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ 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
x (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k' a
x')
        some_insert p
_ (More Map k a
s) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ 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
x Map k a
s

-- | Insert with a combining function.  @'insertWith' f key value mp@ will
-- insert the pair (key, value) into @mp@ if key does not exist in the map. If
-- the key does exist, the function will insert @f new_value old_value@.
insertWith :: (Hashable k, Ord k)
           => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith :: forall k a.
(Hashable k, Ord k) =>
(a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith a -> a -> a
f k
k a
x (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> Int -> Some k a -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
I.insertWith Some k a -> Some k a -> Some k a
forall {p}. p -> Some k a -> Some k a
some_insert_with (k -> Int
forall a. Hashable a => a -> Int
hash k
k) (k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x) IntMap (Some k a)
m
  where some_insert_with :: p -> Some k a -> Some k a
some_insert_with p
_ (Only k
k' a
x') | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (a -> a -> a
f a
x a
x')
                                        | Bool
otherwise = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ 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
x (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k' a
x')
        some_insert_with p
_ (More Map k a
s) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith a -> a -> a
f k
k a
x Map k a
s

-- | Insert with a combining function.  @'insertWithKey' f key value mp@ will
-- insert the pair (key, value) into @mp@ if key does not exist in the map. If
-- the key does exist, the function will insert @f key new_value old_value@.
insertWithKey :: (Hashable k, Ord k)
              => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey k -> a -> a -> a
f k
k a
x (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> Int -> Some k a -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
I.insertWith Some k a -> Some k a -> Some k a
forall {p}. p -> Some k a -> Some k a
some_insert_with_key (k -> Int
forall a. Hashable a => a -> Int
hash k
k) (k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x) IntMap (Some k a)
m
  where some_insert_with_key :: p -> Some k a -> Some k a
some_insert_with_key p
_ (Only k
k' a
x') | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (k -> a -> a -> a
f k
k a
x a
x')
                                            | Bool
otherwise = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ 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
x (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k' a
x')
        some_insert_with_key p
_ (More Map k a
s) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some 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
x Map k a
s

-- | The expression (@'insertLookupWithKey' f k x map@) is a pair where the
-- first element is equal to (@'lookup' k map@) and the second element equal to
-- (@'insertWithKey' f k x map@).
insertLookupWithKey :: (Hashable k, Ord k)
                    => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
insertLookupWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)
insertLookupWithKey k -> a -> a -> a
f k
k a
x (Map IntMap (Some k a)
m) =
  case (Int -> Some k a -> Some k a -> Some k a)
-> Int
-> Some k a
-> IntMap (Some k a)
-> (Maybe (Some k a), IntMap (Some k a))
forall a.
(Int -> a -> a -> a) -> Int -> a -> IntMap a -> (Maybe a, IntMap a)
I.insertLookupWithKey Int -> Some k a -> Some k a -> Some k a
forall {p} {p}. p -> p -> Some k a -> Some k a
some_insert_with_key (k -> Int
forall a. Hashable a => a -> Int
hash k
k) (k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k a
x) IntMap (Some k a)
m of
    (Maybe (Some k a)
found, IntMap (Some k a)
m') -> (Maybe (Some k a)
found Maybe (Some k a) -> (Some k a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= k -> Some k a -> Maybe a
forall k a. Ord k => k -> Some k a -> Maybe a
some_lookup k
k, IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map IntMap (Some k a)
m')
  where some_insert_with_key :: p -> p -> Some k a -> Some k a
some_insert_with_key p
_ p
_ (Only k
k' a
x') | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (k -> a -> a -> a
f k
k a
x a
x')
                                              | Bool
otherwise = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ 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
x (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k' a
x')
        some_insert_with_key p
_ p
_ (More Map k a
s) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some 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
x Map k a
s


{--------------------------------------------------------------------
  Deletion
--------------------------------------------------------------------}

some_norm :: M.Map k v -> Maybe (Some k v)
some_norm :: forall k v. Map k v -> Maybe (Some k v)
some_norm Map k v
s = case Map k v -> Int
forall k a. Map k a -> Int
M.size Map k v
s of Int
0 -> Maybe (Some k v)
forall a. Maybe a
Nothing
                               Int
1 -> case Map k v -> (k, v)
forall k a. Map k a -> (k, a)
M.findMin Map k v
s of (k
k, v
x) -> Some k v -> Maybe (Some k v)
forall a. a -> Maybe a
Just (Some k v -> Maybe (Some k v)) -> Some k v -> Maybe (Some k v)
forall a b. (a -> b) -> a -> b
$ k -> v -> Some k v
forall k v. k -> v -> Some k v
Only k
k v
x
                               Int
_ -> Some k v -> Maybe (Some k v)
forall a. a -> Maybe a
Just (Some k v -> Maybe (Some k v)) -> Some k v -> Maybe (Some k v)
forall a b. (a -> b) -> a -> b
$ Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v
s

some_norm' :: M.Map k v -> Some k v
some_norm' :: forall k v. Map k v -> Some k v
some_norm' Map k v
s = case Map k v -> Int
forall k a. Map k a -> Int
M.size Map k v
s of Int
1 -> case Map k v -> (k, v)
forall k a. Map k a -> (k, a)
M.findMin Map k v
s of (k
k, v
x) -> k -> v -> Some k v
forall k v. k -> v -> Some k v
Only k
k v
x
                                Int
_ -> Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v
s

-- | Delete a key and its value from the map. When the key is not
-- a member of the map, the original map is returned.
delete :: (Hashable k, Ord k)
       => k -> Map k a -> Map k a
delete :: forall k a. (Hashable k, Ord k) => k -> Map k a -> Map k a
delete k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k a))
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> Maybe a) -> Int -> IntMap a -> IntMap a
I.update Some k a -> Maybe (Some k a)
forall {v}. Some k v -> Maybe (Some k v)
some_delete (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_delete :: Some k v -> Maybe (Some k v)
some_delete v :: Some k v
v@(Only k
k' v
_) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k'  = Maybe (Some k v)
forall a. Maybe a
Nothing
                                  | Bool
otherwise  = Some k v -> Maybe (Some k v)
forall a. a -> Maybe a
Just Some k v
v
        some_delete (More Map k v
t) = Map k v -> Maybe (Some k v)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k v -> Maybe (Some k v)) -> Map k v -> Maybe (Some k v)
forall a b. (a -> b) -> a -> b
$ k -> Map k v -> Map k v
forall k a. Ord k => k -> Map k a -> Map k a
M.delete k
k Map k v
t

-- | Adjust a value at a specific key. When the key is not a member of the map,
-- the original map is returned.
adjust :: (Hashable k, Ord k)
       => (a -> a) -> k -> Map k a -> Map k a
adjust :: forall k a.
(Hashable k, Ord k) =>
(a -> a) -> k -> Map k a -> Map k a
adjust a -> a
f k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a)
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a) -> Int -> IntMap a -> IntMap a
I.adjust Some k a -> Some k a
some_adjust (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_adjust :: Some k a -> Some k a
some_adjust v :: Some k a
v@(Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k'  = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (a -> a
f a
x)
                                  | Bool
otherwise  = Some k a
v
        some_adjust (More Map k a
t) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (a -> a) -> k -> Map k a -> Map k a
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
M.adjust a -> a
f k
k Map k a
t

-- | Adjust a value at a specific key. When the key is not a member of the map,
-- the original map is returned.
adjustWithKey :: (Hashable k, Ord k)
              => (k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey k -> a -> a
f k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a)
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a) -> Int -> IntMap a -> IntMap a
I.adjust Some k a -> Some k a
some_adjust_with_key (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_adjust_with_key :: Some k a -> Some k a
some_adjust_with_key v :: Some k a
v@(Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k'  = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (k -> a -> a
f k
k a
x)
                                           | Bool
otherwise  = Some k a
v
        some_adjust_with_key (More Map k a
t) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (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
t

-- | The expression (@'update' f k map@) updates the value @x@ at @k@ (if it is
-- in the map). If (@f x@) is 'Nothing', the element is deleted. If it is
-- (@'Just' y@), the key @k@ is bound to the new value @y@.
update :: (Hashable k, Ord k)
       => (a -> Maybe a) -> k -> Map k a -> Map k a
update :: forall k a.
(Hashable k, Ord k) =>
(a -> Maybe a) -> k -> Map k a -> Map k a
update a -> Maybe a
f k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k a))
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> Maybe a) -> Int -> IntMap a -> IntMap a
I.update Some k a -> Maybe (Some k a)
some_update (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_update :: Some k a -> Maybe (Some k a)
some_update v :: Some k a
v@(Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = a -> Maybe a
f a
x Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k'
                                  | Bool
otherwise = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
        some_update (More Map k a
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (a -> Maybe a) -> k -> Map k a -> Map k a
forall k a. Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
M.update a -> Maybe a
f k
k Map k a
t

-- | The expression (@'update' f k map@) updates the value @x@ at @k@ (if it is
-- in the map). If (@f k x@) is 'Nothing', the element is deleted. If it is
-- (@'Just' y@), the key @k@ is bound to the new value @y@.
updateWithKey :: (Hashable k, Ord k)
              => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey k -> a -> Maybe a
f k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k a))
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> Maybe a) -> Int -> IntMap a -> IntMap a
I.update Some k a -> Maybe (Some k a)
some_update_with_key (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_update_with_key :: Some k a -> Maybe (Some k a)
some_update_with_key v :: Some k a
v@(Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Maybe a
f k
k a
x Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k'
                                           | Bool
otherwise = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
        some_update_with_key (More Map k a
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (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
t

-- | Lookup and update.  The function returns original value, if it is updated.
-- This is different behavior than 'Data.Map.updateLookupWithKey'.  Returns the
-- original key value if the map entry is deleted.
updateLookupWithKey :: (Hashable k, Ord k)
                    => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
updateLookupWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
updateLookupWithKey k -> a -> Maybe a
f k
k (Map IntMap (Some k a)
m) =
  case (Int -> Some k a -> Maybe (Some k a))
-> Int
-> IntMap (Some k a)
-> (Maybe (Some k a), IntMap (Some k a))
forall a.
(Int -> a -> Maybe a) -> Int -> IntMap a -> (Maybe a, IntMap a)
I.updateLookupWithKey Int -> Some k a -> Maybe (Some k a)
forall {p}. p -> Some k a -> Maybe (Some k a)
some_update_with_key (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m of
    (Maybe (Some k a)
found, IntMap (Some k a)
m') -> (Maybe (Some k a)
found Maybe (Some k a) -> (Some k a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= k -> Some k a -> Maybe a
forall k a. Ord k => k -> Some k a -> Maybe a
some_lookup k
k, IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map IntMap (Some k a)
m')
  where some_update_with_key :: p -> Some k a -> Maybe (Some k a)
some_update_with_key p
_ v :: Some k a
v@(Only k
k' a
x) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = k -> a -> Maybe a
f k
k a
x Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k'
                                             | Bool
otherwise = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
        some_update_with_key p
_ (More Map k a
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (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
t

-- | The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence
-- thereof.  'alter' can be used to insert, delete, or update a value in an
-- 'Map'.
alter :: (Hashable k, Ord k)
      => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
alter :: forall k a.
(Hashable k, Ord k) =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
alter Maybe a -> Maybe a
f k
k (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Maybe (Some k a) -> Maybe (Some k a))
-> Int -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
I.alter Maybe (Some k a) -> Maybe (Some k a)
some_alter (k -> Int
forall a. Hashable a => a -> Int
hash k
k) IntMap (Some k a)
m
  where some_alter :: Maybe (Some k a) -> Maybe (Some k a)
some_alter Maybe (Some k a)
Nothing = Maybe a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k
        some_alter (Just v :: Some k a
v@(Only k
k' a
x)) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
k' = Maybe a -> Maybe a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
x) Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k'
                                        | Bool
otherwise = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
        some_alter (Just (More Map k a
t)) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (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
t


{--------------------------------------------------------------------
  Union
--------------------------------------------------------------------}
-- | The union of a list of maps.
unions :: Ord k => [Map k a] -> Map k a
unions :: forall k a. Ord k => [Map k a] -> Map k a
unions [Map k a]
xs = (Map k a -> Map k a -> Map k a) -> Map k a -> [Map k a] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
union Map k a
forall k a. Map k a
empty [Map k a]
xs

-- | The union of a list of maps, with a combining operation.
unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a
unionsWith :: forall k a. Ord k => (a -> a -> a) -> [Map k a] -> Map k a
unionsWith a -> a -> a
f [Map k a]
xs = (Map k a -> Map k a -> Map k a) -> Map k a -> [Map k a] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((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
unionWith a -> a -> a
f) Map k a
forall k a. Map k a
empty [Map k a]
xs

-- | The (left-biased) union of two maps.
-- It prefers the first map when duplicate keys are encountered,
-- i.e. (@'union' == 'unionWith' 'const'@).
union :: Ord k => Map k a -> Map k a -> Map k a
union :: forall k a. Ord k => Map k a -> Map k a -> Map k a
union (Map IntMap (Some k a)
m1) (Map IntMap (Some k a)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> IntMap (Some k a) -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
I.unionWith Some k a -> Some k a -> Some k a
forall {k} {v}. Ord k => Some k v -> Some k v -> Some k v
some_union IntMap (Some k a)
m1 IntMap (Some k a)
m2
  where some_union :: Some k v -> Some k v -> Some k v
some_union v :: Some k v
v@(Only k
k v
x) (Only k
l v
y) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = Some k v
v
                                           | Bool
otherwise = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (k -> v -> Map k v
forall k a. k -> a -> Map k a
M.singleton k
k v
x Map k v -> Map k v -> Map k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` k -> v -> Map k v
forall k a. k -> a -> Map k a
M.singleton k
l v
y)
        some_union (Only k
k v
x) (More Map k v
t) = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ k -> v -> Map k v
forall k a. k -> a -> Map k a
M.singleton k
k v
x Map k v -> Map k v -> Map k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` Map k v
t
        some_union (More Map k v
t) (Only k
k v
x) = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v
t Map k v -> Map k v -> Map k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` k -> v -> Map k v
forall k a. k -> a -> Map k a
M.singleton k
k v
x
        some_union (More Map k v
t) (More Map k v
u) = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v
t Map k v -> Map k v -> Map k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` Map k v
u

some_union_with_key :: Ord k => (k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
some_union_with_key :: forall k a.
Ord k =>
(k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
some_union_with_key k -> a -> a -> a
f (Only k
k a
x) (Only k
l a
y) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k (k -> a -> a -> a
f k
k a
x a
y)
                                            | Bool
otherwise = Map k a -> Some k a
forall k v. Map k v -> Some k v
More ((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 (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k a
x) (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
l a
y))
some_union_with_key k -> a -> a -> a
f (Only k
k a
x) (More Map k a
t) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (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 (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k a
x) Map k a
t
some_union_with_key k -> a -> a -> a
f (More Map k a
t) (Only k
k a
x) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (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
t (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k a
x)
some_union_with_key k -> a -> a -> a
f (More Map k a
t) (More Map k a
u) = Map k a -> Some k a
forall k v. Map k v -> Some k v
More (Map k a -> Some k a) -> Map k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (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
t Map k a
u

-- | The union with a combining function.
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith :: forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith a -> a -> a
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k a)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> IntMap (Some k a) -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
I.unionWith ((k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
some_union_with_key ((k -> a -> a -> a) -> Some k a -> Some k a -> Some k a)
-> (k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> k -> a -> a -> a
forall a b. a -> b -> a
const a -> a -> a
f) IntMap (Some k a)
m1 IntMap (Some k a)
m2

-- | The union with a combining function.
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey :: forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey k -> a -> a -> a
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k a)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k a -> Some k a)
-> IntMap (Some k a) -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
I.unionWith ((k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Some k a -> Some k a -> Some k a
some_union_with_key k -> a -> a -> a
f) IntMap (Some k a)
m1 IntMap (Some k a)
m2

{--------------------------------------------------------------------
  Difference
--------------------------------------------------------------------}
-- | Difference between two maps (based on keys).
difference :: Ord k => Map k a -> Map k b -> Map k a
difference :: forall k a b. Ord k => Map k a -> Map k b -> Map k a
difference (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Maybe (Some k a))
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k a)
forall a b. (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
I.differenceWith Some k a -> Some k b -> Maybe (Some k a)
forall {k} {v} {b}.
Ord k =>
Some k v -> Some k b -> Maybe (Some k v)
some_diff IntMap (Some k a)
m1 IntMap (Some k b)
m2
  where some_diff :: Some k v -> Some k b -> Maybe (Some k v)
some_diff v :: Some k v
v@(Only k
k v
_) (Only k
l b
_) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = Maybe (Some k v)
forall a. Maybe a
Nothing
                                          | Bool
otherwise = Some k v -> Maybe (Some k v)
forall a. a -> Maybe a
Just Some k v
v
        some_diff v :: Some k v
v@(Only k
k v
_) (More Map k b
t) | k
k k -> Map k b -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.member` Map k b
t = Maybe (Some k v)
forall a. Maybe a
Nothing
                                        | Bool
otherwise      = Some k v -> Maybe (Some k v)
forall a. a -> Maybe a
Just Some k v
v
        some_diff (More Map k v
t) (Only k
k b
_) = Map k v -> Maybe (Some k v)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k v -> Maybe (Some k v)) -> Map k v -> Maybe (Some k v)
forall a b. (a -> b) -> a -> b
$ k -> Map k v -> Map k v
forall k a. Ord k => k -> Map k a -> Map k a
M.delete k
k Map k v
t
        some_diff (More Map k v
t) (More Map k b
u) = Map k v -> Maybe (Some k v)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k v -> Maybe (Some k v)) -> Map k v -> Maybe (Some k v)
forall a b. (a -> b) -> a -> b
$ Map k v
t Map k v -> Map k b -> Map k v
forall k a b. Ord k => Map k a -> Map k b -> Map k a
`M.difference` Map k b
u

some_diff_with_key :: Ord k => (k -> a -> b -> Maybe a) -> Some k a -> Some k b -> Maybe (Some k a)
some_diff_with_key :: forall k a b.
Ord k =>
(k -> a -> b -> Maybe a)
-> Some k a -> Some k b -> Maybe (Some k a)
some_diff_with_key k -> a -> b -> Maybe a
f v :: Some k a
v@(Only k
k a
x) (Only k
l b
y) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = k -> a -> b -> Maybe a
f k
k a
x b
y Maybe a -> (a -> Maybe (Some k a)) -> Maybe (Some k a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k a -> Maybe (Some k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k a -> Maybe (Some k a))
-> (a -> Some k a) -> a -> Maybe (Some k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Some k a
forall k v. k -> v -> Some k v
Only k
k
                                             | Bool
otherwise = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
some_diff_with_key k -> a -> b -> Maybe a
f (Only k
k a
x) (More Map k b
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some 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 (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k a
x) Map k b
t
some_diff_with_key k -> a -> b -> Maybe a
f (More Map k a
t) (Only k
k b
x) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some 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
t (k -> b -> Map k b
forall k a. k -> a -> Map k a
M.singleton k
k b
x)
some_diff_with_key k -> a -> b -> Maybe a
f (More Map k a
t) (More Map k b
u) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some 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
t Map k b
u

-- | Difference with a combining function.
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWith :: forall k a b.
Ord k =>
(a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWith a -> b -> Maybe a
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Maybe (Some k a))
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k a)
forall a b. (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
I.differenceWith ((k -> a -> b -> Maybe a)
-> Some k a -> Some k b -> Maybe (Some k a)
forall k a b.
Ord k =>
(k -> a -> b -> Maybe a)
-> Some k a -> Some k b -> Maybe (Some k a)
some_diff_with_key ((k -> a -> b -> Maybe a)
 -> Some k a -> Some k b -> Maybe (Some k a))
-> (k -> a -> b -> Maybe a)
-> Some k a
-> Some k b
-> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (a -> b -> Maybe a) -> k -> a -> b -> Maybe a
forall a b. a -> b -> a
const a -> b -> Maybe a
f) IntMap (Some k a)
m1 IntMap (Some k b)
m2

-- | Difference with a combining function. When two equal keys are
-- encountered, the combining function is applied to the key and both values.
-- If it returns 'Nothing', the element is discarded (proper set difference).
-- If it returns (@'Just' y@), the element is updated with a new value @y@.
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWithKey :: forall k a b.
Ord k =>
(k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWithKey k -> a -> b -> Maybe a
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Maybe (Some k a))
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k a)
forall a b. (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
I.differenceWith ((k -> a -> b -> Maybe a)
-> Some k a -> Some k b -> Maybe (Some k a)
forall k a b.
Ord k =>
(k -> a -> b -> Maybe a)
-> Some k a -> Some k b -> Maybe (Some k a)
some_diff_with_key k -> a -> b -> Maybe a
f) IntMap (Some k a)
m1 IntMap (Some k b)
m2


{--------------------------------------------------------------------
  Intersection
--------------------------------------------------------------------}
delete_empty :: I.IntMap (Some k a) -> I.IntMap (Some k a)
delete_empty :: forall k a. IntMap (Some k a) -> IntMap (Some k a)
delete_empty = (Some k a -> Bool) -> IntMap (Some k a) -> IntMap (Some k a)
forall a. (a -> Bool) -> IntMap a -> IntMap a
I.filter Some k a -> Bool
forall {k} {a}. Some k a -> Bool
some_empty
  where some_empty :: Some k a -> Bool
some_empty (Only k
_ a
_) = Bool
True
        some_empty (More Map k a
t) = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Map k a -> Bool
forall k a. Map k a -> Bool
M.null Map k a
t

-- | The (left-biased) intersection of two maps (based on keys).
intersection :: Ord k => Map k a -> Map k b -> Map k a
intersection :: forall k a b. Ord k => Map k a -> Map k b -> Map k a
intersection (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$ IntMap (Some k a) -> IntMap (Some k a)
forall k a. IntMap (Some k a) -> IntMap (Some k a)
delete_empty (IntMap (Some k a) -> IntMap (Some k a))
-> IntMap (Some k a) -> IntMap (Some k a)
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Some k a)
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k a)
forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
I.intersectionWith Some k a -> Some k b -> Some k a
forall {k} {v} {b}. Ord k => Some k v -> Some k b -> Some k v
some_intersection IntMap (Some k a)
m1 IntMap (Some k b)
m2
  where some_intersection :: Some k v -> Some k b -> Some k v
some_intersection v :: Some k v
v@(Only k
k v
_) (Only k
l b
_) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = Some k v
v
                                                  | Bool
otherwise = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v
forall k a. Map k a
M.empty)
        some_intersection v :: Some k v
v@(Only k
k v
_) (More Map k b
t) | k
k k -> Map k b -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.member` Map k b
t = Some k v
v
                                                | Bool
otherwise      = Map k v -> Some k v
forall k v. Map k v -> Some k v
More (Map k v
forall k a. Map k a
M.empty)
        some_intersection (More Map k v
t) (Only k
k b
x) = Map k v -> Some k v
forall k v. Map k v -> Some k v
some_norm' (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v -> Map k b -> Map k v
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.intersection Map k v
t (k -> b -> Map k b
forall k a. k -> a -> Map k a
M.singleton k
k b
x)
        some_intersection (More Map k v
t) (More Map k b
u) = Map k v -> Some k v
forall k v. Map k v -> Some k v
some_norm' (Map k v -> Some k v) -> Map k v -> Some k v
forall a b. (a -> b) -> a -> b
$ Map k v -> Map k b -> Map k v
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.intersection Map k v
t Map k b
u

some_intersection_with_key :: Ord k => (k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
some_intersection_with_key :: forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
some_intersection_with_key k -> a -> b -> c
f (Only k
k a
x) (Only k
l b
y) | k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l  = k -> c -> Some k c
forall k v. k -> v -> Some k v
Only k
k (k -> a -> b -> c
f k
k a
x b
y)
                                                   | Bool
otherwise = Map k c -> Some k c
forall k v. Map k v -> Some k v
More (Map k c
forall k a. Map k a
M.empty)
some_intersection_with_key k -> a -> b -> c
f (Only k
k a
x) (More Map k b
t) = Map k c -> Some k c
forall k v. Map k v -> Some k v
some_norm' (Map k c -> Some k c) -> Map k c -> Some 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 (k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton k
k a
x) Map k b
t
some_intersection_with_key k -> a -> b -> c
f (More Map k a
t) (Only k
k b
x) = Map k c -> Some k c
forall k v. Map k v -> Some k v
some_norm' (Map k c -> Some k c) -> Map k c -> Some 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
t (k -> b -> Map k b
forall k a. k -> a -> Map k a
M.singleton k
k b
x)
some_intersection_with_key k -> a -> b -> c
f (More Map k a
t) (More Map k b
u) = Map k c -> Some k c
forall k v. Map k v -> Some k v
some_norm' (Map k c -> Some k c) -> Map k c -> Some 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
t Map k b
u

-- | The intersection with a combining function.
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith :: forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith a -> b -> c
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k c) -> Map k c
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k c) -> Map k c) -> IntMap (Some k c) -> Map k c
forall a b. (a -> b) -> a -> b
$ IntMap (Some k c) -> IntMap (Some k c)
forall k a. IntMap (Some k a) -> IntMap (Some k a)
delete_empty (IntMap (Some k c) -> IntMap (Some k c))
-> IntMap (Some k c) -> IntMap (Some k c)
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Some k c)
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k c)
forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
I.intersectionWith ((k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
some_intersection_with_key ((k -> a -> b -> c) -> Some k a -> Some k b -> Some k c)
-> (k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
forall a b. (a -> b) -> a -> b
$ (a -> b -> c) -> k -> a -> b -> c
forall a b. a -> b -> a
const a -> b -> c
f) IntMap (Some k a)
m1 IntMap (Some k b)
m2

-- | The intersection with a combining function.
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey :: forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWithKey k -> a -> b -> c
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) = IntMap (Some k c) -> Map k c
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k c) -> Map k c) -> IntMap (Some k c) -> Map k c
forall a b. (a -> b) -> a -> b
$ IntMap (Some k c) -> IntMap (Some k c)
forall k a. IntMap (Some k a) -> IntMap (Some k a)
delete_empty (IntMap (Some k c) -> IntMap (Some k c))
-> IntMap (Some k c) -> IntMap (Some k c)
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b -> Some k c)
-> IntMap (Some k a) -> IntMap (Some k b) -> IntMap (Some k c)
forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
I.intersectionWith ((k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Some k a -> Some k b -> Some k c
some_intersection_with_key k -> a -> b -> c
f) IntMap (Some k a)
m1 IntMap (Some k b)
m2


{--------------------------------------------------------------------
  Submap
--------------------------------------------------------------------}
-- | Is this a proper submap? (ie. a submap but not equal).
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool
isProperSubmapOf :: forall k a. (Ord k, Eq a) => Map k a -> Map k a -> Bool
isProperSubmapOf Map k a
m1 Map k a
m2 = Map k a -> Map k a -> Bool
forall k a. (Ord k, Eq a) => Map k a -> Map k a -> Bool
isSubmapOf Map k a
m1 Map k a
m2 Bool -> Bool -> Bool
&& Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m2

-- | Is this a proper submap? (ie. a submap but not equal).  The expression
-- (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when @m1@ and @m2@ are not
-- equal, all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
-- applied to their respective values.
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
isProperSubmapOfBy :: forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
isProperSubmapOfBy a -> b -> Bool
f Map k a
m1 Map k b
m2 = (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
isSubmapOfBy a -> b -> Bool
f Map k a
m1 Map k b
m2 Bool -> Bool -> Bool
&& Map k a -> Int
forall k a. Map k a -> Int
size Map k a
m1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Map k b -> Int
forall k a. Map k a -> Int
size Map k b
m2

-- | Is this a submap?
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool
isSubmapOf :: forall k a. (Ord k, Eq a) => Map k a -> Map k a -> Bool
isSubmapOf (Map IntMap (Some k a)
m1) (Map IntMap (Some k a)
m2) =
  (Some k a -> Some k a -> Bool)
-> IntMap (Some k a) -> IntMap (Some k a) -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
I.isSubmapOfBy Some k a -> Some k a -> Bool
forall {k} {a}. (Ord k, Eq a) => Some k a -> Some k a -> Bool
some_isSubmapOf IntMap (Some k a)
m1 IntMap (Some k a)
m2
  where some_isSubmapOf :: Some k a -> Some k a -> Bool
some_isSubmapOf (Only k
k a
_) (Only k
l a
_) = k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l
        some_isSubmapOf (Only k
k a
_) (More Map k a
t)   = k
k k -> Map k a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.member` Map k a
t
        some_isSubmapOf (More Map k a
_) (Only k
_ a
_)   = Bool
False
        some_isSubmapOf (More Map k a
t) (More Map k a
u)     = Map k a
t Map k a -> Map k a -> Bool
forall k a. (Ord k, Eq a) => Map k a -> Map k a -> Bool
`M.isSubmapOf` Map k a
u

-- | The expression (@'isSubmapOfBy' f m1 m2@) returns 'True' if all keys in
-- @m1@ are in @m2@, and when @f@ returns 'True' when applied to their
-- respective values.
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy :: forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy a -> b -> Bool
f (Map IntMap (Some k a)
m1) (Map IntMap (Some k b)
m2) =
  (Some k a -> Some k b -> Bool)
-> IntMap (Some k a) -> IntMap (Some k b) -> Bool
forall a b. (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
I.isSubmapOfBy Some k a -> Some k b -> Bool
forall {k}. Ord k => Some k a -> Some k b -> Bool
some_isSubmapOfBy IntMap (Some k a)
m1 IntMap (Some k b)
m2
  where some_isSubmapOfBy :: Some k a -> Some k b -> Bool
some_isSubmapOfBy (Only k
k a
x) (Only k
l b
y) = k
k k -> k -> Bool
forall a. Ord a => a -> a -> Bool
`eq` k
l Bool -> Bool -> Bool
&& a
x a -> b -> Bool
`f` b
y
        some_isSubmapOfBy (Only k
k a
x) (More Map k b
t) = 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
t of
                                                  Just b
y -> a -> b -> Bool
f a
x b
y
                                                  Maybe b
_      -> Bool
False
        some_isSubmapOfBy (More Map k a
_) (Only k
_ b
_) = Bool
False
        some_isSubmapOfBy (More Map k a
t) (More Map k b
u)   = (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
t Map k b
u


{--------------------------------------------------------------------
  Mapping
--------------------------------------------------------------------}
-- | Map a function over all values in the map.
map :: (a -> b) -> Map k a -> Map k b
map :: forall a b k. (a -> b) -> Map k a -> Map k b
map a -> b
f (Map IntMap (Some k a)
m) = IntMap (Some k b) -> Map k b
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k b) -> Map k b) -> IntMap (Some k b) -> Map k b
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b) -> IntMap (Some k a) -> IntMap (Some k b)
forall a b. (a -> b) -> IntMap a -> IntMap b
I.map Some k a -> Some k b
forall {k}. Some k a -> Some k b
some_map IntMap (Some k a)
m
  where some_map :: Some k a -> Some k b
some_map (Only k
k a
x) = k -> b -> Some k b
forall k v. k -> v -> Some k v
Only k
k (b -> Some k b) -> b -> Some k b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
x
        some_map (More Map k a
t)   = Map k b -> Some k b
forall k v. Map k v -> Some k v
More (Map k b -> Some k b) -> Map k b -> Some k b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> Map k a -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
M.map a -> b
f Map k a
t

-- | Map a function over all values in the map.
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey :: forall k a b. (k -> a -> b) -> Map k a -> Map k b
mapWithKey k -> a -> b
f (Map IntMap (Some k a)
m) = IntMap (Some k b) -> Map k b
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k b) -> Map k b) -> IntMap (Some k b) -> Map k b
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Some k b) -> IntMap (Some k a) -> IntMap (Some k b)
forall a b. (a -> b) -> IntMap a -> IntMap b
I.map Some k a -> Some k b
some_map_with_key IntMap (Some k a)
m
  where some_map_with_key :: Some k a -> Some k b
some_map_with_key (Only k
k a
x) = k -> b -> Some k b
forall k v. k -> v -> Some k v
Only k
k (b -> Some k b) -> b -> Some k b
forall a b. (a -> b) -> a -> b
$ k -> a -> b
f k
k a
x
        some_map_with_key (More Map k a
t)   = Map k b -> Some k b
forall k v. Map k v -> Some k v
More (Map k b -> Some k b) -> Map k b -> Some k b
forall a b. (a -> b) -> a -> b
$ (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
t

-- | The function @'mapAccum'@ threads an accumulating argument through the map
-- in unspecified order of keys.
mapAccum :: (a -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccum :: forall a b c k. (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccum a -> b -> (a, c)
f a
a (Map IntMap (Some k b)
m) =
  case (a -> Some k b -> (a, Some k c))
-> a -> IntMap (Some k b) -> (a, IntMap (Some k c))
forall a b c. (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
I.mapAccum a -> Some k b -> (a, Some k c)
forall {k}. a -> Some k b -> (a, Some k c)
some_map_accum a
a IntMap (Some k b)
m of
    (a
acc, IntMap (Some k c)
m') -> (a
acc, IntMap (Some k c) -> Map k c
forall k v. IntMap (Some k v) -> Map k v
Map IntMap (Some k c)
m')
  where some_map_accum :: a -> Some k b -> (a, Some k c)
some_map_accum a
acc (Only k
k b
x) = case a -> b -> (a, c)
f a
acc b
x of (a
acc', c
x') -> (a
acc', k -> c -> Some k c
forall k v. k -> v -> Some k v
Only k
k c
x')
        some_map_accum a
acc (More Map k b
t)   = case (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
forall a b c k. (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
M.mapAccum a -> b -> (a, c)
f a
acc Map k b
t of (a
acc', Map k c
t') -> (a
acc', Map k c -> Some k c
forall k v. Map k v -> Some k v
More Map k c
t')

-- | The function @'mapAccumWithKey'@ threads an accumulating argument through
-- the map in unspecified order of keys.
mapAccumWithKey :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
mapAccumWithKey :: forall a k b c.
(a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccumWithKey a -> k -> b -> (a, c)
f a
a (Map IntMap (Some k b)
m) =
  case (a -> Some k b -> (a, Some k c))
-> a -> IntMap (Some k b) -> (a, IntMap (Some k c))
forall a b c. (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
I.mapAccum a -> Some k b -> (a, Some k c)
some_map_accum_with_key a
a IntMap (Some k b)
m of
    (a
acc, IntMap (Some k c)
m') -> (a
acc, IntMap (Some k c) -> Map k c
forall k v. IntMap (Some k v) -> Map k v
Map IntMap (Some k c)
m')
  where some_map_accum_with_key :: a -> Some k b -> (a, Some k c)
some_map_accum_with_key a
acc (Only k
k b
x) = case a -> k -> b -> (a, c)
f a
acc k
k b
x of (a
acc', c
x') -> (a
acc', k -> c -> Some k c
forall k v. k -> v -> Some k v
Only k
k c
x')
        some_map_accum_with_key a
acc (More Map k b
t)   = case (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
acc Map k b
t of (a
acc', Map k c
t') -> (a
acc', Map k c -> Some k c
forall k v. Map k v -> Some k v
More Map k c
t')


{--------------------------------------------------------------------
  Filter
--------------------------------------------------------------------}
-- | Filter all values that satisfy some predicate.
filter :: Ord k => (a -> Bool) -> Map k a -> Map k a
filter :: forall k a. Ord k => (a -> Bool) -> Map k a -> Map k a
filter a -> Bool
p (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k a))
-> IntMap (Some k a) -> IntMap (Some k a)
forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
I.mapMaybe Some k a -> Maybe (Some k a)
forall {k}. Some k a -> Maybe (Some k a)
some_filter IntMap (Some k a)
m
  where some_filter :: Some k a -> Maybe (Some k a)
some_filter v :: Some k a
v@(Only k
_ a
x) | a -> Bool
p a
x       = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
                                 | Bool
otherwise = Maybe (Some k a)
forall a. Maybe a
Nothing
        some_filter (More Map k a
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Map k a -> Map k a
forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter a -> Bool
p Map k a
t

-- | Filter all keys\/values that satisfy some predicate.
filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey :: forall k a. Ord k => (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey k -> a -> Bool
p (Map IntMap (Some k a)
m) = IntMap (Some k a) -> Map k a
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k a) -> Map k a) -> IntMap (Some k a) -> Map k a
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k a))
-> IntMap (Some k a) -> IntMap (Some k a)
forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
I.mapMaybe Some k a -> Maybe (Some k a)
some_filter_with_key IntMap (Some k a)
m
  where some_filter_with_key :: Some k a -> Maybe (Some k a)
some_filter_with_key v :: Some k a
v@(Only k
k a
x) | k -> a -> Bool
p k
k a
x     = Some k a -> Maybe (Some k a)
forall a. a -> Maybe a
Just Some k a
v
                                          | Bool
otherwise = Maybe (Some k a)
forall a. Maybe a
Nothing
        some_filter_with_key (More Map k a
t) = Map k a -> Maybe (Some k a)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k a -> Maybe (Some k a)) -> Map k a -> Maybe (Some k a)
forall a b. (a -> b) -> a -> b
$ (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
p Map k a
t

-- | Partition the map according to some predicate. The first map contains all
-- elements that satisfy the predicate, the second all elements that fail the
-- predicate.
partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a, Map k a)
partition :: forall k a. Ord k => (a -> Bool) -> Map k a -> (Map k a, Map k a)
partition a -> Bool
p Map k a
m = ((a -> Bool) -> Map k a -> Map k a
forall k a. Ord k => (a -> Bool) -> Map k a -> Map k a
filter a -> Bool
p Map k a
m, (a -> Bool) -> Map k a -> Map k a
forall k a. Ord k => (a -> Bool) -> Map k a -> Map k a
filter (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p) Map k a
m)

-- | Partition the map according to some predicate. The first map contains all
-- elements that satisfy the predicate, the second all elements that fail the
-- predicate.
partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
partitionWithKey :: forall k a.
Ord k =>
(k -> a -> Bool) -> Map k a -> (Map k a, Map k a)
partitionWithKey k -> a -> Bool
p Map k a
m = ((k -> a -> Bool) -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey k -> a -> Bool
p Map k a
m, (k -> a -> Bool) -> Map k a -> Map k a
forall k a. Ord k => (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey (\k
k -> Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Bool
p k
k) Map k a
m)

-- | Map values and collect the 'Just' results.
mapMaybe :: Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe :: forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe a -> Maybe b
f (Map IntMap (Some k a)
m) = IntMap (Some k b) -> Map k b
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k b) -> Map k b) -> IntMap (Some k b) -> Map k b
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k b))
-> IntMap (Some k a) -> IntMap (Some k b)
forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
I.mapMaybe Some k a -> Maybe (Some k b)
forall {k}. Some k a -> Maybe (Some k b)
some_map_maybe IntMap (Some k a)
m
  where some_map_maybe :: Some k a -> Maybe (Some k b)
some_map_maybe (Only k
k a
x) = a -> Maybe b
f a
x Maybe b -> (b -> Maybe (Some k b)) -> Maybe (Some k b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k b -> Maybe (Some k b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k b -> Maybe (Some k b))
-> (b -> Some k b) -> b -> Maybe (Some k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> b -> Some k b
forall k v. k -> v -> Some k v
Only k
k
        some_map_maybe (More Map k a
t) = Map k b -> Maybe (Some k b)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k b -> Maybe (Some k b)) -> Map k b -> Maybe (Some k b)
forall a b. (a -> b) -> a -> b
$ (a -> Maybe b) -> Map k a -> Map k b
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
M.mapMaybe a -> Maybe b
f Map k a
t

-- | Map keys\/values and collect the 'Just' results.
mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey :: forall k a b. Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f (Map IntMap (Some k a)
m) = IntMap (Some k b) -> Map k b
forall k v. IntMap (Some k v) -> Map k v
Map (IntMap (Some k b) -> Map k b) -> IntMap (Some k b) -> Map k b
forall a b. (a -> b) -> a -> b
$
  (Some k a -> Maybe (Some k b))
-> IntMap (Some k a) -> IntMap (Some k b)
forall a b. (a -> Maybe b) -> IntMap a -> IntMap b
I.mapMaybe Some k a -> Maybe (Some k b)
some_map_maybe_with_key IntMap (Some k a)
m
  where some_map_maybe_with_key :: Some k a -> Maybe (Some k b)
some_map_maybe_with_key (Only k
k a
x) = k -> a -> Maybe b
f k
k a
x Maybe b -> (b -> Maybe (Some k b)) -> Maybe (Some k b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Some k b -> Maybe (Some k b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Some k b -> Maybe (Some k b))
-> (b -> Some k b) -> b -> Maybe (Some k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> b -> Some k b
forall k v. k -> v -> Some k v
Only k
k
        some_map_maybe_with_key (More Map k a
t) = Map k b -> Maybe (Some k b)
forall k v. Map k v -> Maybe (Some k v)
some_norm (Map k b -> Maybe (Some k b)) -> Map k b -> Maybe (Some 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
t

-- | Map values and separate the 'Left' and 'Right' results.
mapEither :: Ord k => (a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEither :: forall k a b c.
Ord k =>
(a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEither a -> Either b c
f Map k a
m = ((a -> Maybe b) -> Map k a -> Map k b
forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe (Either b c -> Maybe b
forall a b. Either a b -> Maybe a
maybe_left (Either b c -> Maybe b) -> (a -> Either b c) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either b c
f) Map k a
m, (a -> Maybe c) -> Map k a -> Map k c
forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe (Either b c -> Maybe c
forall a b. Either a b -> Maybe b
maybe_right (Either b c -> Maybe c) -> (a -> Either b c) -> a -> Maybe c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either b c
f) Map k a
m)

-- | Map keys\/values and separate the 'Left' and 'Right' results.
mapEitherWithKey :: Ord k => (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEitherWithKey :: forall k a b c.
Ord k =>
(k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
mapEitherWithKey k -> a -> Either b c
f Map k a
m = ((k -> a -> Maybe b) -> Map k a -> Map k b
forall k a b. Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey (\k
k a
a -> Either b c -> Maybe b
forall a b. Either a b -> Maybe a
maybe_left  (k -> a -> Either b c
f k
k a
a)) Map k a
m
                       ,(k -> a -> Maybe c) -> Map k a -> Map k c
forall k a b. Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey (\k
k a
a -> Either b c -> Maybe c
forall a b. Either a b -> Maybe b
maybe_right (k -> a -> Either b c
f k
k a
a)) Map k a
m)

-- Helper functions for this section
maybe_left :: Either a b -> Maybe a
maybe_left :: forall a b. Either a b -> Maybe a
maybe_left (Left a
a) = a -> Maybe a
forall a. a -> Maybe a
Just a
a
maybe_left (Right b
_) = Maybe a
forall a. Maybe a
Nothing

maybe_right :: Either a b -> Maybe b
maybe_right :: forall a b. Either a b -> Maybe b
maybe_right (Right b
b) = b -> Maybe b
forall a. a -> Maybe a
Just b
b
maybe_right (Left a
_) = Maybe b
forall a. Maybe a
Nothing


{--------------------------------------------------------------------
  Fold
--------------------------------------------------------------------}
-- | Fold the values in the map, such that @'fold' f z == 'Prelude.foldr'
-- f z . 'elems'@.
fold :: (a -> b -> b) -> b -> Map k a -> b
fold :: forall a b k. (a -> b -> b) -> b -> Map k a -> b
fold a -> b -> b
f b
z (Map IntMap (Some k a)
m) = (Some k a -> b -> b) -> b -> IntMap (Some k a) -> b
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr Some k a -> b -> b
forall {k}. Some k a -> b -> b
some_fold b
z IntMap (Some k a)
m
  where some_fold :: Some k a -> b -> b
some_fold (Only k
_ a
x) b
y = a -> b -> b
f a
x b
y
        some_fold (More Map k a
t) b
y   = (a -> b -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> b) -> b -> Map k a -> b
mfoldr a -> b -> b
f b
y Map k a
t

-- | Fold the keys and values in the map, such that @'foldWithKey' f z ==
-- 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@.
foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
foldWithKey :: forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
foldWithKey k -> a -> b -> b
f b
z (Map IntMap (Some k a)
m) = (Some k a -> b -> b) -> b -> IntMap (Some k a) -> b
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr Some k a -> b -> b
some_fold_with_key b
z IntMap (Some k a)
m
  where some_fold_with_key :: Some k a -> b -> b
some_fold_with_key (Only k
k a
x) b
y = k -> a -> b -> b
f k
k a
x b
y
        some_fold_with_key (More Map k a
t) 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
y Map k a
t

mfoldr :: (a -> b -> b) -> b -> M.Map k a -> b
ifoldr :: (a -> b -> b) -> b -> I.IntMap a -> b
#if MIN_VERSION_containers(0,5,0)
mfoldr :: forall a b k. (a -> b -> b) -> b -> Map k a -> b
mfoldr = (a -> b -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr
ifoldr :: forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr = (a -> b -> b) -> b -> IntMap a -> b
forall a b. (a -> b -> b) -> b -> IntMap a -> b
I.foldr
#else
mfoldr = M.fold
ifoldr = I.fold
#endif

{--------------------------------------------------------------------
  List variations
--------------------------------------------------------------------}
-- | Return all elements of the map in arbitrary order of their keys.
elems :: Map k a -> [a]
elems :: forall k a. Map k a -> [a]
elems (Map IntMap (Some k a)
m) = (Some k a -> [a] -> [a]) -> [a] -> IntMap (Some k a) -> [a]
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr Some k a -> [a] -> [a]
forall {k} {a}. Some k a -> [a] -> [a]
some_append_elems [] IntMap (Some k a)
m
  where some_append_elems :: Some k a -> [a] -> [a]
some_append_elems (Only k
_ a
x) [a]
acc = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc
        some_append_elems (More Map k a
t) [a]
acc   = Map k a -> [a]
forall k a. Map k a -> [a]
M.elems Map k a
t [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
acc

-- | Return all keys of the map in arbitrary order.
keys  :: Map k a -> [k]
keys :: forall k a. Map k a -> [k]
keys (Map IntMap (Some k a)
m) = (Some k a -> [k] -> [k]) -> [k] -> IntMap (Some k a) -> [k]
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr Some k a -> [k] -> [k]
forall {a} {a}. Some a a -> [a] -> [a]
some_append_keys [] IntMap (Some k a)
m
  where some_append_keys :: Some a a -> [a] -> [a]
some_append_keys (Only a
k a
_) [a]
acc = a
k a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc
        some_append_keys (More Map a a
t) [a]
acc   = Map a a -> [a]
forall k a. Map k a -> [k]
M.keys Map a a
t [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
acc

-- | The set of all keys of the map.
keysSet :: Ord k => Map k a -> S.Set k
keysSet :: forall k a. Ord k => Map k a -> Set k
keysSet (Map IntMap (Some k a)
m) = (Some k a -> Set k -> Set k) -> Set k -> IntMap (Some k a) -> Set k
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr (Set k -> Set k -> Set k
forall a. Ord a => Set a -> Set a -> Set a
S.union (Set k -> Set k -> Set k)
-> (Some k a -> Set k) -> Some k a -> Set k -> Set k
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Some k a -> Set k
forall {k} {a}. Some k a -> Set k
some_keys_set) Set k
forall a. Set a
S.empty IntMap (Some k a)
m
  where some_keys_set :: Some k a -> Set k
some_keys_set (Only k
k a
_) = k -> Set k
forall a. a -> Set a
S.singleton k
k
        some_keys_set (More Map k a
t)   = Map k a -> Set k
forall k a. Map k a -> Set k
M.keysSet Map k a
t

-- | Return all key\/value pairs in the map in arbitrary key order.
assocs :: Map k a -> [(k,a)]
assocs :: forall k a. Map k a -> [(k, a)]
assocs = Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
toList


{--------------------------------------------------------------------
  Lists
--------------------------------------------------------------------}
-- | Convert the map to a list of key\/value pairs.
toList :: Map k a -> [(k,a)]
toList :: forall k a. Map k a -> [(k, a)]
toList (Map IntMap (Some k a)
m) =
  (Some k a -> [(k, a)] -> [(k, a)])
-> [(k, a)] -> IntMap (Some k a) -> [(k, a)]
forall a b. (a -> b -> b) -> b -> IntMap a -> b
ifoldr Some k a -> [(k, a)] -> [(k, a)]
forall {k} {a}. Some k a -> [(k, a)] -> [(k, a)]
some_append [] IntMap (Some k a)
m
  where some_append :: Some k a -> [(k, a)] -> [(k, a)]
some_append (Only k
k a
x) [(k, a)]
acc = (k
k, a
x) (k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
: [(k, a)]
acc
        some_append (More Map k a
t) [(k, a)]
acc   = Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
M.toList Map k a
t [(k, a)] -> [(k, a)] -> [(k, a)]
forall a. [a] -> [a] -> [a]
++ [(k, a)]
acc

-- | Create a map from a list of key\/value pairs.
fromList :: (Hashable k, Ord k)
         => [(k,a)] -> Map k a
fromList :: forall k a. (Hashable k, Ord k) => [(k, a)] -> Map k a
fromList [(k, a)]
xs = (Map k a -> (k, a) -> Map k a) -> Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Map k a
m (k
k, a
x) -> k -> a -> Map k a -> Map k a
forall k a. (Hashable k, Ord k) => k -> a -> Map k a -> Map k a
insert k
k a
x Map k a
m) Map k a
forall k a. Map k a
empty [(k, a)]
xs

-- | Create a map from a list of key\/value pairs with a combining function.
fromListWith :: (Hashable k, Ord k) => (a -> a -> a) -> [(k,a)] -> Map k a
fromListWith :: forall k a.
(Hashable k, Ord k) =>
(a -> a -> a) -> [(k, a)] -> Map k a
fromListWith a -> a -> a
f [(k, a)]
xs = (Map k a -> (k, a) -> Map k a) -> Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Map k a
m (k
k, a
x) -> (a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
(Hashable k, Ord k) =>
(a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith a -> a -> a
f k
k a
x Map k a
m) Map k a
forall k a. Map k a
empty [(k, a)]
xs

-- | Build a map from a list of key\/value pairs with a combining function.
fromListWithKey :: (Hashable k, Ord k) => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromListWithKey :: forall k a.
(Hashable k, Ord k) =>
(k -> a -> a -> a) -> [(k, a)] -> Map k a
fromListWithKey k -> a -> a -> a
f [(k, a)]
xs = (Map k a -> (k, a) -> Map k a) -> Map k a -> [(k, a)] -> Map k a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Map k a
m (k
k, a
x) -> (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
forall k a.
(Hashable k, Ord k) =>
(k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey k -> a -> a -> a
f k
k a
x Map k a
m) Map k a
forall k a. Map k a
empty [(k, a)]
xs