HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepqueue-1.5.0.0Haskell2010

Data.PQueue.Max

General purpose priority queue, supporting view-maximum operations.

An amortized running time is given for each operation, with n referring to the length of the sequence and k being the integral index used by some operations. These bounds hold even in a persistent (shared) setting.

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

This implementation does not guarantee stable behavior.

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.

  • 1 type
  • 45 values
  • Packagepqueue-1.5.0.0
  • Exports46
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMax.hs
newtypenewtype MaxQueue a
#

A priority queue with elements of type a. Supports extracting the maximum element. Implemented as a wrapper around MinQueue.

Instances8Eq, Data, Ord, Read, Show, Semigroup, …

Basic operations

3 declarations

Query operations

6 declarations
valuefindMax :: MaxQueue a -> a
#

O(1). Returns the maximum element of the queue. Throws an error on an empty queue.

valuegetMax :: MaxQueue a -> Maybe a
#

O(1). The top (maximum) element of the queue, if there is one.

Construction operations

4 declarations
valuesingleton :: a -> MaxQueue a
#

O(1). Construct a priority queue with a single element.

Subsets

0 declarations

Extracting subsets

value(!!) :: Ord a => MaxQueue a -> Int -> a
#

O(k \log n)/. Returns the (k+1)th largest element of the queue.

valuetake :: Ord a => Int -> MaxQueue a -> [a]
#

O(k \log n)/. Returns the list of the k largest elements of the queue, in descending order, or all elements of the queue, if k >= n.

valuedrop :: Ord a => Int -> MaxQueue a -> MaxQueue a
#

O(k \log n)/. Returns the queue with the k largest elements deleted, or the empty queue if k >= n.

Predicates

valuetakeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a]
#

takeWhile, applied to a predicate p and a queue queue, returns the longest prefix (possibly empty) of queue of elements that satisfy p.

valuespan :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a)
#

span, applied to a predicate p and a queue queue, returns a tuple where first element is longest prefix (possibly empty) of queue of elements that satisfy p and second element is the remainder of the queue.

valuebreak :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a)
#

break, applied to a predicate p and a queue queue, returns a tuple where first element is longest prefix (possibly empty) of queue of elements that do not satisfy p and second element is the remainder of the queue.

Filter/Map

4 declarations
valuepartition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a)
#

O(n). Returns a pair of queues, where the left queue contains those elements that satisfy the predicate, and the right queue contains those that do not.

Fold/Functor/Traversable variations

5 declarations
valuemap :: Ord b => (a -> b) -> MaxQueue a -> MaxQueue b
#

O(n). Creates a new priority queue containing the images of the elements of this queue. Equivalent to fromList . map f . toList.

valuefoldrAsc :: Ord a => (a -> b -> b) -> b -> MaxQueue a -> b
#

O(n \log n). Performs a right-fold on the elements of a priority queue in ascending order. foldrAsc f z q == foldlDesc (flip f) z q.

valuefoldlAsc :: Ord a => (b -> a -> b) -> b -> MaxQueue a -> b
#

O(n \log n). Performs a left-fold on the elements of a priority queue in descending order. foldlAsc f z q == foldrDesc (flip f) z q.

valuefoldrDesc :: Ord a => (a -> b -> b) -> b -> MaxQueue a -> b
#

O(n \log n). Performs a right-fold on the elements of a priority queue in descending order.

valuefoldlDesc :: Ord a => (b -> a -> b) -> b -> MaxQueue a -> b
#

O(n \log n). Performs a left-fold on the elements of a priority queue in descending order.

List operations

6 declarations
valuetoList :: Ord a => MaxQueue a -> [a]
#

O(n \log n). Returns the elements of the priority queue in ascending order. Equivalent to toDescList.

If the order of the elements is irrelevant, consider using toListU.

valuetoAscList :: Ord a => MaxQueue a -> [a]
#

O(n \log n). Extracts the elements of the priority queue in ascending order.

valuetoDescList :: Ord a => MaxQueue a -> [a]
#

O(n \log n). Extracts the elements of the priority queue in descending order.

valuefromList :: Ord a => [a] -> MaxQueue a
#

O(n \log n). Constructs a priority queue from an unordered list.

valuefromAscList :: [a] -> MaxQueue a
#

O(n). Constructs a priority queue from an ascending list. Warning: Does not check the precondition.

valuefromDescList :: [a] -> MaxQueue a
#

O(n). Constructs a priority queue from a descending list. Warning: Does not check the precondition.

Unordered operations

7 declarations
valuemapU :: (a -> b) -> MaxQueue a -> MaxQueue b
#

O(n). Assumes that the function it is given is monotonic, and applies this function to every element of the priority queue. Does not check the precondition.

valuefoldrU :: (a -> b -> b) -> b -> MaxQueue a -> b
#

O(n). Unordered right fold on a priority queue.

valuefoldlU :: (b -> a -> b) -> b -> MaxQueue a -> b
#

O(n). Unordered left fold on a priority queue. This is rarely what you want; foldrU and foldlU' are more likely to perform well.

valuefoldlU' :: (b -> a -> b) -> b -> MaxQueue a -> b
#

O(n). Unordered strict left fold on a priority queue.

valuetoListU :: MaxQueue a -> [a]
#

O(n). Returns a list of the elements of the priority queue, in no particular order.

Miscellaneous operations

2 declarations
valueseqSpine :: MaxQueue 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 MaxQueue 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.