The SimpleCxxLib package


#include "pqueue.h"

class PriorityQueue<ValueType>

This class models a structure called a priority queue in which values are processed in order of priority. As in conventional English usage, lower priority numbers correspond to higher effective priorities, so that a priority 1 item takes precedence over a priority 2 item.
Constructor
PriorityQueue() Initializes a new priority queue, which is initially empty.
Methods
back() Returns the last value in the queue by reference.
clear() Removes all elements from the priority queue.
dequeue() Removes and returns the highest priority value.
enqueue(value, priority) Adds value to the queue with the specified priority.
front() Returns the first value in the queue by reference.
isEmpty() Returns true if the priority queue contains no elements.
peek() Returns the value of highest priority in the queue, without removing it.
peekPriority() Returns the priority of the first element in the queue, without removing it.
size() Returns the number of values in the priority queue.
toString() Converts the queue to a printable string representation.

Constructor detail


PriorityQueue();
Initializes a new priority queue, which is initially empty.

Usage:

PriorityQueue<ValueType> pq;

Method detail


int size() const;
Returns the number of values in the priority queue.

Usage:

int n = pq.size();

bool isEmpty() const;
Returns true if the priority queue contains no elements.

Usage:

if (pq.isEmpty()) ...

void clear();
Removes all elements from the priority queue.

Usage:

pq.clear();

void enqueue(ValueType value, double priority);
Adds value to the queue with the specified priority. Lower priority numbers correspond to higher priorities, which means that all priority 1 elements are dequeued before any priority 2 elements.

Usage:

pq.enqueue(value, priority);

ValueType dequeue();
Removes and returns the highest priority value. If multiple entries in the queue have the same priority, those values are dequeued in the same order in which they were enqueued.

Usage:

ValueType first = pq.dequeue();

ValueType peek() const;
Returns the value of highest priority in the queue, without removing it.

Usage:

ValueType first = pq.peek();

double peekPriority() const;
Returns the priority of the first element in the queue, without removing it.

Usage:

double priority = pq.peekPriority();

ValueType & front();
Returns the first value in the queue by reference.

Usage:

ValueType first = pq.front();

ValueType & back();
Returns the last value in the queue by reference.

Usage:

ValueType last = pq.back();

string toString();
Converts the queue to a printable string representation.

Usage:

string str = pq.toString();