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

Modulepsqueues-0.2.8.2Haskell2010

Data.HashPSQ

A HashPSQ offers very similar performance to IntPSQ. In case of collisions, it uses an OrdPSQ locally to solve those.

This means worst case complexity is usually given by O(min(n,W), log n), where W is the number of bits in an Int. This simplifies to O(min(n,W)) since log n is always smaller than W on current machines.

  • 1 type
  • 26 values
  • Packagepsqueues-0.2.8.2
  • Exports27
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceHashPSQ.hs

Type

1 declaration
newtypenewtype HashPSQ k p v
#

A priority search queue with keys of type k 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

5 declarations
valuesize :: HashPSQ k p v -> Int
#

O(n) The number of elements stored in the PSQ.

Construction

2 declarations

Insertion

1 declaration
valueinsert
  1. :: (Ord k, Hashable k, Ord p)
  2. => k
  3. -> p
  4. -> v
  5. -> HashPSQ k p v
  6. -> HashPSQ k 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

4 declarations
valuedelete :: (Hashable k, Ord k, Ord p) => k -> HashPSQ k p v -> HashPSQ k 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 :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> HashPSQ k 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. :: (Hashable k, Ord k, Ord p)
  2. => Maybe (p, v) -> (b, Maybe (p, v))
  3. -> k
  4. -> HashPSQ k p v
  5. -> (b, HashPSQ k 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.

valuealterMin
  1. :: (Hashable k, Ord k, Ord p)
  2. => Maybe (k, p, v) -> (b, Maybe (k, p, v))
  3. -> HashPSQ k p v
  4. -> (b, HashPSQ k p v)
#

O(min(n,W)) A variant of alter which works on the element with the minimum priority. Unlike alter, this variant also allows you to change the key of the element.

Lists

3 declarations
valuefromList :: (Hashable k, Ord k, Ord p) => [(k, p, v)] -> HashPSQ k 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 :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> [(k, p, v)]
#

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

Views

4 declarations
valueinsertView
  1. :: (Hashable k, Ord k, Ord p)
  2. => k
  3. -> p
  4. -> v
  5. -> HashPSQ k p v
  6. -> (Maybe (p, v), HashPSQ k 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
  1. :: (Hashable k, Ord k, Ord p)
  2. => k
  3. -> HashPSQ k p v
  4. -> Maybe (p, v, HashPSQ k 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
  1. :: (Hashable k, Ord k, Ord p)
  2. => HashPSQ k p v
  3. -> Maybe (k, p, v, HashPSQ k p v)
#

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

valueatMostView
  1. :: (Hashable k, Ord k, Ord p)
  2. => p
  3. -> HashPSQ k p v
  4. -> ([(k, p, v)], HashPSQ k 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

3 declarations
valuemap :: (k -> p -> v -> w) -> HashPSQ k p v -> HashPSQ k p w
#

O(n) Modify every value in the queue.

valueunsafeMapMonotonic
  1. :: k -> p -> v -> (q, w)
  2. -> HashPSQ k p v
  3. -> HashPSQ k 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' :: (k -> p -> v -> a -> a) -> a -> HashPSQ k 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

3 declarations

Validity check

1 declaration
valuevalid :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> Bool
#

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