{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.PQueue.Prio.Max
-- Copyright   :  (c) Louis Wasserman 2010
-- License     :  BSD-style
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
-----------------------------------------------------------------------------
module Data.PQueue.Prio.Max.Internals (
  MaxPQueue (..),
  -- * Construction
  empty,
  singleton,
  insert,
  insertBehind,
  union,
  unions,
  -- * Query
  null,
  size,
  -- ** Maximum view
  findMax,
  getMax,
  deleteMax,
  deleteFindMax,
  adjustMax,
  adjustMaxA,
  adjustMaxWithKey,
  adjustMaxWithKeyA,
  updateMax,
  updateMaxA,
  updateMaxWithKey,
  updateMaxWithKeyA,
  maxView,
  maxViewWithKey,
  -- * Traversal
  -- ** Map
  map,
  mapWithKey,
  mapKeys,
  mapKeysMonotonic,
  -- ** Fold
  foldrWithKey,
  foldlWithKey,
  -- ** Traverse
  traverseWithKey,
  mapMWithKey,
  -- * Subsets
  -- ** Indexed
  take,
  drop,
  splitAt,
  -- ** Predicates
  takeWhile,
  takeWhileWithKey,
  dropWhile,
  dropWhileWithKey,
  span,
  spanWithKey,
  break,
  breakWithKey,
  -- *** Filter
  filter,
  filterWithKey,
  partition,
  partitionWithKey,
  mapMaybe,
  mapMaybeWithKey,
  mapEither,
  mapEitherWithKey,
  -- * List operations
  -- ** Conversion from lists
  fromList,
  fromAscList,
  fromDescList,
  -- ** Conversion to lists
  keys,
  elems,
  assocs,
  toAscList,
  toDescList,
  toList,
  -- * Unordered operations
  foldrU,
  foldMapWithKeyU,
  foldrWithKeyU,
  foldlU,
  foldlU',
  foldlWithKeyU,
  foldlWithKeyU',
  traverseU,
  traverseWithKeyU,
  keysU,
  elemsU,
  assocsU,
  toListU,
  -- * Helper methods
  seqSpine
  )
  where

import Data.Maybe (fromMaybe)
import Data.PQueue.Internals.Down
import Data.PQueue.Prio.Internals (MinPQueue)
import qualified Data.PQueue.Prio.Internals as PrioInternals
import Control.DeepSeq (NFData(rnf))

#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup(..), stimesMonoid)
#endif

import Prelude hiding (map, filter, break, span, takeWhile, dropWhile, splitAt, take, drop, (!!), null)
import qualified Data.Foldable as F

import qualified Data.PQueue.Prio.Min as Q

#ifdef __GLASGOW_HASKELL__
import Data.Data (Data)
import Text.Read (Lexeme(Ident), lexP, parens, prec,
  readPrec, readListPrec, readListPrecDefault)
#endif


#ifndef __GLASGOW_HASKELL__
build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build f = f (:) []
#endif

-- | A priority queue where values of type @a@ are annotated with keys of type @k@.
-- The queue supports extracting the element with maximum key.
newtype MaxPQueue k a = MaxPQ (MinPQueue (Down k) a)
# if __GLASGOW_HASKELL__
  deriving (MaxPQueue k a -> MaxPQueue k a -> Bool
(MaxPQueue k a -> MaxPQueue k a -> Bool)
-> (MaxPQueue k a -> MaxPQueue k a -> Bool) -> Eq (MaxPQueue k a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k a. (Ord k, Eq a) => MaxPQueue k a -> MaxPQueue k a -> Bool
/= :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c/= :: forall k a. (Ord k, Eq a) => MaxPQueue k a -> MaxPQueue k a -> Bool
== :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c== :: forall k a. (Ord k, Eq a) => MaxPQueue k a -> MaxPQueue k a -> Bool
Eq, Eq (MaxPQueue k a)
Eq (MaxPQueue k a)
-> (MaxPQueue k a -> MaxPQueue k a -> Ordering)
-> (MaxPQueue k a -> MaxPQueue k a -> Bool)
-> (MaxPQueue k a -> MaxPQueue k a -> Bool)
-> (MaxPQueue k a -> MaxPQueue k a -> Bool)
-> (MaxPQueue k a -> MaxPQueue k a -> Bool)
-> (MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a)
-> (MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a)
-> Ord (MaxPQueue k a)
MaxPQueue k a -> MaxPQueue k a -> Bool
MaxPQueue k a -> MaxPQueue k a -> Ordering
MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall k a. (Ord k, Ord a) => Eq (MaxPQueue k a)
forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Bool
forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Ordering
forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
min :: MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
$cmin :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
max :: MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
$cmax :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
>= :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c>= :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Bool
> :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c> :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Bool
<= :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c<= :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Bool
< :: MaxPQueue k a -> MaxPQueue k a -> Bool
$c< :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Bool
compare :: MaxPQueue k a -> MaxPQueue k a -> Ordering
$ccompare :: forall k a.
(Ord k, Ord a) =>
MaxPQueue k a -> MaxPQueue k a -> Ordering
$cp1Ord :: forall k a. (Ord k, Ord a) => Eq (MaxPQueue k a)
Ord, Typeable (MaxPQueue k a)
DataType
Constr
Typeable (MaxPQueue k a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a))
-> (MaxPQueue k a -> Constr)
-> (MaxPQueue k a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (MaxPQueue k a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (MaxPQueue k a)))
-> ((forall b. Data b => b -> b) -> MaxPQueue k a -> MaxPQueue k a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r)
-> (forall u. (forall d. Data d => d -> u) -> MaxPQueue k a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> MaxPQueue k a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> MaxPQueue k a -> m (MaxPQueue k a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> MaxPQueue k a -> m (MaxPQueue k a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> MaxPQueue k a -> m (MaxPQueue k a))
-> Data (MaxPQueue k a)
MaxPQueue k a -> DataType
MaxPQueue k a -> Constr
(forall b. Data b => b -> b) -> MaxPQueue k a -> MaxPQueue k a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MaxPQueue k a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> MaxPQueue k a -> u
forall u. (forall d. Data d => d -> u) -> MaxPQueue k a -> [u]
forall k a. (Data k, Data a, Ord k) => Typeable (MaxPQueue k a)
forall k a. (Data k, Data a, Ord k) => MaxPQueue k a -> DataType
forall k a. (Data k, Data a, Ord k) => MaxPQueue k a -> Constr
forall k a.
(Data k, Data a, Ord k) =>
(forall b. Data b => b -> b) -> MaxPQueue k a -> MaxPQueue k a
forall k a u.
(Data k, Data a, Ord k) =>
Int -> (forall d. Data d => d -> u) -> MaxPQueue k a -> u
forall k a u.
(Data k, Data a, Ord k) =>
(forall d. Data d => d -> u) -> MaxPQueue k a -> [u]
forall k a r r'.
(Data k, Data a, Ord k) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
forall k a r r'.
(Data k, Data a, Ord k) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
forall k a (m :: * -> *).
(Data k, Data a, Ord k, Monad m) =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
forall k a (m :: * -> *).
(Data k, Data a, Ord k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
forall k a (c :: * -> *).
(Data k, Data a, Ord k) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a)
forall k a (c :: * -> *).
(Data k, Data a, Ord k) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a)
forall k a (t :: * -> *) (c :: * -> *).
(Data k, Data a, Ord k, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (MaxPQueue k a))
forall k a (t :: * -> * -> *) (c :: * -> *).
(Data k, Data a, Ord k, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MaxPQueue k a))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (MaxPQueue k a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MaxPQueue k a))
$cMaxPQ :: Constr
$tMaxPQueue :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
$cgmapMo :: forall k a (m :: * -> *).
(Data k, Data a, Ord k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
gmapMp :: (forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
$cgmapMp :: forall k a (m :: * -> *).
(Data k, Data a, Ord k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
gmapM :: (forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
$cgmapM :: forall k a (m :: * -> *).
(Data k, Data a, Ord k, Monad m) =>
(forall d. Data d => d -> m d)
-> MaxPQueue k a -> m (MaxPQueue k a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> MaxPQueue k a -> u
$cgmapQi :: forall k a u.
(Data k, Data a, Ord k) =>
Int -> (forall d. Data d => d -> u) -> MaxPQueue k a -> u
gmapQ :: (forall d. Data d => d -> u) -> MaxPQueue k a -> [u]
$cgmapQ :: forall k a u.
(Data k, Data a, Ord k) =>
(forall d. Data d => d -> u) -> MaxPQueue k a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
$cgmapQr :: forall k a r r'.
(Data k, Data a, Ord k) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
$cgmapQl :: forall k a r r'.
(Data k, Data a, Ord k) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MaxPQueue k a -> r
gmapT :: (forall b. Data b => b -> b) -> MaxPQueue k a -> MaxPQueue k a
$cgmapT :: forall k a.
(Data k, Data a, Ord k) =>
(forall b. Data b => b -> b) -> MaxPQueue k a -> MaxPQueue k a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MaxPQueue k a))
$cdataCast2 :: forall k a (t :: * -> * -> *) (c :: * -> *).
(Data k, Data a, Ord k, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MaxPQueue k a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (MaxPQueue k a))
$cdataCast1 :: forall k a (t :: * -> *) (c :: * -> *).
(Data k, Data a, Ord k, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (MaxPQueue k a))
dataTypeOf :: MaxPQueue k a -> DataType
$cdataTypeOf :: forall k a. (Data k, Data a, Ord k) => MaxPQueue k a -> DataType
toConstr :: MaxPQueue k a -> Constr
$ctoConstr :: forall k a. (Data k, Data a, Ord k) => MaxPQueue k a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a)
$cgunfold :: forall k a (c :: * -> *).
(Data k, Data a, Ord k) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MaxPQueue k a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a)
$cgfoldl :: forall k a (c :: * -> *).
(Data k, Data a, Ord k) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MaxPQueue k a -> c (MaxPQueue k a)
$cp1Data :: forall k a. (Data k, Data a, Ord k) => Typeable (MaxPQueue k a)
Data)
# else
  deriving (Eq, Ord)
# endif

instance (NFData k, NFData a) => NFData (MaxPQueue k a) where
  rnf :: MaxPQueue k a -> ()
rnf (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> ()
forall a. NFData a => a -> ()
rnf MinPQueue (Down k) a
q

first' :: (a -> b) -> (a, c) -> (b, c)
first' :: (a -> b) -> (a, c) -> (b, c)
first' a -> b
f (a
a, c
c) = (a -> b
f a
a, c
c)

#if MIN_VERSION_base(4,9,0)
instance Ord k => Semigroup (MaxPQueue k a) where
  <> :: MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
(<>) = MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
forall k a.
Ord k =>
MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
union
  stimes :: b -> MaxPQueue k a -> MaxPQueue k a
stimes = b -> MaxPQueue k a -> MaxPQueue k a
forall b a. (Integral b, Monoid a) => b -> a -> a
stimesMonoid
#endif

instance Ord k => Monoid (MaxPQueue k a) where
  mempty :: MaxPQueue k a
mempty = MaxPQueue k a
forall k a. MaxPQueue k a
empty
#if !MIN_VERSION_base(4,11,0)
  mappend = union
#endif
  mconcat :: [MaxPQueue k a] -> MaxPQueue k a
mconcat = [MaxPQueue k a] -> MaxPQueue k a
forall k a. Ord k => [MaxPQueue k a] -> MaxPQueue k a
unions

instance (Ord k, Show k, Show a) => Show (MaxPQueue k a) where
  showsPrec :: Int -> MaxPQueue k a -> ShowS
showsPrec Int
p MaxPQueue k a
xs = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
    String -> ShowS
showString String
"fromDescList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, a)] -> ShowS
forall a. Show a => a -> ShowS
shows (MaxPQueue k a -> [(k, a)]
forall k a. Ord k => MaxPQueue k a -> [(k, a)]
toDescList MaxPQueue k a
xs)

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

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

instance Functor (MaxPQueue k) where
  fmap :: (a -> b) -> MaxPQueue k a -> MaxPQueue k b
fmap a -> b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((a -> b) -> MinPQueue (Down k) a -> MinPQueue (Down k) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f MinPQueue (Down k) a
q)

instance Ord k => Foldable (MaxPQueue k) where
  foldr :: (a -> b -> b) -> b -> MaxPQueue k a -> b
foldr a -> b -> b
f b
z (MaxPQ MinPQueue (Down k) a
q) = (a -> b -> b) -> b -> MinPQueue (Down k) a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
z MinPQueue (Down k) a
q
  foldl :: (b -> a -> b) -> b -> MaxPQueue k a -> b
foldl b -> a -> b
f b
z (MaxPQ MinPQueue (Down k) a
q) = (b -> a -> b) -> b -> MinPQueue (Down k) a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl b -> a -> b
f b
z MinPQueue (Down k) a
q

  length :: MaxPQueue k a -> Int
length = MaxPQueue k a -> Int
forall k a. MaxPQueue k a -> Int
size
  null :: MaxPQueue k a -> Bool
null = MaxPQueue k a -> Bool
forall k a. MaxPQueue k a -> Bool
null

-- | Traverses in descending order. 'mapM' is strictly accumulating like
-- 'mapMWithKey'.
instance Ord k => Traversable (MaxPQueue k) where
  traverse :: (a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverse a -> f b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) b -> MaxPQueue k b)
-> f (MinPQueue (Down k) b) -> f (MaxPQueue k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> MinPQueue (Down k) a -> f (MinPQueue (Down k) b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f MinPQueue (Down k) a
q
  mapM :: (a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b)
mapM = (k -> a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b)
forall k (m :: * -> *) a b.
(Ord k, Monad m) =>
(k -> a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b)
mapMWithKey ((k -> a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b))
-> ((a -> m b) -> k -> a -> m b)
-> (a -> m b)
-> MaxPQueue k a
-> m (MaxPQueue k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m b) -> k -> a -> m b
forall a b. a -> b -> a
const
  sequence :: MaxPQueue k (m a) -> m (MaxPQueue k a)
sequence = (m a -> m a) -> MaxPQueue k (m a) -> m (MaxPQueue k a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM m a -> m a
forall a. a -> a
id

-- | \(O(1)\). Returns the empty priority queue.
empty :: MaxPQueue k a
empty :: MaxPQueue k a
empty = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
forall k a. MinPQueue k a
Q.empty

-- | \(O(1)\). Constructs a singleton priority queue.
singleton :: k -> a -> MaxPQueue k a
singleton :: k -> a -> MaxPQueue k a
singleton k
k a
a = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (Down k -> a -> MinPQueue (Down k) a
forall k a. k -> a -> MinPQueue k a
Q.singleton (k -> Down k
forall a. a -> Down a
Down k
k) a
a)

-- | Amortized \(O(1)\), worst-case \(O(\log n)\). Inserts
-- an element with the specified key into the queue.
insert :: Ord k => k -> a -> MaxPQueue k a -> MaxPQueue k a
insert :: k -> a -> MaxPQueue k a -> MaxPQueue k a
insert k
k a
a (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (Down k -> a -> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. Ord k => k -> a -> MinPQueue k a -> MinPQueue k a
Q.insert (k -> Down k
forall a. a -> Down a
Down k
k) a
a MinPQueue (Down k) a
q)

-- | \(O(n)\) (an earlier implementation had \(O(1)\) but was buggy).
-- Insert an element with the specified key into the priority queue,
-- putting it behind elements whose key compares equal to the
-- inserted one.
insertBehind :: Ord k => k -> a -> MaxPQueue k a -> MaxPQueue k a
insertBehind :: k -> a -> MaxPQueue k a -> MaxPQueue k a
insertBehind k
k a
a (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (Down k -> a -> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. Ord k => k -> a -> MinPQueue k a -> MinPQueue k a
Q.insertBehind (k -> Down k
forall a. a -> Down a
Down k
k) a
a MinPQueue (Down k) a
q)

-- | Amortized \(O(\log \min(n_1,n_2))\), worst-case \(O(\log \max(n_1,n_2))\). Returns the union
-- of the two specified queues.
union :: Ord k => MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q1 union :: MaxPQueue k a -> MaxPQueue k a -> MaxPQueue k a
`union` MaxPQ MinPQueue (Down k) a
q2 = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) a
q1 MinPQueue (Down k) a
-> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a.
Ord k =>
MinPQueue k a -> MinPQueue k a -> MinPQueue k a
`Q.union` MinPQueue (Down k) a
q2)

-- | The union of a list of queues: (@'unions' == 'List.foldl' 'union' 'empty'@).
unions :: Ord k => [MaxPQueue k a] -> MaxPQueue k a
unions :: [MaxPQueue k a] -> MaxPQueue k a
unions [MaxPQueue k a]
qs = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ([MinPQueue (Down k) a] -> MinPQueue (Down k) a
forall k a. Ord k => [MinPQueue k a] -> MinPQueue k a
Q.unions [MinPQueue (Down k) a
q | MaxPQ MinPQueue (Down k) a
q <- [MaxPQueue k a]
qs])

-- | \(O(1)\). Checks if this priority queue is empty.
null :: MaxPQueue k a -> Bool
null :: MaxPQueue k a -> Bool
null (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> Bool
forall k a. MinPQueue k a -> Bool
Q.null MinPQueue (Down k) a
q

-- | \(O(1)\). Returns the size of this priority queue.
size :: MaxPQueue k a -> Int
size :: MaxPQueue k a -> Int
size (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> Int
forall k a. MinPQueue k a -> Int
Q.size MinPQueue (Down k) a
q

-- | \(O(1)\). The maximal (key, element) in the queue. Calls 'error' if empty.
findMax :: MaxPQueue k a -> (k, a)
findMax :: MaxPQueue k a -> (k, a)
findMax = (k, a) -> Maybe (k, a) -> (k, a)
forall a. a -> Maybe a -> a
fromMaybe (String -> (k, a)
forall a. HasCallStack => String -> a
error String
"Error: findMax called on an empty queue") (Maybe (k, a) -> (k, a))
-> (MaxPQueue k a -> Maybe (k, a)) -> MaxPQueue k a -> (k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> Maybe (k, a)
forall k a. MaxPQueue k a -> Maybe (k, a)
getMax

-- | \(O(1)\). The maximal (key, element) in the queue, if the queue is nonempty.
getMax :: MaxPQueue k a -> Maybe (k, a)
getMax :: MaxPQueue k a -> Maybe (k, a)
getMax (MaxPQ MinPQueue (Down k) a
q) = do
  (Down k
k, a
a) <- MinPQueue (Down k) a -> Maybe (Down k, a)
forall k a. MinPQueue k a -> Maybe (k, a)
Q.getMin MinPQueue (Down k) a
q
  (k, a) -> Maybe (k, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (k
k, a
a)

-- | \(O(\log n)\). Delete and find the element with the maximum key. Calls 'error' if empty.
deleteMax :: Ord k => MaxPQueue k a -> MaxPQueue k a
deleteMax :: MaxPQueue k a -> MaxPQueue k a
deleteMax (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. Ord k => MinPQueue k a -> MinPQueue k a
Q.deleteMin MinPQueue (Down k) a
q)

-- | \(O(\log n)\). Delete and find the element with the maximum key. Calls 'error' if empty.
deleteFindMax :: Ord k => MaxPQueue k a -> ((k, a), MaxPQueue k a)
deleteFindMax :: MaxPQueue k a -> ((k, a), MaxPQueue k a)
deleteFindMax = ((k, a), MaxPQueue k a)
-> Maybe ((k, a), MaxPQueue k a) -> ((k, a), MaxPQueue k a)
forall a. a -> Maybe a -> a
fromMaybe (String -> ((k, a), MaxPQueue k a)
forall a. HasCallStack => String -> a
error String
"Error: deleteFindMax called on an empty queue") (Maybe ((k, a), MaxPQueue k a) -> ((k, a), MaxPQueue k a))
-> (MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a))
-> MaxPQueue k a
-> ((k, a), MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
forall k a. Ord k => MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
maxViewWithKey

-- | \(O(1)\). Alter the value at the maximum key. If the queue is empty, does nothing.
adjustMax :: (a -> a) -> MaxPQueue k a -> MaxPQueue k a
adjustMax :: (a -> a) -> MaxPQueue k a -> MaxPQueue k a
adjustMax = (k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a
forall k a. (k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a
adjustMaxWithKey ((k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a)
-> ((a -> a) -> k -> a -> a)
-> (a -> a)
-> MaxPQueue k a
-> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> k -> a -> a
forall a b. a -> b -> a
const

-- | \(O(1)\) per operation. Alter the value at the maximum key in an
-- 'Applicative' context. If the queue is empty, does nothing.
--
-- @since 1.4.2
adjustMaxA :: Applicative f => (a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
adjustMaxA :: (a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
adjustMaxA = (k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
forall (f :: * -> *) k a.
Applicative f =>
(k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
adjustMaxWithKeyA ((k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a))
-> ((a -> f a) -> k -> a -> f a)
-> (a -> f a)
-> MaxPQueue k a
-> f (MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f a) -> k -> a -> f a
forall a b. a -> b -> a
const

-- | \(O(1)\). Alter the value at the maximum key. If the queue is empty, does nothing.
adjustMaxWithKey :: (k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a
adjustMaxWithKey :: (k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a
adjustMaxWithKey k -> a -> a
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> a) -> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. (k -> a -> a) -> MinPQueue k a -> MinPQueue k a
Q.adjustMinWithKey (k -> a -> a
f (k -> a -> a) -> (Down k -> k) -> Down k -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | \(O(1)\) per operation. Alter the value at the maximum key in an
-- 'Applicative' context. If the queue is empty, does nothing.
--
-- @since 1.4.2
adjustMaxWithKeyA :: Applicative f => (k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
adjustMaxWithKeyA :: (k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a)
adjustMaxWithKeyA k -> a -> f a
f (MaxPQ MinPQueue (Down k) a
q) = (MinPQueue (Down k) a -> MaxPQueue k a)
-> (Down k -> a -> f a)
-> MinPQueue (Down k) a
-> f (MaxPQueue k a)
forall (f :: * -> *) k a r.
Applicative f =>
(MinPQueue k a -> r) -> (k -> a -> f a) -> MinPQueue k a -> f r
PrioInternals.adjustMinWithKeyA' MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (k -> a -> f a
f (k -> a -> f a) -> (Down k -> k) -> Down k -> a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q

-- | \(O(\log n)\). (Actually \(O(1)\) if there's no deletion.) Update the value at the maximum key.
-- If the queue is empty, does nothing.
updateMax :: Ord k => (a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
updateMax :: (a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
updateMax = (k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
forall k a.
Ord k =>
(k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
updateMaxWithKey ((k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a)
-> ((a -> Maybe a) -> k -> a -> Maybe a)
-> (a -> Maybe a)
-> MaxPQueue k a
-> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> k -> a -> Maybe a
forall a b. a -> b -> a
const

-- | \(O(\log n)\) per operation. (Actually \(O(1)\) if there's no deletion.) Update
-- the value at the maximum key in an 'Applicative' context. If the queue is
-- empty, does nothing.
--
-- @since 1.4.2
updateMaxA :: (Applicative f, Ord k) => (a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
updateMaxA :: (a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
updateMaxA = (k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
forall (f :: * -> *) k a.
(Applicative f, Ord k) =>
(k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
updateMaxWithKeyA ((k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a))
-> ((a -> f (Maybe a)) -> k -> a -> f (Maybe a))
-> (a -> f (Maybe a))
-> MaxPQueue k a
-> f (MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f (Maybe a)) -> k -> a -> f (Maybe a)
forall a b. a -> b -> a
const

-- | \(O(\log n)\). (Actually \(O(1)\) if there's no deletion.) Update the value at the maximum key.
-- If the queue is empty, does nothing.
updateMaxWithKey :: Ord k => (k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
updateMaxWithKey :: (k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a
updateMaxWithKey k -> a -> Maybe a
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> Maybe a)
-> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a.
Ord k =>
(k -> a -> Maybe a) -> MinPQueue k a -> MinPQueue k a
Q.updateMinWithKey (k -> a -> Maybe a
f (k -> a -> Maybe a) -> (Down k -> k) -> Down k -> a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | \(O(\log n)\) per operation. (Actually \(O(1)\) if there's no deletion.) Update
-- the value at the maximum key in an 'Applicative' context. If the queue is
-- empty, does nothing.
--
-- @since 1.4.2
updateMaxWithKeyA :: (Applicative f, Ord k) => (k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
updateMaxWithKeyA :: (k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a)
updateMaxWithKeyA k -> a -> f (Maybe a)
f (MaxPQ MinPQueue (Down k) a
q) = (MinPQueue (Down k) a -> MaxPQueue k a)
-> (Down k -> a -> f (Maybe a))
-> MinPQueue (Down k) a
-> f (MaxPQueue k a)
forall (f :: * -> *) k a r.
(Applicative f, Ord k) =>
(MinPQueue k a -> r)
-> (k -> a -> f (Maybe a)) -> MinPQueue k a -> f r
PrioInternals.updateMinWithKeyA' MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (k -> a -> f (Maybe a)
f (k -> a -> f (Maybe a))
-> (Down k -> k) -> Down k -> a -> f (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q

-- | \(O(\log n)\). Retrieves the value associated with the maximum key of the queue, and the queue
-- stripped of that element, or 'Nothing' if passed an empty queue.
maxView :: Ord k => MaxPQueue k a -> Maybe (a, MaxPQueue k a)
maxView :: MaxPQueue k a -> Maybe (a, MaxPQueue k a)
maxView MaxPQueue k a
q = do
  ((k
_, a
a), MaxPQueue k a
q') <- MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
forall k a. Ord k => MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
maxViewWithKey MaxPQueue k a
q
  (a, MaxPQueue k a) -> Maybe (a, MaxPQueue k a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, MaxPQueue k a
q')

-- | \(O(\log n)\). Retrieves the maximal (key, value) pair of the map, and the map stripped of that
-- element, or 'Nothing' if passed an empty map.
maxViewWithKey :: Ord k => MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
maxViewWithKey :: MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
maxViewWithKey (MaxPQ MinPQueue (Down k) a
q) = do
  ((Down k
k, a
a), MinPQueue (Down k) a
q') <- MinPQueue (Down k) a -> Maybe ((Down k, a), MinPQueue (Down k) a)
forall k a. Ord k => MinPQueue k a -> Maybe ((k, a), MinPQueue k a)
Q.minViewWithKey MinPQueue (Down k) a
q
  ((k, a), MaxPQueue k a) -> Maybe ((k, a), MaxPQueue k a)
forall (m :: * -> *) a. Monad m => a -> m a
return ((k
k, a
a), MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q')

-- | \(O(n)\). Map a function over all values in the queue.
map :: (a -> b) -> MaxPQueue k a -> MaxPQueue k b
map :: (a -> b) -> MaxPQueue k a -> MaxPQueue k b
map = (k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b
forall k a b. (k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b
mapWithKey ((k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b)
-> ((a -> b) -> k -> a -> b)
-> (a -> b)
-> MaxPQueue k a
-> MaxPQueue k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> k -> a -> b
forall a b. a -> b -> a
const

-- | \(O(n)\). Map a function over all values in the queue.
mapWithKey :: (k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b
mapWithKey :: (k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b
mapWithKey k -> a -> b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> b) -> MinPQueue (Down k) a -> MinPQueue (Down k) b
forall k a b. (k -> a -> b) -> MinPQueue k a -> MinPQueue k b
Q.mapWithKey (k -> a -> b
f (k -> a -> b) -> (Down k -> k) -> Down k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | \(O(n)\). Map a function over all values in the queue.
mapKeys :: Ord k' => (k -> k') -> MaxPQueue k a -> MaxPQueue k' a
mapKeys :: (k -> k') -> MaxPQueue k a -> MaxPQueue k' a
mapKeys k -> k'
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k') a -> MaxPQueue k' a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> Down k')
-> MinPQueue (Down k) a -> MinPQueue (Down k') a
forall k' k a.
Ord k' =>
(k -> k') -> MinPQueue k a -> MinPQueue k' a
Q.mapKeys ((k -> k') -> Down k -> Down k'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap k -> k'
f) MinPQueue (Down k) a
q)

-- | \(O(n)\). @'mapKeysMonotonic' f q == 'mapKeys' f q@, but only works when @f@ is strictly
-- monotonic. /The precondition is not checked./ This function has better performance than
-- 'mapKeys'.
mapKeysMonotonic :: (k -> k') -> MaxPQueue k a -> MaxPQueue k' a
mapKeysMonotonic :: (k -> k') -> MaxPQueue k a -> MaxPQueue k' a
mapKeysMonotonic k -> k'
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k') a -> MaxPQueue k' a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> Down k')
-> MinPQueue (Down k) a -> MinPQueue (Down k') a
forall k k' a. (k -> k') -> MinPQueue k a -> MinPQueue k' a
Q.mapKeysMonotonic ((k -> k') -> Down k -> Down k'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap k -> k'
f) MinPQueue (Down k) a
q)

-- | \(O(n \log n)\). Fold the keys and values in the map, such that
-- @'foldrWithKey' f z q == 'List.foldr' ('uncurry' f) z ('toDescList' q)@.
--
-- If you do not care about the traversal order, consider using 'foldrWithKeyU'.
foldrWithKey :: Ord k => (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
foldrWithKey :: (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
foldrWithKey k -> a -> b -> b
f b
z (MaxPQ MinPQueue (Down k) a
q) = (Down k -> a -> b -> b) -> b -> MinPQueue (Down k) a -> b
forall k a b.
Ord k =>
(k -> a -> b -> b) -> b -> MinPQueue k a -> b
Q.foldrWithKey (k -> a -> b -> b
f (k -> a -> b -> b) -> (Down k -> k) -> Down k -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) b
z MinPQueue (Down k) a
q

-- | \(O(n \log n)\). Fold the keys and values in the map, such that
-- @'foldlWithKey' f z q == 'List.foldl' ('uncurry' . f) z ('toDescList' q)@.
--
-- If you do not care about the traversal order, consider using 'foldlWithKeyU'.
foldlWithKey :: Ord k => (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKey :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKey b -> k -> a -> b
f b
z0 (MaxPQ MinPQueue (Down k) a
q) = (b -> Down k -> a -> b) -> b -> MinPQueue (Down k) a -> b
forall k b a.
Ord k =>
(b -> k -> a -> b) -> b -> MinPQueue k a -> b
Q.foldlWithKey (\b
z -> b -> k -> a -> b
f b
z (k -> a -> b) -> (Down k -> k) -> Down k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) b
z0 MinPQueue (Down k) a
q

-- | \(O(n \log n)\). Traverses the elements of the queue in descending order by key.
-- (@'traverseWithKey' f q == 'fromDescList' <$> 'traverse' ('uncurry' f) ('toDescList' q)@)
--
-- If you do not care about the /order/ of the traversal, consider using 'traverseWithKeyU'.
--
-- If you are working in a strict monad, consider using 'mapMWithKey'.
traverseWithKey :: (Ord k, Applicative f) => (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseWithKey :: (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseWithKey k -> a -> f b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) b -> MaxPQueue k b)
-> f (MinPQueue (Down k) b) -> f (MaxPQueue k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Down k -> a -> f b)
-> MinPQueue (Down k) a -> f (MinPQueue (Down k) b)
forall k (f :: * -> *) a b.
(Ord k, Applicative f) =>
(k -> a -> f b) -> MinPQueue k a -> f (MinPQueue k b)
Q.traverseWithKey (k -> a -> f b
f (k -> a -> f b) -> (Down k -> k) -> Down k -> a -> f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q

-- | A strictly accumulating version of 'traverseWithKey'. This works well in
-- 'IO' and strict @State@, and is likely what you want for other "strict" monads,
-- where @⊥ >>= pure () = ⊥@.
mapMWithKey :: (Ord k, Monad m) => (k -> a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b)
mapMWithKey :: (k -> a -> m b) -> MaxPQueue k a -> m (MaxPQueue k b)
mapMWithKey k -> a -> m b
f = MaxPQueue k b -> MaxPQueue k a -> m (MaxPQueue k b)
go MaxPQueue k b
forall k a. MaxPQueue k a
empty
  where
    go :: MaxPQueue k b -> MaxPQueue k a -> m (MaxPQueue k b)
go !MaxPQueue k b
acc MaxPQueue k a
q =
      case MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
forall k a. Ord k => MaxPQueue k a -> Maybe ((k, a), MaxPQueue k a)
maxViewWithKey MaxPQueue k a
q of
        Maybe ((k, a), MaxPQueue k a)
Nothing           -> MaxPQueue k b -> m (MaxPQueue k b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MaxPQueue k b
acc
        Just ((k
k, a
a), MaxPQueue k a
q') -> do
          b
b <- k -> a -> m b
f k
k a
a
          let !acc' :: MaxPQueue k b
acc' = k -> b -> MaxPQueue k b -> MaxPQueue k b
forall k a. k -> a -> MaxPQueue k a -> MaxPQueue k a
insertMin' k
k b
b MaxPQueue k b
acc
          MaxPQueue k b -> MaxPQueue k a -> m (MaxPQueue k b)
go MaxPQueue k b
acc' MaxPQueue k a
q'

insertMin' :: k -> a -> MaxPQueue k a -> MaxPQueue k a
insertMin' :: k -> a -> MaxPQueue k a -> MaxPQueue k a
insertMin' k
k a
a (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (Down k -> a -> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. k -> a -> MinPQueue k a -> MinPQueue k a
PrioInternals.insertMax' (k -> Down k
forall a. a -> Down a
Down k
k) a
a MinPQueue (Down k) a
q)

-- | \(O(k \log n)\)/. Takes the first @k@ (key, value) pairs in the queue, or the first @n@ if @k >= n@.
-- (@'take' k q == 'List.take' k ('toDescList' q)@)
take :: Ord k => Int -> MaxPQueue k a -> [(k, a)]
take :: Int -> MaxPQueue k a -> [(k, a)]
take Int
k (MaxPQ MinPQueue (Down k) a
q) = ((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) (Int -> MinPQueue (Down k) a -> [(Down k, a)]
forall k a. Ord k => Int -> MinPQueue k a -> [(k, a)]
Q.take Int
k MinPQueue (Down k) a
q)

-- | \(O(k \log n)\)/. Deletes the first @k@ (key, value) pairs in the queue, or returns an empty queue if @k >= n@.
drop :: Ord k => Int -> MaxPQueue k a -> MaxPQueue k a
drop :: Int -> MaxPQueue k a -> MaxPQueue k a
drop Int
k (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (Int -> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a. Ord k => Int -> MinPQueue k a -> MinPQueue k a
Q.drop Int
k MinPQueue (Down k) a
q)

-- | \(O(k \log n)\)/. Equivalent to @('take' k q, 'drop' k q)@.
splitAt :: Ord k => Int -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
splitAt :: Int -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
splitAt Int
k (MaxPQ MinPQueue (Down k) a
q) = case Int
-> MinPQueue (Down k) a -> ([(Down k, a)], MinPQueue (Down k) a)
forall k a.
Ord k =>
Int -> MinPQueue k a -> ([(k, a)], MinPQueue k a)
Q.splitAt Int
k MinPQueue (Down k) a
q of
  ([(Down k, a)]
xs, MinPQueue (Down k) a
q') -> (((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) [(Down k, a)]
xs, MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q')

-- | Takes the longest possible prefix of elements satisfying the predicate.
-- (@'takeWhile' p q == 'List.takeWhile' (p . 'snd') ('toDescList' q)@)
takeWhile :: Ord k => (a -> Bool) -> MaxPQueue k a -> [(k, a)]
takeWhile :: (a -> Bool) -> MaxPQueue k a -> [(k, a)]
takeWhile = (k -> a -> Bool) -> MaxPQueue k a -> [(k, a)]
forall k a. Ord k => (k -> a -> Bool) -> MaxPQueue k a -> [(k, a)]
takeWhileWithKey ((k -> a -> Bool) -> MaxPQueue k a -> [(k, a)])
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> [(k, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | Takes the longest possible prefix of elements satisfying the predicate.
-- (@'takeWhile' p q == 'List.takeWhile' (uncurry p) ('toDescList' q)@)
takeWhileWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> [(k, a)]
takeWhileWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> [(k, a)]
takeWhileWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = ((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) ((Down k -> a -> Bool) -> MinPQueue (Down k) a -> [(Down k, a)]
forall k a. Ord k => (k -> a -> Bool) -> MinPQueue k a -> [(k, a)]
Q.takeWhileWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | Removes the longest possible prefix of elements satisfying the predicate.
dropWhile :: Ord k => (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
dropWhile :: (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
dropWhile = (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
forall k a.
Ord k =>
(k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
dropWhileWithKey ((k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a)
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | Removes the longest possible prefix of elements satisfying the predicate.
dropWhileWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
dropWhileWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
dropWhileWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> Bool)
-> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a.
Ord k =>
(k -> a -> Bool) -> MinPQueue k a -> MinPQueue k a
Q.dropWhileWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | Equivalent to @('takeWhile' p q, 'dropWhile' p q)@.
span :: Ord k => (a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
span :: (a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
span = (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
spanWithKey ((k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a))
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> ([(k, a)], MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | Equivalent to @'span' ('not' . p)@.
break :: Ord k => (a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
break :: (a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
break = (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
breakWithKey ((k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a))
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> ([(k, a)], MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | Equivalent to @'spanWithKey' (\k a -> 'not' (p k a)) q@.
spanWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
spanWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
spanWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = case (Down k -> a -> Bool)
-> MinPQueue (Down k) a -> ([(Down k, a)], MinPQueue (Down k) a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MinPQueue k a -> ([(k, a)], MinPQueue k a)
Q.spanWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q of
  ([(Down k, a)]
xs, MinPQueue (Down k) a
q') -> (((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) [(Down k, a)]
xs, MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q')

-- | Equivalent to @'spanWithKey' (\k a -> 'not' (p k a)) q@.
breakWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
breakWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a)
breakWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = case (Down k -> a -> Bool)
-> MinPQueue (Down k) a -> ([(Down k, a)], MinPQueue (Down k) a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MinPQueue k a -> ([(k, a)], MinPQueue k a)
Q.breakWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q of
  ([(Down k, a)]
xs, MinPQueue (Down k) a
q') -> (((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) [(Down k, a)]
xs, MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q')

-- | \(O(n)\). Filter all values that satisfy the predicate.
filter :: Ord k => (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
filter :: (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
filter = (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
forall k a.
Ord k =>
(k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
filterWithKey ((k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a)
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | \(O(n)\). Filter all values that satisfy the predicate.
filterWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
filterWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a
filterWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> Bool)
-> MinPQueue (Down k) a -> MinPQueue (Down k) a
forall k a.
Ord k =>
(k -> a -> Bool) -> MinPQueue k a -> MinPQueue k a
Q.filterWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | \(O(n)\). Partition the queue according to a predicate. The first queue contains all elements
-- which satisfy the predicate, the second all elements that fail the predicate.
partition :: Ord k => (a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
partition :: (a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
partition = (k -> a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
partitionWithKey ((k -> a -> Bool)
 -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a))
-> ((a -> Bool) -> k -> a -> Bool)
-> (a -> Bool)
-> MaxPQueue k a
-> (MaxPQueue k a, MaxPQueue k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const

-- | \(O(n)\). Partition the queue according to a predicate. The first queue contains all elements
-- which satisfy the predicate, the second all elements that fail the predicate.
partitionWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
partitionWithKey :: (k -> a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a)
partitionWithKey k -> a -> Bool
p (MaxPQ MinPQueue (Down k) a
q) = case (Down k -> a -> Bool)
-> MinPQueue (Down k) a
-> (MinPQueue (Down k) a, MinPQueue (Down k) a)
forall k a.
Ord k =>
(k -> a -> Bool) -> MinPQueue k a -> (MinPQueue k a, MinPQueue k a)
Q.partitionWithKey (k -> a -> Bool
p (k -> a -> Bool) -> (Down k -> k) -> Down k -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q of
  (MinPQueue (Down k) a
q1, MinPQueue (Down k) a
q0) -> (MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q1, MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) a
q0)

-- | \(O(n)\). Map values and collect the 'Just' results.
mapMaybe :: Ord k => (a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
mapMaybe :: (a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
mapMaybe = (k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
forall k a b.
Ord k =>
(k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
mapMaybeWithKey ((k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b)
-> ((a -> Maybe b) -> k -> a -> Maybe b)
-> (a -> Maybe b)
-> MaxPQueue k a
-> MaxPQueue k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe b) -> k -> a -> Maybe b
forall a b. a -> b -> a
const

-- | \(O(n)\). Map values and collect the 'Just' results.
mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
mapMaybeWithKey :: (k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b
mapMaybeWithKey k -> a -> Maybe b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ ((Down k -> a -> Maybe b)
-> MinPQueue (Down k) a -> MinPQueue (Down k) b
forall k a b.
Ord k =>
(k -> a -> Maybe b) -> MinPQueue k a -> MinPQueue k b
Q.mapMaybeWithKey (k -> a -> Maybe b
f (k -> a -> Maybe b) -> (Down k -> k) -> Down k -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q)

-- | \(O(n)\). Map values and separate the 'Left' and 'Right' results.
mapEither :: Ord k => (a -> Either b c) -> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
mapEither :: (a -> Either b c)
-> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
mapEither = (k -> a -> Either b c)
-> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
forall k a b c.
Ord k =>
(k -> a -> Either b c)
-> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
mapEitherWithKey ((k -> a -> Either b c)
 -> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c))
-> ((a -> Either b c) -> k -> a -> Either b c)
-> (a -> Either b c)
-> MaxPQueue k a
-> (MaxPQueue k b, MaxPQueue k c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either b c) -> k -> a -> Either b c
forall a b. a -> b -> a
const

-- | \(O(n)\). Map values and separate the 'Left' and 'Right' results.
mapEitherWithKey :: Ord k => (k -> a -> Either b c) -> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
mapEitherWithKey :: (k -> a -> Either b c)
-> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c)
mapEitherWithKey k -> a -> Either b c
f (MaxPQ MinPQueue (Down k) a
q) = case (Down k -> a -> Either b c)
-> MinPQueue (Down k) a
-> (MinPQueue (Down k) b, MinPQueue (Down k) c)
forall k a b c.
Ord k =>
(k -> a -> Either b c)
-> MinPQueue k a -> (MinPQueue k b, MinPQueue k c)
Q.mapEitherWithKey (k -> a -> Either b c
f (k -> a -> Either b c)
-> (Down k -> k) -> Down k -> a -> Either b c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q of
  (MinPQueue (Down k) b
qL, MinPQueue (Down k) c
qR) -> (MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) b
qL, MinPQueue (Down k) c -> MaxPQueue k c
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ MinPQueue (Down k) c
qR)

-- | \(O(n)\). Build a priority queue from the list of (key, value) pairs.
fromList :: Ord k => [(k, a)] -> MaxPQueue k a
fromList :: [(k, a)] -> MaxPQueue k a
fromList = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) a -> MaxPQueue k a)
-> ([(k, a)] -> MinPQueue (Down k) a) -> [(k, a)] -> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Down k, a)] -> MinPQueue (Down k) a
forall k a. Ord k => [(k, a)] -> MinPQueue k a
Q.fromList ([(Down k, a)] -> MinPQueue (Down k) a)
-> ([(k, a)] -> [(Down k, a)]) -> [(k, a)] -> MinPQueue (Down k) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> (Down k, a)) -> [(k, a)] -> [(Down k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((k -> Down k) -> (k, a) -> (Down k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' k -> Down k
forall a. a -> Down a
Down)

-- | \(O(n)\). Build a priority queue from an ascending list of (key, value) pairs. /The precondition is not checked./
fromAscList :: [(k, a)] -> MaxPQueue k a
fromAscList :: [(k, a)] -> MaxPQueue k a
fromAscList = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) a -> MaxPQueue k a)
-> ([(k, a)] -> MinPQueue (Down k) a) -> [(k, a)] -> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Down k, a)] -> MinPQueue (Down k) a
forall k a. [(k, a)] -> MinPQueue k a
Q.fromDescList ([(Down k, a)] -> MinPQueue (Down k) a)
-> ([(k, a)] -> [(Down k, a)]) -> [(k, a)] -> MinPQueue (Down k) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> (Down k, a)) -> [(k, a)] -> [(Down k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((k -> Down k) -> (k, a) -> (Down k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' k -> Down k
forall a. a -> Down a
Down)

-- | \(O(n)\). Build a priority queue from a descending list of (key, value) pairs. /The precondition is not checked./
fromDescList :: [(k, a)] -> MaxPQueue k a
fromDescList :: [(k, a)] -> MaxPQueue k a
fromDescList = MinPQueue (Down k) a -> MaxPQueue k a
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) a -> MaxPQueue k a)
-> ([(k, a)] -> MinPQueue (Down k) a) -> [(k, a)] -> MaxPQueue k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Down k, a)] -> MinPQueue (Down k) a
forall k a. [(k, a)] -> MinPQueue k a
Q.fromAscList ([(Down k, a)] -> MinPQueue (Down k) a)
-> ([(k, a)] -> [(Down k, a)]) -> [(k, a)] -> MinPQueue (Down k) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> (Down k, a)) -> [(k, a)] -> [(Down k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((k -> Down k) -> (k, a) -> (Down k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' k -> Down k
forall a. a -> Down a
Down)

-- | \(O(n \log n)\). Return all keys of the queue in descending order.
keys :: Ord k => MaxPQueue k a -> [k]
keys :: MaxPQueue k a -> [k]
keys = ((k, a) -> k) -> [(k, a)] -> [k]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> k
forall a b. (a, b) -> a
fst ([(k, a)] -> [k])
-> (MaxPQueue k a -> [(k, a)]) -> MaxPQueue k a -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> [(k, a)]
forall k a. Ord k => MaxPQueue k a -> [(k, a)]
toDescList

-- | \(O(n \log n)\). Return all elements of the queue in descending order by key.
elems :: Ord k => MaxPQueue k a -> [a]
elems :: MaxPQueue k a -> [a]
elems = ((k, a) -> a) -> [(k, a)] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> a
forall a b. (a, b) -> b
snd ([(k, a)] -> [a])
-> (MaxPQueue k a -> [(k, a)]) -> MaxPQueue k a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> [(k, a)]
forall k a. Ord k => MaxPQueue k a -> [(k, a)]
toDescList

-- | \(O(n \log n)\). Equivalent to 'toDescList'.
assocs :: Ord k => MaxPQueue k a -> [(k, a)]
assocs :: MaxPQueue k a -> [(k, a)]
assocs = MaxPQueue k a -> [(k, a)]
forall k a. Ord k => MaxPQueue k a -> [(k, a)]
toDescList

-- | \(O(n \log n)\). Return all (key, value) pairs in ascending order by key.
toAscList :: Ord k => MaxPQueue k a -> [(k, a)]
toAscList :: MaxPQueue k a -> [(k, a)]
toAscList (MaxPQ MinPQueue (Down k) a
q) = ((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) (MinPQueue (Down k) a -> [(Down k, a)]
forall k a. Ord k => MinPQueue k a -> [(k, a)]
Q.toDescList MinPQueue (Down k) a
q)

-- | \(O(n \log n)\). Return all (key, value) pairs in descending order by key.
toDescList :: Ord k => MaxPQueue k a -> [(k, a)]
toDescList :: MaxPQueue k a -> [(k, a)]
toDescList (MaxPQ MinPQueue (Down k) a
q) = ((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) (MinPQueue (Down k) a -> [(Down k, a)]
forall k a. Ord k => MinPQueue k a -> [(k, a)]
Q.toAscList MinPQueue (Down k) a
q)

-- | \(O(n \log n)\). Equivalent to 'toDescList'.
--
-- If the traversal order is irrelevant, consider using 'toListU'.
toList :: Ord k => MaxPQueue k a -> [(k, a)]
toList :: MaxPQueue k a -> [(k, a)]
toList = MaxPQueue k a -> [(k, a)]
forall k a. Ord k => MaxPQueue k a -> [(k, a)]
toDescList

-- | \(O(n)\). An unordered right fold over the elements of the queue, in no particular order.
foldrU :: (a -> b -> b) -> b -> MaxPQueue k a -> b
foldrU :: (a -> b -> b) -> b -> MaxPQueue k a -> b
foldrU = (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
forall k a b. (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
foldrWithKeyU ((k -> a -> b -> b) -> b -> MaxPQueue k a -> b)
-> ((a -> b -> b) -> k -> a -> b -> b)
-> (a -> b -> b)
-> b
-> MaxPQueue k a
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> k -> a -> b -> b
forall a b. a -> b -> a
const

-- | \(O(n)\). An unordered right fold over the elements of the queue, in no particular order.
foldrWithKeyU :: (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
foldrWithKeyU :: (k -> a -> b -> b) -> b -> MaxPQueue k a -> b
foldrWithKeyU k -> a -> b -> b
f b
z (MaxPQ MinPQueue (Down k) a
q) = (Down k -> a -> b -> b) -> b -> MinPQueue (Down k) a -> b
forall k a b. (k -> a -> b -> b) -> b -> MinPQueue k a -> b
Q.foldrWithKeyU (k -> a -> b -> b
f (k -> a -> b -> b) -> (Down k -> k) -> Down k -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) b
z MinPQueue (Down k) a
q

-- | \(O(n)\). An unordered monoidal fold over the elements of the queue, in no particular order.
--
-- @since 1.4.2
foldMapWithKeyU :: Monoid m => (k -> a -> m) -> MaxPQueue k a -> m
foldMapWithKeyU :: (k -> a -> m) -> MaxPQueue k a -> m
foldMapWithKeyU k -> a -> m
f (MaxPQ MinPQueue (Down k) a
q) = (Down k -> a -> m) -> MinPQueue (Down k) a -> m
forall m k a. Monoid m => (k -> a -> m) -> MinPQueue k a -> m
Q.foldMapWithKeyU (k -> a -> m
f (k -> a -> m) -> (Down k -> k) -> Down k -> a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q

-- | \(O(n)\). An unordered left fold over the elements of the queue, in no
-- particular order. This is rarely what you want; 'foldrU' and 'foldlU'' are
-- more likely to perform well.
foldlU :: (b -> a -> b) -> b -> MaxPQueue k a -> b
foldlU :: (b -> a -> b) -> b -> MaxPQueue k a -> b
foldlU b -> a -> b
f = (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
forall b k a. (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU ((a -> b) -> k -> a -> b
forall a b. a -> b -> a
const ((a -> b) -> k -> a -> b) -> (b -> a -> b) -> b -> k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
f)

-- | \(O(n)\). An unordered strict left fold over the elements of the queue, in no
-- particular order.
--
-- @since 1.4.2
foldlU' :: (b -> a -> b) -> b -> MaxPQueue k a -> b
foldlU' :: (b -> a -> b) -> b -> MaxPQueue k a -> b
foldlU' b -> a -> b
f = (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
forall b k a. (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU' ((a -> b) -> k -> a -> b
forall a b. a -> b -> a
const ((a -> b) -> k -> a -> b) -> (b -> a -> b) -> b -> k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
f)

-- | \(O(n)\). An unordered left fold over the elements of the queue, in no
-- particular order. This is rarely what you want; 'foldrWithKeyU' and
-- 'foldlWithKeyU'' are more likely to perform well.
foldlWithKeyU :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU b -> k -> a -> b
f b
z0 (MaxPQ MinPQueue (Down k) a
q) = (b -> Down k -> a -> b) -> b -> MinPQueue (Down k) a -> b
forall b k a. (b -> k -> a -> b) -> b -> MinPQueue k a -> b
Q.foldlWithKeyU (\b
z -> b -> k -> a -> b
f b
z (k -> a -> b) -> (Down k -> k) -> Down k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) b
z0 MinPQueue (Down k) a
q

-- | \(O(n)\). An unordered left fold over the elements of the queue, in no particular order.
--
-- @since 1.4.2
foldlWithKeyU' :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU' :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b
foldlWithKeyU' b -> k -> a -> b
f b
z0 (MaxPQ MinPQueue (Down k) a
q) = (b -> Down k -> a -> b) -> b -> MinPQueue (Down k) a -> b
forall b k a. (b -> k -> a -> b) -> b -> MinPQueue k a -> b
Q.foldlWithKeyU' (\b
z -> b -> k -> a -> b
f b
z (k -> a -> b) -> (Down k -> k) -> Down k -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) b
z0 MinPQueue (Down k) a
q

-- | \(O(n)\). An unordered traversal over a priority queue, in no particular order.
-- While there is no guarantee in which order the elements are traversed, the resulting
-- priority queue will be perfectly valid.
traverseU :: (Applicative f) => (a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseU :: (a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseU = (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseWithKeyU ((k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b))
-> ((a -> f b) -> k -> a -> f b)
-> (a -> f b)
-> MaxPQueue k a
-> f (MaxPQueue k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f b) -> k -> a -> f b
forall a b. a -> b -> a
const

-- | \(O(n)\). An unordered traversal over a priority queue, in no particular order.
-- While there is no guarantee in which order the elements are traversed, the resulting
-- priority queue will be perfectly valid.
traverseWithKeyU :: (Applicative f) => (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseWithKeyU :: (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b)
traverseWithKeyU k -> a -> f b
f (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) b -> MaxPQueue k b
forall k a. MinPQueue (Down k) a -> MaxPQueue k a
MaxPQ (MinPQueue (Down k) b -> MaxPQueue k b)
-> f (MinPQueue (Down k) b) -> f (MaxPQueue k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Down k -> a -> f b)
-> MinPQueue (Down k) a -> f (MinPQueue (Down k) b)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f b) -> MinPQueue k a -> f (MinPQueue k b)
Q.traverseWithKeyU (k -> a -> f b
f (k -> a -> f b) -> (Down k -> k) -> Down k -> a -> f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Down k -> k
forall a. Down a -> a
unDown) MinPQueue (Down k) a
q

-- | \(O(n)\). Return all keys of the queue in no particular order.
keysU :: MaxPQueue k a -> [k]
keysU :: MaxPQueue k a -> [k]
keysU = ((k, a) -> k) -> [(k, a)] -> [k]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> k
forall a b. (a, b) -> a
fst ([(k, a)] -> [k])
-> (MaxPQueue k a -> [(k, a)]) -> MaxPQueue k a -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> [(k, a)]
forall k a. MaxPQueue k a -> [(k, a)]
toListU

-- | \(O(n)\). Return all elements of the queue in no particular order.
elemsU :: MaxPQueue k a -> [a]
elemsU :: MaxPQueue k a -> [a]
elemsU = ((k, a) -> a) -> [(k, a)] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, a) -> a
forall a b. (a, b) -> b
snd ([(k, a)] -> [a])
-> (MaxPQueue k a -> [(k, a)]) -> MaxPQueue k a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaxPQueue k a -> [(k, a)]
forall k a. MaxPQueue k a -> [(k, a)]
toListU

-- | \(O(n)\). Equivalent to 'toListU'.
assocsU :: MaxPQueue k a -> [(k, a)]
assocsU :: MaxPQueue k a -> [(k, a)]
assocsU = MaxPQueue k a -> [(k, a)]
forall k a. MaxPQueue k a -> [(k, a)]
toListU

-- | \(O(n)\). Returns all (key, value) pairs in the queue in no particular order.
toListU :: MaxPQueue k a -> [(k, a)]
toListU :: MaxPQueue k a -> [(k, a)]
toListU (MaxPQ MinPQueue (Down k) a
q) = ((Down k, a) -> (k, a)) -> [(Down k, a)] -> [(k, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Down k -> k) -> (Down k, a) -> (k, a)
forall a b c. (a -> b) -> (a, c) -> (b, c)
first' Down k -> k
forall a. Down a -> a
unDown) (MinPQueue (Down k) a -> [(Down k, a)]
forall k a. MinPQueue k a -> [(k, a)]
Q.toListU MinPQueue (Down k) a
q)

-- | \(O(\log n)\). @seqSpine q r@ forces the spine of @q@ and returns @r@.
--
-- Note: The spine of a 'MaxPQueue' is stored somewhat lazily. Most operations
-- take great care to prevent chains of thunks from accumulating along the
-- spine to the detriment of performance. However, 'mapKeysMonotonic' can leave
-- expensive thunks in the structure and repeated applications of that function
-- can create thunk chains.
seqSpine :: MaxPQueue k a -> b -> b
seqSpine :: MaxPQueue k a -> b -> b
seqSpine (MaxPQ MinPQueue (Down k) a
q) = MinPQueue (Down k) a -> b -> b
forall k a b. MinPQueue k a -> b -> b
Q.seqSpine MinPQueue (Down k) a
q