Data.OrdPSQ

psqueues-0.2.2.3: Pure priority search queues

Safe Haskell | Trustworthy
Language | Haskell98

Data.OrdPSQ

Contents

- Type
- Query
- Construction
- Insertion
- Delete/Update
- Conversion
- Views
- Traversals
- Validity check

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

- data OrdPSQ k p v
- null :: OrdPSQ k p v -> Bool
- size :: OrdPSQ k p v -> Int
- member :: Ord k => k -> OrdPSQ k p v -> Bool
- lookup :: Ord k => k -> OrdPSQ k p v -> Maybe (p, v)
- findMin :: OrdPSQ k p v -> Maybe (k, p, v)
- empty :: OrdPSQ k p v
- singleton :: k -> p -> v -> OrdPSQ k p v
- insert :: (Ord k, Ord p) => k -> p -> v -> OrdPSQ k p v -> OrdPSQ k p v
- delete :: (Ord k, Ord p) => k -> OrdPSQ k p v -> OrdPSQ k p v
- deleteMin :: (Ord k, Ord p) => OrdPSQ k p v -> OrdPSQ k p v
- alter :: (Ord k, Ord p) => (Maybe (p, v) -> (b, Maybe (p, v))) -> k -> OrdPSQ k p v -> (b, OrdPSQ k p v)
- alterMin :: (Ord k, Ord p) => (Maybe (k, p, v) -> (b, Maybe (k, p, v))) -> OrdPSQ k p v -> (b, OrdPSQ k p v)
- fromList :: (Ord k, Ord p) => [(k, p, v)] -> OrdPSQ k p v
- toList :: OrdPSQ k p v -> [(k, p, v)]
- toAscList :: OrdPSQ k p v -> [(k, p, v)]
- keys :: OrdPSQ k p v -> [k]
- insertView :: (Ord k, Ord p) => k -> p -> v -> OrdPSQ k p v -> (Maybe (p, v), OrdPSQ k p v)
- deleteView :: (Ord k, Ord p) => k -> OrdPSQ k p v -> Maybe (p, v, OrdPSQ k p v)
- minView :: (Ord k, Ord p) => OrdPSQ k p v -> Maybe (k, p, v, OrdPSQ k p v)
- map :: forall k p v w. (k -> p -> v -> w) -> OrdPSQ k p v -> OrdPSQ k p w
- fold' :: (k -> p -> v -> a -> a) -> a -> OrdPSQ k p v -> a
- valid :: (Ord k, Ord p) => OrdPSQ k p v -> Bool

# Type

data OrdPSQ k p v #

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

Instances

Functor (OrdPSQ k p) # |
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) # |
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) # |
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) # |
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) # |
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) # |
Methods rnf :: OrdPSQ k p v -> () #

# Query

null :: OrdPSQ k p v -> Bool #

O(1) True if the queue is empty.

size :: OrdPSQ k p v -> Int #

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

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

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

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

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) #

O(1) The element with the lowest priority.

# Construction

empty :: OrdPSQ k p v #

O(1) The empty queue.

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

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 #

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 #

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 #

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) #

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) #

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 #

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)] #

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)] #

O(n) Convert to an ascending list.

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

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) #

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) #

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) #

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 #

O(n) Modify every value in the queue.

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

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