{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.IntMap.NonEmpty.Internal (
NEIntMap(..)
, Key
, singleton
, nonEmptyMap
, withNonEmpty
, fromList
, toList
, map
, insertWith
, union
, unions
, elems
, size
, toMap
, foldr
, foldr'
, foldr1
, foldl
, foldl'
, foldl1
, traverseWithKey
, traverseWithKey1
, foldMapWithKey
, traverseMapWithKey
, insertMinMap
, insertMaxMap
, valid
, lookupMinMap
, lookupMaxMap
) where
import Control.Applicative
import Control.Comonad
import Control.DeepSeq
import Control.Monad
import Data.Coerce
import Data.Data
import Data.Function
import Data.Functor.Alt
import Data.Functor.Classes
import Data.Functor.Invariant
import Data.IntMap.Internal (IntMap(..), Key)
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe
import Data.Semigroup
import Data.Semigroup.Foldable (Foldable1(fold1))
import Data.Semigroup.Traversable (Traversable1(..))
import Prelude hiding (Foldable(..), map)
import Text.Read
import qualified Data.Aeson as A
import qualified Data.Foldable as F
import qualified Data.IntMap as M
import qualified Data.List as L
import qualified Data.Semigroup.Foldable as F1
data NEIntMap a =
NEIntMap { forall a. NEIntMap a -> Key
neimK0 :: !Key
, forall a. NEIntMap a -> a
neimV0 :: a
, forall a. NEIntMap a -> IntMap a
neimIntMap :: !(IntMap a)
}
deriving (Typeable)
instance Eq a => Eq (NEIntMap a) where
NEIntMap a
t1 == :: NEIntMap a -> NEIntMap a -> Bool
== NEIntMap a
t2 = forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap a
t1) forall a. Eq a => a -> a -> Bool
== forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap a
t2)
Bool -> Bool -> Bool
&& forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
t1 forall a. Eq a => a -> a -> Bool
== forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
t2
instance Ord a => Ord (NEIntMap a) where
compare :: NEIntMap a -> NEIntMap a -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. NEIntMap a -> NonEmpty (Key, a)
toList
< :: NEIntMap a -> NEIntMap a -> Bool
(<) = forall a. Ord a => a -> a -> Bool
(<) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. NEIntMap a -> NonEmpty (Key, a)
toList
> :: NEIntMap a -> NEIntMap a -> Bool
(>) = forall a. Ord a => a -> a -> Bool
(>) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. NEIntMap a -> NonEmpty (Key, a)
toList
<= :: NEIntMap a -> NEIntMap a -> Bool
(<=) = forall a. Ord a => a -> a -> Bool
(<=) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. NEIntMap a -> NonEmpty (Key, a)
toList
>= :: NEIntMap a -> NEIntMap a -> Bool
(>=) = forall a. Ord a => a -> a -> Bool
(>=) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. NEIntMap a -> NonEmpty (Key, a)
toList
instance Eq1 NEIntMap where
liftEq :: forall a b. (a -> b -> Bool) -> NEIntMap a -> NEIntMap b -> Bool
liftEq a -> b -> Bool
eq NEIntMap a
m1 NEIntMap b
m2 = forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap a
m1) forall a. Eq a => a -> a -> Bool
== forall a. IntMap a -> Key
M.size (forall a. NEIntMap a -> IntMap a
neimIntMap NEIntMap b
m2)
Bool -> Bool -> Bool
&& forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq) (forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
m1) (forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap b
m2)
instance Ord1 NEIntMap where
liftCompare :: forall a b.
(a -> b -> Ordering) -> NEIntMap a -> NEIntMap b -> Ordering
liftCompare a -> b -> Ordering
cmp NEIntMap a
m NEIntMap b
n =
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare (forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
cmp) (forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
m) (forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap b
n)
instance Show1 NEIntMap where
liftShowsPrec :: forall a.
(Key -> a -> ShowS) -> ([a] -> ShowS) -> Key -> NEIntMap a -> ShowS
liftShowsPrec Key -> a -> ShowS
sp [a] -> ShowS
sl Key
d NEIntMap a
m =
forall a. (Key -> a -> ShowS) -> String -> Key -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Key -> a -> ShowS) -> ([a] -> ShowS) -> Key -> f a -> ShowS
liftShowsPrec Key -> (Key, a) -> ShowS
sp' [(Key, a)] -> ShowS
sl') String
"fromList" Key
d (forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
m)
where
sp' :: Key -> (Key, a) -> ShowS
sp' = forall (f :: * -> *) a.
Show1 f =>
(Key -> a -> ShowS) -> ([a] -> ShowS) -> Key -> f a -> ShowS
liftShowsPrec Key -> a -> ShowS
sp [a] -> ShowS
sl
sl' :: [(Key, a)] -> ShowS
sl' = forall (f :: * -> *) a.
Show1 f =>
(Key -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Key -> a -> ShowS
sp [a] -> ShowS
sl
instance Read1 NEIntMap where
liftReadsPrec :: forall a.
(Key -> ReadS a) -> ReadS [a] -> Key -> ReadS (NEIntMap a)
liftReadsPrec Key -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Key -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
forall a t.
(Key -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Key -> ReadS a) -> ReadS [a] -> Key -> ReadS (f a)
liftReadsPrec Key -> ReadS (Key, a)
rp' ReadS [(Key, a)]
rl') String
"fromList" forall a. NonEmpty (Key, a) -> NEIntMap a
fromList
where
rp' :: Key -> ReadS (Key, a)
rp' = forall (f :: * -> *) a.
Read1 f =>
(Key -> ReadS a) -> ReadS [a] -> Key -> ReadS (f a)
liftReadsPrec Key -> ReadS a
rp ReadS [a]
rl
rl' :: ReadS [(Key, a)]
rl' = forall (f :: * -> *) a.
Read1 f =>
(Key -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Key -> ReadS a
rp ReadS [a]
rl
instance Read e => Read (NEIntMap e) where
readPrec :: ReadPrec (NEIntMap e)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ forall a. Key -> ReadPrec a -> ReadPrec a
prec Key
10 forall a b. (a -> b) -> a -> b
$ do
Ident String
"fromList" <- ReadPrec Lexeme
lexP
NonEmpty (Key, e)
xs <- forall a. ReadPrec a -> ReadPrec a
parens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Key -> ReadPrec a -> ReadPrec a
prec Key
10 forall a b. (a -> b) -> a -> b
$ forall a. Read a => ReadPrec a
readPrec
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. NonEmpty (Key, a) -> NEIntMap a
fromList NonEmpty (Key, e)
xs)
readListPrec :: ReadPrec [NEIntMap e]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault
instance Show a => Show (NEIntMap a) where
showsPrec :: Key -> NEIntMap a -> ShowS
showsPrec Key
d NEIntMap a
m = Bool -> ShowS -> ShowS
showParen (Key
d forall a. Ord a => a -> a -> Bool
> Key
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 a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
m) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
")"
instance NFData a => NFData (NEIntMap a) where
rnf :: NEIntMap a -> ()
rnf (NEIntMap Key
k a
v IntMap a
a) = forall a. NFData a => a -> ()
rnf Key
k seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
v seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf IntMap a
a
instance Data a => Data (NEIntMap a) where
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NEIntMap a -> c (NEIntMap a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z NEIntMap a
im = forall g. g -> c g
z forall a. NonEmpty (Key, a) -> NEIntMap a
fromList forall d b. Data d => c (d -> b) -> d -> c b
`f` forall a. NEIntMap a -> NonEmpty (Key, a)
toList NEIntMap a
im
toConstr :: NEIntMap a -> Constr
toConstr NEIntMap a
_ = Constr
fromListConstr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (NEIntMap a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Key
constrIndex Constr
c of
Key
1 -> forall b r. Data b => c (b -> r) -> c r
k (forall r. r -> c r
z forall a. NonEmpty (Key, a) -> NEIntMap a
fromList)
Key
_ -> forall a. HasCallStack => String -> a
error String
"gunfold"
dataTypeOf :: NEIntMap a -> DataType
dataTypeOf NEIntMap a
_ = DataType
intMapDataType
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (NEIntMap a))
dataCast1 forall d. Data d => c (t d)
f = 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 forall d. Data d => c (t d)
f
fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
intMapDataType String
"fromList" [] Fixity
Prefix
intMapDataType :: DataType
intMapDataType :: DataType
intMapDataType = String -> [Constr] -> DataType
mkDataType String
"Data.IntMap.NonEmpty.Internal.NEIntMap" [Constr
fromListConstr]
instance A.ToJSON a => A.ToJSON (NEIntMap a) where
toJSON :: NEIntMap a -> Value
toJSON = forall a. ToJSON a => a -> Value
A.toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap
toEncoding :: NEIntMap a -> Encoding
toEncoding = forall a. ToJSON a => a -> Encoding
A.toEncoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap
instance A.FromJSON a => A.FromJSON (NEIntMap a) where
parseJSON :: Value -> Parser (NEIntMap a)
parseJSON = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err) forall (f :: * -> *) a. Applicative f => a -> f a
pure
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a. FromJSON a => Value -> Parser a
A.parseJSON
where
err :: String
err = String
"NEIntMap: Non-empty map expected, but empty map found"
instance Alt NEIntMap where
<!> :: forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
(<!>) = forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
union
foldr :: (a -> b -> b) -> b -> NEIntMap a -> b
foldr :: forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr a -> b -> b
f b
z (NEIntMap Key
_ a
v IntMap a
m) = a
v a -> b -> b
`f` forall a b. (a -> b -> b) -> b -> IntMap a -> b
M.foldr a -> b -> b
f b
z IntMap a
m
{-# INLINE foldr #-}
foldr' :: (a -> b -> b) -> b -> NEIntMap a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr' a -> b -> b
f b
z (NEIntMap Key
_ a
v IntMap a
m) = a
v a -> b -> b
`f` b
y
where
!y :: b
y = forall a b. (a -> b -> b) -> b -> IntMap a -> b
M.foldr' a -> b -> b
f b
z IntMap a
m
{-# INLINE foldr' #-}
foldr1 :: (a -> a -> a) -> NEIntMap a -> a
foldr1 :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldr1 a -> a -> a
f (NEIntMap Key
_ a
v IntMap a
m) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe a
v (a -> a -> a
f a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall a b. (a -> b -> b) -> b -> IntMap a -> b
M.foldr a -> a -> a
f))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe (a, IntMap a)
M.maxView
forall a b. (a -> b) -> a -> b
$ IntMap a
m
{-# INLINE foldr1 #-}
foldl :: (a -> b -> a) -> a -> NEIntMap b -> a
foldl :: forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl a -> b -> a
f a
z (NEIntMap Key
_ b
v IntMap b
m) = forall a b. (a -> b -> a) -> a -> IntMap b -> a
M.foldl a -> b -> a
f (a -> b -> a
f a
z b
v) IntMap b
m
{-# INLINE foldl #-}
foldl' :: (a -> b -> a) -> a -> NEIntMap b -> a
foldl' :: forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl' a -> b -> a
f a
z (NEIntMap Key
_ b
v IntMap b
m) = forall a b. (a -> b -> a) -> a -> IntMap b -> a
M.foldl' a -> b -> a
f a
x IntMap b
m
where
!x :: a
x = a -> b -> a
f a
z b
v
{-# INLINE foldl' #-}
foldl1 :: (a -> a -> a) -> NEIntMap a -> a
foldl1 :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldl1 a -> a -> a
f (NEIntMap Key
_ a
v IntMap a
m) = forall a b. (a -> b -> a) -> a -> IntMap b -> a
M.foldl a -> a -> a
f a
v IntMap a
m
{-# INLINE foldl1 #-}
foldMapWithKey
:: Semigroup m
=> (Key -> a -> m)
-> NEIntMap a
-> m
foldMapWithKey :: forall m a. Semigroup m => (Key -> a -> m) -> NEIntMap a -> m
foldMapWithKey Key -> a -> m
f = forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
F1.foldMap1 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Key -> a -> m
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> NonEmpty (Key, a)
toList
{-# INLINE foldMapWithKey #-}
map :: (a -> b) -> NEIntMap a -> NEIntMap b
map :: forall a b. (a -> b) -> NEIntMap a -> NEIntMap b
map a -> b
f (NEIntMap Key
k0 a
v IntMap a
m) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 (a -> b
f a
v) (forall a b. (a -> b) -> IntMap a -> IntMap b
M.map a -> b
f IntMap a
m)
{-# NOINLINE [1] map #-}
{-# RULES
"map/map" forall f g xs . map f (map g xs) = map (f . g) xs
#-}
{-# RULES
"map/coerce" map coerce = coerce
#-}
union
:: NEIntMap a
-> NEIntMap a
-> NEIntMap a
union :: forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
union n1 :: NEIntMap a
n1@(NEIntMap Key
k1 a
v1 IntMap a
m1) n2 :: NEIntMap a
n2@(NEIntMap Key
k2 a
v2 IntMap a
m2) = case forall a. Ord a => a -> a -> Ordering
compare Key
k1 Key
k2 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 a
v1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> IntMap a -> IntMap a
M.union IntMap a
m1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n2
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k1 a
v1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> IntMap a -> IntMap a
M.union IntMap a
m1 forall a b. (a -> b) -> a -> b
$ IntMap a
m2
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k2 a
v2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> IntMap a -> IntMap a
M.union (forall a. NEIntMap a -> IntMap a
toMap NEIntMap a
n1) forall a b. (a -> b) -> a -> b
$ IntMap a
m2
{-# INLINE union #-}
unions
:: Foldable1 f
=> f (NEIntMap a)
-> NEIntMap a
unions :: forall (f :: * -> *) a. Foldable1 f => f (NEIntMap a) -> NEIntMap a
unions (forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
F1.toNonEmpty->(NEIntMap a
m :| [NEIntMap a]
ms)) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
union NEIntMap a
m [NEIntMap a]
ms
{-# INLINE unions #-}
elems :: NEIntMap a -> NonEmpty a
elems :: forall a. NEIntMap a -> NonEmpty a
elems (NEIntMap Key
_ a
v IntMap a
m) = a
v forall a. a -> [a] -> NonEmpty a
:| forall a. IntMap a -> [a]
M.elems IntMap a
m
{-# INLINE elems #-}
size :: NEIntMap a -> Int
size :: forall a. NEIntMap a -> Key
size (NEIntMap Key
_ a
_ IntMap a
m) = Key
1 forall a. Num a => a -> a -> a
+ forall a. IntMap a -> Key
M.size IntMap a
m
{-# INLINE size #-}
toMap :: NEIntMap a -> IntMap a
toMap :: forall a. NEIntMap a -> IntMap a
toMap (NEIntMap Key
k a
v IntMap a
m) = forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap Key
k a
v IntMap a
m
{-# INLINE toMap #-}
traverseWithKey
:: Applicative t
=> (Key -> a -> t b)
-> NEIntMap a
-> t (NEIntMap b)
traverseWithKey :: forall (t :: * -> *) a b.
Applicative t =>
(Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
traverseWithKey Key -> a -> t b
f (NEIntMap Key
k a
v IntMap a
m0) =
forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> a -> t b
f Key
k a
v
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (t :: * -> *) a b.
Applicative t =>
(Key -> a -> t b) -> IntMap a -> t (IntMap b)
traverseMapWithKey Key -> a -> t b
f IntMap a
m0
{-# INLINE traverseWithKey #-}
traverseWithKey1
:: Apply t
=> (Key -> a -> t b)
-> NEIntMap a
-> t (NEIntMap b)
traverseWithKey1 :: forall (t :: * -> *) a b.
Apply t =>
(Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
traverseWithKey1 Key -> a -> t b
f (NEIntMap Key
k0 a
v IntMap a
m0) = case forall (f :: * -> *) a. MaybeApply f a -> Either (f a) a
runMaybeApply MaybeApply t (IntMap b)
m1 of
Left t (IntMap b)
m2 -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> a -> t b
f Key
k0 a
v forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> t (IntMap b)
m2
Right IntMap b
m2 -> forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0) IntMap b
m2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> a -> t b
f Key
k0 a
v
where
m1 :: MaybeApply t (IntMap b)
m1 = forall (t :: * -> *) a b.
Applicative t =>
(Key -> a -> t b) -> IntMap a -> t (IntMap b)
traverseMapWithKey (\Key
k -> forall (f :: * -> *) a. Either (f a) a -> MaybeApply f a
MaybeApply forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> a -> t b
f Key
k) IntMap a
m0
{-# INLINABLE traverseWithKey1 #-}
toList :: NEIntMap a -> NonEmpty (Key, a)
toList :: forall a. NEIntMap a -> NonEmpty (Key, a)
toList (NEIntMap Key
k a
v IntMap a
m) = (Key
k,a
v) forall a. a -> [a] -> NonEmpty a
:| forall a. IntMap a -> [(Key, a)]
M.toList IntMap a
m
{-# INLINE toList #-}
nonEmptyMap :: IntMap a -> Maybe (NEIntMap a)
nonEmptyMap :: forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap = (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry) forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe ((Key, a), IntMap a)
M.minViewWithKey
{-# INLINE nonEmptyMap #-}
withNonEmpty
:: r
-> (NEIntMap a -> r)
-> IntMap a
-> r
withNonEmpty :: forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty r
def NEIntMap a -> r
f = forall b a. b -> (a -> b) -> Maybe a -> b
maybe r
def NEIntMap a -> r
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> Maybe (NEIntMap a)
nonEmptyMap
{-# INLINE withNonEmpty #-}
fromList :: NonEmpty (Key, a) -> NEIntMap a
fromList :: forall a. NonEmpty (Key, a) -> NEIntMap a
fromList ((Key
k, a
v) :| [(Key, a)]
xs) = forall r a. r -> (NEIntMap a -> r) -> IntMap a -> r
withNonEmpty (forall a. Key -> a -> NEIntMap a
singleton Key
k a
v) (forall a. (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWith (forall a b. a -> b -> a
const forall a. a -> a
id) Key
k a
v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [(Key, a)] -> IntMap a
M.fromList
forall a b. (a -> b) -> a -> b
$ [(Key, a)]
xs
{-# INLINE fromList #-}
singleton :: Key -> a -> NEIntMap a
singleton :: forall a. Key -> a -> NEIntMap a
singleton Key
k a
v = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v forall a. IntMap a
M.empty
{-# INLINE singleton #-}
insertWith
:: (a -> a -> a)
-> Key
-> a
-> NEIntMap a
-> NEIntMap a
insertWith :: forall a. (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
insertWith a -> a -> a
f Key
k a
v n :: NEIntMap a
n@(NEIntMap Key
k0 a
v0 IntMap a
m) = case forall a. Ord a => a -> a -> Ordering
compare Key
k Key
k0 of
Ordering
LT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> IntMap a
toMap forall a b. (a -> b) -> a -> b
$ NEIntMap a
n
Ordering
EQ -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k (a -> a -> a
f a
v a
v0) IntMap a
m
Ordering
GT -> forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 a
v0 forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
M.insertWith a -> a -> a
f Key
k a
v IntMap a
m
{-# INLINE insertWith #-}
instance Semigroup (NEIntMap a) where
<> :: NEIntMap a -> NEIntMap a -> NEIntMap a
(<>) = forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
union
{-# INLINE (<>) #-}
sconcat :: NonEmpty (NEIntMap a) -> NEIntMap a
sconcat = forall (f :: * -> *) a. Foldable1 f => f (NEIntMap a) -> NEIntMap a
unions
{-# INLINE sconcat #-}
instance Functor NEIntMap where
fmap :: forall a b. (a -> b) -> NEIntMap a -> NEIntMap b
fmap = forall a b. (a -> b) -> NEIntMap a -> NEIntMap b
map
{-# INLINE fmap #-}
a
x <$ :: forall a b. a -> NEIntMap b -> NEIntMap a
<$ NEIntMap Key
k b
_ IntMap b
m = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
x (a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ IntMap b
m)
{-# INLINE (<$) #-}
instance Invariant NEIntMap where
invmap :: forall a b. (a -> b) -> (b -> a) -> NEIntMap a -> NEIntMap b
invmap a -> b
f b -> a
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f
{-# INLINE invmap #-}
instance F.Foldable NEIntMap where
#if MIN_VERSION_base(4,11,0)
fold :: forall m. Monoid m => NEIntMap m -> m
fold (NEIntMap Key
_ m
v IntMap m
m) = m
v forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
F.fold (forall a. IntMap a -> [a]
M.elems IntMap m
m)
{-# INLINE fold #-}
foldMap :: forall m a. Monoid m => (a -> m) -> NEIntMap a -> m
foldMap a -> m
f (NEIntMap Key
_ a
v IntMap a
m) = a -> m
f a
v forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap a -> m
f (forall a. IntMap a -> [a]
M.elems IntMap a
m)
{-# INLINE foldMap #-}
#else
fold (NEIntMap _ v m) = v `mappend` F.fold (M.elems m)
{-# INLINE fold #-}
foldMap f (NEIntMap _ v m) = f v `mappend` F.foldMap f (M.elems m)
{-# INLINE foldMap #-}
#endif
foldr :: forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr = forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr
{-# INLINE foldr #-}
foldr' :: forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr' = forall a b. (a -> b -> b) -> b -> NEIntMap a -> b
foldr'
{-# INLINE foldr' #-}
foldr1 :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldr1 = forall a. (a -> a -> a) -> NEIntMap a -> a
foldr1
{-# INLINE foldr1 #-}
foldl :: forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl = forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl
{-# INLINE foldl #-}
foldl' :: forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl' = forall a b. (a -> b -> a) -> a -> NEIntMap b -> a
foldl'
{-# INLINE foldl' #-}
foldl1 :: forall a. (a -> a -> a) -> NEIntMap a -> a
foldl1 = forall a. (a -> a -> a) -> NEIntMap a -> a
foldl1
{-# INLINE foldl1 #-}
null :: forall a. NEIntMap a -> Bool
null NEIntMap a
_ = Bool
False
{-# INLINE null #-}
length :: forall a. NEIntMap a -> Key
length = forall a. NEIntMap a -> Key
size
{-# INLINE length #-}
elem :: forall a. Eq a => a -> NEIntMap a -> Bool
elem a
x (NEIntMap Key
_ a
v IntMap a
m) = forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
F.elem a
x IntMap a
m
Bool -> Bool -> Bool
|| a
x forall a. Eq a => a -> a -> Bool
== a
v
{-# INLINE elem #-}
toList :: forall a. NEIntMap a -> [a]
toList = forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NEIntMap a -> NonEmpty a
elems
{-# INLINE toList #-}
instance Traversable NEIntMap where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NEIntMap a -> f (NEIntMap b)
traverse a -> f b
f = forall (t :: * -> *) a b.
Applicative t =>
(Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
traverseWithKey (forall a b. a -> b -> a
const a -> f b
f)
{-# INLINE traverse #-}
instance Foldable1 NEIntMap where
#if MIN_VERSION_base(4,11,0)
fold1 :: forall m. Semigroup m => NEIntMap m -> m
fold1 (NEIntMap Key
_ m
v IntMap m
m) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe m
v (m
v forall a. Semigroup a => a -> a -> a
<>)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap forall a. a -> Maybe a
Just
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> [a]
M.elems
forall a b. (a -> b) -> a -> b
$ IntMap m
m
#else
fold1 (NEIntMap _ v m) = option v (v <>)
. F.foldMap (Option . Just)
. M.elems
$ m
#endif
{-# INLINE fold1 #-}
foldMap1 :: forall m a. Semigroup m => (a -> m) -> NEIntMap a -> m
foldMap1 a -> m
f = forall m a. Semigroup m => (Key -> a -> m) -> NEIntMap a -> m
foldMapWithKey (forall a b. a -> b -> a
const a -> m
f)
{-# INLINE foldMap1 #-}
toNonEmpty :: forall a. NEIntMap a -> NonEmpty a
toNonEmpty = forall a. NEIntMap a -> NonEmpty a
elems
{-# INLINE toNonEmpty #-}
instance Traversable1 NEIntMap where
traverse1 :: forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> NEIntMap a -> f (NEIntMap b)
traverse1 a -> f b
f = forall (t :: * -> *) a b.
Apply t =>
(Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
traverseWithKey1 (forall a b. a -> b -> a
const a -> f b
f)
{-# INLINE traverse1 #-}
instance Comonad NEIntMap where
extract :: forall a. NEIntMap a -> a
extract = forall a. NEIntMap a -> a
neimV0
{-# INLINE extract #-}
duplicate :: forall a. NEIntMap a -> NEIntMap (NEIntMap a)
duplicate n0 :: NEIntMap a
n0@(NEIntMap Key
k0 a
_ IntMap a
m0) = forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k0 NEIntMap a
n0
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [(Key, a)] -> IntMap a
M.fromDistinctAscList
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
L.mapAccumL forall {a}. IntMap a -> (Key, a) -> (IntMap a, (Key, NEIntMap a))
go IntMap a
m0
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> [(Key, a)]
M.toList
forall a b. (a -> b) -> a -> b
$ IntMap a
m0
where
go :: IntMap a -> (Key, a) -> (IntMap a, (Key, NEIntMap a))
go IntMap a
m (Key
k, a
v) = (IntMap a
m', (Key
k, forall a. Key -> a -> IntMap a -> NEIntMap a
NEIntMap Key
k a
v IntMap a
m'))
where
!m' :: IntMap a
m' = forall a. IntMap a -> IntMap a
M.deleteMin IntMap a
m
{-# INLINE duplicate #-}
valid :: NEIntMap a -> Bool
valid :: forall a. NEIntMap a -> Bool
valid (NEIntMap Key
k a
_ IntMap a
m) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Key
k forall a. Ord a => a -> a -> Bool
<) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) (forall a. IntMap a -> Maybe ((Key, a), IntMap a)
M.minViewWithKey IntMap a
m)
insertMinMap :: Key -> a -> IntMap a -> IntMap a
insertMinMap :: forall a. Key -> a -> IntMap a -> IntMap a
insertMinMap = forall a. Key -> a -> IntMap a -> IntMap a
M.insert
{-# INLINABLE insertMinMap #-}
insertMaxMap :: Key -> a -> IntMap a -> IntMap a
insertMaxMap :: forall a. Key -> a -> IntMap a -> IntMap a
insertMaxMap = forall a. Key -> a -> IntMap a -> IntMap a
M.insert
{-# INLINABLE insertMaxMap #-}
traverseMapWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)
traverseMapWithKey :: forall (t :: * -> *) a b.
Applicative t =>
(Key -> a -> t b) -> IntMap a -> t (IntMap b)
traverseMapWithKey Key -> a -> t b
f = IntMap a -> t (IntMap b)
go
where
go :: IntMap a -> t (IntMap b)
go IntMap a
Nil = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. IntMap a
Nil
go (Tip Key
k a
v) = forall a. Key -> a -> IntMap a
Tip Key
k forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> a -> t b
f Key
k a
v
go (Bin Key
p Key
m IntMap a
l IntMap a
r) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. Key -> Key -> IntMap a -> IntMap a -> IntMap a
Bin Key
p Key
m)) (IntMap a -> t (IntMap b)
go IntMap a
r) (IntMap a -> t (IntMap b)
go IntMap a
l)
{-# INLINE traverseMapWithKey #-}
lookupMinMap :: IntMap a -> Maybe (Key, a)
#if MIN_VERSION_containers(0,5,11)
lookupMinMap :: forall a. IntMap a -> Maybe (Key, a)
lookupMinMap = forall a. IntMap a -> Maybe (Key, a)
M.lookupMin
#else
lookupMinMap = fmap fst . M.minViewWithKey
#endif
{-# INLINE lookupMinMap #-}
lookupMaxMap :: IntMap a -> Maybe (Key, a)
#if MIN_VERSION_containers(0,5,11)
lookupMaxMap :: forall a. IntMap a -> Maybe (Key, a)
lookupMaxMap = forall a. IntMap a -> Maybe (Key, a)
M.lookupMax
#else
lookupMaxMap = fmap fst . M.maxViewWithKey
#endif
{-# INLINE lookupMaxMap #-}