API Reference

PQ class

class priorityq.PQ(values=None, comparator=<built-in function cmp>, duplicates=False, store=None)[source]

A flexible PriorityQueue wrapper to allow deletions, fast findings and updates of element priorities.

adjust(value_or_handle)[source]

Called to reevaluate the position of an entry within the heap.

This is usually called after an entry has been modified such that its position in the heap would have changed (due to a change in its priority).

find(value)[source]

Returns a handle to the first instance of a particular value.

pop()[source]

Removes the top value from the PQ and returns its value.

push(value)[source]

Pushes a new value onto the PQ.

If the value already exists, then the value is only added again to the storage if the duplicates flag is set to False.

Returns a handle to the value within the PQ.

remove(value_or_handle)[source]

Removes a given value from the PQ.

If a handle is passed instead of a value then only the value referred by the handle is removed (regardless of other duplicates). To remove all instances of a value from the PQ, pass the value instead.

top

Returns a handle to the minimum (top) value.

base.Storage class

class priorityq.storage.base.Storage(cmpfunc=<built-in function cmp>)[source]

Base class of all storage strategies that can back a priority queue.

Implementations

priorityq.storage.binheap.Storage priorityq.storage.binaryheap.Storage

adjust(handle)[source]

Called to move a particular value to the correct position in the heap after it has been modified.

all_handles()[source]

Returns a list of all the values in the heap.

clear()[source]

Removes all elements from the heap.

comparator

Returns the comparator currently used to prioritize the elements in this heap.

find(value)[source]

Returns a handle to the given value in the heap.

heapify(values)[source]

Heapifies a collection of values onto this heap.

Parameters

values - The iteratable of values that are to be added.

Returns

A list of handles for the values that were actually added onto the heap.

pop()[source]

Pops the top value from the stack and returns a handle to it.

push(value)[source]

Pushes a new value onto the storage.

Returns an implementation specific opaque handle to the value within the heap.

remove(handle)[source]

Remove a value that is referenced by a particular handle from the heap.

top

Returns a handle to the top value on the heap.

base.Handle class

class priorityq.storage.base.Handle(value)[source]

Base class of all opaque reference/handles to actual values contained in the internal nodes of a PQ. Specific implementations of priority queues/heaps would inherit from this and return these handle values which can also be passed back to a queue.

value

Returns the value pointed by the handle.