HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulepqueue-1.7.0.0Haskell2010

Data.PQueue.Prio.Max

General purpose priority queue. Each element is associated with a key, and the priority queue supports viewing and extracting the element with the maximum key.

A worst-case bound is given for each operation. In some cases, an amortized bound is also specified; these bounds hold even in a persistent context.

This implementation is based on a binomial heap augmented with a global root.

We do not guarantee stable behavior. Ties are broken arbitrarily -- that is, if k1 <= k2 and k2 <= k1, then there are no guarantees about the relative order in which k1, k2, and their associated elements are returned. (Unlike Data.Map, we allow multiple elements with the same key.)

This implementation offers a number of methods of the form xxxU, where U stands for unordered. No guarantees whatsoever are made on the execution or traversal order of these functions.

Contents
  1. Construction
  2. Query
  3. Maximum view
  4. Traversal
  5. Map
  6. Fold
  7. Traverse
  8. Subsets
  9. Indexed
  10. Predicates
  11. Filter
  12. List operations
  13. Conversion from lists
  14. Conversion to lists
  15. Unordered operations
  16. Helper methods
Synopsis
  • Packagepqueue-1.7.0.0
  • Exports72
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMax.hs
newtypenewtype MaxPQueue k a

A priority queue where values of type a are annotated with keys of type k. The queue supports extracting the element with maximum key.

Instances14FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Functor, Foldable, Traversable, …

Construction

valueinsert :: Ord k => k -> a -> MaxPQueue k a -> MaxPQueue k a

Amortized O(1), worst-case O(\log n). Inserts an element with the specified key into the queue.

Query

Maximum view

valueupdateMax :: Ord k => (a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a

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.

Traversal

Map

valuemapKeysMonotonic :: (k -> k') -> MaxPQueue k a -> MaxPQueue k' a

O(n). mapKeysMonotonic f q == mapKeys f q, but only works when f is (weakly) monotonic (meaning that x <= y implies f x <= f y). The precondition is not checked. This function has better performance than mapKeys.

Note: if the given function returns bottom for any of the keys in the queue, then the portion of the queue which is bottom is unspecified.

Fold

Traverse

Subsets

Indexed

Predicates

Filter

valuepartition
  1. :: Ord k
  2. => a -> Bool
  3. -> MaxPQueue k a
  4. -> (MaxPQueue k a, MaxPQueue k a)

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.

List operations

Conversion from lists

valuefromAscList :: [(k, a)] -> MaxPQueue k a

O(n). Build a priority queue from an ascending list of (key, value) pairs. The precondition is not checked.

valuefromDescList :: [(k, a)] -> MaxPQueue k a

O(n). Build a priority queue from a descending list of (key, value) pairs. The precondition is not checked.

Conversion to lists

valuekeys :: Ord k => MaxPQueue k a -> [k]

O(n \log n). Return all keys of the queue in descending order.

valueelems :: Ord k => MaxPQueue k a -> [a]

O(n \log n). Return all elements of the queue in descending order by key.

Unordered operations

valuefoldrU :: (a -> b -> b) -> b -> MaxPQueue k a -> b

O(n). An unordered right fold over the elements of the queue, in no particular order.

valuefoldrWithKeyU :: (k -> a -> b -> b) -> b -> MaxPQueue k a -> b

O(n). An unordered right fold over the elements of the queue, in no particular order.

valuefoldlU :: (b -> a -> b) -> b -> MaxPQueue k a -> b

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.

valuefoldlU' :: (b -> a -> b) -> b -> MaxPQueue k a -> b

O(n). An unordered strict left fold over the elements of the queue, in no particular order.

valuefoldlWithKeyU' :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b

O(n). An unordered left fold over the elements of the queue, in no particular order.

valuetraverseU
  1. :: Applicative f
  2. => a -> f b
  3. -> MaxPQueue k a
  4. -> f (MaxPQueue k b)

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.

valuetraverseWithKeyU
  1. :: Applicative f
  2. => k -> a -> f b
  3. -> MaxPQueue k a
  4. -> f (MaxPQueue k b)

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.

valuetoListU :: MaxPQueue k a -> [(k, a)]

O(n). Returns all (key, value) pairs in the queue in no particular order.

Helper methods

valueseqSpine :: MaxPQueue k a -> b -> b

Deprecated. This function is no longer necessary or useful.

O(\log n). seqSpine q r forces the spine of q and returns r.

Note: The spine of a MaxPQueue is stored somewhat lazily. In earlier versions of this package, some operations could produce chains of thunks along the spine, occasionally necessitating manual forcing. Now, all operations are careful to force enough to avoid this problem.