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

Modulepsqueues-0.2.8.3Haskell2010

Data.IntPSQ

IntPSQ fixes the key type to Int. It is generally much faster than an OrdPSQ.

Many operations have a worst-case complexity of O(min(n,W)). This means that the operation can -- become linear in the number of elements with a maximum of W -- the number of bits in an Int (32 or 64).

Contents
  1. Type
  2. Query
  3. Construction
  4. Insertion
  5. Delete/update
  6. Lists
  7. Views
  8. Traversal
  9. Unsafe operations
  10. Validity check
Synopsis
  • Packagepsqueues-0.2.8.3
  • Exports30
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIntPSQ.hs

Type

datadata IntPSQ p v

A priority search queue with Int keys and priorities of type p and values of type v. It is strict in keys, priorities and values.

Instances6Functor, Foldable, Traversable, Eq, Show, NFData

Query

Construction

Insertion

valueinsert :: Ord p => Int -> p -> v -> IntPSQ p v -> IntPSQ p v

O(min(n,W)) Insert a new key, priority and value into the queue. If the key is already present in the queue, the associated priority and value are replaced with the supplied priority and value.

Delete/update

valuedelete :: Ord p => Int -> IntPSQ p v -> IntPSQ p v

O(min(n,W)) Delete a key and its priority and value from the queue. When the key is not a member of the queue, the original queue is returned.

valuedeleteMin :: Ord p => IntPSQ p v -> IntPSQ p v

O(min(n,W)) Delete the binding with the least priority, and return the rest of the queue stripped of that binding. In case the queue is empty, the empty queue is returned again.

valuealter
  1. :: Ord p
  2. => Maybe (p, v) -> (b, Maybe (p, v))
  3. -> Int
  4. -> IntPSQ p v
  5. -> (b, IntPSQ p v)

O(min(n,W)) The expression alter f k queue alters the value x at k, or absence thereof. alter can be used to insert, delete, or update a value in a queue. It also allows you to calculate an additional value b.

Lists

valuefromList :: Ord p => [(Int, p, v)] -> IntPSQ p v

O(n*min(n,W)) Build a queue from a list of (key, priority, value) tuples. If the list contains more than one priority and value for the same key, the last priority and value for the key is retained.

valuetoList :: IntPSQ p v -> [(Int, p, v)]

O(n) Convert a queue to a list of (key, priority, value) tuples. The order of the list is not specified.

Views

valueinsertView
  1. :: Ord p
  2. => Int
  3. -> p
  4. -> v
  5. -> IntPSQ p v
  6. -> (Maybe (p, v), IntPSQ p v)

O(min(n,W)) Insert a new key, priority and value into the queue. If the key is already present in the queue, then the evicted priority and value can be found the first element of the returned tuple.

valuedeleteView :: Ord p => Int -> IntPSQ p v -> Maybe (p, v, IntPSQ p v)

O(min(n,W)) Delete a key and its priority and value from the queue. If the key was present, the associated priority and value are returned in addition to the updated queue.

valueminView :: Ord p => IntPSQ p v -> Maybe (Int, p, v, IntPSQ p v)

O(min(n,W)) Retrieve the binding with the least priority, and the rest of the queue stripped of that binding.

valueatMostView :: Ord p => p -> IntPSQ p v -> ([(Int, p, v)], IntPSQ p v)

Return a list of elements ordered by key whose priorities are at most pt, and the rest of the queue stripped of these elements. The returned list of elements can be in any order: no guarantees there.

Traversal

valueunsafeMapMonotonic :: (Key -> p -> v -> (q, w)) -> IntPSQ p v -> IntPSQ q w

O(n) Maps a function over the values and priorities of the queue. The function f must be monotonic with respect to the priorities. I.e. if x < y, then fst (f k x v) < fst (f k y v). The precondition is not checked. If f is not monotonic, then the result will be invalid.

valuefold' :: (Int -> p -> v -> a -> a) -> a -> IntPSQ p v -> a

O(n) Strict fold over every key, priority and value in the queue. The order in which the fold is performed is not specified.

Unsafe operations

valueunsafeInsertIncreasePriority
  1. :: Ord p
  2. => Key
  3. -> p
  4. -> v
  5. -> IntPSQ p v
  6. -> IntPSQ p v

Internal function to insert a key with priority larger than the maximal priority in the heap. This is always the case when using the PSQ as the basis to implement a LRU cache, which associates a access-tick-number with every element.

valueunsafeInsertWithIncreasePriority
  1. :: Ord p
  2. => p -> v -> p -> v -> (p, v)
  3. -> Key
  4. -> p
  5. -> v
  6. -> IntPSQ p v
  7. -> IntPSQ p v

This name is not chosen well anymore. This function:

  • Either inserts a value at a new key with a prio higher than the max of the entire PSQ.

  • Or, overrides the value at the key with a prio strictly higher than the original prio at that key (but not necessarily the max of the entire PSQ).

Validity check

valuevalid :: Ord p => IntPSQ p v -> Bool

O(n^2) Internal function to check if the IntPSQ is valid, i.e. if all invariants hold. This should always be the case.