{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TypeFamilies #-}

-- |
-- An abstract interface for maps from JSON keys to values.
--
-- @since 2.0.0.0

module Data.Aeson.KeyMap (
    -- * Map Type
    KeyMap,

    -- * Query
    null,
    lookup,
    (!?),
    size,
    member,

    -- * Construction
    empty,
    singleton,

    -- ** Insertion
    insert,
    insertWith,

    -- * Deletion
    delete,

    -- * Update
    alterF,

    -- * Combine
    difference,
    union,
    unionWith,
    unionWithKey,
    intersection,
    intersectionWith,
    intersectionWithKey,
    alignWith,
    alignWithKey,

    -- * Lists
    fromList,
    fromListWith,
    toList,
    toAscList,
    elems,

    -- * Maps
    fromHashMap,
    toHashMap,
    fromHashMapText,
    toHashMapText,
    coercionToHashMap,
    fromMap,
    toMap,
    fromMapText,
    toMapText,
    coercionToMap,

    -- * Traversal
    -- ** Map
    map,
    mapWithKey,
    mapKeyVal,
    traverse,
    traverseWithKey,

    -- * Folds
    foldr,
    foldr',
    foldl,
    foldl',
    foldMapWithKey,
    foldrWithKey,

    -- * Conversions
    keys,

    -- * Filter
    filter,
    filterWithKey,
    mapMaybe,
    mapMaybeWithKey,

    -- * Key Type
    Key,
) where

-- Import stuff from Prelude explicitly
import Prelude (Eq(..), Ord((>)), Int, Bool(..), Maybe(..))
import Prelude ((.), ($))
import Prelude (Functor(fmap), Monad(..))
import Prelude (Show, showsPrec, showParen, shows, showString)

import Control.Applicative (Applicative)
import Control.DeepSeq (NFData(..))
import Data.Aeson.Key (Key)
import Data.Bifunctor (first)
import Data.Data (Data)
import Data.Hashable (Hashable(..))
import Data.HashMap.Strict (HashMap)
import Data.Map (Map)
import Data.Monoid (Monoid(mempty, mappend))
import Data.Semigroup (Semigroup((<>)))
import Data.Text (Text)
import Data.These (These (..))
import Data.Type.Coercion (Coercion (..))
import Data.Typeable (Typeable)
import Text.Read (Read (..), Lexeme(..), readListPrecDefault, prec, lexP, parens)

import qualified Data.Aeson.Key as Key
import qualified Data.Foldable as F
import qualified Data.Traversable as T
import qualified Data.HashMap.Strict as H
import qualified Data.List as L
import qualified Data.Map.Strict as M
import qualified Language.Haskell.TH.Syntax as TH
import qualified Data.Foldable.WithIndex    as WI (FoldableWithIndex (..))
import qualified Data.Functor.WithIndex     as WI (FunctorWithIndex (..))
import qualified Data.Traversable.WithIndex as WI (TraversableWithIndex (..))
import qualified Data.Semialign as SA
import qualified Data.Semialign.Indexed as SAI
import qualified GHC.Exts
import qualified Test.QuickCheck as QC
import qualified Witherable as W

#ifdef USE_ORDEREDMAP

-------------------------------------------------------------------------------
-- Map
-------------------------------------------------------------------------------

-- | A map from JSON key type 'Key' to 'v'.
newtype KeyMap v = KeyMap { forall v. KeyMap v -> Map Key v
unKeyMap :: Map Key v }
  deriving (KeyMap v -> KeyMap v -> Bool
forall v. Eq v => KeyMap v -> KeyMap v -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: KeyMap v -> KeyMap v -> Bool
$c/= :: forall v. Eq v => KeyMap v -> KeyMap v -> Bool
== :: KeyMap v -> KeyMap v -> Bool
$c== :: forall v. Eq v => KeyMap v -> KeyMap v -> Bool
Eq, KeyMap v -> KeyMap v -> Bool
KeyMap v -> KeyMap v -> Ordering
KeyMap v -> KeyMap v -> KeyMap 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 {v}. Ord v => Eq (KeyMap v)
forall v. Ord v => KeyMap v -> KeyMap v -> Bool
forall v. Ord v => KeyMap v -> KeyMap v -> Ordering
forall v. Ord v => KeyMap v -> KeyMap v -> KeyMap v
min :: KeyMap v -> KeyMap v -> KeyMap v
$cmin :: forall v. Ord v => KeyMap v -> KeyMap v -> KeyMap v
max :: KeyMap v -> KeyMap v -> KeyMap v
$cmax :: forall v. Ord v => KeyMap v -> KeyMap v -> KeyMap v
>= :: KeyMap v -> KeyMap v -> Bool
$c>= :: forall v. Ord v => KeyMap v -> KeyMap v -> Bool
> :: KeyMap v -> KeyMap v -> Bool
$c> :: forall v. Ord v => KeyMap v -> KeyMap v -> Bool
<= :: KeyMap v -> KeyMap v -> Bool
$c<= :: forall v. Ord v => KeyMap v -> KeyMap v -> Bool
< :: KeyMap v -> KeyMap v -> Bool
$c< :: forall v. Ord v => KeyMap v -> KeyMap v -> Bool
compare :: KeyMap v -> KeyMap v -> Ordering
$ccompare :: forall v. Ord v => KeyMap v -> KeyMap v -> Ordering
Ord, Typeable, KeyMap v -> DataType
KeyMap v -> Constr
forall {v}. Data v => Typeable (KeyMap v)
forall v. Data v => KeyMap v -> DataType
forall v. Data v => KeyMap v -> Constr
forall v.
Data v =>
(forall b. Data b => b -> b) -> KeyMap v -> KeyMap v
forall v u.
Data v =>
Int -> (forall d. Data d => d -> u) -> KeyMap v -> u
forall v u.
Data v =>
(forall d. Data d => d -> u) -> KeyMap v -> [u]
forall v r r'.
Data v =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
forall v r r'.
Data v =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
forall v (m :: * -> *).
(Data v, Monad m) =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
forall v (m :: * -> *).
(Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
forall v (c :: * -> *).
Data v =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (KeyMap v)
forall v (c :: * -> *).
Data v =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> KeyMap v -> c (KeyMap v)
forall v (t :: * -> *) (c :: * -> *).
(Data v, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (KeyMap v))
forall v (t :: * -> * -> *) (c :: * -> *).
(Data v, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (KeyMap v))
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 (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (KeyMap v)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> KeyMap v -> c (KeyMap v)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (KeyMap v))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
$cgmapMo :: forall v (m :: * -> *).
(Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
$cgmapMp :: forall v (m :: * -> *).
(Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
$cgmapM :: forall v (m :: * -> *).
(Data v, Monad m) =>
(forall d. Data d => d -> m d) -> KeyMap v -> m (KeyMap v)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> KeyMap v -> u
$cgmapQi :: forall v u.
Data v =>
Int -> (forall d. Data d => d -> u) -> KeyMap v -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> KeyMap v -> [u]
$cgmapQ :: forall v u.
Data v =>
(forall d. Data d => d -> u) -> KeyMap v -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
$cgmapQr :: forall v r r'.
Data v =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
$cgmapQl :: forall v r r'.
Data v =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> KeyMap v -> r
gmapT :: (forall b. Data b => b -> b) -> KeyMap v -> KeyMap v
$cgmapT :: forall v.
Data v =>
(forall b. Data b => b -> b) -> KeyMap v -> KeyMap v
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (KeyMap v))
$cdataCast2 :: forall v (t :: * -> * -> *) (c :: * -> *).
(Data v, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (KeyMap v))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (KeyMap v))
$cdataCast1 :: forall v (t :: * -> *) (c :: * -> *).
(Data v, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (KeyMap v))
dataTypeOf :: KeyMap v -> DataType
$cdataTypeOf :: forall v. Data v => KeyMap v -> DataType
toConstr :: KeyMap v -> Constr
$ctoConstr :: forall v. Data v => KeyMap v -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (KeyMap v)
$cgunfold :: forall v (c :: * -> *).
Data v =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (KeyMap v)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> KeyMap v -> c (KeyMap v)
$cgfoldl :: forall v (c :: * -> *).
Data v =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> KeyMap v -> c (KeyMap v)
Data, forall a b. a -> KeyMap b -> KeyMap a
forall a b. (a -> b) -> KeyMap a -> KeyMap b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> KeyMap b -> KeyMap a
$c<$ :: forall a b. a -> KeyMap b -> KeyMap a
fmap :: forall a b. (a -> b) -> KeyMap a -> KeyMap b
$cfmap :: forall a b. (a -> b) -> KeyMap a -> KeyMap b
Functor)


-- | Construct an empty map.
empty :: KeyMap v
empty :: forall v. KeyMap v
empty = forall v. Map Key v -> KeyMap v
KeyMap forall k a. Map k a
M.empty

-- | Is the map empty?
null :: KeyMap v -> Bool
null :: forall v. KeyMap v -> Bool
null = forall k a. Map k a -> Bool
M.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Return the number of key-value mappings in this map.
size :: KeyMap v -> Int
size :: forall v. KeyMap v -> Int
size = forall k a. Map k a -> Int
M.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Construct a map with a single element.
singleton :: Key -> v -> KeyMap v
singleton :: forall v. Key -> v -> KeyMap v
singleton Key
k v
v = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. k -> a -> Map k a
M.singleton Key
k v
v)

-- | Is the key a member of the map?
member :: Key -> KeyMap a -> Bool
member :: forall a. Key -> KeyMap a -> Bool
member Key
t (KeyMap Map Key a
m) = forall k a. Ord k => k -> Map k a -> Bool
M.member Key
t Map Key a
m

-- | Remove the mapping for the specified key from this map if present.
delete :: Key -> KeyMap v -> KeyMap v
delete :: forall v. Key -> KeyMap v -> KeyMap v
delete Key
k (KeyMap Map Key v
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. Ord k => k -> Map k a -> Map k a
M.delete Key
k Map Key v
m)

-- | 'alterF' can be used to insert, delete, or update a value in a map.
alterF :: Functor f => (Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
alterF :: forall (f :: * -> *) v.
Functor f =>
(Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
alterF Maybe v -> f (Maybe v)
f Key
k = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall v. Map Key v -> KeyMap v
KeyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
M.alterF Maybe v -> f (Maybe v)
f Key
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Return the value to which the specified key is mapped,
-- or Nothing if this map contains no mapping for the key.
lookup :: Key -> KeyMap v -> Maybe v
lookup :: forall v. Key -> KeyMap v -> Maybe v
lookup Key
t KeyMap v
tm = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Key
t (forall v. KeyMap v -> Map Key v
unKeyMap KeyMap v
tm)

-- | Associate the specified value with the specified key
-- in this map. If this map previously contained a mapping
-- for the key, the old value is replaced.
insert :: Key -> v -> KeyMap v -> KeyMap v
insert :: forall v. Key -> v -> KeyMap v -> KeyMap v
insert Key
k v
v KeyMap v
tm = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Key
k v
v (forall v. KeyMap v -> Map Key v
unKeyMap KeyMap v
tm))

-- | Insert with a function combining new and old values, taken in that order.
--
-- @since 2.1.1.0
insertWith :: (a -> a -> a) -> Key -> a -> KeyMap a -> KeyMap a
insertWith :: forall a. (a -> a -> a) -> Key -> a -> KeyMap a -> KeyMap a
insertWith a -> a -> a
f Key
k a
v KeyMap a
m = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith a -> a -> a
f Key
k a
v (forall v. KeyMap v -> Map Key v
unKeyMap KeyMap a
m))

-- | Map a function over all values in the map.
map :: (a -> b) -> KeyMap a -> KeyMap b
map :: forall a b. (a -> b) -> KeyMap a -> KeyMap b
map = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

-- | Map a function over all values in the map.
--
-- @since 2.1.0.0
mapWithKey :: (Key -> a -> b) -> KeyMap a -> KeyMap b
mapWithKey :: forall a b. (Key -> a -> b) -> KeyMap a -> KeyMap b
mapWithKey Key -> a -> b
f (KeyMap Map Key a
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b. (k -> a -> b) -> Map k a -> Map k b
M.mapWithKey Key -> a -> b
f Map Key a
m)

foldMapWithKey :: Monoid m => (Key -> a -> m) -> KeyMap a -> m
foldMapWithKey :: forall m a. Monoid m => (Key -> a -> m) -> KeyMap a -> m
foldMapWithKey Key -> a -> m
f (KeyMap Map Key a
m) = forall m k a. Monoid m => (k -> a -> m) -> Map k a -> m
M.foldMapWithKey Key -> a -> m
f Map Key a
m

foldr :: (a -> b -> b) -> b -> KeyMap a -> b
foldr :: forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr a -> b -> b
f b
z (KeyMap Map Key a
m) = forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr a -> b -> b
f b
z Map Key a
m

foldr' :: (a -> b -> b) -> b -> KeyMap a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr' a -> b -> b
f b
z (KeyMap Map Key a
m) = forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr' a -> b -> b
f b
z Map Key a
m

foldl :: (b -> a -> b) -> b -> KeyMap a -> b
foldl :: forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl b -> a -> b
f b
z (KeyMap Map Key a
m) = forall a b k. (a -> b -> a) -> a -> Map k b -> a
M.foldl b -> a -> b
f b
z Map Key a
m

foldl' :: (b -> a -> b) -> b -> KeyMap a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl' b -> a -> b
f b
z (KeyMap Map Key a
m) = forall a b k. (a -> b -> a) -> a -> Map k b -> a
M.foldl' b -> a -> b
f b
z Map Key a
m

-- | Reduce this map by applying a binary operator to all
-- elements, using the given starting value (typically the
-- right-identity of the operator).
foldrWithKey :: (Key -> v -> a -> a) -> a -> KeyMap v -> a
foldrWithKey :: forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
foldrWithKey Key -> v -> a -> a
f a
a = forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey Key -> v -> a -> a
f a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Perform an Applicative action for each key-value pair
-- in a 'KeyMap' and produce a 'KeyMap' of all the results.
traverse :: Applicative f => (v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverse :: forall (f :: * -> *) v1 v2.
Applicative f =>
(v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverse v1 -> f v2
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall v. Map Key v -> KeyMap v
KeyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse v1 -> f v2
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Perform an Applicative action for each key-value pair
-- in a 'KeyMap' and produce a 'KeyMap' of all the results.
traverseWithKey :: Applicative f => (Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseWithKey :: forall (f :: * -> *) v1 v2.
Applicative f =>
(Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseWithKey Key -> v1 -> f v2
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall v. Map Key v -> KeyMap v
KeyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
M.traverseWithKey Key -> v1 -> f v2
f  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Construct a map from a list of elements. Uses the
-- provided function, f, to merge duplicate entries with
-- (f newVal oldVal).
fromListWith :: (v -> v -> v) ->  [(Key, v)] -> KeyMap v
fromListWith :: forall v. (v -> v -> v) -> [(Key, v)] -> KeyMap v
fromListWith v -> v -> v
op = forall v. Map Key v -> KeyMap v
KeyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith v -> v -> v
op

-- |  Construct a map with the supplied mappings. If the
-- list contains duplicate mappings, the later mappings take
-- precedence.
--
-- >>> fromList [("a", 'x'), ("a", 'y')]
-- fromList [("a",'y')]
--
fromList :: [(Key, v)] -> KeyMap v
fromList :: forall v. [(Key, v)] -> KeyMap v
fromList = forall v. Map Key v -> KeyMap v
KeyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
M.fromList

-- | Return a list of this map's keys and elements.
--
-- The order is not stable. Use 'toAscList' for stable ordering.
toList :: KeyMap v -> [(Key, v)]
toList :: forall v. KeyMap v -> [(Key, v)]
toList = forall k a. Map k a -> [(k, a)]
M.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Return a list of this map' elements.
--
-- @since 2.0.3.0
elems :: KeyMap v -> [v]
elems :: forall v. KeyMap v -> [v]
elems = forall k a. Map k a -> [a]
M.elems forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Return a list of this map's elements in ascending order
-- based of the textual key.
toAscList :: KeyMap v -> [(Key, v)]
toAscList :: forall v. KeyMap v -> [(Key, v)]
toAscList = forall k a. Map k a -> [(k, a)]
M.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Difference of two maps. Return elements of the first
-- map not existing in the second.
difference :: KeyMap v -> KeyMap v' -> KeyMap v
difference :: forall v v'. KeyMap v -> KeyMap v' -> KeyMap v
difference KeyMap v
tm1 KeyMap v'
tm2 = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.difference (forall v. KeyMap v -> Map Key v
unKeyMap KeyMap v
tm1) (forall v. KeyMap v -> Map Key v
unKeyMap KeyMap v'
tm2))

-- | The (left-biased) union of two maps. It prefers the first map when duplicate
-- keys are encountered, i.e. ('union' == 'unionWith' 'const').
union :: KeyMap v -> KeyMap v -> KeyMap v
union :: forall v. KeyMap v -> KeyMap v -> KeyMap v
union (KeyMap Map Key v
x) (KeyMap Map Key v
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map Key v
x Map Key v
y)

-- | The union with a combining function.
unionWith :: (v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWith :: forall v. (v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWith v -> v -> v
f (KeyMap Map Key v
x) (KeyMap Map Key v
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
M.unionWith v -> v -> v
f Map Key v
x Map Key v
y)

-- | The union with a combining function.
unionWithKey :: (Key -> v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWithKey :: forall v. (Key -> v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWithKey Key -> v -> v -> v
f (KeyMap Map Key v
x) (KeyMap Map Key v
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
M.unionWithKey Key -> v -> v -> v
f Map Key v
x Map Key v
y)

-- | The (left-biased) intersection of two maps (based on keys).
intersection :: KeyMap a -> KeyMap b -> KeyMap a
intersection :: forall v v'. KeyMap v -> KeyMap v' -> KeyMap v
intersection (KeyMap Map Key a
x) (KeyMap Map Key b
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.intersection Map Key a
x Map Key b
y)

-- | The intersection with a combining function.
intersectionWith :: (a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWith :: forall a b c. (a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWith a -> b -> c
f (KeyMap Map Key a
x) (KeyMap Map Key b
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
M.intersectionWith a -> b -> c
f Map Key a
x Map Key b
y)

-- | The intersection with a combining function.
intersectionWithKey :: (Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWithKey :: forall a b c.
(Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWithKey Key -> a -> b -> c
f (KeyMap Map Key a
x) (KeyMap Map Key b
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b c.
Ord k =>
(k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
M.intersectionWithKey Key -> a -> b -> c
f Map Key a
x Map Key b
y)

-- | Return a list of this map's keys.
keys :: KeyMap v -> [Key]
keys :: forall v. KeyMap v -> [Key]
keys = forall k a. Map k a -> [k]
M.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> Map Key v
unKeyMap

-- | Convert a 'KeyMap' to a 'HashMap'.
toHashMap :: KeyMap v -> HashMap Key v
toHashMap :: forall v. KeyMap v -> HashMap Key v
toHashMap = forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
H.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [(Key, v)]
toList

-- | Convert a 'HashMap' to a 'KeyMap'.
fromHashMap :: HashMap Key v -> KeyMap v
fromHashMap :: forall v. HashMap Key v -> KeyMap v
fromHashMap = forall v. [(Key, v)] -> KeyMap v
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. HashMap k v -> [(k, v)]
H.toList

-- | Convert a 'KeyMap' to a 'Map'.
toMap :: KeyMap v -> Map Key v
toMap :: forall v. KeyMap v -> Map Key v
toMap = forall v. KeyMap v -> Map Key v
unKeyMap

-- | Convert a 'Map' to a 'KeyMap'.
fromMap :: Map Key v -> KeyMap v
fromMap :: forall v. Map Key v -> KeyMap v
fromMap = forall v. Map Key v -> KeyMap v
KeyMap

coercionToHashMap :: Maybe (Coercion (HashMap Key v) (KeyMap v))
coercionToHashMap :: forall v. Maybe (Coercion (HashMap Key v) (KeyMap v))
coercionToHashMap = forall a. Maybe a
Nothing
{-# INLINE coercionToHashMap #-}

coercionToMap :: Maybe (Coercion (Map Key v) (KeyMap v))
coercionToMap :: forall v. Maybe (Coercion (Map Key v) (KeyMap v))
coercionToMap = forall a. a -> Maybe a
Just forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion
{-# INLINE coercionToMap #-}

-- | Transform the keys and values of a 'KeyMap'.
mapKeyVal :: (Key -> Key) -> (v1 -> v2)
          -> KeyMap v1 -> KeyMap v2
mapKeyVal :: forall v1 v2. (Key -> Key) -> (v1 -> v2) -> KeyMap v1 -> KeyMap v2
mapKeyVal Key -> Key
fk v1 -> v2
kv = forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
foldrWithKey (\Key
k v1
v -> forall v. Key -> v -> KeyMap v -> KeyMap v
insert (Key -> Key
fk Key
k) (v1 -> v2
kv v1
v)) forall v. KeyMap v
empty
{-# INLINE mapKeyVal #-}

-- | Filter all keys/values that satisfy some predicate.
filter :: (v -> Bool) -> KeyMap v -> KeyMap v
filter :: forall v. (v -> Bool) -> KeyMap v -> KeyMap v
filter v -> Bool
f (KeyMap Map Key v
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter v -> Bool
f Map Key v
m)

-- | Filter all keys/values that satisfy some predicate.
filterWithKey :: (Key -> v -> Bool) -> KeyMap v -> KeyMap v
filterWithKey :: forall v. (Key -> v -> Bool) -> KeyMap v -> KeyMap v
filterWithKey Key -> v -> Bool
f (KeyMap Map Key v
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a. (k -> a -> Bool) -> Map k a -> Map k a
M.filterWithKey Key -> v -> Bool
f Map Key v
m)

-- | Map values and collect the Just results.
mapMaybe :: (a -> Maybe b) -> KeyMap a -> KeyMap b
mapMaybe :: forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
mapMaybe a -> Maybe b
f (KeyMap Map Key a
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall a b k. (a -> Maybe b) -> Map k a -> Map k b
M.mapMaybe a -> Maybe b
f Map Key a
m)

-- | Map values and collect the Just results.
mapMaybeWithKey :: (Key -> v -> Maybe u) -> KeyMap v -> KeyMap u
mapMaybeWithKey :: forall v u. (Key -> v -> Maybe u) -> KeyMap v -> KeyMap u
mapMaybeWithKey Key -> v -> Maybe u
f (KeyMap Map Key v
m) = forall v. Map Key v -> KeyMap v
KeyMap (forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
M.mapMaybeWithKey Key -> v -> Maybe u
f Map Key v
m)

#else

-------------------------------------------------------------------------------
-- HashMap
-------------------------------------------------------------------------------

import Data.List (sortBy)
import Data.Ord (comparing)
import Prelude (fst)

-- | A map from JSON key type 'Key' to 'v'.
newtype KeyMap v = KeyMap { unKeyMap :: HashMap Key v }
  deriving (Eq, Ord, Typeable, Data, Functor)

-- | Construct an empty map.
empty :: KeyMap v
empty = KeyMap H.empty

-- | Is the map empty?
null :: KeyMap v -> Bool
null = H.null . unKeyMap

-- | Return the number of key-value mappings in this map.
size :: KeyMap v -> Int
size = H.size . unKeyMap

-- | Construct a map with a single element.
singleton :: Key -> v -> KeyMap v
singleton k v = KeyMap (H.singleton k v)

-- | Is the key a member of the map?
member :: Key -> KeyMap a -> Bool
member t (KeyMap m) = H.member t m

-- | Remove the mapping for the specified key from this map if present.
delete :: Key -> KeyMap v -> KeyMap v
delete k (KeyMap m) = KeyMap (H.delete k m)

-- | 'alterF' can be used to insert, delete, or update a value in a map.
alterF :: Functor f => (Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
alterF f k = fmap KeyMap . H.alterF f k . unKeyMap

-- | Return the value to which the specified key is mapped,
-- or Nothing if this map contains no mapping for the key.
lookup :: Key -> KeyMap v -> Maybe v
lookup t tm = H.lookup t (unKeyMap tm)

-- | Associate the specified value with the specified key
-- in this map. If this map previously contained a mapping
-- for the key, the old value is replaced.
insert :: Key -> v -> KeyMap v -> KeyMap v
insert k v tm = KeyMap (H.insert k v (unKeyMap tm))

-- | Insert with a function combining new and old values, taken in that order.
--
-- @since 2.1.1.0
insertWith :: (a -> a -> a) -> Key -> a -> KeyMap a -> KeyMap a
insertWith f k v m = KeyMap (H.insertWith f k v (unKeyMap m))

-- | Map a function over all values in the map.
map :: (a -> b) -> KeyMap a -> KeyMap b
map = fmap

-- | Map a function over all values in the map.
--
-- @since 2.1.0.0
mapWithKey :: (Key -> a -> b) -> KeyMap a -> KeyMap b
mapWithKey f (KeyMap m) = KeyMap (H.mapWithKey f m)

foldMapWithKey :: Monoid m => (Key -> a -> m) -> KeyMap a -> m
foldMapWithKey f (KeyMap m) = H.foldMapWithKey f m

foldr :: (a -> b -> b) -> b -> KeyMap a -> b
foldr f z (KeyMap m) = H.foldr f z m

foldr' :: (a -> b -> b) -> b -> KeyMap a -> b
foldr' f z (KeyMap m) = H.foldr' f z m

foldl :: (b -> a -> b) -> b -> KeyMap a -> b
foldl f z (KeyMap m) = H.foldl f z m

foldl' :: (b -> a -> b) -> b -> KeyMap a -> b
foldl' f z (KeyMap m) = H.foldl' f z m

-- | Reduce this map by applying a binary operator to all
-- elements, using the given starting value (typically the
-- right-identity of the operator).
foldrWithKey :: (Key -> v -> a -> a) -> a -> KeyMap v -> a
foldrWithKey f a = H.foldrWithKey f a . unKeyMap

-- | Perform an Applicative action for each key-value pair
-- in a 'KeyMap' and produce a 'KeyMap' of all the results.
traverse :: Applicative f => (v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverse f = fmap KeyMap . T.traverse f . unKeyMap

-- | Perform an Applicative action for each key-value pair
-- in a 'KeyMap' and produce a 'KeyMap' of all the results.
traverseWithKey :: Applicative f => (Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseWithKey f = fmap KeyMap . H.traverseWithKey f  . unKeyMap

-- | Construct a map from a list of elements. Uses the
-- provided function, f, to merge duplicate entries with
-- (f newVal oldVal).
fromListWith :: (v -> v -> v) ->  [(Key, v)] -> KeyMap v
fromListWith op = KeyMap . H.fromListWith op

-- |  Construct a map with the supplied mappings. If the
-- list contains duplicate mappings, the later mappings take
-- precedence.
fromList :: [(Key, v)] -> KeyMap v
fromList = KeyMap . H.fromList

-- | Return a list of this map's elements.
--
-- The order is not stable. Use 'toAscList' for stable ordering.
toList :: KeyMap v -> [(Key, v)]
toList = H.toList . unKeyMap

-- | Return a list of this map' elements.
--
-- @since 2.0.3.0
elems :: KeyMap v -> [v]
elems = H.elems . unKeyMap

-- | Return a list of this map's elements in ascending order
-- based of the textual key.
toAscList :: KeyMap v -> [(Key, v)]
toAscList = sortBy (comparing fst) . toList

-- | Difference of two maps. Return elements of the first
-- map not existing in the second.
difference :: KeyMap v -> KeyMap v' -> KeyMap v
difference tm1 tm2 = KeyMap (H.difference (unKeyMap tm1) (unKeyMap tm2))

-- | The (left-biased) union of two maps. It prefers the first map when duplicate
-- keys are encountered, i.e. ('union' == 'unionWith' 'const').
union :: KeyMap v -> KeyMap v -> KeyMap v
union (KeyMap x) (KeyMap y) = KeyMap (H.union x y)

-- | The union with a combining function.
unionWith :: (v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWith f (KeyMap x) (KeyMap y) = KeyMap (H.unionWith f x y)

-- | The union with a combining function.
unionWithKey :: (Key -> v -> v -> v) -> KeyMap v -> KeyMap v -> KeyMap v
unionWithKey f (KeyMap x) (KeyMap y) = KeyMap (H.unionWithKey f x y)

-- | The (left-biased) intersection of two maps (based on keys).
intersection :: KeyMap a -> KeyMap b -> KeyMap a
intersection (KeyMap x) (KeyMap y) = KeyMap (H.intersection x y)

-- | The intersection with a combining function.
intersectionWith :: (a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWith f (KeyMap x) (KeyMap y) = KeyMap (H.intersectionWith f x y)

-- | The intersection with a combining function.
intersectionWithKey :: (Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWithKey f (KeyMap x) (KeyMap y) = KeyMap (H.intersectionWithKey f x y)

-- | Return a list of this map's keys.
keys :: KeyMap v -> [Key]
keys = H.keys . unKeyMap

-- | Convert a 'KeyMap' to a 'HashMap'.
toHashMap :: KeyMap v -> HashMap Key v
toHashMap = unKeyMap

-- | Convert a 'HashMap' to a 'KeyMap'.
fromHashMap :: HashMap Key v -> KeyMap v
fromHashMap = KeyMap

-- | Convert a 'KeyMap' to a 'Map'.
toMap :: KeyMap v -> Map Key v
toMap = M.fromList . toList

-- | Convert a 'Map' to a 'KeyMap'.
fromMap :: Map Key v -> KeyMap v
fromMap = fromList . M.toList

coercionToHashMap :: Maybe (Coercion (HashMap Key v) (KeyMap v))
coercionToHashMap = Just Coercion
{-# INLINE coercionToHashMap #-}

coercionToMap :: Maybe (Coercion (Map Key v) (KeyMap v))
coercionToMap = Nothing
{-# INLINE coercionToMap #-}

-- | Transform the keys and values of a 'KeyMap'.
mapKeyVal :: (Key -> Key) -> (v1 -> v2)
          -> KeyMap v1 -> KeyMap v2
mapKeyVal fk kv = foldrWithKey (\k v -> insert (fk k) (kv v)) empty
{-# INLINE mapKeyVal #-}

-- | Filter all keys/values that satisfy some predicate.
filter :: (v -> Bool) -> KeyMap v -> KeyMap v
filter f (KeyMap m) = KeyMap (H.filter f m)

-- | Filter all keys/values that satisfy some predicate.
filterWithKey :: (Key -> v -> Bool) -> KeyMap v -> KeyMap v
filterWithKey f (KeyMap m) = KeyMap (H.filterWithKey f m)

-- | Map values and collect the Just results.
mapMaybe :: (a -> Maybe b) -> KeyMap a -> KeyMap b
mapMaybe f (KeyMap m) = KeyMap (H.mapMaybe f m)

-- | Map values and collect the Just results.
mapMaybeWithKey :: (Key -> v -> Maybe u) -> KeyMap v -> KeyMap u
mapMaybeWithKey f (KeyMap m) = KeyMap (H.mapMaybeWithKey f m)

#endif

-------------------------------------------------------------------------------
-- combinators using existing abstractions
-------------------------------------------------------------------------------

-- | Return the value to which the specified key is mapped,
-- or Nothing if this map contains no mapping for the key.
--
-- This is a flipped version of 'lookup'.
--
-- @since 2.1.1.0
--
(!?) :: KeyMap v -> Key -> Maybe v
!? :: forall v. KeyMap v -> Key -> Maybe v
(!?) KeyMap v
m Key
k = forall v. Key -> KeyMap v -> Maybe v
lookup Key
k KeyMap v
m

-- | Generalized union with combining function.
alignWith :: (These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWith :: forall a b c. (These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWith These a b -> c
f (KeyMap Map Key a
x) (KeyMap Map Key b
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall (f :: * -> *) a b c.
Semialign f =>
(These a b -> c) -> f a -> f b -> f c
SA.alignWith These a b -> c
f Map Key a
x Map Key b
y)

-- | Generalized union with combining function.
alignWithKey :: (Key -> These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWithKey :: forall a b c.
(Key -> These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWithKey Key -> These a b -> c
f (KeyMap Map Key a
x) (KeyMap Map Key b
y) = forall v. Map Key v -> KeyMap v
KeyMap (forall i (f :: * -> *) a b c.
SemialignWithIndex i f =>
(i -> These a b -> c) -> f a -> f b -> f c
SAI.ialignWith Key -> These a b -> c
f Map Key a
x Map Key b
y)

-- | Convert a 'KeyMap' to a @'HashMap' 'Text'@.
toHashMapText :: KeyMap v -> HashMap Text  v
toHashMapText :: forall v. KeyMap v -> HashMap Text v
toHashMapText = forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
H.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
L.map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key -> Text
Key.toText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [(Key, v)]
toList

-- | Convert a @'HashMap' 'Text'@to a 'KeyMap'.
fromHashMapText :: HashMap Text v -> KeyMap v
fromHashMapText :: forall v. HashMap Text v -> KeyMap v
fromHashMapText = forall v. [(Key, v)] -> KeyMap v
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
L.map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Key
Key.fromText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. HashMap k v -> [(k, v)]
H.toList

-- | Convert a 'KeyMap' to a @'Map' 'Text'@.
--
-- @since 2.0.2.0
toMapText :: KeyMap v -> Map Text v
toMapText :: forall v. KeyMap v -> Map Text v
toMapText = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
L.map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key -> Text
Key.toText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [(Key, v)]
toList

-- | Convert a @'Map' 'Text'@to a 'KeyMap'.
--
-- @since 2.0.2.0
fromMapText :: Map Text v -> KeyMap v
fromMapText :: forall v. Map Text v -> KeyMap v
fromMapText = forall v. [(Key, v)] -> KeyMap v
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
L.map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Key
Key.fromText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
M.toList

-------------------------------------------------------------------------------
-- Instances
-------------------------------------------------------------------------------

-- This are defined using concrete combinators above.

instance Read v => Read (KeyMap v) where
    readPrec :: ReadPrec (KeyMap v)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
      Ident String
"fromList" <- ReadPrec Lexeme
lexP
      [(Key, v)]
xs <- forall a. Read a => ReadPrec a
readPrec
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall v. [(Key, v)] -> KeyMap v
fromList [(Key, v)]
xs)

    readListPrec :: ReadPrec [KeyMap v]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault

instance Show v => Show (KeyMap v) where
    showsPrec :: Int -> KeyMap v -> ShowS
showsPrec Int
d KeyMap v
m = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
      String -> ShowS
showString String
"fromList " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows (forall v. KeyMap v -> [(Key, v)]
toAscList KeyMap v
m)

instance F.Foldable KeyMap where
    foldMap :: forall m a. Monoid m => (a -> m) -> KeyMap a -> m
foldMap a -> m
f = forall m a. Monoid m => (Key -> a -> m) -> KeyMap a -> m
foldMapWithKey (\ Key
_k a
v -> a -> m
f a
v)
    {-# INLINE foldMap #-}
    foldr :: forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr = forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr
    foldr' :: forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr' = forall a b. (a -> b -> b) -> b -> KeyMap a -> b
foldr'
    foldl :: forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl = forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl
    foldl' :: forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl' = forall b a. (b -> a -> b) -> b -> KeyMap a -> b
foldl'
    null :: forall v. KeyMap v -> Bool
null = forall v. KeyMap v -> Bool
null
    length :: forall v. KeyMap v -> Int
length = forall v. KeyMap v -> Int
size

instance T.Traversable KeyMap where
    traverse :: forall (f :: * -> *) v1 v2.
Applicative f =>
(v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverse = forall (f :: * -> *) v1 v2.
Applicative f =>
(v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverse

instance Semigroup (KeyMap v) where
    <> :: KeyMap v -> KeyMap v -> KeyMap v
(<>) = forall v. KeyMap v -> KeyMap v -> KeyMap v
union

instance Monoid (KeyMap v) where
    mempty :: KeyMap v
mempty = forall v. KeyMap v
empty
    mappend :: KeyMap v -> KeyMap v -> KeyMap v
mappend = forall a. Semigroup a => a -> a -> a
(<>)

-- | @since 2.0.2.0
instance GHC.Exts.IsList (KeyMap v) where
    type Item (KeyMap v) = (Key, v)
    fromList :: [Item (KeyMap v)] -> KeyMap v
fromList = forall v. [(Key, v)] -> KeyMap v
fromList
    toList :: KeyMap v -> [Item (KeyMap v)]
toList   = forall v. KeyMap v -> [(Key, v)]
toAscList

-------------------------------------------------------------------------------
-- template-haskell
-------------------------------------------------------------------------------

instance TH.Lift v => TH.Lift (KeyMap v) where
    lift :: forall (m :: * -> *). Quote m => KeyMap v -> m Exp
lift KeyMap v
m = [| fromList m' |] where m' :: [(Key, v)]
m' = forall v. KeyMap v -> [(Key, v)]
toList KeyMap v
m

#if MIN_VERSION_template_haskell(2,17,0)
    liftTyped :: forall (m :: * -> *). Quote m => KeyMap v -> Code m (KeyMap v)
liftTyped = forall a (m :: * -> *). Quote m => m Exp -> Code m a
TH.unsafeCodeCoerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
TH.lift
#elif MIN_VERSION_template_haskell(2,16,0)
    liftTyped = TH.unsafeTExpCoerce . TH.lift
#endif

-------------------------------------------------------------------------------
-- hashable
-------------------------------------------------------------------------------

instance Hashable v => Hashable (KeyMap v) where
#ifdef USE_ORDEREDMAP
    hashWithSalt :: Int -> KeyMap v -> Int
hashWithSalt Int
salt (KeyMap Map Key v
m) = forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
M.foldlWithKey'
        (\Int
acc Key
k v
v -> Int
acc forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Key
k forall a. Hashable a => Int -> a -> Int
`hashWithSalt` v
v)
        (forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (forall k a. Map k a -> Int
M.size Map Key v
m)) Map Key v
m
#else
    hashWithSalt salt (KeyMap hm) = hashWithSalt salt hm
#endif

-------------------------------------------------------------------------------
-- deepseq
-------------------------------------------------------------------------------

instance NFData v => NFData (KeyMap v) where
    rnf :: KeyMap v -> ()
rnf (KeyMap Map Key v
hm) = forall a. NFData a => a -> ()
rnf Map Key v
hm

-------------------------------------------------------------------------------
-- indexed-traversable
-------------------------------------------------------------------------------

instance WI.FunctorWithIndex Key KeyMap where
    imap :: forall a b. (Key -> a -> b) -> KeyMap a -> KeyMap b
imap = forall a b. (Key -> a -> b) -> KeyMap a -> KeyMap b
mapWithKey

instance WI.FoldableWithIndex Key KeyMap where
    ifoldr :: forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
ifoldr   = forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
foldrWithKey

instance WI.TraversableWithIndex Key KeyMap where
    itraverse :: forall (f :: * -> *) v1 v2.
Applicative f =>
(Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
itraverse = forall (f :: * -> *) v1 v2.
Applicative f =>
(Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseWithKey

-------------------------------------------------------------------------------
-- semialign
-------------------------------------------------------------------------------

instance SA.Zip KeyMap where
    zipWith :: forall a b c. (a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
zipWith = forall a b c. (a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWith

instance SAI.ZipWithIndex Key KeyMap where
    izipWith :: forall a b c.
(Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
izipWith = forall a b c.
(Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
intersectionWithKey

instance SA.Semialign KeyMap where
    alignWith :: forall a b c. (These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWith = forall a b c. (These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWith

instance SAI.SemialignWithIndex Key KeyMap where
    ialignWith :: forall a b c.
(Key -> These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
ialignWith = forall a b c.
(Key -> These a b -> c) -> KeyMap a -> KeyMap b -> KeyMap c
alignWithKey

instance SA.Align KeyMap where
    nil :: forall v. KeyMap v
nil = forall v. KeyMap v
empty

-------------------------------------------------------------------------------
-- witherable
-------------------------------------------------------------------------------

instance W.Filterable KeyMap where
    filter :: forall v. (v -> Bool) -> KeyMap v -> KeyMap v
filter = forall v. (v -> Bool) -> KeyMap v -> KeyMap v
filter
    mapMaybe :: forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
mapMaybe = forall a b. (a -> Maybe b) -> KeyMap a -> KeyMap b
mapMaybe

instance W.Witherable KeyMap where

instance W.FilterableWithIndex Key KeyMap where
    ifilter :: forall v. (Key -> v -> Bool) -> KeyMap v -> KeyMap v
ifilter = forall v. (Key -> v -> Bool) -> KeyMap v -> KeyMap v
filterWithKey
    imapMaybe :: forall v u. (Key -> v -> Maybe u) -> KeyMap v -> KeyMap u
imapMaybe = forall v u. (Key -> v -> Maybe u) -> KeyMap v -> KeyMap u
mapMaybeWithKey

instance W.WitherableWithIndex Key KeyMap where

-------------------------------------------------------------------------------
-- QuickCheck
-------------------------------------------------------------------------------

-- | @since 2.0.3.0
instance QC.Arbitrary1 KeyMap where
    liftArbitrary :: forall a. Gen a -> Gen (KeyMap a)
liftArbitrary Gen a
a  = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall v. [(Key, v)] -> KeyMap v
fromList (forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
QC.liftArbitrary (forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
QC.liftArbitrary Gen a
a))
    liftShrink :: forall a. (a -> [a]) -> KeyMap a -> [KeyMap a]
liftShrink a -> [a]
shr KeyMap a
m = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall v. [(Key, v)] -> KeyMap v
fromList (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
QC.liftShrink (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
QC.liftShrink a -> [a]
shr) (forall v. KeyMap v -> [(Key, v)]
toList KeyMap a
m))

-- | @since 2.0.3.0
instance QC.Arbitrary v => QC.Arbitrary (KeyMap v) where
    arbitrary :: Gen (KeyMap v)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
QC.arbitrary1
    shrink :: KeyMap v -> [KeyMap v]
shrink    = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
QC.shrink1

-- | @since 2.0.3.0
instance QC.CoArbitrary v => QC.CoArbitrary (KeyMap v) where
    coarbitrary :: forall b. KeyMap v -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
QC.coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [(Key, v)]
toList

-- | @since 2.0.3.0
instance QC.Function v => QC.Function (KeyMap v) where
    function :: forall b. (KeyMap v -> b) -> KeyMap v :-> b
function = forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
QC.functionMap forall v. KeyMap v -> [(Key, v)]
toList forall v. [(Key, v)] -> KeyMap v
fromList