psqueues-0.2.2.3: Pure priority search queues

Safe HaskellNone
LanguageHaskell98

Data.HashPSQ

Contents

Description

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.

Synopsis

Type

data 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.

Instances

Functor (HashPSQ k p) 
Foldable (HashPSQ k p) 
Traversable (HashPSQ k p) 
(Eq k, Eq p, Eq v, Hashable k, Ord k, Ord p) => Eq (HashPSQ k p v) 
(Show k, Show p, Show v) => Show (HashPSQ k p v) 
(NFData k, NFData p, NFData v) => NFData (HashPSQ k p v) 

Query

null :: HashPSQ k p v -> Bool

O(1) True if the queue is empty.

size :: HashPSQ k p v -> Int

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

member :: (Hashable k, Ord k, Ord p) => k -> HashPSQ k p v -> Bool

O(min(n,W)) Check if a key is present in the the queue.

lookup :: (Ord k, Hashable k, Ord p) => k -> HashPSQ k p v -> Maybe (p, v)

O(min(n,W)) The priority and value of a given key, or Nothing if the key is not bound.

findMin :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> Maybe (k, p, v)

O(1) The element with the lowest priority.

Construction

empty :: HashPSQ k p v

O(1) The empty queue.

singleton :: (Hashable k, Ord k, Ord p) => k -> p -> v -> HashPSQ k p v

O(1) Build a queue with one element.

Insertion

insert :: (Ord k, Hashable k, Ord p) => k -> p -> v -> HashPSQ k 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, the associated priority and value are replaced with the supplied priority and value.

Delete/update

delete :: (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.

deleteMin :: (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.

alter :: (Hashable k, Ord k, Ord p) => (Maybe (p, v) -> (b, Maybe (p, v))) -> k -> HashPSQ k p v -> (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.

alterMin :: (Hashable k, Ord k, Ord p) => (Maybe (k, p, v) -> (b, Maybe (k, p, v))) -> HashPSQ k p v -> (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

fromList :: (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.

toList :: (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.

keys :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> [k]

O(n) Obtain the list of present keys in the queue.

Views

insertView :: (Hashable k, Ord k, Ord p) => k -> p -> v -> HashPSQ k p v -> (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.

deleteView :: forall k p v. (Hashable k, Ord k, Ord p) => k -> HashPSQ k p v -> 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.

minView :: (Hashable k, Ord k, Ord p) => HashPSQ k p v -> 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.

Traversal

map :: (k -> p -> v -> w) -> HashPSQ k p v -> HashPSQ k p w

O(n) Modify every value in the queue.

fold' :: (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.

Validity check

valid :: (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.