{-# LANGUAGE BangPatterns       #-}
{-# LANGUAGE CPP                #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ViewPatterns       #-}
{-# OPTIONS_HADDOCK not-home    #-}

-- |
-- Module      : Data.IntMap.NonEmpty.Internal
-- Copyright   : (c) Justin Le 2018
-- License     : BSD3
--
-- Maintainer  : justin@jle.im
-- Stability   : experimental
-- Portability : non-portable
--
-- Unsafe internal-use functions used in the implementation of
-- "Data.IntMap.NonEmpty".  These functions can potentially be used to
-- break the abstraction of 'NEIntMap' and produce unsound maps, so be
-- wary!
module Data.IntMap.NonEmpty.Internal (
  -- * Non-Empty IntMap type
    NEIntMap(..)
  , Key
  , singleton
  , nonEmptyMap
  , withNonEmpty
  , fromList
  , toList
  , map
  , insertWith
  , union
  , unions
  , elems
  , size
  , toMap
  -- * Folds
  , foldr
  , foldr'
  , foldr1
  , foldl
  , foldl'
  , foldl1
  -- * Traversals
  , traverseWithKey
  , traverseWithKey1
  , foldMapWithKey
  , traverseMapWithKey
  -- * Unsafe IntMap Functions
  , insertMinMap
  , insertMaxMap
  -- * Debug
  , valid
  -- * CPP compatibility
  , 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

-- | A non-empty (by construction) map from integer keys to values @a@.  At
-- least one key-value pair exists in an @'NEIntMap' v@ at all times.
--
-- Functions that /take/ an 'NEIntMap' can safely operate on it with the
-- assumption that it has at least one key-value pair.
--
-- Functions that /return/ an 'NEIntMap' provide an assurance that the result
-- has at least one key-value pair.
--
-- "Data.IntMap.NonEmpty" re-exports the API of "Data.IntMap", faithfully
-- reproducing asymptotics, typeclass constraints, and semantics.
-- Functions that ensure that input and output maps are both non-empty
-- (like 'Data.IntMap.NonEmpty.insert') return 'NEIntMap', but functions that
-- might potentially return an empty map (like 'Data.IntMap.NonEmpty.delete')
-- return a 'IntMap' instead.
--
-- You can directly construct an 'NEIntMap' with the API from
-- "Data.IntMap.NonEmpty"; it's more or less the same as constructing a normal
-- 'IntMap', except you don't have access to 'Data.IntMap.empty'.  There are also
-- a few ways to construct an 'NEIntMap' from a 'IntMap':
--
-- 1.  The 'nonEmptyMap' smart constructor will convert a @'IntMap' k a@ into
--     a @'Maybe' ('NEIntMap' k a)@, returning 'Nothing' if the original 'IntMap'
--     was empty.
-- 2.  You can use the 'Data.IntMap.NonEmpty.insertIntMap' family of functions to
--     insert a value into a 'IntMap' to create a guaranteed 'NEIntMap'.
-- 3.  You can use the 'Data.IntMap.NonEmpty.IsNonEmpty' and
--     'Data.IntMap.NonEmpty.IsEmpty' patterns to "pattern match" on a 'IntMap'
--     to reveal it as either containing a 'NEIntMap' or an empty map.
-- 4.  'withNonEmpty' offers a continuation-based interface for
--     deconstructing a 'IntMap' and treating it as if it were an
--     'NEIntMap'.
--
-- You can convert an 'NEIntMap' into a 'IntMap' with 'toMap' or
-- 'Data.IntMap.NonEmpty.IsNonEmpty', essentially "obscuring" the non-empty
-- property from the type.
data NEIntMap a =
    NEIntMap { forall a. NEIntMap a -> Key
neimK0     :: !Key    -- ^ invariant: must be smaller than smallest key in map
             , 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

-- Data instance code from Data.IntMap.Internal
--
-- Copyright   :  (c) Daan Leijen 2002
--                (c) Andriy Palamarchuk 2008
--                (c) wren romano 2016
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"

-- | @since 0.3.4.4
instance Alt NEIntMap where
    <!> :: forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
(<!>) = forall a. NEIntMap a -> NEIntMap a -> NEIntMap a
union

-- | /O(n)/. Fold the values in the map using the given right-associative
-- binary operator, such that @'foldr' f z == 'Prelude.foldr' f z . 'elems'@.
--
-- > elemsList map = foldr (:) [] map
--
-- > let f a len = len + (length a)
-- > foldr f 0 (fromList ((5,"a") :| [(3,"bbb")])) == 4
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 #-}

-- | /O(n)/. A strict version of 'foldr'. Each application of the operator
-- is evaluated before using the result in the next application. This
-- function is strict in the starting value.
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' #-}

-- | /O(n)/. A version of 'foldr' that uses the value at the maximal key in
-- the map as the starting value.
--
-- Note that, unlike 'Data.Foldable.foldr1' for 'IntMap', this function is
-- total if the input function is total.
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 #-}

-- | /O(n)/. Fold the values in the map using the given left-associative
-- binary operator, such that @'foldl' f z == 'Prelude.foldl' f z . 'elems'@.
--
-- > elemsList = reverse . foldl (flip (:)) []
--
-- > let f len a = len + (length a)
-- > foldl f 0 (fromList ((5,"a") :| [(3,"bbb")])) == 4
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 #-}

-- | /O(n)/. A strict version of 'foldl'. Each application of the operator
-- is evaluated before using the result in the next application. This
-- function is strict in the starting value.
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' #-}

-- | /O(n)/. A version of 'foldl' that uses the value at the minimal key in
-- the map as the starting value.
--
-- Note that, unlike 'Data.Foldable.foldl1' for 'IntMap', this function is
-- total if the input function is total.
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 #-}

-- | /O(n)/. Fold the keys and values in the map using the given semigroup,
-- such that
--
-- @'foldMapWithKey' f = 'Data.Semigroup.Foldable.fold1' . 'Data.IntMap.NonEmpty.mapWithKey' f@
--
-- __WARNING__: Differs from @Data.IntMap.foldMapWithKey@, which traverses
-- positive items first, then negative items.
--
-- This can be an asymptotically faster than
-- 'Data.IntMap.NonEmpty.foldrWithKey' or 'Data.IntMap.NonEmpty.foldlWithKey' for
-- some monoids.

-- TODO: benchmark against maxView method
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 #-}

-- | /O(n)/. IntMap a function over all values in the map.
--
-- > map (++ "x") (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "bx") :| [(5, "ax")])
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
 #-}

-- | /O(m*log(n\/m + 1)), m <= n/.
-- The expression (@'union' t1 t2@) takes the left-biased union of @t1@ and
-- @t2@. It prefers @t1@ when duplicate keys are encountered, i.e.
-- (@'union' == 'Data.IntMap.NonEmpty.unionWith' 'const'@).
--
-- > union (fromList ((5, "a") :| [(3, "b")])) (fromList ((5, "A") :| [(7, "C")])) == fromList ((3, "b") :| [(5, "a"), (7, "C")])
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 #-}

-- | The left-biased union of a non-empty list of maps.
--
-- > unions (fromList ((5, "a") :| [(3, "b")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "A3") :| [(3, "B3")])])
-- >     == fromList [(3, "b"), (5, "a"), (7, "C")]
-- > unions (fromList ((5, "A3") :| [(3, "B3")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "a") :| [(3, "b")])])
-- >     == fromList ((3, "B3") :| [(5, "A3"), (7, "C")])
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 #-}

-- | /O(n)/.
-- Return all elements of the map in the ascending order of their keys.
--
-- > elems (fromList ((5,"a") :| [(3,"b")])) == ("b" :| ["a"])
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 #-}

-- | /O(1)/. The number of elements in the map.  Guaranteed to be greater
-- than zero.
--
-- > size (singleton 1 'a')                          == 1
-- > size (fromList ((1,'a') :| [(2,'c'), (3,'b')])) == 3
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 #-}

-- | /O(log n)/.
-- Convert a non-empty map back into a normal possibly-empty map, for usage
-- with functions that expect 'IntMap'.
--
-- Can be thought of as "obscuring" the non-emptiness of the map in its
-- type.  See the 'Data.IntMap.NonEmpty.IsNotEmpty' pattern.
--
-- 'nonEmptyMap' and @'maybe' 'Data.IntMap.empty' 'toMap'@ form an isomorphism: they
-- are perfect structure-preserving inverses of eachother.
--
-- > toMap (fromList ((3,"a") :| [(5,"b")])) == Data.IntMap.fromList [(3,"a"), (5,"b")]
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 #-}

-- | /O(n)/.
-- @'traverseWithKey' f m == 'fromList' <$> 'traverse' (\(k, v) -> (,) k <$> f k v) ('toList' m)@
-- That is, behaves exactly like a regular 'traverse' except that the traversing
-- function also has access to the key associated with a value.
--
-- /Use 'traverseWithKey1'/ whenever possible (if your 'Applicative'
-- also has 'Apply' instance).  This version is provided only for types
-- that do not have 'Apply' instance, since 'Apply' is not at the moment
-- (and might not ever be) an official superclass of 'Applicative'.
--
-- __WARNING__: Differs from @Data.IntMap.traverseWithKey@, which traverses
-- positive items first, then negative items.
--
-- @
-- 'traverseWithKey' f = 'unwrapApplicative' . 'traverseWithKey1' (\\k -> WrapApplicative . f k)
-- @
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 #-}

-- | /O(n)/.
-- @'traverseWithKey1' f m == 'fromList' <$> 'traverse1' (\(k, v) -> (,) k <$> f k v) ('toList' m)@
--
-- That is, behaves exactly like a regular 'traverse1' except that the traversing
-- function also has access to the key associated with a value.
--
-- __WARNING__: Differs from @Data.IntMap.traverseWithKey@, which traverses
-- positive items first, then negative items.
--
-- Is more general than 'traverseWithKey', since works with all 'Apply',
-- and not just 'Applicative'.

-- TODO: benchmark against maxView-based methods
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 #-}

-- | /O(n)/. Convert the map to a non-empty list of key\/value pairs.
--
-- > toList (fromList ((5,"a") :| [(3,"b")])) == ((3,"b") :| [(5,"a")])
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 #-}

-- | /O(log n)/. Smart constructor for an 'NEIntMap' from a 'IntMap'.  Returns
-- 'Nothing' if the 'IntMap' was originally actually empty, and @'Just' n@
-- with an 'NEIntMap', if the 'IntMap' was not empty.
--
-- 'nonEmptyMap' and @'maybe' 'Data.IntMap.empty' 'toMap'@ form an
-- isomorphism: they are perfect structure-preserving inverses of
-- eachother.
--
-- See 'Data.IntMap.NonEmpty.IsNonEmpty' for a pattern synonym that lets you
-- "match on" the possiblity of a 'IntMap' being an 'NEIntMap'.
--
-- > nonEmptyMap (Data.IntMap.fromList [(3,"a"), (5,"b")]) == Just (fromList ((3,"a") :| [(5,"b")]))
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 #-}

-- | /O(log n)/. A general continuation-based way to consume a 'IntMap' as if
-- it were an 'NEIntMap'. @'withNonEmpty' def f@ will take a 'IntMap'.  If map is
-- empty, it will evaluate to @def@.  Otherwise, a non-empty map 'NEIntMap'
-- will be fed to the function @f@ instead.
--
-- @'nonEmptyMap' == 'withNonEmpty' 'Nothing' 'Just'@
withNonEmpty
    :: r                    -- ^ value to return if map is empty
    -> (NEIntMap a -> r)     -- ^ function to apply if map is not empty
    -> 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 #-}

-- | /O(n*log n)/. Build a non-empty map from a non-empty list of
-- key\/value pairs. See also 'Data.IntMap.NonEmpty.fromAscList'. If the list
-- contains more than one value for the same key, the last value for the
-- key is retained.
--
-- > fromList ((5,"a") :| [(3,"b"), (5, "c")]) == fromList ((5,"c") :| [(3,"b")])
-- > fromList ((5,"c") :| [(3,"b"), (5, "a")]) == fromList ((5,"a") :| [(3,"b")])

-- TODO: write manually and optimize to be equivalent to
-- 'fromDistinctAscList' if items are ordered, just like the actual
-- 'M.fromList'.
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 #-}

-- | /O(1)/. A map with a single element.
--
-- > singleton 1 'a'        == fromList ((1, 'a') :| [])
-- > size (singleton 1 'a') == 1
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 #-}

-- | /O(log n)/. Insert with a function, combining new value and old value.
-- @'insertWith' f key value mp@ will insert the pair (key, value) into
-- @mp@ if key does not exist in the map. If the key does exist, the
-- function will insert the pair @(key, f new_value old_value)@.
--
-- See 'Data.IntMap.NonEmpty.insertIntMapWith' for a version where the first
-- argument is a 'IntMap'.
--
-- > insertWith (++) 5 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "xxxa")])
-- > insertWith (++) 7 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "a"), (7, "xxx")])
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 #-}


-- | Left-biased union
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 (<$) #-}

-- | @since 0.3.4.4
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 #-}

-- | Traverses elements in order of ascending keys.
--
-- __WARNING:__ 'F.fold' and 'F.foldMap' are different than for the
-- 'IntMap' instance.  They traverse elements in order of ascending keys,
-- while 'IntMap' traverses positive keys first, then negative keys.
--
-- 'Data.Foldable.foldr1', 'Data.Foldable.foldl1', 'Data.Foldable.minimum',
-- 'Data.Foldable.maximum' are all total.
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 #-}
    -- TODO: use build
    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 #-}

-- | Traverses elements in order of ascending keys
--
-- __WARNING:__ Different than for the 'IntMap' instance.  They traverse
-- elements in order of ascending keys, while 'IntMap' traverses positive
-- keys first, then negative keys.
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 #-}

-- | Traverses elements in order of ascending keys
--
-- __WARNING:__ 'F1.fold1' and 'F1.foldMap1' are different than 'F.fold' and
-- 'F.foldMap' for the 'IntMap' instance of 'Foldable'.  They traverse
-- elements in order of ascending keys, while 'IntMap' traverses positive
-- keys first, then negative keys.
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 #-}

-- | Traverses elements in order of ascending keys
--
-- __WARNING:__ 'traverse1' and 'sequence1' are different 'traverse' and
-- 'sequence' for the 'IntMap' instance of 'Traversable'.  They traverse
-- elements in order of ascending keys, while 'IntMap' traverses positive
-- keys first, then negative keys.
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 #-}

-- | 'extract' gets the value at the minimal key, and 'duplicate' produces
-- a map of maps comprised of all keys from the original map greater than
-- or equal to the current key.
--
-- @since 0.1.1.0
instance Comonad NEIntMap where
    extract :: forall a. NEIntMap a -> a
extract = forall a. NEIntMap a -> a
neimV0
    {-# INLINE extract #-}
    -- We'd like to use 'M.mapAccumWithKey', but it traverses things in the
    -- wrong order.
    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 #-}

-- | /O(n)/. Test if the internal map structure is valid.
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)





-- | /O(log n)/. Insert new key and value into a map where keys are
-- /strictly greater than/ the new key.  That is, the new key must be
-- /strictly less than/ all keys present in the 'IntMap'.  /The precondition
-- is not checked./
--
-- At the moment this is simply an alias for @Data.IntSet.insert@, but it's
-- left here as a placeholder in case this eventually gets implemented in
-- a more efficient way.

-- TODO: implementation
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 #-}

-- | /O(log n)/. Insert new key and value into a map where keys are
-- /strictly less than/ the new key.  That is, the new key must be
-- /strictly greater than/ all keys present in the 'IntMap'.  /The
-- precondition is not checked./
--
-- At the moment this is simply an alias for @Data.IntSet.insert@, but it's
-- left here as a placeholder in case this eventually gets implemented in
-- a more efficient way.

-- TODO: implementation
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 #-}

-- | /O(n)/. A fixed version of 'Data.IntMap.traverseWithKey' that
-- traverses items in ascending order of keys.
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 #-}

-- ---------------------------------------------
-- | CPP for new functions not in old containers
-- ---------------------------------------------

-- | Compatibility layer for 'Data.IntMap.Lazy.lookupMinMap'.
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 #-}

-- | Compatibility layer for 'Data.IntMap.Lazy.lookupMaxMap'.
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 #-}