{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# OPTIONS_GHC -Wall #-}

-- Like 'UniqFM', these are maps for keys which are Uniquable.
-- Unlike 'UniqFM', these maps also remember their keys, which
-- makes them a much better drop in replacement for 'Data.Map.Map'.
--
-- Key preservation is right-biased.
module UniqMap (
    UniqMap,
    emptyUniqMap,
    isNullUniqMap,
    unitUniqMap,
    listToUniqMap,
    listToUniqMap_C,
    addToUniqMap,
    addListToUniqMap,
    addToUniqMap_C,
    addToUniqMap_Acc,
    alterUniqMap,
    addListToUniqMap_C,
    adjustUniqMap,
    delFromUniqMap,
    delListFromUniqMap,
    plusUniqMap,
    plusUniqMap_C,
    plusMaybeUniqMap_C,
    plusUniqMapList,
    minusUniqMap,
    intersectUniqMap,
    disjointUniqMap,
    mapUniqMap,
    filterUniqMap,
    partitionUniqMap,
    sizeUniqMap,
    elemUniqMap,
    lookupUniqMap,
    lookupWithDefaultUniqMap,
    anyUniqMap,
    allUniqMap,
    -- Non-deterministic functions omitted
) where

import GhcPrelude

import UniqFM

import Unique
import Outputable

import Data.Semigroup as Semi ( Semigroup(..) )
import Data.Coerce
import Data.Maybe
import Data.Data

-- | Maps indexed by 'Uniquable' keys
newtype UniqMap k a = UniqMap (UniqFM (k, a))
    deriving (Typeable (UniqMap k a)
DataType
Constr
Typeable (UniqMap k a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (UniqMap k a))
-> (UniqMap k a -> Constr)
-> (UniqMap k a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (UniqMap k a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (UniqMap k a)))
-> ((forall b. Data b => b -> b) -> UniqMap k a -> UniqMap k a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r)
-> (forall u. (forall d. Data d => d -> u) -> UniqMap k a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> UniqMap k a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a))
-> Data (UniqMap k a)
UniqMap k a -> DataType
UniqMap k a -> Constr
(forall b. Data b => b -> b) -> UniqMap k a -> UniqMap k a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (UniqMap k a)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (UniqMap k a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> UniqMap k a -> u
forall u. (forall d. Data d => d -> u) -> UniqMap k a -> [u]
forall k a. (Data k, Data a) => Typeable (UniqMap k a)
forall k a. (Data k, Data a) => UniqMap k a -> DataType
forall k a. (Data k, Data a) => UniqMap k a -> Constr
forall k a.
(Data k, Data a) =>
(forall b. Data b => b -> b) -> UniqMap k a -> UniqMap k a
forall k a u.
(Data k, Data a) =>
Int -> (forall d. Data d => d -> u) -> UniqMap k a -> u
forall k a u.
(Data k, Data a) =>
(forall d. Data d => d -> u) -> UniqMap k a -> [u]
forall k a r r'.
(Data k, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
forall k a r r'.
(Data k, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
forall k a (m :: * -> *).
(Data k, Data a, Monad m) =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
forall k a (m :: * -> *).
(Data k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
forall k a (c :: * -> *).
(Data k, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (UniqMap k a)
forall k a (c :: * -> *).
(Data k, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a)
forall k a (t :: * -> *) (c :: * -> *).
(Data k, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (UniqMap k a))
forall k a (t :: * -> * -> *) (c :: * -> *).
(Data k, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (UniqMap k a))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (UniqMap k a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (UniqMap k a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (UniqMap k a))
$cUniqMap :: Constr
$tUniqMap :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
$cgmapMo :: forall k a (m :: * -> *).
(Data k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
gmapMp :: (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
$cgmapMp :: forall k a (m :: * -> *).
(Data k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
gmapM :: (forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
$cgmapM :: forall k a (m :: * -> *).
(Data k, Data a, Monad m) =>
(forall d. Data d => d -> m d) -> UniqMap k a -> m (UniqMap k a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> UniqMap k a -> u
$cgmapQi :: forall k a u.
(Data k, Data a) =>
Int -> (forall d. Data d => d -> u) -> UniqMap k a -> u
gmapQ :: (forall d. Data d => d -> u) -> UniqMap k a -> [u]
$cgmapQ :: forall k a u.
(Data k, Data a) =>
(forall d. Data d => d -> u) -> UniqMap k a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
$cgmapQr :: forall k a r r'.
(Data k, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
$cgmapQl :: forall k a r r'.
(Data k, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UniqMap k a -> r
gmapT :: (forall b. Data b => b -> b) -> UniqMap k a -> UniqMap k a
$cgmapT :: forall k a.
(Data k, Data a) =>
(forall b. Data b => b -> b) -> UniqMap k a -> UniqMap k a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (UniqMap k a))
$cdataCast2 :: forall k a (t :: * -> * -> *) (c :: * -> *).
(Data k, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (UniqMap k a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (UniqMap k a))
$cdataCast1 :: forall k a (t :: * -> *) (c :: * -> *).
(Data k, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (UniqMap k a))
dataTypeOf :: UniqMap k a -> DataType
$cdataTypeOf :: forall k a. (Data k, Data a) => UniqMap k a -> DataType
toConstr :: UniqMap k a -> Constr
$ctoConstr :: forall k a. (Data k, Data a) => UniqMap k a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (UniqMap k a)
$cgunfold :: forall k a (c :: * -> *).
(Data k, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (UniqMap k a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a)
$cgfoldl :: forall k a (c :: * -> *).
(Data k, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> UniqMap k a -> c (UniqMap k a)
$cp1Data :: forall k a. (Data k, Data a) => Typeable (UniqMap k a)
Data, UniqMap k a -> UniqMap k a -> Bool
(UniqMap k a -> UniqMap k a -> Bool)
-> (UniqMap k a -> UniqMap k a -> Bool) -> Eq (UniqMap k a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k a. (Eq k, Eq a) => UniqMap k a -> UniqMap k a -> Bool
/= :: UniqMap k a -> UniqMap k a -> Bool
$c/= :: forall k a. (Eq k, Eq a) => UniqMap k a -> UniqMap k a -> Bool
== :: UniqMap k a -> UniqMap k a -> Bool
$c== :: forall k a. (Eq k, Eq a) => UniqMap k a -> UniqMap k a -> Bool
Eq, a -> UniqMap k b -> UniqMap k a
(a -> b) -> UniqMap k a -> UniqMap k b
(forall a b. (a -> b) -> UniqMap k a -> UniqMap k b)
-> (forall a b. a -> UniqMap k b -> UniqMap k a)
-> Functor (UniqMap k)
forall a b. a -> UniqMap k b -> UniqMap k a
forall a b. (a -> b) -> UniqMap k a -> UniqMap k b
forall k a b. a -> UniqMap k b -> UniqMap k a
forall k a b. (a -> b) -> UniqMap k a -> UniqMap k b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> UniqMap k b -> UniqMap k a
$c<$ :: forall k a b. a -> UniqMap k b -> UniqMap k a
fmap :: (a -> b) -> UniqMap k a -> UniqMap k b
$cfmap :: forall k a b. (a -> b) -> UniqMap k a -> UniqMap k b
Functor)
type role UniqMap nominal representational

instance Semigroup (UniqMap k a) where
  <> :: UniqMap k a -> UniqMap k a -> UniqMap k a
(<>) = UniqMap k a -> UniqMap k a -> UniqMap k a
forall k a. UniqMap k a -> UniqMap k a -> UniqMap k a
plusUniqMap

instance Monoid (UniqMap k a) where
    mempty :: UniqMap k a
mempty = UniqMap k a
forall k a. UniqMap k a
emptyUniqMap
    mappend :: UniqMap k a -> UniqMap k a -> UniqMap k a
mappend = UniqMap k a -> UniqMap k a -> UniqMap k a
forall a. Semigroup a => a -> a -> a
(Semi.<>)

instance (Outputable k, Outputable a) => Outputable (UniqMap k a) where
    ppr :: UniqMap k a -> SDoc
ppr (UniqMap UniqFM (k, a)
m) =
        SDoc -> SDoc
brackets (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
fsep ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ([SDoc] -> [SDoc]) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> a -> b
$
        [ k -> SDoc
forall a. Outputable a => a -> SDoc
ppr k
k SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"->" SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
v
        | (k
k, a
v) <- UniqFM (k, a) -> [(k, a)]
forall elt. UniqFM elt -> [elt]
eltsUFM UniqFM (k, a)
m ]

liftC :: (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC :: (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC a -> a -> a
f (k
_, a
v) (k
k', a
v') = (k
k', a -> a -> a
f a
v a
v')

emptyUniqMap :: UniqMap k a
emptyUniqMap :: UniqMap k a
emptyUniqMap = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap UniqFM (k, a)
forall elt. UniqFM elt
emptyUFM

isNullUniqMap :: UniqMap k a -> Bool
isNullUniqMap :: UniqMap k a -> Bool
isNullUniqMap (UniqMap UniqFM (k, a)
m) = UniqFM (k, a) -> Bool
forall elt. UniqFM elt -> Bool
isNullUFM UniqFM (k, a)
m

unitUniqMap :: Uniquable k => k -> a -> UniqMap k a
unitUniqMap :: k -> a -> UniqMap k a
unitUniqMap k
k a
v = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (k -> (k, a) -> UniqFM (k, a)
forall key elt. Uniquable key => key -> elt -> UniqFM elt
unitUFM k
k (k
k, a
v))

listToUniqMap :: Uniquable k => [(k,a)] -> UniqMap k a
listToUniqMap :: [(k, a)] -> UniqMap k a
listToUniqMap [(k, a)]
kvs = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap ([(k, (k, a))] -> UniqFM (k, a)
forall key elt. Uniquable key => [(key, elt)] -> UniqFM elt
listToUFM [ (k
k,(k
k,a
v)) | (k
k,a
v) <- [(k, a)]
kvs])

listToUniqMap_C :: Uniquable k => (a -> a -> a) -> [(k,a)] -> UniqMap k a
listToUniqMap_C :: (a -> a -> a) -> [(k, a)] -> UniqMap k a
listToUniqMap_C a -> a -> a
f [(k, a)]
kvs = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a) -> (k, a)) -> [(k, (k, a))] -> UniqFM (k, a)
forall key elt.
Uniquable key =>
(elt -> elt -> elt) -> [(key, elt)] -> UniqFM elt
listToUFM_C ((a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
forall a k. (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC a -> a -> a
f) [ (k
k,(k
k,a
v)) | (k
k,a
v) <- [(k, a)]
kvs]

addToUniqMap :: Uniquable k => UniqMap k a -> k -> a -> UniqMap k a
addToUniqMap :: UniqMap k a -> k -> a -> UniqMap k a
addToUniqMap (UniqMap UniqFM (k, a)
m) k
k a
v = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> k -> (k, a) -> UniqFM (k, a)
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM UniqFM (k, a)
m k
k (k
k, a
v)

addListToUniqMap :: Uniquable k => UniqMap k a -> [(k,a)] -> UniqMap k a
addListToUniqMap :: UniqMap k a -> [(k, a)] -> UniqMap k a
addListToUniqMap (UniqMap UniqFM (k, a)
m) [(k, a)]
kvs = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    UniqFM (k, a) -> [(k, (k, a))] -> UniqFM (k, a)
forall key elt.
Uniquable key =>
UniqFM elt -> [(key, elt)] -> UniqFM elt
addListToUFM UniqFM (k, a)
m [(k
k,(k
k,a
v)) | (k
k,a
v) <- [(k, a)]
kvs]

addToUniqMap_C :: Uniquable k
               => (a -> a -> a)
               -> UniqMap k a
               -> k
               -> a
               -> UniqMap k a
addToUniqMap_C :: (a -> a -> a) -> UniqMap k a -> k -> a -> UniqMap k a
addToUniqMap_C a -> a -> a
f (UniqMap UniqFM (k, a)
m) k
k a
v = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a) -> (k, a))
-> UniqFM (k, a) -> k -> (k, a) -> UniqFM (k, a)
forall key elt.
Uniquable key =>
(elt -> elt -> elt) -> UniqFM elt -> key -> elt -> UniqFM elt
addToUFM_C ((a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
forall a k. (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC a -> a -> a
f) UniqFM (k, a)
m k
k (k
k, a
v)

addToUniqMap_Acc :: Uniquable k
                 => (b -> a -> a)
                 -> (b -> a)
                 -> UniqMap k a
                 -> k
                 -> b
                 -> UniqMap k a
addToUniqMap_Acc :: (b -> a -> a) -> (b -> a) -> UniqMap k a -> k -> b -> UniqMap k a
addToUniqMap_Acc b -> a -> a
exi b -> a
new (UniqMap UniqFM (k, a)
m) k
k0 b
v0 = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    (b -> (k, a) -> (k, a))
-> (b -> (k, a)) -> UniqFM (k, a) -> k -> b -> UniqFM (k, a)
forall key elt elts.
Uniquable key =>
(elt -> elts -> elts)
-> (elt -> elts) -> UniqFM elts -> key -> elt -> UniqFM elts
addToUFM_Acc (\b
b (k
k, a
v) -> (k
k, b -> a -> a
exi b
b a
v))
                 (\b
b -> (k
k0, b -> a
new b
b))
                 UniqFM (k, a)
m k
k0 b
v0

alterUniqMap :: Uniquable k
             => (Maybe a -> Maybe a)
             -> UniqMap k a
             -> k
             -> UniqMap k a
alterUniqMap :: (Maybe a -> Maybe a) -> UniqMap k a -> k -> UniqMap k a
alterUniqMap Maybe a -> Maybe a
f (UniqMap UniqFM (k, a)
m) k
k = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    (Maybe (k, a) -> Maybe (k, a))
-> UniqFM (k, a) -> k -> UniqFM (k, a)
forall key elt.
Uniquable key =>
(Maybe elt -> Maybe elt) -> UniqFM elt -> key -> UniqFM elt
alterUFM ((a -> (k, a)) -> Maybe a -> Maybe (k, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k
k,) (Maybe a -> Maybe (k, a))
-> (Maybe (k, a) -> Maybe a) -> Maybe (k, a) -> Maybe (k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
f (Maybe a -> Maybe a)
-> (Maybe (k, a) -> Maybe a) -> Maybe (k, a) -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> a) -> Maybe (k, a) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> a
forall a b. (a, b) -> b
snd) UniqFM (k, a)
m k
k

addListToUniqMap_C
    :: Uniquable k
    => (a -> a -> a)
    -> UniqMap k a
    -> [(k, a)]
    -> UniqMap k a
addListToUniqMap_C :: (a -> a -> a) -> UniqMap k a -> [(k, a)] -> UniqMap k a
addListToUniqMap_C a -> a -> a
f (UniqMap UniqFM (k, a)
m) [(k, a)]
kvs = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a) -> (k, a))
-> UniqFM (k, a) -> [(k, (k, a))] -> UniqFM (k, a)
forall key elt.
Uniquable key =>
(elt -> elt -> elt) -> UniqFM elt -> [(key, elt)] -> UniqFM elt
addListToUFM_C ((a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
forall a k. (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC a -> a -> a
f) UniqFM (k, a)
m
        [(k
k,(k
k,a
v)) | (k
k,a
v) <- [(k, a)]
kvs]

adjustUniqMap
    :: Uniquable k
    => (a -> a)
    -> UniqMap k a
    -> k
    -> UniqMap k a
adjustUniqMap :: (a -> a) -> UniqMap k a -> k -> UniqMap k a
adjustUniqMap a -> a
f (UniqMap UniqFM (k, a)
m) k
k = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a)) -> UniqFM (k, a) -> k -> UniqFM (k, a)
forall key elt.
Uniquable key =>
(elt -> elt) -> UniqFM elt -> key -> UniqFM elt
adjustUFM (\(k
_,a
v) -> (k
k,a -> a
f a
v)) UniqFM (k, a)
m k
k

delFromUniqMap :: Uniquable k => UniqMap k a -> k -> UniqMap k a
delFromUniqMap :: UniqMap k a -> k -> UniqMap k a
delFromUniqMap (UniqMap UniqFM (k, a)
m) k
k = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> k -> UniqFM (k, a)
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM UniqFM (k, a)
m k
k

delListFromUniqMap :: Uniquable k => UniqMap k a -> [k] -> UniqMap k a
delListFromUniqMap :: UniqMap k a -> [k] -> UniqMap k a
delListFromUniqMap (UniqMap UniqFM (k, a)
m) [k]
ks = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> [k] -> UniqFM (k, a)
forall key elt. Uniquable key => UniqFM elt -> [key] -> UniqFM elt
delListFromUFM UniqFM (k, a)
m [k]
ks

plusUniqMap :: UniqMap k a -> UniqMap k a -> UniqMap k a
plusUniqMap :: UniqMap k a -> UniqMap k a -> UniqMap k a
plusUniqMap (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, a)
m2) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> UniqFM (k, a) -> UniqFM (k, a)
forall elt. UniqFM elt -> UniqFM elt -> UniqFM elt
plusUFM UniqFM (k, a)
m1 UniqFM (k, a)
m2

plusUniqMap_C :: (a -> a -> a) -> UniqMap k a -> UniqMap k a -> UniqMap k a
plusUniqMap_C :: (a -> a -> a) -> UniqMap k a -> UniqMap k a -> UniqMap k a
plusUniqMap_C a -> a -> a
f (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, a)
m2) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a) -> (k, a))
-> UniqFM (k, a) -> UniqFM (k, a) -> UniqFM (k, a)
forall elt.
(elt -> elt -> elt) -> UniqFM elt -> UniqFM elt -> UniqFM elt
plusUFM_C ((a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
forall a k. (a -> a -> a) -> (k, a) -> (k, a) -> (k, a)
liftC a -> a -> a
f) UniqFM (k, a)
m1 UniqFM (k, a)
m2

plusMaybeUniqMap_C :: (a -> a -> Maybe a) -> UniqMap k a -> UniqMap k a -> UniqMap k a
plusMaybeUniqMap_C :: (a -> a -> Maybe a) -> UniqMap k a -> UniqMap k a -> UniqMap k a
plusMaybeUniqMap_C a -> a -> Maybe a
f (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, a)
m2) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$
    ((k, a) -> (k, a) -> Maybe (k, a))
-> UniqFM (k, a) -> UniqFM (k, a) -> UniqFM (k, a)
forall elt.
(elt -> elt -> Maybe elt) -> UniqFM elt -> UniqFM elt -> UniqFM elt
plusMaybeUFM_C (\(k
_, a
v) (k
k', a
v') -> (a -> (k, a)) -> Maybe a -> Maybe (k, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k
k',) (a -> a -> Maybe a
f a
v a
v')) UniqFM (k, a)
m1 UniqFM (k, a)
m2

plusUniqMapList :: [UniqMap k a] -> UniqMap k a
plusUniqMapList :: [UniqMap k a] -> UniqMap k a
plusUniqMapList [UniqMap k a]
xs = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ [UniqFM (k, a)] -> UniqFM (k, a)
forall elt. [UniqFM elt] -> UniqFM elt
plusUFMList ([UniqMap k a] -> [UniqFM (k, a)]
coerce [UniqMap k a]
xs)

minusUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a
minusUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a
minusUniqMap (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, b)
m2) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> UniqFM (k, b) -> UniqFM (k, a)
forall elt1 elt2. UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
minusUFM UniqFM (k, a)
m1 UniqFM (k, b)
m2

intersectUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a
intersectUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a
intersectUniqMap (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, b)
m2) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ UniqFM (k, a) -> UniqFM (k, b) -> UniqFM (k, a)
forall elt1 elt2. UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
intersectUFM UniqFM (k, a)
m1 UniqFM (k, b)
m2

disjointUniqMap :: UniqMap k a -> UniqMap k b -> Bool
disjointUniqMap :: UniqMap k a -> UniqMap k b -> Bool
disjointUniqMap (UniqMap UniqFM (k, a)
m1) (UniqMap UniqFM (k, b)
m2) = UniqFM (k, a) -> UniqFM (k, b) -> Bool
forall elt1 elt2. UniqFM elt1 -> UniqFM elt2 -> Bool
disjointUFM UniqFM (k, a)
m1 UniqFM (k, b)
m2

mapUniqMap :: (a -> b) -> UniqMap k a -> UniqMap k b
mapUniqMap :: (a -> b) -> UniqMap k a -> UniqMap k b
mapUniqMap a -> b
f (UniqMap UniqFM (k, a)
m) = UniqFM (k, b) -> UniqMap k b
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, b) -> UniqMap k b) -> UniqFM (k, b) -> UniqMap k b
forall a b. (a -> b) -> a -> b
$ ((k, a) -> (k, b)) -> UniqFM (k, a) -> UniqFM (k, b)
forall elt1 elt2. (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
mapUFM ((a -> b) -> (k, a) -> (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) UniqFM (k, a)
m -- (,) k instance

filterUniqMap :: (a -> Bool) -> UniqMap k a -> UniqMap k a
filterUniqMap :: (a -> Bool) -> UniqMap k a -> UniqMap k a
filterUniqMap a -> Bool
f (UniqMap UniqFM (k, a)
m) = UniqFM (k, a) -> UniqMap k a
forall k a. UniqFM (k, a) -> UniqMap k a
UniqMap (UniqFM (k, a) -> UniqMap k a) -> UniqFM (k, a) -> UniqMap k a
forall a b. (a -> b) -> a -> b
$ ((k, a) -> Bool) -> UniqFM (k, a) -> UniqFM (k, a)
forall elt. (elt -> Bool) -> UniqFM elt -> UniqFM elt
filterUFM (a -> Bool
f (a -> Bool) -> ((k, a) -> a) -> (k, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, a) -> a
forall a b. (a, b) -> b
snd) UniqFM (k, a)
m

partitionUniqMap :: (a -> Bool) -> UniqMap k a -> (UniqMap k a, UniqMap k a)
partitionUniqMap :: (a -> Bool) -> UniqMap k a -> (UniqMap k a, UniqMap k a)
partitionUniqMap a -> Bool
f (UniqMap UniqFM (k, a)
m) =
    (UniqFM (k, a), UniqFM (k, a)) -> (UniqMap k a, UniqMap k a)
coerce ((UniqFM (k, a), UniqFM (k, a)) -> (UniqMap k a, UniqMap k a))
-> (UniqFM (k, a), UniqFM (k, a)) -> (UniqMap k a, UniqMap k a)
forall a b. (a -> b) -> a -> b
$ ((k, a) -> Bool) -> UniqFM (k, a) -> (UniqFM (k, a), UniqFM (k, a))
forall elt. (elt -> Bool) -> UniqFM elt -> (UniqFM elt, UniqFM elt)
partitionUFM (a -> Bool
f (a -> Bool) -> ((k, a) -> a) -> (k, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, a) -> a
forall a b. (a, b) -> b
snd) UniqFM (k, a)
m

sizeUniqMap :: UniqMap k a -> Int
sizeUniqMap :: UniqMap k a -> Int
sizeUniqMap (UniqMap UniqFM (k, a)
m) = UniqFM (k, a) -> Int
forall elt. UniqFM elt -> Int
sizeUFM UniqFM (k, a)
m

elemUniqMap :: Uniquable k => k -> UniqMap k a -> Bool
elemUniqMap :: k -> UniqMap k a -> Bool
elemUniqMap k
k (UniqMap UniqFM (k, a)
m) = k -> UniqFM (k, a) -> Bool
forall key elt. Uniquable key => key -> UniqFM elt -> Bool
elemUFM k
k UniqFM (k, a)
m

lookupUniqMap :: Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap :: UniqMap k a -> k -> Maybe a
lookupUniqMap (UniqMap UniqFM (k, a)
m) k
k = ((k, a) -> a) -> Maybe (k, a) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> a
forall a b. (a, b) -> b
snd (UniqFM (k, a) -> k -> Maybe (k, a)
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM UniqFM (k, a)
m k
k)

lookupWithDefaultUniqMap :: Uniquable k => UniqMap k a -> a -> k -> a
lookupWithDefaultUniqMap :: UniqMap k a -> a -> k -> a
lookupWithDefaultUniqMap (UniqMap UniqFM (k, a)
m) a
a k
k = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
a (((k, a) -> a) -> Maybe (k, a) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> a
forall a b. (a, b) -> b
snd (UniqFM (k, a) -> k -> Maybe (k, a)
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM UniqFM (k, a)
m k
k))

anyUniqMap :: (a -> Bool) -> UniqMap k a -> Bool
anyUniqMap :: (a -> Bool) -> UniqMap k a -> Bool
anyUniqMap a -> Bool
f (UniqMap UniqFM (k, a)
m) = ((k, a) -> Bool) -> UniqFM (k, a) -> Bool
forall elt. (elt -> Bool) -> UniqFM elt -> Bool
anyUFM (a -> Bool
f (a -> Bool) -> ((k, a) -> a) -> (k, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, a) -> a
forall a b. (a, b) -> b
snd) UniqFM (k, a)
m

allUniqMap :: (a -> Bool) -> UniqMap k a -> Bool
allUniqMap :: (a -> Bool) -> UniqMap k a -> Bool
allUniqMap a -> Bool
f (UniqMap UniqFM (k, a)
m) = ((k, a) -> Bool) -> UniqFM (k, a) -> Bool
forall elt. (elt -> Bool) -> UniqFM elt -> Bool
allUFM (a -> Bool
f (a -> Bool) -> ((k, a) -> a) -> (k, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, a) -> a
forall a b. (a, b) -> b
snd) UniqFM (k, a)
m