psqueues-0.2.2.3: Pure priority search queues

Safe HaskellTrustworthy
LanguageHaskell98

Data.OrdPSQ

Contents

Description

An OrdPSQ uses the Ord instance of the key type to build a priority search queue.

It is based on Ralf Hinze's work.

  • Hinze, R., A Simple Implementation Technique for Priority Search Queues, ICFP 2001, pp. 110-121

http://citeseer.ist.psu.edu/hinze01simple.html

This means it is similar to the PSQueue package but our benchmarks showed it perform quite a bit faster.

Synopsis

Type

data OrdPSQ k p v Source #

A mapping from keys k to priorites p and values v. It is strict in keys, priorities and values.

Instances

Functor (OrdPSQ k p) Source # 

Methods

fmap :: (a -> b) -> OrdPSQ k p a -> OrdPSQ k p b #

(<$) :: a -> OrdPSQ k p b -> OrdPSQ k p a #

Foldable (OrdPSQ k p) Source # 

Methods

fold :: Monoid m => OrdPSQ k p m -> m #

foldMap :: Monoid m => (a -> m) -> OrdPSQ k p a -> m #

foldr :: (a -> b -> b) -> b -> OrdPSQ k p a -> b #

foldr' :: (a -> b -> b) -> b -> OrdPSQ k p a -> b #

foldl :: (b -> a -> b) -> b -> OrdPSQ k p a -> b #

foldl' :: (b -> a -> b) -> b -> OrdPSQ k p a -> b #

foldr1 :: (a -> a -> a) -> OrdPSQ k p a -> a #

foldl1 :: (a -> a -> a) -> OrdPSQ k p a -> a #

toList :: OrdPSQ k p a -> [a] #

null :: OrdPSQ k p a -> Bool #

length :: OrdPSQ k p a -> Int #

elem :: Eq a => a -> OrdPSQ k p a -> Bool #

maximum :: Ord a => OrdPSQ k p a -> a #

minimum :: Ord a => OrdPSQ k p a -> a #

sum :: Num a => OrdPSQ k p a -> a #

product :: Num a => OrdPSQ k p a -> a #

Traversable (OrdPSQ k p) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> OrdPSQ k p a -> f (OrdPSQ k p b) #

sequenceA :: Applicative f => OrdPSQ k p (f a) -> f (OrdPSQ k p a) #

mapM :: Monad m => (a -> m b) -> OrdPSQ k p a -> m (OrdPSQ k p b) #

sequence :: Monad m => OrdPSQ k p (m a) -> m (OrdPSQ k p a) #

(Ord k, Ord p, Eq v) => Eq (OrdPSQ k p v) Source # 

Methods

(==) :: OrdPSQ k p v -> OrdPSQ k p v -> Bool #

(/=) :: OrdPSQ k p v -> OrdPSQ k p v -> Bool #

(Show k, Show p, Show v) => Show (OrdPSQ k p v) Source # 

Methods

showsPrec :: Int -> OrdPSQ k p v -> ShowS #

show :: OrdPSQ k p v -> String #

showList :: [OrdPSQ k p v] -> ShowS #

(NFData k, NFData p, NFData v) => NFData (OrdPSQ k p v) Source # 

Methods

rnf :: OrdPSQ k p v -> () #

Query

null :: OrdPSQ k p v -> Bool Source #

O(1) True if the queue is empty.

size :: OrdPSQ k p v -> Int Source #

O(1) The number of elements in a queue.

member :: Ord k => k -> OrdPSQ k p v -> Bool Source #

O(log n) Check if a key is present in the the queue.

lookup :: Ord k => k -> OrdPSQ k p v -> Maybe (p, v) Source #

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

findMin :: OrdPSQ k p v -> Maybe (k, p, v) Source #

O(1) The element with the lowest priority.

Construction

empty :: OrdPSQ k p v Source #

O(1) The empty queue.

singleton :: k -> p -> v -> OrdPSQ k p v Source #

O(1) Build a queue with one element.

Insertion

insert :: (Ord k, Ord p) => k -> p -> v -> OrdPSQ k p v -> OrdPSQ k p v Source #

O(log n) 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 :: (Ord k, Ord p) => k -> OrdPSQ k p v -> OrdPSQ k p v Source #

O(log n) 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 :: (Ord k, Ord p) => OrdPSQ k p v -> OrdPSQ k p v Source #

O(log n) 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 :: (Ord k, Ord p) => (Maybe (p, v) -> (b, Maybe (p, v))) -> k -> OrdPSQ k p v -> (b, OrdPSQ k p v) Source #

O(log n) 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 :: (Ord k, Ord p) => (Maybe (k, p, v) -> (b, Maybe (k, p, v))) -> OrdPSQ k p v -> (b, OrdPSQ k p v) Source #

O(log n) 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.

Conversion

fromList :: (Ord k, Ord p) => [(k, p, v)] -> OrdPSQ k p v Source #

O(n*log n) 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 :: OrdPSQ k p v -> [(k, p, v)] Source #

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

toAscList :: OrdPSQ k p v -> [(k, p, v)] Source #

O(n) Convert to an ascending list.

keys :: OrdPSQ k p v -> [k] Source #

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

Views

insertView :: (Ord k, Ord p) => k -> p -> v -> OrdPSQ k p v -> (Maybe (p, v), OrdPSQ k p v) Source #

O(log n) 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 :: (Ord k, Ord p) => k -> OrdPSQ k p v -> Maybe (p, v, OrdPSQ k p v) Source #

O(log n) 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 :: (Ord k, Ord p) => OrdPSQ k p v -> Maybe (k, p, v, OrdPSQ k p v) Source #

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

Traversals

map :: forall k p v w. (k -> p -> v -> w) -> OrdPSQ k p v -> OrdPSQ k p w Source #

O(n) Modify every value in the queue.

fold' :: (k -> p -> v -> a -> a) -> a -> OrdPSQ k p v -> a Source #

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 :: (Ord k, Ord p) => OrdPSQ k p v -> Bool Source #

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